You are here

public function SearchConfigSettingsFormTest::testSearchModuleDisabling 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::testSearchModuleDisabling()
  2. 10 core/modules/search/tests/src/Functional/SearchConfigSettingsFormTest.php \Drupal\Tests\search\Functional\SearchConfigSettingsFormTest::testSearchModuleDisabling()

Verifies that you can disable individual search plugins.

File

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

Class

SearchConfigSettingsFormTest
Verify the search config settings form.

Namespace

Drupal\Tests\search\Functional

Code

public function testSearchModuleDisabling() {

  // Array of search plugins to test: 'keys' are the keywords to search for,
  // and 'text' is the text to assert is on the results page.
  $plugin_info = [
    'node_search' => [
      'keys' => 'pizza',
      'text' => $this->searchNode
        ->label(),
    ],
    'user_search' => [
      'keys' => $this->searchUser
        ->getAccountName(),
      'text' => $this->searchUser
        ->getEmail(),
    ],
    'dummy_search_type' => [
      'keys' => 'foo',
      'text' => 'Dummy search snippet to display',
    ],
  ];
  $plugins = array_keys($plugin_info);

  /** @var $entities \Drupal\search\SearchPageInterface[] */
  $entities = SearchPage::loadMultiple();

  // Disable all of the search pages.
  foreach ($entities as $entity) {
    $entity
      ->disable()
      ->save();
  }

  // Test each plugin if it's enabled as the only search plugin.
  foreach ($entities as $entity_id => $entity) {
    $this
      ->setDefaultThroughUi($entity_id);

    // Run a search from the correct search URL.
    $info = $plugin_info[$entity_id];
    $this
      ->drupalGet('search/' . $entity
      ->getPath(), [
      'query' => [
        'keys' => $info['keys'],
      ],
    ]);
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $this
      ->assertNoText('no results', $entity
      ->label() . ' search found results');
    $this
      ->assertText($info['text'], 'Correct search text found');

    // Verify that other plugin search tab labels are not visible.
    foreach ($plugins as $other) {
      if ($other != $entity_id) {
        $label = $entities[$other]
          ->label();
        $this
          ->assertNoText($label, $label . ' search tab is not shown');
      }
    }

    // Run a search from the search block on the node page. Verify you get
    // to this plugin's search results page.
    $terms = [
      'keys' => $info['keys'],
    ];
    $this
      ->drupalPostForm('node', $terms, t('Search'));
    $current = $this
      ->getURL();
    $expected = Url::fromRoute('search.view_' . $entity
      ->id(), [], [
      'query' => [
        'keys' => $info['keys'],
      ],
      'absolute' => TRUE,
    ])
      ->toString();
    $this
      ->assertEqual($current, $expected, 'Block redirected to right search page');

    // Try an invalid search path, which should 404.
    $this
      ->drupalGet('search/not_a_plugin_path');
    $this
      ->assertSession()
      ->statusCodeEquals(404);
    $entity
      ->disable()
      ->save();
  }

  // Set the node search as default.
  $this
    ->setDefaultThroughUi('node_search');

  // Test with all search plugins enabled. When you go to the search
  // page or run search, all plugins should be shown.
  foreach ($entities as $entity) {
    $entity
      ->enable()
      ->save();
  }
  \Drupal::service('router.builder')
    ->rebuild();
  $paths = [
    [
      'path' => 'search/node',
      'options' => [
        'query' => [
          'keys' => 'pizza',
        ],
      ],
    ],
    [
      'path' => 'search/node',
      'options' => [],
    ],
  ];
  foreach ($paths as $item) {
    $this
      ->drupalGet($item['path'], $item['options']);
    foreach ($plugins as $entity_id) {
      $label = $entities[$entity_id]
        ->label();
      $this
        ->assertText($label, new FormattableMarkup('%label search tab is shown', [
        '%label' => $label,
      ]));
    }
  }
}