block_class.test 12 KB

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