annotate core/modules/search/tests/src/Functional/SearchDateIntervalTest.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\search\Functional;
Chris@0 4
Chris@0 5 use Drupal\language\Entity\ConfigurableLanguage;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Tests searching with date filters that exclude some translations.
Chris@0 9 *
Chris@0 10 * @group search
Chris@0 11 */
Chris@0 12 class SearchDateIntervalTest extends SearchTestBase {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Modules to enable.
Chris@0 16 *
Chris@0 17 * @var string[]
Chris@0 18 */
Chris@0 19 public static $modules = ['language', 'search_date_query_alter'];
Chris@0 20
Chris@0 21 protected function setUp() {
Chris@0 22 parent::setUp();
Chris@0 23
Chris@0 24 // Create and log in user.
Chris@0 25 $test_user = $this->drupalCreateUser(['access content', 'search content', 'use advanced search', 'administer nodes', 'administer languages', 'access administration pages', 'administer site configuration']);
Chris@0 26 $this->drupalLogin($test_user);
Chris@0 27
Chris@0 28 // Add a new language.
Chris@0 29 ConfigurableLanguage::createFromLangcode('es')->save();
Chris@0 30
Chris@0 31 // Set up times to be applied to the English and Spanish translations of the
Chris@0 32 // node create time, so that they are filtered in/out in the
Chris@0 33 // search_date_query_alter test module.
Chris@0 34 $created_time_en = new \DateTime('February 10 2016 10PM');
Chris@0 35 $created_time_es = new \DateTime('March 19 2016 10PM');
Chris@0 36 $default_format = filter_default_format();
Chris@0 37
Chris@0 38 $node = $this->drupalCreateNode([
Chris@0 39 'title' => 'Node EN',
Chris@0 40 'type' => 'page',
Chris@0 41 'body' => [
Chris@0 42 'value' => $this->randomMachineName(32),
Chris@0 43 'format' => $default_format,
Chris@0 44 ],
Chris@0 45 'langcode' => 'en',
Chris@0 46 'created' => $created_time_en->getTimestamp(),
Chris@0 47 ]);
Chris@0 48
Chris@0 49 // Add Spanish translation to the node.
Chris@0 50 $translation = $node->addTranslation('es', ['title' => 'Node ES']);
Chris@0 51 $translation->body->value = $this->randomMachineName(32);
Chris@0 52 $translation->created->value = $created_time_es->getTimestamp();
Chris@0 53 $node->save();
Chris@0 54
Chris@0 55 // Update the index.
Chris@0 56 $plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
Chris@0 57 $plugin->updateIndex();
Chris@0 58 search_update_totals();
Chris@0 59 }
Chris@0 60
Chris@0 61 /**
Chris@0 62 * Tests searching with date filters that exclude some translations.
Chris@0 63 */
Chris@0 64 public function testDateIntervalQueryAlter() {
Chris@0 65 // Search for keyword node.
Chris@0 66 $edit = ['keys' => 'node'];
Chris@0 67 $this->drupalPostForm('search/node', $edit, t('Search'));
Chris@0 68
Chris@0 69 // The nodes must have the same node ID but the created date is different.
Chris@0 70 // So only the Spanish translation must appear.
Chris@0 71 $this->assertLink('Node ES', 0, 'Spanish translation found in search results');
Chris@0 72 $this->assertNoLink('Node EN', 'Search results do not contain English node');
Chris@0 73 }
Chris@0 74
Chris@0 75 }