ctools.test 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * @file Test classes for code in the CTools module file.
  4. */
  5. /**
  6. * Test menu links depending on user permissions.
  7. */
  8. class CtoolsModuleTestCase extends DrupalWebTestCase {
  9. /**
  10. * {@inheritdoc}
  11. */
  12. public static function getInfo() {
  13. return array(
  14. 'name' => 'Ctools module functions tests',
  15. 'description' => 'Check functions in the ctools.module not otherwise tested.',
  16. 'group' => 'ctools',
  17. 'dependencies' => array('ctools'),
  18. );
  19. }
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function setUp(array $modules = array()) {
  24. $modules[] = 'ctools';
  25. parent::setUp($modules);
  26. }
  27. /**
  28. * Test that the break phrase function behaves as expected.
  29. */
  30. public function testBreakPhrase() {
  31. $tests = array(
  32. NULL => array('value' => array()),
  33. '' => array('value' => array()),
  34. '1' => array('operator' => 'and', 'value' => array(1)),
  35. '99' => array('operator' => 'and', 'value' => array(99)),
  36. '+1' => array('invalid_input' => TRUE, 'value' => array(-1)),
  37. ' 1' => array('invalid_input' => TRUE, 'value' => array(-1)),
  38. '1 ' => array('invalid_input' => TRUE, 'value' => array(-1)),
  39. '-1' => array('invalid_input' => TRUE, 'value' => array(-1)),
  40. '-99' => array('invalid_input' => TRUE, 'value' => array(-1)),
  41. '1,2' => array('operator' => 'and', 'value' => array(1, 2)),
  42. '1 2' => array('operator' => 'or', 'value' => array(1, 2)),
  43. '1+2' => array('operator' => 'or', 'value' => array(1, 2)),
  44. '1,2,3' => array('operator' => 'and', 'value' => array(1, 2, 3)),
  45. '1 2 3' => array('operator' => 'or', 'value' => array(1, 2, 3)),
  46. '1+2+3' => array('operator' => 'or', 'value' => array(1, 2, 3)),
  47. '1 , 2 , 3' => array('invalid_input' => TRUE, 'value' => array(-1)),
  48. '1 + 2 + 3' => array('invalid_input' => TRUE, 'value' => array(-1)),
  49. '1,2,3,4,5,6,7,8,9' => array(
  50. 'operator' => 'and',
  51. 'value' => array(1, 2, 3, 4, 5, 6, 7, 8, 9),
  52. ),
  53. '1 2,3,4 5 6 7 8 9' => array('invalid_input' => TRUE, 'value' => array(-1)),
  54. );
  55. foreach ($tests as $string => $expected) {
  56. $result = ctools_break_phrase($string);
  57. $expected = (object) $expected;
  58. $this->assertEqual($result, $expected, 'Break Phrase test patterns: ' . $string);
  59. }
  60. }
  61. /**
  62. * Test that the (deprecated) getuserroles returns expected array.
  63. */
  64. public function testGetUserRoles() {
  65. $result = ctools_get_roles();
  66. $this->assertTrue(is_array($result), 'get_roles returns an array');
  67. // A key-value array of integers.
  68. foreach ($result as $k => $v) {
  69. $this->assertTrue(is_numeric($k), 'Role key is numeric; ' . $k);
  70. $this->assertTrue(is_string($v), 'Role id is string; ' . $v);
  71. }
  72. }
  73. /**
  74. * Test the ctools_attach_js function returns the expected paths.
  75. */
  76. public function testAttachJs() {
  77. $taxonomy_path = drupal_get_path('module', 'taxonomy');
  78. $ctools_path = drupal_get_path('module', 'ctools');
  79. // Func should probably do a different thing but this is current behaviour.
  80. $path = ctools_attach_js('');
  81. $this->assertEqual($path, $ctools_path . '/js/.js', 'Attach an empty string');
  82. $path = ctools_attach_js('foo');
  83. $this->assertEqual($path, $ctools_path . '/js/foo.js', 'Attach simple string');
  84. $path = ctools_attach_js('foo', 'ctools', '');
  85. $this->assertEqual($path, $ctools_path . '//foo.js', 'Attach string with empty subdir');
  86. $path = ctools_attach_js('foo', 'ctools', 'javascript');
  87. $this->assertEqual($path, $ctools_path . '/javascript/foo.js', 'Attach string with alternate subdir');
  88. $path = ctools_attach_js('foo', 'taxonomy', 'javascript');
  89. $this->assertEqual($path, $taxonomy_path . '/javascript/foo.js', 'Attach string from different module');
  90. }
  91. /**
  92. * Test the ctools_attach_css function returns the expected paths.
  93. */
  94. public function testAttachCss() {
  95. $taxonomy_path = drupal_get_path('module', 'taxonomy');
  96. $ctools_path = drupal_get_path('module', 'ctools');
  97. // Func should probably do a different thing but this is current behaviour.
  98. $path = ctools_attach_css('');
  99. $this->assertEqual($path, $ctools_path . '/css/.css', 'Attach empty string');
  100. $path = ctools_attach_css('foo');
  101. $this->assertEqual($path, $ctools_path . '/css/foo.css', 'Attach simple string');
  102. $path = ctools_attach_css('foo', 'ctools', '');
  103. $this->assertEqual($path, $ctools_path . '//foo.css', 'Attach string with empty subdir');
  104. $path = ctools_attach_css('foo', 'ctools', 'theme');
  105. $this->assertEqual($path, $ctools_path . '/theme/foo.css', 'Attach string with alternate subdir');
  106. $path = ctools_attach_css('foo', 'taxonomy', 'theme');
  107. $this->assertEqual($path, $taxonomy_path . '/theme/foo.css', 'Attach string from different module');
  108. }
  109. /**
  110. * Test the ctools version compare function.
  111. */
  112. public function testApiVersionCompare() {
  113. // We're beyond version 1.
  114. $ok = ctools_api_version('1.0');
  115. $this->assertTrue($ok, 'Check API version 1.0 is ok');
  116. // We're beyond version 1.0.1 too.
  117. $ok = ctools_api_version('1.0.1');
  118. $this->assertTrue($ok, 'Check API version 1.0.1 is ok');
  119. // Not (yet) on api version 10.
  120. $ok = ctools_api_version('10.0');
  121. $this->assertFalse($ok, 'Check API version 10.0 is not ok');
  122. // We are (currently) between version 1.1 and version 4.0.
  123. $ok = ctools_api_version('1.1', '4.0');
  124. $this->assertTrue($ok, 'Check API is between 1 and 4');
  125. }
  126. /**
  127. * Test that the ctools_classs_add works.
  128. */
  129. public function testClassesAdd() {
  130. ctools_class_reset();
  131. ctools_class_add('testclass');
  132. $classes = ctools_get_classes();
  133. $this->assertEqual(is_array($classes), 1, 'Classes should be an array');
  134. $this->assertEqual(count($classes), 1, 'Classes array has one element');
  135. $this->assertEqual(count($classes['html']), 1, 'Classes array has element: html');
  136. $this->assertTrue(isset($classes['html']['add']), 'Classes array has element: html/add');
  137. $this->assertEqual($classes['html']['add'], array('testclass'), 'Classes array has expected value');
  138. ctools_class_add('class2 class3');
  139. $classes = ctools_get_classes();
  140. $this->assertEqual(is_array($classes), 1, 'Classes should be an array');
  141. $this->assertEqual(count($classes['html']), 1, 'Classes array has element: html');
  142. // TODO: An undesirable result: array('testclass', 'class2', 'class3') is better.
  143. $this->assertEqual($classes['html']['add'], array(
  144. 'testclass',
  145. 'class2 class3',
  146. ), 'Classes array has expected value');
  147. }
  148. /**
  149. * Test that the ctools_classs_remove works.
  150. */
  151. public function testClassesRemove() {
  152. ctools_class_reset();
  153. ctools_class_remove('testclass');
  154. $classes = ctools_get_classes();
  155. $this->assertEqual(is_array($classes), 1, 'Classes should be an array');
  156. $this->assertEqual(count($classes), 1, 'Classes array has one element');
  157. $this->assertEqual(count($classes['html']), 1, 'Classes array has element: html');
  158. $this->assertTrue(isset($classes['html']['remove']), 'Classes array has element: html/remove');
  159. $this->assertEqual($classes['html']['remove'], array('testclass'), 'Classes array has expected value');
  160. ctools_class_remove('class2 class3');
  161. $classes = ctools_get_classes();
  162. $this->assertEqual(count($classes), 1, 'Classes array has one element');
  163. $this->assertEqual(count($classes['html']), 1, 'Classes array has element: html');
  164. // This is an undesirable result, is array('testclass', 'class2', 'class3') better.
  165. $this->assertEqual($classes['html']['remove'], array(
  166. 'testclass',
  167. 'class2 class3',
  168. ), 'Classes array has expected value');
  169. }
  170. /**
  171. * Test that the ctools_classs_add and ctools_classs_remove interact well.
  172. */
  173. public function testClassesAddRemove() {
  174. ctools_class_reset();
  175. ctools_class_add('testclass');
  176. ctools_class_remove('testclass');
  177. $classes = ctools_get_classes();
  178. $this->assertTrue(isset($classes['html']['add']), 'Classes array has an add set');
  179. $this->assertEqual($classes['html']['add'], array('testclass'), 'testclass is in the add set');
  180. $this->assertTrue(isset($classes['html']['remove']), 'Classes array has a remove set');
  181. // TODO: Is it really good to let this happen?
  182. $this->assertEqual($classes['html']['remove'], array('testclass'), 'testclass is in the remove set');
  183. }
  184. /**
  185. * Test that the ctools_classs_add and ctools_classs_remove interact well .. 2.
  186. */
  187. public function testClassesAddRemove2() {
  188. ctools_class_reset();
  189. ctools_class_add('class2 class3');
  190. ctools_class_remove('class3');
  191. $classes = ctools_get_classes();
  192. $this->assertTrue(isset($classes['html']['add']), 'Classes array has an add set');
  193. $this->assertEqual($classes['html']['add'], array('class2 class3'), 'Added class2 class3 is in add set');
  194. $this->assertTrue(isset($classes['html']['remove']), 'Classes array has a remove set');
  195. // TODO: Is it really good to let this happen?
  196. $this->assertEqual($classes['html']['remove'], array('class3'), 'class3 in remove set');
  197. }
  198. }