Project

General

Profile

Paste
Download (5.74 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / tests / handlers / views_handler_sort_date.test @ 4003efde

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of ViewsHandlerSortDateTest.
6
 */
7

    
8
/**
9
 * Tests for core views_handler_sort_date handler.
10
 */
11
class ViewsHandlerSortDateTest extends ViewsSqlTest {
12
  public static function getInfo() {
13
    return array(
14
      'name' => 'Sort: date',
15
      'description' => 'Test the core views_handler_sort_date handler.',
16
      'group' => 'Views Handlers',
17
    );
18
  }
19

    
20
  protected function expectedResultSet($granularity, $reverse = TRUE) {
21
    $expected = array();
22
    if (!$reverse) {
23
      switch ($granularity) {
24
        case 'second':
25
          $expected = array(
26
            array('name' => 'John'),
27
            array('name' => 'Paul'),
28
            array('name' => 'Meredith'),
29
            array('name' => 'Ringo'),
30
            array('name' => 'George'),
31
          );
32
          break;
33

    
34
        case 'minute':
35
          $expected = array(
36
            array('name' => 'John'),
37
            array('name' => 'Paul'),
38
            array('name' => 'Ringo'),
39
            array('name' => 'Meredith'),
40
            array('name' => 'George'),
41
          );
42
          break;
43

    
44
        case 'hour':
45
          $expected = array(
46
            array('name' => 'John'),
47
            array('name' => 'Ringo'),
48
            array('name' => 'Paul'),
49
            array('name' => 'Meredith'),
50
            array('name' => 'George'),
51
          );
52
          break;
53

    
54
        case 'day':
55
          $expected = array(
56
            array('name' => 'John'),
57
            array('name' => 'Ringo'),
58
            array('name' => 'Paul'),
59
            array('name' => 'Meredith'),
60
            array('name' => 'George'),
61
          );
62
          break;
63

    
64
        case 'month':
65
          $expected = array(
66
            array('name' => 'John'),
67
            array('name' => 'George'),
68
            array('name' => 'Ringo'),
69
            array('name' => 'Paul'),
70
            array('name' => 'Meredith'),
71
          );
72
          break;
73

    
74
        case 'year':
75
          $expected = array(
76
            array('name' => 'John'),
77
            array('name' => 'George'),
78
            array('name' => 'Ringo'),
79
            array('name' => 'Paul'),
80
            array('name' => 'Meredith'),
81
          );
82
          break;
83
      }
84
    }
85
    else {
86
      switch ($granularity) {
87
        case 'second':
88
          $expected = array(
89
            array('name' => 'George'),
90
            array('name' => 'Ringo'),
91
            array('name' => 'Meredith'),
92
            array('name' => 'Paul'),
93
            array('name' => 'John'),
94
          );
95
          break;
96

    
97
        case 'minute':
98
          $expected = array(
99
            array('name' => 'George'),
100
            array('name' => 'Ringo'),
101
            array('name' => 'Meredith'),
102
            array('name' => 'Paul'),
103
            array('name' => 'John'),
104
          );
105
          break;
106

    
107
        case 'hour':
108
          $expected = array(
109
            array('name' => 'George'),
110
            array('name' => 'Ringo'),
111
            array('name' => 'Paul'),
112
            array('name' => 'Meredith'),
113
            array('name' => 'John'),
114
          );
115
          break;
116

    
117
        case 'day':
118
          $expected = array(
119
            array('name' => 'George'),
120
            array('name' => 'John'),
121
            array('name' => 'Ringo'),
122
            array('name' => 'Paul'),
123
            array('name' => 'Meredith'),
124
          );
125
          break;
126

    
127
        case 'month':
128
          $expected = array(
129
            array('name' => 'John'),
130
            array('name' => 'George'),
131
            array('name' => 'Ringo'),
132
            array('name' => 'Paul'),
133
            array('name' => 'Meredith'),
134
          );
135
          break;
136

    
137
        case 'year':
138
          $expected = array(
139
            array('name' => 'John'),
140
            array('name' => 'George'),
141
            array('name' => 'Ringo'),
142
            array('name' => 'Paul'),
143
            array('name' => 'Meredith'),
144
          );
145
          break;
146
      }
147
    }
148

    
149
    return $expected;
150
  }
151

    
152
  /**
153
   * Tests numeric ordering of the result set.
154
   */
155
  public function testDateOrdering() {
156
    foreach (array('second', 'minute', 'hour', 'day', 'month', 'year') as $granularity) {
157
      foreach (array(FALSE, TRUE) as $reverse) {
158
        $view = $this->getBasicView();
159

    
160
        // Change the fields.
161
        $view->display['default']->handler->override_option('fields', array(
162
          'name' => array(
163
            'id' => 'name',
164
            'table' => 'views_test',
165
            'field' => 'name',
166
            'relationship' => 'none',
167
          ),
168
          'created' => array(
169
            'id' => 'created',
170
            'table' => 'views_test',
171
            'field' => 'created',
172
            'relationship' => 'none',
173
          ),
174
        ));
175

    
176
        // Change the ordering.
177
        $view->display['default']->handler->override_option('sorts', array(
178
          'created' => array(
179
            'id' => 'created',
180
            'table' => 'views_test',
181
            'field' => 'created',
182
            'relationship' => 'none',
183
            'granularity' => $granularity,
184
            'order' => $reverse ? 'DESC' : 'ASC',
185
          ),
186
          'id' => array(
187
            'id' => 'id',
188
            'table' => 'views_test',
189
            'field' => 'id',
190
            'relationship' => 'none',
191
            'order' => 'ASC',
192
          ),
193
        ));
194

    
195
        // Execute the view.
196
        $this->executeView($view);
197

    
198
        // Verify the result.
199
        $this->assertEqual(count($this->dataSet()), count($view->result), t('The number of returned rows match.'));
200
        $this->assertIdenticalResultset($view, $this->expectedResultSet($granularity, $reverse), array(
201
          'views_test_name' => 'name',
202
        ), t('Result is returned correctly when ordering by granularity @granularity, @reverse.', array('@granularity' => $granularity, '@reverse' => $reverse ? t('reverse') : t('forward'))));
203
        $view->destroy();
204
        unset($view);
205
      }
206
    }
207
  }
208

    
209
}