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

Drupal example source code file (views_handler_filter_date.test)

This example Drupal source code file (views_handler_filter_date.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, between, custom, expected_result, format, format_date, function, h:s, nid, node, php, return, view, y-m-d

The views_handler_filter_date.test Drupal example source code

<?php
// $Id: views_handler_filter_date.test,v 1.1.4.3 2010/11/25 20:07:44 dereine Exp $

/**
 * Tests the handler filter_date.
 *
 * @TODO
 *   Convert to "beatles".
 */
class ViewsHandlerFilterDateTest extends ViewsSqlTest {
  public static function getInfo() {
    return array(
      'name' => 'Filter: Date',
      'description' => 'Test the core views_handler_filter_date handler.',
      'group' => 'Views Handlers',
    );
  }

  function setUp() {
    parent::setUp();
    // Add some basic test nodes.
    $this->nodes = array();
    $this->nodes[] = $this->drupalCreateNode(array('created' => 100000));
    $this->nodes[] = $this->drupalCreateNode(array('created' => 200000));
    $this->nodes[] = $this->drupalCreateNode(array('created' => 300000));
    $this->nodes[] = $this->drupalCreateNode(array('created' => time() + 86400));

    $this->map = array(
      'nid' => 'nid',
    );
    $this->enableViewsUi();
  }

  /**
  /* Test the general offset functionality.
   */
  function testOffset() {
    $view = $this->views_test_offset();
    // Test offset for simple operator.
    $view->set_display('default');
    $view->init_handlers();
    $view->filter['created']->operator = '>';
    $view->filter['created']->value['type'] = 'offset';
    $view->filter['created']->value['value'] = '+1 hour';
    $view->execute_display('default');
    $expected_result = array(
      array('nid' => $this->nodes[3]->nid),
    );
    $this->assertIdenticalResultset($view, $expected_result, $this->map);
    $view->delete();

    // Test offset for between operator.
    $view->set_display('default');
    $view->init_handlers();
    $view->filter['created']->operator = 'between';
    $view->filter['created']->value['type'] = 'offset';
    $view->filter['created']->value['max'] = '+2 days';
    $view->filter['created']->value['min'] = '+1 hour';
    $view->execute_display('default');
    $expected_result = array(
      array('nid' => $this->nodes[3]->nid),
    );
    $this->assertIdenticalResultset($view, $expected_result, $this->map);
    $view->delete();
  }


  /**
   * Test the filter operator between/not between.
   */
  function testBetween() {
    // Test between with min and max.
    $view = $this->views_test_between();
    $view->set_display('default');
    $view->init_handlers();
    $view->filter['created']->operator = 'between';
    $view->filter['created']->value['min'] = format_date(150000, 'custom', 'Y-m-d H:s');
    $view->filter['created']->value['max'] = format_date(250000, 'custom', 'Y-m-d H:s');
    $view->execute_display('default');
    $expected_result = array(
      array('nid' => $this->nodes[1]->nid),
    );
    $this->assertIdenticalResultset($view, $expected_result, $this->map);
    $view->delete();

    // Test between with just min.
    $view = $this->views_test_between();
    $view->set_display('default');
    $view->init_handlers();
    $view->filter['created']->operator = 'between';
    $view->filter['created']->value['max'] = format_date(250000, 'custom', 'Y-m-d H:s');
    $view->execute_display('default');
    $expected_result = array(
      array('nid' => $this->nodes[0]->nid),
      array('nid' => $this->nodes[1]->nid),
    );
    $this->assertIdenticalResultset($view, $expected_result, $this->map);
    $view->delete();

    // Test not between with min and max.
    $view = $this->views_test_between();
    $view->set_display('default');
    $view->init_handlers();
    $view->filter['created']->operator = 'not between';
    $view->filter['created']->value['min'] = format_date(150000, 'custom', 'Y-m-d H:s');
    $view->filter['created']->value['max'] = format_date(250000, 'custom', 'Y-m-d H:s');
    $view->execute_display('default');
    $expected_result = array(
      array('nid' => $this->nodes[0]->nid),
      array('nid' => $this->nodes[2]->nid),
      array('nid' => $this->nodes[3]->nid),
    );
    $this->assertIdenticalResultset($view, $expected_result, $this->map);
    $view->delete();

    // Test not between with min.
    $view = $this->views_test_between();
    $view->set_display('default');
    $view->init_handlers();
    $view->filter['created']->operator = 'not between';
    $view->filter['created']->value['max'] = format_date(150000, 'custom', 'Y-m-d H:s');
    $view->execute_display('default');
    $expected_result = array(
      array('nid' => $this->nodes[1]->nid),
      array('nid' => $this->nodes[2]->nid),
      array('nid' => $this->nodes[3]->nid),
    );
    $this->assertIdenticalResultset($view, $expected_result, $this->map);
    $view->delete();
  }

  /**
   * Take sure the validation callbacks works.
   */
  function testUiValidation() {
    $view = $this->views_test_between();
    $view->save();

    $admin_user =   $this->drupalCreateUser(array('administer views', 'administer site configuration'));
    $this->drupalLogin($admin_user);
    menu_rebuild();
    $this->drupalGet('admin/structure/views/edit/test_filter_date_between');
    $this->drupalGet('admin/structure/views/nojs/config-item/test_filter_date_between/default/filter/created');

    $edit = array();
    // Generate a definitive wrong value, which should be checked by validation.
    $edit['options[value][value]'] = $this->randomString() . '-------';
    $this->drupalPost(NULL, $edit, t('Update'));
    $this->assertText(t('Invalid date format.'), 'Take sure that validation is runned and the invalidate date format is identified.');
  }

  function views_test_between() {
    $view = new view;
    $view->name = 'test_filter_date_between';
    $view->description = '';
    $view->tag = '';
    $view->base_table = 'node';
    $view->api_version = '3.0-alpha1';
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

    /* Display: Defaults */
    $handler = $view->new_display('default', 'Defaults', 'default');
    $handler->display->display_options['access']['type'] = 'none';
    $handler->display->display_options['cache']['type'] = 'none';
    $handler->display->display_options['query']['type'] = 'views_query';
    $handler->display->display_options['exposed_form']['type'] = 'basic';
    $handler->display->display_options['pager']['type'] = 'full';
    $handler->display->display_options['style_plugin'] = 'default';
    $handler->display->display_options['row_plugin'] = 'fields';
    /* Field: Node: Nid */
    $handler->display->display_options['fields']['nid']['id'] = 'nid';
    $handler->display->display_options['fields']['nid']['table'] = 'node';
    $handler->display->display_options['fields']['nid']['field'] = 'nid';
    /* Filter: Node: Updated date */
    $handler->display->display_options['filters']['created']['id'] = 'created';
    $handler->display->display_options['filters']['created']['table'] = 'node';
    $handler->display->display_options['filters']['created']['field'] = 'created';

    return $view;
  }
  
  function views_test_offset() {
    $view = $this->views_test_between();
    return $view;
  }
}

Other Drupal examples (source code examples)

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