summaryrefslogtreecommitdiff
path: root/kolab.org/www/drupal-7.15/sites/all/modules/views/tests/handlers/views_handler_filter_date.test
blob: 34ccba6676bcd232bd45c4768658f5f61cb93d0b (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
<?php

/**
 * @file
 * Definition of ViewsHandlerFilterDateTest.
 */

/**
 * Tests the core views_handler_filter_date handler.
 */
class ViewsHandlerFilterDateTest extends ViewsSqlTest {
  public static function getInfo() {
    return array(
      'name' => 'Filter: Date',
      'description' => 'Test the core views_handler_filter_date handler.',
      'group' => 'Views Handlers',
    );
  }

  function setUp() {
    parent::setUp();
    // Add some basic test nodes.
    $this->nodes = array();
    $this->nodes[] = $this->drupalCreateNode(array('created' => 100000));
    $this->nodes[] = $this->drupalCreateNode(array('created' => 200000));
    $this->nodes[] = $this->drupalCreateNode(array('created' => 300000));
    $this->nodes[] = $this->drupalCreateNode(array('created' => time() + 86400));

    $this->map = array(
      'nid' => 'nid',
    );
    $this->enableViewsUi();
  }

  /**
   * Test the general offset functionality.
   */
  function testOffset() {
    $view = $this->views_test_offset();
    // Test offset for simple operator.
    $view->set_display('default');
    $view->init_handlers();
    $view->filter['created']->operator = '>';
    $view->filter['created']->value['type'] = 'offset';
    $view->filter['created']->value['value'] = '+1 hour';
    $view->execute_display('default');
    $expected_result = array(
      array('nid' => $this->nodes[3]->nid),
    );
    $this->assertIdenticalResultset($view, $expected_result, $this->map);
    $view->destroy();

    // Test offset for between operator.
    $view->set_display('default');
    $view->init_handlers();
    $view->filter['created']->operator = 'between';
    $view->filter['created']->value['type'] = 'offset';
    $view->filter['created']->value['max'] = '+2 days';
    $view->filter['created']->value['min'] = '+1 hour';
    $view->execute_display('default');
    $expected_result = array(
      array('nid' => $this->nodes[3]->nid),
    );
    $this->assertIdenticalResultset($view, $expected_result, $this->map);
    $view->destroy();
  }


  /**
   * Tests the filter operator between/not between.
   */
  function testBetween() {
    // Test between with min and max.
    $view = $this->views_test_between();
    $view->set_display('default');
    $view->init_handlers();
    $view->filter['created']->operator = 'between';
    $view->filter['created']->value['min'] = format_date(150000, 'custom', 'Y-m-d H:s');
    $view->filter['created']->value['max'] = format_date(250000, 'custom', 'Y-m-d H:s');
    $view->execute_display('default');
    $expected_result = array(
      array('nid' => $this->nodes[1]->nid),
    );
    $this->assertIdenticalResultset($view, $expected_result, $this->map);
    $view->destroy();

    // Test between with just max.
    $view = $this->views_test_between();
    $view->set_display('default');
    $view->init_handlers();
    $view->filter['created']->operator = 'between';
    $view->filter['created']->value['max'] = format_date(250000, 'custom', 'Y-m-d H:s');
    $view->execute_display('default');
    $expected_result = array(
      array('nid' => $this->nodes[0]->nid),
      array('nid' => $this->nodes[1]->nid),
    );
    $this->assertIdenticalResultset($view, $expected_result, $this->map);
    $view->destroy();

    // Test not between with min and max.
    $view = $this->views_test_between();
    $view->set_display('default');
    $view->init_handlers();
    $view->filter['created']->operator = 'not between';
    $view->filter['created']->value['min'] = format_date(150000, 'custom', 'Y-m-d H:s');
    $view->filter['created']->value['max'] = format_date(250000, 'custom', 'Y-m-d H:s');
    $view->execute_display('default');
    $expected_result = array(
      array('nid' => $this->nodes[0]->nid),
      array('nid' => $this->nodes[2]->nid),
      array('nid' => $this->nodes[3]->nid),
    );
    $this->assertIdenticalResultset($view, $expected_result, $this->map);
    $view->destroy();

    // Test not between with just max.
    $view = $this->views_test_between();
    $view->set_display('default');
    $view->init_handlers();
    $view->filter['created']->operator = 'not between';
    $view->filter['created']->value['max'] = format_date(150000, 'custom', 'Y-m-d H:s');
    $view->execute_display('default');
    $expected_result = array(
      array('nid' => $this->nodes[1]->nid),
      array('nid' => $this->nodes[2]->nid),
      array('nid' => $this->nodes[3]->nid),
    );
    $this->assertIdenticalResultset($view, $expected_result, $this->map);
    $view->destroy();
  }

  /**
   * Make sure the validation callbacks works.
   */
  function testUiValidation() {
    $view = $this->views_test_between();
    $view->save();

    $admin_user =   $this->drupalCreateUser(array('administer views', 'administer site configuration'));
    $this->drupalLogin($admin_user);
    menu_rebuild();
    $this->drupalGet('admin/structure/views/view/test_filter_date_between/edit');
    $this->drupalGet('admin/structure/views/nojs/config-item/test_filter_date_between/default/filter/created');

    $edit = array();
    // Generate a definitive wrong value, which should be checked by validation.
    $edit['options[value][value]'] = $this->randomString() . '-------';
    $this->drupalPost(NULL, $edit, t('Apply'));
    $this->assertText(t('Invalid date format.'), 'Make sure that validation is runned and the invalidate date format is identified.');
  }

  function views_test_between() {
    $view = new view;
    $view->name = 'test_filter_date_between';
    $view->description = '';
    $view->tag = '';
    $view->base_table = 'node';
    $view->human_name = '';
    $view->core = 0;
    $view->api_version = '3.0';
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

    /* Display: Master */
    $handler = $view->new_display('default', 'Master', 'default');
    $handler->display->display_options['access']['type'] = 'none';
    $handler->display->display_options['cache']['type'] = 'none';
    $handler->display->display_options['query']['type'] = 'views_query';
    $handler->display->display_options['query']['options']['query_comment'] = FALSE;
    $handler->display->display_options['exposed_form']['type'] = 'basic';
    $handler->display->display_options['pager']['type'] = 'full';
    $handler->display->display_options['style_plugin'] = 'default';
    $handler->display->display_options['row_plugin'] = 'fields';
    /* Field: Content: Nid */
    $handler->display->display_options['fields']['nid']['id'] = 'nid';
    $handler->display->display_options['fields']['nid']['table'] = 'node';
    $handler->display->display_options['fields']['nid']['field'] = 'nid';
    /* Filter criterion: Content: Post date */
    $handler->display->display_options['filters']['created']['id'] = 'created';
    $handler->display->display_options['filters']['created']['table'] = 'node';
    $handler->display->display_options['filters']['created']['field'] = 'created';

    return $view;
  }

  function views_test_offset() {
    $view = $this->views_test_between();
    return $view;
  }
}