summaryrefslogtreecommitdiffstats
path: root/views_natural_sort.test
blob: 9835d069aefd1cd8c027032d28b5ef015c011c03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
50
51
52
53
54
55
56
57
58
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php

/**
 * @file
 * Tests for the Views Natural Sort module.
 */

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

/**
 * Basic tests for the Views Natural Sort Module.
 */
class ViewsNaturalSortBasicTest extends ViewsTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Views Natural Sort Basic Test',
      'description' => 'Tests basic functionality of View Natural Sort',
      'group' => 'Views Natural Sort',
    );
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp('views', 'views_natural_sort', 'views_natural_sort_test');
  }

  /**
   * Test the default settings for stripping out the words A, An, and The.
   */
  public function testNaturalSortDefaultBeginningWords() {
    $titles = array(
      'A Stripped Zebra',
      'Oklahoma',
      'The King And I',
    );
    $nodes = array();
    foreach ($titles as $title) {
      $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')
    );
  }

  /**
   * Test the default settings for symbol removal in sorting.
   */
  public function testNaturalSortDefaultSymbols() {
    $titles = array(
      'A(Z',
      'A[B',
      'A\\C',
    );
    foreach ($titles as $title) {
      $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')
    );
  }

  /**
   * Test sorting strings that contain numbers in them.
   */
  public function testNaturalSortNumbers() {
    $titles = array(
      '1 apple',
      '2 apples',
      '10 apples',
      '-1 apples',
      '-10 apples',
      '-2 apples',
      '-3.550 apples',
      '-3.5501 apples',
      '3.5501 apples',
      '3.550 apples',
    );
    foreach ($titles as $title) {
      $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')
    );
  }

}
/**
 * Tests all the "Days of the week" natural sort functionality.
 */
class ViewsNaturalSortDaysOfTheWeekTest extends DrupalWebTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Views Natural Sort Days of the Week',
      'description' => 'Tests Days of the Week Sorting',
      'group' => 'Views Natural Sort',
    );
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp('views', 'views_natural_sort');
    variable_set('views_natural_sort_days_of_the_week_enabled', TRUE);
  }

  /**
   * Test tranformation functionality of the days of the week transformation.
   */
  public function testDefaultDayReplace() {
    foreach (views_natural_sort_days_of_the_week_get_default_days() as $replaced => $day) {
      $this->assertEqual(views_natural_sort_days_of_the_week_sort_days($day), $replaced);
    }
  }

  /**
   * Test changing the first day of the week.
   */
  public function testFirstDayMonday() {
    variable_set("views_natural_sort_days_of_the_week_first_day_en", "Monday");
    $expected = array(
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday",
      "Sunday",
    );
    foreach ($expected as $replaced => $day) {
      $this->assertEqual(views_natural_sort_days_of_the_week_sort_days($day), $replaced);
    }
  }

  /**
   * Test sorting day abbreviations.
   */
  public function testAbbreviations() {
    $expected = array(
      "Sun",
      "Mon.",
      "Tues",
      "Wed",
      "Thur",
      "Fri.",
      "Sat",
    );
    foreach ($expected as $replaced => $day) {
      $this->assertEqual(views_natural_sort_days_of_the_week_sort_days($day), $replaced);
    }
  }

}