You are here

public function SearchConfigSettingsFormTest::testMultipleSearchPages in Drupal 9

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

Tests multiple search pages of the same type.

File

core/modules/search/tests/src/Functional/SearchConfigSettingsFormTest.php, line 260

Class

SearchConfigSettingsFormTest
Verify the search config settings form.

Namespace

Drupal\Tests\search\Functional

Code

public function testMultipleSearchPages() {
  $this
    ->assertDefaultSearch('node_search', 'The default page is set to the installer default.');
  $search_storage = \Drupal::entityTypeManager()
    ->getStorage('search_page');
  $entities = $search_storage
    ->loadMultiple();
  $search_storage
    ->delete($entities);
  $this
    ->assertDefaultSearch(FALSE);

  // Ensure that no search pages are configured.
  $this
    ->drupalGet('admin/config/search/pages');
  $this
    ->assertSession()
    ->pageTextContains('No search pages have been configured.');

  // Add a search page.
  $edit = [];
  $edit['search_type'] = 'search_extra_type_search';
  $this
    ->submitForm($edit, 'Add search page');
  $this
    ->assertSession()
    ->titleEquals('Add new search page | Drupal');
  $first = [];
  $first['label'] = $this
    ->randomString();
  $first_id = $first['id'] = strtolower($this
    ->randomMachineName(8));
  $first['path'] = strtolower($this
    ->randomMachineName(8));
  $this
    ->submitForm($first, 'Save');
  $this
    ->assertDefaultSearch($first_id, 'The default page matches the only search page.');
  $this
    ->assertSession()
    ->pageTextContains("The {$first['label']} search page has been added.");

  // Attempt to add a search page with an existing path.
  $edit = [];
  $edit['search_type'] = 'search_extra_type_search';
  $this
    ->submitForm($edit, 'Add search page');
  $edit = [];
  $edit['label'] = $this
    ->randomString();
  $edit['id'] = strtolower($this
    ->randomMachineName(8));
  $edit['path'] = $first['path'];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('The search page path must be unique.');

  // Add a second search page.
  $second = [];
  $second['label'] = $this
    ->randomString();
  $second_id = $second['id'] = strtolower($this
    ->randomMachineName(8));
  $second['path'] = strtolower($this
    ->randomMachineName(8));
  $this
    ->submitForm($second, 'Save');
  $this
    ->assertDefaultSearch($first_id, 'The default page matches the only search page.');

  // Ensure both search pages have their tabs displayed.
  $this
    ->drupalGet('search');
  $elements = $this
    ->xpath('//*[contains(@class, :class)]//a', [
    ':class' => 'tabs primary',
  ]);
  $this
    ->assertSame(Url::fromRoute('search.view_' . $first_id)
    ->toString(), $elements[0]
    ->getAttribute('href'));
  $this
    ->assertSame(Url::fromRoute('search.view_' . $second_id)
    ->toString(), $elements[1]
    ->getAttribute('href'));

  // Switch the weight of the search pages and check the order of the tabs.
  $edit = [
    'entities[' . $first_id . '][weight]' => 10,
    'entities[' . $second_id . '][weight]' => -10,
  ];
  $this
    ->drupalGet('admin/config/search/pages');
  $this
    ->submitForm($edit, 'Save configuration');
  $this
    ->drupalGet('search');
  $elements = $this
    ->xpath('//*[contains(@class, :class)]//a', [
    ':class' => 'tabs primary',
  ]);
  $this
    ->assertSame(Url::fromRoute('search.view_' . $second_id)
    ->toString(), $elements[0]
    ->getAttribute('href'));
  $this
    ->assertSame(Url::fromRoute('search.view_' . $first_id)
    ->toString(), $elements[1]
    ->getAttribute('href'));

  // Check the initial state of the search pages.
  $this
    ->drupalGet('admin/config/search/pages');
  $this
    ->verifySearchPageOperations($first_id, TRUE, FALSE, FALSE, FALSE);
  $this
    ->verifySearchPageOperations($second_id, TRUE, TRUE, TRUE, FALSE);

  // Change the default search page.
  $this
    ->clickLink('Set as default');
  $this
    ->assertSession()
    ->pageTextContains("The default search page is now {$second['label']}. Be sure to check the ordering of your search pages.");
  $this
    ->verifySearchPageOperations($first_id, TRUE, TRUE, TRUE, FALSE);
  $this
    ->verifySearchPageOperations($second_id, TRUE, FALSE, FALSE, FALSE);

  // Disable the first search page.
  $this
    ->clickLink('Disable');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->linkNotExists('Disable');
  $this
    ->verifySearchPageOperations($first_id, TRUE, TRUE, FALSE, TRUE);
  $this
    ->verifySearchPageOperations($second_id, TRUE, FALSE, FALSE, FALSE);

  // Enable the first search page.
  $this
    ->clickLink('Enable');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->verifySearchPageOperations($first_id, TRUE, TRUE, TRUE, FALSE);
  $this
    ->verifySearchPageOperations($second_id, TRUE, FALSE, FALSE, FALSE);

  // Test deleting.
  $this
    ->clickLink('Delete');
  $this
    ->assertSession()
    ->pageTextContains("Are you sure you want to delete the search page {$first['label']}?");
  $this
    ->submitForm([], 'Delete');
  $this
    ->assertSession()
    ->pageTextContains("The search page {$first['label']} has been deleted.");
  $this
    ->verifySearchPageOperations($first_id, FALSE, FALSE, FALSE, FALSE);
}