SearchNumbersTest.php 3.08 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\search\Tests\SearchNumbersTest.
6 7 8 9
 */

namespace Drupal\search\Tests;

10 11
use Drupal\Core\Language\Language;

12
/**
13
 * Tests that numbers with different formats can be searched.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 */
class SearchNumbersTest extends SearchTestBase {
  protected $test_user;
  protected $numbers;
  protected $nodes;

  public static function getInfo() {
    return array(
      'name' => 'Search numbers',
      'description' => 'Check that numbers can be searched',
      'group' => 'Search',
    );
  }

  function setUp() {
    parent::setUp();

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

    // Create content with various numbers in it.
    // Note: 50 characters is the current limit of the search index's word
    // field.
    $this->numbers = array(
      'ISBN' => '978-0446365383',
      'UPC' => '036000 291452',
      'EAN bar code' => '5901234123457',
      'negative' => '-123456.7890',
      'quoted negative' => '"-123456.7890"',
      'leading zero' => '0777777777',
      'tiny' => '111',
      'small' => '22222222222222',
      'medium' => '333333333333333333333333333',
      'large' => '444444444444444444444444444444444444444',
      'gigantic' => '5555555555555555555555555555555555555555555555555',
      'over fifty characters' => '666666666666666666666666666666666666666666666666666666666666',
50 51
      'date' => '01/02/2009',
      'commas' => '987,654,321',
52 53 54 55
    );

    foreach ($this->numbers as $doc => $num) {
      $info = array(
56
        'body' => array(array('value' => $num)),
57
        'type' => 'page',
58
        'language' => Language::LANGCODE_NOT_SPECIFIED,
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        'title' => $doc . ' number',
      );
      $this->nodes[$doc] = $this->drupalCreateNode($info);
    }

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

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

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

      // Verify that the node title does not appear on the search page
      // with a dummy search.
85
      $this->drupalPostForm('search/node',
86 87
        array('keys' => 'foo'),
        t('Search'));
88
      $this->assertNoText($node->label(), $type . ': node title not shown in dummy search');
89 90 91

      // Verify that the node title does appear as a link on the search page
      // when searching for the number.
92
      $this->drupalPostForm('search/node',
93 94
        array('keys' => $number),
        t('Search'));
95
      $this->assertText($node->label(), format_string('%type: node title shown (search found the node) in search for number %number.', array('%type' => $type, '%number' => $number)));
96 97 98
    }
  }
}