views_access.test 9.9 KB

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