Projet

Général

Profil

Paste
Télécharger (11,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / tests / views_access.test @ 4003efde

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
  /**
21
   * {@inheritdoc}
22
   */
23
  public function setUp(array $modules = array()) {
24
    parent::setUp($modules);
25

    
26
    $this->admin_user = $this->drupalCreateUser(array('access all views'));
27
    $this->web_user = $this->drupalCreateUser();
28
    $this->web_role = current($this->web_user->roles);
29

    
30
    $this->normal_role = $this->drupalCreateRole(array());
31
    $this->normal_user = $this->drupalCreateUser(array('views_test test permission'));
32
    $this->normal_user->roles[$this->normal_role] = $this->normal_role;
33
    // Reset the plugin data.
34
    views_fetch_plugin_data(NULL, NULL, TRUE);
35
  }
36

    
37
  function viewsPlugins() {
38
    $plugins = array(
39
      'access' =>  array(
40
        'test_static' => array(
41
          'title' => t('Static test access plugin'),
42
          'help' => t('Provides a static test access plugin.'),
43
          'handler' => 'views_test_plugin_access_test_static',
44
          'path' => drupal_get_path('module', 'views_test') . '/test_plugins',
45
        ),
46
        'test_dynamic' => array(
47
          'title' => t('Dynamic test access plugin'),
48
          'help' => t('Provides a dynamic test access plugin.'),
49
          'handler' => 'views_test_plugin_access_test_dynamic',
50
          'path' => drupal_get_path('module', 'views_test') . '/test_plugins',
51
        ),
52
      ),
53
    );
54

    
55
    return $plugins;
56
  }
57

    
58
  /**
59
   * Tests none access plugin.
60
   */
61
  function testAccessNone() {
62
    $view = $this->view_access_none();
63

    
64
    $view->set_display('default');
65

    
66
    $this->assertTrue($view->display_handler->access($this->admin_user), t('Admin-Account should be able to access the view everytime'));
67
    $this->assertTrue($view->display_handler->access($this->web_user));
68
    $this->assertTrue($view->display_handler->access($this->normal_user));
69
  }
70

    
71
  /**
72
   * Tests perm access plugin.
73
   */
74
  function testAccessPerm() {
75
    $view = $this->view_access_perm();
76

    
77
    $view->set_display('default');
78
    $access_plugin = $view->display_handler->get_plugin('access');
79

    
80
    $this->assertTrue($view->display_handler->access($this->admin_user), t('Admin-Account should be able to access the view everytime'));
81
    $this->assertFalse($view->display_handler->access($this->web_user));
82
    $this->assertTrue($view->display_handler->access($this->normal_user));
83
  }
84

    
85
  /**
86
   * Tests role access plugin.
87
   */
88
  function testAccessRole() {
89
    $view = $this->view_access_role();
90

    
91
    $view->set_display('default');
92
    $access_plugin = $view->display_handler->get_plugin('access');
93

    
94
    $this->assertTrue($view->display_handler->access($this->admin_user), t('Admin-Account should be able to access the view everytime'));
95
    $this->assertFalse($view->display_handler->access($this->web_user));
96
    $this->assertTrue($view->display_handler->access($this->normal_user));
97
  }
98

    
99
  /**
100
   * @todo Test abstract access plugin.
101
   */
102

    
103
  /**
104
   * Tests static access check.
105
   */
106
  function testStaticAccessPlugin() {
107
    $view = $this->view_access_static();
108

    
109
    $view->set_display('default');
110
    $access_plugin = $view->display_handler->get_plugin('access');
111

    
112
    $this->assertFalse($access_plugin->access($this->normal_user));
113

    
114
    $access_plugin->options['access'] = TRUE;
115
    $this->assertTrue($access_plugin->access($this->normal_user));
116

    
117
    // FALSE comes from hook_menu caching.
118
    $expected_hook_menu = array(
119
      'views_test_test_static_access_callback', array(FALSE)
120
    );
121
    $hook_menu = $view->execute_hook_menu('page_1');
122
    $this->assertEqual($expected_hook_menu, $hook_menu['test_access_static']['access arguments'][0]);
123

    
124
    $expected_hook_menu = array(
125
      'views_test_test_static_access_callback', array(TRUE)
126
    );
127
    $this->assertTrue(views_access($expected_hook_menu));
128
  }
129

    
130
  /**
131
   * Tests dynamic access plugin.
132
   */
133
  function testDynamicAccessPlugin() {
134
    $view = $this->view_access_dynamic();
135
    $argument1 = $this->randomName();
136
    $argument2 = $this->randomName();
137
    variable_set('test_dynamic_access_argument1', $argument1);
138
    variable_set('test_dynamic_access_argument2', $argument2);
139

    
140
    $view->set_display('default');
141
    $access_plugin = $view->display_handler->get_plugin('access');
142

    
143
    $this->assertFalse($access_plugin->access($this->normal_user));
144

    
145
    $access_plugin->options['access'] = TRUE;
146
    $this->assertFalse($access_plugin->access($this->normal_user));
147

    
148
    $view->set_arguments(array($argument1, $argument2));
149
    $this->assertTrue($access_plugin->access($this->normal_user));
150

    
151
    // FALSE comes from hook_menu caching.
152
    $expected_hook_menu = array(
153
      'views_test_test_dynamic_access_callback', array(FALSE, 1, 2)
154
    );
155
    $hook_menu = $view->execute_hook_menu('page_1');
156
    $this->assertEqual($expected_hook_menu, $hook_menu['test_access_dynamic']['access arguments'][0]);
157

    
158
    $expected_hook_menu = array(
159
      'views_test_test_dynamic_access_callback', array(TRUE, 1, 2)
160
    );
161
    $this->assertTrue(views_access($expected_hook_menu, $argument1, $argument2));
162
  }
163

    
164
  /**
165
   * Tests access for a view with a missing access plugin.
166
   */
167
  public function testMissingAccessPlugin() {
168
    $view = $this->getMissingAccessPluginTestView();
169

    
170
    $view->set_display('default');
171
    $access_plugin = $view->display_handler->get_plugin('access');
172
    $this->assertFalse($access_plugin);
173

    
174
    $this->assertTrue($view->display_handler->access($this->admin_user), t('Admin-Account should be able to access the view everytime'));
175
    $this->assertTrue($view->display_handler->access($this->web_user));
176
    $this->assertTrue($view->display_handler->access($this->normal_user));
177

    
178
    $hook_menu = $view->execute_hook_menu('page_1');
179
    $this->assertTrue($hook_menu['test_access_missing']['access arguments'][0]);
180

    
181
    $this->assertTrue(views_access(TRUE));
182
  }
183

    
184
  function view_access_none() {
185
    $view = new view;
186
    $view->name = 'test_access_none';
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'] = 'none';
198
    $handler->display->display_options['cache']['type'] = 'none';
199
    $handler->display->display_options['exposed_form']['type'] = 'basic';
200
    $handler->display->display_options['pager']['type'] = 'full';
201
    $handler->display->display_options['style_plugin'] = 'default';
202
    $handler->display->display_options['row_plugin'] = 'fields';
203

    
204
    return $view;
205
  }
206

    
207
  function view_access_perm() {
208
    $view = new view;
209
    $view->name = 'test_access_perm';
210
    $view->description = '';
211
    $view->tag = '';
212
    $view->view_php = '';
213
    $view->base_table = 'node';
214
    $view->is_cacheable = FALSE;
215
    $view->api_version = 2;
216
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
217

    
218
    /* Display: Master */
219
    $handler = $view->new_display('default', 'Master', 'default');
220
    $handler->display->display_options['access']['type'] = 'perm';
221
    $handler->display->display_options['access']['perm'] = 'views_test test permission';
222
    $handler->display->display_options['cache']['type'] = 'none';
223
    $handler->display->display_options['exposed_form']['type'] = 'basic';
224
    $handler->display->display_options['pager']['type'] = 'full';
225
    $handler->display->display_options['style_plugin'] = 'default';
226
    $handler->display->display_options['row_plugin'] = 'fields';
227

    
228
    return $view;
229
  }
230

    
231
  function view_access_role() {
232
    $view = new view;
233
    $view->name = 'test_access_role';
234
    $view->description = '';
235
    $view->tag = '';
236
    $view->view_php = '';
237
    $view->base_table = 'node';
238
    $view->is_cacheable = FALSE;
239
    $view->api_version = 2;
240
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
241

    
242
    /* Display: Master */
243
    $handler = $view->new_display('default', 'Master', 'default');
244
    $handler->display->display_options['access']['type'] = 'role';
245
    $handler->display->display_options['access']['role'] = array(
246
      $this->normal_role => $this->normal_role,
247
    );
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
    return $view;
255
  }
256

    
257
  function view_access_dynamic() {
258
    $view = new view;
259
    $view->name = 'test_access_dynamic';
260
    $view->description = '';
261
    $view->tag = '';
262
    $view->view_php = '';
263
    $view->base_table = 'node';
264
    $view->is_cacheable = FALSE;
265
    $view->api_version = 2;
266
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
267

    
268
    /* Display: Master */
269
    $handler = $view->new_display('default', 'Master', 'default');
270
    $handler->display->display_options['access']['type'] = 'test_dynamic';
271
    $handler->display->display_options['cache']['type'] = 'none';
272
    $handler->display->display_options['exposed_form']['type'] = 'basic';
273
    $handler->display->display_options['pager']['type'] = 'full';
274
    $handler->display->display_options['style_plugin'] = 'default';
275
    $handler->display->display_options['row_plugin'] = 'fields';
276

    
277
    $handler = $view->new_display('page', 'Page', 'page_1');
278
    $handler->display->display_options['path'] = 'test_access_dynamic';
279

    
280
    return $view;
281
  }
282

    
283
  function view_access_static() {
284
    $view = new view;
285
    $view->name = 'test_access_static';
286
    $view->description = '';
287
    $view->tag = '';
288
    $view->view_php = '';
289
    $view->base_table = 'node';
290
    $view->is_cacheable = FALSE;
291
    $view->api_version = 2;
292
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
293

    
294
    /* Display: Master */
295
    $handler = $view->new_display('default', 'Master', 'default');
296
    $handler->display->display_options['access']['type'] = 'test_static';
297
    $handler->display->display_options['cache']['type'] = 'none';
298
    $handler->display->display_options['exposed_form']['type'] = 'basic';
299
    $handler->display->display_options['pager']['type'] = 'full';
300
    $handler->display->display_options['style_plugin'] = 'default';
301
    $handler->display->display_options['row_plugin'] = 'fields';
302

    
303
    $handler = $view->new_display('page', 'Page', 'page_1');
304
    $handler->display->display_options['path'] = 'test_access_static';
305

    
306
    return $view;
307
  }
308

    
309
  /**
310
   * Generates a view with an access plugin that doesn't exist.
311
   */
312
  protected function getMissingAccessPluginTestView() {
313
    $view = new view();
314
    $view->name = 'test_access_missing';
315
    $view->description = '';
316
    $view->tag = '';
317
    $view->view_php = '';
318
    $view->base_table = 'node';
319
    $view->is_cacheable = FALSE;
320
    $view->api_version = 2;
321
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
322

    
323
    /* Display: Master */
324
    $handler = $view->new_display('default', 'Master', 'default');
325
    $handler->display->display_options['access']['type'] = 'does_not_exist';
326
    $handler->display->display_options['cache']['type'] = 'none';
327
    $handler->display->display_options['exposed_form']['type'] = 'basic';
328
    $handler->display->display_options['pager']['type'] = 'full';
329
    $handler->display->display_options['style_plugin'] = 'default';
330
    $handler->display->display_options['row_plugin'] = 'fields';
331

    
332
    $handler = $view->new_display('page', 'Page', 'page_1');
333
    $handler->display->display_options['path'] = 'test_access_missing';
334

    
335
    return $view;
336
  }
337

    
338
}