annotate core/modules/search/tests/src/Functional/SearchExactTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\search\Functional;
Chris@0 4
Chris@17 5 use Drupal\Tests\BrowserTestBase;
Chris@17 6
Chris@0 7 /**
Chris@0 8 * Tests that searching for a phrase gets the correct page count.
Chris@0 9 *
Chris@0 10 * @group search
Chris@0 11 */
Chris@17 12 class SearchExactTest extends BrowserTestBase {
Chris@17 13
Chris@17 14 /**
Chris@17 15 * {@inheritdoc}
Chris@17 16 */
Chris@17 17 protected static $modules = ['node', 'search'];
Chris@17 18
Chris@0 19 /**
Chris@0 20 * Tests that the correct number of pager links are found for both keywords and phrases.
Chris@0 21 */
Chris@0 22 public function testExactQuery() {
Chris@17 23 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
Chris@17 24
Chris@0 25 // Log in with sufficient privileges.
Chris@0 26 $user = $this->drupalCreateUser(['create page content', 'search content']);
Chris@0 27 $this->drupalLogin($user);
Chris@0 28
Chris@0 29 $settings = [
Chris@0 30 'type' => 'page',
Chris@0 31 'title' => 'Simple Node',
Chris@0 32 ];
Chris@0 33 // Create nodes with exact phrase.
Chris@0 34 for ($i = 0; $i <= 17; $i++) {
Chris@0 35 $settings['body'] = [['value' => 'love pizza']];
Chris@0 36 $this->drupalCreateNode($settings);
Chris@0 37 }
Chris@0 38 // Create nodes containing keywords.
Chris@0 39 for ($i = 0; $i <= 17; $i++) {
Chris@0 40 $settings['body'] = [['value' => 'love cheesy pizza']];
Chris@0 41 $this->drupalCreateNode($settings);
Chris@0 42 }
Chris@0 43 // Create another node and save it for later.
Chris@0 44 $settings['body'] = [['value' => 'Druplicon']];
Chris@0 45 $node = $this->drupalCreateNode($settings);
Chris@0 46
Chris@0 47 // Update the search index.
Chris@0 48 $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
Chris@0 49 search_update_totals();
Chris@0 50
Chris@0 51 // Refresh variables after the treatment.
Chris@0 52 $this->refreshVariables();
Chris@0 53
Chris@0 54 // Test that the correct number of pager links are found for keyword search.
Chris@0 55 $edit = ['keys' => 'love pizza'];
Chris@0 56 $this->drupalPostForm('search/node', $edit, t('Search'));
Chris@0 57 $this->assertLinkByHref('page=1', 0, '2nd page link is found for keyword search.');
Chris@0 58 $this->assertLinkByHref('page=2', 0, '3rd page link is found for keyword search.');
Chris@0 59 $this->assertLinkByHref('page=3', 0, '4th page link is found for keyword search.');
Chris@0 60 $this->assertNoLinkByHref('page=4', '5th page link is not found for keyword search.');
Chris@0 61
Chris@0 62 // Test that the correct number of pager links are found for exact phrase search.
Chris@0 63 $edit = ['keys' => '"love pizza"'];
Chris@0 64 $this->drupalPostForm('search/node', $edit, t('Search'));
Chris@0 65 $this->assertLinkByHref('page=1', 0, '2nd page link is found for exact phrase search.');
Chris@0 66 $this->assertNoLinkByHref('page=2', '3rd page link is not found for exact phrase search.');
Chris@0 67
Chris@0 68 // Check that with post settings turned on the post information is displayed.
Chris@0 69 $node_type_config = \Drupal::configFactory()->getEditable('node.type.page');
Chris@0 70 $node_type_config->set('display_submitted', TRUE);
Chris@0 71 $node_type_config->save();
Chris@0 72
Chris@0 73 $edit = ['keys' => 'Druplicon'];
Chris@0 74 $this->drupalPostForm('search/node', $edit, t('Search'));
Chris@18 75 $this->assertText($user->getAccountName(), 'Basic page node displays author name when post settings are on.');
Chris@18 76 $this->assertText($this->container->get('date.formatter')->format($node->getChangedTime(), 'short'), 'Basic page node displays post date when post settings are on.');
Chris@0 77
Chris@0 78 // Check that with post settings turned off the user and changed date
Chris@0 79 // information is not displayed.
Chris@0 80 $node_type_config->set('display_submitted', FALSE);
Chris@0 81 $node_type_config->save();
Chris@0 82 $edit = ['keys' => 'Druplicon'];
Chris@0 83 $this->drupalPostForm('search/node', $edit, t('Search'));
Chris@18 84 $this->assertNoText($user->getAccountName(), 'Basic page node does not display author name when post settings are off.');
Chris@18 85 $this->assertNoText($this->container->get('date.formatter')->format($node->getChangedTime(), 'short'), 'Basic page node does not display post date when post settings are off.');
Chris@0 86
Chris@0 87 }
Chris@0 88
Chris@0 89 }