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