views_handler_sort_random.test

Definition of ViewsHandlerSortRandomTest.

File

tests/handlers/views_handler_sort_random.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of ViewsHandlerSortRandomTest.
  5. */
  6. /**
  7. * Tests for core views_handler_sort_random handler.
  8. */
  9. class ViewsHandlerSortRandomTest extends ViewsSqlTest {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Sort: random',
  13. 'description' => 'Test the core views_handler_sort_random handler.',
  14. 'group' => 'Views Handlers',
  15. );
  16. }
  17. /**
  18. * Add more items to the test set, to make the order tests more robust.
  19. */
  20. protected function dataSet() {
  21. $data = parent::dataSet();
  22. for ($i = 0; $i < 50; $i++) {
  23. $data[] = array(
  24. 'name' => 'name_' . $i,
  25. 'age' => $i,
  26. 'job' => 'job_' . $i,
  27. 'created' => rand(0, time()),
  28. );
  29. }
  30. return $data;
  31. }
  32. /**
  33. * Return a basic view with random ordering.
  34. */
  35. protected function getBasicRandomView() {
  36. $view = $this->getBasicView();
  37. // Add a random ordering.
  38. $view->display['default']->handler->override_option('sorts', array(
  39. 'random' => array(
  40. 'id' => 'random',
  41. 'field' => 'random',
  42. 'table' => 'views',
  43. ),
  44. ));
  45. return $view;
  46. }
  47. /**
  48. * Tests random ordering of the result set.
  49. *
  50. * @see DatabaseSelectTestCase::testRandomOrder()
  51. */
  52. public function testRandomOrdering() {
  53. // Execute a basic view first.
  54. $view = $this->getBasicView();
  55. $this->executeView($view);
  56. // Verify the result.
  57. $this->assertEqual(count($this->dataSet()), count($view->result), t('The number of returned rows match.'));
  58. $this->assertIdenticalResultset($view, $this->dataSet(), array(
  59. 'views_test_name' => 'name',
  60. 'views_test_age' => 'age',
  61. ));
  62. // Execute a random view, we expect the result set to be different.
  63. $view_random = $this->getBasicRandomView();
  64. $this->executeView($view_random);
  65. $this->assertEqual(count($this->dataSet()), count($view_random->result), t('The number of returned rows match.'));
  66. $this->assertNotIdenticalResultset($view_random, $view->result, array(
  67. 'views_test_name' => 'views_test_name',
  68. 'views_test_age' => 'views_test_name',
  69. ));
  70. // Execute a second random view, we expect the result set to be different again.
  71. $view_random_2 = $this->getBasicRandomView();
  72. $this->executeView($view_random_2);
  73. $this->assertEqual(count($this->dataSet()), count($view_random_2->result), t('The number of returned rows match.'));
  74. $this->assertNotIdenticalResultset($view_random, $view->result, array(
  75. 'views_test_name' => 'views_test_name',
  76. 'views_test_age' => 'views_test_name',
  77. ));
  78. }
  79. }