Skip navigation
Help

block.test

  1. drupal
    1. 7 drupal/modules/block/block.test

Tests for block.module.

Classes

NameDescription
BlockAdminThemeTestCaseTest the block system with admin themes.
BlockCacheTestCaseTest block caching.
BlockHTMLIdTestCaseTest block HTML id validity.
BlockTemplateSuggestionsUnitTestUnit tests for template_preprocess_block().
BlockTestCase
NewDefaultThemeBlocksTest blocks correctly initialized when picking a new default theme.
NonDefaultBlockAdmin

File

drupal/modules/block/block.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Tests for block.module.
  5. */
  6. class BlockTestCase extends DrupalWebTestCase {
  7. protected $regions;
  8. protected $admin_user;
  9. public static function getInfo() {
  10. return array(
  11. 'name' => 'Block functionality',
  12. 'description' => 'Add, edit and delete custom block. Configure and move a module-defined block.',
  13. 'group' => 'Block',
  14. );
  15. }
  16. function setUp() {
  17. parent::setUp();
  18. // Create and log in an administrative user having access to the Full HTML
  19. // text format.
  20. $full_html_format = filter_format_load('full_html');
  21. $this->admin_user = $this->drupalCreateUser(array(
  22. 'administer blocks',
  23. filter_permission_name($full_html_format),
  24. 'access administration pages',
  25. ));
  26. $this->drupalLogin($this->admin_user);
  27. // Define the existing regions
  28. $this->regions = array();
  29. $this->regions[] = 'header';
  30. $this->regions[] = 'sidebar_first';
  31. $this->regions[] = 'content';
  32. $this->regions[] = 'sidebar_second';
  33. $this->regions[] = 'footer';
  34. }
  35. /**
  36. * Test creating custom block, moving it to a specific region and then deleting it.
  37. */
  38. function testCustomBlock() {
  39. // Confirm that the add block link appears on block overview pages.
  40. $this->drupalGet('admin/structure/block');
  41. $this->assertRaw(l('Add block', 'admin/structure/block/add'), t('Add block link is present on block overview page for default theme.'));
  42. $this->drupalGet('admin/structure/block/list/seven');
  43. $this->assertRaw(l('Add block', 'admin/structure/block/list/seven/add'), t('Add block link is present on block overview page for non-default theme.'));
  44. // Confirm that hidden regions are not shown as options for block placement
  45. // when adding a new block.
  46. theme_enable(array('stark'));
  47. $themes = list_themes();
  48. $this->drupalGet('admin/structure/block/add');
  49. foreach ($themes as $key => $theme) {
  50. if ($theme->status) {
  51. foreach ($theme->info['regions_hidden'] as $hidden_region) {
  52. $elements = $this->xpath('//select[@id=:id]//option[@value=:value]', array(':id' => 'edit-regions-' . $key, ':value' => $hidden_region));
  53. $this->assertFalse(isset($elements[0]), t('The hidden region @region is not available for @theme.', array('@region' => $hidden_region, '@theme' => $key)));
  54. }
  55. }
  56. }
  57. // Add a new custom block by filling out the input form on the admin/structure/block/add page.
  58. $custom_block = array();
  59. $custom_block['info'] = $this->randomName(8);
  60. $custom_block['title'] = $this->randomName(8);
  61. $custom_block['body[value]'] = $this->randomName(32);
  62. $this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));
  63. // Confirm that the custom block has been created, and then query the created bid.
  64. $this->assertText(t('The block has been created.'), t('Custom block successfully created.'));
  65. $bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(':info' => $custom_block['info']))->fetchField();
  66. // Check to see if the custom block was created by checking that it's in the database.
  67. $this->assertNotNull($bid, t('Custom block found in database'));
  68. // Check that block_block_view() returns the correct title and content.
  69. $data = block_block_view($bid);
  70. $format = db_query("SELECT format FROM {block_custom} WHERE bid = :bid", array(':bid' => $bid))->fetchField();
  71. $this->assertTrue(array_key_exists('subject', $data) && empty($data['subject']), t('block_block_view() provides an empty block subject, since custom blocks do not have default titles.'));
  72. $this->assertEqual(check_markup($custom_block['body[value]'], $format), $data['content'], t('block_block_view() provides correct block content.'));
  73. // Check if the block can be moved to all availble regions.
  74. $custom_block['module'] = 'block';
  75. $custom_block['delta'] = $bid;
  76. foreach ($this->regions as $region) {
  77. $this->moveBlockToRegion($custom_block, $region);
  78. }
  79. // Verify presence of configure and delete links for custom block.
  80. $this->drupalGet('admin/structure/block');
  81. $this->assertLinkByHref('admin/structure/block/manage/block/' . $bid . '/configure', 0, t('Custom block configure link found.'));
  82. $this->assertLinkByHref('admin/structure/block/manage/block/' . $bid . '/delete', 0, t('Custom block delete link found.'));
  83. // Set visibility only for authenticated users, to verify delete functionality.
  84. $edit = array();
  85. $edit['roles[2]'] = TRUE;
  86. $this->drupalPost('admin/structure/block/manage/block/' . $bid . '/configure', $edit, t('Save block'));
  87. // Delete the created custom block & verify that it's been deleted and no longer appearing on the page.
  88. $this->clickLink(t('delete'));
  89. $this->drupalPost('admin/structure/block/manage/block/' . $bid . '/delete', array(), t('Delete'));
  90. $this->assertRaw(t('The block %title has been removed.', array('%title' => $custom_block['info'])), t('Custom block successfully deleted.'));
  91. $this->assertNoText(t($custom_block['title']), t('Custom block no longer appears on page.'));
  92. $count = db_query("SELECT 1 FROM {block_role} WHERE module = :module AND delta = :delta", array(':module' => $custom_block['module'], ':delta' => $custom_block['delta']))->fetchField();
  93. $this->assertFalse($count, t('Table block_role being cleaned.'));
  94. }
  95. /**
  96. * Test creating custom block using Full HTML.
  97. */
  98. function testCustomBlockFormat() {
  99. // Add a new custom block by filling out the input form on the admin/structure/block/add page.
  100. $custom_block = array();
  101. $custom_block['info'] = $this->randomName(8);
  102. $custom_block['title'] = $this->randomName(8);
  103. $custom_block['body[value]'] = '<h1>Full HTML</h1>';
  104. $full_html_format = filter_format_load('full_html');
  105. $custom_block['body[format]'] = $full_html_format->format;
  106. $this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));
  107. // Set the created custom block to a specific region.
  108. $bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(':info' => $custom_block['info']))->fetchField();
  109. $edit = array();
  110. $edit['blocks[block_' . $bid . '][region]'] = $this->regions[1];
  111. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  112. // Confirm that the custom block is being displayed using configured text format.
  113. $this->drupalGet('node');
  114. $this->assertRaw('<h1>Full HTML</h1>', t('Custom block successfully being displayed using Full HTML.'));
  115. // Confirm that a user without access to Full HTML can not see the body field,
  116. // but can still submit the form without errors.
  117. $block_admin = $this->drupalCreateUser(array('administer blocks'));
  118. $this->drupalLogin($block_admin);
  119. $this->drupalGet('admin/structure/block/manage/block/' . $bid . '/configure');
  120. $this->assertFieldByXPath("//textarea[@name='body[value]' and @disabled='disabled']", t('This field has been disabled because you do not have sufficient permissions to edit it.'), t('Body field contains denied message'));
  121. $this->drupalPost('admin/structure/block/manage/block/' . $bid . '/configure', array(), t('Save block'));
  122. $this->assertNoText(t('Ensure that each block description is unique.'));
  123. // Confirm that the custom block is still being displayed using configured text format.
  124. $this->drupalGet('node');
  125. $this->assertRaw('<h1>Full HTML</h1>', t('Custom block successfully being displayed using Full HTML.'));
  126. }
  127. /**
  128. * Test block visibility.
  129. */
  130. function testBlockVisibility() {
  131. $block = array();
  132. // Create a random title for the block
  133. $title = $this->randomName(8);
  134. // Create the custom block
  135. $custom_block = array();
  136. $custom_block['info'] = $this->randomName(8);
  137. $custom_block['title'] = $title;
  138. $custom_block['body[value]'] = $this->randomName(32);
  139. $this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));
  140. $bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(':info' => $custom_block['info']))->fetchField();
  141. $block['module'] = 'block';
  142. $block['delta'] = $bid;
  143. $block['title'] = $title;
  144. // Set the block to be hidden on any user path, and to be shown only to
  145. // authenticated users.
  146. $edit = array();
  147. $edit['pages'] = 'user*';
  148. $edit['roles[2]'] = TRUE;
  149. $this->drupalPost('admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure', $edit, t('Save block'));
  150. // Move block to the first sidebar.
  151. $this->moveBlockToRegion($block, $this->regions[1]);
  152. $this->drupalGet('');
  153. $this->assertText($title, t('Block was displayed on the front page.'));
  154. $this->drupalGet('user');
  155. $this->assertNoText($title, t('Block was not displayed according to block visibility rules.'));
  156. $this->drupalGet('USER/' . $this->admin_user->uid);
  157. $this->assertNoText($title, t('Block was not displayed according to block visibility rules regardless of path case.'));
  158. // Confirm that the block is not displayed to anonymous users.
  159. $this->drupalLogout();
  160. $this->drupalGet('');
  161. $this->assertNoText($title, t('Block was not displayed to anonymous users.'));
  162. }
  163. /**
  164. * Test user customization of block visibility.
  165. */
  166. function testBlockVisibilityPerUser() {
  167. $block = array();
  168. // Create a random title for the block.
  169. $title = $this->randomName(8);
  170. // Create our custom test block.
  171. $custom_block = array();
  172. $custom_block['info'] = $this->randomName(8);
  173. $custom_block['title'] = $title;
  174. $custom_block['body[value]'] = $this->randomName(32);
  175. $this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));
  176. $bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(':info' => $custom_block['info']))->fetchField();
  177. $block['module'] = 'block';
  178. $block['delta'] = $bid;
  179. $block['title'] = $title;
  180. // Move block to the first sidebar.
  181. $this->moveBlockToRegion($block, $this->regions[1]);
  182. // Set the block to be customizable per user, visible by default.
  183. $edit = array();
  184. $edit['custom'] = BLOCK_CUSTOM_ENABLED;
  185. $this->drupalPost('admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure', $edit, t('Save block'));
  186. // Disable block visibility for the admin user.
  187. $edit = array();
  188. $edit['block[' . $block['module'] . '][' . $block['delta'] . ']'] = FALSE;
  189. $this->drupalPost('user/' . $this->admin_user->uid . '/edit', $edit, t('Save'));
  190. $this->drupalGet('');
  191. $this->assertNoText($block['title'], t('Block was not displayed according to per user block visibility setting.'));
  192. // Set the block to be customizable per user, hidden by default.
  193. $edit = array();
  194. $edit['custom'] = BLOCK_CUSTOM_DISABLED;
  195. $this->drupalPost('admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure', $edit, t('Save block'));
  196. // Enable block visibility for the admin user.
  197. $edit = array();
  198. $edit['block[' . $block['module'] . '][' . $block['delta'] . ']'] = TRUE;
  199. $this->drupalPost('user/' . $this->admin_user->uid . '/edit', $edit, t('Save'));
  200. $this->drupalGet('');
  201. $this->assertText($block['title'], t('Block was displayed according to per user block visibility setting.'));
  202. }
  203. /**
  204. * Test configuring and moving a module-define block to specific regions.
  205. */
  206. function testBlock() {
  207. // Select the Navigation block to be configured and moved.
  208. $block = array();
  209. $block['module'] = 'system';
  210. $block['delta'] = 'management';
  211. $block['title'] = $this->randomName(8);
  212. // Set block title to confirm that interface works and override any custom titles.
  213. $this->drupalPost('admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure', array('title' => $block['title']), t('Save block'));
  214. $this->assertText(t('The block configuration has been saved.'), t('Block title set.'));
  215. $bid = db_query("SELECT bid FROM {block} WHERE module = :module AND delta = :delta", array(
  216. ':module' => $block['module'],
  217. ':delta' => $block['delta'],
  218. ))->fetchField();
  219. // Check to see if the block was created by checking that it's in the database.
  220. $this->assertNotNull($bid, t('Block found in database'));
  221. // Check if the block can be moved to all availble regions.
  222. foreach ($this->regions as $region) {
  223. $this->moveBlockToRegion($block, $region);
  224. }
  225. // Set the block to the disabled region.
  226. $edit = array();
  227. $edit['blocks[' . $block['module'] . '_' . $block['delta'] . '][region]'] = '-1';
  228. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  229. // Confirm that the block was moved to the proper region.
  230. $this->assertText(t('The block settings have been updated.'), t('Block successfully move to disabled region.'));
  231. $this->assertNoText(t($block['title']), t('Block no longer appears on page.'));
  232. // Confirm that the regions xpath is not availble
  233. $xpath = $this->buildXPathQuery('//div[@id=:id]/*', array(':id' => 'block-block-' . $bid));
  234. $this->assertNoFieldByXPath($xpath, FALSE, t('Custom block found in no regions.'));
  235. // For convenience of developers, put the navigation block back.
  236. $edit = array();
  237. $edit['blocks[' . $block['module'] . '_' . $block['delta'] . '][region]'] = $this->regions[1];
  238. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  239. $this->assertText(t('The block settings have been updated.'), t('Block successfully move to first sidebar region.'));
  240. $this->drupalPost('admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure', array('title' => 'Navigation'), t('Save block'));
  241. $this->assertText(t('The block configuration has been saved.'), t('Block title set.'));
  242. }
  243. function moveBlockToRegion($block, $region) {
  244. // Set the created block to a specific region.
  245. $edit = array();
  246. $edit['blocks[' . $block['module'] . '_' . $block['delta'] . '][region]'] = $region;
  247. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  248. // Confirm that the block was moved to the proper region.
  249. $this->assertText(t('The block settings have been updated.'), t('Block successfully moved to %region_name region.', array( '%region_name' => $region)));
  250. // Confirm that the block is being displayed.
  251. $this->drupalGet('node');
  252. $this->assertText(t($block['title']), t('Block successfully being displayed on the page.'));
  253. // Confirm that the custom block was found at the proper region.
  254. $xpath = $this->buildXPathQuery('//div[@class=:region-class]//div[@id=:block-id]/*', array(
  255. ':region-class' => 'region region-' . str_replace('_', '-', $region),
  256. ':block-id' => 'block-' . $block['module'] . '-' . $block['delta'],
  257. ));
  258. $this->assertFieldByXPath($xpath, NULL, t('Custom block found in %region_name region.', array('%region_name' => $region)));
  259. }
  260. /**
  261. * Test _block_rehash().
  262. */
  263. function testBlockRehash() {
  264. module_enable(array('block_test'));
  265. $this->assertTrue(module_exists('block_test'), t('Test block module enabled.'));
  266. // Our new block should be inserted in the database when we visit the
  267. // block management page.
  268. $this->drupalGet('admin/structure/block');
  269. // Our test block's caching should default to DRUPAL_CACHE_PER_ROLE.
  270. $current_caching = db_query("SELECT cache FROM {block} WHERE module = 'block_test' AND delta = 'test_cache'")->fetchField();
  271. $this->assertEqual($current_caching, DRUPAL_CACHE_PER_ROLE, t('Test block cache mode defaults to DRUPAL_CACHE_PER_ROLE.'));
  272. // Disable caching for this block.
  273. variable_set('block_test_caching', DRUPAL_NO_CACHE);
  274. // Flushing all caches should call _block_rehash().
  275. drupal_flush_all_caches();
  276. // Verify that the database is updated with the new caching mode.
  277. $current_caching = db_query("SELECT cache FROM {block} WHERE module = 'block_test' AND delta = 'test_cache'")->fetchField();
  278. $this->assertEqual($current_caching, DRUPAL_NO_CACHE, t("Test block's database entry updated to DRUPAL_NO_CACHE."));
  279. }
  280. }
  281. class NonDefaultBlockAdmin extends DrupalWebTestCase {
  282. public static function getInfo() {
  283. return array(
  284. 'name' => 'Non default theme admin',
  285. 'description' => 'Check the administer page for non default theme.',
  286. 'group' => 'Block',
  287. );
  288. }
  289. /**
  290. * Test non-default theme admin.
  291. */
  292. function testNonDefaultBlockAdmin() {
  293. $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer themes'));
  294. $this->drupalLogin($admin_user);
  295. theme_enable(array('stark'));
  296. $this->drupalGet('admin/structure/block/list/stark');
  297. }
  298. }
  299. /**
  300. * Test blocks correctly initialized when picking a new default theme.
  301. */
  302. class NewDefaultThemeBlocks extends DrupalWebTestCase {
  303. public static function getInfo() {
  304. return array(
  305. 'name' => 'New default theme blocks',
  306. 'description' => 'Checks that the new default theme gets blocks.',
  307. 'group' => 'Block',
  308. );
  309. }
  310. /**
  311. * Check the enabled Bartik blocks are correctly copied over.
  312. */
  313. function testNewDefaultThemeBlocks() {
  314. // Create administrative user.
  315. $admin_user = $this->drupalCreateUser(array('administer themes'));
  316. $this->drupalLogin($admin_user);
  317. // Ensure no other theme's blocks are in the block table yet.
  318. $themes = array();
  319. $themes['default'] = variable_get('theme_default', 'bartik');
  320. if ($admin_theme = variable_get('admin_theme')) {
  321. $themes['admin'] = $admin_theme;
  322. }
  323. $count = db_query_range('SELECT 1 FROM {block} WHERE theme NOT IN (:themes)', 0, 1, array(':themes' => $themes))->fetchField();
  324. $this->assertFalse($count, t('Only the default theme and the admin theme have blocks.'));
  325. // Populate list of all blocks for matching against new theme.
  326. $blocks = array();
  327. $result = db_query('SELECT * FROM {block} WHERE theme = :theme', array(':theme' => $themes['default']));
  328. foreach ($result as $block) {
  329. // $block->theme and $block->bid will not match, so remove them.
  330. unset($block->theme, $block->bid);
  331. $blocks[$block->module][$block->delta] = $block;
  332. }
  333. // Turn on the Stark theme and ensure that it contains all of the blocks
  334. // the default theme had.
  335. theme_enable(array('stark'));
  336. variable_set('theme_default', 'stark');
  337. $result = db_query('SELECT * FROM {block} WHERE theme = :theme', array(':theme' => 'stark'));
  338. foreach ($result as $block) {
  339. unset($block->theme, $block->bid);
  340. $this->assertEqual($blocks[$block->module][$block->delta], $block, t('Block %name matched', array('%name' => $block->module . '-' . $block->delta)));
  341. }
  342. }
  343. }
  344. /**
  345. * Test the block system with admin themes.
  346. */
  347. class BlockAdminThemeTestCase extends DrupalWebTestCase {
  348. public static function getInfo() {
  349. return array(
  350. 'name' => 'Admin theme block admin accessibility',
  351. 'description' => "Check whether the block administer page for a disabled theme accessible if and only if it's the admin theme.",
  352. 'group' => 'Block',
  353. );
  354. }
  355. /**
  356. * Check for the accessibility of the admin theme on the block admin page.
  357. */
  358. function testAdminTheme() {
  359. // Create administrative user.
  360. $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer themes'));
  361. $this->drupalLogin($admin_user);
  362. // Ensure that access to block admin page is denied when theme is disabled.
  363. $this->drupalGet('admin/structure/block/list/stark');
  364. $this->assertResponse(403, t('The block admin page for a disabled theme can not be accessed'));
  365. // Enable admin theme and confirm that tab is accessible.
  366. $edit['admin_theme'] = 'stark';
  367. $this->drupalPost('admin/appearance', $edit, t('Save configuration'));
  368. $this->drupalGet('admin/structure/block/list/stark');
  369. $this->assertResponse(200, t('The block admin page for the admin theme can be accessed'));
  370. }
  371. }
  372. /**
  373. * Test block caching.
  374. */
  375. class BlockCacheTestCase extends DrupalWebTestCase {
  376. protected $admin_user;
  377. protected $normal_user;
  378. protected $normal_user_alt;
  379. public static function getInfo() {
  380. return array(
  381. 'name' => 'Block caching',
  382. 'description' => 'Test block caching.',
  383. 'group' => 'Block',
  384. );
  385. }
  386. function setUp() {
  387. parent::setUp('block_test');
  388. // Create an admin user, log in and enable test blocks.
  389. $this->admin_user = $this->drupalCreateUser(array('administer blocks', 'access administration pages'));
  390. $this->drupalLogin($this->admin_user);
  391. // Create additional users to test caching modes.
  392. $this->normal_user = $this->drupalCreateUser();
  393. $this->normal_user_alt = $this->drupalCreateUser();
  394. // Sync the roles, since drupalCreateUser() creates separate roles for
  395. // the same permission sets.
  396. user_save($this->normal_user_alt, array('roles' => $this->normal_user->roles));
  397. $this->normal_user_alt->roles = $this->normal_user->roles;
  398. // Enable block caching.
  399. variable_set('block_cache', TRUE);
  400. // Enable our test block.
  401. $edit['blocks[block_test_test_cache][region]'] = 'sidebar_first';
  402. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  403. }
  404. /**
  405. * Test DRUPAL_CACHE_PER_ROLE.
  406. */
  407. function testCachePerRole() {
  408. $this->setCacheMode(DRUPAL_CACHE_PER_ROLE);
  409. // Enable our test block. Set some content for it to display.
  410. $current_content = $this->randomName();
  411. variable_set('block_test_content', $current_content);
  412. $this->drupalLogin($this->normal_user);
  413. $this->drupalGet('');
  414. $this->assertText($current_content, t('Block content displays.'));
  415. // Change the content, but the cached copy should still be served.
  416. $old_content = $current_content;
  417. $current_content = $this->randomName();
  418. variable_set('block_test_content', $current_content);
  419. $this->drupalGet('');
  420. $this->assertText($old_content, t('Block is served from the cache.'));
  421. // Clear the cache and verify that the stale data is no longer there.
  422. cache_clear_all();
  423. $this->drupalGet('');
  424. $this->assertNoText($old_content, t('Block cache clear removes stale cache data.'));
  425. $this->assertText($current_content, t('Fresh block content is displayed after clearing the cache.'));
  426. // Test whether the cached data is served for the correct users.
  427. $old_content = $current_content;
  428. $current_content = $this->randomName();
  429. variable_set('block_test_content', $current_content);
  430. $this->drupalLogout();
  431. $this->drupalGet('');
  432. $this->assertNoText($old_content, t('Anonymous user does not see content cached per-role for normal user.'));
  433. $this->drupalLogin($this->normal_user_alt);
  434. $this->drupalGet('');
  435. $this->assertText($old_content, t('User with the same roles sees per-role cached content.'));
  436. $this->drupalLogin($this->admin_user);
  437. $this->drupalGet('');
  438. $this->assertNoText($old_content, t('Admin user does not see content cached per-role for normal user.'));
  439. $this->drupalLogin($this->normal_user);
  440. $this->drupalGet('');
  441. $this->assertText($old_content, t('Block is served from the per-role cache.'));
  442. }
  443. /**
  444. * Test DRUPAL_CACHE_GLOBAL.
  445. */
  446. function testCacheGlobal() {
  447. $this->setCacheMode(DRUPAL_CACHE_GLOBAL);
  448. $current_content = $this->randomName();
  449. variable_set('block_test_content', $current_content);
  450. $this->drupalGet('');
  451. $this->assertText($current_content, t('Block content displays.'));
  452. $old_content = $current_content;
  453. $current_content = $this->randomName();
  454. variable_set('block_test_content', $current_content);
  455. $this->drupalLogout();
  456. $this->drupalGet('user');
  457. $this->assertText($old_content, t('Block content served from global cache.'));
  458. }
  459. /**
  460. * Test DRUPAL_NO_CACHE.
  461. */
  462. function testNoCache() {
  463. $this->setCacheMode(DRUPAL_NO_CACHE);
  464. $current_content = $this->randomName();
  465. variable_set('block_test_content', $current_content);
  466. // If DRUPAL_NO_CACHE has no effect, the next request would be cached.
  467. $this->drupalGet('');
  468. $this->assertText($current_content, t('Block content displays.'));
  469. // A cached copy should not be served.
  470. $current_content = $this->randomName();
  471. variable_set('block_test_content', $current_content);
  472. $this->drupalGet('');
  473. $this->assertText($current_content, t('DRUPAL_NO_CACHE prevents blocks from being cached.'));
  474. }
  475. /**
  476. * Test DRUPAL_CACHE_PER_USER.
  477. */
  478. function testCachePerUser() {
  479. $this->setCacheMode(DRUPAL_CACHE_PER_USER);
  480. $current_content = $this->randomName();
  481. variable_set('block_test_content', $current_content);
  482. $this->drupalLogin($this->normal_user);
  483. $this->drupalGet('');
  484. $this->assertText($current_content, t('Block content displays.'));
  485. $old_content = $current_content;
  486. $current_content = $this->randomName();
  487. variable_set('block_test_content', $current_content);
  488. $this->drupalGet('');
  489. $this->assertText($old_content, t('Block is served from per-user cache.'));
  490. $this->drupalLogin($this->normal_user_alt);
  491. $this->drupalGet('');
  492. $this->assertText($current_content, t('Per-user block cache is not served for other users.'));
  493. $this->drupalLogin($this->normal_user);
  494. $this->drupalGet('');
  495. $this->assertText($old_content, t('Per-user block cache is persistent.'));
  496. }
  497. /**
  498. * Test DRUPAL_CACHE_PER_PAGE.
  499. */
  500. function testCachePerPage() {
  501. $this->setCacheMode(DRUPAL_CACHE_PER_PAGE);
  502. $current_content = $this->randomName();
  503. variable_set('block_test_content', $current_content);
  504. $this->drupalGet('node');
  505. $this->assertText($current_content, t('Block content displays on the node page.'));
  506. $old_content = $current_content;
  507. $current_content = $this->randomName();
  508. variable_set('block_test_content', $current_content);
  509. $this->drupalGet('user');
  510. $this->assertNoText($old_content, t('Block content cached for the node page does not show up for the user page.'));
  511. $this->drupalGet('node');
  512. $this->assertText($old_content, t('Block content cached for the node page.'));
  513. }
  514. /**
  515. * Private helper method to set the test block's cache mode.
  516. */
  517. private function setCacheMode($cache_mode) {
  518. db_update('block')
  519. ->fields(array('cache' => $cache_mode))
  520. ->condition('module', 'block_test')
  521. ->execute();
  522. $current_mode = db_query("SELECT cache FROM {block} WHERE module = 'block_test'")->fetchField();
  523. if ($current_mode != $cache_mode) {
  524. $this->fail(t('Unable to set cache mode to %mode. Current mode: %current_mode', array('%mode' => $cache_mode, '%current_mode' => $current_mode)));
  525. }
  526. }
  527. }
  528. /**
  529. * Test block HTML id validity.
  530. */
  531. class BlockHTMLIdTestCase extends DrupalWebTestCase {
  532. public static function getInfo() {
  533. return array(
  534. 'name' => 'Block HTML id',
  535. 'description' => 'Test block HTML id validity.',
  536. 'group' => 'Block',
  537. );
  538. }
  539. function setUp() {
  540. parent::setUp('block_test');
  541. // Create an admin user, log in and enable test blocks.
  542. $this->admin_user = $this->drupalCreateUser(array('administer blocks', 'access administration pages'));
  543. $this->drupalLogin($this->admin_user);
  544. // Enable our test block.
  545. $edit['blocks[block_test_test_html_id][region]'] = 'sidebar_first';
  546. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  547. // Make sure the block has some content so it will appear
  548. $current_content = $this->randomName();
  549. variable_set('block_test_content', $current_content);
  550. }
  551. /**
  552. * Test valid HTML id.
  553. */
  554. function testHTMLId() {
  555. $this->drupalGet('');
  556. $this->assertRaw('block-block-test-test-html-id', t('HTML id for test block is valid.'));
  557. }
  558. }
  559. /**
  560. * Unit tests for template_preprocess_block().
  561. */
  562. class BlockTemplateSuggestionsUnitTest extends DrupalUnitTestCase {
  563. public static function getInfo() {
  564. return array(
  565. 'name' => 'Block template suggestions',
  566. 'description' => 'Test the template_preprocess_block() function.',
  567. 'group' => 'Block',
  568. );
  569. }
  570. /**
  571. * Test if template_preprocess_block() handles the suggestions right.
  572. */
  573. function testBlockThemeHookSuggestions() {
  574. // Define block delta with underscore to be preprocessed
  575. $block1 = new stdClass();
  576. $block1->module = 'block';
  577. $block1->delta = 'underscore_test';
  578. $block1->region = 'footer';
  579. $variables1 = array();
  580. $variables1['elements']['#block'] = $block1;
  581. $variables1['elements']['#children'] = '';
  582. template_preprocess_block($variables1);
  583. $this->assertEqual($variables1['theme_hook_suggestions'], array('block__footer', 'block__block', 'block__block__underscore_test'), t('Found expected block suggestions for delta with underscore'));
  584. // Define block delta with hyphens to be preprocessed. Hyphens should be
  585. // replaced with underscores.
  586. $block2 = new stdClass();
  587. $block2->module = 'block';
  588. $block2->delta = 'hyphen-test';
  589. $block2->region = 'footer';
  590. $variables2 = array();
  591. $variables2['elements']['#block'] = $block2;
  592. $variables2['elements']['#children'] = '';
  593. template_preprocess_block($variables2);
  594. $this->assertEqual($variables2['theme_hook_suggestions'], array('block__footer', 'block__block', 'block__block__hyphen_test'), t('Hyphens (-) in block delta were replaced by underscore (_)'));
  595. }
  596. }