| 1 |
<?php
|
| 2 |
// $Id: block.test,v 1.29 2009/10/14 02:13:15 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Tests for the block module
|
| 7 |
*/
|
| 8 |
|
| 9 |
class BlockTestCase extends DrupalWebTestCase {
|
| 10 |
protected $regions;
|
| 11 |
|
| 12 |
public static function getInfo() {
|
| 13 |
return array(
|
| 14 |
'name' => 'Block functionality',
|
| 15 |
'description' => 'Add, edit and delete custom block. Configure and move a module-defined block.',
|
| 16 |
'group' => 'Block',
|
| 17 |
);
|
| 18 |
}
|
| 19 |
|
| 20 |
function setUp() {
|
| 21 |
parent::setUp();
|
| 22 |
|
| 23 |
// Create and login user
|
| 24 |
$admin_user = $this->drupalCreateUser(array('administer blocks', 'administer filters', 'access administration pages'));
|
| 25 |
$this->drupalLogin($admin_user);
|
| 26 |
|
| 27 |
// Define the existing regions
|
| 28 |
$this->regions = array();
|
| 29 |
$this->regions[] = array('name' => 'header', 'class' => 'region region-header clearfix');
|
| 30 |
$this->regions[] = array('name' => 'sidebar_first');
|
| 31 |
$this->regions[] = array('name' => 'content');
|
| 32 |
$this->regions[] = array('name' => 'sidebar_second');
|
| 33 |
$this->regions[] = array('name' => 'footer');
|
| 34 |
}
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Test creating custom block, moving it to a specific region and then deleting it.
|
| 38 |
*/
|
| 39 |
function testCustomBlock() {
|
| 40 |
// Add a new custom block by filling out the input form on the admin/structure/block/add page.
|
| 41 |
$custom_block = array();
|
| 42 |
$custom_block['info'] = $this->randomName(8);
|
| 43 |
$custom_block['title'] = $this->randomName(8);
|
| 44 |
$custom_block['body'] = $this->randomName(32);
|
| 45 |
$this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));
|
| 46 |
|
| 47 |
// Confirm that the custom block has been created, and then query the created bid.
|
| 48 |
$this->assertText(t('The block has been created.'), t('Custom block successfully created.'));
|
| 49 |
$bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(':info' => $custom_block['info']))->fetchField();
|
| 50 |
|
| 51 |
// Check to see if the custom block was created by checking that it's in the database..
|
| 52 |
$this->assertNotNull($bid, t('Custom block found in database'));
|
| 53 |
|
| 54 |
// Check if the block can be moved to all availble regions.
|
| 55 |
$custom_block['module'] = 'block';
|
| 56 |
$custom_block['delta'] = $bid;
|
| 57 |
foreach ($this->regions as $region) {
|
| 58 |
$this->moveBlockToRegion($custom_block, $region);
|
| 59 |
}
|
| 60 |
|
| 61 |
// Verify presence of configure and delete links for custom block.
|
| 62 |
$this->drupalGet('admin/structure/block');
|
| 63 |
$this->assertRaw(l(t('configure'), 'admin/structure/block/manage/block/' . $bid . '/configure'), t('Custom block configure link found.'));
|
| 64 |
$this->assertRaw(l(t('delete'), 'admin/structure/block/manage/block/' . $bid . '/delete'), t('Custom block delete link found.'));
|
| 65 |
|
| 66 |
// Delete the created custom block & verify that it's been deleted and no longer appearing on the page.
|
| 67 |
$this->clickLink(t('delete'));
|
| 68 |
$this->drupalPost('admin/structure/block/manage/block/' . $bid . '/delete', array(), t('Delete'));
|
| 69 |
$this->assertRaw(t('The block %title has been removed.', array('%title' => $custom_block['info'])), t('Custom block successfully deleted.'));
|
| 70 |
$this->assertNoText(t($custom_block['title']), t('Custom block no longer appears on page.'));
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
* Test creating custom block using Full HTML.
|
| 75 |
*/
|
| 76 |
function testCustomBlockFormat() {
|
| 77 |
// Add a new custom block by filling out the input form on the admin/structure/block/add page.
|
| 78 |
$custom_block = array();
|
| 79 |
$custom_block['info'] = $this->randomName(8);
|
| 80 |
$custom_block['title'] = $this->randomName(8);
|
| 81 |
$custom_block['body'] = '<h1>Full HTML</h1>';
|
| 82 |
$custom_block['body_format'] = 2;
|
| 83 |
$this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));
|
| 84 |
|
| 85 |
// Set the created custom block to a specific region.
|
| 86 |
$bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(':info' => $custom_block['info']))->fetchField();
|
| 87 |
$edit = array();
|
| 88 |
$edit['block_' . $bid . '[region]'] = $this->regions[1]['name'];
|
| 89 |
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
|
| 90 |
|
| 91 |
// Confirm that the custom block is being displayed using configured text format.
|
| 92 |
$this->drupalGet('node');
|
| 93 |
$this->assertRaw('<h1>Full HTML</h1>', t('Custom block successfully being displayed using Full HTML.'));
|
| 94 |
|
| 95 |
// Confirm that a user without access to Full HTML can not see the body field,
|
| 96 |
// but can still submit the form without errors.
|
| 97 |
$block_admin = $this->drupalCreateUser(array('administer blocks'));
|
| 98 |
$this->drupalLogin($block_admin);
|
| 99 |
$this->drupalGet('admin/structure/block/manage/block/' . $bid . '/configure');
|
| 100 |
$this->assertNoText(t('Block body'));
|
| 101 |
$this->drupalPost('admin/structure/block/manage/block/' . $bid . '/configure', array(), t('Save block'));
|
| 102 |
$this->assertNoText(t('Please ensure that each block description is unique.'));
|
| 103 |
|
| 104 |
// Confirm that the custom block is still being displayed using configured text format.
|
| 105 |
$this->drupalGet('node');
|
| 106 |
$this->assertRaw('<h1>Full HTML</h1>', t('Custom block successfully being displayed using Full HTML.'));
|
| 107 |
}
|
| 108 |
|
| 109 |
/**
|
| 110 |
* Test block visibility.
|
| 111 |
*/
|
| 112 |
function testBlockVisibility() {
|
| 113 |
$block = array();
|
| 114 |
|
| 115 |
// Create a random title for the block
|
| 116 |
$title = $this->randomName(8);
|
| 117 |
|
| 118 |
// Create the custom block
|
| 119 |
$custom_block = array();
|
| 120 |
$custom_block['info'] = $this->randomName(8);
|
| 121 |
$custom_block['title'] = $title;
|
| 122 |
$custom_block['body'] = $this->randomName(32);
|
| 123 |
$this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));
|
| 124 |
|
| 125 |
$bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(':info' => $custom_block['info']))->fetchField();
|
| 126 |
$block['module'] = 'block';
|
| 127 |
$block['delta'] = $bid;
|
| 128 |
$block['title'] = $title;
|
| 129 |
|
| 130 |
// Set the block to be hidden on any user path, and to be shown only to
|
| 131 |
// authenticated users.
|
| 132 |
$edit = array();
|
| 133 |
$edit['pages'] = 'user*';
|
| 134 |
$edit['roles[2]'] = TRUE;
|
| 135 |
$this->drupalPost('admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure', $edit, t('Save block'));
|
| 136 |
|
| 137 |
// Move block to the first sidebar.
|
| 138 |
$this->moveBlockToRegion($block, $this->regions[1]);
|
| 139 |
|
| 140 |
$this->drupalGet('');
|
| 141 |
$this->assertText($title, t('Block was displayed on the front page.'));
|
| 142 |
|
| 143 |
$this->drupalGet('user');
|
| 144 |
$this->assertNoText($title, t('Block was not displayed according to block visibility rules.'));
|
| 145 |
|
| 146 |
// Confirm that the block is not displayed to anonymous users.
|
| 147 |
$this->drupalLogout();
|
| 148 |
$this->drupalGet('');
|
| 149 |
$this->assertNoText($title, t('Block was not displayed to anonymous users.'));
|
| 150 |
}
|
| 151 |
|
| 152 |
/**
|
| 153 |
* Test configuring and moving a module-define block to specific regions.
|
| 154 |
*/
|
| 155 |
function testBlock() {
|
| 156 |
// Select the Navigation block to be configured and moved.
|
| 157 |
$block = array();
|
| 158 |
$block['module'] = 'system';
|
| 159 |
$block['delta'] = 'management';
|
| 160 |
$block['title'] = $this->randomName(8);
|
| 161 |
|
| 162 |
// Set block title to confirm that interface works and override any custom titles.
|
| 163 |
$this->drupalPost('admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure', array('title' => $block['title']), t('Save block'));
|
| 164 |
$this->assertText(t('The block configuration has been saved.'), t('Block title set.'));
|
| 165 |
$bid = db_query("SELECT bid FROM {block} WHERE module = :module AND delta = :delta", array(
|
| 166 |
':module' => $block['module'],
|
| 167 |
':delta' => $block['delta'],
|
| 168 |
))->fetchField();
|
| 169 |
|
| 170 |
// Check to see if the block was created by checking that it's in the database.
|
| 171 |
$this->assertNotNull($bid, t('Block found in database'));
|
| 172 |
|
| 173 |
// Check if the block can be moved to all availble regions.
|
| 174 |
foreach ($this->regions as $region) {
|
| 175 |
$this->moveBlockToRegion($block, $region);
|
| 176 |
}
|
| 177 |
|
| 178 |
// Set the block to the disabled region.
|
| 179 |
$edit = array();
|
| 180 |
$edit[$block['module'] . '_' . $block['delta'] . '[region]'] = '-1';
|
| 181 |
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
|
| 182 |
|
| 183 |
// Confirm that the block was moved to the proper region.
|
| 184 |
$this->assertText(t('The block settings have been updated.'), t('Block successfully move to disabled region.'));
|
| 185 |
$this->assertNoText(t($block['title']), t('Block no longer appears on page.'));
|
| 186 |
|
| 187 |
// Confirm that the regions xpath is not availble
|
| 188 |
$xpath = '//div[@id="block-block-' . $bid . '"]/*';
|
| 189 |
$this->assertNoFieldByXPath($xpath, FALSE, t('Custom block found in no regions.'));
|
| 190 |
|
| 191 |
// For convenience of developers, put the navigation block back.
|
| 192 |
$edit = array();
|
| 193 |
$edit[$block['module'] . '_' . $block['delta'] . '[region]'] = $this->regions[1]['name'];
|
| 194 |
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
|
| 195 |
$this->assertText(t('The block settings have been updated.'), t('Block successfully move to first sidebar region.'));
|
| 196 |
|
| 197 |
$this->drupalPost('admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure', array('title' => 'Navigation'), t('Save block'));
|
| 198 |
$this->assertText(t('The block configuration has been saved.'), t('Block title set.'));
|
| 199 |
}
|
| 200 |
|
| 201 |
function moveBlockToRegion($block, $region) {
|
| 202 |
// If an id for an region hasn't been specified, we assume it's the same as the name.
|
| 203 |
if (!(isset($region['class']))) {
|
| 204 |
$region['class'] = 'region region-' . str_replace('_', '-', $region['name']);
|
| 205 |
}
|
| 206 |
|
| 207 |
// Set the created block to a specific region.
|
| 208 |
$edit = array();
|
| 209 |
$edit[$block['module'] . '_' . $block['delta'] . '[region]'] = $region['name'];
|
| 210 |
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
|
| 211 |
|
| 212 |
// Confirm that the block was moved to the proper region.
|
| 213 |
$this->assertText(t('The block settings have been updated.'), t('Block successfully moved to %region_name region.', array( '%region_name' => $region['name'])));
|
| 214 |
|
| 215 |
// Confirm that the block is being displayed.
|
| 216 |
$this->drupalGet('node');
|
| 217 |
$this->assertText(t($block['title']), t('Block successfully being displayed on the page.'));
|
| 218 |
|
| 219 |
// Confirm that the custom block was found at the proper region.
|
| 220 |
$xpath = '//div[@class="' . $region['class'] . '"]//div[@id="block-' . $block['module'] . '-' . $block['delta'] . '"]/*';
|
| 221 |
$this->assertFieldByXPath($xpath, FALSE, t('Custom block found in %region_name region.', array('%region_name' => $region['name'])));
|
| 222 |
}
|
| 223 |
}
|
| 224 |
|
| 225 |
class NonDefaultBlockAdmin extends DrupalWebTestCase {
|
| 226 |
public static function getInfo() {
|
| 227 |
return array(
|
| 228 |
'name' => 'Non default theme admin',
|
| 229 |
'description' => 'Check the administer page for non default theme.',
|
| 230 |
'group' => 'Block',
|
| 231 |
);
|
| 232 |
}
|
| 233 |
|
| 234 |
/**
|
| 235 |
* Test non-default theme admin.
|
| 236 |
*/
|
| 237 |
function testNonDefaultBlockAdmin() {
|
| 238 |
$admin_user = $this->drupalCreateUser(array('administer blocks', 'administer site configuration'));
|
| 239 |
$this->drupalLogin($admin_user);
|
| 240 |
$this->drupalPost('admin/appearance', array('status[stark]' => 1), t('Save configuration'));
|
| 241 |
$this->drupalGet('admin/structure/block/list/stark');
|
| 242 |
}
|
| 243 |
}
|
| 244 |
|
| 245 |
/**
|
| 246 |
* Test blocks correctly initialized when picking a new default theme.
|
| 247 |
*/
|
| 248 |
class NewDefaultThemeBlocks extends DrupalWebTestCase {
|
| 249 |
public static function getInfo() {
|
| 250 |
return array(
|
| 251 |
'name' => 'New default theme blocks',
|
| 252 |
'description' => 'Checks that the new default theme gets blocks.',
|
| 253 |
'group' => 'Block',
|
| 254 |
);
|
| 255 |
}
|
| 256 |
|
| 257 |
/**
|
| 258 |
* Check the enabled Garland blocks are correctly copied over.
|
| 259 |
*/
|
| 260 |
function testNewDefaultThemeBlocks() {
|
| 261 |
// Create administrative user.
|
| 262 |
$admin_user = $this->drupalCreateUser(array('administer site configuration'));
|
| 263 |
$this->drupalLogin($admin_user);
|
| 264 |
|
| 265 |
// Ensure no other theme's blocks are in the block table yet.
|
| 266 |
$count = db_query_range("SELECT 1 FROM {block} WHERE theme NOT IN ('garland', 'seven')", 0, 1)->fetchField();
|
| 267 |
$this->assertFalse($count, t('Only Garland and Seven have blocks.'));
|
| 268 |
|
| 269 |
// Populate list of all blocks for matching against new theme.
|
| 270 |
$blocks = array();
|
| 271 |
$result = db_query("SELECT * FROM {block} WHERE theme = 'garland'");
|
| 272 |
foreach ($result as $block) {
|
| 273 |
// $block->theme and $block->bid will not match, so remove them.
|
| 274 |
unset($block->theme, $block->bid);
|
| 275 |
$blocks[$block->module][$block->delta] = $block;
|
| 276 |
}
|
| 277 |
|
| 278 |
// Turn on the Stark theme and ensure that it contains all of the blocks
|
| 279 |
// that Garland did.
|
| 280 |
$this->drupalPost('admin/appearance', array('theme_default' => 'stark'), t('Save configuration'));
|
| 281 |
$result = db_query("SELECT * FROM {block} WHERE theme='stark'");
|
| 282 |
foreach ($result as $block) {
|
| 283 |
unset($block->theme, $block->bid);
|
| 284 |
$this->assertEqual($blocks[$block->module][$block->delta], $block, t('Block %name matched', array('%name' => $block->module . '-' . $block->delta)));
|
| 285 |
}
|
| 286 |
}
|
| 287 |
}
|
| 288 |
|
| 289 |
/**
|
| 290 |
* Test the block system with admin themes.
|
| 291 |
*/
|
| 292 |
class BlockAdminThemeTestCase extends DrupalWebTestCase {
|
| 293 |
public static function getInfo() {
|
| 294 |
return array(
|
| 295 |
'name' => 'Admin theme block admin accessibility',
|
| 296 |
'description' => "Check whether the block administer page for a disabled theme acccessible if and only if it's the admin theme.",
|
| 297 |
'group' => 'Block',
|
| 298 |
);
|
| 299 |
}
|
| 300 |
|
| 301 |
/**
|
| 302 |
* Check for the accessibility of the admin theme on the block admin page.
|
| 303 |
*/
|
| 304 |
function testAdminTheme() {
|
| 305 |
// Create administrative user.
|
| 306 |
$admin_user = $this->drupalCreateUser(array('administer blocks', 'administer site configuration'));
|
| 307 |
$this->drupalLogin($admin_user);
|
| 308 |
|
| 309 |
// Ensure that access to block admin page is denied when theme is disabled.
|
| 310 |
$this->drupalGet('admin/structure/block/list/stark');
|
| 311 |
$this->assertResponse(403, t('The block admin page for a disabled theme can not be accessed'));
|
| 312 |
|
| 313 |
// Enable admin theme and confirm that tab is accessible.
|
| 314 |
$edit['admin_theme'] = 'stark';
|
| 315 |
$this->drupalPost('admin/appearance', $edit, t('Save configuration'));
|
| 316 |
$this->drupalGet('admin/structure/block/list/stark');
|
| 317 |
$this->assertResponse(200, t('The block admin page for the admin theme can be accessed'));
|
| 318 |
}
|
| 319 |
}
|