123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?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() {
- 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;
- }
- /**
- * 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));
- }
- 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;
- }
- }
|