<?php
// $Id: views_access.test,v 1.1.2.1 2010/02/13 21:01:42 dereine Exp $

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

  public function setUp() {
    parent::setUp('views', 'views_test');

    $this->admin_user = $this->drupalCreateUser(array('access all views'));
    $this->web_user = $this->drupalCreateUser();
    $this->web_role = current($this->web_user->roles);
    $this->normal_user = $this->drupalCreateUser(array('views_test test permission'));
    $roles = array_keys($this->normal_user->roles);
    $this->normal_role = array_pop($roles);
  }

  /**
   * 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));
  }

  /**
   * @todo 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));
  }

  /**
   * @todo 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.
   */

  /**
   * @todo Test menu access check.
   */

  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;
  }
}