annotate core/modules/search/tests/src/Functional/SearchPreprocessLangcodeTest.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents
children
rev   line source
Chris@4 1 <?php
Chris@4 2
Chris@4 3 namespace Drupal\Tests\search\Functional;
Chris@4 4
Chris@4 5 use Drupal\Tests\BrowserTestBase;
Chris@4 6
Chris@4 7 /**
Chris@4 8 * Tests that the search preprocessing uses the correct language code.
Chris@4 9 *
Chris@4 10 * @group search
Chris@4 11 */
Chris@4 12 class SearchPreprocessLangcodeTest extends BrowserTestBase {
Chris@4 13
Chris@4 14 /**
Chris@4 15 * {@inheritdoc}
Chris@4 16 */
Chris@4 17 protected static $modules = ['node', 'search', 'search_langcode_test'];
Chris@4 18
Chris@4 19 /**
Chris@4 20 * Test node for searching.
Chris@4 21 *
Chris@4 22 * @var \Drupal\node\NodeInterface
Chris@4 23 */
Chris@4 24 protected $node;
Chris@4 25
Chris@4 26 protected function setUp() {
Chris@4 27 parent::setUp();
Chris@4 28
Chris@4 29 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
Chris@4 30
Chris@4 31 $web_user = $this->drupalCreateUser([
Chris@4 32 'create page content',
Chris@4 33 'edit own page content',
Chris@4 34 'search content',
Chris@4 35 'use advanced search',
Chris@4 36 ]);
Chris@4 37 $this->drupalLogin($web_user);
Chris@4 38 }
Chris@4 39
Chris@4 40 /**
Chris@4 41 * Tests that hook_search_preprocess() returns the correct langcode.
Chris@4 42 */
Chris@4 43 public function testPreprocessLangcode() {
Chris@4 44 // Create a node.
Chris@4 45 $this->node = $this->drupalCreateNode(['body' => [[]], 'langcode' => 'en']);
Chris@4 46
Chris@4 47 // First update the index. This does the initial processing.
Chris@4 48 $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
Chris@4 49
Chris@4 50 // Then, run the shutdown function. Testing is a unique case where indexing
Chris@4 51 // and searching has to happen in the same request, so running the shutdown
Chris@4 52 // function manually is needed to finish the indexing process.
Chris@4 53 search_update_totals();
Chris@4 54
Chris@4 55 // Search for the additional text that is added by the preprocess
Chris@4 56 // function. If you search for text that is in the node, preprocess is
Chris@4 57 // not invoked on the node during the search excerpt generation.
Chris@4 58 $edit = ['or' => 'Additional text'];
Chris@4 59 $this->drupalPostForm('search/node', $edit, 'edit-submit--2');
Chris@4 60
Chris@4 61 // Checks if the langcode message has been set by hook_search_preprocess().
Chris@4 62 $this->assertText('Langcode Preprocess Test: en');
Chris@4 63 }
Chris@4 64
Chris@4 65 /**
Chris@4 66 * Tests stemming for hook_search_preprocess().
Chris@4 67 */
Chris@4 68 public function testPreprocessStemming() {
Chris@4 69 // Create a node.
Chris@4 70 $this->node = $this->drupalCreateNode([
Chris@4 71 'title' => 'we are testing',
Chris@4 72 'body' => [[]],
Chris@4 73 'langcode' => 'en',
Chris@4 74 ]);
Chris@4 75
Chris@4 76 // First update the index. This does the initial processing.
Chris@4 77 $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
Chris@4 78
Chris@4 79 // Then, run the shutdown function. Testing is a unique case where indexing
Chris@4 80 // and searching has to happen in the same request, so running the shutdown
Chris@4 81 // function manually is needed to finish the indexing process.
Chris@4 82 search_update_totals();
Chris@4 83
Chris@4 84 // Search for the title of the node with a POST query.
Chris@4 85 $edit = ['or' => 'testing'];
Chris@4 86 $this->drupalPostForm('search/node', $edit, 'edit-submit--2');
Chris@4 87
Chris@4 88 // Check if the node has been found.
Chris@4 89 $this->assertText('Search results');
Chris@4 90 $this->assertText('we are testing');
Chris@4 91
Chris@4 92 // Search for the same node using a different query.
Chris@4 93 $edit = ['or' => 'test'];
Chris@4 94 $this->drupalPostForm('search/node', $edit, 'edit-submit--2');
Chris@4 95
Chris@4 96 // Check if the node has been found.
Chris@4 97 $this->assertText('Search results');
Chris@4 98 $this->assertText('we are testing');
Chris@4 99 }
Chris@4 100
Chris@4 101 }