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