updated core

This commit is contained in:
Bachir Soussi Chiadmi
2018-01-24 13:36:32 +01:00
parent cd7dea45a9
commit f9374cf96d
1997 changed files with 5175 additions and 127715 deletions
@@ -34,6 +34,8 @@ class Search extends FilterPluginBase {
/**
* TRUE if the search query has been parsed.
*
* @var bool
*/
protected $parsed = FALSE;
-1
View File
@@ -571,7 +571,6 @@ class SearchQuery extends SelectExtender {
}
}
// Add arguments for the keyword relevance normalization number.
$normalization = 1.0 / $this->normalize;
for ($i = 0; $i < $this->relevance_count; $i++) {
@@ -1,115 +0,0 @@
<?php
namespace Drupal\search\Tests;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
/**
* Tests that comment count display toggles properly on comment status of node.
*
* Issue 537278
*
* - Nodes with comment status set to Open should always how comment counts
* - Nodes with comment status set to Closed should show comment counts
* only when there are comments
* - Nodes with comment status set to Hidden should never show comment counts
*
* @group search
*/
class SearchCommentCountToggleTest extends SearchTestBase {
use CommentTestTrait;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node', 'comment');
/**
* A user with permission to search and post comments.
*
* @var \Drupal\user\UserInterface
*/
protected $searchingUser;
/**
* Array of nodes available to search.
*
* @var \Drupal\node\NodeInterface[]
*/
protected $searchableNodes;
protected function setUp() {
parent::setUp();
// Create searching user.
$this->searchingUser = $this->drupalCreateUser(array('search content', 'access content', 'access comments', 'post comments', 'skip comment approval'));
// Log in with sufficient privileges.
$this->drupalLogin($this->searchingUser);
// Add a comment field.
$this->addDefaultCommentField('node', 'article');
// Create initial nodes.
$node_params = array('type' => 'article', 'body' => array(array('value' => 'SearchCommentToggleTestCase')));
$this->searchableNodes['1 comment'] = $this->drupalCreateNode($node_params);
$this->searchableNodes['0 comments'] = $this->drupalCreateNode($node_params);
// Create a comment array
$edit_comment = array();
$edit_comment['subject[0][value]'] = $this->randomMachineName();
$edit_comment['comment_body[0][value]'] = $this->randomMachineName();
// Post comment to the test node with comment
$this->drupalPostForm('comment/reply/node/' . $this->searchableNodes['1 comment']->id() . '/comment', $edit_comment, t('Save'));
// First update the index. This does the initial processing.
$this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
// Then, 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();
}
/**
* Verify that comment count display toggles properly on comment status of node
*/
function testSearchCommentCountToggle() {
// Search for the nodes by string in the node body.
$edit = array(
'keys' => "'SearchCommentToggleTestCase'",
);
$this->drupalGet('search/node');
// Test comment count display for nodes with comment status set to Open
$this->drupalPostForm(NULL, $edit, t('Search'));
$this->assertText(t('0 comments'), 'Empty comment count displays for nodes with comment status set to Open');
$this->assertText(t('1 comment'), 'Non-empty comment count displays for nodes with comment status set to Open');
// Test comment count display for nodes with comment status set to Closed
$this->searchableNodes['0 comments']->set('comment', CommentItemInterface::CLOSED);
$this->searchableNodes['0 comments']->save();
$this->searchableNodes['1 comment']->set('comment', CommentItemInterface::CLOSED);
$this->searchableNodes['1 comment']->save();
$this->drupalPostForm(NULL, $edit, t('Search'));
$this->assertNoText(t('0 comments'), 'Empty comment count does not display for nodes with comment status set to Closed');
$this->assertText(t('1 comment'), 'Non-empty comment count displays for nodes with comment status set to Closed');
// Test comment count display for nodes with comment status set to Hidden
$this->searchableNodes['0 comments']->set('comment', CommentItemInterface::HIDDEN);
$this->searchableNodes['0 comments']->save();
$this->searchableNodes['1 comment']->set('comment', CommentItemInterface::HIDDEN);
$this->searchableNodes['1 comment']->save();
$this->drupalPostForm(NULL, $edit, t('Search'));
$this->assertNoText(t('0 comments'), 'Empty comment count does not display for nodes with comment status set to Hidden');
$this->assertNoText(t('1 comment'), 'Non-empty comment count does not display for nodes with comment status set to Hidden');
}
}
@@ -1,75 +0,0 @@
<?php
namespace Drupal\search\Tests;
use Drupal\language\Entity\ConfigurableLanguage;
/**
* Tests searching with date filters that exclude some translations.
*
* @group search
*/
class SearchDateIntervalTest extends SearchTestBase {
/**
* Modules to enable.
*
* @var string[]
*/
public static $modules = ['language', 'search_date_query_alter'];
protected function setUp() {
parent::setUp();
// Create and log in user.
$test_user = $this->drupalCreateUser(['access content', 'search content', 'use advanced search', 'administer nodes', 'administer languages', 'access administration pages', 'administer site configuration']);
$this->drupalLogin($test_user);
// Add a new language.
ConfigurableLanguage::createFromLangcode('es')->save();
// Set up times to be applied to the English and Spanish translations of the
// node create time, so that they are filtered in/out in the
// search_date_query_alter test module.
$created_time_en = new \DateTime('February 10 2016 10PM');
$created_time_es = new \DateTime('March 19 2016 10PM');
$default_format = filter_default_format();
$node = $this->drupalCreateNode([
'title' => 'Node EN',
'type' => 'page',
'body' => [
'value' => $this->randomMachineName(32),
'format' => $default_format,
],
'langcode' => 'en',
'created' => $created_time_en->format('U'),
]);
// Add Spanish translation to the node.
$translation = $node->addTranslation('es', ['title' => 'Node ES']);
$translation->body->value = $this->randomMachineName(32);
$translation->created->value = $created_time_es->format('U');
$node->save();
// Update the index.
$plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
$plugin->updateIndex();
search_update_totals();
}
/**
* Tests searching with date filters that exclude some translations.
*/
public function testDateIntervalQueryAlter() {
// Search for keyword node.
$edit = ['keys' => 'node'];
$this->drupalPostForm('search/node', $edit, t('Search'));
// The nodes must have the same node ID but the created date is different.
// So only the Spanish translation must appear.
$this->assertLink('Node ES', 0, 'Spanish translation found in search results');
$this->assertNoLink('Node EN', 'Search results do not contain English node');
}
}
@@ -1,79 +0,0 @@
<?php
namespace Drupal\search\Tests;
/**
* Tests that searching for a phrase gets the correct page count.
*
* @group search
*/
class SearchExactTest extends SearchTestBase {
/**
* Tests that the correct number of pager links are found for both keywords and phrases.
*/
function testExactQuery() {
// Log in with sufficient privileges.
$user = $this->drupalCreateUser(array('create page content', 'search content'));
$this->drupalLogin($user);
$settings = array(
'type' => 'page',
'title' => 'Simple Node',
);
// Create nodes with exact phrase.
for ($i = 0; $i <= 17; $i++) {
$settings['body'] = array(array('value' => 'love pizza'));
$this->drupalCreateNode($settings);
}
// Create nodes containing keywords.
for ($i = 0; $i <= 17; $i++) {
$settings['body'] = array(array('value' => 'love cheesy pizza'));
$this->drupalCreateNode($settings);
}
// Create another node and save it for later.
$settings['body'] = array(array('value' => 'Druplicon'));
$node = $this->drupalCreateNode($settings);
// Update the search index.
$this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
search_update_totals();
// Refresh variables after the treatment.
$this->refreshVariables();
// Test that the correct number of pager links are found for keyword search.
$edit = array('keys' => 'love pizza');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertLinkByHref('page=1', 0, '2nd page link is found for keyword search.');
$this->assertLinkByHref('page=2', 0, '3rd page link is found for keyword search.');
$this->assertLinkByHref('page=3', 0, '4th page link is found for keyword search.');
$this->assertNoLinkByHref('page=4', '5th page link is not found for keyword search.');
// Test that the correct number of pager links are found for exact phrase search.
$edit = array('keys' => '"love pizza"');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertLinkByHref('page=1', 0, '2nd page link is found for exact phrase search.');
$this->assertNoLinkByHref('page=2', '3rd page link is not found for exact phrase search.');
// Check that with post settings turned on the post information is displayed.
$node_type_config = \Drupal::configFactory()->getEditable('node.type.page');
$node_type_config->set('display_submitted', TRUE);
$node_type_config->save();
$edit = array('keys' => 'Druplicon');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertText($user->getUsername(), 'Basic page node displays author name when post settings are on.');
$this->assertText(format_date($node->getChangedTime(), 'short'), 'Basic page node displays post date when post settings are on.');
// Check that with post settings turned off the user and changed date
// information is not displayed.
$node_type_config->set('display_submitted', FALSE);
$node_type_config->save();
$edit = array('keys' => 'Druplicon');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertNoText($user->getUsername(), 'Basic page node does not display author name when post settings are off.');
$this->assertNoText(format_date($node->getChangedTime(), 'short'), 'Basic page node does not display post date when post settings are off.');
}
}
@@ -1,62 +0,0 @@
<?php
namespace Drupal\search\Tests;
use Drupal\Component\Utility\Html;
/**
* Verify the search without keywords set and extra conditions.
*
* Verifies that a plugin can override the isSearchExecutable() method to allow
* searching without keywords set and that GET query parameters are made
* available to plugins during search execution.
*
* @group search
*/
class SearchKeywordsConditionsTest extends SearchTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('comment', 'search_extra_type', 'test_page_test');
/**
* A user with permission to search and post comments.
*
* @var \Drupal\user\UserInterface
*/
protected $searchingUser;
protected function setUp() {
parent::setUp();
// Create searching user.
$this->searchingUser = $this->drupalCreateUser(array('search content', 'access content', 'access comments', 'skip comment approval'));
// Log in with sufficient privileges.
$this->drupalLogin($this->searchingUser);
}
/**
* Verify the keywords are captured and conditions respected.
*/
function testSearchKeywordsConditions() {
// No keys, not conditions - no results.
$this->drupalGet('search/dummy_path');
$this->assertNoText('Dummy search snippet to display');
// With keys - get results.
$keys = 'bike shed ' . $this->randomMachineName();
$this->drupalGet("search/dummy_path", array('query' => array('keys' => $keys)));
$this->assertText("Dummy search snippet to display. Keywords: {$keys}");
$keys = 'blue drop ' . $this->randomMachineName();
$this->drupalGet("search/dummy_path", array('query' => array('keys' => $keys)));
$this->assertText("Dummy search snippet to display. Keywords: {$keys}");
// Add some conditions and keys.
$keys = 'moving drop ' . $this->randomMachineName();
$this->drupalGet("search/dummy_path", array('query' => array('keys' => 'bike', 'search_conditions' => $keys)));
$this->assertText("Dummy search snippet to display.");
$this->assertRaw(Html::escape(print_r(array('keys' => 'bike', 'search_conditions' => $keys), TRUE)));
}
}
@@ -1,322 +0,0 @@
<?php
namespace Drupal\search\Tests;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
/**
* Tests entities with multilingual fields.
*
* @group search
*/
class SearchMultilingualEntityTest extends SearchTestBase {
/**
* List of searchable nodes.
*
* @var \Drupal\node\NodeInterface[]
*/
protected $searchableNodes = array();
/**
* Node search plugin.
*
* @var \Drupal\node\Plugin\Search\NodeSearch
*/
protected $plugin;
public static $modules = array('language', 'locale', 'comment');
protected function setUp() {
parent::setUp();
// Create a user who can administer search, do searches, see the status
// report, and administer cron. Log in.
$user = $this->drupalCreateUser(array('administer search', 'search content', 'use advanced search', 'access content', 'access site reports', 'administer site configuration'));
$this->drupalLogin($user);
// Set up the search plugin.
$this->plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
// Check indexing counts before adding any nodes.
$this->assertIndexCounts(0, 0, 'before adding nodes');
$this->assertDatabaseCounts(0, 0, 'before adding nodes');
// Add two new languages.
ConfigurableLanguage::createFromLangcode('hu')->save();
ConfigurableLanguage::createFromLangcode('sv')->save();
// Make the body field translatable. The title is already translatable by
// definition. The parent class has already created the article and page
// content types.
$field_storage = FieldStorageConfig::loadByName('node', 'body');
$field_storage->setTranslatable(TRUE);
$field_storage->save();
// Create a few page nodes with multilingual body values.
$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',
),
// After the third node, we don't care what the settings are. But we
// need to have at least 5 to make sure the throttling is working
// correctly. So, let's make 8 total.
array(
),
array(
),
array(
),
array(
),
array(
),
);
$this->searchableNodes = array();
foreach ($nodes as $setting) {
$this->searchableNodes[] = $this->drupalCreateNode($setting);
}
// Add a single translation to the second node.
$translation = $this->searchableNodes[1]->addTranslation('hu', array('title' => 'Second node hu'));
$translation->body->value = $this->randomMachineName(32);
$this->searchableNodes[1]->save();
// Add two translations to the third node.
$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();
// Verify that we have 8 nodes left to do.
$this->assertIndexCounts(8, 8, 'before updating the search index');
$this->assertDatabaseCounts(0, 0, 'before updating the search index');
}
/**
* Tests the indexing throttle and search results with multilingual nodes.
*/
function testMultilingualSearch() {
// Index only 2 nodes per cron run. We cannot do this setting in the UI,
// because it doesn't go this low.
$this->config('search.settings')->set('index.cron_limit', 2)->save();
// Get a new search plugin, to make sure it has this setting.
$this->plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
// Update the index. This does the initial processing.
$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();
$this->assertIndexCounts(6, 8, 'after updating partially');
$this->assertDatabaseCounts(2, 0, 'after updating partially');
// Now index the rest of the nodes.
// Make sure index throttle is high enough, via the UI.
$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');
// Get a new search plugin, to make sure it has this setting.
$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');
// Click the reindex button on the admin page, verify counts, and reindex.
$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();
// Test search results.
// This should find two results for the second and third node.
$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.');
// Now filter for Hungarian results only.
$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.');
// Test for search with common key word across multiple languages.
$this->plugin->setSearch('node', array(), array());
$search_result = $this->plugin->execute();
$this->assertEqual(count($search_result), 6, 'The search found total six results');
// Test with language filters and common key word.
$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');
// 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');
}
// Mark one of the nodes for reindexing, using the API function, and
// verify indexing status.
search_mark_for_reindex('node_search', $this->searchableNodes[0]->id());
$this->assertIndexCounts(1, 8, '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, 8, 'after indexing again');
// Mark one node for reindexing by saving it, and verify indexing status.
$this->searchableNodes[1]->save();
$this->assertIndexCounts(1, 8, 'after marking one node to reindex via save');
// The request time is always the same throughout test runs. Update the
// request time to a previous time, to simulate it having been marked
// previously.
$current = REQUEST_TIME;
$old = $current - 10;
db_update('search_dataset')
->fields(array('reindex' => $old))
->condition('reindex', $current, '>=')
->execute();
// Save the node again. Verify that the request time on it is not updated.
$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');
// Add a bogus entry to the search index table using a different search
// type. This will not appear in the index status, because it is not
// managed by a plugin.
search_index('foo', $this->searchableNodes[0]->id(), 'en', 'some text');
$this->assertIndexCounts(1, 8, 'after adding a different index item');
// Mark just this "foo" index for reindexing.
search_mark_for_reindex('foo');
$this->assertIndexCounts(1, 8, 'after reindexing the other search type');
// Mark everything for reindexing.
search_mark_for_reindex();
$this->assertIndexCounts(8, 8, 'after reindexing everything');
// Clear one item from the index, but with wrong language.
$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');
// Clear using correct language.
search_index_clear('node_search', $this->searchableNodes[0]->id(), 'en');
$this->assertDatabaseCounts(7, 1, 'after clear with right language');
// Don't specify language.
search_index_clear('node_search', $this->searchableNodes[1]->id());
$this->assertDatabaseCounts(6, 1, 'unspecified language clear');
// Clear everything in 'foo'.
search_index_clear('foo');
$this->assertDatabaseCounts(6, 0, 'other index clear');
// Clear everything.
search_index_clear();
$this->assertDatabaseCounts(0, 0, 'complete clear');
}
/**
* Verifies the indexing status counts.
*
* @param int $remaining
* Count of remaining items to verify.
* @param int $total
* Count of total items to verify.
* @param string $message
* Message to use, something like "after updating the search index".
*/
protected function assertIndexCounts($remaining, $total, $message) {
// Check status via plugin method call.
$status = $this->plugin->indexStatus();
$this->assertEqual($status['remaining'], $remaining, 'Remaining items ' . $message . ' is ' . $remaining);
$this->assertEqual($status['total'], $total, 'Total items ' . $message . ' is ' . $total);
// Check text in progress section of Search settings page. Note that this
// test avoids using
// \Drupal\Core\StringTranslation\TranslationInterface::formatPlural(), so
// it tests for fragments of text.
$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);
// Check text in pages section of Search settings page.
$this->assertText($indexed . ' of ' . $total . ' indexed', 'Progress text in pages section of Search settings page is correct at: ' . $message);
// Check text on status report page.
$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);
}
/**
* Checks actual database counts of items in the search index.
*
* @param int $count_node
* Count of node items to assert.
* @param int $count_foo
* Count of "foo" items to assert.
* @param string $message
* Message suffix to use.
*/
protected function assertDatabaseCounts($count_node, $count_foo, $message) {
// Count number of distinct nodes by ID.
$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);
// Count number of "foo" records.
$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);
}
}
@@ -1,80 +0,0 @@
<?php
namespace Drupal\search\Tests;
/**
* Tests search functionality with diacritics.
*
* @group search
*/
class SearchNodeDiacriticsTest extends SearchTestBase {
/**
* A user with permission to use advanced search.
*
* @var \Drupal\user\UserInterface
*/
public $testUser;
protected function setUp() {
parent::setUp();
node_access_rebuild();
// Create a test user and log in.
$this->testUser = $this->drupalCreateUser(array('access content', 'search content', 'use advanced search', 'access user profiles'));
$this->drupalLogin($this->testUser);
}
/**
* Tests that search returns results with diacritics in the search phrase.
*/
function testPhraseSearchPunctuation() {
$body_text = 'The Enricþment Center is cómmīŦŧęđ to the well BɆĬŇĜ of æll påŔťıçȉpǎǹţș. ';
$body_text .= 'Also meklēt (see #731298)';
$this->drupalCreateNode(array('body' => array(array('value' => $body_text))));
// Update the search index.
$this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
search_update_totals();
// Refresh variables after the treatment.
$this->refreshVariables();
$edit = array('keys' => 'meklet');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertRaw('<strong>meklēt</strong>');
$edit = array('keys' => 'meklēt');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertRaw('<strong>meklēt</strong>');
$edit = array('keys' => 'cómmīŦŧęđ BɆĬŇĜ påŔťıçȉpǎǹţș');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertRaw('<strong>cómmīŦŧęđ</strong>');
$this->assertRaw('<strong>BɆĬŇĜ</strong>');
$this->assertRaw('<strong>påŔťıçȉpǎǹţș</strong>');
$edit = array('keys' => 'committed being participants');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertRaw('<strong>cómmīŦŧęđ</strong>');
$this->assertRaw('<strong>BɆĬŇĜ</strong>');
$this->assertRaw('<strong>påŔťıçȉpǎǹţș</strong>');
$edit = array('keys' => 'Enricþment');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertRaw('<strong>Enricþment</strong>');
$edit = array('keys' => 'Enritchment');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertNoRaw('<strong>Enricþment</strong>');
$edit = array('keys' => 'æll');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertRaw('<strong>æll</strong>');
$edit = array('keys' => 'all');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertNoRaw('<strong>æll</strong>');
}
}
@@ -1,63 +0,0 @@
<?php
namespace Drupal\search\Tests;
/**
* Tests search functionality with punctuation and HTML entities.
*
* @group search
*/
class SearchNodePunctuationTest extends SearchTestBase {
/**
* A user with permission to use advanced search.
*
* @var \Drupal\user\UserInterface
*/
public $testUser;
protected function setUp() {
parent::setUp();
node_access_rebuild();
// Create a test user and log in.
$this->testUser = $this->drupalCreateUser(array('access content', 'search content', 'use advanced search', 'access user profiles'));
$this->drupalLogin($this->testUser);
}
/**
* Tests that search works with punctuation and HTML entities.
*/
function testPhraseSearchPunctuation() {
$node = $this->drupalCreateNode(array('body' => array(array('value' => "The bunny's ears were fluffy."))));
$node2 = $this->drupalCreateNode(array('body' => array(array('value' => 'Dignissim Aliquam &amp; Quieligo meus natu quae quia te. Damnum&copy; erat&mdash; neo pneum. Facilisi feugiat ibidem ratis.'))));
// Update the search index.
$this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
search_update_totals();
// Refresh variables after the treatment.
$this->refreshVariables();
// Submit a phrase wrapped in double quotes to include the punctuation.
$edit = array('keys' => '"bunny\'s"');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertText($node->label());
// Check if the author is linked correctly to the user profile page.
$username = $node->getOwner()->getUsername();
$this->assertLink($username);
// Search for "&" and verify entities are not broken up in the output.
$edit = array('keys' => '&');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertNoRaw('<strong>&</strong>amp;');
$this->assertText('You must include at least one keyword');
$edit = array('keys' => '&amp;');
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertNoRaw('<strong>&</strong>amp;');
$this->assertText('You must include at least one keyword');
}
}
@@ -122,7 +122,6 @@ class SearchPageCacheTagsTest extends SearchTestBase {
$this->container->get('module_installer')->install(['field_ui', 'entity_reference']);
$this->resetAll();
// Creates a new content type that will have an entity reference.
$type_name = 'entity_reference_test';
$type = $this->drupalCreateContentType(['name' => $type_name, 'type' => $type_name]);
@@ -1,44 +0,0 @@
<?php
namespace Drupal\search\Tests;
/**
* Tests if the result page can be overridden.
*
* Verifies that a plugin can override the buildResults() method to
* control what the search results page looks like.
*
* @group search
*/
class SearchPageOverrideTest extends SearchTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('search_extra_type');
/**
* A user with permission to administer search.
*
* @var \Drupal\user\UserInterface
*/
public $searchUser;
protected function setUp() {
parent::setUp();
// Log in as a user that can create and search content.
$this->searchUser = $this->drupalCreateUser(array('search content', 'administer search'));
$this->drupalLogin($this->searchUser);
}
function testSearchPageHook() {
$keys = 'bike shed ' . $this->randomMachineName();
$this->drupalGet("search/dummy_path", array('query' => array('keys' => $keys)));
$this->assertText('Dummy search snippet', 'Dummy search snippet is shown');
$this->assertText('Test page text is here', 'Page override is working');
}
}
@@ -1,51 +0,0 @@
<?php
namespace Drupal\search\Tests;
/**
* Tests that search works with numeric locale settings.
*
* @group search
*/
class SearchSetLocaleTest extends SearchTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('comment');
/**
* A node search plugin instance.
*
* @var \Drupal\search\Plugin\SearchInterface
*/
protected $nodeSearchPlugin;
protected function setUp() {
parent::setUp();
// Create a plugin instance.
$this->nodeSearchPlugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
// Create a node with a very simple body.
$this->drupalCreateNode(array('body' => array(array('value' => 'tapir'))));
// Update the search index.
$this->nodeSearchPlugin->updateIndex();
search_update_totals();
}
/**
* Verify that search works with a numeric locale set.
*/
public function testSearchWithNumericLocale() {
// French decimal point is comma.
setlocale(LC_NUMERIC, 'fr_FR');
$this->nodeSearchPlugin->setSearch('tapir', array(), array());
// The call to execute will throw an exception if a float in the wrong
// format is passed in the query to the database, so an assertion is not
// necessary here.
$this->nodeSearchPlugin->execute();
}
}
@@ -1,81 +0,0 @@
<?php
namespace Drupal\search\Tests;
use Drupal\Component\Utility\Unicode;
/**
* Tests that the search_simply() function works as intended.
*
* @group search
*/
class SearchSimplifyTest extends SearchTestBase {
/**
* Tests that all Unicode characters simplify correctly.
*/
function testSearchSimplifyUnicode() {
// This test uses a file that was constructed so that the even lines are
// boundary characters, and the odd lines are valid word characters. (It
// was generated as a sequence of all the Unicode characters, and then the
// boundary characters (punctuation, spaces, etc.) were split off into
// their own lines). So the even-numbered lines should simplify to nothing,
// and the odd-numbered lines we need to split into shorter chunks and
// verify that simplification doesn't lose any characters.
$input = file_get_contents(\Drupal::root() . '/core/modules/search/tests/UnicodeTest.txt');
$basestrings = explode(chr(10), $input);
$strings = array();
foreach ($basestrings as $key => $string) {
if ($key % 2) {
// Even line - should simplify down to a space.
$simplified = search_simplify($string);
$this->assertIdentical($simplified, ' ', "Line $key is excluded from the index");
}
else {
// Odd line, should be word characters.
// Split this into 30-character chunks, so we don't run into limits
// of truncation in search_simplify().
$start = 0;
while ($start < Unicode::strlen($string)) {
$newstr = Unicode::substr($string, $start, 30);
// Special case: leading zeros are removed from numeric strings,
// and there's one string in this file that is numbers starting with
// zero, so prepend a 1 on that string.
if (preg_match('/^[0-9]+$/', $newstr)) {
$newstr = '1' . $newstr;
}
$strings[] = $newstr;
$start += 30;
}
}
}
foreach ($strings as $key => $string) {
$simplified = search_simplify($string);
$this->assertTrue(Unicode::strlen($simplified) >= Unicode::strlen($string), "Nothing is removed from string $key.");
}
// Test the low-numbered ASCII control characters separately. They are not
// in the text file because they are problematic for diff, especially \0.
$string = '';
for ($i = 0; $i < 32; $i++) {
$string .= chr($i);
}
$this->assertIdentical(' ', search_simplify($string), 'Search simplify works for ASCII control characters.');
}
/**
* Tests that search_simplify() does the right thing with punctuation.
*/
function testSearchSimplifyPunctuation() {
$cases = array(
array('20.03/94-28,876', '20039428876', 'Punctuation removed from numbers'),
array('great...drupal--module', 'great drupal module', 'Multiple dot and dashes are word boundaries'),
array('very_great-drupal.module', 'verygreatdrupalmodule', 'Single dot, dash, underscore are removed'),
array('regular,punctuation;word', 'regular punctuation word', 'Punctuation is a word boundary'),
);
foreach ($cases as $case) {
$out = trim(search_simplify($case[0]));
$this->assertEqual($out, $case[1], $case[2]);
}
}
}
@@ -1,151 +0,0 @@
<?php
namespace Drupal\search\Tests;
use Drupal\Component\Utility\Unicode;
/**
* Tests that CJK tokenizer works as intended.
*
* @group search
*/
class SearchTokenizerTest extends SearchTestBase {
/**
* Verifies that strings of CJK characters are tokenized.
*
* The search_simplify() function does special things with numbers, symbols,
* and punctuation. So we only test that CJK characters that are not in these
* character classes are tokenized properly. See PREG_CLASS_CKJ for more
* information.
*/
function testTokenizer() {
// Set the minimum word size to 1 (to split all CJK characters) and make
// sure CJK tokenizing is turned on.
$this->config('search.settings')
->set('index.minimum_word_size', 1)
->set('index.overlap_cjk', TRUE)
->save();
$this->refreshVariables();
// Create a string of CJK characters from various character ranges in
// the Unicode tables.
// Beginnings of the character ranges.
$starts = array(
'CJK unified' => 0x4e00,
'CJK Ext A' => 0x3400,
'CJK Compat' => 0xf900,
'Hangul Jamo' => 0x1100,
'Hangul Ext A' => 0xa960,
'Hangul Ext B' => 0xd7b0,
'Hangul Compat' => 0x3131,
'Half non-punct 1' => 0xff21,
'Half non-punct 2' => 0xff41,
'Half non-punct 3' => 0xff66,
'Hangul Syllables' => 0xac00,
'Hiragana' => 0x3040,
'Katakana' => 0x30a1,
'Katakana Ext' => 0x31f0,
'CJK Reserve 1' => 0x20000,
'CJK Reserve 2' => 0x30000,
'Bomofo' => 0x3100,
'Bomofo Ext' => 0x31a0,
'Lisu' => 0xa4d0,
'Yi' => 0xa000,
);
// Ends of the character ranges.
$ends = array(
'CJK unified' => 0x9fcf,
'CJK Ext A' => 0x4dbf,
'CJK Compat' => 0xfaff,
'Hangul Jamo' => 0x11ff,
'Hangul Ext A' => 0xa97f,
'Hangul Ext B' => 0xd7ff,
'Hangul Compat' => 0x318e,
'Half non-punct 1' => 0xff3a,
'Half non-punct 2' => 0xff5a,
'Half non-punct 3' => 0xffdc,
'Hangul Syllables' => 0xd7af,
'Hiragana' => 0x309f,
'Katakana' => 0x30ff,
'Katakana Ext' => 0x31ff,
'CJK Reserve 1' => 0x2fffd,
'CJK Reserve 2' => 0x3fffd,
'Bomofo' => 0x312f,
'Bomofo Ext' => 0x31b7,
'Lisu' => 0xa4fd,
'Yi' => 0xa48f,
);
// Generate characters consisting of starts, midpoints, and ends.
$chars = array();
$charcodes = array();
foreach ($starts as $key => $value) {
$charcodes[] = $starts[$key];
$chars[] = $this->code2utf($starts[$key]);
$mid = round(0.5 * ($starts[$key] + $ends[$key]));
$charcodes[] = $mid;
$chars[] = $this->code2utf($mid);
$charcodes[] = $ends[$key];
$chars[] = $this->code2utf($ends[$key]);
}
// Merge into a string and tokenize.
$string = implode('', $chars);
$out = trim(search_simplify($string));
$expected = Unicode::strtolower(implode(' ', $chars));
// Verify that the output matches what we expect.
$this->assertEqual($out, $expected, 'CJK tokenizer worked on all supplied CJK characters');
}
/**
* Verifies that strings of non-CJK characters are not tokenized.
*
* This is just a sanity check - it verifies that strings of letters are
* not tokenized.
*/
function testNoTokenizer() {
// Set the minimum word size to 1 (to split all CJK characters) and make
// sure CJK tokenizing is turned on.
$this->config('search.settings')
->set('index.minimum_word_size', 1)
->set('index.overlap_cjk', TRUE)
->save();
$this->refreshVariables();
$letters = 'abcdefghijklmnopqrstuvwxyz';
$out = trim(search_simplify($letters));
$this->assertEqual($letters, $out, 'Letters are not CJK tokenized');
}
/**
* Like PHP chr() function, but for unicode characters.
*
* chr() only works for ASCII characters up to character 255. This function
* converts a number to the corresponding unicode character. Adapted from
* functions supplied in comments on several functions on php.net.
*/
function code2utf($num) {
if ($num < 128) {
return chr($num);
}
if ($num < 2048) {
return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
}
if ($num < 65536) {
return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
}
if ($num < 2097152) {
return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
}
return '';
}
}