function SearchNodePunctuationTest::testPhraseSearchPunctuation

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

Tests that search works with punctuation and HTML entities.

File

core/modules/search/tests/src/Functional/SearchNodePunctuationTest.php, line 55

Class

SearchNodePunctuationTest
Tests search functionality with punctuation and HTML entities.

Namespace

Drupal\Tests\search\Functional

Code

public function testPhraseSearchPunctuation() : void {
    $node = $this->drupalCreateNode([
        'body' => [
            [
                'value' => "The bunny's ears were fluffy.",
            ],
        ],
    ]);
    // cSpell:disable-next-line
    $this->drupalCreateNode([
        'body' => [
            [
                'value' => 'Dignissim Aliquam & Quieligo meus natu quae quia te. Damnum© erat— neo pneum. Facilisi feugiat ibidem ratis.',
            ],
        ],
    ]);
    // Update the search index.
    $this->container
        ->get('plugin.manager.search')
        ->createInstance('node_search')
        ->updateIndex();
    // Refresh variables after the treatment.
    $this->refreshVariables();
    // Submit a phrase wrapped in double quotes to include the punctuation.
    $edit = [
        'keys' => '"bunny\'s"',
    ];
    $this->drupalGet('search/node');
    $this->submitForm($edit, 'Search');
    $this->assertSession()
        ->pageTextContains($node->label());
    // Check if the author is linked correctly to the user profile page.
    $username = $node->getOwner()
        ->getAccountName();
    $this->assertSession()
        ->linkExists($username);
    // Search for "&" and verify entities are not broken up in the output.
    $edit = [
        'keys' => '&',
    ];
    $this->drupalGet('search/node');
    $this->submitForm($edit, 'Search');
    $this->assertSession()
        ->responseNotContains('<strong>&</strong>amp;');
    $this->assertSession()
        ->statusMessageContains('You must include at least one keyword', 'warning');
    $edit = [
        'keys' => '&amp;',
    ];
    $this->drupalGet('search/node');
    $this->submitForm($edit, 'Search');
    $this->assertSession()
        ->responseNotContains('<strong>&</strong>amp;');
    $this->assertSession()
        ->statusMessageContains('You must include at least one keyword', 'warning');
}

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