Project

General

Profile

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

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

1
<?php
2

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

    
8
/**
9
 * Tests the numeric filter handler.
10
 */
11
class ViewsHandlerFilterNumericTest extends ViewsSqlTest {
12
  var $column_map = array();
13

    
14
  public static function getInfo() {
15
    return array(
16
      'name' => 'Filter: Numeric',
17
      'description' => 'Tests the numeric filter handler',
18
      'group' => 'Views Handlers',
19
    );
20
  }
21

    
22
  /**
23
   * {@inheritdoc}
24
   */
25
  protected function dataSet() {
26
    $data_set = parent::dataSet();
27
    $data_set[] = array(
28
      'name' => 'Charles',
29
      'age' => NULL,
30
      'job' => 'Bassist',
31
        'created' => gmmktime(6, 30, 10, 1, 1, 2001),
32
    );
33
    return $data_set;
34
  }
35

    
36
  /**
37
   * {@inheritdoc}
38
   */
39
  protected function schemaDefinition() {
40
    $schema = parent::schemaDefinition();
41
    $schema['views_test']['fields']['age']['not null'] = FALSE;
42
    $schema['views_test']['indexes'] = array();
43
    return $schema;
44
  }
45

    
46
  /**
47
   * {@inheritdoc}
48
   */
49
  public function setUp(array $modules = array()) {
50
    parent::setUp($modules);
51

    
52
    $this->column_map = array(
53
      'views_test_name' => 'name',
54
      'views_test_age' => 'age',
55
    );
56
  }
57

    
58
  function viewsData() {
59
    $data = parent::viewsData();
60
    $data['views_test']['age']['filter']['allow empty'] = TRUE;
61
    $data['views_test']['id']['filter']['allow empty'] = FALSE;
62

    
63
    return $data;
64
  }
65

    
66
  public function testFilterNumericSimple() {
67
    $view = $this->getBasicView();
68

    
69
    // Change the filtering.
70
    $view->display['default']->handler->override_option('filters', array(
71
      'age' => array(
72
        'id' => 'age',
73
        'table' => 'views_test',
74
        'field' => 'age',
75
        'relationship' => 'none',
76
        'operator' => '=',
77
        'value' => array('value' => 28),
78
      ),
79
    ));
80

    
81
    $this->executeView($view);
82
    $resultset = array(
83
      array(
84
        'name' => 'Ringo',
85
        'age' => 28,
86
      ),
87
    );
88
    $this->assertIdenticalResultset($view, $resultset, $this->column_map);
89
  }
90

    
91
  public function testFilterNumericExposedGroupedSimple() {
92
    $filters = $this->getGroupedExposedFilters();
93
    $view = $this->getBasicPageView();
94

    
95
    // Filter: Age, Operator: =, Value: 28
96
    $filters['age']['group_info']['default_group'] = 1;
97
    $view->set_display('page_1');
98
    $view->display['page_1']->handler->override_option('filters', $filters);
99

    
100
    $this->executeView($view);
101
    $resultset = array(
102
      array(
103
        'name' => 'Ringo',
104
        'age' => 28,
105
      ),
106
    );
107
    $this->assertIdenticalResultset($view, $resultset, $this->column_map);
108
  }
109

    
110
  public function testFilterNumericBetween() {
111
    $view = $this->getBasicView();
112

    
113
    // Change the filtering.
114
    $view->display['default']->handler->override_option('filters', array(
115
      'age' => array(
116
        'id' => 'age',
117
        'table' => 'views_test',
118
        'field' => 'age',
119
        'relationship' => 'none',
120
        'operator' => 'between',
121
        'value' => array(
122
          'min' => 26,
123
          'max' => 29,
124
        ),
125
      ),
126
    ));
127

    
128
    $this->executeView($view);
129
    $resultset = array(
130
      array(
131
        'name' => 'George',
132
        'age' => 27,
133
      ),
134
      array(
135
        'name' => 'Ringo',
136
        'age' => 28,
137
      ),
138
      array(
139
        'name' => 'Paul',
140
        'age' => 26,
141
      ),
142
    );
143
    $this->assertIdenticalResultset($view, $resultset, $this->column_map);
144

    
145
    // Test not between.
146
    $view->delete();
147
    $view = $this->getBasicView();
148

    
149
    // Change the filtering.
150
    $view->display['default']->handler->override_option('filters', array(
151
      'age' => array(
152
        'id' => 'age',
153
        'table' => 'views_test',
154
        'field' => 'age',
155
        'relationship' => 'none',
156
        'operator' => 'not between',
157
        'value' => array(
158
          'min' => 26,
159
          'max' => 29,
160
        ),
161
      ),
162
    ));
163

    
164
    $this->executeView($view);
165
    $resultset = array(
166
      array(
167
        'name' => 'John',
168
        'age' => 25,
169
      ),
170
      array(
171
        'name' => 'Paul',
172
        'age' => 26,
173
      ),
174
      array(
175
        'name' => 'Meredith',
176
        'age' => 30,
177
      ),
178
    );
179
    $this->assertIdenticalResultset($view, $resultset, $this->column_map);
180
  }
181

    
182
  public function testFilterNumericExposedGroupedBetween() {
183
    $filters = $this->getGroupedExposedFilters();
184
    $view = $this->getBasicPageView();
185

    
186
    // Filter: Age, Operator: between, Value: 26 and 29
187
    $filters['age']['group_info']['default_group'] = 2;
188
    $view->set_display('page_1');
189
    $view->display['page_1']->handler->override_option('filters', $filters);
190

    
191
    $this->executeView($view);
192
    $resultset = array(
193
      array(
194
        'name' => 'George',
195
        'age' => 27,
196
      ),
197
      array(
198
        'name' => 'Ringo',
199
        'age' => 28,
200
      ),
201
      array(
202
        'name' => 'Paul',
203
        'age' => 26,
204
      ),
205
    );
206
    $this->assertIdenticalResultset($view, $resultset, $this->column_map);
207
  }
208

    
209
  public function testFilterNumericExposedGroupedNotBetween() {
210
    $filters = $this->getGroupedExposedFilters();
211
    $view = $this->getBasicPageView();
212

    
213
    // Filter: Age, Operator: not between, Value: 26 and 29
214
    $filters['age']['group_info']['default_group'] = 3;
215
    $view->set_display('page_1');
216
    $view->display['page_1']->handler->override_option('filters', $filters);
217

    
218
    $this->executeView($view);
219
    $resultset = array(
220
      array(
221
        'name' => 'John',
222
        'age' => 25,
223
      ),
224
      array(
225
        'name' => 'Paul',
226
        'age' => 26,
227
      ),
228
      array(
229
        'name' => 'Meredith',
230
        'age' => 30,
231
      ),
232
    );
233
    $this->assertIdenticalResultset($view, $resultset, $this->column_map);
234
  }
235

    
236

    
237
  public function testFilterNumericEmpty() {
238
    $view = $this->getBasicView();
239

    
240
    // Change the filtering.
241
    $view->display['default']->handler->override_option('filters', array(
242
      'age' => array(
243
        'id' => 'age',
244
        'table' => 'views_test',
245
        'field' => 'age',
246
        'relationship' => 'none',
247
        'operator' => 'empty',
248
      ),
249
    ));
250

    
251
    $this->executeView($view);
252
    $resultset = array(
253
      array(
254
        'name' => 'Charles',
255
        'age' => NULL,
256
      ),
257
    );
258
    $this->assertIdenticalResultset($view, $resultset, $this->column_map);
259

    
260
    $view->delete();
261
    $view = $this->getBasicView();
262

    
263
    // Change the filtering.
264
    $view->display['default']->handler->override_option('filters', array(
265
      'age' => array(
266
        'id' => 'age',
267
        'table' => 'views_test',
268
        'field' => 'age',
269
        'relationship' => 'none',
270
        'operator' => 'not empty',
271
      ),
272
    ));
273

    
274
    $this->executeView($view);
275
    $resultset = array(
276
      array(
277
        'name' => 'John',
278
        'age' => 25,
279
      ),
280
      array(
281
        'name' => 'George',
282
        'age' => 27,
283
      ),
284
      array(
285
        'name' => 'Ringo',
286
        'age' => 28,
287
      ),
288
      array(
289
        'name' => 'Paul',
290
        'age' => 26,
291
      ),
292
      array(
293
        'name' => 'Meredith',
294
        'age' => 30,
295
      ),
296
    );
297
    $this->assertIdenticalResultset($view, $resultset, $this->column_map);
298
  }
299

    
300

    
301
  public function testFilterNumericExposedGroupedEmpty() {
302
    $filters = $this->getGroupedExposedFilters();
303
    $view = $this->getBasicPageView();
304

    
305
    // Filter: Age, Operator: empty, Value.
306
    $filters['age']['group_info']['default_group'] = 4;
307
    $view->set_display('page_1');
308
    $view->display['page_1']->handler->override_option('filters', $filters);
309

    
310
    $this->executeView($view);
311
    $resultset = array(
312
      array(
313
        'name' => 'Charles',
314
        'age' => NULL,
315
      ),
316
    );
317
    $this->assertIdenticalResultset($view, $resultset, $this->column_map);
318
  }
319

    
320
  public function testFilterNumericExposedGroupedNotEmpty() {
321
    $filters = $this->getGroupedExposedFilters();
322
    $view = $this->getBasicPageView();
323

    
324
    // Filter: Age, Operator: empty, Value.
325
    $filters['age']['group_info']['default_group'] = 5;
326
    $view->set_display('page_1');
327
    $view->display['page_1']->handler->override_option('filters', $filters);
328

    
329
    $this->executeView($view);
330
    $resultset = array(
331
      array(
332
        'name' => 'John',
333
        'age' => 25,
334
      ),
335
      array(
336
        'name' => 'George',
337
        'age' => 27,
338
      ),
339
      array(
340
        'name' => 'Ringo',
341
        'age' => 28,
342
      ),
343
      array(
344
        'name' => 'Paul',
345
        'age' => 26,
346
      ),
347
      array(
348
        'name' => 'Meredith',
349
        'age' => 30,
350
      ),
351
    );
352
    $this->assertIdenticalResultset($view, $resultset, $this->column_map);
353
  }
354

    
355
  /**
356
   * Tests the limit operators functionality.
357
   */
358
  public function testFilterNumericExposedLimitOperators() {
359
    $filters = $this->getGroupedExposedFilters();
360
    $view = $this->getBasicView();
361

    
362
    $available_operators = array('<', '>', 'between');
363

    
364
    $filters['age']['expose'] += array(
365
      'limit_operators' => TRUE,
366
      'available_operators' => drupal_map_assoc($available_operators),
367
    );
368

    
369
    $view->display['default']->handler->override_option('filters', $filters);
370

    
371

    
372
    $this->executeView($view);
373

    
374
    $form = array();
375
    $form_state = array();
376
    $view->filter['age']->operator_form($form, $form_state);
377

    
378
    $operator = $form['operator'];
379

    
380
    $this->assertTrue(in_array($operator['#default_value'], $available_operators), 'Default value operator found in list of available operators.');
381

    
382
    foreach ($available_operators as $available_operator) {
383
      $this->assertTrue($operator['#options'][$available_operator], format_string('@operator found in options', array('@operator' => $available_operator)));
384
    }
385
  }
386

    
387
  /**
388
   * Tests exposed numeric filter with exposed operator.
389
   */
390
  public function testFilterNumericExposedOperator() {
391
    $this->applyFilterNumericExposedOperator('=', array('value' => '27'), array(
392
      array(
393
        'name' => 'George',
394
        'age' => 27,
395
      ),
396
    ));
397
    $this->applyFilterNumericExposedOperator('<', array('value' => '27'), array(
398
      array(
399
        'name' => 'John',
400
        'age' => 25,
401
      ),
402
      array(
403
        'name' => 'Paul',
404
        'age' => 26,
405
      ),
406
    ));
407
    $this->applyFilterNumericExposedOperator('<=', array(
408
      'value' => '27',
409
    ), array(
410
      array(
411
        'name' => 'John',
412
        'age' => 25,
413
      ),
414
      array(
415
        'name' => 'George',
416
        'age' => 27,
417
      ),
418
      array(
419
        'name' => 'Paul',
420
        'age' => 26,
421
      ),
422
    ));
423
    $this->applyFilterNumericExposedOperator('!=', array(
424
      'value' => '27',
425
    ), array(
426
      array(
427
        'name' => 'John',
428
        'age' => 25,
429
      ),
430
      array(
431
        'name' => 'Ringo',
432
        'age' => 28,
433
      ),
434
      array(
435
        'name' => 'Paul',
436
        'age' => 26,
437
      ),
438
      array(
439
        'name' => 'Meredith',
440
        'age' => 30,
441
      ),
442
    ));
443
    $this->applyFilterNumericExposedOperator('>=', array(
444
      'value' => '27',
445
    ), array(
446
      array(
447
        'name' => 'George',
448
        'age' => 27,
449
      ),
450
      array(
451
        'name' => 'Ringo',
452
        'age' => 28,
453
      ),
454
      array(
455
        'name' => 'Meredith',
456
        'age' => 30,
457
      ),
458
    ));
459
    $this->applyFilterNumericExposedOperator('>', array('value' => '27'), array(
460
      array(
461
        'name' => 'Ringo',
462
        'age' => 28,
463
      ),
464
      array(
465
        'name' => 'Meredith',
466
        'age' => 30,
467
      ),
468
    ));
469
    $this->applyFilterNumericExposedOperator('between', array(
470
      'min' => '28',
471
      'max' => '31',
472
    ), array(
473
      array(
474
        'name' => 'Ringo',
475
        'age' => 28,
476
      ),
477
      array(
478
        'name' => 'Meredith',
479
        'age' => 30,
480
      ),
481
    ));
482
    $this->applyFilterNumericExposedOperator('not between', array(
483
      'min' => '28',
484
      'max' => '31',
485
    ), array(
486
      array(
487
        'name' => 'John',
488
        'age' => 25,
489
      ),
490
      array(
491
        'name' => 'George',
492
        'age' => 27,
493
      ),
494
      array(
495
        'name' => 'Ringo',
496
        'age' => 28,
497
      ),
498
      array(
499
        'name' => 'Paul',
500
        'age' => 26,
501
      ),
502
    ));
503
    $this->applyFilterNumericExposedOperator('empty', array(), array(
504
      array(
505
        'name' => 'Charles',
506
        'age' => NULL,
507
      ),
508
    ));
509
    $this->applyFilterNumericExposedOperator('not empty', array(), array(
510
      array(
511
        'name' => 'John',
512
        'age' => 25,
513
      ),
514
      array(
515
        'name' => 'George',
516
        'age' => 27,
517
      ),
518
      array(
519
        'name' => 'Ringo',
520
        'age' => 28,
521
      ),
522
      array(
523
        'name' => 'Paul',
524
        'age' => 26,
525
      ),
526
      array(
527
        'name' => 'Meredith',
528
        'age' => 30,
529
      ),
530
    ));
531
    $this->applyFilterNumericExposedOperator('regular_expression', array(
532
      'value' => '^(0|[1-9][0-9]*)$',
533
    ), array(
534
      array(
535
        'name' => 'John',
536
        'age' => 25,
537
      ),
538
      array(
539
        'name' => 'George',
540
        'age' => 27,
541
      ),
542
      array(
543
        'name' => 'Ringo',
544
        'age' => 28,
545
      ),
546
      array(
547
        'name' => 'Paul',
548
        'age' => 26,
549
      ),
550
      array(
551
        'name' => 'Meredith',
552
        'age' => 30,
553
      ),
554
    ));
555
    $this->applyFilterNumericExposedOperator('not_regular_expression', array(
556
      'value' => '^(0|[1-9][0-9]*)$',
557
    ), array());
558
  }
559

    
560
  /**
561
   * Tests exposed numeric filter with an individual exposed operator.
562
   *
563
   * @param string $operator
564
   *   Operator to test.
565
   * @param array $value
566
   *   Filter value to use in exposed input. Keys might be 'value', 'min' or
567
   *   'max'. If one of those keys doesn't exist, an empty string is used as the
568
   *   key's value.
569
   * @param array $resultset
570
   *   The expected result set.
571
   */
572
  protected function applyFilterNumericExposedOperator($operator, array $value, array $resultset) {
573
    $exposed_input = array(
574
      'age' => ($value += array(
575
        'value' => '',
576
        'min' => '',
577
        'max' => '',
578
      )),
579
      'age_op' => $operator,
580
    );
581
    $filters = array(
582
      'age' => array(
583
        'id' => 'age',
584
        'table' => 'views_test',
585
        'field' => 'age',
586
        'relationship' => 'none',
587
        'exposed' => TRUE,
588
        'expose' => array(
589
          'operator' => 'age_op',
590
          'label' => 'age',
591
          'identifier' => 'age',
592
          'use_operator' => TRUE,
593
        ),
594
      ),
595
    );
596
    $view = $this->getBasicPageView();
597
    $view->set_display('page_1');
598
    $view->display['page_1']->handler->override_option('filters', $filters);
599
    $view->set_exposed_input($exposed_input);
600
    $this->executeView($view);
601
    $this->assertIdenticalResultset($view, $resultset, $this->column_map, 'Identical result set for ' . $operator . ' with untouched values.');
602
    $view->destroy();
603

    
604
    // Min, max and value fields are shown/hidden only via JS, so they might
605
    // still be set from a previous operation. Assert that this doesn't change
606
    // the expected result set.
607
    $exposed_input['age'] += array(
608
      'value' => '25',
609
      'min' => '28',
610
      'max' => '30',
611
    );
612
    $view = $this->getBasicPageView();
613
    $view->set_display('page_1');
614
    $view->display['page_1']->handler->override_option('filters', $filters);
615
    $view->set_exposed_input($exposed_input);
616
    $this->executeView($view);
617
    $this->assertIdenticalResultset($view, $resultset, $this->column_map, 'Identical result set for ' . $operator . ' with leftover values from previous operation.');
618
  }
619

    
620
  public function testAllowEmpty() {
621
    $view = $this->getBasicView();
622

    
623
    $view->display['default']->handler->override_option('filters', array(
624
      'id' => array(
625
        'id' => 'id',
626
        'table' => 'views_test',
627
        'field' => 'id',
628
        'relationship' => 'none',
629
      ),
630
      'age' => array(
631
        'id' => 'age',
632
        'table' => 'views_test',
633
        'field' => 'age',
634
        'relationship' => 'none',
635
      ),
636
    ));
637

    
638
    $view->set_display('default');
639
    $view->init_handlers();
640

    
641
    $id_operators = $view->filter['id']->operators();
642
    $age_operators = $view->filter['age']->operators();
643

    
644
    $this->assertFalse(isset($id_operators['empty']));
645
    $this->assertFalse(isset($id_operators['not empty']));
646
    $this->assertTrue(isset($age_operators['empty']));
647
    $this->assertTrue(isset($age_operators['not empty']));
648
  }
649

    
650
  protected function getGroupedExposedFilters() {
651
    $filters = array(
652
      'age' => array(
653
        'id' => 'age',
654
        'table' => 'views_test',
655
        'field' => 'age',
656
        'relationship' => 'none',
657
        'exposed' => TRUE,
658
        'expose' => array(
659
          'operator' => 'age_op',
660
          'label' => 'age',
661
          'identifier' => 'age',
662
        ),
663
        'is_grouped' => TRUE,
664
        'group_info' => array(
665
          'label' => 'age',
666
          'identifier' => 'age',
667
          'default_group' => 'All',
668
          'group_items' => array(
669
            1 => array(
670
              'title' => 'Age is 28',
671
              'operator' => '=',
672
              'value' => array('value' => 28),
673
            ),
674
            2 => array(
675
              'title' => 'Age is between 26 and 29',
676
              'operator' => 'between',
677
              'value' => array(
678
                'min' => 26,
679
                'max' => 29,
680
              ),
681
            ),
682
            3 => array(
683
              'title' => 'Age is not between 26 and 29',
684
              'operator' => 'not between',
685
              'value' => array(
686
                'min' => 26,
687
                'max' => 29,
688
              ),
689
            ),
690
            4 => array(
691
              'title' => 'Age is empty',
692
              'operator' => 'empty',
693
            ),
694
            5 => array(
695
              'title' => 'Age is not empty',
696
              'operator' => 'not empty',
697
            ),
698
          ),
699
        ),
700
      ),
701
    );
702
    return $filters;
703
  }
704

    
705
}