Added the Porter Stemmer module to improve searches. This doesn't deal with some...
[yaffs-website] / web / modules / contrib / porterstemmer / src / Tests / LangCodeTest.php
1 <?php
2
3 namespace Drupal\Tests\porterstemmer\Functional;
4
5 use Drupal\search\Tests\SearchTestBase;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\language\Entity\ConfigurableLanguage;
9
10 /**
11  * Tests that EN language search terms are stemmed and return stemmed content.
12  *
13  * @group porterstemmer
14  */
15 class LangCodeTest extends SearchTestBase {
16
17   /**
18    * Modules to install.
19    *
20    * @var array
21    */
22   public static $modules = array('search', 'porterstemmer', 'language');
23
24   /**
25    * A user with permission to administer nodes.
26    *
27    * @var \Drupal\user\UserInterface
28    */
29   protected $testUser;
30
31   /**
32    * An array of content for testing purposes.
33    *
34    * @var string[]
35    */
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.',
40   );
41
42   /**
43    * An array of search terms.
44    *
45    * @var string[]
46    */
47   protected $searches = array(
48     'walk',
49     'walked',
50     'walking',
51   );
52
53   /**
54    * An array of nodes created for testing purposes.
55    *
56    * @var \Drupal\node\NodeInterface[]
57    */
58   protected $nodes;
59
60   /**
61    * {@inheritdoc}
62    */
63   protected function setUp() {
64     parent::setUp();
65
66     $this->testUser = $this->drupalCreateUser(array(
67       'search content',
68       'access content',
69       'administer nodes',
70       'access site reports',
71       'use advanced search',
72       'administer languages',
73       'access administration pages',
74       'administer site configuration',
75     ));
76     $this->drupalLogin($this->testUser);
77
78     // Add a new language.
79     ConfigurableLanguage::createFromLangcode('fr')->save();
80
81     // Make the body field translatable. The title is already translatable by
82     // definition.
83     $field_storage = FieldStorageConfig::loadByName('node', 'body');
84     $field_storage->setTranslatable(TRUE);
85     $field_storage->save();
86
87     // Create EN language nodes.
88     foreach ($this->test_data as $title => $body) {
89       $info = array(
90         'title' => $title . ' (EN)',
91         'body' => array(array('value' => $body)),
92         'type' => 'page',
93         'langcode' => 'en',
94       );
95       $this->nodes[$title] = $this->drupalCreateNode($info);
96     }
97
98     // Create non-EN nodes.
99     foreach ($this->test_data as $title => $body) {
100       $info = array(
101         'title' => $title . ' (FR)',
102         'body' => array(array('value' => $body)),
103         'type' => 'page',
104         'langcode' => 'fr',
105       );
106       $this->nodes[$title] = $this->drupalCreateNode($info);
107     }
108
109     // Create language-unspecified nodes.
110     foreach ($this->test_data as $title => $body) {
111       $info = array(
112         'title' => $title . ' (UND)',
113         'body' => array(array('value' => $body)),
114         'type' => 'page',
115         'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
116       );
117       $this->nodes[$title] = $this->drupalCreateNode($info);
118     }
119
120     // Run cron to ensure the content is indexed.
121     $this->cronRun();
122     $this->drupalGet('admin/reports/dblog');
123     $this->assertText(t('Cron run completed'), 'Log shows cron run completed');
124   }
125
126   /**
127    * Test that search variations return English language results.
128    */
129   protected function testStemSearching() {
130
131     foreach ($this->searches as $search) {
132       $this->drupalPostForm('search/node', array('keys' => $search), t('Search'));
133
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)));
137       }
138
139       // Check for results by language.
140       switch ($search) {
141         case 'walk':
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)));
144           break;
145
146         case 'walked':
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)));
149           break;
150
151         case 'walking':
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)));
154           break;
155
156       }
157     }
158   }
159
160 }