You are here

public function SearchLanguageTest::testLanguages in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/search/tests/src/Functional/SearchLanguageTest.php \Drupal\Tests\search\Functional\SearchLanguageTest::testLanguages()

File

core/modules/search/tests/src/Functional/SearchLanguageTest.php, line 103

Class

SearchLanguageTest
Tests advanced search with different languages added.

Namespace

Drupal\Tests\search\Functional

Code

public function testLanguages() {

  // Add predefined language.
  $edit = [
    'predefined_langcode' => 'fr',
  ];
  $this
    ->drupalGet('admin/config/regional/language/add');
  $this
    ->submitForm($edit, 'Add language');
  $this
    ->assertSession()
    ->pageTextContains('French');

  // Now we should have languages displayed.
  $this
    ->drupalGet('search/node');
  $this
    ->assertSession()
    ->pageTextContains('Languages');
  $this
    ->assertSession()
    ->pageTextContains('English');
  $this
    ->assertSession()
    ->pageTextContains('French');

  // Ensure selecting no language does not make the query different.
  $this
    ->drupalGet('search/node');
  $this
    ->submitForm([], 'edit-submit--2');
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('search.view_node_search', [], [
    'query' => [
      'keys' => '',
    ],
  ]));

  // Pick French and ensure it is selected.
  $edit = [
    'language[fr]' => TRUE,
  ];
  $this
    ->drupalGet('search/node');
  $this
    ->submitForm($edit, 'edit-submit--2');

  // Get the redirected URL.
  $url = $this
    ->getUrl();
  $parts = parse_url($url);
  $query_string = isset($parts['query']) ? rawurldecode($parts['query']) : '';
  $this
    ->assertStringContainsString('=language:fr', $query_string, 'Language filter language:fr add to the query string.');

  // Search for keyword node and language filter as Spanish.
  $edit = [
    'keys' => 'node',
    'language[es]' => TRUE,
  ];
  $this
    ->drupalGet('search/node');
  $this
    ->submitForm($edit, 'edit-submit--2');

  // Check for Spanish results.
  $this
    ->assertSession()
    ->linkExists('Second node this is the Spanish title', 0, 'Second node Spanish title found in search results');
  $this
    ->assertSession()
    ->linkExists('Third node es', 0, 'Third node Spanish found in search results');

  // Ensure that results don't contain other language nodes.
  $this
    ->assertSession()
    ->linkNotExists('First node en', 'Search results do not contain first English node');
  $this
    ->assertSession()
    ->linkNotExists('Second node en', 'Search results do not contain second English node');
  $this
    ->assertSession()
    ->linkNotExists('Third node en', 'Search results do not contain third English node');

  // Change the default language and delete English.
  $path = 'admin/config/regional/language';
  $this
    ->drupalGet($path);
  $this
    ->assertSession()
    ->checkboxChecked('edit-site-default-language-en');
  $edit = [
    'site_default_language' => 'fr',
  ];
  $this
    ->drupalGet($path);
  $this
    ->submitForm($edit, 'Save configuration');
  $this
    ->assertSession()
    ->checkboxNotChecked('edit-site-default-language-en');
  $this
    ->drupalGet('admin/config/regional/language/delete/en');
  $this
    ->submitForm([], 'Delete');
}