views_natural_sort.test 5.37 KB
Newer Older
1
<?php
2 3 4

/**
 * @file
5
 * Tests for the Views Natural Sort module.
6 7 8
 */

module_load_include('test', 'views', 'tests/views_query');
9

10
/**
11
 * Basic tests for the Views Natural Sort Module.
12
 */
13 14
class ViewsNaturalSortBasicTest extends ViewsTestCase {

15
  /**
16
   * {@inheritdoc}
17
   */
18 19 20 21 22 23 24 25
  public static function getInfo() {
    return array(
      'name' => 'Views Natural Sort Basic Test',
      'description' => 'Tests basic functionality of View Natural Sort',
      'group' => 'Views Natural Sort',
    );
  }

26
  /**
27
   * {@inheritdoc}
28
   */
29 30 31 32
  protected function setUp() {
    parent::setUp('views', 'views_natural_sort', 'views_natural_sort_test');
  }

33
  /**
34
   * Test the default settings for stripping out the words A, An, and The.
35
   */
36 37 38 39 40 41 42
  public function testNaturalSortDefaultBeginningWords() {
    $titles = array(
      'A Stripped Zebra',
      'Oklahoma',
      'The King And I',
    );
    $nodes = array();
43
    foreach ($titles as $title) {
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      $nodes[] = $this->drupalCreateNode(array(
        'type' => 'views_natural_sort_test_content',
        'title' => $title,
      ));
    }
    $view = views_get_view('views_natural_sort_test');
    $this->executeView($view);
    $this->assertIdenticalResultset(
      $view,
      array(
        array('title' => 'The King And I'),
        array('title' => 'Oklahoma'),
        array('title' => 'A Stripped Zebra'),
      ),
      array('node_title' => 'title')
    );
  }

62
  /**
63
   * Test the default settings for symbol removal in sorting.
64
   */
65 66 67 68 69 70
  public function testNaturalSortDefaultSymbols() {
    $titles = array(
      'A(Z',
      'A[B',
      'A\\C',
    );
71
    foreach ($titles as $title) {
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
      $nodes[] = $this->drupalCreateNode(array(
        'type' => 'views_natural_sort_test_content',
        'title' => $title,
      ));
    }
    $view = views_get_view('views_natural_sort_test');
    $this->executeView($view);
    $this->assertIdenticalResultset(
      $view,
      array(
        array('title' => 'A[B'),
        array('title' => 'A\\C'),
        array('title' => 'A(Z'),
      ),
      array('node_title' => 'title')
    );
  }

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  /**
   * Test Unicode symbol removal in sorting.
   */
  public function testNaturalSortUnicodeSymbols() {
    $symbols = variable_set('views_natural_sort_symbols_remove', "#…\",'\\()[]«?!»¡¿");
    $titles = array(
      'Cuando… se abre, ¿dará algún tipo de señal?',
    );
    $expected = array(
      'Cuando se abre dará algún tipo de señal',
    );
    foreach ($titles as $key => $title) {
      $this->assertEqual(views_natural_sort_remove_symbols($title), $expected[$key]);
    }
  }

106
  /**
107
   * Test sorting strings that contain numbers in them.
108
   */
109 110 111 112 113 114 115
  public function testNaturalSortNumbers() {
    $titles = array(
      '1 apple',
      '2 apples',
      '10 apples',
      '-1 apples',
      '-10 apples',
116
      '-2 apples',
117 118 119 120 121
      '-3.550 apples',
      '-3.5501 apples',
      '3.5501 apples',
      '3.550 apples',
    );
122
    foreach ($titles as $title) {
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
      $nodes[] = $this->drupalCreateNode(array(
        'type' => 'views_natural_sort_test_content',
        'title' => $title,
      ));
    }
    $view = views_get_view('views_natural_sort_test');
    $this->executeView($view);
    $this->assertIdenticalResultset(
      $view,
      array(
        array('title' => '-10 apples'),
        array('title' => '-3.5501 apples'),
        array('title' => '-3.550 apples'),
        array('title' => '-2 apples'),
        array('title' => '-1 apples'),
        array('title' => '1 apple'),
        array('title' => '2 apples'),
        array('title' => '3.550 apples'),
        array('title' => '3.5501 apples'),
        array('title' => '10 apples'),
      ),
      array('node_title' => 'title')
    );
  }
147

148 149
}
/**
150
 * Tests all the "Days of the week" natural sort functionality.
151
 */
152
class ViewsNaturalSortDaysOfTheWeekTest extends DrupalWebTestCase {
153

154
  /**
155
   * {@inheritdoc}
156
   */
157 158
  public static function getInfo() {
    return array(
159 160 161
      'name' => 'Views Natural Sort Days of the Week',
      'description' => 'Tests Days of the Week Sorting',
      'group' => 'Views Natural Sort',
162 163 164
    );
  }

165
  /**
166
   * {@inheritdoc}
167
   */
168 169
  protected function setUp() {
    parent::setUp('views', 'views_natural_sort');
170
    variable_set('views_natural_sort_days_of_the_week_enabled', TRUE);
171 172
  }

173
  /**
174
   * Test tranformation functionality of the days of the week transformation.
175 176 177
   */
  public function testDefaultDayReplace() {
    foreach (views_natural_sort_days_of_the_week_get_default_days() as $replaced => $day) {
178 179
      $this->assertEqual(views_natural_sort_days_of_the_week_sort_days($day), $replaced);
    }
180 181
  }

182
  /**
183
   * Test changing the first day of the week.
184 185
   */
  public function testFirstDayMonday() {
186 187 188 189 190 191 192 193 194 195
    variable_set("views_natural_sort_days_of_the_week_first_day_en", "Monday");
    $expected = array(
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday",
      "Sunday",
    );
196
    foreach ($expected as $replaced => $day) {
197 198 199
      $this->assertEqual(views_natural_sort_days_of_the_week_sort_days($day), $replaced);
    }
  }
200 201

  /**
202
   * Test sorting day abbreviations.
203 204
   */
  public function testAbbreviations() {
205 206 207 208 209 210 211 212 213
    $expected = array(
      "Sun",
      "Mon.",
      "Tues",
      "Wed",
      "Thur",
      "Fri.",
      "Sat",
    );
214
    foreach ($expected as $replaced => $day) {
215 216
      $this->assertEqual(views_natural_sort_days_of_the_week_sort_days($day), $replaced);
    }
217
  }
218

219
}