<?php

/**
 * @file
 * Definition of ViewsAccessTest.
 */

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

  /**
   * {@inheritdoc}
   */
  public function setUp(array $modules = array()) {
    parent::setUp($modules);

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

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

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

  /**
   * Tests 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.
   */

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

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

  /**
   * Tests access for a view with a missing access plugin.
   */
  public function testMissingAccessPlugin() {
    $view = $this->getMissingAccessPluginTestView();

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

    $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));

    $hook_menu = $view->execute_hook_menu('page_1');
    $this->assertTrue($hook_menu['test_access_missing']['access arguments'][0]);

    $this->assertTrue(views_access(TRUE));
  }

  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: Master */
    $handler = $view->new_display('default', 'Master', '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: Master */
    $handler = $view->new_display('default', 'Master', '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: Master */
    $handler = $view->new_display('default', 'Master', '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: Master */
    $handler = $view->new_display('default', 'Master', '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: Master */
    $handler = $view->new_display('default', 'Master', '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;
  }

  /**
   * Generates a view with an access plugin that doesn't exist.
   */
  protected function getMissingAccessPluginTestView() {
    $view = new view();
    $view->name = 'test_access_missing';
    $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: Master */
    $handler = $view->new_display('default', 'Master', 'default');
    $handler->display->display_options['access']['type'] = 'does_not_exist';
    $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_missing';

    return $view;
  }

}
