Newer
Older
<?php
/**
* @file
* Definition of Drupal\search\Tests\SearchMultilingualEntityTest.
*/
namespace Drupal\search\Tests;
use Drupal\Core\Language\Language;
catch
committed
use Drupal\field\Field;
/**
* Tests entities with multilingual fields.
*/
class SearchMultilingualEntityTest extends SearchTestBase {
/**
* List of searchable nodes.
*
* @var array
*/
protected $searchable_nodes = array();
Alex Pott
committed
/**
* Node search plugin.
*
* @var \Drupal\node\Plugin\Search\NodeSearch
*/
protected $plugin;
public static $modules = array('language', 'locale', 'comment');
public static function getInfo() {
return array(
'name' => 'Multilingual entities',
'description' => 'Tests entities with multilingual fields.',
'group' => 'Search',
);
}
function setUp() {
parent::setUp();
// Add two new languages.
$language = new Language(array(
'id' => 'hu',
));
catch
committed
$language = new Language(array(
'id' => 'sv',
));
catch
committed
// Make the body field translatable. The title is already translatable by
// definition. The parent class has already created the article and page
// content types.
catch
committed
$field = Field::fieldInfo()->getField('node', 'body');
$field->translatable = TRUE;
$field->save();
// Create a few page nodes with multilingual body values.
$default_format = filter_default_format();
$nodes = array(
array(
catch
committed
'title' => 'First node en',
Angie Byron
committed
'body' => array(array('value' => $this->randomName(32), 'format' => $default_format)),
catch
committed
'title' => 'Second node this is the English title',
Angie Byron
committed
'body' => array(array('value' => $this->randomName(32), 'format' => $default_format)),
catch
committed
'title' => 'Third node en',
Angie Byron
committed
'body' => array(array('value' => $this->randomName(32), 'format' => $default_format)),
'langcode' => 'en',
),
);
$this->searchable_nodes = array();
Angie Byron
committed
foreach ($nodes as $setting) {
$this->searchable_nodes[] = $this->drupalCreateNode($setting);
catch
committed
Alex Pott
committed
// Add a single translation to the second node.
catch
committed
$translation = $this->searchable_nodes[1]->addTranslation('hu', array('title' => 'Second node hu'));
Angie Byron
committed
$translation->body->value = $this->randomName(32);
$this->searchable_nodes[1]->save();
// Add two translations to the third node.
catch
committed
$translation = $this->searchable_nodes[2]->addTranslation('hu', array('title' => 'Third node this is the Hungarian title'));
Angie Byron
committed
$translation->body->value = $this->randomName(32);
catch
committed
$translation = $this->searchable_nodes[2]->addTranslation('sv', array('title' => 'Third node sv'));
Angie Byron
committed
$translation->body->value = $this->randomName(32);
$this->searchable_nodes[2]->save();
Alex Pott
committed
// Create a user who can administer search and do searches.
$user = $this->drupalCreateUser(array('administer search', 'search content', 'use advanced search', 'access content'));
$this->drupalLogin($user);
$this->plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
Alex Pott
committed
* Tests the indexing throttle and search results with multilingual nodes.
Alex Pott
committed
function testMultilingualSearch() {
// Index only 2 nodes per cron run. We cannot do this setting in the UI,
// because it doesn't go this low.
\Drupal::config('search.settings')->set('index.cron_limit', 2)->save();
$this->assertIndexCounts(3, 3, 'before updating the search index');
// Update the index. This does the initial processing.
Alex Pott
committed
$this->plugin->updateIndex();
// Run the shutdown function. Testing is a unique case where indexing
// and searching has to happen in the same request, so running the shutdown
// function manually is needed to finish the indexing process.
search_update_totals();
Alex Pott
committed
$this->assertIndexCounts(1, 3, 'after updating partially');
Alex Pott
committed
// Now index the rest of the nodes.
// Make sure index throttle is high enough, via the UI.
$this->drupalPostForm('admin/config/search/settings', array('cron_limit' => 20), t('Save configuration'));
$this->assertEqual(20, \Drupal::config('search.settings')->get('index.cron_limit', 100), 'Config setting was saved correctly');
$this->plugin->updateIndex();
Alex Pott
committed
$this->assertIndexCounts(0, 3, 'after updating fully');
// Test search results.
catch
committed
// This should find two results for the second and third node.
Alex Pott
committed
$this->plugin->setSearch('English OR Hungarian', array(), array());
$search_result = $this->plugin->execute();
$this->assertEqual(count($search_result), 2, 'Found two results.');
// Nodes are saved directly after each other and have the same created time
// so testing for the order is not possible.
$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.');
catch
committed
// Now filter for Hungarian results only.
Alex Pott
committed
$this->plugin->setSearch('English OR Hungarian', array('f' => array('language:hu')), array());
$search_result = $this->plugin->execute();
catch
committed
$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.');
catch
committed
// Test for search with common key word across multiple languages.
Alex Pott
committed
$this->plugin->setSearch('node', array(), array());
$search_result = $this->plugin->execute();
catch
committed
$this->assertEqual(count($search_result), 6, 'The search found total six results');
// Test with language filters and common key word.
Alex Pott
committed
$this->plugin->setSearch('node', array('f' => array('language:hu')), array());
$search_result = $this->plugin->execute();
catch
committed
$this->assertEqual(count($search_result), 2, 'The search found 2 results');
// Test to check for the language of result items.
foreach($search_result as $result) {
$this->assertEqual($result['langcode'], 'hu', 'The search found the correct Hungarian result');
}
Alex Pott
committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Mark one of the nodes for reindexing, using the API function, and
// verify indexing status.
search_reindex($this->searchable_nodes[0]->id(), 'node_search');
$this->assertIndexCounts(1, 3, 'after marking one node to reindex via API function');
// Update the index and verify the totals again.
$this->plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
$this->plugin->updateIndex();
search_update_totals();
$this->assertIndexCounts(0, 3, 'after indexing again');
// Mark one node for reindexing by saving it, and verify indexing status.
$this->searchable_nodes[1]->save();
$this->assertIndexCounts(1, 3, 'after marking one node to reindex via save');
}
/**
* Verifies the indexing status counts.
*
* @param $remaining
* Count of remaining items to verify.
* @param $total
* Count of total items to verify.
* @param $message
* Message to use, something like "after updating the search index".
*/
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);