Projet

Général

Profil

Paste
Télécharger (80,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / search / search.test @ 30d5b9c5

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for search.module.
6
 */
7

    
8
// The search index can contain different types of content. Typically the type is 'node'.
9
// Here we test with _test_ and _test2_ as the type.
10
define('SEARCH_TYPE', '_test_');
11
define('SEARCH_TYPE_2', '_test2_');
12
define('SEARCH_TYPE_JPN', '_test3_');
13

    
14
/**
15
 * Indexes content and queries it.
16
 */
17
class SearchMatchTestCase extends DrupalWebTestCase {
18
  public static function getInfo() {
19
    return array(
20
      'name' => 'Search engine queries',
21
      'description' => 'Indexes content and queries it.',
22
      'group' => 'Search',
23
    );
24
  }
25

    
26
  /**
27
   * Implementation setUp().
28
   */
29
  function setUp() {
30
    parent::setUp('search');
31
  }
32

    
33
  /**
34
   * Test search indexing.
35
   */
36
  function testMatching() {
37
    $this->_setup();
38
    $this->_testQueries();
39
  }
40

    
41
  /**
42
   * Set up a small index of items to test against.
43
   */
44
  function _setup() {
45
    variable_set('minimum_word_size', 3);
46

    
47
    for ($i = 1; $i <= 7; ++$i) {
48
      search_index($i, SEARCH_TYPE, $this->getText($i));
49
    }
50
    for ($i = 1; $i <= 5; ++$i) {
51
      search_index($i + 7, SEARCH_TYPE_2, $this->getText2($i));
52
    }
53
    // No getText builder function for Japanese text; just a simple array.
54
    foreach (array(
55
      13 => '以呂波耳・ほへとち。リヌルヲ。',
56
      14 => 'ドルーパルが大好きよ!',
57
      15 => 'コーヒーとケーキ',
58
    ) as $i => $jpn) {
59
      search_index($i, SEARCH_TYPE_JPN, $jpn);
60
    }
61
    search_update_totals();
62
  }
63

    
64
  /**
65
   * _test_: Helper method for generating snippets of content.
66
   *
67
   * Generated items to test against:
68
   *   1  ipsum
69
   *   2  dolore sit
70
   *   3  sit am ut
71
   *   4  am ut enim am
72
   *   5  ut enim am minim veniam
73
   *   6  enim am minim veniam es cillum
74
   *   7  am minim veniam es cillum dolore eu
75
   */
76
  function getText($n) {
77
    $words = explode(' ', "Ipsum dolore sit am. Ut enim am minim veniam. Es cillum dolore eu.");
78
    return implode(' ', array_slice($words, $n - 1, $n));
79
  }
80

    
81
  /**
82
   * _test2_: Helper method for generating snippets of content.
83
   *
84
   * Generated items to test against:
85
   *   8  dear
86
   *   9  king philip
87
   *   10 philip came over
88
   *   11 came over from germany
89
   *   12 over from germany swimming
90
   */
91
  function getText2($n) {
92
    $words = explode(' ', "Dear King Philip came over from Germany swimming.");
93
    return implode(' ', array_slice($words, $n - 1, $n));
94
  }
95

    
96
  /**
97
   * Run predefine queries looking for indexed terms.
98
   */
99
  function _testQueries() {
100
    /*
101
      Note: OR queries that include short words in OR groups are only accepted
102
      if the ORed terms are ANDed with at least one long word in the rest of the query.
103

    
104
      e.g. enim dolore OR ut = enim (dolore OR ut) = (enim dolor) OR (enim ut) -> good
105
      e.g. dolore OR ut = (dolore) OR (ut) -> bad
106

    
107
      This is a design limitation to avoid full table scans.
108
    */
109
    $queries = array(
110
      // Simple AND queries.
111
      'ipsum' => array(1),
112
      'enim' => array(4, 5, 6),
113
      'xxxxx' => array(),
114
      'enim minim' => array(5, 6),
115
      'enim xxxxx' => array(),
116
      'dolore eu' => array(7),
117
      'dolore xx' => array(),
118
      'ut minim' => array(5),
119
      'xx minim' => array(),
120
      'enim veniam am minim ut' => array(5),
121
      // Simple OR queries.
122
      'dolore OR ipsum' => array(1, 2, 7),
123
      'dolore OR xxxxx' => array(2, 7),
124
      'dolore OR ipsum OR enim' => array(1, 2, 4, 5, 6, 7),
125
      'ipsum OR dolore sit OR cillum' => array(2, 7),
126
      'minim dolore OR ipsum' => array(7),
127
      'dolore OR ipsum veniam' => array(7),
128
      'minim dolore OR ipsum OR enim' => array(5, 6, 7),
129
      'dolore xx OR yy' => array(),
130
      'xxxxx dolore OR ipsum' => array(),
131
      // Negative queries.
132
      'dolore -sit' => array(7),
133
      'dolore -eu' => array(2),
134
      'dolore -xxxxx' => array(2, 7),
135
      'dolore -xx' => array(2, 7),
136
      // Phrase queries.
137
      '"dolore sit"' => array(2),
138
      '"sit dolore"' => array(),
139
      '"am minim veniam es"' => array(6, 7),
140
      '"minim am veniam es"' => array(),
141
      // Mixed queries.
142
      '"am minim veniam es" OR dolore' => array(2, 6, 7),
143
      '"minim am veniam es" OR "dolore sit"' => array(2),
144
      '"minim am veniam es" OR "sit dolore"' => array(),
145
      '"am minim veniam es" -eu' => array(6),
146
      '"am minim veniam" -"cillum dolore"' => array(5, 6),
147
      '"am minim veniam" -"dolore cillum"' => array(5, 6, 7),
148
      'xxxxx "minim am veniam es" OR dolore' => array(),
149
      'xx "minim am veniam es" OR dolore' => array()
150
    );
151
    foreach ($queries as $query => $results) {
152
      $result = db_select('search_index', 'i')
153
        ->extend('SearchQuery')
154
        ->searchExpression($query, SEARCH_TYPE)
155
        ->execute();
156

    
157
      $set = $result ? $result->fetchAll() : array();
158
      $this->_testQueryMatching($query, $set, $results);
159
      $this->_testQueryScores($query, $set, $results);
160
    }
161

    
162
    // These queries are run against the second index type, SEARCH_TYPE_2.
163
    $queries = array(
164
      // Simple AND queries.
165
      'ipsum' => array(),
166
      'enim' => array(),
167
      'enim minim' => array(),
168
      'dear' => array(8),
169
      'germany' => array(11, 12),
170
    );
171
    foreach ($queries as $query => $results) {
172
      $result = db_select('search_index', 'i')
173
        ->extend('SearchQuery')
174
        ->searchExpression($query, SEARCH_TYPE_2)
175
        ->execute();
176

    
177
      $set = $result ? $result->fetchAll() : array();
178
      $this->_testQueryMatching($query, $set, $results);
179
      $this->_testQueryScores($query, $set, $results);
180
    }
181

    
182
    // These queries are run against the third index type, SEARCH_TYPE_JPN.
183
    $queries = array(
184
      // Simple AND queries.
185
      '呂波耳' => array(13),
186
      '以呂波耳' => array(13),
187
      'ほへと ヌルヲ' => array(13),
188
      'とちリ' => array(),
189
      'ドルーパル' => array(14),
190
      'パルが大' => array(14),
191
      'コーヒー' => array(15),
192
      'ヒーキ' => array(),
193
    );
194
    foreach ($queries as $query => $results) {
195
      $result = db_select('search_index', 'i')
196
        ->extend('SearchQuery')
197
        ->searchExpression($query, SEARCH_TYPE_JPN)
198
        ->execute();
199

    
200
      $set = $result ? $result->fetchAll() : array();
201
      $this->_testQueryMatching($query, $set, $results);
202
      $this->_testQueryScores($query, $set, $results);
203
    }
204
  }
205

    
206
  /**
207
   * Test the matching abilities of the engine.
208
   *
209
   * Verify if a query produces the correct results.
210
   */
211
  function _testQueryMatching($query, $set, $results) {
212
    // Get result IDs.
213
    $found = array();
214
    foreach ($set as $item) {
215
      $found[] = $item->sid;
216
    }
217

    
218
    // Compare $results and $found.
219
    sort($found);
220
    sort($results);
221
    $this->assertEqual($found, $results, "Query matching '$query'");
222
  }
223

    
224
  /**
225
   * Test the scoring abilities of the engine.
226
   *
227
   * Verify if a query produces normalized, monotonous scores.
228
   */
229
  function _testQueryScores($query, $set, $results) {
230
    // Get result scores.
231
    $scores = array();
232
    foreach ($set as $item) {
233
      $scores[] = $item->calculated_score;
234
    }
235

    
236
    // Check order.
237
    $sorted = $scores;
238
    sort($sorted);
239
    $this->assertEqual($scores, array_reverse($sorted), "Query order '$query'");
240

    
241
    // Check range.
242
    $this->assertEqual(!count($scores) || (min($scores) > 0.0 && max($scores) <= 1.0001), TRUE, "Query scoring '$query'");
243
  }
244
}
245

    
246
/**
247
 * Tests the bike shed text on no results page, and text on the search page.
248
 */
249
class SearchPageText extends DrupalWebTestCase {
250
  protected $searching_user;
251

    
252
  public static function getInfo() {
253
    return array(
254
      'name' => 'Search page text',
255
      'description' => 'Tests the bike shed text on the no results page, and various other text on search pages.',
256
      'group' => 'Search'
257
    );
258
  }
259

    
260
  function setUp() {
261
    parent::setUp('search');
262

    
263
    // Create user.
264
    $this->searching_user = $this->drupalCreateUser(array('search content', 'access user profiles'));
265
  }
266

    
267
  /**
268
   * Tests the failed search text, and various other text on the search page.
269
   */
270
  function testSearchText() {
271
    $this->drupalLogin($this->searching_user);
272
    $this->drupalGet('search/node');
273
    $this->assertText(t('Enter your keywords'));
274
    $this->assertText(t('Search'));
275
    $title = t('Search') . ' | Drupal';
276
    $this->assertTitle($title, 'Search page title is correct');
277

    
278
    $edit = array();
279
    $edit['keys'] = 'bike shed ' . $this->randomName();
280
    $this->drupalPost('search/node', $edit, t('Search'));
281
    $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.');
282
    $this->assertText(t('Search'));
283
    $this->assertTitle($title, 'Search page title is correct');
284

    
285
    $edit['keys'] = $this->searching_user->name;
286
    $this->drupalPost('search/user', $edit, t('Search'));
287
    $this->assertText(t('Search'));
288
    $this->assertTitle($title, 'Search page title is correct');
289

    
290
    // Test that search keywords containing slashes are correctly loaded
291
    // from the path and displayed in the search form.
292
    $arg = $this->randomName() . '/' . $this->randomName();
293
    $this->drupalGet('search/node/' . $arg);
294
    $input = $this->xpath("//input[@id='edit-keys' and @value='{$arg}']");
295
    $this->assertFalse(empty($input), 'Search keys with a / are correctly set as the default value in the search box.');
296

    
297
    // Test a search input exceeding the limit of AND/OR combinations to test
298
    // the Denial-of-Service protection.
299
    $limit = variable_get('search_and_or_limit', 7);
300
    $keys = array();
301
    for ($i = 0; $i < $limit + 1; $i++) {
302
      $keys[] = $this->randomName(3);
303
      if ($i % 2 == 0) {
304
        $keys[] = 'OR';
305
      }
306
    }
307
    $edit['keys'] = implode(' ', $keys);
308
    $this->drupalPost('search/node', $edit, t('Search'));
309
    $this->assertRaw(t('Your search used too many AND/OR expressions. Only the first @count terms were included in this search.', array('@count' => $limit)));
310
  }
311
}
312

    
313
/**
314
 * Indexes content and tests the advanced search form.
315
 */
316
class SearchAdvancedSearchForm extends DrupalWebTestCase {
317
  protected $node;
318

    
319
  public static function getInfo() {
320
    return array(
321
      'name' => 'Advanced search form',
322
      'description' => 'Indexes content and tests the advanced search form.',
323
      'group' => 'Search',
324
    );
325
  }
326

    
327
  function setUp() {
328
    parent::setUp('search');
329
    // Create and login user.
330
    $test_user = $this->drupalCreateUser(array('access content', 'search content', 'use advanced search', 'administer nodes'));
331
    $this->drupalLogin($test_user);
332

    
333
    // Create initial node.
334
    $node = $this->drupalCreateNode();
335
    $this->node = $this->drupalCreateNode();
336

    
337
    // First update the index. This does the initial processing.
338
    node_update_index();
339

    
340
    // Then, run the shutdown function. Testing is a unique case where indexing
341
    // and searching has to happen in the same request, so running the shutdown
342
    // function manually is needed to finish the indexing process.
343
    search_update_totals();
344
  }
345

    
346
  /**
347
   * Test using the search form with GET and POST queries.
348
   * Test using the advanced search form to limit search to nodes of type "Basic page".
349
   */
350
  function testNodeType() {
351
    $this->assertTrue($this->node->type == 'page', 'Node type is Basic page.');
352

    
353
    // Assert that the dummy title doesn't equal the real title.
354
    $dummy_title = 'Lorem ipsum';
355
    $this->assertNotEqual($dummy_title, $this->node->title, "Dummy title doesn't equal node title");
356

    
357
    // Search for the dummy title with a GET query.
358
    $this->drupalGet('search/node/' . $dummy_title);
359
    $this->assertNoText($this->node->title, 'Basic page node is not found with dummy title.');
360

    
361
    // Search for the title of the node with a GET query.
362
    $this->drupalGet('search/node/' . $this->node->title);
363
    $this->assertText($this->node->title, 'Basic page node is found with GET query.');
364

    
365
    // Search for the title of the node with a POST query.
366
    $edit = array('or' => $this->node->title);
367
    $this->drupalPost('search/node', $edit, t('Advanced search'));
368
    $this->assertText($this->node->title, 'Basic page node is found with POST query.');
369

    
370
    // Advanced search type option.
371
    $this->drupalPost('search/node', array_merge($edit, array('type[page]' => 'page')), t('Advanced search'));
372
    $this->assertText($this->node->title, 'Basic page node is found with POST query and type:page.');
373

    
374
    $this->drupalPost('search/node', array_merge($edit, array('type[article]' => 'article')), t('Advanced search'));
375
    $this->assertText('bike shed', 'Article node is not found with POST query and type:article.');
376
  }
377
}
378

    
379
/**
380
 * Indexes content and tests ranking factors.
381
 */
382
class SearchRankingTestCase extends DrupalWebTestCase {
383
  public static function getInfo() {
384
    return array(
385
      'name' => 'Search engine ranking',
386
      'description' => 'Indexes content and tests ranking factors.',
387
      'group' => 'Search',
388
    );
389
  }
390

    
391
  /**
392
   * Implementation setUp().
393
   */
394
  function setUp() {
395
    parent::setUp('search', 'statistics', 'comment');
396
  }
397

    
398
  function testRankings() {
399
    // Login with sufficient privileges.
400
    $this->drupalLogin($this->drupalCreateUser(array('skip comment approval', 'create page content')));
401

    
402
    // Build a list of the rankings to test.
403
    $node_ranks = array('sticky', 'promote', 'relevance', 'recent', 'comments', 'views');
404

    
405
    // Create nodes for testing.
406
    foreach ($node_ranks as $node_rank) {
407
      $settings = array(
408
        'type' => 'page',
409
        'title' => 'Drupal rocks',
410
        'body' => array(LANGUAGE_NONE => array(array('value' => "Drupal's search rocks"))),
411
      );
412
      foreach (array(0, 1) as $num) {
413
        if ($num == 1) {
414
          switch ($node_rank) {
415
            case 'sticky':
416
            case 'promote':
417
              $settings[$node_rank] = 1;
418
              break;
419
            case 'relevance':
420
              $settings['body'][LANGUAGE_NONE][0]['value'] .= " really rocks";
421
              break;
422
            case 'recent':
423
              $settings['created'] = REQUEST_TIME + 3600;
424
              break;
425
            case 'comments':
426
              $settings['comment'] = 2;
427
              break;
428
          }
429
        }
430
        $nodes[$node_rank][$num] = $this->drupalCreateNode($settings);
431
      }
432
    }
433

    
434
    // Update the search index.
435
    module_invoke_all('update_index');
436
    search_update_totals();
437

    
438
    // Refresh variables after the treatment.
439
    $this->refreshVariables();
440

    
441
    // Add a comment to one of the nodes.
442
    $edit = array();
443
    $edit['subject'] = 'my comment title';
444
    $edit['comment_body[' . LANGUAGE_NONE . '][0][value]'] = 'some random comment';
445
    $this->drupalGet('comment/reply/' . $nodes['comments'][1]->nid);
446
    $this->drupalPost(NULL, $edit, t('Preview'));
447
    $this->drupalPost(NULL, $edit, t('Save'));
448

    
449
    // Enable counting of statistics.
450
    variable_set('statistics_count_content_views', 1);
451

    
452
    // Then View one of the nodes a bunch of times.
453
    for ($i = 0; $i < 5; $i ++) {
454
      $this->drupalGet('node/' . $nodes['views'][1]->nid);
455
    }
456

    
457
    // Test each of the possible rankings.
458
    foreach ($node_ranks as $node_rank) {
459
      // Disable all relevancy rankings except the one we are testing.
460
      foreach ($node_ranks as $var) {
461
        variable_set('node_rank_' . $var, $var == $node_rank ? 10 : 0);
462
      }
463

    
464
      // Do the search and assert the results.
465
      $set = node_search_execute('rocks');
466
      $this->assertEqual($set[0]['node']->nid, $nodes[$node_rank][1]->nid, 'Search ranking "' . $node_rank . '" order.');
467
    }
468
  }
469

    
470
  /**
471
   * Test rankings of HTML tags.
472
   */
473
  function testHTMLRankings() {
474
    // Login with sufficient privileges.
475
    $this->drupalLogin($this->drupalCreateUser(array('create page content')));
476

    
477
    // Test HTML tags with different weights.
478
    $sorted_tags = array('h1', 'h2', 'h3', 'h4', 'a', 'h5', 'h6', 'notag');
479
    $shuffled_tags = $sorted_tags;
480

    
481
    // Shuffle tags to ensure HTML tags are ranked properly.
482
    shuffle($shuffled_tags);
483
    $settings = array(
484
      'type' => 'page',
485
      'title' => 'Simple node',
486
    );
487
    foreach ($shuffled_tags as $tag) {
488
      switch ($tag) {
489
        case 'a':
490
          $settings['body'] = array(LANGUAGE_NONE => array(array('value' => l('Drupal Rocks', 'node'), 'format' => 'full_html')));
491
          break;
492
        case 'notag':
493
          $settings['body'] = array(LANGUAGE_NONE => array(array('value' => 'Drupal Rocks')));
494
          break;
495
        default:
496
          $settings['body'] = array(LANGUAGE_NONE => array(array('value' => "<$tag>Drupal Rocks</$tag>", 'format' => 'full_html')));
497
          break;
498
      }
499
      $nodes[$tag] = $this->drupalCreateNode($settings);
500
    }
501

    
502
    // Update the search index.
503
    module_invoke_all('update_index');
504
    search_update_totals();
505

    
506
    // Refresh variables after the treatment.
507
    $this->refreshVariables();
508

    
509
    // Disable all other rankings.
510
    $node_ranks = array('sticky', 'promote', 'recent', 'comments', 'views');
511
    foreach ($node_ranks as $node_rank) {
512
      variable_set('node_rank_' . $node_rank, 0);
513
    }
514
    $set = node_search_execute('rocks');
515

    
516
    // Test the ranking of each tag.
517
    foreach ($sorted_tags as $tag_rank => $tag) {
518
      // Assert the results.
519
      if ($tag == 'notag') {
520
        $this->assertEqual($set[$tag_rank]['node']->nid, $nodes[$tag]->nid, 'Search tag ranking for plain text order.');
521
      } else {
522
        $this->assertEqual($set[$tag_rank]['node']->nid, $nodes[$tag]->nid, 'Search tag ranking for "&lt;' . $sorted_tags[$tag_rank] . '&gt;" order.');
523
      }
524
    }
525

    
526
    // Test tags with the same weight against the sorted tags.
527
    $unsorted_tags = array('u', 'b', 'i', 'strong', 'em');
528
    foreach ($unsorted_tags as $tag) {
529
      $settings['body'] = array(LANGUAGE_NONE => array(array('value' => "<$tag>Drupal Rocks</$tag>", 'format' => 'full_html')));
530
      $node = $this->drupalCreateNode($settings);
531

    
532
      // Update the search index.
533
      module_invoke_all('update_index');
534
      search_update_totals();
535

    
536
      // Refresh variables after the treatment.
537
      $this->refreshVariables();
538

    
539
      $set = node_search_execute('rocks');
540

    
541
      // Ranking should always be second to last.
542
      $set = array_slice($set, -2, 1);
543

    
544
      // Assert the results.
545
      $this->assertEqual($set[0]['node']->nid, $node->nid, 'Search tag ranking for "&lt;' . $tag . '&gt;" order.');
546

    
547
      // Delete node so it doesn't show up in subsequent search results.
548
      node_delete($node->nid);
549
    }
550
  }
551

    
552
  /**
553
   * Verifies that if we combine two rankings, search still works.
554
   *
555
   * See issue http://drupal.org/node/771596
556
   */
557
  function testDoubleRankings() {
558
    // Login with sufficient privileges.
559
    $this->drupalLogin($this->drupalCreateUser(array('skip comment approval', 'create page content')));
560

    
561
    // See testRankings() above - build a node that will rank high for sticky.
562
    $settings = array(
563
      'type' => 'page',
564
      'title' => 'Drupal rocks',
565
      'body' => array(LANGUAGE_NONE => array(array('value' => "Drupal's search rocks"))),
566
      'sticky' => 1,
567
    );
568

    
569
    $node = $this->drupalCreateNode($settings);
570

    
571
    // Update the search index.
572
    module_invoke_all('update_index');
573
    search_update_totals();
574

    
575
    // Refresh variables after the treatment.
576
    $this->refreshVariables();
577

    
578
    // Set up for ranking sticky and lots of comments; make sure others are
579
    // disabled.
580
    $node_ranks = array('sticky', 'promote', 'relevance', 'recent', 'comments', 'views');
581
    foreach ($node_ranks as $var) {
582
      $value = ($var == 'sticky' || $var == 'comments') ? 10 : 0;
583
      variable_set('node_rank_' . $var, $value);
584
    }
585

    
586
    // Do the search and assert the results.
587
    $set = node_search_execute('rocks');
588
    $this->assertEqual($set[0]['node']->nid, $node->nid, 'Search double ranking order.');
589
  }
590
}
591

    
592
/**
593
 * Tests the rendering of the search block.
594
 */
595
class SearchBlockTestCase extends DrupalWebTestCase {
596
  public static function getInfo() {
597
    return array(
598
      'name' => 'Block availability',
599
      'description' => 'Check if the search form block is available.',
600
      'group' => 'Search',
601
    );
602
  }
603

    
604
  function setUp() {
605
    parent::setUp('search');
606

    
607
    // Create and login user
608
    $admin_user = $this->drupalCreateUser(array('administer blocks', 'search content'));
609
    $this->drupalLogin($admin_user);
610
  }
611

    
612
  function testSearchFormBlock() {
613
    // Set block title to confirm that the interface is available.
614
    $this->drupalPost('admin/structure/block/manage/search/form/configure', array('title' => $this->randomName(8)), t('Save block'));
615
    $this->assertText(t('The block configuration has been saved.'), 'Block configuration set.');
616

    
617
    // Set the block to a region to confirm block is available.
618
    $edit = array();
619
    $edit['blocks[search_form][region]'] = 'footer';
620
    $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
621
    $this->assertText(t('The block settings have been updated.'), 'Block successfully move to footer region.');
622
  }
623

    
624
  /**
625
   * Test that the search block form works correctly.
626
   */
627
  function testBlock() {
628
    // Enable the block, and place it in the 'content' region so that it isn't
629
    // hidden on 404 pages.
630
    $edit = array('blocks[search_form][region]' => 'content');
631
    $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
632

    
633
    // Test a normal search via the block form, from the front page.
634
    $terms = array('search_block_form' => 'test');
635
    $this->drupalPost('node', $terms, t('Search'));
636
    $this->assertText('Your search yielded no results');
637

    
638
    // Test a search from the block on a 404 page.
639
    $this->drupalGet('foo');
640
    $this->assertResponse(404);
641
    $this->drupalPost(NULL, $terms, t('Search'));
642
    $this->assertResponse(200);
643
    $this->assertText('Your search yielded no results');
644

    
645
    // Test a search from the block when it doesn't appear on the search page.
646
    $edit = array('pages' => 'search');
647
    $this->drupalPost('admin/structure/block/manage/search/form/configure', $edit, t('Save block'));
648
    $this->drupalPost('node', $terms, t('Search'));
649
    $this->assertText('Your search yielded no results');
650

    
651
    // Confirm that the user is redirected to the search page.
652
    $this->assertEqual(
653
      $this->getUrl(),
654
      url('search/node/' . $terms['search_block_form'], array('absolute' => TRUE)),
655
      'Redirected to correct url.'
656
    );
657

    
658
    // Test an empty search via the block form, from the front page.
659
    $terms = array('search_block_form' => '');
660
    $this->drupalPost('node', $terms, t('Search'));
661
    $this->assertText('Please enter some keywords');
662

    
663
    // Confirm that the user is redirected to the search page, when form is submitted empty.
664
    $this->assertEqual(
665
      $this->getUrl(),
666
      url('search/node/', array('absolute' => TRUE)),
667
      'Redirected to correct url.'
668
    );
669

    
670
    // Test that after entering a too-short keyword in the form, you can then
671
    // search again with a longer keyword. First test using the block form.
672
    $terms = array('search_block_form' => 'a');
673
    $this->drupalPost('node', $terms, t('Search'));
674
    $this->assertText('You must include at least one positive keyword with 3 characters or more');
675
    $terms = array('search_block_form' => 'foo');
676
    $this->drupalPost(NULL, $terms, t('Search'));
677
    $this->assertNoText('You must include at least one positive keyword with 3 characters or more');
678
    $this->assertText('Your search yielded no results');
679

    
680
    // Same test again, using the search page form for the second search this time.
681
    $terms = array('search_block_form' => 'a');
682
    $this->drupalPost('node', $terms, t('Search'));
683
    $terms = array('keys' => 'foo');
684
    $this->drupalPost(NULL, $terms, t('Search'));
685
    $this->assertNoText('You must include at least one positive keyword with 3 characters or more');
686
    $this->assertText('Your search yielded no results');
687
  }
688
}
689

    
690
/**
691
 * Tests that searching for a phrase gets the correct page count.
692
 */
693
class SearchExactTestCase extends DrupalWebTestCase {
694
  public static function getInfo() {
695
    return array(
696
      'name' => 'Search engine phrase queries',
697
      'description' => 'Tests that searching for a phrase gets the correct page count.',
698
      'group' => 'Search',
699
    );
700
  }
701

    
702
  function setUp() {
703
    parent::setUp('search');
704
  }
705

    
706
  /**
707
   * Tests that the correct number of pager links are found for both keywords and phrases.
708
   */
709
  function testExactQuery() {
710
    // Login with sufficient privileges.
711
    $this->drupalLogin($this->drupalCreateUser(array('create page content', 'search content')));
712

    
713
    $settings = array(
714
      'type' => 'page',
715
      'title' => 'Simple Node',
716
    );
717
    // Create nodes with exact phrase.
718
    for ($i = 0; $i <= 17; $i++) {
719
      $settings['body'] = array(LANGUAGE_NONE => array(array('value' => 'love pizza')));
720
      $this->drupalCreateNode($settings);
721
    }
722
    // Create nodes containing keywords.
723
    for ($i = 0; $i <= 17; $i++) {
724
      $settings['body'] = array(LANGUAGE_NONE => array(array('value' => 'love cheesy pizza')));
725
      $this->drupalCreateNode($settings);
726
    }
727

    
728
    // Update the search index.
729
    module_invoke_all('update_index');
730
    search_update_totals();
731

    
732
    // Refresh variables after the treatment.
733
    $this->refreshVariables();
734

    
735
    // Test that the correct number of pager links are found for keyword search.
736
    $edit = array('keys' => 'love pizza');
737
    $this->drupalPost('search/node', $edit, t('Search'));
738
    $this->assertLinkByHref('page=1', 0, '2nd page link is found for keyword search.');
739
    $this->assertLinkByHref('page=2', 0, '3rd page link is found for keyword search.');
740
    $this->assertLinkByHref('page=3', 0, '4th page link is found for keyword search.');
741
    $this->assertNoLinkByHref('page=4', '5th page link is not found for keyword search.');
742

    
743
    // Test that the correct number of pager links are found for exact phrase search.
744
    $edit = array('keys' => '"love pizza"');
745
    $this->drupalPost('search/node', $edit, t('Search'));
746
    $this->assertLinkByHref('page=1', 0, '2nd page link is found for exact phrase search.');
747
    $this->assertNoLinkByHref('page=2', '3rd page link is not found for exact phrase search.');
748
  }
749
}
750

    
751
/**
752
 * Test integration searching comments.
753
 */
754
class SearchCommentTestCase extends DrupalWebTestCase {
755
  protected $admin_user;
756

    
757
  public static function getInfo() {
758
    return array(
759
      'name' => 'Comment Search tests',
760
      'description' => 'Test integration searching comments.',
761
      'group' => 'Search',
762
    );
763
  }
764

    
765
  function setUp() {
766
    parent::setUp('comment', 'search');
767

    
768
    // Create and log in an administrative user having access to the Full HTML
769
    // text format.
770
    $full_html_format = filter_format_load('full_html');
771
    $permissions = array(
772
      'administer filters',
773
      filter_permission_name($full_html_format),
774
      'administer permissions',
775
      'create page content',
776
      'skip comment approval',
777
      'access comments',
778
    );
779
    $this->admin_user = $this->drupalCreateUser($permissions);
780
    $this->drupalLogin($this->admin_user);
781
  }
782

    
783
  /**
784
   * Verify that comments are rendered using proper format in search results.
785
   */
786
  function testSearchResultsComment() {
787
    $comment_body = 'Test comment body';
788

    
789
    variable_set('comment_preview_article', DRUPAL_OPTIONAL);
790
    // Enable check_plain() for 'Filtered HTML' text format.
791
    $filtered_html_format_id = 'filtered_html';
792
    $edit = array(
793
      'filters[filter_html_escape][status]' => TRUE,
794
    );
795
    $this->drupalPost('admin/config/content/formats/' . $filtered_html_format_id, $edit, t('Save configuration'));
796
    // Allow anonymous users to search content.
797
    $edit = array(
798
      DRUPAL_ANONYMOUS_RID . '[search content]' => 1,
799
      DRUPAL_ANONYMOUS_RID . '[access comments]' => 1,
800
      DRUPAL_ANONYMOUS_RID . '[post comments]' => 1,
801
    );
802
    $this->drupalPost('admin/people/permissions', $edit, t('Save permissions'));
803

    
804
    // Create a node.
805
    $node = $this->drupalCreateNode(array('type' => 'article'));
806
    // Post a comment using 'Full HTML' text format.
807
    $edit_comment = array();
808
    $edit_comment['subject'] = 'Test comment subject';
809
    $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][value]'] = '<h1>' . $comment_body . '</h1>';
810
    $full_html_format_id = 'full_html';
811
    $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][format]'] = $full_html_format_id;
812
    $this->drupalPost('comment/reply/' . $node->nid, $edit_comment, t('Save'));
813

    
814
    // Invoke search index update.
815
    $this->drupalLogout();
816
    $this->cronRun();
817

    
818
    // Search for the comment subject.
819
    $edit = array(
820
      'search_block_form' => "'" . $edit_comment['subject'] . "'",
821
    );
822
    $this->drupalPost('', $edit, t('Search'));
823
    $this->assertText($node->title, 'Node found in search results.');
824
    $this->assertText($edit_comment['subject'], 'Comment subject found in search results.');
825

    
826
    // Search for the comment body.
827
    $edit = array(
828
      'search_block_form' => "'" . $comment_body . "'",
829
    );
830
    $this->drupalPost('', $edit, t('Search'));
831
    $this->assertText($node->title, 'Node found in search results.');
832

    
833
    // Verify that comment is rendered using proper format.
834
    $this->assertText($comment_body, 'Comment body text found in search results.');
835
    $this->assertNoRaw(t('n/a'), 'HTML in comment body is not hidden.');
836
    $this->assertNoRaw(check_plain($edit_comment['comment_body[' . LANGUAGE_NONE . '][0][value]']), 'HTML in comment body is not escaped.');
837

    
838
    // Hide comments.
839
    $this->drupalLogin($this->admin_user);
840
    $node->comment = 0;
841
    node_save($node);
842

    
843
    // Invoke search index update.
844
    $this->drupalLogout();
845
    $this->cronRun();
846

    
847
    // Search for $title.
848
    $this->drupalPost('', $edit, t('Search'));
849
    $this->assertNoText($comment_body, 'Comment body text not found in search results.');
850
  }
851

    
852
  /**
853
   * Verify access rules for comment indexing with different permissions.
854
   */
855
  function testSearchResultsCommentAccess() {
856
    $comment_body = 'Test comment body';
857
    $this->comment_subject = 'Test comment subject';
858
    $this->admin_role = $this->admin_user->roles;
859
    unset($this->admin_role[DRUPAL_AUTHENTICATED_RID]);
860
    $this->admin_role = key($this->admin_role);
861

    
862
    // Create a node.
863
    variable_set('comment_preview_article', DRUPAL_OPTIONAL);
864
    $this->node = $this->drupalCreateNode(array('type' => 'article'));
865

    
866
    // Post a comment using 'Full HTML' text format.
867
    $edit_comment = array();
868
    $edit_comment['subject'] = $this->comment_subject;
869
    $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][value]'] = '<h1>' . $comment_body . '</h1>';
870
    $this->drupalPost('comment/reply/' . $this->node->nid, $edit_comment, t('Save'));
871

    
872
    $this->drupalLogout();
873
    $this->setRolePermissions(DRUPAL_ANONYMOUS_RID);
874
    $this->checkCommentAccess('Anon user has search permission but no access comments permission, comments should not be indexed');
875

    
876
    $this->setRolePermissions(DRUPAL_ANONYMOUS_RID, TRUE);
877
    $this->checkCommentAccess('Anon user has search permission and access comments permission, comments should be indexed', TRUE);
878

    
879
    $this->drupalLogin($this->admin_user);
880
    $this->drupalGet('admin/people/permissions');
881

    
882
    // Disable search access for authenticated user to test admin user.
883
    $this->setRolePermissions(DRUPAL_AUTHENTICATED_RID, FALSE, FALSE);
884

    
885
    $this->setRolePermissions($this->admin_role);
886
    $this->checkCommentAccess('Admin user has search permission but no access comments permission, comments should not be indexed');
887

    
888
    $this->setRolePermissions($this->admin_role, TRUE);
889
    $this->checkCommentAccess('Admin user has search permission and access comments permission, comments should be indexed', TRUE);
890

    
891
    $this->setRolePermissions(DRUPAL_AUTHENTICATED_RID);
892
    $this->checkCommentAccess('Authenticated user has search permission but no access comments permission, comments should not be indexed');
893

    
894
    $this->setRolePermissions(DRUPAL_AUTHENTICATED_RID, TRUE);
895
    $this->checkCommentAccess('Authenticated user has search permission and access comments permission, comments should be indexed', TRUE);
896

    
897
    // Verify that access comments permission is inherited from the
898
    // authenticated role.
899
    $this->setRolePermissions(DRUPAL_AUTHENTICATED_RID, TRUE, FALSE);
900
    $this->setRolePermissions($this->admin_role);
901
    $this->checkCommentAccess('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);
902

    
903
    // Verify that search content permission is inherited from the authenticated
904
    // role.
905
    $this->setRolePermissions(DRUPAL_AUTHENTICATED_RID, TRUE, TRUE);
906
    $this->setRolePermissions($this->admin_role, TRUE, FALSE);
907
    $this->checkCommentAccess('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);
908

    
909
  }
910

    
911
  /**
912
   * Set permissions for role.
913
   */
914
  function setRolePermissions($rid, $access_comments = FALSE, $search_content = TRUE) {
915
    $permissions = array(
916
      'access comments' => $access_comments,
917
      'search content' => $search_content,
918
    );
919
    user_role_change_permissions($rid, $permissions);
920
  }
921

    
922
  /**
923
   * Update search index and search for comment.
924
   */
925
  function checkCommentAccess($message, $assume_access = FALSE) {
926
    // Invoke search index update.
927
    search_touch_node($this->node->nid);
928
    $this->cronRun();
929

    
930
    // Search for the comment subject.
931
    $edit = array(
932
      'search_block_form' => "'" . $this->comment_subject . "'",
933
    );
934
    $this->drupalPost('', $edit, t('Search'));
935
    $method = $assume_access ? 'assertText' : 'assertNoText';
936
    $verb = $assume_access ? 'found' : 'not found';
937
    $this->{$method}($this->node->title, "Node $verb in search results: " . $message);
938
    $this->{$method}($this->comment_subject, "Comment subject $verb in search results: " . $message);
939
  }
940

    
941
  /**
942
   * Verify that 'add new comment' does not appear in search results or index.
943
   */
944
  function testAddNewComment() {
945
    // Create a node with a short body.
946
    $settings = array(
947
      'type' => 'article',
948
      'title' => 'short title',
949
      'body' => array(LANGUAGE_NONE => array(array('value' => 'short body text'))),
950
    );
951

    
952
    $user = $this->drupalCreateUser(array('search content', 'create article content', 'access content'));
953
    $this->drupalLogin($user);
954

    
955
    $node = $this->drupalCreateNode($settings);
956
    // Verify that if you view the node on its own page, 'add new comment'
957
    // is there.
958
    $this->drupalGet('node/' . $node->nid);
959
    $this->assertText(t('Add new comment'), 'Add new comment appears on node page');
960

    
961
    // Run cron to index this page.
962
    $this->drupalLogout();
963
    $this->cronRun();
964

    
965
    // Search for 'comment'. Should be no results.
966
    $this->drupalLogin($user);
967
    $this->drupalPost('search/node', array('keys' => 'comment'), t('Search'));
968
    $this->assertText(t('Your search yielded no results'), 'No results searching for the word comment');
969

    
970
    // Search for the node title. Should be found, and 'Add new comment' should
971
    // not be part of the search snippet.
972
    $this->drupalPost('search/node', array('keys' => 'short'), t('Search'));
973
    $this->assertText($node->title, 'Search for keyword worked');
974
    $this->assertNoText(t('Add new comment'), 'Add new comment does not appear on search results page');
975
  }
976

    
977
}
978

    
979
/**
980
 * Tests search_expression_insert() and search_expression_extract().
981
 *
982
 * @see http://drupal.org/node/419388 (issue)
983
 */
984
class SearchExpressionInsertExtractTestCase extends DrupalUnitTestCase {
985
  public static function getInfo() {
986
    return array(
987
      'name' => 'Search expression insert/extract',
988
      'description' => 'Tests the functions search_expression_insert() and search_expression_extract()',
989
      'group' => 'Search',
990
    );
991
  }
992

    
993
  function setUp() {
994
    drupal_load('module', 'search');
995
    parent::setUp();
996
  }
997

    
998
  /**
999
   * Tests search_expression_insert() and search_expression_extract().
1000
   */
1001
  function testInsertExtract() {
1002
    $base_expression = "mykeyword";
1003
    // Build an array of option, value, what should be in the expression, what
1004
    // should be retrieved from expression.
1005
    $cases = array(
1006
      array('foo', 'bar', 'foo:bar', 'bar'), // Normal case.
1007
      array('foo', NULL, '', NULL), // Empty value: shouldn't insert.
1008
      array('foo', ' ', 'foo:', ''), // Space as value: should insert but retrieve empty string.
1009
      array('foo', '', 'foo:', ''), // Empty string as value: should insert but retrieve empty string.
1010
      array('foo', '0', 'foo:0', '0'), // String zero as value: should insert.
1011
      array('foo', 0, 'foo:0', '0'), // Numeric zero as value: should insert.
1012
    );
1013

    
1014
    foreach ($cases as $index => $case) {
1015
      $after_insert = search_expression_insert($base_expression, $case[0], $case[1]);
1016
      if (empty($case[2])) {
1017
        $this->assertEqual($after_insert, $base_expression, "Empty insert does not change expression in case $index");
1018
      }
1019
      else {
1020
        $this->assertEqual($after_insert, $base_expression . ' ' . $case[2], "Insert added correct expression for case $index");
1021
      }
1022

    
1023
      $retrieved = search_expression_extract($after_insert, $case[0]);
1024
      if (!isset($case[3])) {
1025
        $this->assertFalse(isset($retrieved), "Empty retrieval results in unset value in case $index");
1026
      }
1027
      else {
1028
        $this->assertEqual($retrieved, $case[3], "Value is retrieved for case $index");
1029
      }
1030

    
1031
      $after_clear = search_expression_insert($after_insert, $case[0]);
1032
      $this->assertEqual(trim($after_clear), $base_expression, "After clearing, base expression is restored for case $index");
1033

    
1034
      $cleared = search_expression_extract($after_clear, $case[0]);
1035
      $this->assertFalse(isset($cleared), "After clearing, value could not be retrieved for case $index");
1036
    }
1037
  }
1038
}
1039

    
1040
/**
1041
 * Tests that comment count display toggles properly on comment status of node
1042
 *
1043
 * Issue 537278
1044
 *
1045
 * - Nodes with comment status set to Open should always how comment counts
1046
 * - Nodes with comment status set to Closed should show comment counts
1047
 *     only when there are comments
1048
 * - Nodes with comment status set to Hidden should never show comment counts
1049
 */
1050
class SearchCommentCountToggleTestCase extends DrupalWebTestCase {
1051
  protected $searching_user;
1052
  protected $searchable_nodes;
1053

    
1054
  public static function getInfo() {
1055
    return array(
1056
      'name' => 'Comment count toggle',
1057
      'description' => 'Verify that comment count display toggles properly on comment status of node.',
1058
      'group' => 'Search',
1059
    );
1060
  }
1061

    
1062
  function setUp() {
1063
    parent::setUp('search');
1064

    
1065
    // Create searching user.
1066
    $this->searching_user = $this->drupalCreateUser(array('search content', 'access content', 'access comments', 'skip comment approval'));
1067

    
1068
    // Create initial nodes.
1069
    $node_params = array('type' => 'article', 'body' => array(LANGUAGE_NONE => array(array('value' => 'SearchCommentToggleTestCase'))));
1070

    
1071
    $this->searchable_nodes['1 comment'] = $this->drupalCreateNode($node_params);
1072
    $this->searchable_nodes['0 comments'] = $this->drupalCreateNode($node_params);
1073

    
1074
    // Login with sufficient privileges.
1075
    $this->drupalLogin($this->searching_user);
1076

    
1077
    // Create a comment array
1078
    $edit_comment = array();
1079
    $edit_comment['subject'] = $this->randomName();
1080
    $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][value]'] = $this->randomName();
1081
    $filtered_html_format_id = 'filtered_html';
1082
    $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][format]'] = $filtered_html_format_id;
1083

    
1084
    // Post comment to the test node with comment
1085
    $this->drupalPost('comment/reply/' . $this->searchable_nodes['1 comment']->nid, $edit_comment, t('Save'));
1086

    
1087
    // First update the index. This does the initial processing.
1088
    node_update_index();
1089

    
1090
    // Then, run the shutdown function. Testing is a unique case where indexing
1091
    // and searching has to happen in the same request, so running the shutdown
1092
    // function manually is needed to finish the indexing process.
1093
    search_update_totals();
1094
  }
1095

    
1096
  /**
1097
   * Verify that comment count display toggles properly on comment status of node
1098
   */
1099
  function testSearchCommentCountToggle() {
1100
    // Search for the nodes by string in the node body.
1101
    $edit = array(
1102
      'search_block_form' => "'SearchCommentToggleTestCase'",
1103
    );
1104

    
1105
    // Test comment count display for nodes with comment status set to Open
1106
    $this->drupalPost('', $edit, t('Search'));
1107
    $this->assertText(t('0 comments'), 'Empty comment count displays for nodes with comment status set to Open');
1108
    $this->assertText(t('1 comment'), 'Non-empty comment count displays for nodes with comment status set to Open');
1109

    
1110
    // Test comment count display for nodes with comment status set to Closed
1111
    $this->searchable_nodes['0 comments']->comment = COMMENT_NODE_CLOSED;
1112
    node_save($this->searchable_nodes['0 comments']);
1113
    $this->searchable_nodes['1 comment']->comment = COMMENT_NODE_CLOSED;
1114
    node_save($this->searchable_nodes['1 comment']);
1115

    
1116
    $this->drupalPost('', $edit, t('Search'));
1117
    $this->assertNoText(t('0 comments'), 'Empty comment count does not display for nodes with comment status set to Closed');
1118
    $this->assertText(t('1 comment'), 'Non-empty comment count displays for nodes with comment status set to Closed');
1119

    
1120
    // Test comment count display for nodes with comment status set to Hidden
1121
    $this->searchable_nodes['0 comments']->comment = COMMENT_NODE_HIDDEN;
1122
    node_save($this->searchable_nodes['0 comments']);
1123
    $this->searchable_nodes['1 comment']->comment = COMMENT_NODE_HIDDEN;
1124
    node_save($this->searchable_nodes['1 comment']);
1125

    
1126
    $this->drupalPost('', $edit, t('Search'));
1127
    $this->assertNoText(t('0 comments'), 'Empty comment count does not display for nodes with comment status set to Hidden');
1128
    $this->assertNoText(t('1 comment'), 'Non-empty comment count does not display for nodes with comment status set to Hidden');
1129
  }
1130
}
1131

    
1132
/**
1133
 * Test search_simplify() on every Unicode character, and some other cases.
1134
 */
1135
class SearchSimplifyTestCase extends DrupalWebTestCase {
1136
  public static function getInfo() {
1137
    return array(
1138
      'name' => 'Search simplify',
1139
      'description' => 'Check that the search_simply() function works as intended.',
1140
      'group' => 'Search',
1141
    );
1142
  }
1143

    
1144
  /**
1145
   * Tests that all Unicode characters simplify correctly.
1146
   */
1147
  function testSearchSimplifyUnicode() {
1148
    // This test uses a file that was constructed so that the even lines are
1149
    // boundary characters, and the odd lines are valid word characters. (It
1150
    // was generated as a sequence of all the Unicode characters, and then the
1151
    // boundary chararacters (punctuation, spaces, etc.) were split off into
1152
    // their own lines).  So the even-numbered lines should simplify to nothing,
1153
    // and the odd-numbered lines we need to split into shorter chunks and
1154
    // verify that simplification doesn't lose any characters.
1155
    $input = file_get_contents(DRUPAL_ROOT . '/modules/search/tests/UnicodeTest.txt');
1156
    $basestrings = explode(chr(10), $input);
1157
    $strings = array();
1158
    foreach ($basestrings as $key => $string) {
1159
      if ($key %2) {
1160
        // Even line - should simplify down to a space.
1161
        $simplified = search_simplify($string);
1162
        $this->assertIdentical($simplified, ' ', "Line $key is excluded from the index");
1163
      }
1164
      else {
1165
        // Odd line, should be word characters.
1166
        // Split this into 30-character chunks, so we don't run into limits
1167
        // of truncation in search_simplify().
1168
        $start = 0;
1169
        while ($start < drupal_strlen($string)) {
1170
          $newstr = drupal_substr($string, $start, 30);
1171
          // Special case: leading zeros are removed from numeric strings,
1172
          // and there's one string in this file that is numbers starting with
1173
          // zero, so prepend a 1 on that string.
1174
          if (preg_match('/^[0-9]+$/', $newstr)) {
1175
            $newstr = '1' . $newstr;
1176
          }
1177
          $strings[] = $newstr;
1178
          $start += 30;
1179
        }
1180
      }
1181
    }
1182
    foreach ($strings as $key => $string) {
1183
      $simplified = search_simplify($string);
1184
      $this->assertTrue(drupal_strlen($simplified) >= drupal_strlen($string), "Nothing is removed from string $key.");
1185
    }
1186

    
1187
    // Test the low-numbered ASCII control characters separately. They are not
1188
    // in the text file because they are problematic for diff, especially \0.
1189
    $string = '';
1190
    for ($i = 0; $i < 32; $i++) {
1191
      $string .= chr($i);
1192
    }
1193
    $this->assertIdentical(' ', search_simplify($string), 'Search simplify works for ASCII control characters.');
1194
  }
1195

    
1196
  /**
1197
   * Tests that search_simplify() does the right thing with punctuation.
1198
   */
1199
  function testSearchSimplifyPunctuation() {
1200
    $cases = array(
1201
      array('20.03/94-28,876', '20039428876', 'Punctuation removed from numbers'),
1202
      array('great...drupal--module', 'great drupal module', 'Multiple dot and dashes are word boundaries'),
1203
      array('very_great-drupal.module', 'verygreatdrupalmodule', 'Single dot, dash, underscore are removed'),
1204
      array('regular,punctuation;word', 'regular punctuation word', 'Punctuation is a word boundary'),
1205
    );
1206

    
1207
    foreach ($cases as $case) {
1208
      $out = trim(search_simplify($case[0]));
1209
      $this->assertEqual($out, $case[1], $case[2]);
1210
    }
1211
  }
1212
}
1213

    
1214

    
1215
/**
1216
 * Tests keywords and conditions.
1217
 */
1218
class SearchKeywordsConditions extends DrupalWebTestCase {
1219

    
1220
  public static function getInfo() {
1221
    return array(
1222
      'name' => 'Keywords and conditions',
1223
      'description' => 'Verify the search pulls in keywords and extra conditions.',
1224
      'group' => 'Search',
1225
    );
1226
  }
1227

    
1228
  function setUp() {
1229
    parent::setUp('search', 'search_extra_type');
1230
    // Create searching user.
1231
    $this->searching_user = $this->drupalCreateUser(array('search content', 'access content', 'access comments', 'skip comment approval'));
1232
    // Login with sufficient privileges.
1233
    $this->drupalLogin($this->searching_user);
1234
    // Test with all search modules enabled.
1235
    variable_set('search_active_modules', array('node' => 'node', 'user' => 'user', 'search_extra_type' => 'search_extra_type'));
1236
    menu_rebuild();
1237
  }
1238

    
1239
  /**
1240
   * Verify the kewords are captured and conditions respected.
1241
   */
1242
  function testSearchKeyswordsConditions() {
1243
    // No keys, not conditions - no results.
1244
    $this->drupalGet('search/dummy_path');
1245
    $this->assertNoText('Dummy search snippet to display');
1246
    // With keys - get results.
1247
    $keys = 'bike shed ' . $this->randomName();
1248
    $this->drupalGet("search/dummy_path/{$keys}");
1249
    $this->assertText("Dummy search snippet to display. Keywords: {$keys}");
1250
    $keys = 'blue drop ' . $this->randomName();
1251
    $this->drupalGet("search/dummy_path", array('query' => array('keys' => $keys)));
1252
    $this->assertText("Dummy search snippet to display. Keywords: {$keys}");
1253
    // Add some conditions and keys.
1254
    $keys = 'moving drop ' . $this->randomName();
1255
    $this->drupalGet("search/dummy_path/bike", array('query' => array('search_conditions' => $keys)));
1256
    $this->assertText("Dummy search snippet to display.");
1257
    $this->assertRaw(print_r(array('search_conditions' => $keys), TRUE));
1258
    // Add some conditions and no keys.
1259
    $keys = 'drop kick ' . $this->randomName();
1260
    $this->drupalGet("search/dummy_path", array('query' => array('search_conditions' => $keys)));
1261
    $this->assertText("Dummy search snippet to display.");
1262
    $this->assertRaw(print_r(array('search_conditions' => $keys), TRUE));
1263
  }
1264
}
1265

    
1266
/**
1267
 * Tests that numbers can be searched.
1268
 */
1269
class SearchNumbersTestCase extends DrupalWebTestCase {
1270
  protected $test_user;
1271
  protected $numbers;
1272
  protected $nodes;
1273

    
1274
  public static function getInfo() {
1275
    return array(
1276
      'name' => 'Search numbers',
1277
      'description' => 'Check that numbers can be searched',
1278
      'group' => 'Search',
1279
    );
1280
  }
1281

    
1282
  function setUp() {
1283
    parent::setUp('search');
1284

    
1285
    $this->test_user = $this->drupalCreateUser(array('search content', 'access content', 'administer nodes', 'access site reports'));
1286
    $this->drupalLogin($this->test_user);
1287

    
1288
    // Create content with various numbers in it.
1289
    // Note: 50 characters is the current limit of the search index's word
1290
    // field.
1291
    $this->numbers = array(
1292
      'ISBN' => '978-0446365383',
1293
      'UPC' => '036000 291452',
1294
      'EAN bar code' => '5901234123457',
1295
      'negative' => '-123456.7890',
1296
      'quoted negative' => '"-123456.7890"',
1297
      'leading zero' => '0777777777',
1298
      'tiny' => '111',
1299
      'small' => '22222222222222',
1300
      'medium' => '333333333333333333333333333',
1301
      'large' => '444444444444444444444444444444444444444',
1302
      'gigantic' => '5555555555555555555555555555555555555555555555555',
1303
      'over fifty characters' => '666666666666666666666666666666666666666666666666666666666666',
1304
      'date', '01/02/2009',
1305
      'commas', '987,654,321',
1306
    );
1307

    
1308
    foreach ($this->numbers as $doc => $num) {
1309
      $info = array(
1310
        'body' => array(LANGUAGE_NONE => array(array('value' => $num))),
1311
        'type' => 'page',
1312
        'language' => LANGUAGE_NONE,
1313
        'title' => $doc . ' number',
1314
      );
1315
      $this->nodes[$doc] = $this->drupalCreateNode($info);
1316
    }
1317

    
1318
    // Run cron to ensure the content is indexed.
1319
    $this->cronRun();
1320
    $this->drupalGet('admin/reports/dblog');
1321
    $this->assertText(t('Cron run completed'), 'Log shows cron run completed');
1322
  }
1323

    
1324
  /**
1325
   * Tests that all the numbers can be searched.
1326
   */
1327
  function testNumberSearching() {
1328
    $types = array_keys($this->numbers);
1329

    
1330
    foreach ($types as $type) {
1331
      $number = $this->numbers[$type];
1332
      // If the number is negative, remove the - sign, because - indicates
1333
      // "not keyword" when searching.
1334
      $number = ltrim($number, '-');
1335
      $node = $this->nodes[$type];
1336

    
1337
      // Verify that the node title does not appear on the search page
1338
      // with a dummy search.
1339
      $this->drupalPost('search/node',
1340
        array('keys' => 'foo'),
1341
        t('Search'));
1342
      $this->assertNoText($node->title, $type . ': node title not shown in dummy search');
1343

    
1344
      // Verify that the node title does appear as a link on the search page
1345
      // when searching for the number.
1346
      $this->drupalPost('search/node',
1347
        array('keys' => $number),
1348
        t('Search'));
1349
      $this->assertText($node->title, format_string('%type: node title shown (search found the node) in search for number %number.', array('%type' => $type, '%number' => $number)));
1350
    }
1351
  }
1352
}
1353

    
1354
/**
1355
 * Tests that numbers can be searched, with more complex matching.
1356
 */
1357
class SearchNumberMatchingTestCase extends DrupalWebTestCase {
1358
  protected $test_user;
1359
  protected $numbers;
1360
  protected $nodes;
1361

    
1362
  public static function getInfo() {
1363
    return array(
1364
      'name' => 'Search number matching',
1365
      'description' => 'Check that numbers can be searched with more complex matching',
1366
      'group' => 'Search',
1367
    );
1368
  }
1369

    
1370
  function setUp() {
1371
    parent::setUp('search');
1372

    
1373
    $this->test_user = $this->drupalCreateUser(array('search content', 'access content', 'administer nodes', 'access site reports'));
1374
    $this->drupalLogin($this->test_user);
1375

    
1376
    // Define a group of numbers that should all match each other --
1377
    // numbers with internal punctuation should match each other, as well
1378
    // as numbers with and without leading zeros and leading/trailing
1379
    // . and -.
1380
    $this->numbers = array(
1381
      '123456789',
1382
      '12/34/56789',
1383
      '12.3456789',
1384
      '12-34-56789',
1385
      '123,456,789',
1386
      '-123456789',
1387
      '0123456789',
1388
    );
1389

    
1390
    foreach ($this->numbers as $num) {
1391
      $info = array(
1392
        'body' => array(LANGUAGE_NONE => array(array('value' => $num))),
1393
        'type' => 'page',
1394
        'language' => LANGUAGE_NONE,
1395
      );
1396
      $this->nodes[] = $this->drupalCreateNode($info);
1397
    }
1398

    
1399
    // Run cron to ensure the content is indexed.
1400
    $this->cronRun();
1401
    $this->drupalGet('admin/reports/dblog');
1402
    $this->assertText(t('Cron run completed'), 'Log shows cron run completed');
1403
  }
1404

    
1405
  /**
1406
   * Tests that all the numbers can be searched.
1407
   */
1408
  function testNumberSearching() {
1409
    for ($i = 0; $i < count($this->numbers); $i++) {
1410
      $node = $this->nodes[$i];
1411

    
1412
      // Verify that the node title does not appear on the search page
1413
      // with a dummy search.
1414
      $this->drupalPost('search/node',
1415
        array('keys' => 'foo'),
1416
        t('Search'));
1417
      $this->assertNoText($node->title, format_string('%number: node title not shown in dummy search', array('%number' => $i)));
1418

    
1419
      // Now verify that we can find node i by searching for any of the
1420
      // numbers.
1421
      for ($j = 0; $j < count($this->numbers); $j++) {
1422
        $number = $this->numbers[$j];
1423
        // If the number is negative, remove the - sign, because - indicates
1424
        // "not keyword" when searching.
1425
        $number = ltrim($number, '-');
1426

    
1427
        $this->drupalPost('search/node',
1428
          array('keys' => $number),
1429
          t('Search'));
1430
        $this->assertText($node->title, format_string('%i: node title shown (search found the node) in search for number %number', array('%i' => $i, '%number' => $number)));
1431
      }
1432
    }
1433

    
1434
  }
1435
}
1436

    
1437
/**
1438
 * Test config page.
1439
 */
1440
class SearchConfigSettingsForm extends DrupalWebTestCase {
1441
  public $search_user;
1442
  public $search_node;
1443

    
1444
  public static function getInfo() {
1445
    return array(
1446
      'name' => 'Config settings form',
1447
      'description' => 'Verify the search config settings form.',
1448
      'group' => 'Search',
1449
    );
1450
  }
1451

    
1452
  function setUp() {
1453
    parent::setUp('search', 'search_extra_type');
1454

    
1455
    // Login as a user that can create and search content.
1456
    $this->search_user = $this->drupalCreateUser(array('search content', 'administer search', 'administer nodes', 'bypass node access', 'access user profiles', 'administer users', 'administer blocks'));
1457
    $this->drupalLogin($this->search_user);
1458

    
1459
    // Add a single piece of content and index it.
1460
    $node = $this->drupalCreateNode();
1461
    $this->search_node = $node;
1462
    // Link the node to itself to test that it's only indexed once. The content
1463
    // also needs the word "pizza" so we can use it as the search keyword.
1464
    $langcode = LANGUAGE_NONE;
1465
    $body_key = "body[$langcode][0][value]";
1466
    $edit[$body_key] = l($node->title, 'node/' . $node->nid) . ' pizza sandwich';
1467
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
1468

    
1469
    node_update_index();
1470
    search_update_totals();
1471

    
1472
    // Enable the search block.
1473
    $edit = array();
1474
    $edit['blocks[search_form][region]'] = 'content';
1475
    $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
1476
  }
1477

    
1478
  /**
1479
   * Verify the search settings form.
1480
   */
1481
  function testSearchSettingsPage() {
1482

    
1483
    // Test that the settings form displays the correct count of items left to index.
1484
    $this->drupalGet('admin/config/search/settings');
1485
    $this->assertText(t('There are @count items left to index.', array('@count' => 0)));
1486

    
1487
    // Test the re-index button.
1488
    $this->drupalPost('admin/config/search/settings', array(), t('Re-index site'));
1489
    $this->assertText(t('Are you sure you want to re-index the site'));
1490
    $this->drupalPost('admin/config/search/settings/reindex', array(), t('Re-index site'));
1491
    $this->assertText(t('The index will be rebuilt'));
1492
    $this->drupalGet('admin/config/search/settings');
1493
    $this->assertText(t('There is 1 item left to index.'));
1494

    
1495
    // Test that the form saves with the default values.
1496
    $this->drupalPost('admin/config/search/settings', array(), t('Save configuration'));
1497
    $this->assertText(t('The configuration options have been saved.'), 'Form saves with the default values.');
1498

    
1499
    // Test that the form does not save with an invalid word length.
1500
    $edit = array(
1501
      'minimum_word_size' => $this->randomName(3),
1502
    );
1503
    $this->drupalPost('admin/config/search/settings', $edit, t('Save configuration'));
1504
    $this->assertNoText(t('The configuration options have been saved.'), 'Form does not save with an invalid word length.');
1505
  }
1506

    
1507
  /**
1508
   * Verify that you can disable individual search modules.
1509
   */
1510
  function testSearchModuleDisabling() {
1511
    // Array of search modules to test: 'path' is the search path, 'title' is
1512
    // the tab title, 'keys' are the keywords to search for, and 'text' is
1513
    // the text to assert is on the results page.
1514
    $module_info = array(
1515
      'node' => array(
1516
        'path' => 'node',
1517
        'title' => 'Content',
1518
        'keys' => 'pizza',
1519
        'text' => $this->search_node->title,
1520
      ),
1521
      'user' => array(
1522
        'path' => 'user',
1523
        'title' => 'User',
1524
        'keys' => $this->search_user->name,
1525
        'text' => $this->search_user->mail,
1526
      ),
1527
      'search_extra_type' => array(
1528
        'path' => 'dummy_path',
1529
        'title' => 'Dummy search type',
1530
        'keys' => 'foo',
1531
        'text' => 'Dummy search snippet to display',
1532
      ),
1533
    );
1534
    $modules = array_keys($module_info);
1535

    
1536
    // Test each module if it's enabled as the only search module.
1537
    foreach ($modules as $module) {
1538
      // Enable the one module and disable other ones.
1539
      $info = $module_info[$module];
1540
      $edit = array();
1541
      foreach ($modules as $other) {
1542
        $edit['search_active_modules[' . $other . ']'] = (($other == $module) ? $module : FALSE);
1543
      }
1544
      $edit['search_default_module'] = $module;
1545
      $this->drupalPost('admin/config/search/settings', $edit, t('Save configuration'));
1546

    
1547
      // Run a search from the correct search URL.
1548
      $this->drupalGet('search/' . $info['path'] . '/' . $info['keys']);
1549
      $this->assertNoText('no results', $info['title'] . ' search found results');
1550
      $this->assertText($info['text'], 'Correct search text found');
1551

    
1552
      // Verify that other module search tab titles are not visible.
1553
      foreach ($modules as $other) {
1554
        if ($other != $module) {
1555
          $title = $module_info[$other]['title'];
1556
          $this->assertNoText($title, $title . ' search tab is not shown');
1557
        }
1558
      }
1559

    
1560
      // Run a search from the search block on the node page. Verify you get
1561
      // to this module's search results page.
1562
      $terms = array('search_block_form' => $info['keys']);
1563
      $this->drupalPost('node', $terms, t('Search'));
1564
      $this->assertEqual(
1565
        $this->getURL(),
1566
        url('search/' . $info['path'] . '/' . $info['keys'], array('absolute' => TRUE)),
1567
        'Block redirected to right search page');
1568

    
1569
      // Try an invalid search path. Should redirect to our active module.
1570
      $this->drupalGet('search/not_a_module_path');
1571
      $this->assertEqual(
1572
        $this->getURL(),
1573
        url('search/' . $info['path'], array('absolute' => TRUE)),
1574
        'Invalid search path redirected to default search page');
1575
    }
1576

    
1577
    // Test with all search modules enabled. When you go to the search
1578
    // page or run search, all modules should be shown.
1579
    $edit = array();
1580
    foreach ($modules as $module) {
1581
      $edit['search_active_modules[' . $module . ']'] = $module;
1582
    }
1583
    $edit['search_default_module'] = 'node';
1584

    
1585
    $this->drupalPost('admin/config/search/settings', $edit, t('Save configuration'));
1586

    
1587
    foreach (array('search/node/pizza', 'search/node') as $path) {
1588
      $this->drupalGet($path);
1589
      foreach ($modules as $module) {
1590
        $title = $module_info[$module]['title'];
1591
        $this->assertText($title, format_string('%title search tab is shown', array('%title' => $title)));
1592
      }
1593
    }
1594
  }
1595
}
1596

    
1597
/**
1598
 * Tests the search_excerpt() function.
1599
 */
1600
class SearchExcerptTestCase extends DrupalWebTestCase {
1601
  public static function getInfo() {
1602
    return array(
1603
      'name' => 'Search excerpt extraction',
1604
      'description' => 'Tests that the search_excerpt() function works.',
1605
      'group' => 'Search',
1606
    );
1607
  }
1608

    
1609
  function setUp() {
1610
    parent::setUp('search');
1611
  }
1612

    
1613
  /**
1614
   * Tests search_excerpt() with several simulated search keywords.
1615
   *
1616
   * Passes keywords and a sample marked up string, "The quick
1617
   * brown fox jumps over the lazy dog", and compares it to the
1618
   * correctly marked up string. The correctly marked up string
1619
   * contains either highlighted keywords or the original marked
1620
   * up string if no keywords matched the string.
1621
   */
1622
  function testSearchExcerpt() {
1623
    // Make some text with entities and tags.
1624
    $text = 'The <strong>quick</strong> <a href="#">brown</a> fox &amp; jumps <h2>over</h2> the lazy dog';
1625
    // Note: The search_excerpt() function adds some extra spaces -- not
1626
    // important for HTML formatting. Remove these for comparison.
1627
    $expected = 'The quick brown fox &amp; jumps over the lazy dog';
1628
    $result = preg_replace('| +|', ' ', search_excerpt('nothing', $text));
1629
    $this->assertEqual(preg_replace('| +|', ' ', $result), $expected, 'Entire string is returned when keyword is not found in short string');
1630

    
1631
    $result = preg_replace('| +|', ' ', search_excerpt('fox', $text));
1632
    $this->assertEqual($result, 'The quick brown <strong>fox</strong> &amp; jumps over the lazy dog ...', 'Found keyword is highlighted');
1633

    
1634
    $longtext = str_repeat($text . ' ', 10);
1635
    $result = preg_replace('| +|', ' ', search_excerpt('nothing', $longtext));
1636
    $this->assertTrue(strpos($result, $expected) === 0, 'When keyword is not found in long string, return value starts as expected');
1637

    
1638
    $entities = str_repeat('k&eacute;sz&iacute;t&eacute;se ', 20);
1639
    $result = preg_replace('| +|', ' ', search_excerpt('nothing', $entities));
1640
    $this->assertFalse(strpos($result, '&'), 'Entities are not present in excerpt');
1641
    $this->assertTrue(strpos($result, 'í') > 0, 'Entities are converted in excerpt');
1642

    
1643
    // The node body that will produce this rendered $text is:
1644
    // 123456789 HTMLTest +123456789+&lsquo;  +&lsquo;  +&lsquo;  +&lsquo;  +12345678  &nbsp;&nbsp;  +&lsquo;  +&lsquo;  +&lsquo;   &lsquo;
1645
    $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> ";
1646
    $result = search_excerpt('HTMLTest', $text);
1647
    $this->assertFalse(empty($result),  'Rendered Multi-byte HTML encodings are not corrupted in search excerpts');
1648
  }
1649

    
1650
  /**
1651
   * Tests search_excerpt() with search keywords matching simplified words.
1652
   *
1653
   * Excerpting should handle keywords that are matched only after going through
1654
   * search_simplify(). This test passes keywords that match simplified words
1655
   * and compares them with strings that contain the original unsimplified word.
1656
   */
1657
  function testSearchExcerptSimplified() {
1658
    $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 Drupalism? Suspendisse ac lacus libero. Ut non est vel nisl faucibus interdum nec sed leo. Pellentesque sem risus, vulputate eu semper eget, auctor in libero.';
1659
    $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.';
1660

    
1661
    // Make some text with some keywords that will get simplified.
1662
    $text = $lorem1 . ' Number: 123456.7890 Hyphenated: one-two abc,def ' . $lorem2;
1663
    // Note: The search_excerpt() function adds some extra spaces -- not
1664
    // important for HTML formatting. Remove these for comparison.
1665
    $result = preg_replace('| +|', ' ', search_excerpt('123456.7890', $text));
1666
    $this->assertTrue(strpos($result, 'Number: <strong>123456.7890</strong>') !== FALSE, 'Numeric keyword is highlighted with exact match');
1667

    
1668
    $result = preg_replace('| +|', ' ', search_excerpt('1234567890', $text));
1669
    $this->assertTrue(strpos($result, 'Number: <strong>123456.7890</strong>') !== FALSE, 'Numeric keyword is highlighted with simplified match');
1670

    
1671
    $result = preg_replace('| +|', ' ', search_excerpt('Number 1234567890', $text));
1672
    $this->assertTrue(strpos($result, '<strong>Number</strong>: <strong>123456.7890</strong>') !== FALSE, 'Punctuated and numeric keyword is highlighted with simplified match');
1673

    
1674
    $result = preg_replace('| +|', ' ', search_excerpt('"Number 1234567890"', $text));
1675
    $this->assertTrue(strpos($result, '<strong>Number: 123456.7890</strong>') !== FALSE, 'Phrase with punctuated and numeric keyword is highlighted with simplified match');
1676

    
1677
    $result = preg_replace('| +|', ' ', search_excerpt('"Hyphenated onetwo"', $text));
1678
    $this->assertTrue(strpos($result, '<strong>Hyphenated: one-two</strong>') !== FALSE, 'Phrase with punctuated and hyphenated keyword is highlighted with simplified match');
1679

    
1680
    $result = preg_replace('| +|', ' ', search_excerpt('"abc def"', $text));
1681
    $this->assertTrue(strpos($result, '<strong>abc,def</strong>') !== FALSE, 'Phrase with keyword simplified into two separate words is highlighted with simplified match');
1682

    
1683
    // Test phrases with characters which are being truncated.
1684
    $result = preg_replace('| +|', ' ', search_excerpt('"ipsum _"', $text));
1685
    $this->assertTrue(strpos($result, '<strong>ipsum </strong>') !== FALSE, 'Only valid part of the phrase is highlighted and invalid part containing "_" is ignored.');
1686

    
1687
    $result = preg_replace('| +|', ' ', search_excerpt('"ipsum 0000"', $text));
1688
    $this->assertTrue(strpos($result, '<strong>ipsum </strong>') !== FALSE, 'Only valid part of the phrase is highlighted and invalid part "0000" is ignored.');
1689

    
1690
    // Test combination of the valid keyword and keyword containing only
1691
    // characters which are being truncated during simplification.
1692
    $result = preg_replace('| +|', ' ', search_excerpt('ipsum _', $text));
1693
    $this->assertTrue(strpos($result, '<strong>ipsum</strong>') !== FALSE, 'Only valid keyword is highlighted and invalid keyword "_" is ignored.');
1694

    
1695
    $result = preg_replace('| +|', ' ', search_excerpt('ipsum 0000', $text));
1696
    $this->assertTrue(strpos($result, '<strong>ipsum</strong>') !== FALSE, 'Only valid keyword is highlighted and invalid keyword "0000" is ignored.');
1697
  }
1698
}
1699

    
1700
/**
1701
 * Test the CJK tokenizer.
1702
 */
1703
class SearchTokenizerTestCase extends DrupalWebTestCase {
1704
  public static function getInfo() {
1705
    return array(
1706
      'name' => 'CJK tokenizer',
1707
      'description' => 'Check that CJK tokenizer works as intended.',
1708
      'group' => 'Search',
1709
    );
1710
  }
1711

    
1712
  function setUp() {
1713
    parent::setUp('search');
1714
  }
1715

    
1716
  /**
1717
   * Verifies that strings of CJK characters are tokenized.
1718
   *
1719
   * The search_simplify() function does special things with numbers, symbols,
1720
   * and punctuation. So we only test that CJK characters that are not in these
1721
   * character classes are tokenized properly. See PREG_CLASS_CKJ for more
1722
   * information.
1723
   */
1724
  function testTokenizer() {
1725
    // Set the minimum word size to 1 (to split all CJK characters) and make
1726
    // sure CJK tokenizing is turned on.
1727
    variable_set('minimum_word_size', 1);
1728
    variable_set('overlap_cjk', TRUE);
1729
    $this->refreshVariables();
1730

    
1731
    // Create a string of CJK characters from various character ranges in
1732
    // the Unicode tables.
1733

    
1734
    // Beginnings of the character ranges.
1735
    $starts = array(
1736
      'CJK unified' => 0x4e00,
1737
      'CJK Ext A' => 0x3400,
1738
      'CJK Compat' => 0xf900,
1739
      'Hangul Jamo' => 0x1100,
1740
      'Hangul Ext A' => 0xa960,
1741
      'Hangul Ext B' => 0xd7b0,
1742
      'Hangul Compat' => 0x3131,
1743
      'Half non-punct 1' => 0xff21,
1744
      'Half non-punct 2' => 0xff41,
1745
      'Half non-punct 3' => 0xff66,
1746
      'Hangul Syllables' => 0xac00,
1747
      'Hiragana' => 0x3040,
1748
      'Katakana' => 0x30a1,
1749
      'Katakana Ext' => 0x31f0,
1750
      'CJK Reserve 1' => 0x20000,
1751
      'CJK Reserve 2' => 0x30000,
1752
      'Bomofo' => 0x3100,
1753
      'Bomofo Ext' => 0x31a0,
1754
      'Lisu' => 0xa4d0,
1755
      'Yi' => 0xa000,
1756
    );
1757

    
1758
    // Ends of the character ranges.
1759
    $ends = array(
1760
      'CJK unified' => 0x9fcf,
1761
      'CJK Ext A' => 0x4dbf,
1762
      'CJK Compat' => 0xfaff,
1763
      'Hangul Jamo' => 0x11ff,
1764
      'Hangul Ext A' => 0xa97f,
1765
      'Hangul Ext B' => 0xd7ff,
1766
      'Hangul Compat' => 0x318e,
1767
      'Half non-punct 1' => 0xff3a,
1768
      'Half non-punct 2' => 0xff5a,
1769
      'Half non-punct 3' => 0xffdc,
1770
      'Hangul Syllables' => 0xd7af,
1771
      'Hiragana' => 0x309f,
1772
      'Katakana' => 0x30ff,
1773
      'Katakana Ext' => 0x31ff,
1774
      'CJK Reserve 1' => 0x2fffd,
1775
      'CJK Reserve 2' => 0x3fffd,
1776
      'Bomofo' => 0x312f,
1777
      'Bomofo Ext' => 0x31b7,
1778
      'Lisu' => 0xa4fd,
1779
      'Yi' => 0xa48f,
1780
    );
1781

    
1782
    // Generate characters consisting of starts, midpoints, and ends.
1783
    $chars = array();
1784
    $charcodes = array();
1785
    foreach ($starts as $key => $value) {
1786
      $charcodes[] = $starts[$key];
1787
      $chars[] = $this->code2utf($starts[$key]);
1788
      $mid = round(0.5 * ($starts[$key] + $ends[$key]));
1789
      $charcodes[] = $mid;
1790
      $chars[] = $this->code2utf($mid);
1791
      $charcodes[] = $ends[$key];
1792
      $chars[] = $this->code2utf($ends[$key]);
1793
    }
1794

    
1795
    // Merge into a string and tokenize.
1796
    $string = implode('', $chars);
1797
    $out = trim(search_simplify($string));
1798
    $expected = drupal_strtolower(implode(' ', $chars));
1799

    
1800
    // Verify that the output matches what we expect.
1801
    $this->assertEqual($out, $expected, 'CJK tokenizer worked on all supplied CJK characters');
1802
  }
1803

    
1804
  /**
1805
   * Verifies that strings of non-CJK characters are not tokenized.
1806
   *
1807
   * This is just a sanity check - it verifies that strings of letters are
1808
   * not tokenized.
1809
   */
1810
  function testNoTokenizer() {
1811
    // Set the minimum word size to 1 (to split all CJK characters) and make
1812
    // sure CJK tokenizing is turned on.
1813
    variable_set('minimum_word_size', 1);
1814
    variable_set('overlap_cjk', TRUE);
1815
    $this->refreshVariables();
1816

    
1817
    $letters = 'abcdefghijklmnopqrstuvwxyz';
1818
    $out = trim(search_simplify($letters));
1819

    
1820
    $this->assertEqual($letters, $out, 'Letters are not CJK tokenized');
1821
  }
1822

    
1823
  /**
1824
   * Like PHP chr() function, but for unicode characters.
1825
   *
1826
   * chr() only works for ASCII characters up to character 255. This function
1827
   * converts a number to the corresponding unicode character. Adapted from
1828
   * functions supplied in comments on several functions on php.net.
1829
   */
1830
  function code2utf($num) {
1831
    if ($num < 128) {
1832
      return chr($num);
1833
    }
1834

    
1835
    if ($num < 2048) {
1836
      return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
1837
    }
1838

    
1839
    if ($num < 65536) {
1840
      return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
1841
    }
1842

    
1843
    if ($num < 2097152) {
1844
      return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
1845
    }
1846

    
1847
    return '';
1848
  }
1849
}
1850

    
1851
/**
1852
 * Tests that we can embed a form in search results and submit it.
1853
 */
1854
class SearchEmbedForm extends DrupalWebTestCase {
1855
  /**
1856
   * Node used for testing.
1857
   */
1858
  public $node;
1859

    
1860
  /**
1861
   * Count of how many times the form has been submitted.
1862
   */
1863
  public $submit_count = 0;
1864

    
1865
  public static function getInfo() {
1866
    return array(
1867
      'name' => 'Embedded forms',
1868
      'description' => 'Verifies that a form embedded in search results works',
1869
      'group' => 'Search',
1870
    );
1871
  }
1872

    
1873
  function setUp() {
1874
    parent::setUp('search', 'search_embedded_form');
1875

    
1876
    // Create a user and a node, and update the search index.
1877
    $test_user = $this->drupalCreateUser(array('access content', 'search content', 'administer nodes'));
1878
    $this->drupalLogin($test_user);
1879

    
1880
    $this->node = $this->drupalCreateNode();
1881

    
1882
    node_update_index();
1883
    search_update_totals();
1884

    
1885
    // Set up a dummy initial count of times the form has been submitted.
1886
    $this->submit_count = 12;
1887
    variable_set('search_embedded_form_submitted', $this->submit_count);
1888
    $this->refreshVariables();
1889
  }
1890

    
1891
  /**
1892
   * Tests that the embedded form appears and can be submitted.
1893
   */
1894
  function testEmbeddedForm() {
1895
    // First verify we can submit the form from the module's page.
1896
    $this->drupalPost('search_embedded_form',
1897
      array('name' => 'John'),
1898
      t('Send away'));
1899
    $this->assertText(t('Test form was submitted'), 'Form message appears');
1900
    $count = variable_get('search_embedded_form_submitted', 0);
1901
    $this->assertEqual($this->submit_count + 1, $count, 'Form submission count is correct');
1902
    $this->submit_count = $count;
1903

    
1904
    // Now verify that we can see and submit the form from the search results.
1905
    $this->drupalGet('search/node/' . $this->node->title);
1906
    $this->assertText(t('Your name'), 'Form is visible');
1907
    $this->drupalPost('search/node/' . $this->node->title,
1908
      array('name' => 'John'),
1909
      t('Send away'));
1910
    $this->assertText(t('Test form was submitted'), 'Form message appears');
1911
    $count = variable_get('search_embedded_form_submitted', 0);
1912
    $this->assertEqual($this->submit_count + 1, $count, 'Form submission count is correct');
1913
    $this->submit_count = $count;
1914

    
1915
    // Now verify that if we submit the search form, it doesn't count as
1916
    // our form being submitted.
1917
    $this->drupalPost('search',
1918
      array('keys' => 'foo'),
1919
      t('Search'));
1920
    $this->assertNoText(t('Test form was submitted'), 'Form message does not appear');
1921
    $count = variable_get('search_embedded_form_submitted', 0);
1922
    $this->assertEqual($this->submit_count, $count, 'Form submission count is correct');
1923
    $this->submit_count = $count;
1924
  }
1925
}
1926

    
1927
/**
1928
 * Tests that hook_search_page runs.
1929
 */
1930
class SearchPageOverride extends DrupalWebTestCase {
1931
  public $search_user;
1932

    
1933
  public static function getInfo() {
1934
    return array(
1935
      'name' => 'Search page override',
1936
      'description' => 'Verify that hook_search_page can override search page display.',
1937
      'group' => 'Search',
1938
    );
1939
  }
1940

    
1941
  function setUp() {
1942
    parent::setUp('search', 'search_extra_type');
1943

    
1944
    // Login as a user that can create and search content.
1945
    $this->search_user = $this->drupalCreateUser(array('search content', 'administer search'));
1946
    $this->drupalLogin($this->search_user);
1947

    
1948
    // Enable the extra type module for searching.
1949
    variable_set('search_active_modules', array('node' => 'node', 'user' => 'user', 'search_extra_type' => 'search_extra_type'));
1950
    menu_rebuild();
1951
  }
1952

    
1953
  function testSearchPageHook() {
1954
    $keys = 'bike shed ' . $this->randomName();
1955
    $this->drupalGet("search/dummy_path/{$keys}");
1956
    $this->assertText('Dummy search snippet', 'Dummy search snippet is shown');
1957
    $this->assertText('Test page text is here', 'Page override is working');
1958
  }
1959
}
1960

    
1961
/**
1962
 * Test node search with multiple languages.
1963
 */
1964
class SearchLanguageTestCase extends DrupalWebTestCase {
1965
  public static function getInfo() {
1966
    return array(
1967
      'name' => 'Search language selection',
1968
      'description' => 'Tests advanced search with different languages enabled.',
1969
      'group' => 'Search',
1970
    );
1971
  }
1972

    
1973
  /**
1974
   * Implementation setUp().
1975
   */
1976
  function setUp() {
1977
    parent::setUp('search', 'locale');
1978

    
1979
    // Create and login user.
1980
    $test_user = $this->drupalCreateUser(array('access content', 'search content', 'use advanced search', 'administer nodes', 'administer languages', 'access administration pages'));
1981
    $this->drupalLogin($test_user);
1982
  }
1983

    
1984
  function testLanguages() {
1985
    // Check that there are initially no languages displayed.
1986
    $this->drupalGet('search/node');
1987
    $this->assertNoText(t('Languages'), 'No languages to choose from.');
1988

    
1989
    // Add predefined language.
1990
    $edit = array('langcode' => 'fr');
1991
    $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
1992
    $this->assertText('fr', 'Language added successfully.');
1993

    
1994
    // Now we should have languages displayed.
1995
    $this->drupalGet('search/node');
1996
    $this->assertText(t('Languages'), 'Languages displayed to choose from.');
1997
    $this->assertText(t('English'), 'English is a possible choice.');
1998
    $this->assertText(t('French'), 'French is a possible choice.');
1999

    
2000
    // Ensure selecting no language does not make the query different.
2001
    $this->drupalPost('search/node', array(), t('Advanced search'));
2002
    $this->assertEqual($this->getUrl(), url('search/node/', array('absolute' => TRUE)), 'Correct page redirection, no language filtering.');
2003

    
2004
    // Pick French and ensure it is selected.
2005
    $edit = array('language[fr]' => TRUE);
2006
    $this->drupalPost('search/node', $edit, t('Advanced search'));
2007
    $this->assertFieldByXPath('//input[@name="keys"]', 'language:fr', 'Language filter added to query.');
2008

    
2009
    // Change the default language and disable English.
2010
    $path = 'admin/config/regional/language';
2011
    $this->drupalGet($path);
2012
    $this->assertFieldChecked('edit-site-default-en', 'English is the default language.');
2013
    $edit = array('site_default' => 'fr');
2014
    $this->drupalPost(NULL, $edit, t('Save configuration'));
2015
    $this->assertNoFieldChecked('edit-site-default-en', 'Default language updated.');
2016
    $edit = array('enabled[en]' => FALSE);
2017
    $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
2018
    $this->assertNoFieldChecked('edit-enabled-en', 'Language disabled.');
2019

    
2020
    // Check that there are again no languages displayed.
2021
    $this->drupalGet('search/node');
2022
    $this->assertNoText(t('Languages'), 'No languages to choose from.');
2023
  }
2024
}
2025

    
2026
/**
2027
 * Tests node search with node access control.
2028
 */
2029
class SearchNodeAccessTest extends DrupalWebTestCase {
2030
  public $test_user;
2031

    
2032
  public static function getInfo() {
2033
    return array(
2034
      'name' => 'Search and node access',
2035
      'description' => 'Tests search functionality with node access control.',
2036
      'group' => 'Search',
2037
    );
2038
  }
2039

    
2040
  function setUp() {
2041
    parent::setUp('search', 'node_access_test');
2042
    node_access_rebuild();
2043

    
2044
    // Create a test user and log in.
2045
    $this->test_user = $this->drupalCreateUser(array('access content', 'search content', 'use advanced search'));
2046
    $this->drupalLogin($this->test_user);
2047
  }
2048

    
2049
  /**
2050
   * Tests that search works with punctuation and HTML entities.
2051
   */
2052
  function testPhraseSearchPunctuation() {
2053
    $node = $this->drupalCreateNode(array('body' => array(LANGUAGE_NONE => array(array('value' => "The bunny's ears were fuzzy.")))));
2054
    $node2 = $this->drupalCreateNode(array('body' => array(LANGUAGE_NONE => array(array('value' => 'Dignissim Aliquam &amp; Quieligo meus natu quae quia te. Damnum&copy; erat&mdash; neo pneum. Facilisi feugiat ibidem ratis.')))));
2055

    
2056
    // Update the search index.
2057
    module_invoke_all('update_index');
2058
    search_update_totals();
2059

    
2060
    // Refresh variables after the treatment.
2061
    $this->refreshVariables();
2062

    
2063
    // Submit a phrase wrapped in double quotes to include the punctuation.
2064
    $edit = array('keys' => '"bunny\'s"');
2065
    $this->drupalPost('search/node', $edit, t('Search'));
2066
    $this->assertText($node->title);
2067

    
2068
    // Search for "&" and verify entities are not broken up in the output.
2069
    $edit = array('keys' => '&');
2070
    $this->drupalPost('search/node', $edit, t('Search'));
2071
    $this->assertNoRaw('<strong>&</strong>amp;');
2072
    $this->assertText('You must include at least one positive keyword');
2073

    
2074
    $edit = array('keys' => '&amp;');
2075
    $this->drupalPost('search/node', $edit, t('Search'));
2076
    $this->assertNoRaw('<strong>&</strong>amp;');
2077
    $this->assertText('You must include at least one positive keyword');
2078
  }
2079
}
2080

    
2081
/**
2082
 * Tests node search with query tags.
2083
 */
2084
class SearchNodeTagTest extends DrupalWebTestCase {
2085
  public $test_user;
2086

    
2087
  public static function getInfo() {
2088
    return array(
2089
      'name' => 'Node search query tags',
2090
      'description' => 'Tests Node search tags functionality.',
2091
      'group' => 'Search',
2092
    );
2093
  }
2094

    
2095
  function setUp() {
2096
    parent::setUp('search', 'search_node_tags');
2097
    node_access_rebuild();
2098

    
2099
    // Create a test user and log in.
2100
    $this->test_user = $this->drupalCreateUser(array('search content'));
2101
    $this->drupalLogin($this->test_user);
2102
  }
2103

    
2104
  /**
2105
   * Tests that the correct tags are available and hooks invoked.
2106
   */
2107
  function testNodeSearchQueryTags() {
2108
    $this->drupalCreateNode(array('body' => array(LANGUAGE_NONE => array(array('value' => 'testing testing testing.')))));
2109

    
2110
    // Update the search index.
2111
    module_invoke_all('update_index');
2112
    search_update_totals();
2113

    
2114
    $edit = array('keys' => 'testing');
2115
    $this->drupalPost('search/node', $edit, t('Search'));
2116

    
2117
    $this->assertTrue(variable_get('search_node_tags_test_query_tag', FALSE), 'hook_query_alter() was invoked and the query contained the "search_node" tag.');
2118
    $this->assertTrue(variable_get('search_node_tags_test_query_tag_hook', FALSE), 'hook_query_search_node_alter() was invoked.');
2119
  }
2120
}
2121

    
2122
/**
2123
 * Tests searching with locale values set.
2124
 */
2125
class SearchSetLocaleTest extends DrupalWebTestCase {
2126

    
2127
  public static function getInfo() {
2128
    return array(
2129
      'name' => 'Search with numeric locale set',
2130
      'description' => 'Check that search works with numeric locale settings',
2131
      'group' => 'Search',
2132
    );
2133
  }
2134

    
2135
  function setUp() {
2136
    parent::setUp('search');
2137

    
2138
    // Create a simple node so something will be put in the index.
2139
    $info = array(
2140
      'body' => array(LANGUAGE_NONE => array(array('value' => 'Tapir'))),
2141
    );
2142
    $this->drupalCreateNode($info);
2143

    
2144
    // Run cron to index.
2145
    $this->cronRun();
2146
  }
2147

    
2148
  /**
2149
   * Verify that search works with a numeric locale set.
2150
   */
2151
  public function testSearchWithNumericLocale() {
2152
    // French decimal point is comma.
2153
    setlocale(LC_NUMERIC, 'fr_FR');
2154

    
2155
    // An exception will be thrown if a float in the wrong format occurs in the
2156
    // query to the database, so an assertion is not necessary here.
2157
    db_select('search_index', 'i')
2158
      ->extend('searchquery')
2159
      ->searchexpression('tapir', 'node')
2160
      ->execute();
2161
  }
2162
}