Chris@0: drupalCreateUser(['access content', 'search content', 'use advanced search', 'administer nodes', 'administer languages', 'access administration pages', 'administer site configuration']); Chris@0: $this->drupalLogin($test_user); Chris@0: Chris@0: // Add a new language. Chris@0: ConfigurableLanguage::createFromLangcode('es')->save(); Chris@0: Chris@0: // Make the body field translatable. The title is already translatable by Chris@0: // definition. The parent class has already created the article and page Chris@0: // content types. Chris@0: $field_storage = FieldStorageConfig::loadByName('node', 'body'); Chris@0: $field_storage->setTranslatable(TRUE); Chris@0: $field_storage->save(); Chris@0: Chris@0: // Create a few page nodes with multilingual body values. Chris@0: $default_format = filter_default_format(); Chris@0: $nodes = [ Chris@0: [ Chris@0: 'title' => 'First node en', Chris@0: 'type' => 'page', Chris@0: 'body' => [['value' => $this->randomMachineName(32), 'format' => $default_format]], Chris@0: 'langcode' => 'en', Chris@0: ], Chris@0: [ Chris@0: 'title' => 'Second node this is the Spanish title', Chris@0: 'type' => 'page', Chris@0: 'body' => [['value' => $this->randomMachineName(32), 'format' => $default_format]], Chris@0: 'langcode' => 'es', Chris@0: ], Chris@0: [ Chris@0: 'title' => 'Third node en', Chris@0: 'type' => 'page', Chris@0: 'body' => [['value' => $this->randomMachineName(32), 'format' => $default_format]], Chris@0: 'langcode' => 'en', Chris@0: ], Chris@0: ]; Chris@0: $this->searchableNodes = []; Chris@0: foreach ($nodes as $setting) { Chris@0: $this->searchableNodes[] = $this->drupalCreateNode($setting); Chris@0: } Chris@0: Chris@0: // Add English translation to the second node. Chris@0: $translation = $this->searchableNodes[1]->addTranslation('en', ['title' => 'Second node en']); Chris@0: $translation->body->value = $this->randomMachineName(32); Chris@0: $this->searchableNodes[1]->save(); Chris@0: Chris@0: // Add Spanish translation to the third node. Chris@0: $translation = $this->searchableNodes[2]->addTranslation('es', ['title' => 'Third node es']); Chris@0: $translation->body->value = $this->randomMachineName(32); Chris@0: $this->searchableNodes[2]->save(); Chris@0: Chris@0: // Update the index and then run the shutdown method. Chris@0: $plugin = $this->container->get('plugin.manager.search')->createInstance('node_search'); Chris@0: $plugin->updateIndex(); Chris@0: search_update_totals(); Chris@0: } Chris@0: Chris@0: public function testLanguages() { Chris@0: // Add predefined language. Chris@0: $edit = ['predefined_langcode' => 'fr']; Chris@0: $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language')); Chris@0: $this->assertText('French', 'Language added successfully.'); Chris@0: Chris@0: // Now we should have languages displayed. Chris@0: $this->drupalGet('search/node'); Chris@0: $this->assertText(t('Languages'), 'Languages displayed to choose from.'); Chris@0: $this->assertText(t('English'), 'English is a possible choice.'); Chris@0: $this->assertText(t('French'), 'French is a possible choice.'); Chris@0: Chris@0: // Ensure selecting no language does not make the query different. Chris@0: $this->drupalPostForm('search/node', [], t('Advanced search')); Chris@0: $this->assertUrl(\Drupal::url('search.view_node_search', [], ['query' => ['keys' => ''], 'absolute' => TRUE]), [], 'Correct page redirection, no language filtering.'); Chris@0: Chris@0: // Pick French and ensure it is selected. Chris@0: $edit = ['language[fr]' => TRUE]; Chris@0: $this->drupalPostForm('search/node', $edit, t('Advanced search')); Chris@0: // Get the redirected URL. Chris@0: $url = $this->getUrl(); Chris@0: $parts = parse_url($url); Chris@0: $query_string = isset($parts['query']) ? rawurldecode($parts['query']) : ''; Chris@0: $this->assertTrue(strpos($query_string, '=language:fr') !== FALSE, 'Language filter language:fr add to the query string.'); Chris@0: Chris@0: // Search for keyword node and language filter as Spanish. Chris@0: $edit = ['keys' => 'node', 'language[es]' => TRUE]; Chris@0: $this->drupalPostForm('search/node', $edit, t('Advanced search')); Chris@0: // Check for Spanish results. Chris@0: $this->assertLink('Second node this is the Spanish title', 0, 'Second node Spanish title found in search results'); Chris@0: $this->assertLink('Third node es', 0, 'Third node Spanish found in search results'); Chris@0: // Ensure that results don't contain other language nodes. Chris@0: $this->assertNoLink('First node en', 'Search results do not contain first English node'); Chris@0: $this->assertNoLink('Second node en', 'Search results do not contain second English node'); Chris@0: $this->assertNoLink('Third node en', 'Search results do not contain third English node'); Chris@0: Chris@0: // Change the default language and delete English. Chris@0: $path = 'admin/config/regional/language'; Chris@0: $this->drupalGet($path); Chris@0: $this->assertFieldChecked('edit-site-default-language-en', 'Default language updated.'); Chris@0: $edit = [ Chris@0: 'site_default_language' => 'fr', Chris@0: ]; Chris@0: $this->drupalPostForm($path, $edit, t('Save configuration')); Chris@0: $this->assertNoFieldChecked('edit-site-default-language-en', 'Default language updated.'); Chris@0: $this->drupalPostForm('admin/config/regional/language/delete/en', [], t('Delete')); Chris@0: } Chris@0: Chris@0: }