home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (views_handler_sort_random.test)

This example Drupal source code file (views_handler_sort_random.test) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, count, data, function, match, number, of, php, return, returned, rows, view_random, views_test_age, views_test_name

The views_handler_sort_random.test Drupal example source code

<?php
// $Id: views_handler_sort_random.test,v 1.1.2.1 2010/11/07 11:32:25 dereine Exp $

/**
 * Test for core views_handler_sort_random handler.
 */
class ViewsHandlerSortRandomTest extends ViewsSqlTest {
  public static function getInfo() {
    return array(
      'name' => t('Sort: random'),
      'description' => t('Test the core views_handler_sort_random handler.'),
      'group' => t('Views Handlers'),
    );
  }

  /**
   * Add more items to the test set, to make the order tests more robust.
   */
  protected function dataSet() {
    $data = parent::dataSet();
    for ($i = 0; $i < 50; $i++) {
      $data[] = array(
        'name' => 'name_' . $i,
        'age' => $i,
        'job' => 'job_' . $i,
        'created' => rand(0, time()),
      );
    }
    return $data;
  }

  /**
   * Return a basic view with random ordering.
   */
  protected function getBasicRandomView() {
    $view = $this->getBasicView();

    // Add a random ordering.
    $view->display['default']->handler->override_option('sorts', array(
      'random' => array(
        'id' => 'random',
        'field' => 'random',
        'table' => 'views',
      ),
    ));

    return $view;
  }

  /**
   * Test random ordering of the result set.
   *
   * @see DatabaseSelectTestCase::testRandomOrder().
   */
  public function testRandomOrdering() {
    // Execute a basic view first.
    $view = $this->getBasicView();
    $this->executeView($view);

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

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

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

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal views_handler_sort_random.test source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.