You are here

public function SearchConfigSettingsFormTest::testMultipleSearchPages in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/search/tests/src/Functional/SearchConfigSettingsFormTest.php \Drupal\Tests\search\Functional\SearchConfigSettingsFormTest::testMultipleSearchPages()
  2. 10 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 251

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
    ->assertText(t('No search pages have been configured.'));

  // Add a search page.
  $edit = [];
  $edit['search_type'] = 'search_extra_type_search';
  $this
    ->drupalPostForm(NULL, $edit, t('Add search page'));
  $this
    ->assertTitle('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
    ->drupalPostForm(NULL, $first, t('Save'));
  $this
    ->assertDefaultSearch($first_id, 'The default page matches the only search page.');
  $this
    ->assertRaw(t('The %label search page has been added.', [
    '%label' => $first['label'],
  ]));

  // Attempt to add a search page with an existing path.
  $edit = [];
  $edit['search_type'] = 'search_extra_type_search';
  $this
    ->drupalPostForm(NULL, $edit, t('Add search page'));
  $edit = [];
  $edit['label'] = $this
    ->randomString();
  $edit['id'] = strtolower($this
    ->randomMachineName(8));
  $edit['path'] = $first['path'];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText(t('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
    ->drupalPostForm(NULL, $second, t('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
    ->assertIdentical($elements[0]
    ->getAttribute('href'), Url::fromRoute('search.view_' . $first_id)
    ->toString());
  $this
    ->assertIdentical($elements[1]
    ->getAttribute('href'), Url::fromRoute('search.view_' . $second_id)
    ->toString());

  // 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
    ->drupalPostForm('admin/config/search/pages', $edit, t('Save configuration'));
  $this
    ->drupalGet('search');
  $elements = $this
    ->xpath('//*[contains(@class, :class)]//a', [
    ':class' => 'tabs primary',
  ]);
  $this
    ->assertIdentical($elements[0]
    ->getAttribute('href'), Url::fromRoute('search.view_' . $second_id)
    ->toString());
  $this
    ->assertIdentical($elements[1]
    ->getAttribute('href'), Url::fromRoute('search.view_' . $first_id)
    ->toString());

  // 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(t('Set as default'));
  $this
    ->assertRaw(t('The default search page is now %label. Be sure to check the ordering of your search pages.', [
    '%label' => $second['label'],
  ]));
  $this
    ->verifySearchPageOperations($first_id, TRUE, TRUE, TRUE, FALSE);
  $this
    ->verifySearchPageOperations($second_id, TRUE, FALSE, FALSE, FALSE);

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

  // Enable the first search page.
  $this
    ->clickLink(t('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(t('Delete'));
  $this
    ->assertRaw(t('Are you sure you want to delete the search page %label?', [
    '%label' => $first['label'],
  ]));
  $this
    ->drupalPostForm(NULL, [], t('Delete'));
  $this
    ->assertRaw(t('The search page %label has been deleted.', [
    '%label' => $first['label'],
  ]));
  $this
    ->verifySearchPageOperations($first_id, FALSE, FALSE, FALSE, FALSE);
}