Project

General

Profile

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

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

1
<?php
2

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

    
8
/**
9
 * Tests for core views_handler_sort_random handler.
10
 */
11
class ViewsHandlerSortRandomTest extends ViewsSqlTest {
12

    
13
  /**
14
   *
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Sort: random',
19
      'description' => 'Test the core views_handler_sort_random handler.',
20
      'group' => 'Views Handlers',
21
    );
22
  }
23

    
24
  /**
25
   * Add more items to the test set, to make the order tests more robust.
26
   */
27
  protected function dataSet() {
28
    $data = parent::dataSet();
29
    for ($i = 0; $i < 50; $i++) {
30
      $data[] = array(
31
        'name' => 'name_' . $i,
32
        'age' => $i,
33
        'job' => 'job_' . $i,
34
        'created' => rand(0, time()),
35
      );
36
    }
37
    return $data;
38
  }
39

    
40
  /**
41
   * Return a basic view with random ordering.
42
   */
43
  protected function getBasicRandomView() {
44
    $view = $this->getBasicView();
45

    
46
    // Add a random ordering.
47
    $view->display['default']->handler->override_option('sorts', array(
48
      'random' => array(
49
        'id' => 'random',
50
        'field' => 'random',
51
        'table' => 'views',
52
      ),
53
    ));
54

    
55
    return $view;
56
  }
57

    
58
  /**
59
   * Tests random ordering of the result set.
60
   *
61
   * @see DatabaseSelectTestCase::testRandomOrder()
62
   */
63
  public function testRandomOrdering() {
64
    // Execute a basic view first.
65
    $view = $this->getBasicView();
66
    $this->executeView($view);
67

    
68
    // Verify the result.
69
    $this->assertEqual(count($this->dataSet()), count($view->result), t('The number of returned rows match.'));
70
    $this->assertIdenticalResultset($view, $this->dataSet(), array(
71
      'views_test_name' => 'name',
72
      'views_test_age' => 'age',
73
    ));
74

    
75
    // Execute a random view, we expect the result set to be different.
76
    $view_random = $this->getBasicRandomView();
77
    $this->executeView($view_random);
78
    $this->assertEqual(count($this->dataSet()), count($view_random->result), t('The number of returned rows match.'));
79
    $this->assertNotIdenticalResultset($view_random, $view->result, array(
80
      'views_test_name' => 'views_test_name',
81
      'views_test_age' => 'views_test_name',
82
    ));
83

    
84
    // Execute a second random view, we expect the result set to be different
85
    // again.
86
    $view_random_2 = $this->getBasicRandomView();
87
    $this->executeView($view_random_2);
88
    $this->assertEqual(count($this->dataSet()), count($view_random_2->result), t('The number of returned rows match.'));
89
    $this->assertNotIdenticalResultset($view_random, $view->result, array(
90
      'views_test_name' => 'views_test_name',
91
      'views_test_age' => 'views_test_name',
92
    ));
93
  }
94

    
95
}