skinr_ui.test 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the Skinr UI module.
  5. */
  6. /**
  7. * Base class for Skinr UI tests.
  8. */
  9. class SkinrUITestCase extends DrupalWebTestCase {
  10. protected $profile = 'testing';
  11. function setUp() {
  12. $modules = func_get_args();
  13. if (isset($modules[0]) && is_array($modules[0])) {
  14. $modules = $modules[0];
  15. }
  16. parent::setUp(array_merge(array('block', 'comment', 'contextual', 'skinr_ui', 'skinr_ui_test'), $modules));
  17. $this->admin_user = $this->drupalCreateUser(array(
  18. 'administer blocks',
  19. 'access contextual links',
  20. 'administer skinr',
  21. 'edit skin settings',
  22. 'edit advanced skin settings',
  23. ));
  24. $this->drupalLogin($this->admin_user);
  25. // Enable main system block for content region and the user menu block for
  26. // the first sidebar.
  27. // @see http://drupal.org/node/913086
  28. $default_theme = variable_get('theme_default', 'bartik');
  29. db_merge('block')
  30. ->key(array(
  31. 'theme' => $default_theme,
  32. 'module' => 'system',
  33. 'delta' => 'main',
  34. ))
  35. ->fields(array(
  36. 'status' => 1,
  37. 'region' => 'content',
  38. 'pages' => '',
  39. ))
  40. ->execute();
  41. db_merge('block')
  42. ->key(array(
  43. 'theme' => $default_theme,
  44. 'module' => 'system',
  45. 'delta' => 'user-menu',
  46. ))
  47. ->fields(array(
  48. 'status' => 1,
  49. 'region' => 'sidebar_first',
  50. 'pages' => '',
  51. ))
  52. ->execute();
  53. db_merge('block')
  54. ->key(array(
  55. 'theme' => $default_theme,
  56. 'module' => 'search',
  57. 'delta' => 'form',
  58. ))
  59. ->fields(array(
  60. 'status' => 1,
  61. 'region' => 'sidebar_first',
  62. 'pages' => '',
  63. ))
  64. ->execute();
  65. }
  66. /**
  67. * Asserts that a class is set for the given element id.
  68. *
  69. * @param $id
  70. * Id of the HTML element to check.
  71. * @param $class
  72. * The class name to check for.
  73. * @param $message
  74. * Message to display.
  75. * @return
  76. * TRUE on pass, FALSE on fail.
  77. */
  78. function assertSkinrClass($id, $class, $message = '') {
  79. $elements = $this->xpath('//div[@id=:id]', array(':id' => $id));
  80. $class_attr = (string) $elements[0]['class'];
  81. $this->assertTrue(strpos($class_attr, ' ' . $class . ' '), $message);
  82. }
  83. /**
  84. * Asserts that a class is not set for the given element id.
  85. *
  86. * @param $id
  87. * Id of the HTML element to check.
  88. * @param $class
  89. * The class name to check for.
  90. * @param $message
  91. * Message to display.
  92. * @return
  93. * TRUE on pass, FALSE on fail.
  94. */
  95. function assertNoSkinrClass($id, $class, $message = '') {
  96. $elements = $this->xpath('//div[@id=:id]', array(':id' => $id));
  97. $class_attr = (string) $elements[0]['class'];
  98. $this->assertFalse(strpos($class_attr, ' ' . $class . ' '), $message);
  99. }
  100. }
  101. /**
  102. * Tests UI functionality.
  103. */
  104. class SkinrUIBasicTestCase extends SkinrUITestCase {
  105. public static function getInfo() {
  106. return array(
  107. 'name' => 'UI',
  108. 'description' => 'Tests basic Skinr UI functionality.',
  109. 'group' => 'Skinr',
  110. );
  111. }
  112. /**
  113. * Tests basic configuration and applying of a skin.
  114. *
  115. * @todo For some reason, contextual links are not visible in the debug output
  116. * when running tests; likely a core bug in contextual.js. However, the
  117. * links are contained in the output. Keep this in mind when manually
  118. * reviewing the debug output after running tests!
  119. * @todo Remove the overly verbose inline comments after the Skinr development
  120. * team has figured out how to write tests.
  121. */
  122. function testSkinEdit() {
  123. // Go to the front page, on which the user menu block should appear.
  124. $this->drupalGet('');
  125. // Click the first (index 0) 'Edit skin' link on the page, which should be
  126. // the link in the contextual links of the user menu block, since no other
  127. // skinnable elements are visible on the page.
  128. // For now, this is a simple way to assert and access Skinr links. In the
  129. // future, we want to be more explicit in testing; i.e., verify that there
  130. // is really only this link, its 'href' is correct, that it appears in the
  131. // right location, etc.pp; DrupalWebTestCase ($this) provides many helper
  132. // functions to assert such things.
  133. $this->clickLink(t('Edit skin'), 0);
  134. // Verify that we end up on the expected URL to configure skins for the
  135. // user menu block.
  136. $front = variable_get('site_frontpage', 'node');
  137. $this->assertUrl('admin/structure/skinr/edit/nojs/block/system__user-menu/configure', array(
  138. 'query' => array('destination' => $front),
  139. ));
  140. // skinr_ui_test.module got enabled in setUp(), so its skins should be
  141. // available.
  142. // Verify that we can apply the skinr_ui_test_border skin to the block.
  143. $edit = array(
  144. 'skinr_settings[block_type][bartik][groups][general][skinr_ui_test_bgcolor]' => 'bgcolor_red',
  145. );
  146. // NULL means that we want to post to the page that is still contained in
  147. // SimpleTest's internal browser; i.e., the page of the path above. Instead
  148. // of passing NULL, you can also pass a Drupal system path and SimpleTest
  149. // will automatically do a $this->drupalGet($path) for you before posting.
  150. $this->drupalPost(NULL, $edit, t('Save'));
  151. // After posting, we expect to be redirected to the originating page, due
  152. // to the 'destination' query parameter in the 'Edit skin' link. Since we
  153. // came from the front page, Drupal will redirect us to the actual path of
  154. // the front page, not ''.
  155. // Verify that we were redirected to the originating page.
  156. $this->assertUrl($front);
  157. // Verify that the skin has been applied.
  158. $this->assertSkinrClass('block-system-user-menu', 'bgcolor-red', 'CSS class of configured skin option found.');
  159. }
  160. /**
  161. * Tests access control for editing additional CSS classes.
  162. */
  163. function testSkinAdditionalEdit() {
  164. // Verify that we can apply additional CSS classes.
  165. $edit = array(
  166. 'skinr_settings[block_type][bartik][groups][_additional][_additional]' => 'additional',
  167. );
  168. $this->drupalPost('admin/structure/skinr/edit/nojs/block/system__user-menu/configure', $edit, t('Save'));
  169. // Verify that the skin has been applied.
  170. $this->drupalGet('');
  171. $this->assertSkinrClass('block-system-user-menu', 'additional', 'Additional CSS class <em>additional</em> of configured skin option found.');
  172. // Now let's check the same for a user that has no access to alter this.
  173. $user = $this->drupalCreateUser(array('edit skin settings'));
  174. $this->drupalLogin($user);
  175. // Verify that the additional CSS classes field is not enabled.
  176. $this->drupalGet('admin/structure/skinr/edit/nojs/block/system__user-menu/configure');
  177. $this->assertNoFieldByName('skinr_settings[block_type][bartik][groups][_additional][_additional]', NULL, 'Additional CSS classes field is not enabled for this user.');
  178. // Save form when additional CSS classes is not set.
  179. $edit = array();
  180. $this->drupalPost(NULL, $edit, t('Save'));
  181. // Verify that the old class is still applied.
  182. $this->drupalGet('');
  183. $this->assertSkinrClass('block-system-user-menu', 'additional', 'Additional CSS class <em>additional</em> of configured skin option found.');
  184. }
  185. /**
  186. * Tests output of widgets on the skin configuration form.
  187. */
  188. function testSkinEditWidgets() {
  189. // Go to the edit page for system__user_menu block.
  190. $this->drupalGet('admin/structure/skinr/library');
  191. $this->drupalGet('admin/structure/skinr/edit/nojs/block/system__user-menu/configure');
  192. // Check the widgets.
  193. $this->assertFieldByName('skinr_settings[block_type][bartik][groups][general][skinr_ui_test_bgcolor]', NULL, 'Widget with valid type is displayed.');
  194. $this->assertNoFieldByName('skinr_settings[block_type][bartik][groups][box][skinr_ui_test_border]', NULL, 'Widget with invalid type is not displayed.');
  195. $this->assertFieldByName('skinr_settings[block_type][bartik][groups][general][skinr_ui_test_custom][custom]', NULL, 'Widget with form callback is displayed.');
  196. // Check for output from empty groups.
  197. $this->assertNoRaw('id="edit-skinr-settings-block-group-bartik-box"', 'Resulting empty group is not displayed.');
  198. }
  199. /**
  200. * Tests access control for editing additional CSS classes.
  201. */
  202. function testSkinEditThemeHooks() {
  203. // Widget should appear for system blocks.
  204. $this->drupalGet('admin/structure/skinr/edit/nojs/block/system__user-menu/configure');
  205. $this->assertField('edit-skinr-settings-block-type-bartik-groups-general-skinr-ui-test-color-color-white', 'The widget, which is limited to system blocks, appeared on the configuration form for system\'s user-menu block.');
  206. // Widget should not appear search blocks.
  207. $this->drupalGet('admin/structure/skinr/edit/nojs/block/search__form/configure');
  208. $this->assertNoField('edit-skinr-settings-block-type-bartik-groups-general-skinr-ui-test-color-color-white', 'The widget, which is limited to system blocks, did not appear on the configuration form for search\'s form block.');
  209. // Widget should appear for page node comments.
  210. $this->drupalGet('admin/structure/skinr/edit/nojs/comment/page/configure');
  211. $this->assertField('edit-skinr-settings-comment-type-bartik-groups-general-skinr-ui-test-color-color-white', 'The widget, which is limited to page node comments, appeared on the configuration form for page node comments.');
  212. // Widget should not appear for article node comments.
  213. $this->drupalGet('admin/structure/skinr/edit/nojs/comment/article/configure');
  214. $this->assertNoField('edit-skinr-settings-comment-type-bartik-groups-general-skinr-ui-test-color-color-white', 'The widget, which is limited to page node comments, did not appear on the configuration form for article node comments.');
  215. // Widget should appear for page nodes.
  216. $this->drupalGet('admin/structure/skinr/edit/nojs/node/page/configure');
  217. $this->assertField('edit-skinr-settings-node-type-bartik-groups-general-skinr-ui-test-color-color-white', 'The widget, which is limited to page node types, appeared on the configuration form for page node types.');
  218. // Widget should not appear for article nodes.
  219. $this->drupalGet('admin/structure/skinr/edit/nojs/node/article/configure');
  220. $this->assertNoField('edit-skinr-settings-node-type-bartik-groups-general-skinr-ui-test-color-color-white', 'The widget, which is limited to page node types, did not appear on the configuration form for article node types.');
  221. }
  222. }
  223. /**
  224. * Tests administrative pages functionality.
  225. */
  226. class SkinrUIAdminTestCase extends SkinrUITestCase {
  227. public static function getInfo() {
  228. return array(
  229. 'name' => 'Administration',
  230. 'description' => 'Tests administrative Skinr UI functionality.',
  231. 'group' => 'Skinr',
  232. );
  233. }
  234. function setUp() {
  235. parent::setUp(array('skinr_test'));
  236. $this->admin_user = $this->drupalCreateUser(array(
  237. 'administer skinr',
  238. 'edit skin settings',
  239. 'edit advanced skin settings',
  240. ));
  241. $this->drupalLogin($this->admin_user);
  242. // Enable Garland and skinr_test_subtheme without enabling its base theme in
  243. // order to test subtheme inheritance functionality.
  244. theme_enable(array('garland', 'skinr_test_subtheme'));
  245. }
  246. /**
  247. * Tests default status of skins.
  248. *
  249. * The skinr_test_basetheme skin defined by the skinr_test_basetheme theme
  250. * specifies a default status for itself. Its subtheme should inherit the
  251. * status of the basetheme.
  252. *
  253. * @todo Add assertions for 'default status' itself.
  254. */
  255. function testSkinDefaultStatus() {
  256. // Verify that it is enabled for the skinr_test_subtheme.
  257. $this->drupalGet('admin/structure/skinr/library/list/skinr_test_subtheme');
  258. $this->assertFieldChecked('edit-skins-general-skinr-test-basetheme-enable', 'skinr_test_basetheme skin is enabled for skinr_test_subtheme.');
  259. // Verify that it is disabled for Bartik by default.
  260. $this->drupalGet('admin/structure/skinr/library/list/bartik');
  261. $this->assertNoFieldChecked('edit-skins-general-skinr-test-basetheme-enable', 'skinr_test_basetheme skin is disabled for Bartik.');
  262. // Verify that it is disabled for Garland by default.
  263. $this->drupalGet('admin/structure/skinr/library/list/garland');
  264. $this->assertNoFieldChecked('edit-skins-general-skinr-test-basetheme-enable', 'skinr_test_basetheme skin is disabled for Garland.');
  265. // Override the status for skinr_test_subtheme and Bartik, then verify them.
  266. $skin = (object) array(
  267. 'theme' => 'skinr_test_subtheme',
  268. 'module' => 'block',
  269. 'element' => 'system-user-menu',
  270. 'skin' => 'skinr_test_subtheme',
  271. 'options' => array('option1', 'option2'),
  272. 'status' => 1,
  273. );
  274. skinr_skin_save($skin);
  275. $skin = skinr_skin_load($skin->sid);
  276. // Override the default skin.
  277. $skin->element = 'system-main';
  278. $this->drupalGet('admin/structure/skinr');
  279. $this->clickLink(t('disable'), 0);
  280. // Unaltered skin configuration object should have been saved with only the status updated.
  281. // Load an uncached version of the skin configuration object.
  282. $skin = skinr_skin_load_unchanged($skin->sid);
  283. $this->assertFalse($skin->status, 'Status was disabled successfully.');
  284. $this->assertEqual($skin->element, 'system-user-menu', 'Only status was updated, even though the object was modified before updating status.');
  285. // Enable the skin configuration.
  286. $this->drupalGet('admin/structure/skinr');
  287. $this->clickLink(t('enable'), 0);
  288. // Load an uncached version of the skin configuration object.
  289. $skin = skinr_skin_load_unchanged($skin->sid);
  290. $this->assertTrue($skin->status, 'Status was enabled successfully.');
  291. }
  292. /**
  293. * Tests skin group functionality.
  294. */
  295. function testSkinGroups() {
  296. $this->drupalGet('admin/structure/skinr/library');
  297. // Verify that the 'General' (default) group appears.
  298. $this->assertText(t('General'));
  299. // Verify that the 'Box styles' group appears, since skinr_ui_test module
  300. // registers a skin in that group.
  301. $this->assertText(t('Box styles'));
  302. }
  303. /**
  304. * Tests skin configuration listing functionality.
  305. */
  306. function testSkinListing() {
  307. $skin = (object) array(
  308. 'theme' => 'skinr_test_subtheme',
  309. 'module' => 'block',
  310. 'element' => 'system__user-menu',
  311. 'skin' => 'skinr_test_subtheme',
  312. 'options' => array('option1', 'option2'),
  313. 'status' => 1,
  314. );
  315. skinr_skin_save($skin);
  316. // Verify that the skin configuration appears on the skin configurations overview page.
  317. $this->drupalGet('admin/structure/skinr');
  318. $this->assertLinkByHref('admin/structure/skinr/skin/' . $skin->sid . '/delete?destination=admin/structure/skinr', 0, 'Skin configuration was found on overview page.');
  319. // @todo Should we check the filtering and update options functionality?
  320. }
  321. }
  322. /**
  323. * Tests rules administrative pages functionality.
  324. */
  325. class SkinrUIRulesTestCase extends SkinrUITestCase {
  326. public static function getInfo() {
  327. return array(
  328. 'name' => 'Rules UI',
  329. 'description' => 'Tests rules functionality for Skinr UI.',
  330. 'group' => 'Skinr',
  331. );
  332. }
  333. /**
  334. * Tests administrative interface for rules.
  335. */
  336. function testRules() {
  337. // Test that there is a rules page.
  338. $this->drupalGet('admin/structure/skinr');
  339. $this->assertLinkByHref('admin/structure/skinr/rules');
  340. // Test that there is a way to add rules.
  341. $this->drupalGet('admin/structure/skinr/rules');
  342. $this->clickLink(t('Create a new rule'), 0);
  343. // Verify that we end up on the expected URL.
  344. $this->assertUrl('admin/structure/skinr/rules/add');
  345. // Verify that we can create the rule.
  346. $edit = array(
  347. 'rule[title]' => 'Rule 1',
  348. 'rule[rule_type]' => 'page',
  349. );
  350. $this->drupalPost(NULL, $edit, t('Add'));
  351. // After posting, we expect to be redirected to the rule edit page.
  352. $this->assertUrl('admin/structure/skinr/rules/1/edit');
  353. // Save rule.
  354. // @todo Add a skin and test whether it applies properly or not.
  355. $edit = array(
  356. );
  357. $this->drupalPost(NULL, $edit, t('Save rule'));
  358. // We should be returned back to the rules page.
  359. $this->assertUrl('admin/structure/skinr/rules');
  360. // Make sure the new rule appears listed on this page.
  361. $this->assertLinkByHref('admin/structure/skinr/rules/1/edit');
  362. }
  363. }
  364. /**
  365. * Tests UI functionality for Block plugin.
  366. */
  367. class SkinrUIPluginTestCase extends DrupalWebTestCase {
  368. public static function getInfo() {
  369. return array(
  370. 'name' => 'Plugins UI - Core',
  371. 'description' => 'Tests Skinr UI functionality for functionality plugins from Drupal core.',
  372. 'group' => 'Skinr',
  373. );
  374. }
  375. function setUp() {
  376. parent::setUp(array('block', 'comment', 'node', 'skinr_ui'));
  377. $this->admin_user = $this->drupalCreateUser(array(
  378. 'administer blocks',
  379. 'access comments',
  380. 'access content',
  381. 'post comments',
  382. 'skip comment approval',
  383. 'access contextual links',
  384. 'administer skinr',
  385. 'edit skin settings',
  386. 'edit advanced skin settings',
  387. 'bypass node access',
  388. ));
  389. $this->drupalLogin($this->admin_user);
  390. }
  391. /**
  392. * Tests block plugin.
  393. */
  394. function testBlock() {
  395. // Enable user menu block for the first sidebar.
  396. // @see http://drupal.org/node/913086
  397. $default_theme = variable_get('theme_default', 'bartik');
  398. db_merge('block')
  399. ->key(array(
  400. 'theme' => $default_theme,
  401. 'module' => 'system',
  402. 'delta' => 'user-menu',
  403. ))
  404. ->fields(array(
  405. 'status' => 1,
  406. 'region' => 'sidebar_first',
  407. 'pages' => '',
  408. ))
  409. ->execute();
  410. // Get front page.
  411. $this->drupalGet('');
  412. // Make sure our contextual link appears on the page.
  413. $this->assertLinkByHref('admin/structure/skinr/edit/nojs/block/system__user-menu/configure', 0, 'Contexual link to edit block\'s skin configuration was found.');
  414. }
  415. /**
  416. * Tests comment plugin.
  417. */
  418. function testComment() {
  419. // Create a node.
  420. $node1 = $this->drupalCreateNode(array('type' => 'article'));
  421. // Go to node.
  422. $uri = entity_uri('node', $node1);
  423. $this->drupalGet($uri['path']);
  424. // Add a comment to the node. With bartik the contextual links won't
  425. // display until there is at least one comment.
  426. $edit = array(
  427. 'comment_body[und][0][value]' => $this->randomString(128),
  428. );
  429. $this->drupalPost(NULL, $edit, t('Save'));
  430. // Make sure our contextual link appears on the page.
  431. $this->assertLinkByHref('admin/structure/skinr/edit/nojs/comment/article/configure', 0, 'Contexual link to edit comment\'s skin configuration was found.');
  432. }
  433. /**
  434. * Tests node plugin.
  435. */
  436. function testNode() {
  437. // Create a node.
  438. $node = $this->drupalCreateNode(array('type' => 'article'));
  439. // Go to node.
  440. $uri = entity_uri('node', $node);
  441. $this->drupalGet($uri['path']);
  442. // Make sure our contextual link appears on the page.
  443. $this->assertLinkByHref('admin/structure/skinr/edit/nojs/node/article/configure', 0, 'Contexual link to edit node\'s skin configuration was found.');
  444. }
  445. }
  446. /**
  447. * Tests UI functionality for Block plugin.
  448. */
  449. /**
  450. * Tests UI functionality for Block plugin.
  451. */
  452. class SkinrUIPluginViewsTestCase extends DrupalWebTestCase {
  453. public static function getInfo() {
  454. return array(
  455. 'name' => 'Plugins UI - Views',
  456. 'description' => 'Tests Skinr UI functionality for functionality plugin from Views.',
  457. 'dependencies' => array('views', 'views_ui'),
  458. 'group' => 'Skinr',
  459. );
  460. }
  461. function setUp() {
  462. parent::setUp(array('views_ui', 'skinr_ui_test'));
  463. $this->admin_user = $this->drupalCreateUser(array(
  464. 'administer views',
  465. 'access all views',
  466. 'access contextual links',
  467. 'administer skinr',
  468. 'edit skin settings',
  469. 'edit advanced skin settings',
  470. ));
  471. $this->drupalLogin($this->admin_user);
  472. }
  473. /**
  474. * Tests views plugin.
  475. */
  476. function testViews() {
  477. // Go to the view's page.
  478. $this->drupalGet('skinr-ui-test-view');
  479. // Make sure our contextual link appears on the page.
  480. $this->assertLinkByHref('admin/structure/skinr/edit/nojs/views/skinr_ui_test__page/configure', 0, "Contexual link to edit view's skin configuration was found.");
  481. }
  482. }