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