function SearchConfigSettingsFormTest::testSearchModuleDisabling

Same name in other branches
  1. 8.9.x 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()
  3. 11.x 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 164

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 \Drupal\search\SearchPageInterface[] $entities */
    $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->assertSession()
            ->pageTextNotContains('no results');
        $this->assertSession()
            ->pageTextContains($info['text']);
        // Verify that other plugin search tab labels are not visible.
        foreach ($plugins as $other) {
            if ($other != $entity_id) {
                $path = 'search/' . $entities[$other]->getPath();
                $this->assertSession()
                    ->elementNotExists('xpath', '//div[@id="block-local-tasks"]//li/a[@data-drupal-link-system-path="' . $path . '"]');
            }
        }
        // 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->drupalGet('node');
        $this->submitForm($terms, 'Search');
        $current = $this->getURL();
        $expected = Url::fromRoute('search.view_' . $entity->id(), [], [
            'query' => [
                'keys' => $info['keys'],
            ],
            'absolute' => TRUE,
        ])
            ->toString();
        $this->assertEquals($expected, $current, '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) {
            $path = 'search/' . $entities[$entity_id]->getPath();
            $label = $entities[$entity_id]->label();
            $this->assertSession()
                ->elementTextContains('xpath', '//div[@id="block-local-tasks"]//li/a[@data-drupal-link-system-path="' . $path . '"]', $label);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.