block_class.test
7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* @file
* Test the Block Class module.
*/
/**
* Provides common functionality for the Block Class test classes.
*/
class BlockClassTestCase extends DrupalWebTestCase {
/**
* User object to perform site browsing
* @var object
*/
protected $privilegedUser;
/**
* Machine name of the module providing the block coupled with delta.
* @var string
*/
protected $module = 'system';
/**
* Block delta as provided by its module.
* @var string
*/
protected $delta = 'main';
/**
* Permissions required by the user to perform the tests.
* @var array
*/
protected $permissions = array(
'administer blocks',
'administer block classes',
);
/**
* Enable modules and create user with specific permissions.
*
* By default Test Cases are carried on the "Main page content" Block.
*/
public function setUp() {
// Merge inherited classes modules, see FieldUITestCase for an example.
$modules = func_get_args();
if (isset($modules[0]) && is_array($modules[0])) {
$modules = $modules[0];
}
$modules[] = 'block_class';
parent::setUp($modules);
// Authenticate test user.
$this->privilegedUser = $this->drupalCreateUser($this->permissions);
$this->drupalLogin($this->privilegedUser);
}
/**
* Update Block CSS class and assert whether it is found when displayed.
*
* @param bool $anon
* (optional) Set to TRUE to view block with anon user, defaults to TRUE.
* @param string $module
* (optional) Machine name of the module Defaults to
* $this->module if set to NULL.
* @param string $delta
* (optional) Block delta as provided by its module. Defaults to
* $this->delta if set to NULL.
*/
public function assertUpdateBlockClass($anon = FALSE, $module = NULL, $delta = NULL) {
// Initialize $module and $delta by default if no value is provided.
if (!isset($module)) {
$module = $this->module;
}
if (!isset($delta)) {
$delta = $this->delta;
}
// Test with three random class names.
$css_classes = implode(' ', array(
$this->randomName(8),
$this->randomName(8),
$this->randomName(8),
));
// Update Block CSS class field.
$this->drupalPost("admin/structure/block/manage/$module/$delta/configure", array('css_class' => $css_classes), t('Save block'));
// Check Block configuration was saved successfully.
$this->assertText(t('The block configuration has been saved.'));
// Browse to the homepage.
$this->drupalGet('');
// Log out if the test is for anonymous user.
if ($anon) {
$this->drupalLogout();
// Browse back to the homepage.
$this->drupalGet('');
}
// Check if the Block CSS classes could be found.
$this->assertPattern('/class=\"(.*?)' . $css_classes . '(.*?)\"/', format_string('The CSS classes were found: @css_classes', array('@css_classes' => $css_classes)));
// Login again after testing with the anonumous user.
if ($anon) {
$this->drupalLogin($this->privilegedUser);
}
}
}
/**
* Test the update and display of the CSS class for a Block.
*/
class BlockClassUpdateDisplayTestCase extends BlockClassTestCase {
/**
* Implements DrupalWebTestCase::getInfo().
*/
public static function getInfo() {
return array(
'name' => 'Block CSS class update and display',
'description' => 'Test the update of a Block CSS class field and the display for the Main Page Content Block.',
'group' => 'Block Class',
);
}
/**
* Update and display a Block multiple times to ensure CSS class is found.
*
* A Block is updated and displayed several times and with logged in or
* anonymous user, with Block cache turned enabled or disabled.
*/
public function testUpdateDisplayClass() {
// Edit the block, change the class and check if the CSS classes are found.
$this->assertUpdateBlockClass();
// Now, turn on caching programmatically and set it to 15 min expiry.
variable_set('block_cache', TRUE);
variable_set('cache_lifetime', 900);
variable_set('page_cache_maximum_age', 900);
// Edit the block, change the class and check with the anonymous user.
$this->assertUpdateBlockClass(TRUE);
// Edit the block, change the class and check with the anonymous user.
$this->assertUpdateBlockClass(TRUE);
}
}
/**
* Test Block Class permissions.
*/
class BlockClassPermissionTestCase extends BlockClassTestCase {
/**
* Implements DrupalWebTestCase::getInfo().
*/
public static function getInfo() {
return array(
'name' => 'Administer block classes permission',
'description' => 'Test the permission added by the module to administer block classes.',
'group' => 'Block Class',
);
}
/**
* Enable modules and create user with specific permissions.
*/
public function setUp() {
// Remove the 'administer block classes' permission from the base class.
$this->permissions = array('administer blocks');
parent::setUp();
}
/**
* Ensure Block CSS classes field is only visible with the right permissions.
*
* Test if a user without 'administer block classes' permission has access to
* the Block CSS classes field on the block configuration page.
*/
public function testPermission() {
// Browse to the "Main page content" block editing form page.
$this->drupalGet("admin/structure/block/manage/{$this->module}/{$this->delta}/configure");
// Check that the css_class field couldn't be found.
// If the field is not found, it can't be submitted through drupalPost.
$this->assertNoFieldById('css_class', 'The Css classes field was not found on the page.');
}
}
/**
* Test Block Class integration with Context.
*/
class BlockClassContextTestCase extends BlockClassUpdateDisplayTestCase {
/**
* Implements DrupalWebTestCase::getInfo().
*/
public static function getInfo() {
return array(
'name' => 'Integration with Context',
'description' => 'Test the integration of Block Class with the Context module and the update/display of a Block CSS class.',
// Include required contributed modules context and ctools for the test.
'dependencies' => array('context', 'ctools'),
'group' => 'Block Class',
);
}
/**
* Enable modules and create user with specific permissions.
*/
public function setUp() {
// Override default module and delta to test with the "Who's online" block.
$this->module = 'user';
$this->delta = 'online';
// Include the Context module and its dependencies to be loaded.
parent::setUp('context', 'ctools');
// Initialize a test context with the test block.
$this->initializeContext();
}
/**
* Helper function to initialize a test Context with a test block.
*/
public function initializeContext() {
// Import a basic context exported through the admin interface.
$context = new stdClass();
$context->disabled = FALSE;
$context->api_version = 3;
$context->name = 'front';
$context->description = 'Frontpage Context';
$context->tag = '';
$context->conditions = array(
'path' => array(
'values' => array(
'<front>' => '<front>',
),
),
);
$context->reactions = array(
'block' => array(
'blocks' => array(
$this->module . '-' . $this->delta => array(
'module' => $this->module,
'delta' => $this->delta,
'region' => 'content',
'weight' => '-10',
),
),
),
);
$context->condition_mode = 0;
// Translatables
// Included for use with string extractors like potx.
t('Frontpage Context');
// Save the context.
context_save($context);
}
}