3 namespace Drupal\Tests\porterstemmer\Functional;
5 use Drupal\search\Tests\SearchTestBase;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\language\Entity\ConfigurableLanguage;
11 * Tests that EN language search terms are stemmed and return stemmed content.
13 * @group porterstemmer
15 class LangCodeTest extends SearchTestBase {
22 public static $modules = array('search', 'porterstemmer', 'language');
25 * A user with permission to administer nodes.
27 * @var \Drupal\user\UserInterface
32 * An array of content for testing purposes.
36 protected $test_data = array(
37 'First Page' => 'I walk through the streets, looking around for trouble.',
38 'Second Page' => 'I walked home from work today.',
39 'Third Page' => 'I am always walking everywhere.',
43 * An array of search terms.
47 protected $searches = array(
54 * An array of nodes created for testing purposes.
56 * @var \Drupal\node\NodeInterface[]
63 protected function setUp() {
66 $this->testUser = $this->drupalCreateUser(array(
70 'access site reports',
71 'use advanced search',
72 'administer languages',
73 'access administration pages',
74 'administer site configuration',
76 $this->drupalLogin($this->testUser);
78 // Add a new language.
79 ConfigurableLanguage::createFromLangcode('fr')->save();
81 // Make the body field translatable. The title is already translatable by
83 $field_storage = FieldStorageConfig::loadByName('node', 'body');
84 $field_storage->setTranslatable(TRUE);
85 $field_storage->save();
87 // Create EN language nodes.
88 foreach ($this->test_data as $title => $body) {
90 'title' => $title . ' (EN)',
91 'body' => array(array('value' => $body)),
95 $this->nodes[$title] = $this->drupalCreateNode($info);
98 // Create non-EN nodes.
99 foreach ($this->test_data as $title => $body) {
101 'title' => $title . ' (FR)',
102 'body' => array(array('value' => $body)),
106 $this->nodes[$title] = $this->drupalCreateNode($info);
109 // Create language-unspecified nodes.
110 foreach ($this->test_data as $title => $body) {
112 'title' => $title . ' (UND)',
113 'body' => array(array('value' => $body)),
115 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
117 $this->nodes[$title] = $this->drupalCreateNode($info);
120 // Run cron to ensure the content is indexed.
122 $this->drupalGet('admin/reports/dblog');
123 $this->assertText(t('Cron run completed'), 'Log shows cron run completed');
127 * Test that search variations return English language results.
129 protected function testStemSearching() {
131 foreach ($this->searches as $search) {
132 $this->drupalPostForm('search/node', array('keys' => $search), t('Search'));
134 // Verify that all English-language test node variants show up in results.
135 foreach ($this->test_data as $title => $body) {
136 $this->assertText($title . ' (EN)', format_string('Search for %search returns English-language node with body %body', array('%search' => $search, '%body' => $body)));
139 // Check for results by language.
142 $this->assertNoText('Second Page (FR)', format_string('Search for %search does not show stemmed non-English results.', array('%search' => $search)));
143 $this->assertNoText('Second Page (UND)', format_string('Search for %search does show stemmed language-unspecified results.', array('%search' => $search)));
147 $this->assertNoText('Second Page (FR)', format_string('Search for %search does not show stemmed non-English results.', array('%search' => $search)));
148 $this->assertNoText('Second Page (UND)', format_string('Search for %search does not show stemmed language-unspecified results.', array('%search' => $search)));
152 $this->assertText('First Page (FR)', format_string('Search for %search does show matching non-English results.', array('%search' => $search)));
153 $this->assertText('First Page (UND)', format_string('Search for %search does show matching language-unspecified results.', array('%search' => $search)));