You are here

public function SearchExactTest::testExactQuery in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/search/tests/src/Functional/SearchExactTest.php \Drupal\Tests\search\Functional\SearchExactTest::testExactQuery()

Tests that the correct number of pager links are found for both keywords and phrases.

File

core/modules/search/tests/src/Functional/SearchExactTest.php, line 27

Class

SearchExactTest
Tests that searching for a phrase gets the correct page count.

Namespace

Drupal\Tests\search\Functional

Code

public function testExactQuery() {
  $this
    ->drupalCreateContentType([
    'type' => 'page',
    'name' => 'Basic page',
  ]);

  // Log in with sufficient privileges.
  $user = $this
    ->drupalCreateUser([
    'create page content',
    'search content',
  ]);
  $this
    ->drupalLogin($user);
  $settings = [
    'type' => 'page',
    'title' => 'Simple Node',
  ];

  // Create nodes with exact phrase.
  for ($i = 0; $i <= 17; $i++) {
    $settings['body'] = [
      [
        'value' => 'love pizza',
      ],
    ];
    $this
      ->drupalCreateNode($settings);
  }

  // Create nodes containing keywords.
  for ($i = 0; $i <= 17; $i++) {
    $settings['body'] = [
      [
        'value' => 'love cheesy pizza',
      ],
    ];
    $this
      ->drupalCreateNode($settings);
  }

  // Create another node and save it for later.
  $settings['body'] = [
    [
      'value' => 'Druplicon',
    ],
  ];
  $node = $this
    ->drupalCreateNode($settings);

  // Update the search index.
  $this->container
    ->get('plugin.manager.search')
    ->createInstance('node_search')
    ->updateIndex();

  // Refresh variables after the treatment.
  $this
    ->refreshVariables();

  // Test that the correct number of pager links are found for keyword search.
  $edit = [
    'keys' => 'love pizza',
  ];
  $this
    ->drupalGet('search/node');
  $this
    ->submitForm($edit, 'Search');
  $this
    ->assertSession()
    ->linkByHrefExists('page=1', 0, '2nd page link is found for keyword search.');
  $this
    ->assertSession()
    ->linkByHrefExists('page=2', 0, '3rd page link is found for keyword search.');
  $this
    ->assertSession()
    ->linkByHrefExists('page=3', 0, '4th page link is found for keyword search.');
  $this
    ->assertSession()
    ->linkByHrefNotExists('page=4', '5th page link is not found for keyword search.');

  // Test that the correct number of pager links are found for exact phrase search.
  $edit = [
    'keys' => '"love pizza"',
  ];
  $this
    ->drupalGet('search/node');
  $this
    ->submitForm($edit, 'Search');
  $this
    ->assertSession()
    ->linkByHrefExists('page=1', 0, '2nd page link is found for exact phrase search.');
  $this
    ->assertSession()
    ->linkByHrefNotExists('page=2', '3rd page link is not found for exact phrase search.');

  // Check that with post settings turned on the post information is displayed.
  $node_type_config = \Drupal::configFactory()
    ->getEditable('node.type.page');
  $node_type_config
    ->set('display_submitted', TRUE);
  $node_type_config
    ->save();
  $edit = [
    'keys' => 'Druplicon',
  ];
  $this
    ->drupalGet('search/node');
  $this
    ->submitForm($edit, 'Search');
  $this
    ->assertSession()
    ->pageTextContains($user
    ->getAccountName());
  $this
    ->assertSession()
    ->pageTextContains($this->container
    ->get('date.formatter')
    ->format($node
    ->getChangedTime(), 'short'));

  // Check that with post settings turned off the user and changed date
  // information is not displayed.
  $node_type_config
    ->set('display_submitted', FALSE);
  $node_type_config
    ->save();
  $edit = [
    'keys' => 'Druplicon',
  ];
  $this
    ->drupalGet('search/node');
  $this
    ->submitForm($edit, 'Search');
  $this
    ->assertSession()
    ->pageTextNotContains($user
    ->getAccountName());
  $this
    ->assertSession()
    ->pageTextNotContains($this->container
    ->get('date.formatter')
    ->format($node
    ->getChangedTime(), 'short'));
}