skinr.test 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the Skinr module.
  5. */
  6. class SkinrWebTestCase extends DrupalWebTestCase {
  7. /**
  8. * Asserts that a class is set for the given element id.
  9. *
  10. * @param $id
  11. * Id of the HTML element to check.
  12. * @param $class
  13. * The class name to check for.
  14. * @param $message
  15. * Message to display.
  16. * @return
  17. * TRUE on pass, FALSE on fail.
  18. */
  19. function assertSkinrClass($id, $class, $message = '') {
  20. $elements = $this->xpath('//div[@id=:id and contains(@class, :class)]', array(
  21. ':id' => $id,
  22. ':class' => $class,
  23. ));
  24. $this->assertTrue(!empty($elements[0]), $message);
  25. }
  26. /**
  27. * Asserts that a class is not set for the given element id.
  28. *
  29. * @param $id
  30. * Id of the HTML element to check.
  31. * @param $class
  32. * The class name to check for.
  33. * @param $message
  34. * Message to display.
  35. * @return
  36. * TRUE on pass, FALSE on fail.
  37. */
  38. function assertNoSkinrClass($id, $class, $message = '') {
  39. $elements = $this->xpath('//div[@id=:id]', array(':id' => $id));
  40. $class_attr = (string) $elements[0]['class'];
  41. $this->assertTrue(strpos($class_attr, $class) === FALSE, $message);
  42. }
  43. }
  44. /**
  45. * Tests basic module installation.
  46. */
  47. class SkinrInstallationTestCase extends DrupalWebTestCase {
  48. protected $profile = 'testing';
  49. public static function getInfo() {
  50. return array(
  51. 'name' => 'Installation',
  52. 'description' => 'Tests basic module installation.',
  53. 'group' => 'Skinr',
  54. );
  55. }
  56. function setUp() {
  57. parent::setUp();
  58. }
  59. /**
  60. * Tests installation and uninstallation of Skinr modules.
  61. */
  62. function testInstallation() {
  63. $this->admin_user = $this->drupalCreateUser(array(
  64. 'access administration pages',
  65. 'administer modules',
  66. 'administer permissions',
  67. ));
  68. $this->drupalLogin($this->admin_user);
  69. // Install the modules.
  70. $edit = array(
  71. 'modules[Skinr][skinr][enable]' => TRUE,
  72. 'modules[Skinr][skinr_ui][enable]' => TRUE,
  73. );
  74. $this->drupalPost('admin/modules', $edit, t('Save configuration'));
  75. // Grant permissions.
  76. $edit = array(
  77. DRUPAL_AUTHENTICATED_RID . '[administer skinr]' => TRUE,
  78. DRUPAL_AUTHENTICATED_RID . '[edit skin settings]' => TRUE,
  79. DRUPAL_AUTHENTICATED_RID . '[edit advanced skin settings]' => TRUE,
  80. );
  81. $this->drupalPost('admin/people/permissions', $edit, t('Save permissions'));
  82. // Verify that we are able to access Skinr's administration pages.
  83. $this->drupalGet('admin/structure/skinr');
  84. $this->assertResponse(200);
  85. // Uninstall the modules.
  86. $edit = array(
  87. 'modules[Skinr][skinr_ui][enable]' => FALSE,
  88. );
  89. $this->drupalPost('admin/modules', $edit, t('Save configuration'));
  90. $edit = array(
  91. 'modules[Skinr][skinr][enable]' => FALSE,
  92. );
  93. $this->drupalPost(NULL, $edit, t('Save configuration'));
  94. // Uninstall Skinr UI first.
  95. $edit = array(
  96. 'uninstall[skinr_ui]' => TRUE,
  97. );
  98. $this->drupalPost('admin/modules/uninstall', $edit, t('Uninstall'));
  99. $this->drupalPost(NULL, array(), t('Uninstall'));
  100. // Now uninstall Skinr.
  101. $edit = array(
  102. 'uninstall[skinr]' => TRUE,
  103. );
  104. $this->drupalPost('admin/modules/uninstall', $edit, t('Uninstall'));
  105. $this->drupalPost(NULL, array(), t('Uninstall'));
  106. // Verify that no system variables are left.
  107. $count = db_query("SELECT 1 FROM {variable} WHERE name LIKE 'skinr_*'")->fetchField();
  108. $this->assertEqual($count, 0, t('No variables found.'));
  109. }
  110. }
  111. /**
  112. * Tests API functionality.
  113. *
  114. * @link http://drupal.org/node/953336#comment-3738456 Make sure this patch is applied to drupal core @endlink
  115. */
  116. class SkinrApiTestCase extends SkinrWebTestCase {
  117. protected $profile = 'testing';
  118. public static function getInfo() {
  119. return array(
  120. 'name' => 'API',
  121. 'description' => 'Tests Skinr API functionality.',
  122. 'group' => 'Skinr',
  123. );
  124. }
  125. public function setUp() {
  126. parent::setUp(array('skinr', 'skinr_test', 'skinr_test_incompatible'));
  127. // Enable skinr_test_subtheme, but NOT the basetheme.
  128. theme_enable(array('skinr_test_subtheme'));
  129. }
  130. /**
  131. * Pass if the message $text was set by one of the CRUD hooks in
  132. * skinr_test.module, i.e., if the $text is an element of
  133. * $_SESSION['skinr_test'].
  134. *
  135. * @param $text
  136. * Plain text to look for.
  137. * @param $message
  138. * Message to display.
  139. * @param $group
  140. * The group this message belongs to, defaults to 'Other'.
  141. * @return
  142. * TRUE on pass, FALSE on fail.
  143. */
  144. protected function assertHookMessage($text, $message = NULL, $group = 'Other') {
  145. if (!isset($message)) {
  146. $message = $text;
  147. }
  148. return $this->assertTrue(array_search($text, $_SESSION['skinr_test']) !== FALSE, $message, $group);
  149. }
  150. /**
  151. * Tests skinr_implements().
  152. */
  153. public function testSkinrImplementsAPI() {
  154. // Verify that skinr_implements() only returns extensions that are
  155. // compatible with this version of Skinr.
  156. $extensions = skinr_implements_api();
  157. // The expected extensions and their specific properties, if any.
  158. $all_expected = array(
  159. // Skinr is always expected.
  160. 'skinr' => array(),
  161. // Node is a required core module, so always expected.
  162. 'node' => array(
  163. 'version' => VERSION,
  164. 'path' => drupal_get_path('module', 'skinr') . '/modules',
  165. 'include file' => drupal_get_path('module', 'skinr') . '/modules/node.skinr.inc',
  166. ),
  167. // skinr_test has been installed.
  168. 'skinr_test' => array(
  169. 'directory' => 'skins',
  170. ),
  171. 'skinr_test_basetheme' => array(
  172. 'type' => 'theme',
  173. 'api' => 2,
  174. 'directory' => 'skins',
  175. 'base themes' => array(),
  176. 'sub themes' => drupal_map_assoc(array('skinr_test_subtheme')),
  177. 'include file' => drupal_get_path('theme', 'skinr_test_basetheme') . '/skinr_test_basetheme.skinr.inc',
  178. ),
  179. 'skinr_test_subtheme' => array(
  180. 'type' => 'theme',
  181. 'api' => 2,
  182. 'directory' => 'skins',
  183. 'base themes' => drupal_map_assoc(array('skinr_test_basetheme')),
  184. 'sub themes' => array(),
  185. 'include file' => drupal_get_path('theme', 'skinr_test_subtheme') . '/skinr_test_subtheme.skinr.inc',
  186. ),
  187. );
  188. // When running tests on Skinr code packaged by drupal.org, all 'version'
  189. // properties will have the version of the Skinr module. When running on a
  190. // repository checkout, the version is NULL (undefined).
  191. $skinr_module_info = system_get_info('module', 'skinr');
  192. $skinr_module_version = (!empty($skinr_module_info['version']) ? $skinr_module_info['version'] : NULL);
  193. foreach ($all_expected as $name => $expected) {
  194. // Populate defaults.
  195. $expected += array(
  196. 'type' => 'module',
  197. 'name' => $name,
  198. 'version' => $skinr_module_version,
  199. );
  200. $expected += array(
  201. 'path' => drupal_get_path($expected['type'], $name),
  202. 'directory' => NULL,
  203. );
  204. $this->assertEqual($extensions[$name], $expected, t('%extension implementation found:<pre>@data</pre>', array(
  205. '%extension' => $name,
  206. '@data' => var_export($extensions[$name], TRUE),
  207. )));
  208. unset($extensions[$name]);
  209. }
  210. // Ensure that skinr_test_incompatible is not contained.
  211. $this->assertTrue(!isset($extensions['skinr_test_incompatible']), 'Incompatible extension not found.');
  212. // After asserting all expected, the list of extensions should be empty.
  213. $this->assertTrue(empty($extensions), 'No unexpected extensions found.');
  214. }
  215. /**
  216. * Test module_implements().
  217. */
  218. function testSkinrImplements() {
  219. // Test clearing cache.
  220. cache_clear_all('skinr_implements', 'cache_bootstrap');
  221. $this->assertFalse(cache_get('skinr_implements', 'cache_bootstrap'), t('The skinr implements cache is empty.'));
  222. $this->drupalGet('');
  223. $this->assertTrue(cache_get('skinr_implements', 'cache_bootstrap'), t('The skinr implements cache is populated after requesting a page.'));
  224. // Test clearing cache with an authenticated user.
  225. $this->user = $this->drupalCreateUser(array());
  226. $this->drupalLogin($this->user);
  227. cache_clear_all('skinr_implements', 'cache_bootstrap');
  228. $this->drupalGet('');
  229. $this->assertTrue(cache_get('skinr_implements', 'cache_bootstrap'), t('The skinr implements cache is populated after requesting a page.'));
  230. // Make sure $module.skinr.inc files (both in the module root, which are
  231. // auto-loaded by drupal, and in custom paths and themes, which are
  232. // loaded by skinr_implements()) are loaded when the hook is called. Also
  233. // ensure only module that implement the current Skinr API are loaded.
  234. $modules = skinr_implements('skinr_skin_info');
  235. // Ensure the hook is found in includes.
  236. $this->assertTrue(in_array('skinr_test', $modules), 'Hook found in $module.skinr.inc file auto-loaded by module_hook().');
  237. $this->assertTrue(in_array('skinr_test_subtheme', $modules), 'Hook found in $module.skinr.inc file in custom path.');
  238. // Ensure that skinr_test_incompatible is not included.
  239. $this->assertTrue(!in_array('skinr_test_incompatible', $modules), 'Hook in incompatible module not found.');
  240. }
  241. /**
  242. * Tests skinr_implements() caching and auto-loading.
  243. */
  244. function testSkinrImplementsCache() {
  245. module_enable(array('block'));
  246. $this->resetAll();
  247. // Enable main system block for content region and the user menu block for
  248. // the first sidebar.
  249. $default_theme = variable_get('theme_default', 'bartik');
  250. db_merge('block')
  251. ->key(array(
  252. 'theme' => $default_theme,
  253. 'module' => 'system',
  254. 'delta' => 'main',
  255. ))
  256. ->fields(array(
  257. 'status' => 1,
  258. 'region' => 'content',
  259. 'pages' => '',
  260. ))
  261. ->execute();
  262. db_merge('block')
  263. ->key(array(
  264. 'theme' => $default_theme,
  265. 'module' => 'system',
  266. 'delta' => 'powered-by',
  267. ))
  268. ->fields(array(
  269. 'status' => 1,
  270. 'region' => 'sidebar_first',
  271. 'pages' => '',
  272. ))
  273. ->execute();
  274. // Enable a skin defined in an include file, which applies to a module
  275. // element that is equally registered in an include file (built-in Block
  276. // module integration).
  277. $skin = (object) array(
  278. 'theme' => $default_theme,
  279. 'module' => 'block',
  280. 'element' => 'system__powered-by',
  281. 'skin' => 'skinr_test_font',
  282. 'options' => array('font_1'),
  283. 'status' => 1,
  284. );
  285. skinr_skin_save($skin);
  286. // Verify the skin is contained in the output.
  287. $this->drupalGet('');
  288. $this->assertSkinrClass('block-system-powered-by', 'font-1', 'Skin found.');
  289. // Once again, so we hit the cache.
  290. $this->drupalGet('');
  291. $this->assertSkinrClass('block-system-powered-by', 'font-1', 'Skin found.');
  292. // Visit skin edit page after to test for groups, after hitting cache.
  293. $this->drupalGet('skinr-test/hook-dynamic-loading');
  294. $this->assertText('success!', t('$module.skinr.inc file auto-loaded.'));
  295. }
  296. /**
  297. * Test that module_invoke_all() can load a hook defined in hook_hook_info().
  298. */
  299. function testSkinrInvokeAll() {
  300. // Ensure functions from $module.skinr.inc in both module root and in
  301. // custom paths are triggered.
  302. $config_info = skinr_invoke_all('skinr_config_info');
  303. $this->assertTrue(in_array('rules', $config_info), 'Function triggered in $module.skinr.inc file auto-loaded by module_hook().');
  304. $this->assertTrue(in_array('node', $config_info), 'Function triggered in $module.skinr.inc file in custom path.');
  305. // Ensure that skinr_test_incompatible is not included.
  306. $this->assertTrue(!in_array('skinr_test_incompatible', $config_info), 'Function in incompatible module not triggered.');
  307. }
  308. /**
  309. * Tests hook_skinr_skin_info().
  310. */
  311. public function testSkinrSkinInfo() {
  312. // Verify that skinr_get_skin_info() finds and returns all registered skins
  313. // in $module.skinr.inc files as well as Skinr plugin files, but does not
  314. // return skins that are incompatible with the current Skinr API version.
  315. $skin_info = skinr_get_skin_info();
  316. $path = drupal_get_path('module', 'skinr_test');
  317. // skinr_test_font is registered via hook_skinr_skin_info() in
  318. // skinr_test.skinr.inc.
  319. $this->assertTrue(isset($skin_info['skinr_test_font']), 'Skin registered in $module.skinr.inc found.');
  320. $this->assertEqual($skin_info['skinr_test_font']['source']['path'], $path, t('Skin path points to module directory: @path', array(
  321. '@path' => $skin_info['skinr_test_font']['source']['path'],
  322. )));
  323. unset($skin_info['skinr_test_font']);
  324. // skinr_test_example is registered via hook_skinr_skin_PLUGIN_info() in
  325. // skins/example.inc.
  326. $this->assertTrue(isset($skin_info['skinr_test_example']), 'Skin registered in plugin file found.');
  327. $this->assertEqual($skin_info['skinr_test_example']['source']['path'], $path . '/skins/example', t('Skin path points to plugin directory: @path', array(
  328. '@path' => $skin_info['skinr_test_example']['source']['path'],
  329. )));
  330. unset($skin_info['skinr_test_example']);
  331. // skinr_test_basetheme is registered via hook_skinr_skin_info() in
  332. // skinr_test_basetheme.skinr.inc.
  333. $this->assertTrue(isset($skin_info['skinr_test_basetheme']), 'Skin registered in $basetheme.skinr.inc found.');
  334. $this->assertEqual($skin_info['skinr_test_basetheme']['source']['path'], $path . '/themes/skinr_test_basetheme', t('Skin path points to basetheme directory: @path', array(
  335. '@path' => $skin_info['skinr_test_basetheme']['source']['path'],
  336. )));
  337. $default_theme = variable_get('theme_default', 'bartik');
  338. $this->assertEqual($skin_info['skinr_test_basetheme']['status'][$default_theme], 0, 'Basetheme skin is disabled for default theme.');
  339. $this->assertEqual($skin_info['skinr_test_basetheme']['status']['skinr_test_basetheme'], 1, 'Basetheme skin is enabled for Skinr test basetheme.');
  340. unset($skin_info['skinr_test_basetheme']);
  341. // skinr_test_subtheme is registered via hook_skinr_skin_info() in
  342. // skinr_test_subtheme.skinr.inc.
  343. $this->assertTrue(isset($skin_info['skinr_test_subtheme']), 'Skin registered in $subtheme.skinr.inc found.');
  344. $this->assertEqual($skin_info['skinr_test_subtheme']['source']['path'], $path . '/themes/skinr_test_subtheme', t('Skin path points to subtheme directory: @path', array(
  345. '@path' => $skin_info['skinr_test_subtheme']['source']['path'],
  346. )));
  347. unset($skin_info['skinr_test_subtheme']);
  348. // Ensure that skinr_test_incompatible is not contained.
  349. $this->assertTrue(!isset($skin_info['skinr_test_incompatible']), 'Incompatible skin not found.');
  350. // After asserting all expected, the list of skins should be empty.
  351. $this->assertTrue(empty($skin_info), t('No unexpected skins found: <pre>@data</pre>', array(
  352. '@data' => var_export($skin_info, TRUE),
  353. )));
  354. }
  355. /**
  356. * Tests hook_skinr_group_info().
  357. */
  358. public function testSkinrGroupInfo() {
  359. $group_info = skinr_get_group_info();
  360. // Verify that default skin groups are found.
  361. $all_expected = array(
  362. 'general' => array(
  363. 'title' => t('General'),
  364. 'weight' => -10,
  365. ),
  366. 'box' => array(
  367. 'title' => t('Box styles'),
  368. ),
  369. 'typography' => array(
  370. 'title' => t('Typography'),
  371. ),
  372. 'layout' => array(
  373. 'title' => t('Layout'),
  374. ),
  375. );
  376. foreach ($all_expected as $name => $expected) {
  377. // We don't want to be pixel-perfect here.
  378. if (isset($group_info[$name]['description'])) {
  379. $expected['description'] = $group_info[$name]['description'];
  380. }
  381. $expected += array(
  382. 'description' => '',
  383. 'weight' => 0,
  384. );
  385. $this->assertEqual($group_info[$name], $expected, t('Group %group found:<pre>@data</pre>', array(
  386. '%group' => $name,
  387. '@data' => var_export($group_info[$name], TRUE),
  388. )));
  389. unset($group_info[$name]);
  390. }
  391. // After asserting all expected, the list of extensions should be empty.
  392. $this->assertTrue(empty($group_info), 'No unexpected groups found.');
  393. }
  394. /**
  395. * Tests hook_skinr_config_info().
  396. */
  397. public function testSkinrConfigInfo() {
  398. // Verify that skinr_get_config_info() finds all existing and compatible
  399. // hook_skinr_config_info() implementations.
  400. $config = skinr_get_config_info();
  401. // Skinr's own implementation in skinr.skinr.inc should always be found.
  402. $this->assertTrue(in_array('rules', $config), 'hook_skinr_config_info() in $module.skinr.inc found.');
  403. foreach ($config as $key => $type) {
  404. if ($type == 'rules') {
  405. unset($config[$key]);
  406. }
  407. }
  408. // Skinr's implementation on behalf of Node module in modules/node.skinr.inc
  409. // should be found.
  410. $this->assertTrue(in_array('node', $config), 'hook_skinr_config_info() in a custom path found.');
  411. foreach ($config as $key => $type) {
  412. if ($type == 'node') {
  413. unset($config[$key]);
  414. }
  415. }
  416. // Ensure that skinr_test_incompatible is not included.
  417. $this->verbose(highlight_string('<?php ' . var_export($config, TRUE), TRUE));
  418. $this->assertTrue(!isset($config['skinr_test_incompatible']), 'Incompatible hook_skinr_config_info() not found.');
  419. // After asserting all expected, the list of skins should be empty.
  420. $this->assertTrue(empty($config), 'No unexpected skins found.');
  421. }
  422. /**
  423. * Test hook invocations for CRUD operations on skin configurations.
  424. */
  425. public function testSkinrSkinHooks() {
  426. $skin = (object) array(
  427. 'theme' => 'skinr_test_subtheme',
  428. 'module' => 'block',
  429. 'element' => 'system__user-menu',
  430. 'skin' => 'skinr_test_subtheme',
  431. 'options' => array('option1', 'option2'),
  432. 'status' => 1,
  433. );
  434. $_SESSION['skinr_test'] = array();
  435. skinr_skin_save($skin);
  436. $this->assertHookMessage('skinr_test_skinr_skin_presave called');
  437. $this->assertHookMessage('skinr_test_skinr_skin_insert called');
  438. $_SESSION['skinr_test'] = array();
  439. $skin = skinr_skin_load($skin->sid);
  440. $this->assertHookMessage('skinr_test_skinr_skin_load called');
  441. $_SESSION['skinr_test'] = array();
  442. $skin = skinr_skin_load_unchanged($skin->sid);
  443. $this->assertHookMessage('skinr_test_skinr_skin_load called');
  444. $_SESSION['skinr_test'] = array();
  445. $skin->options = array('option3');
  446. skinr_skin_save($skin);
  447. $this->assertHookMessage('skinr_test_skinr_skin_presave called');
  448. $this->assertHookMessage('skinr_test_skinr_skin_update called');
  449. $_SESSION['skinr_test'] = array();
  450. skinr_skin_delete($skin->sid);
  451. $this->assertHookMessage('skinr_test_skinr_skin_delete called');
  452. }
  453. /**
  454. * Test skinr_skin_save() against invalid entries.
  455. */
  456. public function testSkinrSkinLoadSave() {
  457. // Only save valid skins.
  458. $skin = (object) array(
  459. 'theme' => '',
  460. 'module' => 'block',
  461. 'element' => 'system__user-menu',
  462. 'skin' => 'skinr_test_subtheme',
  463. 'options' => array('option1', 'option2'),
  464. 'status' => 1,
  465. );
  466. $this->assertFalse(skinr_skin_save($skin), 'Skin configuration object was not saved when $skin->theme was empty.');
  467. $skin->theme = 'skinr_test_subtheme';
  468. $skin->module = '';
  469. $this->assertFalse(skinr_skin_save($skin), 'Skin configuration object was not saved when $skin->module was empty.');
  470. $skin->module = 'block';
  471. $skin->element = '';
  472. $this->assertFalse(skinr_skin_save($skin), 'Skin configuration object was not saved when $skin->element was empty.');
  473. $skin->element = 'system-user-menu';
  474. $skin->skin = '';
  475. $this->assertFalse(skinr_skin_save($skin), 'Skin configuration object was not saved when $skin->skin was empty.');
  476. $skin->skin = 'skinr_test_subtheme';
  477. $skin->options = '';
  478. $this->assertFalse(skinr_skin_save($skin), 'Skin configuration object was not saved when $skin->options was not an array.');
  479. $skin->options = array();
  480. $this->assertFalse(skinr_skin_save($skin), 'Skin configuration object was not saved when $skin->options was an empty array.');
  481. $skin->options = array('option1' => 0, 'option2' => 0);
  482. $this->assertFalse(skinr_skin_save($skin), 'Skin configuration object was not saved when $skin->options was a complex empty array.');
  483. $skin->options = array('option1', 'option2');
  484. $this->assertTrue(skinr_skin_save($skin), 'Skin configuration object was saved.');
  485. $this->assertTrue(isset($skin->sid), 'The sid was added to the skin configuration object.');
  486. // Test loading a skin configuration.
  487. $loaded_skin = skinr_skin_load($skin->sid);
  488. $this->assertTrue(is_array($skin->options), 'Options for the skin configuration object were unserialized.');
  489. $this->assertTrue($loaded_skin->theme == $skin->theme && $loaded_skin->module == $skin->module && $loaded_skin->element == $skin->element && $loaded_skin->status == $skin->status && $loaded_skin->options[0] == $skin->options[0] && $loaded_skin->options[1] == $skin->options[1], 'Skin configuration object was loaded properly.');
  490. // Save a second skin.
  491. $second_skin = (object) array(
  492. 'theme' => 'skinr_test_subtheme',
  493. 'module' => 'block',
  494. 'element' => 'system__main',
  495. 'skin' => 'skinr_test_subtheme',
  496. 'options' => array('option3'),
  497. 'status' => 1,
  498. );
  499. skinr_skin_save($second_skin);
  500. // Test loading multiple skin configurations.
  501. $skins = skinr_skin_load_multiple(array($skin->sid, $second_skin->sid));
  502. $this->assertTrue(count($skins) == 2 && isset($skins[$skin->sid]->sid) && isset($skins[$second_skin->sid]->sid), 'Successfully loaded multiple skins.');
  503. // Test loading all skin configurations.
  504. $skins = skinr_skin_load_multiple();
  505. $this->assertTrue(count($skins) == 2 && isset($skins[$skin->sid]->sid) && isset($skins[$second_skin->sid]->sid), 'Successfully loaded all skins.');
  506. }
  507. }
  508. /**
  509. * Tests API functionality.
  510. *
  511. * @link http://drupal.org/node/953336#comment-3738456 Make sure this patch is applied to drupal core @endlink
  512. */
  513. class SkinrDisplayTestCase extends SkinrWebTestCase {
  514. protected $profile = 'testing';
  515. public static function getInfo() {
  516. return array(
  517. 'name' => 'Display',
  518. 'description' => 'Tests if applied skins appear on the front-end.',
  519. 'group' => 'Skinr',
  520. );
  521. }
  522. public function setUp() {
  523. parent::setUp(array('block', 'skinr', 'skinr_test', 'devel'));
  524. $this->admin_user = $this->drupalCreateUser(array(
  525. 'administer blocks',
  526. ));
  527. $this->drupalLogin($this->admin_user);
  528. // Enable main system block for content region and the user menu block for
  529. // the first sidebar.
  530. // @see http://drupal.org/node/913086
  531. $default_theme = variable_get('theme_default', 'bartik');
  532. db_merge('block')
  533. ->key(array(
  534. 'theme' => $default_theme,
  535. 'module' => 'system',
  536. 'delta' => 'main',
  537. ))
  538. ->fields(array(
  539. 'status' => 1,
  540. 'region' => 'content',
  541. 'pages' => '',
  542. ))
  543. ->execute();
  544. db_merge('block')
  545. ->key(array(
  546. 'theme' => $default_theme,
  547. 'module' => 'system',
  548. 'delta' => 'user-menu',
  549. ))
  550. ->fields(array(
  551. 'status' => 1,
  552. 'region' => 'sidebar_first',
  553. 'pages' => '',
  554. ))
  555. ->execute();
  556. // Enable Garland.
  557. theme_enable(array('garland'));
  558. }
  559. public function testSkinrDisplayed() {
  560. // Save a skin configuration object.
  561. $skin = (object) array(
  562. 'theme' => 'bartik',
  563. 'module' => 'block',
  564. 'element' => 'system__user-menu',
  565. 'skin' => 'skinr_test_font',
  566. 'options' => array('font_1'),
  567. 'status' => 1,
  568. );
  569. $this->assertTrue(skinr_skin_save($skin), 'Skin configuration object was saved.');
  570. $this->verbose(print_r($skin, TRUE));
  571. // Go to the front page.
  572. $this->drupalGet('');
  573. $this->assertSkinrClass('block-system-user-menu', 'font-1', 'CSS class of configured skin option found.');
  574. $content = $this->drupalGetContent();
  575. $css = drupal_get_path('module', 'skinr_test') . '/skinr_test.css';
  576. $this->assertRaw($css, t('Stylesheet was included on page.'));
  577. $js = drupal_get_path('module', 'skinr_test') . '/skinr_test.js';
  578. $this->assertRaw($js, t('Javascript was included on page.'));
  579. }
  580. /**
  581. * Tests loading and saving of rules.
  582. */
  583. public function testSkinrRulesLoadSave() {
  584. // Test saving a rule.
  585. $rule = (object) array(
  586. 'title' => 'Rule 1',
  587. 'rule_type' => 'page',
  588. 'node_types' => array(),
  589. 'roles' => array(),
  590. 'visibility' => 0, // Show on all pages, except those listed.
  591. 'pages' => '',
  592. );
  593. $this->assertTrue(skinr_rule_save($rule), 'Rule object was saved when no filtering is applied.');
  594. $rule->title = '';
  595. $this->assertFalse($status = skinr_rule_save($rule), 'Rule object was not saved when the required $rule->title field was empty.');
  596. $this->pass('Status: ' . ($status ? 'true' : 'false'));
  597. $rule->title = 'Rule 1';
  598. $rule->rule_type = '';
  599. $this->assertFalse(skinr_rule_save($rule), 'Rule object was not saved when the required $rule->rule_type field was empty.');
  600. $rule->rule_type = 'page';
  601. $rule->node_types = FALSE;
  602. $this->assertFalse(skinr_rule_save($rule), 'Rule object was not saved when $rule->node_types was not an array.');
  603. $rule->node_types = array();
  604. $rule->roles = FALSE;
  605. $this->assertFalse(skinr_rule_save($rule), 'Rule object was not saved when $rule->roles was not an array.');
  606. $rule->roles = array();
  607. // Test loading a rule.
  608. $loaded_rule = skinr_rule_load($rule->rid);
  609. $this->assertTrue(is_array($loaded_rule->node_types), 'Node types for the rule object were unserialized.');
  610. $this->assertTrue(is_array($loaded_rule->roles), 'Roles for the rule object were unserialized.');
  611. $this->assertTrue($loaded_rule->title == $rule->title && $loaded_rule->rule_type == $rule->rule_type && $loaded_rule->node_types == $rule->node_types && $loaded_rule->roles == $rule->roles && $loaded_rule->visibility == $rule->visibility && $loaded_rule->pages == $rule->pages, 'Rule object was loaded properly.');
  612. // Save a second rule.
  613. $second_rule = (object) array(
  614. 'title' => 'Rule 2',
  615. 'rule_type' => 'page',
  616. 'node_types' => array(),
  617. 'roles' => array(),
  618. 'visibility' => 0, // Show on all pages, except those listed.
  619. 'pages' => '',
  620. );
  621. skinr_rule_save($second_rule);
  622. // Test loading multiple skin configurations.
  623. $rules = skinr_rule_load_multiple(array($rule->rid, $second_rule->rid));
  624. $this->assertTrue(count($rules) == 2 && isset($rules[$rule->rid]->rid) && isset($rules[$second_rule->rid]->rid), 'Successfully loaded multiple rules.');
  625. // Test loading all skin configurations.
  626. $rules = skinr_rule_load_multiple();
  627. $this->assertTrue(count($rules) == 2 && isset($rules[$rule->rid]->rid) && isset($rules[$second_rule->rid]->rid), 'Successfully loaded all rules.');
  628. }
  629. }
  630. /**
  631. * Tests API functionality.
  632. */
  633. class SkinrRulesApiTestCase extends DrupalWebTestCase {
  634. // @todo Requires http://drupal.org/node/913086
  635. // protected $profile = 'testing';
  636. public static function getInfo() {
  637. return array(
  638. 'name' => 'Rules API',
  639. 'description' => 'Tests Skinr Rules API functionality.',
  640. 'group' => 'Skinr',
  641. );
  642. }
  643. function setUp() {
  644. parent::setUp(array('skinr', 'php'));
  645. // Set up some nodes.
  646. $this->article = $this->drupalCreateNode(array(
  647. 'type' => 'article',
  648. 'title' => 'Article node',
  649. ));
  650. $this->page = $this->drupalCreateNode(array(
  651. 'type' => 'page',
  652. 'title' => 'Page node',
  653. ));
  654. // Set up some users.
  655. $this->web_user = $this->drupalCreateUser(array());
  656. }
  657. /**
  658. * Tests visibility of rules.
  659. */
  660. public function testSkinrRulesVisibility() {
  661. global $user;
  662. $front = variable_get('site_frontpage', 'node');
  663. $rule = (object) array(
  664. 'title' => 'Rule 1',
  665. 'rule_type' => 'page',
  666. 'node_types' => array(),
  667. 'roles' => array(),
  668. 'visibility' => 0, // Show on all pages, except those listed.
  669. 'pages' => '',
  670. );
  671. skinr_rule_save($rule);
  672. // Test visibility when no filters are applied.
  673. $this->assertTrue(skinr_rule_is_visible($rule->rid, $front), 'Rule is visible on front page.');
  674. $this->assertTrue(skinr_rule_is_visible($rule->rid, 'node/' . $this->article->nid), 'Rule is visible on an article node page.');
  675. $this->assertTrue(skinr_rule_is_visible($rule->rid, 'node/' . $this->page->nid), 'Rule is visible on a basic page node.');
  676. // Test visibility with a node type filter.
  677. $rule->node_types = array('article' => 'article');
  678. skinr_rule_save($rule);
  679. $this->assertFalse(skinr_rule_is_visible($rule->rid, $front), 'Node type limited rule is not visible on front page.');
  680. $this->assertTrue(skinr_rule_is_visible($rule->rid, 'node/' . $this->article->nid), 'Node type limited rule is visible on the node type.');
  681. $this->assertFalse(skinr_rule_is_visible($rule->rid, 'node/' . $this->page->nid), 'Node type limited rule is not visible on a different node type.');
  682. // Verify visibility on node/add/* paths.
  683. $this->assertTrue(skinr_rule_is_visible($rule->rid, 'node/add/article'), 'Node type limited rule is visible on the node type add page.');
  684. $this->assertFalse(skinr_rule_is_visible($rule->rid, 'node/add/page'), 'Node type limited rule is not visible on a different node type add page.');
  685. // Test visibility with a roles filter.
  686. $rule->node_types = array();
  687. $rule->roles = array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID);
  688. skinr_rule_save($rule);
  689. $user = $this->web_user;
  690. $this->assertTrue(skinr_rule_is_visible($rule->rid, $front), 'Role limited rule is visible for authenticated users.');
  691. $user = drupal_anonymous_user();
  692. $this->assertFalse(skinr_rule_is_visible($rule->rid, $front), 'Role limited rule is not visible for anonymous users.');
  693. // Test visibility with an exclude page filter.
  694. $rule->roles = array();
  695. $rule->pages = "<front>";
  696. skinr_rule_save($rule);
  697. $this->assertFalse(skinr_rule_is_visible($rule->rid, $front), 'Path excluded rule is not visible on excluded path.');
  698. $this->assertTrue(skinr_rule_is_visible($rule->rid, 'node/' . $this->article->nid), 'Path excluded rule is visible on not excluded path.');
  699. // Test visibility with an include page filter.
  700. $rule->visibility = 1;
  701. skinr_rule_save($rule);
  702. $this->assertTrue(skinr_rule_is_visible($rule->rid, $front), 'Path limited rule is visible on included path.');
  703. $this->assertFalse(skinr_rule_is_visible($rule->rid, 'node/' . $this->article->nid), 'Path limited rule is not visible on different path.');
  704. // Test visibility with a PHP page filter.
  705. $rule->visibility = 2;
  706. $rule->pages = '<?php
  707. return FALSE;
  708. ?>';
  709. skinr_rule_save($rule);
  710. $this->assertFalse(skinr_rule_is_visible($rule->rid, $front), 'PHP disabled rule is not visible on front page.');
  711. $this->assertFalse(skinr_rule_is_visible($rule->rid, 'node/' . $this->article->nid), 'PHP disabled rule is not visible on node type page.');
  712. $rule->pages = '<?php
  713. return TRUE;
  714. ?>';
  715. skinr_rule_save($rule);
  716. $this->assertTrue(skinr_rule_is_visible($rule->rid), 'PHP enabled rule is visible on front page.');
  717. $this->assertTrue(skinr_rule_is_visible($rule->rid, 'node/' . $this->article->nid), 'PHP enabled rule is visible on node type page.');
  718. }
  719. }