You are here

public function SearchConfigSettingsFormTest::testMultipleSearchPages in Zircon Profile 8

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

Tests multiple search pages of the same type.

File

core/modules/search/src/Tests/SearchConfigSettingsFormTest.php, line 230
Contains \Drupal\search\Tests\SearchConfigSettingsFormTest.

Class

SearchConfigSettingsFormTest
Verify the search config settings form.

Namespace

Drupal\search\Tests

Code

public function testMultipleSearchPages() {
  $this
    ->assertDefaultSearch('node_search', 'The default page is set to the installer default.');
  $search_storage = \Drupal::entityManager()
    ->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 = array();
  $edit['search_type'] = 'search_extra_type_search';
  $this
    ->drupalPostForm(NULL, $edit, t('Add new page'));
  $this
    ->assertTitle('Add new search page | Drupal');
  $first = array();
  $first['label'] = $this
    ->randomString();
  $first_id = $first['id'] = strtolower($this
    ->randomMachineName(8));
  $first['path'] = strtolower($this
    ->randomMachineName(8));
  $this
    ->drupalPostForm(NULL, $first, t('Add search page'));
  $this
    ->assertDefaultSearch($first_id, 'The default page matches the only search page.');
  $this
    ->assertRaw(t('The %label search page has been added.', array(
    '%label' => $first['label'],
  )));

  // Attempt to add a search page with an existing path.
  $edit = array();
  $edit['search_type'] = 'search_extra_type_search';
  $this
    ->drupalPostForm(NULL, $edit, t('Add new page'));
  $edit = array();
  $edit['label'] = $this
    ->randomString();
  $edit['id'] = strtolower($this
    ->randomMachineName(8));
  $edit['path'] = $first['path'];
  $this
    ->drupalPostForm(NULL, $edit, t('Add search page'));
  $this
    ->assertText(t('The search page path must be unique.'));

  // Add a second search page.
  $second = array();
  $second['label'] = $this
    ->randomString();
  $second_id = $second['id'] = strtolower($this
    ->randomMachineName(8));
  $second['path'] = strtolower($this
    ->randomMachineName(8));
  $this
    ->drupalPostForm(NULL, $second, t('Add search page'));
  $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', array(
    ':class' => 'tabs primary',
  ));
  $this
    ->assertIdentical((string) $elements[0]['href'], Url::fromRoute('search.view_' . $first_id)
    ->toString());
  $this
    ->assertIdentical((string) $elements[1]['href'], Url::fromRoute('search.view_' . $second_id)
    ->toString());

  // Switch the weight of the search pages and check the order of the tabs.
  $edit = array(
    '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', array(
    ':class' => 'tabs primary',
  ));
  $this
    ->assertIdentical((string) $elements[0]['href'], Url::fromRoute('search.view_' . $second_id)
    ->toString());
  $this
    ->assertIdentical((string) $elements[1]['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.', array(
    '%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
    ->assertResponse(200);
  $this
    ->assertNoLink(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
    ->assertResponse(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?', array(
    '%label' => $first['label'],
  )));
  $this
    ->drupalPostForm(NULL, array(), t('Delete'));
  $this
    ->assertRaw(t('The search page %label has been deleted.', array(
    '%label' => $first['label'],
  )));
  $this
    ->verifySearchPageOperations($first_id, FALSE, FALSE, FALSE, FALSE);
}