View source
<?php
namespace Drupal\Tests\search_api\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\search_api\Entity\Index;
class LanguageIntegrationTest extends SearchApiBrowserTestBase {
public static $modules = [
'node',
'search_api',
'search_api_test',
'language',
];
public function setUp() {
parent::setUp();
ConfigurableLanguage::createFromLangcode('nl')
->save();
ConfigurableLanguage::createFromLangcode('xx-lolspeak')
->save();
$this
->getTestServer();
$this
->getTestIndex();
$this
->drupalLogin($this->adminUser);
}
public function testIndexSettings() {
$article1 = $this
->drupalCreateNode([
'type' => 'article',
]);
$article2 = $this
->drupalCreateNode([
'type' => 'article',
]);
$tracked_items = $this
->countTrackedItems();
$this
->assertEquals(2, $tracked_items, 'Two items are tracked.');
$translation = [
'title' => 'test NL',
'body' => 'NL body',
];
$article1
->addTranslation('nl', $translation)
->save();
$translation = [
'title' => 'test2 NL',
'body' => 'NL body2',
];
$article2
->addTranslation('nl', $translation)
->save();
$translation = [
'title' => 'cats',
'body' => 'Cats test',
];
$article1
->addTranslation('xx-lolspeak', $translation)
->save();
$tracked_items = $this
->countTrackedItems();
$this
->assertEquals(5, $tracked_items, 'Five items are tracked.');
$this
->drupalGet($this
->getIndexPath());
$this
->submitForm([], 'Clear all indexed data');
$this
->submitForm([], 'Confirm');
$this
->drupalGet($this
->getIndexPath());
$this
->submitForm([], 'Index now');
$this
->checkForMetaRefresh();
$this
->assertSession()
->pageTextContains('Successfully indexed 5 items');
$form_values = [
'datasource_configs[entity:node][languages][default]' => 1,
'datasource_configs[entity:node][languages][selected][nl]' => 1,
];
$this
->drupalGet($this
->getIndexPath('edit'));
$this
->submitForm($form_values, 'Save');
$this
->checkForMetaRefresh();
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('The index was successfully saved.');
$this
->drupalGet($this
->getIndexPath());
$this
->submitForm([], 'Index now');
$this
->checkForMetaRefresh();
$this
->assertSession()
->pageTextContains('Successfully indexed 3 items');
$form_values = [
'datasource_configs[entity:node][languages][default]' => 0,
'datasource_configs[entity:node][languages][selected][nl]' => 1,
'datasource_configs[entity:node][bundles][default]' => 0,
'datasource_configs[entity:node][bundles][selected][article]' => 1,
];
$this
->drupalGet($this
->getIndexPath('edit'));
$this
->submitForm($form_values, 'Save');
$this
->checkForMetaRefresh();
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('The index was successfully saved.');
$this
->drupalGet($this
->getIndexPath());
$this
->submitForm([], 'Index now');
$this
->checkForMetaRefresh();
$this
->assertSession()
->pageTextContains('Successfully indexed 2 items');
}
protected function countTrackedItems() {
$index = Index::load($this->indexId);
return $index
->getTrackerInstance()
->getTotalItemsCount();
}
}