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

Drupal example source code file (views_access.test)

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

access, array, default, expected_hook_menu, false, function, handler, none, php, return, test, to, true, view

The views_access.test Drupal example source code

<?php
// $Id: views_access.test,v 1.1.4.6 2011/01/04 21:26:47 dereine Exp $

/**
 * Basic test for pluggable access.
 */
class ViewsAccessTest extends ViewsSqlTest {
  public static function getInfo() {
    return array(
      'name' => t('Access'),
      'description' => t('Tests pluggable access for views.'),
      'group' => t('Views Plugins')
    );
  }

  public function setUp() {
    parent::setUp();

    $this->admin_user = $this->drupalCreateUser(array('access all views'));
    $this->web_user = $this->drupalCreateUser();
    $this->web_role = current($this->web_user->roles);

    $this->normal_role = $this->drupalCreateRole(array());
    $this->normal_user = $this->drupalCreateUser(array('views_test test permission'));
    $this->normal_user->roles[$this->normal_role] = $this->normal_role;
    // Reset the plugin data.
    views_fetch_plugin_data(NULL, NULL, TRUE);
  }

  function viewsPlugins() {
    $plugins = array(
      'access' =>  array(
        'test_static' => array(
          'title' => t('Static test access plugin'),
          'help' => t('Provides a static test access plugin.'),
          'handler' => 'views_test_plugin_access_test_static',
          'path' => drupal_get_path('module', 'views_test') . '/test_plugins',
        ),
        'test_dynamic' => array(
          'title' => t('Dynamic test access plugin'),
          'help' => t('Provides a dynamic test access plugin.'),
          'handler' => 'views_test_plugin_access_test_dynamic',
          'path' => drupal_get_path('module', 'views_test') . '/test_plugins',
        ),
      ),
    );

    return $plugins;
  }

  /**
   * Test none access plugin.
   */
  function testAccessNone() {
    $view = $this->view_access_none();

    $view->set_display('default');

    $this->assertTrue($view->display_handler->access($this->admin_user), t('Admin-Account should be able to access the view everytime'));
    $this->assertTrue($view->display_handler->access($this->web_user));
    $this->assertTrue($view->display_handler->access($this->normal_user));
  }

  /**
   * Test perm access plugin.
   */
  function testAccessPerm() {
    $view = $this->view_access_perm();

    $view->set_display('default');
    $access_plugin = $view->display_handler->get_plugin('access');

    $this->assertTrue($view->display_handler->access($this->admin_user), t('Admin-Account should be able to access the view everytime'));
    $this->assertFalse($view->display_handler->access($this->web_user));
    $this->assertTrue($view->display_handler->access($this->normal_user));
  }

  /**
   * Test role access plugin.
   */
  function testAccessRole() {
    $view = $this->view_access_role();

    $view->set_display('default');
    $access_plugin = $view->display_handler->get_plugin('access');

    $this->assertTrue($view->display_handler->access($this->admin_user), t('Admin-Account should be able to access the view everytime'));
    $this->assertFalse($view->display_handler->access($this->web_user));
    $this->assertTrue($view->display_handler->access($this->normal_user));
  }

  /**
   * @todo Test abstract access plugin.
   */

  /**
   * Test static access check.
   */
  function testStaticAccessPlugin() {
    $view = $this->view_access_static();

    $view->set_display('default');
    $access_plugin = $view->display_handler->get_plugin('access');

    $this->assertFalse($access_plugin->access($this->normal_user));

    $access_plugin->options['access'] = TRUE;
    $this->assertTrue($access_plugin->access($this->normal_user));

    // FALSE comes from hook_menu caching.
    $expected_hook_menu = array(
      'views_test_test_static_access_callback', array(FALSE)
    );
    $hook_menu = $view->execute_hook_menu('page_1');
    $this->assertEqual($expected_hook_menu, $hook_menu['test_access_static']['access arguments'][0]);

    $expected_hook_menu = array(
      'views_test_test_static_access_callback', array(TRUE)
    );
    $this->assertTrue(views_access($expected_hook_menu));
  }

  /**
   * Test dynamic access plugin.
   */
  function testDynamicAccessPlugin() {
    $view = $this->view_access_dynamic();
    $argument1 = $this->randomName();
    $argument2 = $this->randomName();
    variable_set('test_dynamic_access_argument1', $argument1);
    variable_set('test_dynamic_access_argument2', $argument2);

    $view->set_display('default');
    $access_plugin = $view->display_handler->get_plugin('access');

    $this->assertFalse($access_plugin->access($this->normal_user));

    $access_plugin->options['access'] = TRUE;
    $this->assertFalse($access_plugin->access($this->normal_user));

    $view->set_arguments(array($argument1, $argument2));
    $this->assertTrue($access_plugin->access($this->normal_user));

    // FALSE comes from hook_menu caching.
    $expected_hook_menu = array(
      'views_test_test_dynamic_access_callback', array(FALSE, 1, 2)
    );
    $hook_menu = $view->execute_hook_menu('page_1');
    $this->assertEqual($expected_hook_menu, $hook_menu['test_access_dynamic']['access arguments'][0]);

    $expected_hook_menu = array(
      'views_test_test_dynamic_access_callback', array(TRUE, 1, 2)
    );
    $this->assertTrue(views_access($expected_hook_menu, $argument1, $argument2));
  }

  function view_access_none() {
    $view = new view;
    $view->name = 'test_access_none';
    $view->description = '';
    $view->tag = '';
    $view->view_php = '';
    $view->base_table = 'node';
    $view->is_cacheable = FALSE;
    $view->api_version = 2;
    $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['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';

    return $view;
  }

  function view_access_perm() {
    $view = new view;
    $view->name = 'test_access_perm';
    $view->description = '';
    $view->tag = '';
    $view->view_php = '';
    $view->base_table = 'node';
    $view->is_cacheable = FALSE;
    $view->api_version = 2;
    $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'] = 'perm';
    $handler->display->display_options['access']['perm'] = 'views_test test permission';
    $handler->display->display_options['cache']['type'] = 'none';
    $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';

    return $view;
  }

  function view_access_role() {
    $view = new view;
    $view->name = 'test_access_role';
    $view->description = '';
    $view->tag = '';
    $view->view_php = '';
    $view->base_table = 'node';
    $view->is_cacheable = FALSE;
    $view->api_version = 2;
    $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'] = 'role';
    $handler->display->display_options['access']['role'] = array(
      $this->normal_role => $this->normal_role,
    );
    $handler->display->display_options['cache']['type'] = 'none';
    $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';

    return $view;
  }

  function view_access_dynamic() {
    $view = new view;
    $view->name = 'test_access_dynamic';
    $view->description = '';
    $view->tag = '';
    $view->view_php = '';
    $view->base_table = 'node';
    $view->is_cacheable = FALSE;
    $view->api_version = 2;
    $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'] = 'test_dynamic';
    $handler->display->display_options['cache']['type'] = 'none';
    $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';

    $handler = $view->new_display('page', 'Page', 'page_1');
    $handler->display->display_options['path'] = 'test_access_dynamic';

    return $view;
  }

  function view_access_static() {
    $view = new view;
    $view->name = 'test_access_static';
    $view->description = '';
    $view->tag = '';
    $view->view_php = '';
    $view->base_table = 'node';
    $view->is_cacheable = FALSE;
    $view->api_version = 2;
    $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'] = 'test_static';
    $handler->display->display_options['cache']['type'] = 'none';
    $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';

    $handler = $view->new_display('page', 'Page', 'page_1');
    $handler->display->display_options['path'] = 'test_access_static';

    return $view;
  }
}

Other Drupal examples (source code examples)

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