- <?php
- * @file
- * Tests for search.module.
- */
-
- define('SEARCH_TYPE', '_test_');
- define('SEARCH_TYPE_2', '_test2_');
- define('SEARCH_TYPE_JPN', '_test3_');
-
- * Indexes content and queries it.
- */
- class SearchMatchTestCase extends BackdropWebTestCase {
-
- * Implementation setUp().
- */
- function setUp() {
- parent::setUp('search');
- }
-
-
- * Test search indexing.
- */
- function testMatching() {
- $this->_setup();
- $this->_testQueries();
- }
-
-
- * Set up a small index of items to test against.
- */
- function _setup() {
- config_set('search.settings', 'search_minimum_word_size', 3);
-
- for ($i = 1; $i <= 7; ++$i) {
- search_index($i, SEARCH_TYPE, $this->getText($i));
- }
- for ($i = 1; $i <= 5; ++$i) {
- search_index($i + 7, SEARCH_TYPE_2, $this->getText2($i));
- }
-
- foreach (array(
- 13 => '以呂波耳・ほへとち。リヌルヲ。',
- 14 => 'ドルーパルが大好きよ!',
- 15 => 'コーヒーとケーキ',
- ) as $i => $jpn) {
- search_index($i, SEARCH_TYPE_JPN, $jpn);
- }
- search_update_totals();
- }
-
-
- * _test_: Helper method for generating snippets of content.
- *
- * Generated items to test against:
- * 1 ipsum
- * 2 dolore sit
- * 3 sit am ut
- * 4 am ut enim am
- * 5 ut enim am minim veniam
- * 6 enim am minim veniam es cillum
- * 7 am minim veniam es cillum dolore eu
- */
- function getText($n) {
- $words = explode(' ', "Ipsum dolore sit am. Ut enim am minim veniam. Es cillum dolore eu.");
- return implode(' ', array_slice($words, $n - 1, $n));
- }
-
-
- * _test2_: Helper method for generating snippets of content.
- *
- * Generated items to test against:
- * 8 dear
- * 9 king philip
- * 10 philip came over
- * 11 came over from germany
- * 12 over from germany swimming
- */
- function getText2($n) {
- $words = explode(' ', "Dear King Philip came over from Germany swimming.");
- return implode(' ', array_slice($words, $n - 1, $n));
- }
-
-
- * Run predefine queries looking for indexed terms.
- */
- function _testQueries() {
-
- Note: OR queries that include short words in OR groups are only accepted
- if the ORed terms are ANDed with at least one long word in the rest of the query.
-
- e.g. enim dolore OR ut = enim (dolore OR ut) = (enim dolor) OR (enim ut) -> good
- e.g. dolore OR ut = (dolore) OR (ut) -> bad
-
- This is a design limitation to avoid full table scans.
- */
- $queries = array(
-
- 'ipsum' => array(1),
- 'enim' => array(4, 5, 6),
- 'xxxxx' => array(),
- 'enim minim' => array(5, 6),
- 'enim xxxxx' => array(),
- 'dolore eu' => array(7),
- 'dolore xx' => array(),
- 'ut minim' => array(5),
- 'xx minim' => array(),
- 'enim veniam am minim ut' => array(5),
-
- 'dolore OR ipsum' => array(1, 2, 7),
- 'dolore OR xxxxx' => array(2, 7),
- 'dolore OR ipsum OR enim' => array(1, 2, 4, 5, 6, 7),
- 'ipsum OR dolore sit OR cillum' => array(2, 7),
- 'minim dolore OR ipsum' => array(7),
- 'dolore OR ipsum veniam' => array(7),
- 'minim dolore OR ipsum OR enim' => array(5, 6, 7),
- 'dolore xx OR yy' => array(),
- 'xxxxx dolore OR ipsum' => array(),
-
- 'dolore -sit' => array(7),
- 'dolore -eu' => array(2),
- 'dolore -xxxxx' => array(2, 7),
- 'dolore -xx' => array(2, 7),
-
- '"dolore sit"' => array(2),
- '"sit dolore"' => array(),
- '"am minim veniam es"' => array(6, 7),
- '"minim am veniam es"' => array(),
-
- '"am minim veniam es" OR dolore' => array(2, 6, 7),
- '"minim am veniam es" OR "dolore sit"' => array(2),
- '"minim am veniam es" OR "sit dolore"' => array(),
- '"am minim veniam es" -eu' => array(6),
- '"am minim veniam" -"cillum dolore"' => array(5, 6),
- '"am minim veniam" -"dolore cillum"' => array(5, 6, 7),
- 'xxxxx "minim am veniam es" OR dolore' => array(),
- 'xx "minim am veniam es" OR dolore' => array()
- );
- foreach ($queries as $query => $results) {
- $result = db_select('search_index', 'i')
- ->extend('SearchQuery')
- ->searchExpression($query, SEARCH_TYPE)
- ->execute();
-
- $set = $result ? $result->fetchAll() : array();
- $this->_testQueryMatching($query, $set, $results);
- $this->_testQueryScores($query, $set, $results);
- }
-
-
- $queries = array(
-
- 'ipsum' => array(),
- 'enim' => array(),
- 'enim minim' => array(),
- 'dear' => array(8),
- 'germany' => array(11, 12),
- );
- foreach ($queries as $query => $results) {
- $result = db_select('search_index', 'i')
- ->extend('SearchQuery')
- ->searchExpression($query, SEARCH_TYPE_2)
- ->execute();
-
- $set = $result ? $result->fetchAll() : array();
- $this->_testQueryMatching($query, $set, $results);
- $this->_testQueryScores($query, $set, $results);
- }
-
-
- $queries = array(
-
- '呂波耳' => array(13),
- '以呂波耳' => array(13),
- 'ほへと ヌルヲ' => array(13),
- 'とちリ' => array(),
- 'ドルーパル' => array(14),
- 'パルが大' => array(14),
- 'コーヒー' => array(15),
- 'ヒーキ' => array(),
- );
- foreach ($queries as $query => $results) {
- $result = db_select('search_index', 'i')
- ->extend('SearchQuery')
- ->searchExpression($query, SEARCH_TYPE_JPN)
- ->execute();
-
- $set = $result ? $result->fetchAll() : array();
- $this->_testQueryMatching($query, $set, $results);
- $this->_testQueryScores($query, $set, $results);
- }
- }
-
-
- * Test the matching abilities of the engine.
- *
- * Verify if a query produces the correct results.
- */
- function _testQueryMatching($query, $set, $results) {
-
- $found = array();
- foreach ($set as $item) {
- $found[] = $item->sid;
- }
-
-
- sort($found);
- sort($results);
- $this->assertEqual($found, $results, "Query matching '$query'");
- }
-
-
- * Test the scoring abilities of the engine.
- *
- * Verify if a query produces normalized, monotonous scores.
- */
- function _testQueryScores($query, $set, $results) {
-
- $scores = array();
- foreach ($set as $item) {
- $scores[] = $item->calculated_score;
- }
-
-
- $sorted = $scores;
- sort($sorted);
- $this->assertEqual($scores, array_reverse($sorted), "Query order '$query'");
-
-
- $this->assertEqual(!count($scores) || (min($scores) > 0.0 && max($scores) <= 1.0001), TRUE, "Query scoring '$query'");
- }
- }
-
- * Tests the bike shed text on no results page, and text on the search page.
- */
- class SearchPageText extends BackdropWebTestCase {
- protected $searching_user;
-
- function setUp() {
- parent::setUp('search');
-
-
- $this->searching_user = $this->backdropCreateUser(array('search content', 'access user profiles'));
- }
-
-
- * Tests the failed search text, and various other text on the search page.
- */
- function testSearchText() {
- $this->backdropLogin($this->searching_user);
- $this->backdropGet('search/node');
- $this->assertText(t('Enter your keywords'));
- $this->assertText(t('Search'));
- $title = t('Search') . ' | Backdrop CMS';
- $this->assertTitle($title, 'Search page title is correct');
-
- $edit = array();
- $edit['keys'] = 'bike shed ' . $this->randomName();
- $this->backdropPost('search/node', $edit, t('Search'));
- $this->assertText(t('Consider loosening your query with OR. bike OR shed will often show more results than bike shed.'), 'Help text is displayed when search returns no results.');
- $this->assertText(t('Search'));
- $this->assertTitle($title, 'Search page title is correct');
-
- $edit['keys'] = $this->searching_user->name;
- $this->backdropPost('search/user', $edit, t('Search'));
- $this->assertText(t('Search'));
- $this->assertTitle($title, 'Search page title is correct');
-
-
-
- $arg = $this->randomName() . '/' . $this->randomName();
- $this->backdropGet('search/node/' . $arg);
- $input = $this->xpath("//input[@id='edit-keys' and @value='{$arg}']");
- $this->assertFalse(empty($input), 'Search keys with a / are correctly set as the default value in the search box.');
-
-
-
- $limit = config_get('search.settings', 'search_and_or_limit');
- $keys = array();
- for ($i = 0; $i < $limit + 1; $i++) {
- $keys[] = $this->randomName(3);
- if ($i % 2 == 0) {
- $keys[] = 'OR';
- }
- }
- $edit['keys'] = implode(' ', $keys);
- $this->backdropPost('search/node', $edit, t('Search'));
- $this->assertRaw(t('Your search used too many AND/OR expressions. Only the first @count terms were included in this search.', array('@count' => $limit)));
- }
- }
-
- * Indexes content and tests the advanced search form.
- */
- class SearchAdvancedSearchForm extends BackdropWebTestCase {
- protected $node;
-
- function setUp() {
- parent::setUp('search');
-
- $test_user = $this->backdropCreateUser(array('access content', 'search content', 'use advanced search', 'administer nodes'));
- $this->backdropLogin($test_user);
-
-
- $node = $this->backdropCreateNode();
- $this->node = $this->backdropCreateNode();
-
-
- node_update_index();
-
-
-
-
- search_update_totals();
- }
-
-
- * Test using the search form with GET and POST queries.
- * Test using the advanced search form to limit search to nodes of type "Page".
- */
- function testNodeType() {
- $this->assertTrue($this->node->type == 'page', 'Node type is Page.');
-
-
- $dummy_title = 'Lorem ipsum';
- $this->assertNotEqual($dummy_title, $this->node->title, "Dummy title doesn't equal node title");
-
-
- $this->backdropGet('search/node/' . $dummy_title);
- $this->assertNoText($this->node->title, 'Page node is not found with dummy title.');
-
-
- $this->backdropGet('search/node/' . $this->node->title);
- $this->assertText($this->node->title, 'Page node is found with GET query.');
-
-
- $edit = array('or' => $this->node->title);
- $this->backdropPost('search/node', $edit, t('Advanced search'));
- $this->assertText($this->node->title, 'Page node is found with POST query.');
-
-
- $this->backdropPost('search/node', array_merge($edit, array('type[page]' => 'page')), t('Advanced search'));
- $this->assertText($this->node->title, 'Page node is found with POST query and type:page.');
-
- $this->backdropPost('search/node', array_merge($edit, array('type[post]' => 'post')), t('Advanced search'));
- $this->assertText('bike shed', 'Post node is not found with POST query and type:post.');
- }
- }
-
- * Indexes content and tests ranking factors.
- */
- class SearchRankingTestCase extends BackdropWebTestCase {
-
- * Implementation setUp().
- */
- function setUp() {
- parent::setUp('search', 'comment');
- }
-
- function testRankings() {
-
- $this->backdropLogin($this->backdropCreateUser(array('skip comment approval', 'create page content')));
-
-
- $node_ranks = array('sticky', 'promote', 'relevance', 'recent', 'comments');
-
-
- foreach ($node_ranks as $node_rank) {
- $settings = array(
- 'type' => 'page',
- 'title' => 'Backdrop rocks',
- 'body' => array(LANGUAGE_NONE => array(array('value' => "Backdrop's search rocks"))),
- );
- foreach (array(0, 1) as $num) {
- if ($num == 1) {
- switch ($node_rank) {
- case 'sticky':
- case 'promote':
- $settings[$node_rank] = 1;
- break;
- case 'relevance':
- $settings['body'][LANGUAGE_NONE][0]['value'] .= " really rocks";
- break;
- case 'recent':
- $settings['created'] = REQUEST_TIME + 3600;
- break;
- case 'comments':
- $settings['comment'] = 2;
- break;
- }
- }
- $nodes[$node_rank][$num] = $this->backdropCreateNode($settings);
- }
- }
-
-
- module_invoke_all('update_index');
- search_update_totals();
-
-
- $this->refreshVariables();
-
-
- $edit = array();
- $edit['comment_body[' . LANGUAGE_NONE . '][0][value]'] = 'some random comment';
- $this->backdropGet('comment/reply/' . $nodes['comments'][1]->nid);
- $this->backdropPost(NULL, $edit, t('Preview'));
- $this->backdropPost(NULL, $edit, t('Save'));
-
-
- foreach ($node_ranks as $node_rank) {
-
- foreach ($node_ranks as $var) {
- config_set('search.settings', 'node_rank_' . $var, $var == $node_rank ? 10 : 0);
- }
-
-
- $set = node_search_execute('rocks');
- $this->assertEqual($set[0]['node']->nid, $nodes[$node_rank][1]->nid, 'Search ranking "' . $node_rank . '" order.');
- }
- }
-
-
- * Test rankings of HTML tags.
- */
- function testHTMLRankings() {
-
- $this->backdropLogin($this->backdropCreateUser(array('create page content')));
-
-
- $sorted_tags = array('h1', 'h2', 'h3', 'h4', 'a', 'h5', 'h6', 'notag');
- $shuffled_tags = $sorted_tags;
-
-
- shuffle($shuffled_tags);
- $settings = array(
- 'type' => 'page',
- 'title' => 'Simple node',
- );
- foreach ($shuffled_tags as $tag) {
- switch ($tag) {
- case 'a':
- $settings['body'] = array(LANGUAGE_NONE => array(array('value' => l('Backdrop Rocks', 'node'), 'format' => 'full_html')));
- break;
- case 'notag':
- $settings['body'] = array(LANGUAGE_NONE => array(array('value' => 'Backdrop Rocks')));
- break;
- default:
- $settings['body'] = array(LANGUAGE_NONE => array(array('value' => "<$tag>Backdrop Rocks</$tag>", 'format' => 'full_html')));
- break;
- }
- $nodes[$tag] = $this->backdropCreateNode($settings);
- }
-
-
- module_invoke_all('update_index');
- search_update_totals();
-
-
- $this->refreshVariables();
-
-
- $node_ranks = array('sticky', 'promote', 'recent', 'comments');
- foreach ($node_ranks as $node_rank) {
- config_set('search.settings', 'node_rank_' . $node_rank, 0);
- }
- $set = node_search_execute('rocks');
-
-
- foreach ($sorted_tags as $tag_rank => $tag) {
-
- if ($tag == 'notag') {
- $this->assertEqual($set[$tag_rank]['node']->nid, $nodes[$tag]->nid, 'Search tag ranking for plain text order.');
- } else {
- $this->assertEqual($set[$tag_rank]['node']->nid, $nodes[$tag]->nid, 'Search tag ranking for "<' . $sorted_tags[$tag_rank] . '>" order.');
- }
- }
-
-
- $unsorted_tags = array('u', 'b', 'i', 'strong', 'em');
- foreach ($unsorted_tags as $tag) {
- $settings['body'] = array(LANGUAGE_NONE => array(array('value' => "<$tag>Backdrop Rocks</$tag>", 'format' => 'full_html')));
- $node = $this->backdropCreateNode($settings);
-
-
- module_invoke_all('update_index');
- search_update_totals();
-
-
- $this->refreshVariables();
-
- $set = node_search_execute('rocks');
-
-
- $set = array_slice($set, -2, 1);
-
-
- $this->assertEqual($set[0]['node']->nid, $node->nid, 'Search tag ranking for "<' . $tag . '>" order.');
-
-
- node_delete($node->nid);
- }
- }
-
-
- * Verifies that if we combine two rankings, search still works.
- *
- * See issue http://drupal.org/node/771596
- */
- function testDoubleRankings() {
-
- $this->backdropLogin($this->backdropCreateUser(array('skip comment approval', 'create page content')));
-
-
- $settings = array(
- 'type' => 'page',
- 'title' => 'Backdrop rocks',
- 'body' => array(LANGUAGE_NONE => array(array('value' => "Backdrop's search rocks"))),
- 'sticky' => 1,
- );
-
- $node = $this->backdropCreateNode($settings);
-
-
- module_invoke_all('update_index');
- search_update_totals();
-
-
- $this->refreshVariables();
-
-
-
- $node_ranks = array('sticky', 'promote', 'relevance', 'recent', 'comments');
- foreach ($node_ranks as $var) {
- $value = ($var == 'sticky' || $var == 'comments') ? 10 : 0;
- config_set('search.settings', 'node_rank_' . $var, $value);
- }
-
-
- $set = node_search_execute('rocks');
- $this->assertEqual($set[0]['node']->nid, $node->nid, 'Search double ranking order.');
- }
- }
-
- * Tests the rendering of the search block.
- */
- class SearchBlockTestCase extends BackdropWebTestCase {
- function setUp() {
- parent::setUp('search');
-
-
- $admin_user = $this->backdropCreateUser(array('administer blocks', 'search content'));
- $this->backdropLogin($admin_user);
- }
-
-
- * Test that the search block form works correctly.
- */
- function testBlock() {
-
- $layout = layout_load('default');
- $layout->addBlock('search', 'form', 'content');
- $layout->save();
-
-
- $terms = array('search_block_form' => 'test');
- $this->backdropPost('user', $terms, t('Search'));
- $this->assertText('Your search yielded no results');
-
-
- $this->backdropGet('foo');
- $this->assertResponse(404);
- $this->backdropPost(NULL, $terms, t('Search'));
- $this->assertResponse(200);
- $this->assertText('Your search yielded no results');
-
-
- $this->assertEqual(
- $this->getUrl(),
- url('search/node/' . $terms['search_block_form'], array('absolute' => TRUE)),
- 'Redirected to correct url.'
- );
-
-
- $terms = array('search_block_form' => '');
- $this->backdropPost('user', $terms, t('Search'));
- $this->assertText('Please enter some keywords');
-
-
- $this->assertEqual(
- $this->getUrl(),
- url('search/node/', array('absolute' => TRUE)),
- 'Redirected to correct url.'
- );
-
-
-
- $terms = array('search_block_form' => 'a');
- $this->backdropPost('user', $terms, t('Search'));
- $this->assertText('You must include at least one keyword to match in the content. Keywords must be at least 3 characters, and punctuation is ignored.');
- $terms = array('search_block_form' => 'foo');
- $this->backdropPost(NULL, $terms, t('Search'));
- $this->assertNoText('You must include at least one keyword to match in the content. Keywords must be at least 3 characters, and punctuation is ignored.');
- $this->assertText('Your search yielded no results');
-
-
- $terms = array('search_block_form' => 'a');
- $this->backdropPost('user', $terms, t('Search'));
- $terms = array('keys' => 'foo');
- $this->backdropPost(NULL, $terms, t('Search'));
- $this->assertNoText('You must include at least one keyword to match in the content. Keywords must be at least 3 characters, and punctuation is ignored.');
- $this->assertText('Your search yielded no results');
- }
- }
-
- * Tests that searching for a phrase gets the correct page count.
- */
- class SearchExactTestCase extends BackdropWebTestCase {
- function setUp() {
- parent::setUp('search');
- }
-
-
- * Tests that the correct number of pager links are found for both keywords and phrases.
- */
- function testExactQuery() {
-
- $this->backdropLogin($this->backdropCreateUser(array('create page content', 'search content')));
-
- $settings = array(
- 'type' => 'page',
- 'title' => 'Simple Node',
- );
-
- for ($i = 0; $i <= 17; $i++) {
- $settings['body'] = array(LANGUAGE_NONE => array(array('value' => 'love pizza')));
- $this->backdropCreateNode($settings);
- }
-
- for ($i = 0; $i <= 17; $i++) {
- $settings['body'] = array(LANGUAGE_NONE => array(array('value' => 'love cheesy pizza')));
- $this->backdropCreateNode($settings);
- }
-
-
- module_invoke_all('update_index');
- search_update_totals();
-
-
- $this->refreshVariables();
-
-
- $edit = array('keys' => 'love pizza');
- $this->backdropPost('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.');
-
-
- $edit = array('keys' => '"love pizza"');
- $this->backdropPost('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.');
- }
- }
-
- * Test integration searching comments.
- */
- class SearchCommentTestCase extends BackdropWebTestCase {
- protected $admin_user;
- protected $profile = 'testing';
-
-
- * @var string
- */
- protected $comment_subject;
-
-
- * @var string
- */
- protected $admin_role;
-
-
- * @var Node
- */
- protected $node;
-
- function setUp() {
- parent::setUp('comment', 'search');
-
-
- config_set('system.core', 'cache', '0');
-
-
- $this->backdropCreateContentType(array(
- 'type' => 'post',
- 'name' => 'Post',
- 'settings' => array(
- 'comment_default' => COMMENT_NODE_OPEN,
- 'comment_preview' => BACKDROP_OPTIONAL,
- 'comment_title_options' => COMMENT_TITLE_CUSTOM,
- ),
- ));
- $full_html_format = (object) array(
- 'format' => 'full_html',
- 'name' => 'Raw HTML',
- 'weight' => 1,
- 'editor' => NULL,
- 'filters' => array(),
- );
- filter_format_save($full_html_format);
- $this->checkPermissions(array(), TRUE);
-
- $anonymous_permissions = array(
- 'access content',
- 'access comments',
- );
- $authenticated_permissions = array(
- 'access content',
- 'access comments',
- 'post comments',
- );
- user_role_grant_permissions(BACKDROP_ANONYMOUS_ROLE, $anonymous_permissions);
- user_role_grant_permissions(BACKDROP_AUTHENTICATED_ROLE, $authenticated_permissions);
-
-
-
- $permissions = array(
- 'administer filters',
- filter_permission_name($full_html_format),
- 'administer permissions',
- 'create post content',
- 'post comments',
- 'skip comment approval',
- 'access comments',
- );
- $this->admin_user = $this->backdropCreateUser($permissions);
- $this->backdropLogin($this->admin_user);
-
-
- $layout = layout_load('default');
- $layout->addBlock('search', 'form', 'content');
- $layout->save();
- }
-
-
- * Verify that comments are rendered using proper format in search results.
- */
- function testSearchResultsComment() {
- $comment_body = 'Test comment body';
-
-
- $edit = array(
- BACKDROP_ANONYMOUS_ROLE . '[search content]' => 1,
- BACKDROP_ANONYMOUS_ROLE . '[access comments]' => 1,
- BACKDROP_ANONYMOUS_ROLE . '[post comments]' => 1,
- );
- $this->backdropPost('admin/config/people/permissions', $edit, t('Save permissions'));
-
-
- $node = $this->backdropCreateNode(array('type' => 'post'));
-
- $edit_comment = array();
- $edit_comment['subject'] = 'Test comment subject';
- $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][value]'] = '<h1>' . $comment_body . '</h1>';
- $full_html_format_id = 'full_html';
- $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][format]'] = $full_html_format_id;
- $this->backdropPost('comment/reply/' . $node->nid, $edit_comment, t('Save'));
-
-
- $this->backdropLogout();
- $this->cronRun();
-
-
- $edit = array(
- 'search_block_form' => "'" . $edit_comment['subject'] . "'",
- );
- $this->backdropPost('user', $edit, t('Search'));
- $this->assertText($node->title, 'Node found in search results.');
- $this->assertText($edit_comment['subject'], 'Comment subject found in search results.');
-
-
- $edit = array(
- 'search_block_form' => "'" . $comment_body . "'",
- );
- $this->backdropPost('user', $edit, t('Search'));
- $this->assertText($node->title, t('Node found in search results.'));
-
-
- $this->assertText($comment_body, t('Comment body text found in search results.'));
- $this->assertNoRaw(t('n/a'), t('HTML in comment body is not hidden.'));
- $this->assertNoRaw(check_plain($edit_comment['comment_body[' . LANGUAGE_NONE . '][0][value]']), 'HTML in comment body is not escaped.');
-
-
- $this->backdropLogin($this->admin_user);
- $node->comment = 0;
- $node->save();
-
-
- $this->backdropLogout();
- $this->cronRun();
-
-
- $this->backdropPost('user', $edit, t('Search'));
- $this->assertNoText($comment_body, 'Comment body text not found in search results.');
- }
-
-
- * Verify access rules for comment indexing with different permissions.
- */
- function testSearchResultsCommentAccess() {
- $comment_body = 'Test comment body';
- $this->comment_subject = 'Test comment subject';
- $roles = array_diff($this->admin_user->roles, array(BACKDROP_AUTHENTICATED_ROLE));
- $this->admin_role = reset($roles);
-
-
- $this->node = $this->backdropCreateNode(array('type' => 'post'));
-
-
- $edit_comment = array();
- $edit_comment['subject'] = $this->comment_subject;
- $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][value]'] = '<h1>' . $comment_body . '</h1>';
- $this->backdropPost('comment/reply/' . $this->node->nid, $edit_comment, t('Save'));
-
- $this->backdropLogout();
- $this->setRolePermissions(BACKDROP_ANONYMOUS_ROLE);
- $this->assertCommentAccess('Anon user has search permission but no access comments permission, comments should not be indexed');
-
- $this->setRolePermissions(BACKDROP_ANONYMOUS_ROLE, TRUE);
- $this->assertCommentAccess('Anon user has search permission and access comments permission, comments should be indexed', TRUE);
-
- $this->backdropLogin($this->admin_user);
- $this->backdropGet('admin/config/people/permissions');
-
-
- $this->setRolePermissions(BACKDROP_AUTHENTICATED_ROLE, FALSE, FALSE);
-
- $this->setRolePermissions($this->admin_role);
- $this->assertCommentAccess('Admin user has search permission but no access comments permission, comments should not be indexed');
-
- $this->setRolePermissions($this->admin_role, TRUE);
- $this->assertCommentAccess('Admin user has search permission and access comments permission, comments should be indexed', TRUE);
-
- $this->setRolePermissions(BACKDROP_AUTHENTICATED_ROLE);
- $this->assertCommentAccess('Authenticated user has search permission but no access comments permission, comments should not be indexed');
-
- $this->setRolePermissions(BACKDROP_AUTHENTICATED_ROLE, TRUE);
- $this->assertCommentAccess('Authenticated user has search permission and access comments permission, comments should be indexed', TRUE);
-
-
-
- $this->setRolePermissions(BACKDROP_AUTHENTICATED_ROLE, TRUE, FALSE);
- $this->setRolePermissions($this->admin_role);
- $this->assertCommentAccess('Admin user has search permission and no access comments permission, but comments should be indexed because admin user inherits authenticated user\'s permission to access comments', TRUE);
-
-
-
- $this->setRolePermissions(BACKDROP_AUTHENTICATED_ROLE, TRUE, TRUE);
- $this->setRolePermissions($this->admin_role, TRUE, FALSE);
- $this->assertCommentAccess('Admin user has access comments permission and no search permission, but comments should be indexed because admin user inherits authenticated user\'s permission to search', TRUE);
-
- }
-
-
- * Set permissions for role.
- */
- function setRolePermissions($role_name, $access_comments = FALSE, $search_content = TRUE) {
- $permissions = array(
- 'access comments' => $access_comments,
- 'search content' => $search_content,
- );
- user_role_change_permissions($role_name, $permissions);
- }
-
-
- * Update search index and search for comment.
- */
- function assertCommentAccess($message, $assume_access = FALSE) {
-
- search_touch_node($this->node->nid);
- $this->cronRun();
-
-
- $edit = array(
- 'search_block_form' => "'" . $this->comment_subject . "'",
- );
- $this->backdropPost('user', $edit, t('Search'));
- $method = $assume_access ? 'assertText' : 'assertNoText';
- $verb = $assume_access ? 'found' : 'not found';
- $this->{$method}($this->node->title, "Node $verb in search results: " . $message);
- $this->{$method}($this->comment_subject, "Comment subject $verb in search results: " . $message);
- }
-
-
- * Verify that 'Add comment' does not appear in search results or index.
- */
- function testAddNewComment() {
-
- $settings = array(
- 'type' => 'post',
- 'title' => 'short title',
- 'body' => array(LANGUAGE_NONE => array(array('value' => 'short body text'))),
- );
-
- $user = $this->backdropCreateUser(array('search content', 'create post content', 'access content'));
- $this->backdropLogin($user);
-
- $node = $this->backdropCreateNode($settings);
-
-
- $this->backdropGet('node/' . $node->nid);
- $this->assertText(t('Add comment'), '"Add comment" appears on node page');
-
-
- $this->backdropLogout();
- $this->cronRun();
-
-
- $this->backdropLogin($user);
- $this->backdropPost('search/node', array('keys' => 'comment'), t('Search'));
- $this->assertText(t('Your search yielded no results'), 'No results searching for the word comment');
-
-
-
- $this->backdropPost('search/node', array('keys' => 'short'), t('Search'));
- $this->assertText($node->title, 'Search for keyword worked');
- $this->assertNoText(t('Add comment'), '"Add comment" does not appear on search results page');
- }
-
- }
-
- * Tests search_expression_insert() and search_expression_extract().
- *
- * @see http://drupal.org/node/419388 (issue)
- */
- class SearchExpressionInsertExtractTestCase extends BackdropUnitTestCase {
- function setUp() {
- backdrop_load('module', 'search');
- parent::setUp();
- }
-
-
- * Tests search_expression_insert() and search_expression_extract().
- */
- function testInsertExtract() {
- $base_expression = "mykeyword";
-
-
- $cases = array(
- array('foo', 'bar', 'foo:bar', 'bar'),
- array('foo', NULL, '', NULL),
- array('foo', ' ', 'foo:', ''),
- array('foo', '', 'foo:', ''),
- array('foo', '0', 'foo:0', '0'),
- array('foo', 0, 'foo:0', '0'),
- );
-
- foreach ($cases as $index => $case) {
- $after_insert = search_expression_insert($base_expression, $case[0], $case[1]);
- if (empty($case[2])) {
- $this->assertEqual($after_insert, $base_expression, "Empty insert does not change expression in case $index");
- }
- else {
- $this->assertEqual($after_insert, $base_expression . ' ' . $case[2], "Insert added correct expression for case $index");
- }
-
- $retrieved = search_expression_extract($after_insert, $case[0]);
- if (!isset($case[3])) {
- $this->assertFalse(isset($retrieved), "Empty retrieval results in unset value in case $index");
- }
- else {
- $this->assertEqual($retrieved, $case[3], "Value is retrieved for case $index");
- }
-
- $after_clear = search_expression_insert($after_insert, $case[0]);
- $this->assertEqual(trim($after_clear), $base_expression, "After clearing, base expression is restored for case $index");
-
- $cleared = search_expression_extract($after_clear, $case[0]);
- $this->assertFalse(isset($cleared), "After clearing, value could not be retrieved for case $index");
- }
- }
- }
-
- * Tests that comment count display toggles properly on comment status of node
- *
- * Issue 537278
- *
- * - Nodes with comment status set to Open should always show 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.
- */
- class SearchCommentCountToggleTestCase extends BackdropWebTestCase {
- protected $searching_user;
- protected $searchable_nodes;
-
- function setUp() {
- parent::setUp('search');
-
-
- $this->searching_user = $this->backdropCreateUser(array('search content', 'access content', 'access comments', 'skip comment approval'));
-
-
- $node_params = array('type' => 'post', 'body' => array(LANGUAGE_NONE => array(array('value' => 'SearchCommentToggleTestCase'))));
-
- $this->searchable_nodes['1 comment'] = $this->backdropCreateNode($node_params);
- $this->searchable_nodes['0 comments'] = $this->backdropCreateNode($node_params);
-
-
- $this->backdropLogin($this->searching_user);
-
-
- $edit_comment = array();
- $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][value]'] = $this->randomName();
- $filtered_html_format_id = 'filtered_html';
- $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][format]'] = $filtered_html_format_id;
-
-
- $this->backdropPost('comment/reply/' . $this->searchable_nodes['1 comment']->nid, $edit_comment, t('Save'));
-
-
- node_update_index();
-
-
-
-
- search_update_totals();
-
-
- $layout = layout_load('default');
- $layout->addBlock('search', 'form', 'content');
- $layout->save();
- }
-
-
- * Verify that comment count displays correctly based on comment status of
- * node.
- */
- function testSearchCommentCountToggle() {
-
- $edit = array(
- 'search_block_form' => "'SearchCommentToggleTestCase'",
- );
-
-
- $this->backdropPost('user', $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');
-
-
- $this->searchable_nodes['0 comments']->comment = COMMENT_NODE_CLOSED;
- node_save($this->searchable_nodes['0 comments']);
- $this->searchable_nodes['1 comment']->comment = COMMENT_NODE_CLOSED;
- node_save($this->searchable_nodes['1 comment']);
-
- $this->backdropPost('user', $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');
-
-
- $this->searchable_nodes['0 comments']->comment = COMMENT_NODE_HIDDEN;
- node_save($this->searchable_nodes['0 comments']);
- $this->searchable_nodes['1 comment']->comment = COMMENT_NODE_HIDDEN;
- node_save($this->searchable_nodes['1 comment']);
-
- $this->backdropPost('user', $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');
- }
- }
-
- * Test search_simplify() on every Unicode character, and some other cases.
- */
- class SearchSimplifyTestCase extends BackdropWebTestCase {
-
- * Tests that all Unicode characters simplify correctly.
- */
- function testSearchSimplifyUnicode() {
-
-
-
-
-
-
-
- $input = file_get_contents(BACKDROP_ROOT . '/core/modules/search/tests/UnicodeTest.txt');
- $base_strings = explode(chr(10), $input);
- $strings = array();
- foreach ($base_strings as $key => $string) {
- if ($key %2) {
-
- $simplified = search_simplify($string);
- $this->assertIdentical($simplified, ' ', "Line $key is excluded from the index");
- }
- else {
-
-
-
- $start = 0;
- while ($start < backdrop_strlen($string)) {
- $new_string = backdrop_substr($string, $start, 30);
-
-
-
- if (preg_match('/^[0-9]+$/', $new_string)) {
- $new_string = '1' . $new_string;
- }
- $strings[] = $new_string;
- $start += 30;
- }
- }
- }
- foreach ($strings as $key => $string) {
- $simplified = search_simplify($string);
- $this->assertTrue(backdrop_strlen($simplified) >= backdrop_strlen($string), "Nothing is removed from string $key.");
- }
-
-
-
- $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...backdrop--module', 'great backdrop module', 'Multiple dot and dashes are word boundaries'),
- array('very_great-backdrop.module', 'verygreatbackdropmodule', '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]);
- }
- }
- }
-
-
- * Tests keywords and conditions.
- */
- class SearchKeywordsConditions extends BackdropWebTestCase {
-
-
- * @var User
- */
- protected $searching_user;
-
- function setUp() {
- parent::setUp('search', 'search_extra_type');
-
- $this->searching_user = $this->backdropCreateUser(array('search content', 'access content', 'access comments', 'skip comment approval'));
-
- $this->backdropLogin($this->searching_user);
-
- config_set('search.settings', 'search_active_modules', array('node', 'user', 'search_extra_type'));
- menu_rebuild();
- }
-
-
- * Verify the keywords are captured and conditions respected.
- */
- function testSearchKeywordsConditions() {
-
- $this->backdropGet('search/dummy_path');
- $this->assertNoText('Dummy search snippet to display');
-
- $keys = 'bike shed ' . $this->randomName();
- $this->backdropGet("search/dummy_path/{$keys}");
- $this->assertText("Dummy search snippet to display. Keywords: {$keys}");
- $keys = 'blue drop ' . $this->randomName();
- $this->backdropGet("search/dummy_path", array('query' => array('keys' => $keys)));
- $this->assertText("Dummy search snippet to display. Keywords: {$keys}");
-
- $keys = 'moving drop ' . $this->randomName();
- $this->backdropGet("search/dummy_path/bike", array('query' => array('search_conditions' => $keys)));
- $this->assertText("Dummy search snippet to display.");
- $this->assertRaw(print_r(array('search_conditions' => $keys), TRUE));
-
- $keys = 'drop kick ' . $this->randomName();
- $this->backdropGet("search/dummy_path", array('query' => array('search_conditions' => $keys)));
- $this->assertText("Dummy search snippet to display.");
- $this->assertRaw(print_r(array('search_conditions' => $keys), TRUE));
- }
- }
-
- * Tests that numbers can be searched.
- */
- class SearchNumbersTestCase extends BackdropWebTestCase {
- protected $test_user;
- protected $numbers;
- protected $nodes;
-
- function setUp() {
- parent::setUp('search');
-
- $this->test_user = $this->backdropCreateUser(array('search content', 'access content', 'administer nodes', 'access site reports'));
- $this->backdropLogin($this->test_user);
-
-
-
-
- $this->numbers = array(
- 'ISBN' => '978-0446365383',
- 'UPC' => '036000 291452',
- 'EAN bar code' => '5901234123457',
- 'negative' => '-123456.7890',
- 'quoted negative' => '"-123456.7890"',
- 'leading zero' => '0777777777',
- 'tiny' => '111',
- 'small' => '22222222222222',
- 'medium' => '333333333333333333333333333',
- 'large' => '444444444444444444444444444444444444444',
- 'gigantic' => '5555555555555555555555555555555555555555555555555',
- 'over fifty characters' => '666666666666666666666666666666666666666666666666666666666666',
- 'date', '01/02/2009',
- 'commas', '987,654,321',
- );
-
- foreach ($this->numbers as $doc => $num) {
- $info = array(
- 'body' => array(LANGUAGE_NONE => array(array('value' => $num))),
- 'type' => 'page',
- 'langcode' => LANGUAGE_NONE,
- 'title' => $doc . ' number',
- );
- $this->nodes[$doc] = $this->backdropCreateNode($info);
- }
-
-
- $this->cronRun();
- $this->backdropGet('admin/reports/dblog');
- $this->assertText(t('Cron run completed'), 'Log shows cron run completed');
- }
-
-
- * Tests that all the numbers can be searched.
- */
- function testNumberSearching() {
- $types = array_keys($this->numbers);
-
- foreach ($types as $type) {
- $number = $this->numbers[$type];
-
-
- $number = ltrim($number, '-');
- $node = $this->nodes[$type];
-
-
-
- $this->backdropPost('search/node',
- array('keys' => 'foo'),
- t('Search'));
- $this->assertNoText($node->title, format_string('%type: node title not shown in dummy search', array('%type' => $type)));
-
-
-
- $this->backdropPost('search/node',
- array('keys' => $number),
- t('Search'));
- $this->assertText($node->title, format_string('%type: node title shown (search found the node) in search for number %number.', array('%type' => $type, '%number' => $number)));
- }
- }
- }
-
- * Tests that numbers can be searched, with more complex matching.
- */
- class SearchNumberMatchingTestCase extends BackdropWebTestCase {
- protected $test_user;
- protected $numbers;
- protected $nodes;
-
- function setUp() {
- parent::setUp('search');
-
- $this->test_user = $this->backdropCreateUser(array('search content', 'access content', 'administer nodes', 'access site reports'));
- $this->backdropLogin($this->test_user);
-
-
-
-
-
- $this->numbers = array(
- '123456789',
- '12/34/56789',
- '12.3456789',
- '12-34-56789',
- '123,456,789',
- '-123456789',
- '0123456789',
- );
-
- foreach ($this->numbers as $num) {
- $info = array(
- 'body' => array(LANGUAGE_NONE => array(array('value' => $num))),
- 'type' => 'page',
- 'langcode' => LANGUAGE_NONE,
- );
- $this->nodes[] = $this->backdropCreateNode($info);
- }
-
-
- $this->cronRun();
- $this->backdropGet('admin/reports/dblog');
- $this->assertText(t('Cron run completed'), 'Log shows cron run completed');
- }
-
-
- * Tests that all the numbers can be searched.
- */
- function testNumberSearching() {
- for ($i = 0; $i < count($this->numbers); $i++) {
- $node = $this->nodes[$i];
-
-
-
- $this->backdropPost('search/node',
- array('keys' => 'foo'),
- t('Search'));
- $this->assertNoText($node->title, format_string('%number: node title not shown in dummy search', array('%number' => $i)));
-
-
-
- for ($j = 0; $j < count($this->numbers); $j++) {
- $number = $this->numbers[$j];
-
-
- $number = ltrim($number, '-');
-
- $this->backdropPost('search/node',
- array('keys' => $number),
- t('Search'));
- $this->assertText($node->title, format_string('%i: node title shown (search found the node) in search for number %number', array('%i' => $i, '%number' => $number)));
- }
- }
-
- }
- }
-
- * Test config page.
- */
- class SearchConfigSettingsForm extends BackdropWebTestCase {
- public $search_user;
- public $search_node;
-
- function setUp() {
- parent::setUp('search', 'search_extra_type');
-
-
- $this->search_user = $this->backdropCreateUser(array('search content', 'use advanced search', 'administer search', 'administer nodes', 'bypass node access', 'access user profiles', 'administer users', 'administer blocks', 'access site reports'));
- $this->backdropLogin($this->search_user);
-
-
- $nids = (array) db_query('SELECT nid FROM {node}')->fetchCol();
- node_delete_multiple($nids);
-
-
- $node = $this->backdropCreateNode();
- $this->search_node = $node;
-
-
- $langcode = LANGUAGE_NONE;
- $body_key = "body[$langcode][0][value]";
- $edit[$body_key] = l($node->title, 'node/' . $node->nid) . ' pizza sandwich';
- $this->backdropPost('node/' . $node->nid . '/edit', $edit, t('Save'));
-
- node_update_index();
- search_update_totals();
-
-
- $layout = layout_load('default');
- $layout->addBlock('search', 'form', 'content');
- $layout->save();
- }
-
-
- * Verify the search settings form.
- */
- function testSearchSettingsPage() {
-
-
- $this->backdropGet('admin/config/search/settings');
- $this->assertText(t('There are @count items left to index.', array('@count' => 0)));
-
-
- search_reindex();
-
-
- $body_text = 'Gluten-free.';
- $this->backdropCreateNode(array(
- 'body' => array(LANGUAGE_NONE => array(array('value' => $body_text))),
- ));
-
- $this->backdropGet('admin/config/search/settings');
- $this->assertText(t('There are @count items left to index.', array('@count' => 2)));
-
-
- $this->backdropPost('admin/config/search/settings', array(), t('Rebuild search index'));
- $this->assertText(t('This action will rebuild the search index. It may be a lengthy process.'));
- $this->backdropPost('admin/config/search/settings/reindex', array(), t('Rebuild search index'));
-
-
- $this->backdropGet('admin/config/search/settings');
- $this->assertText(t('There are 0 items left to index.'));
-
-
- $this->backdropGet('search/node/' . rawurlencode($body_text));
- $this->assertText($body_text, t('New node was correctly indexed after rebuilding'));
-
-
- $this->backdropPost('admin/config/search/settings', array(), t('Save configuration'));
- $this->assertText(t('The active search modules have been changed.'), 'Form saves with the default values.');
-
-
- $edit = array(
- 'search_minimum_word_size' => $this->randomName(3),
- );
- $this->backdropPost('admin/config/search/settings', $edit, t('Save configuration'));
- $this->assertNoText(t('The active search modules have been changed.'), 'Form does not save with an invalid word length.');
-
-
- $config = config('search.settings');
- $config->set('search_logging', TRUE)->save();
- $text = $this->randomName(5);
- $this->backdropPost('search/node', array('keys' => $text), t('Search'));
- $this->backdropGet('admin/reports/dblog');
- $this->assertLink('Searched Content for ' . $text . '.', 0, 'Search was logged');
-
-
- $config->set('search_logging', FALSE)->save();
- $text = $this->randomName(5);
- $this->backdropPost('search/node', array('keys' => $text), t('Search'));
- $this->backdropGet('admin/reports/dblog');
- $this->assertNoLink('Searched Content for ' . $text . '.', 'Search was not logged');
- }
-
-
- * Verify that you can disable individual search modules.
- */
- function testSearchModuleDisabling() {
-
-
-
- $module_info = array(
- 'node' => array(
- 'path' => 'node',
- 'title' => 'Content',
- 'keys' => 'pizza',
- 'text' => $this->search_node->title,
- ),
- 'user' => array(
- 'path' => 'user',
- 'title' => 'User',
- 'keys' => $this->search_user->name,
- 'text' => $this->search_user->mail,
- ),
- 'search_extra_type' => array(
- 'path' => 'dummy_path',
- 'title' => 'Dummy search type',
- 'keys' => 'foo',
- 'text' => 'Dummy search snippet to display',
- ),
- );
- $modules = array_keys($module_info);
-
-
- foreach ($modules as $module) {
-
- $info = $module_info[$module];
- $edit = array();
- foreach ($modules as $other) {
- $edit['search_active_modules[' . $other . ']'] = (($other == $module) ? $module : FALSE);
- }
- $edit['search_default_module'] = $module;
- $this->backdropPost('admin/config/search/settings', $edit, t('Save configuration'));
-
-
- $this->backdropGet('search/' . $info['path'] . '/' . $info['keys']);
- $this->assertNoText('no results', $info['title'] . ' search found results');
- $this->assertText($info['text'], 'Correct search text found');
-
-
- foreach ($modules as $other) {
- if ($other != $module) {
- $title = $module_info[$other]['title'];
- $this->assertNoText($title, $title . ' search tab is not shown');
- }
- }
-
-
-
- $terms = array('search_block_form' => $info['keys']);
- $this->backdropPost('user', $terms, t('Search'));
- $this->assertEqual(
- $this->getURL(),
- url('search/' . $info['path'] . '/' . $info['keys'], array('absolute' => TRUE)),
- 'Block redirected to right search page');
-
-
- $this->backdropGet('search/not_a_module_path');
- $this->assertEqual(
- $this->getURL(),
- url('search/' . $info['path'], array('absolute' => TRUE)),
- 'Invalid search path redirected to default search page');
- }
-
-
-
- $edit = array();
- foreach ($modules as $module) {
- $edit['search_active_modules[' . $module . ']'] = $module;
- }
- $edit['search_default_module'] = 'node';
-
- $this->backdropPost('admin/config/search/settings', $edit, t('Save configuration'));
-
- foreach (array('search/node/pizza', 'search/node') as $path) {
- $this->backdropGet($path);
- foreach ($modules as $module) {
- $title = $module_info[$module]['title'];
- $this->assertText($title, format_string('%title search tab is shown', array('%title' => $title)));
- }
- }
- }
-
-
- * Verify enabling/disabling of certain node types.
- */
- function testSearchNodeTypes() {
- $new_type1 = $this->backdropCreateContentType();
- $new_type2 = $this->backdropCreateContentType();
- $new_type3 = $this->backdropCreateContentType();
-
-
- $node1 = $this->backdropCreateNode(array(
- 'title' => 'Rabbits',
- 'body' => array(LANGUAGE_NONE => array(array('value' => "The bunny's ears were fuzzy."))),
- 'type' => $new_type1->type,
- ));
- $node2 = $this->backdropCreateNode(array(
- 'title' => 'Llamas',
- 'body' => array(LANGUAGE_NONE => array(array('value' => "Llamas are fuzzy all over."))),
- 'type' => $new_type2->type,
- ));
-
-
- module_invoke_all('update_index');
- search_update_totals();
-
-
- $edit['node_types[page]'] = TRUE;
- $edit['node_types[post]'] = TRUE;
- $edit['node_types[' . $new_type1->type . ']'] = TRUE;
- $edit['node_types[' . $new_type2->type . ']'] = FALSE;
- $edit['node_types[' . $new_type3->type . ']'] = FALSE;
- $this->backdropPost('admin/config/search/settings', $edit, t('Save configuration'));
-
-
- $node3 = $this->backdropCreateNode(array(
- 'title' => 'Fish',
- 'body' => array(LANGUAGE_NONE => array(array('value' => "These creatures are scaly but beautiful. Not fuzzy."))),
- 'type' => $new_type3->type,
- ));
-
-
- module_invoke_all('update_index');
- search_update_totals();
-
-
-
- $this->backdropGet('search/node/fuzzy');
- $this->assertText($node1->type, 'Enabled first node type found on advanced search form.');
- $this->assertNoText($node2->type, 'Disabled second node type not found on advanced search form.');
- $this->assertNoText($node3->type, 'Disabled third node type not found on advanced search form.');
-
-
- $this->backdropGet('search/node/fuzzy');
- $this->assertText($node1->title, 'Fuzzy bunny ears found in search. Node type is enabled.');
- $this->assertNoText($node2->title, 'Fuzzy llamas not found in search. Node type is disabled.');
- $this->assertNoText($node3->title, 'Scaly fish not found in search. Node type is disabled.');
-
-
- $result = db_select('search_index', 'i')
- ->extend('SearchQuery')
- ->searchexpression($node2->title, 'node')
- ->execute();
- $this->assertEqual($result->rowCount(), 1, 'Node correctly still indexed after disabling its type.');
-
-
- $result = db_select('search_index', 'i')
- ->extend('SearchQuery')
- ->searchexpression($node3->title, 'node')
- ->execute();
- $this->assertEqual($result->rowCount(), 0, 'New nodes of disabled type are not added to search index.');
- }
-
- }
-
- * Tests the search_excerpt() function.
- */
- class SearchExcerptTestCase extends BackdropWebTestCase {
- function setUp() {
- parent::setUp('search');
- }
-
-
- * Tests search_excerpt() with several simulated search keywords.
- *
- * Passes keywords and a sample marked up string, "The quick
- * brown fox jumps over the lazy dog", and compares it to the
- * correctly marked up string. The correctly marked up string
- * contains either highlighted keywords or the original marked
- * up string if no keywords matched the string.
- */
- function testSearchExcerpt() {
-
- $text = 'The <strong>quick</strong> <a href="#">brown</a> fox & jumps <h2>over</h2> the lazy dog';
-
-
- $expected = 'The quick brown fox & jumps over the lazy dog';
- $result = preg_replace('| +|', ' ', search_excerpt('nothing', $text));
- $this->assertEqual(preg_replace('| +|', ' ', $result), $expected, 'Entire string is returned when keyword is not found in short string');
-
- $result = preg_replace('| +|', ' ', search_excerpt('fox', $text));
- $this->assertEqual($result, 'The quick brown <strong>fox</strong> & jumps over the lazy dog ...', 'Found keyword is highlighted');
-
- $longtext = str_repeat($text . ' ', 10);
- $result = preg_replace('| +|', ' ', search_excerpt('nothing', $longtext));
- $this->assertTrue(strpos($result, $expected) === 0, 'When keyword is not found in long string, return value starts as expected');
-
- $entities = str_repeat('készítése ', 20);
- $result = preg_replace('| +|', ' ', search_excerpt('nothing', $entities));
- $this->assertFalse(strpos($result, '&'), 'Entities are not present in excerpt');
- $this->assertTrue(strpos($result, 'í') > 0, 'Entities are converted in excerpt');
-
-
-
- $text = "<div class=\"field field-name-body field-type-text-with-summary field-label-hidden\"><div class=\"field-items\"><div class=\"field-item even\" property=\"content:encoded\"><p>123456789 HTMLTest +123456789+‘ +‘ +‘ +‘ +12345678 +‘ +‘ +‘ ‘</p>\n</div></div></div> ";
- $result = search_excerpt('HTMLTest', $text);
- $this->assertFalse(empty($result), 'Rendered Multi-byte HTML encodings are not corrupted in search excerpts');
- }
-
-
- * Tests search_excerpt() with search keywords matching simplified words.
- *
- * Excerpting should handle keywords that are matched only after going through
- * search_simplify(). This test passes keywords that match simplified words
- * and compares them with strings that contain the original unsimplified word.
- */
- function testSearchExcerptSimplified() {
- $lorem1 = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vitae arcu at leo cursus laoreet. Curabitur dui tortor, adipiscing malesuada tempor in, bibendum ac diam. Cras non tellus a libero pellentesque condimentum. What is a foobar? Suspendisse ac lacus libero. Ut non est vel nisl faucibus interdum nec sed leo. Pellentesque sem risus, vulputate eu semper eget, auctor in libero.';
- $lorem2 = 'Ut fermentum est vitae metus convallis scelerisque. Phasellus pellentesque rhoncus tellus, eu dignissim purus posuere id. Quisque eu fringilla ligula. Morbi ullamcorper, lorem et mattis egestas, tortor neque pretium velit, eget eleifend odio turpis eu purus. Donec vitae metus quis leo pretium tincidunt a pulvinar sem. Morbi adipiscing laoreet mauris vel placerat. Nullam elementum, nisl sit amet scelerisque malesuada, dolor nunc hendrerit quam, eu ultrices erat est in orci.';
-
-
- $text = $lorem1 . ' Number: 123456.7890 Hyphenated: one-two abc,def ' . $lorem2;
-
-
- $result = preg_replace('| +|', ' ', search_excerpt('123456.7890', $text));
- $this->assertTrue(strpos($result, 'Number: <strong>123456.7890</strong>') !== FALSE, 'Numeric keyword is highlighted with exact match');
-
- $result = preg_replace('| +|', ' ', search_excerpt('1234567890', $text));
- $this->assertTrue(strpos($result, 'Number: <strong>123456.7890</strong>') !== FALSE, 'Numeric keyword is highlighted with simplified match');
-
- $result = preg_replace('| +|', ' ', search_excerpt('Number 1234567890', $text));
- $this->assertTrue(strpos($result, '<strong>Number</strong>: <strong>123456.7890</strong>') !== FALSE, 'Punctuated and numeric keyword is highlighted with simplified match');
-
- $result = preg_replace('| +|', ' ', search_excerpt('"Number 1234567890"', $text));
- $this->assertTrue(strpos($result, '<strong>Number: 123456.7890</strong>') !== FALSE, 'Phrase with punctuated and numeric keyword is highlighted with simplified match');
-
- $result = preg_replace('| +|', ' ', search_excerpt('"Hyphenated onetwo"', $text));
- $this->assertTrue(strpos($result, '<strong>Hyphenated: one-two</strong>') !== FALSE, 'Phrase with punctuated and hyphenated keyword is highlighted with simplified match');
-
- $result = preg_replace('| +|', ' ', search_excerpt('"abc def"', $text));
- $this->assertTrue(strpos($result, '<strong>abc,def</strong>') !== FALSE, 'Phrase with keyword simplified into two separate words is highlighted with simplified match');
-
-
- $result = preg_replace('| +|', ' ', search_excerpt('"ipsum _"', $text));
- $this->assertTrue(strpos($result, '<strong>ipsum </strong>') !== FALSE, 'Only valid part of the phrase is highlighted and invalid part containing "_" is ignored.');
-
- $result = preg_replace('| +|', ' ', search_excerpt('"ipsum 0000"', $text));
- $this->assertTrue(strpos($result, '<strong>ipsum </strong>') !== FALSE, 'Only valid part of the phrase is highlighted and invalid part "0000" is ignored.');
-
-
-
- $result = preg_replace('| +|', ' ', search_excerpt('ipsum _', $text));
- $this->assertTrue(strpos($result, '<strong>ipsum</strong>') !== FALSE, 'Only valid keyword is highlighted and invalid keyword "_" is ignored.');
-
- $result = preg_replace('| +|', ' ', search_excerpt('ipsum 0000', $text));
- $this->assertTrue(strpos($result, '<strong>ipsum</strong>') !== FALSE, 'Only valid keyword is highlighted and invalid keyword "0000" is ignored.');
- }
- }
-
- * Test the CJK tokenizer.
- */
- class SearchTokenizerTestCase extends BackdropWebTestCase {
- function setUp() {
- parent::setUp('search');
- }
-
-
- * 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() {
-
-
- config('search.settings')
- ->set('search_minimum_word_size', 1)
- ->set('search_overlap_cjk', TRUE)
- ->save();
- $this->refreshVariables();
-
-
-
-
-
- $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 = 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,
- );
-
-
- $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]);
- }
-
-
- $string = implode('', $chars);
- $out = trim(search_simplify($string));
- $expected = backdrop_strtolower(implode(' ', $chars));
-
-
- $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() {
-
-
- config('search.settings')
- ->set('search_minimum_word_size', 1)
- ->set('search_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 '';
- }
- }
-
- * Tests that we can embed a form in search results and submit it.
- */
- class SearchEmbedForm extends BackdropWebTestCase {
-
- * Node used for testing.
- */
- public $node;
-
-
- * Count of how many times the form has been submitted.
- */
- public $submit_count = 0;
-
- function setUp() {
- parent::setUp('search', 'search_embedded_form');
-
-
- $test_user = $this->backdropCreateUser(array('access content', 'search content', 'administer nodes'));
- $this->backdropLogin($test_user);
-
- $this->node = $this->backdropCreateNode();
-
- node_update_index();
- search_update_totals();
-
-
- $this->submit_count = config_get('search.settings', 'search_embedded_form_submitted');
- $this->refreshVariables();
- }
-
-
- * Tests that the embedded form appears and can be submitted.
- */
- function testEmbeddedForm() {
-
- $this->backdropPost('search_embedded_form',
- array('name' => 'John'),'Send away');
- $this->assertText('Test form was submitted', 'Form message appears');
- $count = config_get('search.settings', 'search_embedded_form_submitted');
- $this->assertEqual($this->submit_count + 1, $count, 'Form submission count is correct');
- $this->submit_count = $count;
-
-
- $this->backdropGet('search/node/' . $this->node->title);
- $this->assertText('Your name', 'Form is visible');
- $this->backdropPost('search/node/' . $this->node->title,
- array('name' => 'John'), 'Send away');
- $this->assertText('Test form was submitted', 'Form message appears');
- $count = config_get('search.settings', 'search_embedded_form_submitted');
- $this->assertEqual($this->submit_count + 1, $count, 'Form submission count is correct');
- $this->submit_count = $count;
-
-
-
- $this->backdropPost('search',
- array('keys' => 'foo'),
- t('Search'));
- $this->assertNoText(t('Test form was submitted'), 'Form message does not appear');
- $count = config_get('search.settings', 'search_embedded_form_submitted');
- $this->assertEqual($this->submit_count, $count, 'Form submission count is correct');
- $this->submit_count = $count;
- }
- }
-
- * Tests that hook_search_page runs.
- */
- class SearchPageOverride extends BackdropWebTestCase {
- public $search_user;
-
- function setUp() {
- parent::setUp('search', 'search_extra_type');
-
-
- $this->search_user = $this->backdropCreateUser(array('search content', 'administer search'));
- $this->backdropLogin($this->search_user);
-
-
- config_set('search.settings', 'search_active_modules', array('node', 'user', 'search_extra_type'));
- menu_rebuild();
- }
-
- function testSearchPageHook() {
- $keys = 'bike shed ' . $this->randomName();
- $this->backdropGet("search/dummy_path/{$keys}");
- $this->assertText('Dummy search snippet', 'Dummy search snippet is shown');
- $this->assertText('Test page text is here', 'Page override is working');
- }
- }
-
- * Test node search with multiple languages.
- */
- class SearchLanguageTestCase extends BackdropWebTestCase {
-
- * Implementation setUp().
- */
- function setUp() {
- parent::setUp('search', 'locale');
-
-
- $test_user = $this->backdropCreateUser(array('access content', 'search content', 'use advanced search', 'administer nodes', 'administer languages', 'access administration pages'));
- $this->backdropLogin($test_user);
- }
-
- function testLanguages() {
-
- $this->backdropGet('search/node');
- $this->assertNoText(t('Languages'), 'No languages to choose from.');
-
-
- $edit = array('predefined_langcode' => 'fr');
- $this->backdropPost('admin/config/regional/language/add', $edit, t('Add language'));
- $this->assertText('French', 'Language added successfully.');
-
-
- $this->backdropGet('search/node');
- $this->assertText(t('Languages'), 'Languages displayed to choose from.');
- $this->assertText(t('English'), 'English is a possible choice.');
- $this->assertText(t('French'), 'French is a possible choice.');
-
-
- $this->backdropPost('search/node', array(), t('Advanced search'));
- $this->assertEqual($this->getUrl(), url('search/node/', array('absolute' => TRUE)), 'Correct page redirection, no language filtering.');
-
-
- $edit = array('language[fr]' => TRUE);
- $this->backdropPost('search/node', $edit, t('Advanced search'));
- $this->assertFieldByXPath('//input[@name="keys"]', 'langcode:fr', 'Language filter added to query.');
-
-
- $path = 'admin/config/regional/language';
- $this->backdropGet($path);
- $this->assertFieldChecked('edit-site-default-en', 'English is the default language.');
- $edit = array('site_default' => 'fr');
- $this->backdropPost(NULL, $edit, t('Save configuration'));
- $this->assertNoFieldChecked('edit-site-default-en', 'Default language updated.');
- $edit = array('languages[en][enabled]' => FALSE);
- $this->backdropPost('admin/config/regional/language', $edit, t('Save configuration'));
- $this->assertNoFieldChecked('edit-languages-en-enabled', 'Language disabled.');
-
-
- $this->backdropGet('search/node');
- $this->assertNoText(t('Languages'), 'No languages to choose from.');
- }
- }
-
- * Tests node search with node access control.
- */
- class SearchNodeAccessTest extends BackdropWebTestCase {
- public $test_user;
-
- function setUp() {
- parent::setUp('search', 'node_access_test');
- node_access_rebuild();
-
-
- $this->test_user = $this->backdropCreateUser(array('access content', 'search content', 'use advanced search'));
- $this->backdropLogin($this->test_user);
- }
-
-
- * Tests that search works with punctuation and HTML entities.
- */
- function testPhraseSearchPunctuation() {
- $node = $this->backdropCreateNode(array('body' => array(LANGUAGE_NONE => array(array('value' => "The bunny's ears were fuzzy.")))));
- $node2 = $this->backdropCreateNode(array('body' => array(LANGUAGE_NONE => array(array('value' => 'Dignissim Aliquam & Quieligo meus natu quae quia te. Damnum© erat— neo pneum. Facilisi feugiat ibidem ratis.')))));
-
-
- module_invoke_all('update_index');
- search_update_totals();
-
-
- $this->refreshVariables();
-
-
- $edit = array('keys' => '"bunny\'s"');
- $this->backdropPost('search/node', $edit, t('Search'));
- $this->assertText($node->title);
-
-
- $edit = array('keys' => '&');
- $this->backdropPost('search/node', $edit, t('Search'));
- $this->assertNoRaw('<strong>&</strong>amp;');
- $this->assertText('You must include at least one keyword to match in the content. Keywords must be at least 3 characters, and punctuation is ignored.');
-
- $edit = array('keys' => '&');
- $this->backdropPost('search/node', $edit, t('Search'));
- $this->assertNoRaw('<strong>&</strong>amp;');
- $this->assertText('You must include at least one keyword to match in the content. Keywords must be at least 3 characters, and punctuation is ignored.');
- }
- }
-
- * Tests searching with locale values set.
- */
- class SearchSetLocaleTest extends BackdropWebTestCase {
- function setUp() {
- parent::setUp('search');
-
-
- $info = array(
- 'body' => array(LANGUAGE_NONE => array(array('value' => 'Tapir'))),
- );
- $this->backdropCreateNode($info);
-
-
- $this->cronRun();
- }
-
-
- * Verify that search works with a numeric locale set.
- */
- public function testSearchWithNumericLocale() {
-
- setlocale(LC_NUMERIC, 'fr_FR');
-
-
-
- db_select('search_index', 'i')
- ->extend('SearchQuery')
- ->searchexpression('tapir', 'node')
- ->execute();
- }
- }