Projet

Général

Profil

Paste
Télécharger (15,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / block_class / block_class.test @ ecb492a9

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
   *
16
   * @var object
17
   */
18
  protected $privilegedUser;
19

    
20
  /**
21
   * Machine name of the module providing the block coupled with delta.
22
   *
23
   * @var string
24
   */
25
  protected $module = 'system';
26

    
27
  /**
28
   * Block delta as provided by its module.
29
   *
30
   * @var string
31
   */
32
  protected $delta = 'main';
33

    
34
  /**
35
   * Permissions required by the user to perform the tests.
36
   *
37
   * @var array
38
   */
39
  protected $permissions = array(
40
    'administer blocks',
41
    'administer block classes',
42
  );
43

    
44
  /**
45
   * Enable modules and create user with specific permissions.
46
   *
47
   * By default Test Cases are carried on the "Main page content" Block.
48
   */
49
  public function setUp() {
50
    // Merge inherited classes modules, see FieldUITestCase for an example.
51
    $modules = func_get_args();
52
    if (isset($modules[0]) && is_array($modules[0])) {
53
      $modules = $modules[0];
54
    }
55
    $modules[] = 'block_class';
56
    parent::setUp($modules);
57

    
58
    // Authenticate test user.
59
    $this->privilegedUser = $this->drupalCreateUser($this->permissions);
60
    $this->drupalLogin($this->privilegedUser);
61
  }
62

    
63
  /**
64
   * Update Block CSS class and assert whether it is found when displayed.
65
   *
66
   * @param bool $anon
67
   *   (optional) Set to TRUE to view block with anon user, defaults to TRUE.
68
   * @param string $module
69
   *   (optional) Machine name of the module Defaults to
70
   *   $this->module if set to NULL.
71
   * @param string $delta
72
   *   (optional) Block delta as provided by its module. Defaults to
73
   *   $this->delta if set to NULL.
74
   */
75
  public function assertUpdateBlockClass($anon = FALSE, $module = NULL, $delta = NULL) {
76
    // Initialize $module and $delta by default if no value is provided.
77
    if (!isset($module)) {
78
      $module = $this->module;
79
    }
80
    if (!isset($delta)) {
81
      $delta = $this->delta;
82
    }
83
    // Test with three random class names.
84
    $css_classes = implode(' ', array(
85
      $this->randomName(8),
86
      $this->randomName(8),
87
      $this->randomName(8),
88
    ));
89
    // Update Block CSS class field.
90
    $this->drupalPost("admin/structure/block/manage/$module/$delta/configure", array('css_class' => $css_classes), t('Save block'));
91
    // Check Block configuration was saved successfully.
92
    $this->assertText(t('The block configuration has been saved.'));
93
    // Log out if the test is for anonymous user.
94
    if ($anon) {
95
      $this->drupalLogout();
96
    }
97
    // Browse to the homepage.
98
    $this->drupalGet('');
99
    // Check if the Block CSS classes could be found.
100
    $this->assertPattern('/class=\"(.*?)' . $css_classes . '(.*?)\"/', format_string('The CSS classes were found: @css_classes', array('@css_classes' => $css_classes)));
101
    // Login again after testing with the anonymous user.
102
    if ($anon) {
103
      $this->drupalLogin($this->privilegedUser);
104
    }
105
  }
106

    
107
}
108

    
109
/**
110
 * Test the update and display of the CSS class for a Block.
111
 */
112
class BlockClassUpdateDisplayTestCase extends BlockClassTestCase {
113

    
114
  /**
115
   * Implements DrupalWebTestCase::getInfo().
116
   */
117
  public static function getInfo() {
118
    return array(
119
      'name' => 'Block CSS class update and display',
120
      'description' => 'Test the update of a Block CSS class field and the display for the Main Page Content Block.',
121
      'group' => 'Block Class',
122
    );
123
  }
124

    
125
  /**
126
   * Update and display a Block multiple times to ensure CSS class is found.
127
   *
128
   * A Block is updated and displayed several times and with logged in or
129
   * anonymous user, with Block cache enabled or disabled.
130
   */
131
  public function testUpdateDisplayClass() {
132
    // Edit the block, change the class and check if the CSS classes are found.
133
    $this->assertUpdateBlockClass();
134

    
135
    // Now, turn on caching programmatically and set it to 15 min expiry.
136
    variable_set('block_cache', TRUE);
137
    variable_set('cache_lifetime', 900);
138
    variable_set('page_cache_maximum_age', 900);
139

    
140
    // Edit the block, change the class and check with the anonymous user.
141
    $this->assertUpdateBlockClass(TRUE);
142

    
143
    // Edit the block, change the class and check with the anonymous user.
144
    $this->assertUpdateBlockClass(TRUE);
145
  }
146

    
147
}
148

    
149
/**
150
 * Test Block Class permissions.
151
 */
152
class BlockClassPermissionTestCase extends BlockClassTestCase {
153

    
154
  /**
155
   * Implements DrupalWebTestCase::getInfo().
156
   */
157
  public static function getInfo() {
158
    return array(
159
      'name' => 'Administer block classes permission',
160
      'description' => 'Test the permission added by the module to administer block classes.',
161
      'group' => 'Block Class',
162
    );
163
  }
164

    
165
  /**
166
   * Enable modules and create user with specific permissions.
167
   */
168
  public function setUp() {
169
    // Remove the 'administer block classes' permission from the base class.
170
    $this->permissions = array('administer blocks');
171
    parent::setUp();
172
  }
173

    
174
  /**
175
   * Ensure Block CSS classes field is only visible with the right permissions.
176
   *
177
   * Test if a user without 'administer block classes' permission has access to
178
   * the Block CSS classes field on the block configuration page.
179
   */
180
  public function testPermission() {
181
    // Browse to the "Main page content" block editing form page.
182
    $this->drupalGet("admin/structure/block/manage/{$this->module}/{$this->delta}/configure");
183
    // Check that the css_class field couldn't be found.
184
    // If the field is not found, it can't be submitted through drupalPost.
185
    $this->assertNoFieldById('css_class', 'The Css classes field was not found on the page.');
186
  }
187

    
188
}
189

    
190
/**
191
 * Test Block Class integration with Context.
192
 */
193
class BlockClassContextTestCase extends BlockClassUpdateDisplayTestCase {
194

    
195
  /**
196
   * Implements DrupalWebTestCase::getInfo().
197
   */
198
  public static function getInfo() {
199
    return array(
200
      'name' => 'Integration with Context',
201
      'description' => 'Test the integration of Block Class with the Context module and the update/display of a Block CSS class.',
202
      // Include required contributed modules context and ctools for the test.
203
      'dependencies' => array('context', 'ctools'),
204
      'group' => 'Block Class',
205
    );
206
  }
207

    
208
  /**
209
   * Enable modules and create user with specific permissions.
210
   */
211
  public function setUp() {
212
    // Override default module and delta to test with the "Who's online" block.
213
    $this->module = 'user';
214
    $this->delta = 'online';
215
    // Include the Context module and its dependencies to be loaded.
216
    parent::setUp('context');
217
    // Initialize a test context with the test block.
218
    $this->initializeContext();
219
  }
220

    
221
  /**
222
   * Helper function to initialize a test Context with a test block.
223
   */
224
  public function initializeContext() {
225
    // Import a basic context exported through the admin interface.
226
    $context = new stdClass();
227
    $context->disabled = FALSE;
228
    $context->api_version = 3;
229
    $context->name = 'front';
230
    $context->description = 'Frontpage Context';
231
    $context->tag = '';
232
    $context->conditions = array(
233
      'path' => array(
234
        'values' => array(
235
          '<front>' => '<front>',
236
        ),
237
      ),
238
    );
239
    $context->reactions = array(
240
      'block' => array(
241
        'blocks' => array(
242
          $this->module . '-' . $this->delta => array(
243
            'module' => $this->module,
244
            'delta' => $this->delta,
245
            'region' => 'content',
246
            'weight' => '-10',
247
          ),
248
        ),
249
      ),
250
    );
251
    $context->condition_mode = 0;
252

    
253
    // Translatables
254
    // Included for use with string extractors like potx.
255
    t('Frontpage Context');
256

    
257
    // Save the context.
258
    context_save($context);
259
  }
260

    
261
}
262

    
263
/**
264
 * Test Block Class integration with Features through FE Block.
265
 */
266
class BlockClassFeaturesTestCase extends BlockClassTestCase {
267

    
268
  /**
269
   * Implements DrupalWebTestCase::getInfo().
270
   */
271
  public static function getInfo() {
272
    return array(
273
      'name' => 'Integration with Features',
274
      'description' => 'Test the integration of Block Class with Features through the FE Block module and the update/display of a Block CSS class.',
275
      // Include Features related modules required for this Test Case.
276
      'dependencies' => array('features', 'ctools', 'fe_block'),
277
      'group' => 'Block Class',
278
    );
279
  }
280

    
281
  /**
282
   * Enable modules and create user with specific permissions.
283
   */
284
  public function setUp() {
285
    // Override default module and delta to test with the "Who's online" block.
286
    $this->module = 'user';
287
    $this->delta = 'online';
288
    // Include all Features related modules and Test Helper feature.
289
    parent::setUp('block_class_fe_block_test');
290
  }
291

    
292
  /**
293
   * Test how Block Class reacts when exported to a Feature with FE Block.
294
   *
295
   * Helper Feature's Block configuration settings are imported, updated and
296
   * the display is tested several times, before reverting the feature.
297
   */
298
  public function testFeatureDisplayClass() {
299
    // Block classes exported to the Test Feature module.
300
    $test_classes = 'fe_block-class1 fe_block-class2 fe_block-class3';
301
    // Test helper feature machine name.
302
    $test_feature = 'block_class_fe_block_test';
303

    
304
    // Browse to the front page and check Block's CSS classes configuration.
305
    $this->drupalGet('');
306
    // Check if feature's Block CSS classes could be found.
307
    $this->assertPattern('/class=\"(.*?)' . $test_classes . '(.*?)\"/', format_string('The CSS classes from exported feature were found: @css_classes', array('@css_classes' => $test_classes)));
308

    
309
    // Check Block's configuration form css_class field value.
310
    $this->drupalGet("admin/structure/block/manage/{$this->module}/{$this->delta}/configure");
311
    $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)));
312

    
313
    // Run a standard Update/Display Test check with Anon.
314
    $this->assertUpdateBlockClass(TRUE);
315

    
316
    // Ensure Feature's state is overridden for 'fe_block_settings' component.
317
    module_load_include('inc', 'features', 'features.export');
318
    $test_feature_state = features_get_storage($test_feature);
319
    $this->assertFalse(empty($test_feature_state), 'The state of the <em>Block Class FE Block Integration Test Helper</em> feature is <strong>Overridden</strong>.');
320
    $test_feature_states = features_get_component_states(array($test_feature));
321
    $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>.');
322

    
323
    // Revert feature and check again.
324
    features_revert_module($test_feature);
325

    
326
    // Browse to the front page and check Block's CSS classes configuration.
327
    $this->drupalGet('');
328
    // Check if feature's Block CSS classes could be found.
329
    $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)));
330

    
331
    // Check Block's configuration form css_class field value.
332
    $this->drupalGet("admin/structure/block/manage/{$this->module}/{$this->delta}/configure");
333
    $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)));
334
  }
335

    
336
}
337

    
338
/**
339
 * Test Block Class integration with Menu Block.
340
 */
341
class BlockClassMenuBlockTestCase extends BlockClassTestCase {
342

    
343
  /**
344
   * Implements DrupalWebTestCase::getInfo().
345
   */
346
  public static function getInfo() {
347
    return array(
348
      'name' => 'Integration with Menu Block',
349
      'description' => 'Test the integration of Block Class with the Menu Block module and the update/display of a Menu Block CSS class.',
350
      // We could use Menu Block's API to import a block from code. But part of
351
      // this test is about the creation process of a Menu Block.
352
      'dependencies' => array('menu_block'),
353
      'group' => 'Block Class',
354
    );
355
  }
356

    
357
  /**
358
   * Enable modules and create user with specific permissions.
359
   */
360
  public function setUp() {
361
    // Override default parameters to test with the first Menu Block created.
362
    $this->module = 'menu_block';
363
    $this->delta = 1;
364
    // Add permission required by Menu Block to browse the add menu block page.
365
    $this->permissions[] = 'administer menu';
366
    // Include the Menu Block module and its dependencies to be loaded.
367
    parent::setUp('menu_block');
368
  }
369

    
370
  /**
371
   * Create, update and display a Menu Block to ensure CSS class is found.
372
   *
373
   * A Menu Block is added through the user interface. It is then updated and
374
   * displayed several times with logged in or anonymous user.
375
   */
376
  public function testMenuBlockDisplayClass() {
377
    // Test with three random class names.
378
    $css_classes = implode(' ', array(
379
      $this->randomName(8),
380
      $this->randomName(8),
381
      $this->randomName(8),
382
    ));
383
    // Start with the creation of a new Menu Block.
384
    $this->drupalPost("admin/structure/block/add-menu-block", array(
385
      'css_class' => $css_classes,
386
      'menu_name' => 'navigation',
387
      'regions[bartik]' => 'content',
388
    ), t('Save block'));
389
    // Check if the Block was successfully created.
390
    $this->assertText(t('The block has been created.'));
391
    // Browse to the homepage.
392
    $this->drupalGet('');
393
    // Check if the Block CSS classes could be found.
394
    $this->assertPattern('/class=\"(.*?)' . $css_classes . '(.*?)\"/', format_string('The CSS classes were found: @css_classes', array('@css_classes' => $css_classes)));
395
    // Run the standard tests on the existing Menu Block created above.
396
    // Edit the block, change the class and check with the anonymous user.
397
    $this->assertUpdateBlockClass(TRUE);
398
  }
399

    
400
}
401

    
402
/**
403
 * Test Block Class integration with Views block.
404
 */
405
class BlockClassViewsTestCase extends BlockClassUpdateDisplayTestCase {
406

    
407
  /**
408
   * Implements DrupalWebTestCase::getInfo().
409
   */
410
  public static function getInfo() {
411
    return array(
412
      'name' => 'Integration with Views block',
413
      'description' => 'Test the integration of Block Class with Views blocks and the update/display of a CSS class.',
414
      'dependencies' => array('views'),
415
      'group' => 'Block Class',
416
    );
417
  }
418

    
419
  /**
420
   * Enable modules and create the data necessary to run the tests.
421
   */
422
  public function setUp() {
423
    // Override default parameters to test with the Views: Archive block.
424
    $this->module = 'views';
425
    $this->delta = 'archive-block';
426
    // Include the Views module and its dependencies to be loaded.
427
    parent::setUp('views');
428

    
429
    // Programmatically enable the default Archive View, based on #820110-1.
430
    $status = variable_get('views_defaults', array());
431
    $status['archive'] = FALSE;
432
    variable_set('views_defaults', $status);
433

    
434
    // Note that the Views Archive block is not visible on the Block admin page
435
    // unless the registry is rebuilt with: registry_rebuild().
436
    // Directly publish the Views Archive Block to the content region.
437
    $this->drupalPost("admin/structure/block/manage/{$this->module}/{$this->delta}/configure", array('regions[bartik]' => 'content'), t('Save block'));
438

    
439
    // Create a sample node to have some data to display in Views: Archive.
440
    $this->drupalCreateNode(array(
441
      'type' => 'page',
442
      'status' => 1,
443
      'uid' => $this->privilegedUser->uid,
444
    ));
445
  }
446

    
447
}