root / drupal7 / sites / all / modules / content_access / tests / content_access_test_help.php @ 2cf41c1d
1 |
<?php
|
---|---|
2 |
|
3 |
/**
|
4 |
* @file
|
5 |
* Helper class with auxiliary functions for content access module tests
|
6 |
*/
|
7 |
|
8 |
class ContentAccessTestCase extends DrupalWebTestCase { |
9 |
|
10 |
var $test_user; |
11 |
var $rid; |
12 |
var $admin_user; |
13 |
var $content_type; |
14 |
var $url_content_type_name; |
15 |
var $node1; |
16 |
var $node2; |
17 |
|
18 |
/**
|
19 |
* Preparation work that is done before each test.
|
20 |
* Test users, content types, nodes etc. are created.
|
21 |
*/
|
22 |
function setUp($module = '') { |
23 |
if (empty($module)) { |
24 |
// Enable content access module
|
25 |
parent::setUp('content_access'); |
26 |
} |
27 |
else {
|
28 |
// Enable content access module plus another module
|
29 |
parent::setUp('content_access', $module); |
30 |
// Stop setup when module could not be enabled
|
31 |
if (!module_exists($module)) { |
32 |
$this->pass('No ' . $module . ' module present, skipping test'); |
33 |
return;
|
34 |
} |
35 |
} |
36 |
|
37 |
// Create test user with seperate role
|
38 |
$this->test_user = $this->drupalCreateUser(); |
39 |
|
40 |
// Get the value of the new role
|
41 |
// Needed in D7 because it's by default create two roles for new users
|
42 |
// one role is Authenticated and the second is new default one
|
43 |
// @see drupalCreateUser()
|
44 |
foreach ($this->test_user->roles as $rid => $role) { |
45 |
if (!in_array($rid, array(DRUPAL_AUTHENTICATED_RID))) { |
46 |
$this->rid = $rid; |
47 |
break;
|
48 |
} |
49 |
} |
50 |
|
51 |
// Create admin user
|
52 |
$this->admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'grant content access', 'grant own content access', 'administer nodes', 'access administration pages')); |
53 |
$this->drupalLogin($this->admin_user); |
54 |
|
55 |
// Rebuild content access permissions
|
56 |
node_access_rebuild(); |
57 |
|
58 |
// Create test content type
|
59 |
$this->content_type = $this->drupalCreateContentType(); |
60 |
} |
61 |
|
62 |
/**
|
63 |
* Change access permissions for a content type
|
64 |
*/
|
65 |
function changeAccessContentType($access_settings) { |
66 |
$this->drupalPost('admin/structure/types/manage/'. $this->content_type->type .'/access', $access_settings, t('Submit')); |
67 |
$this->assertText(t('Your changes have been saved.'), 'access rules of content type were updated successfully'); |
68 |
} |
69 |
|
70 |
/**
|
71 |
* Change access permissions for a content type by a given keyword (view, update or delete)
|
72 |
* for the role of the user
|
73 |
*/
|
74 |
function changeAccessContentTypeKeyword($keyword, $access = TRUE, $user = NULL) { |
75 |
if ($user === NULL) { |
76 |
$user = $this->test_user; |
77 |
$roles[$this->rid] = $user->roles[$this->rid]; |
78 |
} else {
|
79 |
foreach ($user->roles as $rid => $role) { |
80 |
if (!in_array($rid, array(DRUPAL_AUTHENTICATED_RID))) { |
81 |
$roles[$rid] = $user->roles[$rid]; |
82 |
break;
|
83 |
} |
84 |
} |
85 |
} |
86 |
|
87 |
$access_settings = array( |
88 |
$keyword .'['. key($roles) .']' => $access, |
89 |
); |
90 |
|
91 |
$this->changeAccessContentType($access_settings); |
92 |
} |
93 |
|
94 |
/**
|
95 |
* Change the per node access setting for a content type
|
96 |
*/
|
97 |
function changeAccessPerNode($access = TRUE) { |
98 |
$access_permissions = array( |
99 |
'per_node' => $access, |
100 |
); |
101 |
$this->changeAccessContentType($access_permissions); |
102 |
} |
103 |
|
104 |
/**
|
105 |
* Change access permissions for a node by a given keyword (view, update or delete)
|
106 |
*/
|
107 |
function changeAccessNodeKeyword($node, $keyword, $access = TRUE) { |
108 |
$user = $this->test_user; |
109 |
$roles[$this->rid] = $user->roles[$this->rid]; |
110 |
|
111 |
$access_settings = array( |
112 |
$keyword .'['. key($roles) .']' => $access, |
113 |
); |
114 |
|
115 |
$this->changeAccessNode($node, $access_settings); |
116 |
} |
117 |
|
118 |
/**
|
119 |
* Change access permission for a node
|
120 |
*/
|
121 |
function changeAccessNode($node, $access_settings) { |
122 |
$this->drupalPost('node/'. $node->nid .'/access', $access_settings, t('Submit')); |
123 |
$this->assertText(t('Your changes have been saved.'), 'access rules of node were updated successfully'); |
124 |
} |
125 |
} |