views_access.test 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * @file
  4. * Definition of ViewsAccessTest.
  5. */
  6. /**
  7. * Basic test for pluggable access.
  8. */
  9. class ViewsAccessTest extends ViewsSqlTest {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Access',
  13. 'description' => 'Tests pluggable access for views.',
  14. 'group' => 'Views Plugins'
  15. );
  16. }
  17. public function setUp() {
  18. parent::setUp();
  19. $this->admin_user = $this->drupalCreateUser(array('access all views'));
  20. $this->web_user = $this->drupalCreateUser();
  21. $this->web_role = current($this->web_user->roles);
  22. $this->normal_role = $this->drupalCreateRole(array());
  23. $this->normal_user = $this->drupalCreateUser(array('views_test test permission'));
  24. $this->normal_user->roles[$this->normal_role] = $this->normal_role;
  25. // Reset the plugin data.
  26. views_fetch_plugin_data(NULL, NULL, TRUE);
  27. }
  28. function viewsPlugins() {
  29. $plugins = array(
  30. 'access' => array(
  31. 'test_static' => array(
  32. 'title' => t('Static test access plugin'),
  33. 'help' => t('Provides a static test access plugin.'),
  34. 'handler' => 'views_test_plugin_access_test_static',
  35. 'path' => drupal_get_path('module', 'views_test') . '/test_plugins',
  36. ),
  37. 'test_dynamic' => array(
  38. 'title' => t('Dynamic test access plugin'),
  39. 'help' => t('Provides a dynamic test access plugin.'),
  40. 'handler' => 'views_test_plugin_access_test_dynamic',
  41. 'path' => drupal_get_path('module', 'views_test') . '/test_plugins',
  42. ),
  43. ),
  44. );
  45. return $plugins;
  46. }
  47. /**
  48. * Tests none access plugin.
  49. */
  50. function testAccessNone() {
  51. $view = $this->view_access_none();
  52. $view->set_display('default');
  53. $this->assertTrue($view->display_handler->access($this->admin_user), t('Admin-Account should be able to access the view everytime'));
  54. $this->assertTrue($view->display_handler->access($this->web_user));
  55. $this->assertTrue($view->display_handler->access($this->normal_user));
  56. }
  57. /**
  58. * Tests perm access plugin.
  59. */
  60. function testAccessPerm() {
  61. $view = $this->view_access_perm();
  62. $view->set_display('default');
  63. $access_plugin = $view->display_handler->get_plugin('access');
  64. $this->assertTrue($view->display_handler->access($this->admin_user), t('Admin-Account should be able to access the view everytime'));
  65. $this->assertFalse($view->display_handler->access($this->web_user));
  66. $this->assertTrue($view->display_handler->access($this->normal_user));
  67. }
  68. /**
  69. * Tests role access plugin.
  70. */
  71. function testAccessRole() {
  72. $view = $this->view_access_role();
  73. $view->set_display('default');
  74. $access_plugin = $view->display_handler->get_plugin('access');
  75. $this->assertTrue($view->display_handler->access($this->admin_user), t('Admin-Account should be able to access the view everytime'));
  76. $this->assertFalse($view->display_handler->access($this->web_user));
  77. $this->assertTrue($view->display_handler->access($this->normal_user));
  78. }
  79. /**
  80. * @todo Test abstract access plugin.
  81. */
  82. /**
  83. * Tests static access check.
  84. */
  85. function testStaticAccessPlugin() {
  86. $view = $this->view_access_static();
  87. $view->set_display('default');
  88. $access_plugin = $view->display_handler->get_plugin('access');
  89. $this->assertFalse($access_plugin->access($this->normal_user));
  90. $access_plugin->options['access'] = TRUE;
  91. $this->assertTrue($access_plugin->access($this->normal_user));
  92. // FALSE comes from hook_menu caching.
  93. $expected_hook_menu = array(
  94. 'views_test_test_static_access_callback', array(FALSE)
  95. );
  96. $hook_menu = $view->execute_hook_menu('page_1');
  97. $this->assertEqual($expected_hook_menu, $hook_menu['test_access_static']['access arguments'][0]);
  98. $expected_hook_menu = array(
  99. 'views_test_test_static_access_callback', array(TRUE)
  100. );
  101. $this->assertTrue(views_access($expected_hook_menu));
  102. }
  103. /**
  104. * Tests dynamic access plugin.
  105. */
  106. function testDynamicAccessPlugin() {
  107. $view = $this->view_access_dynamic();
  108. $argument1 = $this->randomName();
  109. $argument2 = $this->randomName();
  110. variable_set('test_dynamic_access_argument1', $argument1);
  111. variable_set('test_dynamic_access_argument2', $argument2);
  112. $view->set_display('default');
  113. $access_plugin = $view->display_handler->get_plugin('access');
  114. $this->assertFalse($access_plugin->access($this->normal_user));
  115. $access_plugin->options['access'] = TRUE;
  116. $this->assertFalse($access_plugin->access($this->normal_user));
  117. $view->set_arguments(array($argument1, $argument2));
  118. $this->assertTrue($access_plugin->access($this->normal_user));
  119. // FALSE comes from hook_menu caching.
  120. $expected_hook_menu = array(
  121. 'views_test_test_dynamic_access_callback', array(FALSE, 1, 2)
  122. );
  123. $hook_menu = $view->execute_hook_menu('page_1');
  124. $this->assertEqual($expected_hook_menu, $hook_menu['test_access_dynamic']['access arguments'][0]);
  125. $expected_hook_menu = array(
  126. 'views_test_test_dynamic_access_callback', array(TRUE, 1, 2)
  127. );
  128. $this->assertTrue(views_access($expected_hook_menu, $argument1, $argument2));
  129. }
  130. function view_access_none() {
  131. $view = new view;
  132. $view->name = 'test_access_none';
  133. $view->description = '';
  134. $view->tag = '';
  135. $view->view_php = '';
  136. $view->base_table = 'node';
  137. $view->is_cacheable = FALSE;
  138. $view->api_version = 2;
  139. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  140. /* Display: Master */
  141. $handler = $view->new_display('default', 'Master', 'default');
  142. $handler->display->display_options['access']['type'] = 'none';
  143. $handler->display->display_options['cache']['type'] = 'none';
  144. $handler->display->display_options['exposed_form']['type'] = 'basic';
  145. $handler->display->display_options['pager']['type'] = 'full';
  146. $handler->display->display_options['style_plugin'] = 'default';
  147. $handler->display->display_options['row_plugin'] = 'fields';
  148. return $view;
  149. }
  150. function view_access_perm() {
  151. $view = new view;
  152. $view->name = 'test_access_perm';
  153. $view->description = '';
  154. $view->tag = '';
  155. $view->view_php = '';
  156. $view->base_table = 'node';
  157. $view->is_cacheable = FALSE;
  158. $view->api_version = 2;
  159. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  160. /* Display: Master */
  161. $handler = $view->new_display('default', 'Master', 'default');
  162. $handler->display->display_options['access']['type'] = 'perm';
  163. $handler->display->display_options['access']['perm'] = 'views_test test permission';
  164. $handler->display->display_options['cache']['type'] = 'none';
  165. $handler->display->display_options['exposed_form']['type'] = 'basic';
  166. $handler->display->display_options['pager']['type'] = 'full';
  167. $handler->display->display_options['style_plugin'] = 'default';
  168. $handler->display->display_options['row_plugin'] = 'fields';
  169. return $view;
  170. }
  171. function view_access_role() {
  172. $view = new view;
  173. $view->name = 'test_access_role';
  174. $view->description = '';
  175. $view->tag = '';
  176. $view->view_php = '';
  177. $view->base_table = 'node';
  178. $view->is_cacheable = FALSE;
  179. $view->api_version = 2;
  180. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  181. /* Display: Master */
  182. $handler = $view->new_display('default', 'Master', 'default');
  183. $handler->display->display_options['access']['type'] = 'role';
  184. $handler->display->display_options['access']['role'] = array(
  185. $this->normal_role => $this->normal_role,
  186. );
  187. $handler->display->display_options['cache']['type'] = 'none';
  188. $handler->display->display_options['exposed_form']['type'] = 'basic';
  189. $handler->display->display_options['pager']['type'] = 'full';
  190. $handler->display->display_options['style_plugin'] = 'default';
  191. $handler->display->display_options['row_plugin'] = 'fields';
  192. return $view;
  193. }
  194. function view_access_dynamic() {
  195. $view = new view;
  196. $view->name = 'test_access_dynamic';
  197. $view->description = '';
  198. $view->tag = '';
  199. $view->view_php = '';
  200. $view->base_table = 'node';
  201. $view->is_cacheable = FALSE;
  202. $view->api_version = 2;
  203. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  204. /* Display: Master */
  205. $handler = $view->new_display('default', 'Master', 'default');
  206. $handler->display->display_options['access']['type'] = 'test_dynamic';
  207. $handler->display->display_options['cache']['type'] = 'none';
  208. $handler->display->display_options['exposed_form']['type'] = 'basic';
  209. $handler->display->display_options['pager']['type'] = 'full';
  210. $handler->display->display_options['style_plugin'] = 'default';
  211. $handler->display->display_options['row_plugin'] = 'fields';
  212. $handler = $view->new_display('page', 'Page', 'page_1');
  213. $handler->display->display_options['path'] = 'test_access_dynamic';
  214. return $view;
  215. }
  216. function view_access_static() {
  217. $view = new view;
  218. $view->name = 'test_access_static';
  219. $view->description = '';
  220. $view->tag = '';
  221. $view->view_php = '';
  222. $view->base_table = 'node';
  223. $view->is_cacheable = FALSE;
  224. $view->api_version = 2;
  225. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  226. /* Display: Master */
  227. $handler = $view->new_display('default', 'Master', 'default');
  228. $handler->display->display_options['access']['type'] = 'test_static';
  229. $handler->display->display_options['cache']['type'] = 'none';
  230. $handler->display->display_options['exposed_form']['type'] = 'basic';
  231. $handler->display->display_options['pager']['type'] = 'full';
  232. $handler->display->display_options['style_plugin'] = 'default';
  233. $handler->display->display_options['row_plugin'] = 'fields';
  234. $handler = $view->new_display('page', 'Page', 'page_1');
  235. $handler->display->display_options['path'] = 'test_access_static';
  236. return $view;
  237. }
  238. }