View source
<?php
namespace Drupal\search\Tests;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
class SearchMultilingualEntityTest extends SearchTestBase {
protected $searchableNodes = array();
protected $plugin;
public static $modules = array(
'language',
'locale',
'comment',
);
protected function setUp() {
parent::setUp();
$user = $this
->drupalCreateUser(array(
'administer search',
'search content',
'use advanced search',
'access content',
'access site reports',
'administer site configuration',
));
$this
->drupalLogin($user);
$this->plugin = $this->container
->get('plugin.manager.search')
->createInstance('node_search');
$this
->assertIndexCounts(0, 0, 'before adding nodes');
$this
->assertDatabaseCounts(0, 0, 'before adding nodes');
ConfigurableLanguage::createFromLangcode('hu')
->save();
ConfigurableLanguage::createFromLangcode('sv')
->save();
$field_storage = FieldStorageConfig::loadByName('node', 'body');
$field_storage
->setTranslatable(TRUE);
$field_storage
->save();
$default_format = filter_default_format();
$nodes = array(
array(
'title' => 'First node en',
'type' => 'page',
'body' => array(
array(
'value' => $this
->randomMachineName(32),
'format' => $default_format,
),
),
'langcode' => 'en',
),
array(
'title' => 'Second node this is the English title',
'type' => 'page',
'body' => array(
array(
'value' => $this
->randomMachineName(32),
'format' => $default_format,
),
),
'langcode' => 'en',
),
array(
'title' => 'Third node en',
'type' => 'page',
'body' => array(
array(
'value' => $this
->randomMachineName(32),
'format' => $default_format,
),
),
'langcode' => 'en',
),
array(),
array(),
array(),
array(),
array(),
);
$this->searchableNodes = array();
foreach ($nodes as $setting) {
$this->searchableNodes[] = $this
->drupalCreateNode($setting);
}
$translation = $this->searchableNodes[1]
->addTranslation('hu', array(
'title' => 'Second node hu',
));
$translation->body->value = $this
->randomMachineName(32);
$this->searchableNodes[1]
->save();
$translation = $this->searchableNodes[2]
->addTranslation('hu', array(
'title' => 'Third node this is the Hungarian title',
));
$translation->body->value = $this
->randomMachineName(32);
$translation = $this->searchableNodes[2]
->addTranslation('sv', array(
'title' => 'Third node sv',
));
$translation->body->value = $this
->randomMachineName(32);
$this->searchableNodes[2]
->save();
$this
->assertIndexCounts(8, 8, 'before updating the search index');
$this
->assertDatabaseCounts(0, 0, 'before updating the search index');
}
function testMultilingualSearch() {
$this
->config('search.settings')
->set('index.cron_limit', 2)
->save();
$this->plugin = $this->container
->get('plugin.manager.search')
->createInstance('node_search');
$this->plugin
->updateIndex();
search_update_totals();
$this
->assertIndexCounts(6, 8, 'after updating partially');
$this
->assertDatabaseCounts(2, 0, 'after updating partially');
$this
->drupalPostForm('admin/config/search/pages', array(
'cron_limit' => 20,
), t('Save configuration'));
$this
->assertEqual(20, $this
->config('search.settings')
->get('index.cron_limit', 100), 'Config setting was saved correctly');
$this->plugin = $this->container
->get('plugin.manager.search')
->createInstance('node_search');
$this->plugin
->updateIndex();
search_update_totals();
$this
->assertIndexCounts(0, 8, 'after updating fully');
$this
->assertDatabaseCounts(8, 0, 'after updating fully');
$this
->drupalPostForm('admin/config/search/pages', array(), t('Re-index site'));
$this
->drupalPostForm(NULL, array(), t('Re-index site'));
$this
->assertIndexCounts(8, 8, 'after reindex');
$this
->assertDatabaseCounts(8, 0, 'after reindex');
$this->plugin
->updateIndex();
search_update_totals();
$this->plugin
->setSearch('English OR Hungarian', array(), array());
$search_result = $this->plugin
->execute();
$this
->assertEqual(count($search_result), 2, 'Found two results.');
$results = array(
$search_result[0]['title'],
$search_result[1]['title'],
);
$this
->assertTrue(in_array('Third node this is the Hungarian title', $results), 'The search finds the correct Hungarian title.');
$this
->assertTrue(in_array('Second node this is the English title', $results), 'The search finds the correct English title.');
$this->plugin
->setSearch('English OR Hungarian', array(
'f' => array(
'language:hu',
),
), array());
$search_result = $this->plugin
->execute();
$this
->assertEqual(count($search_result), 1, 'The search found only one result');
$this
->assertEqual($search_result[0]['title'], 'Third node this is the Hungarian title', 'The search finds the correct Hungarian title.');
$this->plugin
->setSearch('node', array(), array());
$search_result = $this->plugin
->execute();
$this
->assertEqual(count($search_result), 6, 'The search found total six results');
$this->plugin
->setSearch('node', array(
'f' => array(
'language:hu',
),
), array());
$search_result = $this->plugin
->execute();
$this
->assertEqual(count($search_result), 2, 'The search found 2 results');
foreach ($search_result as $result) {
$this
->assertEqual($result['langcode'], 'hu', 'The search found the correct Hungarian result');
}
search_mark_for_reindex('node_search', $this->searchableNodes[0]
->id());
$this
->assertIndexCounts(1, 8, 'after marking one node to reindex via API function');
$this->plugin = $this->container
->get('plugin.manager.search')
->createInstance('node_search');
$this->plugin
->updateIndex();
search_update_totals();
$this
->assertIndexCounts(0, 8, 'after indexing again');
$this->searchableNodes[1]
->save();
$this
->assertIndexCounts(1, 8, 'after marking one node to reindex via save');
$current = REQUEST_TIME;
$old = $current - 10;
db_update('search_dataset')
->fields(array(
'reindex' => $old,
))
->condition('reindex', $current, '>=')
->execute();
$this->searchableNodes[1]
->save();
$result = db_select('search_dataset', 'd')
->fields('d', array(
'reindex',
))
->condition('type', 'node_search')
->condition('sid', $this->searchableNodes[1]
->id())
->execute()
->fetchField();
$this
->assertEqual($result, $old, 'Reindex time was not updated if node was already marked');
search_index('foo', $this->searchableNodes[0]
->id(), 'en', 'some text');
$this
->assertIndexCounts(1, 8, 'after adding a different index item');
search_mark_for_reindex('foo');
$this
->assertIndexCounts(1, 8, 'after reindexing the other search type');
search_mark_for_reindex();
$this
->assertIndexCounts(8, 8, 'after reindexing everything');
$this
->assertDatabaseCounts(8, 1, 'before clear');
search_index_clear('node_search', $this->searchableNodes[0]
->id(), 'hu');
$this
->assertDatabaseCounts(8, 1, 'after clear with wrong language');
search_index_clear('node_search', $this->searchableNodes[0]
->id(), 'en');
$this
->assertDatabaseCounts(7, 1, 'after clear with right language');
search_index_clear('node_search', $this->searchableNodes[1]
->id());
$this
->assertDatabaseCounts(6, 1, 'unspecified language clear');
search_index_clear('foo');
$this
->assertDatabaseCounts(6, 0, 'other index clear');
search_index_clear();
$this
->assertDatabaseCounts(0, 0, 'complete clear');
}
protected function assertIndexCounts($remaining, $total, $message) {
$status = $this->plugin
->indexStatus();
$this
->assertEqual($status['remaining'], $remaining, 'Remaining items ' . $message . ' is ' . $remaining);
$this
->assertEqual($status['total'], $total, 'Total items ' . $message . ' is ' . $total);
$indexed = $total - $remaining;
$percent = $total > 0 ? floor(100 * $indexed / $total) : 100;
$this
->drupalGet('admin/config/search/pages');
$this
->assertText($percent . '% of the site has been indexed.', 'Progress percent text at top of Search settings page is correct at: ' . $message);
$this
->assertText($remaining . ' item', 'Remaining text at top of Search settings page is correct at: ' . $message);
$this
->assertText($indexed . ' of ' . $total . ' indexed', 'Progress text in pages section of Search settings page is correct at: ' . $message);
$this
->drupalGet('admin/reports/status');
$this
->assertText('Search index progress', 'Search status section header is present on status report page');
$this
->assertText($percent . '%', 'Correct percentage is shown on status report page at: ' . $message);
$this
->assertText('(' . $remaining . ' remaining)', 'Correct remaining value is shown on status report page at: ' . $message);
}
protected function assertDatabaseCounts($count_node, $count_foo, $message) {
$results = db_select('search_dataset', 'i')
->fields('i', array(
'sid',
))
->condition('type', 'node_search')
->groupBy('sid')
->execute()
->fetchCol();
$this
->assertEqual($count_node, count($results), 'Node count was ' . $count_node . ' for ' . $message);
$results = db_select('search_dataset', 'i')
->fields('i', array(
'sid',
))
->condition('type', 'foo')
->execute()
->fetchCol();
$this
->assertEqual($count_foo, count($results), 'Foo count was ' . $count_foo . ' for ' . $message);
}
}