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

Drupal example source code file (views_cache.test)

This example Drupal source code file (views_cache.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

3600, age, array, function, id, match, name, none, of, php, time, type, view, views_test

The views_cache.test Drupal example source code

<?php
// $Id: views_cache.test,v 1.1.4.3 2010/05/24 19:58:36 dereine Exp $
/**
 * @file
 *   test cache system.
 */

module_load_include('test', 'views', 'tests/views_query');
/**
 * Basic test for pluggable caching.
 */
class ViewsCacheTest extends ViewsSqlTest {
  public static function getInfo() {
    return array(
      'name' => t('Cache'),
      'description' => t('Tests pluggable caching for views.'),
      'group' => t('Views Plugins')
    );
  }

  /**
   * Build and return a basic view of the views_test table.
   */
  protected function getBasicView() {
    views_include('view');

    // Create the basic view.
    $view = new view();
    $view->vid = 'test_view';
    $view->add_display('default');
    $view->base_table = 'views_test';

    // Set up the fields we need.
    $display = $view->new_display('default', 'Defaults', 'default');
    $display->override_option('fields', array(
      'id' => array(
        'id' => 'id',
        'table' => 'views_test',
        'field' => 'id',
        'relationship' => 'none',
      ),
      'name' => array(
        'id' => 'name',
        'table' => 'views_test',
        'field' => 'name',
        'relationship' => 'none',
      ),
      'age' => array(
        'id' => 'age',
        'table' => 'views_test',
        'field' => 'age',
        'relationship' => 'none',
      ),
    ));

    // Set up the sort order.
    $display->override_option('sorts', array(
      'id' => array(
        'order' => 'ASC',
        'id' => 'id',
        'table' => 'views_test',
        'field' => 'id',
        'relationship' => 'none',
      ),
    ));

    return $view;
  }

  /**
   * Tests time based caching.
   */
  function testTimeCaching() {
    // Create a basic result which just 2 results.
    $view = $this->getBasicView();
    $view->set_display();
    $view->display_handler->override_option('cache', array(
      'type' => 'time',
      'results_lifespan' => '3600',
      'output_lifespan' => '3600',
    ));

    $view->execute();
    // Verify the result.
    $this->assertEqual(5, count($view->result), t('The number of returned rows match.'));

    // Add another man to the beatles.
    $record = array(
      'name' => 'Rod Davis',
      'age' => 29,
      'job' => 'Banjo',
    );
    drupal_write_record('views_test', $record);

    // The Result should be the same as before, because of the caching.
    $view = $this->getBasicView();
    $view->set_display();
    $view->display_handler->override_option('cache', array(
      'type' => 'time',
      'results_lifespan' => '3600',
      'output_lifespan' => '3600',
    ));

    $view->execute();
    // Verify the result.
    $this->assertEqual(5, count($view->result), t('The number of returned rows match.'));
  }

  /**
   * Tests no caching.
   */
  function testNoneCaching() {
    // Create a basic result which just 2 results.
    $view = $this->getBasicView();
    $view->set_display();
    $view->display_handler->override_option('cache', array(
      'type' => 'none',
    ));

    $view->execute();
    // Verify the result.
    $this->assertEqual(5, count($view->result), t('The number of returned rows match.'));

    // Add another man to the beatles.
    $record = array(
      'name' => 'Rod Davis',
      'age' => 29,
      'job' => 'Banjo',
    );

    drupal_write_record('views_test', $record);

    // The Result should be the same as before, because of the caching.
    $view = $this->getBasicView();
    $view->set_display();
    $view->display_handler->override_option('cache', array(
      'type' => 'time',
      'results_lifespan' => '3600',
      'output_lifespan' => '3600',
    ));

    $view->execute();
    // Verify the result.
    $this->assertEqual(6, count($view->result), t('The number of returned rows match.'));
  }
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal views_cache.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.