views_access.test
9.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?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'
);
}
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;
}
}