function SearchRankingTest::testHTMLRankings

Same name in other branches
  1. 9 core/modules/search/tests/src/Functional/SearchRankingTest.php \Drupal\Tests\search\Functional\SearchRankingTest::testHTMLRankings()
  2. 10 core/modules/search/tests/src/Functional/SearchRankingTest.php \Drupal\Tests\search\Functional\SearchRankingTest::testHTMLRankings()
  3. 11.x core/modules/search/tests/src/Functional/SearchRankingTest.php \Drupal\Tests\search\Functional\SearchRankingTest::testHTMLRankings()

Test rankings of HTML tags.

File

core/modules/search/tests/src/Functional/SearchRankingTest.php, line 221

Class

SearchRankingTest
Indexes content and tests ranking factors.

Namespace

Drupal\Tests\search\Functional

Code

public function testHTMLRankings() {
    $full_html_format = FilterFormat::create([
        'format' => 'full_html',
        'name' => 'Full HTML',
    ]);
    $full_html_format->save();
    // Test HTML tags with different weights.
    $sorted_tags = [
        'h1',
        'h2',
        'h3',
        'h4',
        'a',
        'h5',
        'h6',
        'notag',
    ];
    $shuffled_tags = $sorted_tags;
    // Shuffle tags to ensure HTML tags are ranked properly.
    shuffle($shuffled_tags);
    $settings = [
        'type' => 'page',
        'title' => 'Simple node',
    ];
    $nodes = [];
    foreach ($shuffled_tags as $tag) {
        switch ($tag) {
            case 'a':
                $settings['body'] = [
                    [
                        'value' => Link::fromTextAndUrl('Drupal Rocks', Url::fromRoute('<front>'))->toString(),
                        'format' => 'full_html',
                    ],
                ];
                break;
            case 'notag':
                $settings['body'] = [
                    [
                        'value' => 'Drupal Rocks',
                    ],
                ];
                break;
            default:
                $settings['body'] = [
                    [
                        'value' => "<{$tag}>Drupal Rocks</{$tag}>",
                        'format' => 'full_html',
                    ],
                ];
                break;
        }
        $nodes[$tag] = $this->drupalCreateNode($settings);
    }
    // Update the search index.
    $this->nodeSearch
        ->getPlugin()
        ->updateIndex();
    $search_index = \Drupal::service('search.index');
    assert($search_index instanceof SearchIndexInterface);
    $this->nodeSearch
        ->getPlugin()
        ->setSearch('rocks', [], []);
    // Do the search and assert the results.
    $set = $this->nodeSearch
        ->getPlugin()
        ->execute();
    // Test the ranking of each tag.
    foreach ($sorted_tags as $tag_rank => $tag) {
        // Assert the results.
        if ($tag == 'notag') {
            $this->assertEqual($set[$tag_rank]['node']->id(), $nodes[$tag]->id(), 'Search tag ranking for plain text order.');
        }
        else {
            $this->assertEqual($set[$tag_rank]['node']->id(), $nodes[$tag]->id(), 'Search tag ranking for "&lt;' . $sorted_tags[$tag_rank] . '&gt;" order.');
        }
    }
    // Test tags with the same weight against the sorted tags.
    $unsorted_tags = [
        'u',
        'b',
        'i',
        'strong',
        'em',
    ];
    foreach ($unsorted_tags as $tag) {
        $settings['body'] = [
            [
                'value' => "<{$tag}>Drupal Rocks</{$tag}>",
                'format' => 'full_html',
            ],
        ];
        $node = $this->drupalCreateNode($settings);
        // Update the search index.
        $this->nodeSearch
            ->getPlugin()
            ->updateIndex();
        $this->nodeSearch
            ->getPlugin()
            ->setSearch('rocks', [], []);
        // Do the search and assert the results.
        $set = $this->nodeSearch
            ->getPlugin()
            ->execute();
        // Ranking should always be second to last.
        $set = array_slice($set, -2, 1);
        // Assert the results.
        $this->assertEqual($set[0]['node']->id(), $node->id(), 'Search tag ranking for "&lt;' . $tag . '&gt;" order.');
        // Delete node so it doesn't show up in subsequent search results.
        $node->delete();
    }
}

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