View source
<?php
namespace Drupal\Tests\porterstemmer\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\CronRunTrait;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
class LangCodeTest extends BrowserTestBase {
use CronRunTrait;
protected $defaultTheme = 'stark';
protected static $modules = [
'node',
'search',
'porterstemmer',
'language',
'dblog',
];
protected $testUser;
protected $testData = [
'First Page' => 'I walk through the streets, looking around for trouble.',
'Second Page' => 'I walked home from work today.',
'Third Page' => 'I am always walking everywhere.',
];
protected $searches = [
'walk',
'walked',
'walking',
];
protected $nodes;
protected function setUp() : void {
parent::setUp();
$this->testUser = $this
->drupalCreateUser([
'search content',
'access content',
'administer nodes',
'access site reports',
'use advanced search',
'administer languages',
'access administration pages',
'administer site configuration',
]);
$this
->drupalLogin($this->testUser);
if ($this->profile != 'standard') {
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
}
ConfigurableLanguage::createFromLangcode('fr')
->save();
$field_storage = FieldStorageConfig::loadByName('node', 'body');
$field_storage
->setTranslatable(TRUE);
$field_storage
->save();
foreach ($this->testData as $title => $body) {
$info = [
'title' => $title . ' (EN)',
'body' => [
[
'value' => $body,
],
],
'type' => 'page',
'langcode' => 'en',
];
$this->nodes[$title] = $this
->drupalCreateNode($info);
}
foreach ($this->testData as $title => $body) {
$info = [
'title' => $title . ' (FR)',
'body' => [
[
'value' => $body,
],
],
'type' => 'page',
'langcode' => 'fr',
];
$this->nodes[$title] = $this
->drupalCreateNode($info);
}
foreach ($this->testData as $title => $body) {
$info = [
'title' => $title . ' (UND)',
'body' => [
[
'value' => $body,
],
],
'type' => 'page',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
];
$this->nodes[$title] = $this
->drupalCreateNode($info);
}
$this
->cronRun();
$this
->drupalGet('admin/reports/dblog');
$this
->assertText(t('Cron run completed'), 'Log shows cron run completed');
}
public function testStemSearching() {
foreach ($this->searches as $search) {
$this
->drupalPostForm('search/node', [
'keys' => $search,
], t('Search'));
foreach ($this->testData as $title => $body) {
$this
->assertText($title . ' (EN)', new FormattableMarkup('Search for %search returns English-language node with body %body', [
'%search' => $search,
'%body' => $body,
]));
}
switch ($search) {
case 'walk':
$this
->assertNoText('Second Page (FR)', new FormattableMarkup('Search for %search does not show stemmed non-English results.', [
'%search' => $search,
]));
$this
->assertNoText('Second Page (UND)', new FormattableMarkup('Search for %search does show stemmed language-unspecified results.', [
'%search' => $search,
]));
break;
case 'walked':
$this
->assertNoText('Second Page (FR)', new FormattableMarkup('Search for %search does not show stemmed non-English results.', [
'%search' => $search,
]));
$this
->assertNoText('Second Page (UND)', new FormattableMarkup('Search for %search does not show stemmed language-unspecified results.', [
'%search' => $search,
]));
break;
case 'walking':
$this
->assertText('First Page (FR)', new FormattableMarkup('Search for %search does show matching non-English results.', [
'%search' => $search,
]));
$this
->assertText('First Page (UND)', new FormattableMarkup('Search for %search does show matching language-unspecified results.', [
'%search' => $search,
]));
break;
}
}
}
}