internals.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. /**
  3. * @file
  4. * This file contains the unit tests of the internals.
  5. */
  6. //file_put_contents('test.html', print_r(array($hierarchy, $reference), TRUE));
  7. define('SEP', '|');
  8. define('EURO', 'europe');
  9. define('EURO_BE', 'belgium');
  10. define('EURO_BE_BRU', 'brussels');
  11. define('EURO_BE_HAS', 'hasselt');
  12. define('EURO_FR', 'france');
  13. define('ASIA', 'asia');
  14. define('ASIA_CH', 'china');
  15. define('ASIA_JP', 'japan');
  16. define('ASIA_JP_TOK', 'tokyo');
  17. define('LABEL_EURO', 'Europe');
  18. define('LABEL_EURO_BE', 'Belgium');
  19. define('LABEL_EURO_BE_BRU', 'Brussels');
  20. define('LABEL_EURO_BE_HAS', 'Hasselt');
  21. define('LABEL_EURO_FR', 'France');
  22. define('LABEL_ASIA', 'Asia');
  23. define('LABEL_ASIA_CH', 'China');
  24. define('LABEL_ASIA_JP', 'Japan');
  25. define('LABEL_ASIA_JP_TOK', 'tokyo');
  26. define('LINEAGE_EURO', implode(SEP, array(EURO)));
  27. define('LINEAGE_EURO_BE', implode(SEP, array(EURO, EURO_BE)));
  28. define('LINEAGE_EURO_BE_BRU', implode(SEP, array(EURO, EURO_BE, EURO_BE_BRU)));
  29. define('LINEAGE_EURO_BE_HAS', implode(SEP, array(EURO, EURO_BE, EURO_BE_HAS)));
  30. define('LINEAGE_EURO_FR', implode(SEP, array(EURO, EURO_FR)));
  31. define('LINEAGE_ASIA', implode(SEP, array(ASIA)));
  32. define('LINEAGE_ASIA_CH', implode(SEP, array(ASIA, ASIA_CH)));
  33. define('LINEAGE_ASIA_JP', implode(SEP, array(ASIA, ASIA_JP)));
  34. define('LINEAGE_ASIA_JP_TOK', implode(SEP, array(ASIA, ASIA_JP, ASIA_JP_TOK)));
  35. class HierarchicalSelectInternals extends DrupalWebTestCase {
  36. private $small_hierarchy = array(
  37. EURO => array(
  38. 'label' => LABEL_EURO,
  39. 'children' => array(
  40. EURO_BE => array(
  41. 'label' => LABEL_EURO_BE,
  42. 'children' => array(
  43. EURO_BE_BRU => array(
  44. 'label' => LABEL_EURO_BE_BRU,
  45. ),
  46. EURO_BE_HAS => array(
  47. 'label' => LABEL_EURO_BE_HAS,
  48. ),
  49. ),
  50. ),
  51. EURO_FR => array(
  52. 'label' => LABEL_EURO_FR,
  53. ),
  54. ),
  55. ),
  56. ASIA => array(
  57. 'label' => LABEL_ASIA,
  58. 'children' => array(
  59. ASIA_CH => array(
  60. 'label' => LABEL_ASIA_CH,
  61. ),
  62. ASIA_JP => array(
  63. 'label' => LABEL_ASIA_JP,
  64. 'children' => array(
  65. ASIA_JP_TOK => array(
  66. 'label' => LABEL_ASIA_JP_TOK,
  67. ),
  68. ),
  69. ),
  70. ),
  71. ),
  72. );
  73. /**
  74. * Implementation of getInfo().
  75. */
  76. public function getInfo() {
  77. return array(
  78. 'name' => 'Internals',
  79. 'description' => 'Checks whether all internals are working: the
  80. building of the hierarchy and dropbox objects.',
  81. 'group' => 'Hierarchical Select',
  82. );
  83. }
  84. /**
  85. * Implementation of setUp().
  86. */
  87. public function setUp() {
  88. parent::setUp('hierarchical_select', 'hs_smallhierarchy');
  89. }
  90. // In this test, all settings are disabled.
  91. public function testAllSettingsOff() {
  92. // Generate form item.
  93. $form_item = array(
  94. '#required' => FALSE,
  95. '#config' => array(
  96. 'module' => 'hs_smallhierarchy',
  97. 'params' => array(
  98. 'hierarchy' => $this->small_hierarchy,
  99. 'id' => 'driverpack_platforms',
  100. 'separator' => '|',
  101. ),
  102. 'save_lineage' => 0,
  103. 'enforce_deepest' => 0,
  104. 'resizable' => 1,
  105. 'level_labels' => array(
  106. 'status' => 0,
  107. ),
  108. 'dropbox' => array(
  109. 'status' => 0,
  110. 'limit' => 0,
  111. 'reset_hs' => 1,
  112. ),
  113. 'editability' => array(
  114. 'status' => 0,
  115. 'item_types' => array(),
  116. 'allowed_levels' => array(),
  117. 'allow_new_levels' => 0,
  118. 'max_levels' => 3,
  119. ),
  120. 'entity_count' => array(
  121. 'enabled' => 0,
  122. 'require_entity' => 0,
  123. 'settings' => array(
  124. 'count_children' => 0,
  125. 'entity_types' => array(),
  126. ),
  127. ),
  128. 'animation_delay' => 400,
  129. 'exclusive_lineages' => array(),
  130. 'render_flat_select' => 0,
  131. ),
  132. );
  133. // No selection
  134. list($hierarchy, $dropbox) = $this->generate($form_item, array(), array());
  135. $reference = new stdClass();
  136. $reference->lineage = array(
  137. 0 => 'none',
  138. );
  139. $reference->levels = array(
  140. 0 => array(
  141. 'none' => '<none>',
  142. LINEAGE_EURO => LABEL_EURO,
  143. LINEAGE_ASIA => LABEL_ASIA,
  144. ),
  145. );
  146. $reference->childinfo = array(
  147. 0 => array(
  148. LINEAGE_EURO => 2,
  149. LINEAGE_ASIA => 2,
  150. ),
  151. );
  152. $this->assertHierarchy($hierarchy, $reference);
  153. // Europe
  154. list($hierarchy, $dropbox) = $this->generate($form_item, array(LINEAGE_EURO), array());
  155. $reference->lineage = array(
  156. 0 => LINEAGE_EURO,
  157. 1 => 'label_1',
  158. );
  159. $reference->levels[1] = array(
  160. 'label_1' => '',
  161. LINEAGE_EURO_BE => LABEL_EURO_BE,
  162. LINEAGE_EURO_FR => LABEL_EURO_FR,
  163. );
  164. $reference->childinfo[1] = array(
  165. LINEAGE_EURO_BE => 2,
  166. LINEAGE_EURO_FR => 0,
  167. );
  168. $this->assertHierarchy($hierarchy, $reference);
  169. // Europe > France
  170. list($hierarchy, $dropbox) = $this->generate($form_item, array(LINEAGE_EURO_FR), array());
  171. $reference->lineage = array(
  172. 0 => LINEAGE_EURO,
  173. 1 => LINEAGE_EURO_FR,
  174. );
  175. $this->assertHierarchy($hierarchy, $reference);
  176. // Europe > Belgium
  177. list($hierarchy, $dropbox) = $this->generate($form_item, array(LINEAGE_EURO_BE), array());
  178. $reference->lineage = array(
  179. 0 => LINEAGE_EURO,
  180. 1 => LINEAGE_EURO_BE,
  181. 2 => 'label_2',
  182. );
  183. $reference->levels[1] = array(
  184. 'label_1' => '',
  185. LINEAGE_EURO_BE => LABEL_EURO_BE,
  186. LINEAGE_EURO_FR => LABEL_EURO_FR,
  187. );
  188. $reference->levels[2] = array(
  189. 'label_2' => '',
  190. LINEAGE_EURO_BE_BRU => LABEL_EURO_BE_BRU,
  191. LINEAGE_EURO_BE_HAS => LABEL_EURO_BE_HAS,
  192. );
  193. $reference->childinfo[1] = array(
  194. LINEAGE_EURO_BE => 2,
  195. LINEAGE_EURO_FR => 0,
  196. );
  197. $reference->childinfo[2] = array(
  198. LINEAGE_EURO_BE_BRU => 0,
  199. LINEAGE_EURO_BE_HAS => 0,
  200. );
  201. $this->assertHierarchy($hierarchy, $reference);
  202. // Asia
  203. list($hierarchy, $dropbox) = $this->generate($form_item, array(LINEAGE_ASIA), array());
  204. $reference->lineage = array(
  205. 0 => LINEAGE_ASIA,
  206. 1 => 'label_1',
  207. );
  208. $reference->levels[1] = array(
  209. 'label_1' => '',
  210. LINEAGE_ASIA_CH => LABEL_ASIA_CH,
  211. LINEAGE_ASIA_JP => LABEL_ASIA_JP,
  212. );
  213. unset($reference->levels[2]);
  214. $reference->childinfo[1] = array(
  215. LINEAGE_ASIA_CH => 0,
  216. LINEAGE_ASIA_JP => 1,
  217. );
  218. unset($reference->childinfo[2]);
  219. $this->assertHierarchy($hierarchy, $reference);
  220. // Asia > Japan > Tokyo
  221. list($hierarchy, $dropbox) = $this->generate($form_item, array(LINEAGE_ASIA_JP_TOK), array());
  222. $reference->lineage = array(
  223. 0 => LINEAGE_ASIA,
  224. 1 => LINEAGE_ASIA_JP,
  225. 2 => LINEAGE_ASIA_JP_TOK,
  226. );
  227. $reference->levels[2] = array(
  228. 'label_2' => '',
  229. LINEAGE_ASIA_JP_TOK => LABEL_ASIA_JP_TOK,
  230. );
  231. $reference->childinfo[2] = array(
  232. LINEAGE_ASIA_JP_TOK => 0,
  233. );
  234. $this->assertHierarchy($hierarchy, $reference);
  235. }
  236. // In this test, only enforce_deepest enabled.
  237. public function testEnforceDeepest() {
  238. // Generate form item.
  239. $form_item = array(
  240. '#required' => FALSE,
  241. '#config' => array(
  242. 'module' => 'hs_smallhierarchy',
  243. 'params' => array(
  244. 'hierarchy' => $this->small_hierarchy,
  245. 'id' => 'driverpack_platforms',
  246. 'separator' => '|',
  247. ),
  248. 'save_lineage' => 0,
  249. 'enforce_deepest' => 1,
  250. 'resizable' => 1,
  251. 'level_labels' => array(
  252. 'status' => 0,
  253. ),
  254. 'dropbox' => array(
  255. 'status' => 0,
  256. 'limit' => 0,
  257. 'reset_hs' => 1,
  258. ),
  259. 'editability' => array(
  260. 'status' => 0,
  261. 'item_types' => array(),
  262. 'allowed_levels' => array(),
  263. 'allow_new_levels' => 0,
  264. 'max_levels' => 3,
  265. ),
  266. 'entity_count' => array(
  267. 'enabled' => 0,
  268. 'require_entity' => 0,
  269. 'settings' => array(
  270. 'count_children' => 0,
  271. 'entity_types' => array(),
  272. ),
  273. ),
  274. 'animation_delay' => 400,
  275. 'exclusive_lineages' => array(),
  276. 'render_flat_select' => 0,
  277. ),
  278. );
  279. // No selection
  280. list($hierarchy, $dropbox) = $this->generate($form_item, array(), array());
  281. $reference = new stdClass();
  282. $reference->lineage = array(
  283. 0 => 'label_0',
  284. );
  285. $reference->levels = array(
  286. 0 => array(
  287. 'none' => '<none>',
  288. LINEAGE_EURO => LABEL_EURO,
  289. LINEAGE_ASIA => LABEL_ASIA,
  290. ),
  291. );
  292. $reference->childinfo = array(
  293. 0 => array(
  294. LINEAGE_EURO => 2,
  295. LINEAGE_ASIA => 2,
  296. ),
  297. );
  298. $this->assertHierarchy($hierarchy, $reference);
  299. // Europe
  300. list($hierarchy, $dropbox) = $this->generate($form_item, array(LINEAGE_EURO), array());
  301. $reference->lineage = array(
  302. 0 => LINEAGE_EURO,
  303. 1 => LINEAGE_EURO_BE,
  304. 2 => LINEAGE_EURO_BE_BRU,
  305. );
  306. $reference->levels[1] = array(
  307. LINEAGE_EURO_BE => LABEL_EURO_BE,
  308. LINEAGE_EURO_FR => LABEL_EURO_FR,
  309. );
  310. $reference->levels[2] = array(
  311. LINEAGE_EURO_BE_BRU => LABEL_EURO_BE_BRU,
  312. LINEAGE_EURO_BE_HAS => LABEL_EURO_BE_HAS,
  313. );
  314. $reference->childinfo[1] = array(
  315. LINEAGE_EURO_BE => 2,
  316. LINEAGE_EURO_FR => 0,
  317. );
  318. $reference->childinfo[2] = array(
  319. LINEAGE_EURO_BE_BRU => 0,
  320. LINEAGE_EURO_BE_HAS => 0,
  321. );
  322. $this->assertHierarchy($hierarchy, $reference);
  323. // Europe > France
  324. list($hierarchy, $dropbox) = $this->generate($form_item, array(LINEAGE_EURO_FR), array());
  325. $reference->lineage = array(
  326. 0 => LINEAGE_EURO,
  327. 1 => LINEAGE_EURO_FR,
  328. );
  329. unset($reference->levels[2]);
  330. unset($reference->childinfo[2]);
  331. $this->assertHierarchy($hierarchy, $reference);
  332. // Europe > Belgium
  333. list($hierarchy, $dropbox) = $this->generate($form_item, array(LINEAGE_EURO_BE), array());
  334. $reference->lineage = array(
  335. 0 => LINEAGE_EURO,
  336. 1 => LINEAGE_EURO_BE,
  337. 2 => LINEAGE_EURO_BE_BRU,
  338. );
  339. $reference->levels[1] = array(
  340. LINEAGE_EURO_BE => LABEL_EURO_BE,
  341. LINEAGE_EURO_FR => LABEL_EURO_FR,
  342. );
  343. $reference->levels[2] = array(
  344. LINEAGE_EURO_BE_BRU => LABEL_EURO_BE_BRU,
  345. LINEAGE_EURO_BE_HAS => LABEL_EURO_BE_HAS,
  346. );
  347. $reference->childinfo[1] = array(
  348. LINEAGE_EURO_BE => 2,
  349. LINEAGE_EURO_FR => 0,
  350. );
  351. $reference->childinfo[2] = array(
  352. LINEAGE_EURO_BE_BRU => 0,
  353. LINEAGE_EURO_BE_HAS => 0,
  354. );
  355. $this->assertHierarchy($hierarchy, $reference);
  356. // Asia
  357. list($hierarchy, $dropbox) = $this->generate($form_item, array(LINEAGE_ASIA), array());
  358. $reference->lineage = array(
  359. 0 => LINEAGE_ASIA,
  360. 1 => LINEAGE_ASIA_CH,
  361. );
  362. $reference->levels[1] = array(
  363. LINEAGE_ASIA_CH => LABEL_ASIA_CH,
  364. LINEAGE_ASIA_JP => LABEL_ASIA_JP,
  365. );
  366. unset($reference->levels[2]);
  367. $reference->childinfo[1] = array(
  368. LINEAGE_ASIA_CH => 0,
  369. LINEAGE_ASIA_JP => 1,
  370. );
  371. unset($reference->childinfo[2]);
  372. $this->assertHierarchy($hierarchy, $reference);
  373. // Asia > Japan > Tokyo
  374. list($hierarchy, $dropbox) = $this->generate($form_item, array(LINEAGE_ASIA_JP_TOK), array());
  375. $reference->lineage = array(
  376. 0 => LINEAGE_ASIA,
  377. 1 => LINEAGE_ASIA_JP,
  378. 2 => LINEAGE_ASIA_JP_TOK,
  379. );
  380. $reference->levels[2] = array(
  381. LINEAGE_ASIA_JP_TOK => LABEL_ASIA_JP_TOK,
  382. );
  383. $reference->childinfo[2] = array(
  384. LINEAGE_ASIA_JP_TOK => 0,
  385. );
  386. $this->assertHierarchy($hierarchy, $reference);
  387. }
  388. //--------------------------------------------------------------------------
  389. // Private methods.
  390. private function generate($element, $hs_selection, $db_selection, $op = 'Update') {
  391. $config = $element['#config'];
  392. // Generate the $hierarchy and $dropbox objects using the selections that
  393. // were just calculated.
  394. $dropbox = (!$config['dropbox']['status']) ? FALSE : _hierarchical_select_dropbox_generate($config, $db_selection);
  395. $hierarchy = _hierarchical_select_hierarchy_generate($config, $hs_selection, $element['#required'], $dropbox);
  396. return array($hierarchy, $dropbox);
  397. }
  398. private function assertHierarchy($hierarchy, $reference) {
  399. $this->assertIdentical($hierarchy->lineage, $reference->lineage, 'Hierarchy lineage is correct.');
  400. $this->assertIdentical($hierarchy->levels, $reference->levels, 'Hierarchy levels is correct.');
  401. $this->assertIdentical($hierarchy->childinfo, $reference->childinfo, 'Hierarchy child info is correct.');
  402. }
  403. }