Project

General

Profile

Paste
Download (9.91 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / tests / views_access.test @ 6de61b86

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() {
24
    parent::setUp();
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
  function view_access_none() {
165
    $view = new view;
166
    $view->name = 'test_access_none';
167
    $view->description = '';
168
    $view->tag = '';
169
    $view->view_php = '';
170
    $view->base_table = 'node';
171
    $view->is_cacheable = FALSE;
172
    $view->api_version = 2;
173
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
174

    
175
    /* Display: Master */
176
    $handler = $view->new_display('default', 'Master', 'default');
177
    $handler->display->display_options['access']['type'] = 'none';
178
    $handler->display->display_options['cache']['type'] = 'none';
179
    $handler->display->display_options['exposed_form']['type'] = 'basic';
180
    $handler->display->display_options['pager']['type'] = 'full';
181
    $handler->display->display_options['style_plugin'] = 'default';
182
    $handler->display->display_options['row_plugin'] = 'fields';
183

    
184
    return $view;
185
  }
186

    
187
  function view_access_perm() {
188
    $view = new view;
189
    $view->name = 'test_access_perm';
190
    $view->description = '';
191
    $view->tag = '';
192
    $view->view_php = '';
193
    $view->base_table = 'node';
194
    $view->is_cacheable = FALSE;
195
    $view->api_version = 2;
196
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
197

    
198
    /* Display: Master */
199
    $handler = $view->new_display('default', 'Master', 'default');
200
    $handler->display->display_options['access']['type'] = 'perm';
201
    $handler->display->display_options['access']['perm'] = 'views_test test permission';
202
    $handler->display->display_options['cache']['type'] = 'none';
203
    $handler->display->display_options['exposed_form']['type'] = 'basic';
204
    $handler->display->display_options['pager']['type'] = 'full';
205
    $handler->display->display_options['style_plugin'] = 'default';
206
    $handler->display->display_options['row_plugin'] = 'fields';
207

    
208
    return $view;
209
  }
210

    
211
  function view_access_role() {
212
    $view = new view;
213
    $view->name = 'test_access_role';
214
    $view->description = '';
215
    $view->tag = '';
216
    $view->view_php = '';
217
    $view->base_table = 'node';
218
    $view->is_cacheable = FALSE;
219
    $view->api_version = 2;
220
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
221

    
222
    /* Display: Master */
223
    $handler = $view->new_display('default', 'Master', 'default');
224
    $handler->display->display_options['access']['type'] = 'role';
225
    $handler->display->display_options['access']['role'] = array(
226
      $this->normal_role => $this->normal_role,
227
    );
228
    $handler->display->display_options['cache']['type'] = 'none';
229
    $handler->display->display_options['exposed_form']['type'] = 'basic';
230
    $handler->display->display_options['pager']['type'] = 'full';
231
    $handler->display->display_options['style_plugin'] = 'default';
232
    $handler->display->display_options['row_plugin'] = 'fields';
233

    
234
    return $view;
235
  }
236

    
237
  function view_access_dynamic() {
238
    $view = new view;
239
    $view->name = 'test_access_dynamic';
240
    $view->description = '';
241
    $view->tag = '';
242
    $view->view_php = '';
243
    $view->base_table = 'node';
244
    $view->is_cacheable = FALSE;
245
    $view->api_version = 2;
246
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
247

    
248
    /* Display: Master */
249
    $handler = $view->new_display('default', 'Master', 'default');
250
    $handler->display->display_options['access']['type'] = 'test_dynamic';
251
    $handler->display->display_options['cache']['type'] = 'none';
252
    $handler->display->display_options['exposed_form']['type'] = 'basic';
253
    $handler->display->display_options['pager']['type'] = 'full';
254
    $handler->display->display_options['style_plugin'] = 'default';
255
    $handler->display->display_options['row_plugin'] = 'fields';
256

    
257
    $handler = $view->new_display('page', 'Page', 'page_1');
258
    $handler->display->display_options['path'] = 'test_access_dynamic';
259

    
260
    return $view;
261
  }
262

    
263
  function view_access_static() {
264
    $view = new view;
265
    $view->name = 'test_access_static';
266
    $view->description = '';
267
    $view->tag = '';
268
    $view->view_php = '';
269
    $view->base_table = 'node';
270
    $view->is_cacheable = FALSE;
271
    $view->api_version = 2;
272
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
273

    
274
    /* Display: Master */
275
    $handler = $view->new_display('default', 'Master', 'default');
276
    $handler->display->display_options['access']['type'] = 'test_static';
277
    $handler->display->display_options['cache']['type'] = 'none';
278
    $handler->display->display_options['exposed_form']['type'] = 'basic';
279
    $handler->display->display_options['pager']['type'] = 'full';
280
    $handler->display->display_options['style_plugin'] = 'default';
281
    $handler->display->display_options['row_plugin'] = 'fields';
282

    
283
    $handler = $view->new_display('page', 'Page', 'page_1');
284
    $handler->display->display_options['path'] = 'test_access_static';
285

    
286
    return $view;
287
  }
288
}