Projet

Général

Profil

Paste
Télécharger (6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / nodeaccess / tests / nodeaccess_role.test @ c2ac6d1d

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for the nodeaccess module.
6
 */
7

    
8
/**
9
 * Tests the functionality of the nodeaccess module.
10
 */
11
class NodeaccesssRoleTestCase extends DrupalWebTestCase {
12

    
13
  protected $node;
14

    
15
  protected $user;
16

    
17
  /**
18
   * Enable the nodeaccess module. Add type grant for pages. Rebuild perms.
19
   */
20
  public function setUp() {
21
    parent::setUp('nodeaccess');
22

    
23
    user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access content'));
24

    
25
    nodeaccess_add_type_grant('page');
26

    
27
    // Create a page node, authored by admin.
28
    $this->node = $this->drupalCreateNode(array('type' => 'page', 'uid' => 1));
29

    
30
    // Let's create a sample authenticated user with basic permissions.
31
    $this->user = $this->drupalCreateUser(array('access content'));
32

    
33
    node_access_rebuild();
34
  }
35

    
36
  /**
37
   * Provide some information about this test case.
38
   */
39
  public static function getInfo() {
40
    return array(
41
      'name' => 'Nodeaccess Role-based node visibility',
42
      'description' => 'Tests nodes are correctly hidden based on user roles.',
43
      'group' => 'Nodeaccess',
44
    );
45
  }
46

    
47
  /**
48
   * Test node access when there should be no access whatsoever.
49
   */
50
  public function testRoleNodeVisibilityNoAccess() {
51
    $grants = array(
52
      array(
53
        'gid' => DRUPAL_ANONYMOUS_RID,
54
        'realm' => 'nodeaccess_rid',
55
        'grant_view' => 0,
56
        'grant_update' => 0,
57
        'grant_delete' => 0,
58
      ),
59
      array(
60
        'gid' => DRUPAL_AUTHENTICATED_RID,
61
        'realm' => 'nodeaccess_rid',
62
        'grant_view' => 0,
63
        'grant_update' => 0,
64
        'grant_delete' => 0,
65
      ),
66
    );
67

    
68
    $this->checkRoleGrantAccesses($grants);
69

    
70
  }
71

    
72
  /**
73
   * Test node access when only Authenticated users should have view access.
74
   */
75
  public function testRoleNodeVisibilityViewAuthenticatedOnly() {
76
    $grants = array(
77
      array(
78
        'gid' => DRUPAL_ANONYMOUS_RID,
79
        'realm' => 'nodeaccess_rid',
80
        'grant_view' => 0,
81
        'grant_update' => 0,
82
        'grant_delete' => 0,
83
      ),
84
      array(
85
        'gid' => DRUPAL_AUTHENTICATED_RID,
86
        'realm' => 'nodeaccess_rid',
87
        'grant_view' => 1,
88
        'grant_update' => 0,
89
        'grant_delete' => 0,
90
      ),
91
    );
92

    
93
    nodeaccess_set_grants($this->node, $grants);
94

    
95
    // Run all tests from grant.
96
    $this->checkRoleGrantAccesses($grants);
97

    
98
  }
99

    
100
  /**
101
   * Test node access when authenticated only should have view/edit perms.
102
   */
103
  public function testRoleNodeVisibilityViewEditAuthenticatedOnly() {
104
    $grants = array(
105
      array(
106
        'gid' => DRUPAL_ANONYMOUS_RID,
107
        'realm' => 'nodeaccess_rid',
108
        'grant_view' => 0,
109
        'grant_update' => 0,
110
        'grant_delete' => 0,
111
      ),
112
      array(
113
        'gid' => DRUPAL_AUTHENTICATED_RID,
114
        'realm' => 'nodeaccess_rid',
115
        'grant_view' => 1,
116
        'grant_update' => 1,
117
        'grant_delete' => 0,
118
      ),
119
    );
120

    
121
    nodeaccess_set_grants($this->node, $grants);
122

    
123
    // Run all tests from grant.
124
    $this->checkRoleGrantAccesses($grants);
125

    
126
  }
127

    
128
  /**
129
   * Test node access when authenticated only should have full access.
130
   */
131
  public function testRoleNodeVisibilityAllAuthenticatedOnly() {
132
    $grants = array(
133
      array(
134
        'gid' => DRUPAL_ANONYMOUS_RID,
135
        'realm' => 'nodeaccess_rid',
136
        'grant_view' => 0,
137
        'grant_update' => 0,
138
        'grant_delete' => 0,
139
      ),
140
      array(
141
        'gid' => DRUPAL_AUTHENTICATED_RID,
142
        'realm' => 'nodeaccess_rid',
143
        'grant_view' => 1,
144
        'grant_update' => 1,
145
        'grant_delete' => 1,
146
      ),
147
    );
148

    
149
    nodeaccess_set_grants($this->node, $grants);
150

    
151
    // Run all tests from grant.
152
    $this->checkRoleGrantAccesses($grants);
153

    
154
  }
155

    
156
  /**
157
   * Test node access when anon and authenticated users should have full access.
158
   */
159
  public function testRoleNodeVisibilityFullAccess() {
160
    $grants = array(
161
      array(
162
        'gid' => DRUPAL_ANONYMOUS_RID,
163
        'realm' => 'nodeaccess_rid',
164
        'grant_view' => 1,
165
        'grant_update' => 1,
166
        'grant_delete' => 1,
167
      ),
168
      array(
169
        'gid' => DRUPAL_AUTHENTICATED_RID,
170
        'realm' => 'nodeaccess_rid',
171
        'grant_view' => 1,
172
        'grant_update' => 1,
173
        'grant_delete' => 1,
174
      ),
175
    );
176

    
177
    nodeaccess_set_grants($this->node, $grants);
178

    
179
    // Run all tests from grant.
180
    $this->checkRoleGrantAccesses($grants);
181

    
182

    
183
  }
184

    
185
  /**
186
   * This method will check the access for the node based on grants supplied.
187
   *
188
   * @param array $grants
189
   *  Array of role grants to check.
190
   */
191
  protected function checkRoleGrantAccesses($grants = array()) {
192
    // Anonymous user should not be able to access this regular node.
193

    
194
    foreach ($grants as $grant) {
195
      $logout = FALSE;
196
      if (!empty($grant['realm']) && $grant['realm'] == 'nodeaccess_rid') {
197
        if (isset($grant['gid']) && $grant['gid'] == DRUPAL_AUTHENTICATED_RID) {
198
          $this->drupalLogin($this->user);
199
          $logout = TRUE;
200
        }
201

    
202
        // Check View Access
203
        $this->drupalGet("node/{$this->node->nid}");
204
        if (!empty($grant['grant_view']) && $grant['grant_view']) {
205
          $this->assertResponse(200, 'User is allowed to view the content.');
206
        }
207
        else {
208
          $this->assertResponse(403, 'User is not allowed to view the content.');
209
        }
210

    
211
        // Check Edit Access
212
        $this->drupalGet("node/{$this->node->nid}/edit");
213
        if (!empty($grant['grant_update']) && $grant['grant_update']) {
214
          $this->assertResponse(200, 'User is allowed to edit the content.');
215
        }
216
        else {
217
          $this->assertResponse(403, 'User is not allowed to edit the content.');
218
        }
219

    
220
        // Check Delete Access
221
        $this->drupalGet("node/{$this->node->nid}/delete");
222
        if (!empty($grant['grant_delete']) && $grant['grant_delete']) {
223
          $this->assertResponse(200, 'User is allowed to delete the content.');
224
        }
225
        else {
226
          $this->assertResponse(403, 'User is not allowed to delete the content.');
227
        }
228

    
229
        if ($logout) {
230
          $this->drupalLogout();
231
        }
232
      }
233
    }
234
  }
235

    
236
}