HashTest.php 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @package Cake.Utility
  13. * @since CakePHP(tm) v 2.2.0
  14. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. App::uses('Hash', 'Utility');
  17. /**
  18. * Class HashTest
  19. *
  20. * @package Cake.Utility
  21. */
  22. class HashTest extends CakeTestCase {
  23. /**
  24. * Data provider
  25. *
  26. * @return array
  27. */
  28. public static function articleData() {
  29. return array(
  30. array(
  31. 'Article' => array(
  32. 'id' => '1',
  33. 'user_id' => '1',
  34. 'title' => 'First Article',
  35. 'body' => 'First Article Body'
  36. ),
  37. 'User' => array(
  38. 'id' => '1',
  39. 'user' => 'mariano',
  40. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  41. ),
  42. 'Comment' => array(
  43. array(
  44. 'id' => '1',
  45. 'article_id' => '1',
  46. 'user_id' => '2',
  47. 'comment' => 'First Comment for First Article',
  48. ),
  49. array(
  50. 'id' => '2',
  51. 'article_id' => '1',
  52. 'user_id' => '4',
  53. 'comment' => 'Second Comment for First Article',
  54. ),
  55. ),
  56. 'Tag' => array(
  57. array(
  58. 'id' => '1',
  59. 'tag' => 'tag1',
  60. ),
  61. array(
  62. 'id' => '2',
  63. 'tag' => 'tag2',
  64. )
  65. ),
  66. 'Deep' => array(
  67. 'Nesting' => array(
  68. 'test' => array(
  69. 1 => 'foo',
  70. 2 => array(
  71. 'and' => array('more' => 'stuff')
  72. )
  73. )
  74. )
  75. )
  76. ),
  77. array(
  78. 'Article' => array(
  79. 'id' => '2',
  80. 'user_id' => '1',
  81. 'title' => 'Second Article',
  82. 'body' => 'Second Article Body',
  83. 'published' => 'Y',
  84. ),
  85. 'User' => array(
  86. 'id' => '2',
  87. 'user' => 'mariano',
  88. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  89. ),
  90. 'Comment' => array(),
  91. 'Tag' => array()
  92. ),
  93. array(
  94. 'Article' => array(
  95. 'id' => '3',
  96. 'user_id' => '1',
  97. 'title' => 'Third Article',
  98. 'body' => 'Third Article Body',
  99. ),
  100. 'User' => array(
  101. 'id' => '3',
  102. 'user' => 'mariano',
  103. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  104. ),
  105. 'Comment' => array(),
  106. 'Tag' => array()
  107. ),
  108. array(
  109. 'Article' => array(
  110. 'id' => '4',
  111. 'user_id' => '1',
  112. 'title' => 'Fourth Article',
  113. 'body' => 'Fourth Article Body',
  114. ),
  115. 'User' => array(
  116. 'id' => '4',
  117. 'user' => 'mariano',
  118. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  119. ),
  120. 'Comment' => array(),
  121. 'Tag' => array()
  122. ),
  123. array(
  124. 'Article' => array(
  125. 'id' => '5',
  126. 'user_id' => '1',
  127. 'title' => 'Fifth Article',
  128. 'body' => 'Fifth Article Body',
  129. ),
  130. 'User' => array(
  131. 'id' => '5',
  132. 'user' => 'mariano',
  133. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  134. ),
  135. 'Comment' => array(),
  136. 'Tag' => array()
  137. )
  138. );
  139. }
  140. /**
  141. * Data provider
  142. *
  143. * @return array
  144. */
  145. public static function userData() {
  146. return array(
  147. array(
  148. 'User' => array(
  149. 'id' => 2,
  150. 'group_id' => 1,
  151. 'Data' => array(
  152. 'user' => 'mariano.iglesias',
  153. 'name' => 'Mariano Iglesias'
  154. )
  155. )
  156. ),
  157. array(
  158. 'User' => array(
  159. 'id' => 14,
  160. 'group_id' => 2,
  161. 'Data' => array(
  162. 'user' => 'phpnut',
  163. 'name' => 'Larry E. Masters'
  164. )
  165. )
  166. ),
  167. array(
  168. 'User' => array(
  169. 'id' => 25,
  170. 'group_id' => 1,
  171. 'Data' => array(
  172. 'user' => 'gwoo',
  173. 'name' => 'The Gwoo'
  174. )
  175. )
  176. )
  177. );
  178. }
  179. /**
  180. * Test get()
  181. *
  182. * @return void
  183. */
  184. public function testGet() {
  185. $data = array('abc', 'def');
  186. $result = Hash::get($data, '0');
  187. $this->assertEquals('abc', $result);
  188. $result = Hash::get($data, 0);
  189. $this->assertEquals('abc', $result);
  190. $result = Hash::get($data, '1');
  191. $this->assertEquals('def', $result);
  192. $data = self::articleData();
  193. $result = Hash::get(array(), '1.Article.title');
  194. $this->assertNull($result);
  195. $result = Hash::get($data, '');
  196. $this->assertNull($result);
  197. $result = Hash::get($data, '0.Article.title');
  198. $this->assertEquals('First Article', $result);
  199. $result = Hash::get($data, '1.Article.title');
  200. $this->assertEquals('Second Article', $result);
  201. $result = Hash::get($data, '5.Article.title');
  202. $this->assertNull($result);
  203. $default = array('empty');
  204. $this->assertEquals($default, Hash::get($data, '5.Article.title', $default));
  205. $this->assertEquals($default, Hash::get(array(), '5.Article.title', $default));
  206. $result = Hash::get($data, '1.Article.title.not_there');
  207. $this->assertNull($result);
  208. $result = Hash::get($data, '1.Article');
  209. $this->assertEquals($data[1]['Article'], $result);
  210. $result = Hash::get($data, array('1', 'Article'));
  211. $this->assertEquals($data[1]['Article'], $result);
  212. }
  213. /**
  214. * Test get() with an invalid path
  215. *
  216. * @expectedException InvalidArgumentException
  217. * @return void
  218. */
  219. public function testGetInvalidPath() {
  220. Hash::get(array('one' => 'two'), true);
  221. }
  222. /**
  223. * Test dimensions.
  224. *
  225. * @return void
  226. */
  227. public function testDimensions() {
  228. $result = Hash::dimensions(array());
  229. $this->assertEquals($result, 0);
  230. $data = array('one', '2', 'three');
  231. $result = Hash::dimensions($data);
  232. $this->assertEquals($result, 1);
  233. $data = array('1' => '1.1', '2', '3');
  234. $result = Hash::dimensions($data);
  235. $this->assertEquals($result, 1);
  236. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1'));
  237. $result = Hash::dimensions($data);
  238. $this->assertEquals($result, 2);
  239. $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  240. $result = Hash::dimensions($data);
  241. $this->assertEquals($result, 1);
  242. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  243. $result = Hash::dimensions($data);
  244. $this->assertEquals($result, 2);
  245. }
  246. /**
  247. * Test maxDimensions
  248. *
  249. * @return void
  250. */
  251. public function testMaxDimensions() {
  252. $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  253. $result = Hash::maxDimensions($data);
  254. $this->assertEquals($result, 2);
  255. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  256. $result = Hash::maxDimensions($data);
  257. $this->assertEquals($result, 3);
  258. $data = array(
  259. '1' => array('1.1' => '1.1.1'),
  260. array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))),
  261. '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  262. );
  263. $result = Hash::maxDimensions($data);
  264. $this->assertEquals($result, 4);
  265. $data = array(
  266. '1' => array('1.1' => '1.1.1'),
  267. array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))),
  268. '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  269. );
  270. $result = Hash::maxDimensions($data);
  271. $this->assertEquals($result, 5);
  272. $data = array(
  273. '1' => array('1.1' => '1.1.1'),
  274. array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))),
  275. '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  276. );
  277. $result = Hash::maxDimensions($data);
  278. $this->assertEquals($result, 5);
  279. $data = array(
  280. '1' => array('1.1' => '1.1.1'),
  281. array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))),
  282. '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  283. );
  284. $result = Hash::maxDimensions($data);
  285. $this->assertEquals($result, 5);
  286. }
  287. /**
  288. * Tests Hash::flatten
  289. *
  290. * @return void
  291. */
  292. public function testFlatten() {
  293. $data = array('Larry', 'Curly', 'Moe');
  294. $result = Hash::flatten($data);
  295. $this->assertEquals($result, $data);
  296. $data[9] = 'Shemp';
  297. $result = Hash::flatten($data);
  298. $this->assertEquals($result, $data);
  299. $data = array(
  300. array(
  301. 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post'),
  302. 'Author' => array('id' => '1', 'user' => 'nate', 'password' => 'foo'),
  303. ),
  304. array(
  305. 'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body'),
  306. 'Author' => array('id' => '3', 'user' => 'larry', 'password' => null),
  307. )
  308. );
  309. $result = Hash::flatten($data);
  310. $expected = array(
  311. '0.Post.id' => '1',
  312. '0.Post.author_id' => '1',
  313. '0.Post.title' => 'First Post',
  314. '0.Author.id' => '1',
  315. '0.Author.user' => 'nate',
  316. '0.Author.password' => 'foo',
  317. '1.Post.id' => '2',
  318. '1.Post.author_id' => '3',
  319. '1.Post.title' => 'Second Post',
  320. '1.Post.body' => 'Second Post Body',
  321. '1.Author.id' => '3',
  322. '1.Author.user' => 'larry',
  323. '1.Author.password' => null
  324. );
  325. $this->assertEquals($expected, $result);
  326. $data = array(
  327. array(
  328. 'Post' => array('id' => '1', 'author_id' => null, 'title' => 'First Post'),
  329. 'Author' => array(),
  330. )
  331. );
  332. $result = Hash::flatten($data);
  333. $expected = array(
  334. '0.Post.id' => '1',
  335. '0.Post.author_id' => null,
  336. '0.Post.title' => 'First Post',
  337. '0.Author' => array()
  338. );
  339. $this->assertEquals($expected, $result);
  340. $data = array(
  341. array('Post' => array('id' => 1)),
  342. array('Post' => array('id' => 2)),
  343. );
  344. $result = Hash::flatten($data, '/');
  345. $expected = array(
  346. '0/Post/id' => '1',
  347. '1/Post/id' => '2',
  348. );
  349. $this->assertEquals($expected, $result);
  350. }
  351. /**
  352. * Test diff();
  353. *
  354. * @return void
  355. */
  356. public function testDiff() {
  357. $a = array(
  358. 0 => array('name' => 'main'),
  359. 1 => array('name' => 'about')
  360. );
  361. $b = array(
  362. 0 => array('name' => 'main'),
  363. 1 => array('name' => 'about'),
  364. 2 => array('name' => 'contact')
  365. );
  366. $result = Hash::diff($a, array());
  367. $expected = $a;
  368. $this->assertEquals($expected, $result);
  369. $result = Hash::diff(array(), $b);
  370. $expected = $b;
  371. $this->assertEquals($expected, $result);
  372. $result = Hash::diff($a, $b);
  373. $expected = array(
  374. 2 => array('name' => 'contact')
  375. );
  376. $this->assertEquals($expected, $result);
  377. $b = array(
  378. 0 => array('name' => 'me'),
  379. 1 => array('name' => 'about')
  380. );
  381. $result = Hash::diff($a, $b);
  382. $expected = array(
  383. 0 => array('name' => 'main')
  384. );
  385. $this->assertEquals($expected, $result);
  386. $a = array();
  387. $b = array('name' => 'bob', 'address' => 'home');
  388. $result = Hash::diff($a, $b);
  389. $this->assertEquals($result, $b);
  390. $a = array('name' => 'bob', 'address' => 'home');
  391. $b = array();
  392. $result = Hash::diff($a, $b);
  393. $this->assertEquals($result, $a);
  394. $a = array('key' => true, 'another' => false, 'name' => 'me');
  395. $b = array('key' => 1, 'another' => 0);
  396. $expected = array('name' => 'me');
  397. $result = Hash::diff($a, $b);
  398. $this->assertEquals($expected, $result);
  399. $a = array('key' => 'value', 'another' => null, 'name' => 'me');
  400. $b = array('key' => 'differentValue', 'another' => null);
  401. $expected = array('key' => 'value', 'name' => 'me');
  402. $result = Hash::diff($a, $b);
  403. $this->assertEquals($expected, $result);
  404. $a = array('key' => 'value', 'another' => null, 'name' => 'me');
  405. $b = array('key' => 'differentValue', 'another' => 'value');
  406. $expected = array('key' => 'value', 'another' => null, 'name' => 'me');
  407. $result = Hash::diff($a, $b);
  408. $this->assertEquals($expected, $result);
  409. $a = array('key' => 'value', 'another' => null, 'name' => 'me');
  410. $b = array('key' => 'differentValue', 'another' => 'value');
  411. $expected = array('key' => 'differentValue', 'another' => 'value', 'name' => 'me');
  412. $result = Hash::diff($b, $a);
  413. $this->assertEquals($expected, $result);
  414. $a = array('key' => 'value', 'another' => null, 'name' => 'me');
  415. $b = array(0 => 'differentValue', 1 => 'value');
  416. $expected = $a + $b;
  417. $result = Hash::diff($a, $b);
  418. $this->assertEquals($expected, $result);
  419. }
  420. /**
  421. * Test merge()
  422. *
  423. * @return void
  424. */
  425. public function testMerge() {
  426. $result = Hash::merge(array('foo'), array('bar'));
  427. $this->assertEquals($result, array('foo', 'bar'));
  428. $result = Hash::merge(array('foo'), array('user' => 'bob', 'no-bar'), 'bar');
  429. $this->assertEquals($result, array('foo', 'user' => 'bob', 'no-bar', 'bar'));
  430. $a = array('foo', 'foo2');
  431. $b = array('bar', 'bar2');
  432. $expected = array('foo', 'foo2', 'bar', 'bar2');
  433. $this->assertEquals($expected, Hash::merge($a, $b));
  434. $a = array('foo' => 'bar', 'bar' => 'foo');
  435. $b = array('foo' => 'no-bar', 'bar' => 'no-foo');
  436. $expected = array('foo' => 'no-bar', 'bar' => 'no-foo');
  437. $this->assertEquals($expected, Hash::merge($a, $b));
  438. $a = array('users' => array('bob', 'jim'));
  439. $b = array('users' => array('lisa', 'tina'));
  440. $expected = array('users' => array('bob', 'jim', 'lisa', 'tina'));
  441. $this->assertEquals($expected, Hash::merge($a, $b));
  442. $a = array('users' => array('jim', 'bob'));
  443. $b = array('users' => 'none');
  444. $expected = array('users' => 'none');
  445. $this->assertEquals($expected, Hash::merge($a, $b));
  446. $a = array('users' => array('lisa' => array('id' => 5, 'pw' => 'secret')), 'cakephp');
  447. $b = array('users' => array('lisa' => array('pw' => 'new-pass', 'age' => 23)), 'ice-cream');
  448. $expected = array(
  449. 'users' => array('lisa' => array('id' => 5, 'pw' => 'new-pass', 'age' => 23)),
  450. 'cakephp',
  451. 'ice-cream'
  452. );
  453. $result = Hash::merge($a, $b);
  454. $this->assertEquals($expected, $result);
  455. $c = array(
  456. 'users' => array('lisa' => array('pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')),
  457. 'chocolate'
  458. );
  459. $expected = array(
  460. 'users' => array('lisa' => array('id' => 5, 'pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')),
  461. 'cakephp',
  462. 'ice-cream',
  463. 'chocolate'
  464. );
  465. $this->assertEquals($expected, Hash::merge($a, $b, $c));
  466. $this->assertEquals($expected, Hash::merge($a, $b, array(), $c));
  467. $a = array(
  468. 'Tree',
  469. 'CounterCache',
  470. 'Upload' => array(
  471. 'folder' => 'products',
  472. 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')
  473. )
  474. );
  475. $b = array(
  476. 'Cacheable' => array('enabled' => false),
  477. 'Limit',
  478. 'Bindable',
  479. 'Validator',
  480. 'Transactional'
  481. );
  482. $expected = array(
  483. 'Tree',
  484. 'CounterCache',
  485. 'Upload' => array(
  486. 'folder' => 'products',
  487. 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')
  488. ),
  489. 'Cacheable' => array('enabled' => false),
  490. 'Limit',
  491. 'Bindable',
  492. 'Validator',
  493. 'Transactional'
  494. );
  495. $this->assertEquals($expected, Hash::merge($a, $b));
  496. }
  497. /**
  498. * test normalizing arrays
  499. *
  500. * @return void
  501. */
  502. public function testNormalize() {
  503. $result = Hash::normalize(array('one', 'two', 'three'));
  504. $expected = array('one' => null, 'two' => null, 'three' => null);
  505. $this->assertEquals($expected, $result);
  506. $result = Hash::normalize(array('one', 'two', 'three'), false);
  507. $expected = array('one', 'two', 'three');
  508. $this->assertEquals($expected, $result);
  509. $result = Hash::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'), false);
  510. $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null);
  511. $this->assertEquals($expected, $result);
  512. $result = Hash::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'));
  513. $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null);
  514. $this->assertEquals($expected, $result);
  515. $result = Hash::normalize(array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three'));
  516. $expected = array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three' => null);
  517. $this->assertEquals($expected, $result);
  518. }
  519. /**
  520. * testContains method
  521. *
  522. * @return void
  523. */
  524. public function testContains() {
  525. $data = array('apple', 'bee', 'cyclops');
  526. $this->assertTrue(Hash::contains($data, array('apple')));
  527. $this->assertFalse(Hash::contains($data, array('data')));
  528. $a = array(
  529. 0 => array('name' => 'main'),
  530. 1 => array('name' => 'about')
  531. );
  532. $b = array(
  533. 0 => array('name' => 'main'),
  534. 1 => array('name' => 'about'),
  535. 2 => array('name' => 'contact'),
  536. 'a' => 'b'
  537. );
  538. $this->assertTrue(Hash::contains($a, $a));
  539. $this->assertFalse(Hash::contains($a, $b));
  540. $this->assertTrue(Hash::contains($b, $a));
  541. $a = array(
  542. array('User' => array('id' => 1)),
  543. array('User' => array('id' => 2)),
  544. );
  545. $b = array(
  546. array('User' => array('id' => 1)),
  547. array('User' => array('id' => 2)),
  548. array('User' => array('id' => 3))
  549. );
  550. $this->assertTrue(Hash::contains($b, $a));
  551. $this->assertFalse(Hash::contains($a, $b));
  552. $a = array(0 => 'test', 'string' => null);
  553. $this->assertTrue(Hash::contains($a, array('string' => null)));
  554. $a = array(0 => 'test', 'string' => null);
  555. $this->assertTrue(Hash::contains($a, array('test')));
  556. }
  557. /**
  558. * testFilter method
  559. *
  560. * @return void
  561. */
  562. public function testFilter() {
  563. $result = Hash::filter(array('0', false, true, 0, array('one thing', 'I can tell you', 'is you got to be', false)));
  564. $expected = array('0', 2 => true, 3 => 0, 4 => array('one thing', 'I can tell you', 'is you got to be'));
  565. $this->assertSame($expected, $result);
  566. $result = Hash::filter(array(1, array(false)));
  567. $expected = array(1);
  568. $this->assertEquals($expected, $result);
  569. $result = Hash::filter(array(1, array(false, false)));
  570. $expected = array(1);
  571. $this->assertEquals($expected, $result);
  572. $result = Hash::filter(array(1, array('empty', false)));
  573. $expected = array(1, array('empty'));
  574. $this->assertEquals($expected, $result);
  575. $result = Hash::filter(array(1, array('2', false, array(3, null))));
  576. $expected = array(1, array('2', 2 => array(3)));
  577. $this->assertEquals($expected, $result);
  578. $this->assertSame(array(), Hash::filter(array()));
  579. }
  580. /**
  581. * testNumericArrayCheck method
  582. *
  583. * @return void
  584. */
  585. public function testNumeric() {
  586. $data = array('one');
  587. $this->assertTrue(Hash::numeric(array_keys($data)));
  588. $data = array(1 => 'one');
  589. $this->assertFalse(Hash::numeric($data));
  590. $data = array('one');
  591. $this->assertFalse(Hash::numeric($data));
  592. $data = array('one' => 'two');
  593. $this->assertFalse(Hash::numeric($data));
  594. $data = array('one' => 1);
  595. $this->assertTrue(Hash::numeric($data));
  596. $data = array(0);
  597. $this->assertTrue(Hash::numeric($data));
  598. $data = array('one', 'two', 'three', 'four', 'five');
  599. $this->assertTrue(Hash::numeric(array_keys($data)));
  600. $data = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
  601. $this->assertTrue(Hash::numeric(array_keys($data)));
  602. $data = array('1' => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
  603. $this->assertTrue(Hash::numeric(array_keys($data)));
  604. $data = array('one', 2 => 'two', 3 => 'three', 4 => 'four', 'a' => 'five');
  605. $this->assertFalse(Hash::numeric(array_keys($data)));
  606. $data = array(2.4, 1, 0, -1, -2);
  607. $this->assertTrue(Hash::numeric($data));
  608. }
  609. /**
  610. * Test simple paths.
  611. *
  612. * @return void
  613. */
  614. public function testExtractBasic() {
  615. $data = self::articleData();
  616. $result = Hash::extract($data, '');
  617. $this->assertEquals($data, $result);
  618. $result = Hash::extract($data, '0.Article.title');
  619. $this->assertEquals(array('First Article'), $result);
  620. $result = Hash::extract($data, '1.Article.title');
  621. $this->assertEquals(array('Second Article'), $result);
  622. $result = Hash::extract(array(false), '{n}.Something.another_thing');
  623. $this->assertEquals(array(), $result);
  624. }
  625. /**
  626. * Test the {n} selector
  627. *
  628. * @return void
  629. */
  630. public function testExtractNumericKey() {
  631. $data = self::articleData();
  632. $result = Hash::extract($data, '{n}.Article.title');
  633. $expected = array(
  634. 'First Article', 'Second Article',
  635. 'Third Article', 'Fourth Article',
  636. 'Fifth Article'
  637. );
  638. $this->assertEquals($expected, $result);
  639. $result = Hash::extract($data, '0.Comment.{n}.user_id');
  640. $expected = array(
  641. '2', '4'
  642. );
  643. $this->assertEquals($expected, $result);
  644. }
  645. /**
  646. * Test the {n} selector with inconsistent arrays
  647. *
  648. * @return void
  649. */
  650. public function testExtractNumericMixedKeys() {
  651. $data = array(
  652. 'User' => array(
  653. 0 => array(
  654. 'id' => 4,
  655. 'name' => 'Neo'
  656. ),
  657. 1 => array(
  658. 'id' => 5,
  659. 'name' => 'Morpheus'
  660. ),
  661. 'stringKey' => array(
  662. 'name' => 'Fail'
  663. )
  664. )
  665. );
  666. $result = Hash::extract($data, 'User.{n}.name');
  667. $expected = array('Neo', 'Morpheus');
  668. $this->assertEquals($expected, $result);
  669. }
  670. /**
  671. * Test the {n} selector with non-zero based arrays
  672. *
  673. * @return void
  674. */
  675. public function testExtractNumericNonZero() {
  676. $data = array(
  677. 1 => array(
  678. 'User' => array(
  679. 'id' => 1,
  680. 'name' => 'John',
  681. )
  682. ),
  683. 2 => array(
  684. 'User' => array(
  685. 'id' => 2,
  686. 'name' => 'Bob',
  687. )
  688. ),
  689. 3 => array(
  690. 'User' => array(
  691. 'id' => 3,
  692. 'name' => 'Tony',
  693. )
  694. )
  695. );
  696. $result = Hash::extract($data, '{n}.User.name');
  697. $expected = array('John', 'Bob', 'Tony');
  698. $this->assertEquals($expected, $result);
  699. }
  700. /**
  701. * Test the {s} selector.
  702. *
  703. * @return void
  704. */
  705. public function testExtractStringKey() {
  706. $data = self::articleData();
  707. $result = Hash::extract($data, '{n}.{s}.user');
  708. $expected = array(
  709. 'mariano',
  710. 'mariano',
  711. 'mariano',
  712. 'mariano',
  713. 'mariano'
  714. );
  715. $this->assertEquals($expected, $result);
  716. $result = Hash::extract($data, '{n}.{s}.Nesting.test.1');
  717. $this->assertEquals(array('foo'), $result);
  718. }
  719. /**
  720. * Test the attribute presense selector.
  721. *
  722. * @return void
  723. */
  724. public function testExtractAttributePresence() {
  725. $data = self::articleData();
  726. $result = Hash::extract($data, '{n}.Article[published]');
  727. $expected = array($data[1]['Article']);
  728. $this->assertEquals($expected, $result);
  729. $result = Hash::extract($data, '{n}.Article[id][published]');
  730. $expected = array($data[1]['Article']);
  731. $this->assertEquals($expected, $result);
  732. }
  733. /**
  734. * Test = and != operators.
  735. *
  736. * @return void
  737. */
  738. public function testExtractAttributeEquality() {
  739. $data = self::articleData();
  740. $result = Hash::extract($data, '{n}.Article[id=3]');
  741. $expected = array($data[2]['Article']);
  742. $this->assertEquals($expected, $result);
  743. $result = Hash::extract($data, '{n}.Article[id = 3]');
  744. $expected = array($data[2]['Article']);
  745. $this->assertEquals($expected, $result, 'Whitespace should not matter.');
  746. $result = Hash::extract($data, '{n}.Article[id!=3]');
  747. $this->assertEquals(1, $result[0]['id']);
  748. $this->assertEquals(2, $result[1]['id']);
  749. $this->assertEquals(4, $result[2]['id']);
  750. $this->assertEquals(5, $result[3]['id']);
  751. }
  752. /**
  753. * Test extracting based on attributes with boolean values.
  754. *
  755. * @return void
  756. */
  757. public function testExtractAttributeBoolean() {
  758. $users = array(
  759. array(
  760. 'id' => 2,
  761. 'username' => 'johndoe',
  762. 'active' => true
  763. ),
  764. array(
  765. 'id' => 5,
  766. 'username' => 'kevin',
  767. 'active' => true
  768. ),
  769. array(
  770. 'id' => 9,
  771. 'username' => 'samantha',
  772. 'active' => false
  773. ),
  774. );
  775. $result = Hash::extract($users, '{n}[active=0]');
  776. $this->assertCount(1, $result);
  777. $this->assertEquals($users[2], $result[0]);
  778. $result = Hash::extract($users, '{n}[active=false]');
  779. $this->assertCount(1, $result);
  780. $this->assertEquals($users[2], $result[0]);
  781. $result = Hash::extract($users, '{n}[active=1]');
  782. $this->assertCount(2, $result);
  783. $this->assertEquals($users[0], $result[0]);
  784. $this->assertEquals($users[1], $result[1]);
  785. $result = Hash::extract($users, '{n}[active=true]');
  786. $this->assertCount(2, $result);
  787. $this->assertEquals($users[0], $result[0]);
  788. $this->assertEquals($users[1], $result[1]);
  789. }
  790. /**
  791. * Test that attribute matchers don't cause errors on scalar data.
  792. *
  793. * @return void
  794. */
  795. public function testExtractAttributeEqualityOnScalarValue() {
  796. $data = array(
  797. 'Entity' => array(
  798. 'id' => 1,
  799. 'data1' => 'value',
  800. )
  801. );
  802. $result = Hash::extract($data, 'Entity[id=1].data1');
  803. $this->assertEquals(array('value'), $result);
  804. $data = array('Entity' => false );
  805. $result = Hash::extract($data, 'Entity[id=1].data1');
  806. $this->assertEquals(array(), $result);
  807. }
  808. /**
  809. * Test comparison operators.
  810. *
  811. * @return void
  812. */
  813. public function testExtractAttributeComparison() {
  814. $data = self::articleData();
  815. $result = Hash::extract($data, '{n}.Comment.{n}[user_id > 2]');
  816. $expected = array($data[0]['Comment'][1]);
  817. $this->assertEquals($expected, $result);
  818. $this->assertEquals(4, $expected[0]['user_id']);
  819. $result = Hash::extract($data, '{n}.Comment.{n}[user_id >= 4]');
  820. $expected = array($data[0]['Comment'][1]);
  821. $this->assertEquals($expected, $result);
  822. $this->assertEquals(4, $expected[0]['user_id']);
  823. $result = Hash::extract($data, '{n}.Comment.{n}[user_id < 3]');
  824. $expected = array($data[0]['Comment'][0]);
  825. $this->assertEquals($expected, $result);
  826. $this->assertEquals(2, $expected[0]['user_id']);
  827. $result = Hash::extract($data, '{n}.Comment.{n}[user_id <= 2]');
  828. $expected = array($data[0]['Comment'][0]);
  829. $this->assertEquals($expected, $result);
  830. $this->assertEquals(2, $expected[0]['user_id']);
  831. }
  832. /**
  833. * Test multiple attributes with conditions.
  834. *
  835. * @return void
  836. */
  837. public function testExtractAttributeMultiple() {
  838. $data = self::articleData();
  839. $result = Hash::extract($data, '{n}.Comment.{n}[user_id > 2][id=1]');
  840. $this->assertEmpty($result);
  841. $result = Hash::extract($data, '{n}.Comment.{n}[user_id > 2][id=2]');
  842. $expected = array($data[0]['Comment'][1]);
  843. $this->assertEquals($expected, $result);
  844. $this->assertEquals(4, $expected[0]['user_id']);
  845. }
  846. /**
  847. * Test attribute pattern matching.
  848. *
  849. * @return void
  850. */
  851. public function testExtractAttributePattern() {
  852. $data = self::articleData();
  853. $result = Hash::extract($data, '{n}.Article[title=/^First/]');
  854. $expected = array($data[0]['Article']);
  855. $this->assertEquals($expected, $result);
  856. $result = Hash::extract($data, '{n}.Article[title=/^Fir[a-z]+/]');
  857. $expected = array($data[0]['Article']);
  858. $this->assertEquals($expected, $result);
  859. }
  860. /**
  861. * Test that extract() + matching can hit null things.
  862. *
  863. * @return void
  864. */
  865. public function testExtractMatchesNull() {
  866. $data = array(
  867. 'Country' => array(
  868. array('name' => 'Canada'),
  869. array('name' => 'Australia'),
  870. array('name' => null),
  871. )
  872. );
  873. $result = Hash::extract($data, 'Country.{n}[name=/Canada|^$/]');
  874. $expected = array(
  875. array(
  876. 'name' => 'Canada',
  877. ),
  878. array(
  879. 'name' => null,
  880. ),
  881. );
  882. $this->assertEquals($expected, $result);
  883. }
  884. /**
  885. * Test that uneven keys are handled correctly.
  886. *
  887. * @return void
  888. */
  889. public function testExtractUnevenKeys() {
  890. $data = array(
  891. 'Level1' => array(
  892. 'Level2' => array('test1', 'test2'),
  893. 'Level2bis' => array('test3', 'test4')
  894. )
  895. );
  896. $this->assertEquals(
  897. array('test1', 'test2'),
  898. Hash::extract($data, 'Level1.Level2')
  899. );
  900. $this->assertEquals(
  901. array('test3', 'test4'),
  902. Hash::extract($data, 'Level1.Level2bis')
  903. );
  904. $data = array(
  905. 'Level1' => array(
  906. 'Level2bis' => array(
  907. array('test3', 'test4'),
  908. array('test5', 'test6')
  909. )
  910. )
  911. );
  912. $expected = array(
  913. array('test3', 'test4'),
  914. array('test5', 'test6')
  915. );
  916. $this->assertEquals($expected, Hash::extract($data, 'Level1.Level2bis'));
  917. $data['Level1']['Level2'] = array('test1', 'test2');
  918. $this->assertEquals($expected, Hash::extract($data, 'Level1.Level2bis'));
  919. }
  920. /**
  921. * testSort method
  922. *
  923. * @return void
  924. */
  925. public function testSort() {
  926. $result = Hash::sort(array(), '{n}.name');
  927. $this->assertEquals(array(), $result);
  928. $a = array(
  929. 0 => array(
  930. 'Person' => array('name' => 'Jeff'),
  931. 'Friend' => array(array('name' => 'Nate'))
  932. ),
  933. 1 => array(
  934. 'Person' => array('name' => 'Tracy'),
  935. 'Friend' => array(array('name' => 'Lindsay'))
  936. )
  937. );
  938. $b = array(
  939. 0 => array(
  940. 'Person' => array('name' => 'Tracy'),
  941. 'Friend' => array(array('name' => 'Lindsay'))
  942. ),
  943. 1 => array(
  944. 'Person' => array('name' => 'Jeff'),
  945. 'Friend' => array(array('name' => 'Nate'))
  946. )
  947. );
  948. $a = Hash::sort($a, '{n}.Friend.{n}.name');
  949. $this->assertEquals($a, $b);
  950. $b = array(
  951. 0 => array(
  952. 'Person' => array('name' => 'Jeff'),
  953. 'Friend' => array(array('name' => 'Nate'))
  954. ),
  955. 1 => array(
  956. 'Person' => array('name' => 'Tracy'),
  957. 'Friend' => array(array('name' => 'Lindsay'))
  958. )
  959. );
  960. $a = array(
  961. 0 => array(
  962. 'Person' => array('name' => 'Tracy'),
  963. 'Friend' => array(array('name' => 'Lindsay'))
  964. ),
  965. 1 => array(
  966. 'Person' => array('name' => 'Jeff'),
  967. 'Friend' => array(array('name' => 'Nate'))
  968. )
  969. );
  970. $a = Hash::sort($a, '{n}.Friend.{n}.name', 'desc');
  971. $this->assertEquals($a, $b);
  972. $a = array(
  973. 0 => array(
  974. 'Person' => array('name' => 'Jeff'),
  975. 'Friend' => array(array('name' => 'Nate'))
  976. ),
  977. 1 => array(
  978. 'Person' => array('name' => 'Tracy'),
  979. 'Friend' => array(array('name' => 'Lindsay'))
  980. ),
  981. 2 => array(
  982. 'Person' => array('name' => 'Adam'),
  983. 'Friend' => array(array('name' => 'Bob'))
  984. )
  985. );
  986. $b = array(
  987. 0 => array(
  988. 'Person' => array('name' => 'Adam'),
  989. 'Friend' => array(array('name' => 'Bob'))
  990. ),
  991. 1 => array(
  992. 'Person' => array('name' => 'Jeff'),
  993. 'Friend' => array(array('name' => 'Nate'))
  994. ),
  995. 2 => array(
  996. 'Person' => array('name' => 'Tracy'),
  997. 'Friend' => array(array('name' => 'Lindsay'))
  998. )
  999. );
  1000. $a = Hash::sort($a, '{n}.Person.name', 'asc');
  1001. $this->assertEquals($a, $b);
  1002. $a = array(
  1003. 0 => array('Person' => array('name' => 'Jeff')),
  1004. 1 => array('Shirt' => array('color' => 'black'))
  1005. );
  1006. $b = array(
  1007. 0 => array('Shirt' => array('color' => 'black')),
  1008. 1 => array('Person' => array('name' => 'Jeff')),
  1009. );
  1010. $a = Hash::sort($a, '{n}.Person.name', 'ASC', 'STRING');
  1011. $this->assertSame($a, $b);
  1012. $names = array(
  1013. array('employees' => array(
  1014. array('name' => array('first' => 'John', 'last' => 'Doe')))
  1015. ),
  1016. array('employees' => array(
  1017. array('name' => array('first' => 'Jane', 'last' => 'Doe')))
  1018. ),
  1019. array('employees' => array(array('name' => array()))),
  1020. array('employees' => array(array('name' => array())))
  1021. );
  1022. $result = Hash::sort($names, '{n}.employees.0.name', 'asc');
  1023. $expected = array(
  1024. array('employees' => array(
  1025. array('name' => array('first' => 'John', 'last' => 'Doe')))
  1026. ),
  1027. array('employees' => array(
  1028. array('name' => array('first' => 'Jane', 'last' => 'Doe')))
  1029. ),
  1030. array('employees' => array(array('name' => array()))),
  1031. array('employees' => array(array('name' => array())))
  1032. );
  1033. $this->assertSame($expected, $result);
  1034. $a = array(
  1035. 'SU' => array(
  1036. 'total_fulfillable' => 2
  1037. ),
  1038. 'AA' => array(
  1039. 'total_fulfillable' => 1
  1040. ),
  1041. 'LX' => array(
  1042. 'total_fulfillable' => 0
  1043. ),
  1044. 'BL' => array(
  1045. 'total_fulfillable' => 3
  1046. ),
  1047. );
  1048. $expected = array(
  1049. 'LX' => array(
  1050. 'total_fulfillable' => 0
  1051. ),
  1052. 'AA' => array(
  1053. 'total_fulfillable' => 1
  1054. ),
  1055. 'SU' => array(
  1056. 'total_fulfillable' => 2
  1057. ),
  1058. 'BL' => array(
  1059. 'total_fulfillable' => 3
  1060. ),
  1061. );
  1062. $result = Hash::sort($a, '{s}.total_fulfillable', 'asc');
  1063. $this->assertSame($expected, $result);
  1064. }
  1065. /**
  1066. * Test sort() with numeric option.
  1067. *
  1068. * @return void
  1069. */
  1070. public function testSortNumeric() {
  1071. $items = array(
  1072. array('Item' => array('price' => '155,000')),
  1073. array('Item' => array('price' => '139,000')),
  1074. array('Item' => array('price' => '275,622')),
  1075. array('Item' => array('price' => '230,888')),
  1076. array('Item' => array('price' => '66,000')),
  1077. );
  1078. $result = Hash::sort($items, '{n}.Item.price', 'asc', 'numeric');
  1079. $expected = array(
  1080. array('Item' => array('price' => '66,000')),
  1081. array('Item' => array('price' => '139,000')),
  1082. array('Item' => array('price' => '155,000')),
  1083. array('Item' => array('price' => '230,888')),
  1084. array('Item' => array('price' => '275,622')),
  1085. );
  1086. $this->assertEquals($expected, $result);
  1087. $result = Hash::sort($items, '{n}.Item.price', 'desc', 'numeric');
  1088. $expected = array(
  1089. array('Item' => array('price' => '275,622')),
  1090. array('Item' => array('price' => '230,888')),
  1091. array('Item' => array('price' => '155,000')),
  1092. array('Item' => array('price' => '139,000')),
  1093. array('Item' => array('price' => '66,000')),
  1094. );
  1095. $this->assertEquals($expected, $result);
  1096. }
  1097. /**
  1098. * Test natural sorting.
  1099. *
  1100. * @return void
  1101. */
  1102. public function testSortNatural() {
  1103. if (version_compare(PHP_VERSION, '5.4.0', '<')) {
  1104. $this->markTestSkipped('SORT_NATURAL is available since PHP 5.4.');
  1105. }
  1106. $items = array(
  1107. array('Item' => array('image' => 'img1.jpg')),
  1108. array('Item' => array('image' => 'img99.jpg')),
  1109. array('Item' => array('image' => 'img12.jpg')),
  1110. array('Item' => array('image' => 'img10.jpg')),
  1111. array('Item' => array('image' => 'img2.jpg')),
  1112. );
  1113. $result = Hash::sort($items, '{n}.Item.image', 'desc', 'natural');
  1114. $expected = array(
  1115. array('Item' => array('image' => 'img99.jpg')),
  1116. array('Item' => array('image' => 'img12.jpg')),
  1117. array('Item' => array('image' => 'img10.jpg')),
  1118. array('Item' => array('image' => 'img2.jpg')),
  1119. array('Item' => array('image' => 'img1.jpg')),
  1120. );
  1121. $this->assertEquals($expected, $result);
  1122. $result = Hash::sort($items, '{n}.Item.image', 'asc', 'natural');
  1123. $expected = array(
  1124. array('Item' => array('image' => 'img1.jpg')),
  1125. array('Item' => array('image' => 'img2.jpg')),
  1126. array('Item' => array('image' => 'img10.jpg')),
  1127. array('Item' => array('image' => 'img12.jpg')),
  1128. array('Item' => array('image' => 'img99.jpg')),
  1129. );
  1130. $this->assertEquals($expected, $result);
  1131. }
  1132. /**
  1133. * Test that sort() with 'natural' type will fallback to 'regular' as SORT_NATURAL is introduced in PHP 5.4
  1134. *
  1135. * @return void
  1136. */
  1137. public function testSortNaturalFallbackToRegular() {
  1138. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  1139. $this->markTestSkipped('Skipping SORT_NATURAL fallback test on PHP >= 5.4');
  1140. }
  1141. $a = array(
  1142. 0 => array('Person' => array('name' => 'Jeff')),
  1143. 1 => array('Shirt' => array('color' => 'black'))
  1144. );
  1145. $b = array(
  1146. 0 => array('Shirt' => array('color' => 'black')),
  1147. 1 => array('Person' => array('name' => 'Jeff')),
  1148. );
  1149. $sorted = Hash::sort($a, '{n}.Person.name', 'asc', 'natural');
  1150. $this->assertEquals($sorted, $b);
  1151. }
  1152. /**
  1153. * test sorting with out of order keys.
  1154. *
  1155. * @return void
  1156. */
  1157. public function testSortWithOutOfOrderKeys() {
  1158. $data = array(
  1159. 9 => array('class' => 510, 'test2' => 2),
  1160. 1 => array('class' => 500, 'test2' => 1),
  1161. 2 => array('class' => 600, 'test2' => 2),
  1162. 5 => array('class' => 625, 'test2' => 4),
  1163. 0 => array('class' => 605, 'test2' => 3),
  1164. );
  1165. $expected = array(
  1166. array('class' => 500, 'test2' => 1),
  1167. array('class' => 510, 'test2' => 2),
  1168. array('class' => 600, 'test2' => 2),
  1169. array('class' => 605, 'test2' => 3),
  1170. array('class' => 625, 'test2' => 4),
  1171. );
  1172. $result = Hash::sort($data, '{n}.class', 'asc');
  1173. $this->assertEquals($expected, $result);
  1174. $result = Hash::sort($data, '{n}.test2', 'asc');
  1175. $this->assertEquals($expected, $result);
  1176. }
  1177. /**
  1178. * test sorting with string keys.
  1179. *
  1180. * @return void
  1181. */
  1182. public function testSortString() {
  1183. $toSort = array(
  1184. 'four' => array('number' => 4, 'some' => 'foursome'),
  1185. 'six' => array('number' => 6, 'some' => 'sixsome'),
  1186. 'five' => array('number' => 5, 'some' => 'fivesome'),
  1187. 'two' => array('number' => 2, 'some' => 'twosome'),
  1188. 'three' => array('number' => 3, 'some' => 'threesome')
  1189. );
  1190. $sorted = Hash::sort($toSort, '{s}.number', 'asc');
  1191. $expected = array(
  1192. 'two' => array('number' => 2, 'some' => 'twosome'),
  1193. 'three' => array('number' => 3, 'some' => 'threesome'),
  1194. 'four' => array('number' => 4, 'some' => 'foursome'),
  1195. 'five' => array('number' => 5, 'some' => 'fivesome'),
  1196. 'six' => array('number' => 6, 'some' => 'sixsome')
  1197. );
  1198. $this->assertEquals($expected, $sorted);
  1199. $menus = array(
  1200. 'blogs' => array('title' => 'Blogs', 'weight' => 3),
  1201. 'comments' => array('title' => 'Comments', 'weight' => 2),
  1202. 'users' => array('title' => 'Users', 'weight' => 1),
  1203. );
  1204. $expected = array(
  1205. 'users' => array('title' => 'Users', 'weight' => 1),
  1206. 'comments' => array('title' => 'Comments', 'weight' => 2),
  1207. 'blogs' => array('title' => 'Blogs', 'weight' => 3),
  1208. );
  1209. $result = Hash::sort($menus, '{s}.weight', 'ASC');
  1210. $this->assertEquals($expected, $result);
  1211. }
  1212. /**
  1213. * Test insert()
  1214. *
  1215. * @return void
  1216. */
  1217. public function testInsertSimple() {
  1218. $a = array(
  1219. 'pages' => array('name' => 'page')
  1220. );
  1221. $result = Hash::insert($a, 'files', array('name' => 'files'));
  1222. $expected = array(
  1223. 'pages' => array('name' => 'page'),
  1224. 'files' => array('name' => 'files')
  1225. );
  1226. $this->assertEquals($expected, $result);
  1227. $a = array(
  1228. 'pages' => array('name' => 'page')
  1229. );
  1230. $result = Hash::insert($a, 'pages.name', array());
  1231. $expected = array(
  1232. 'pages' => array('name' => array()),
  1233. );
  1234. $this->assertEquals($expected, $result);
  1235. $a = array(
  1236. 'foo' => array('bar' => 'baz')
  1237. );
  1238. $result = Hash::insert($a, 'some.0123.path', array('foo' => array('bar' => 'baz')));
  1239. $expected = array('foo' => array('bar' => 'baz'));
  1240. $this->assertEquals($expected, Hash::get($result, 'some.0123.path'));
  1241. }
  1242. /**
  1243. * Test inserting with multiple values.
  1244. *
  1245. * @return void
  1246. */
  1247. public function testInsertMulti() {
  1248. $data = self::articleData();
  1249. $result = Hash::insert($data, '{n}.Article.insert', 'value');
  1250. $this->assertEquals('value', $result[0]['Article']['insert']);
  1251. $this->assertEquals('value', $result[1]['Article']['insert']);
  1252. $result = Hash::insert($data, '{n}.Comment.{n}.insert', 'value');
  1253. $this->assertEquals('value', $result[0]['Comment'][0]['insert']);
  1254. $this->assertEquals('value', $result[0]['Comment'][1]['insert']);
  1255. $data = array(
  1256. 0 => array('Item' => array('id' => 1, 'title' => 'first')),
  1257. 1 => array('Item' => array('id' => 2, 'title' => 'second')),
  1258. 2 => array('Item' => array('id' => 3, 'title' => 'third')),
  1259. 3 => array('Item' => array('id' => 4, 'title' => 'fourth')),
  1260. 4 => array('Item' => array('id' => 5, 'title' => 'fifth')),
  1261. );
  1262. $result = Hash::insert($data, '{n}.Item[id=/\b2|\b4/]', array('test' => 2));
  1263. $expected = array(
  1264. 0 => array('Item' => array('id' => 1, 'title' => 'first')),
  1265. 1 => array('Item' => array('id' => 2, 'title' => 'second', 'test' => 2)),
  1266. 2 => array('Item' => array('id' => 3, 'title' => 'third')),
  1267. 3 => array('Item' => array('id' => 4, 'title' => 'fourth', 'test' => 2)),
  1268. 4 => array('Item' => array('id' => 5, 'title' => 'fifth')),
  1269. );
  1270. $this->assertEquals($expected, $result);
  1271. }
  1272. /**
  1273. * Test that insert() can insert data over a string value.
  1274. *
  1275. * @return void
  1276. */
  1277. public function testInsertOverwriteStringValue() {
  1278. $data = array(
  1279. 'Some' => array(
  1280. 'string' => 'value'
  1281. )
  1282. );
  1283. $result = Hash::insert($data, 'Some.string.value', array('values'));
  1284. $expected = array(
  1285. 'Some' => array(
  1286. 'string' => array(
  1287. 'value' => array('values')
  1288. )
  1289. )
  1290. );
  1291. $this->assertEquals($expected, $result);
  1292. }
  1293. /**
  1294. * Test remove() method.
  1295. *
  1296. * @return void
  1297. */
  1298. public function testRemove() {
  1299. $a = array(
  1300. 'pages' => array('name' => 'page'),
  1301. 'files' => array('name' => 'files')
  1302. );
  1303. $result = Hash::remove($a, 'files');
  1304. $expected = array(
  1305. 'pages' => array('name' => 'page')
  1306. );
  1307. $this->assertEquals($expected, $result);
  1308. $a = array(
  1309. 'pages' => array(
  1310. 0 => array('name' => 'main'),
  1311. 1 => array(
  1312. 'name' => 'about',
  1313. 'vars' => array('title' => 'page title')
  1314. )
  1315. )
  1316. );
  1317. $result = Hash::remove($a, 'pages.1.vars');
  1318. $expected = array(
  1319. 'pages' => array(
  1320. 0 => array('name' => 'main'),
  1321. 1 => array('name' => 'about')
  1322. )
  1323. );
  1324. $this->assertEquals($expected, $result);
  1325. $result = Hash::remove($a, 'pages.2.vars');
  1326. $expected = $a;
  1327. $this->assertEquals($expected, $result);
  1328. $a = array(
  1329. 0 => array(
  1330. 'name' => 'pages'
  1331. ),
  1332. 1 => array(
  1333. 'name' => 'files'
  1334. )
  1335. );
  1336. $result = Hash::remove($a, '{n}[name=files]');
  1337. $expected = array(
  1338. 0 => array(
  1339. 'name' => 'pages'
  1340. )
  1341. );
  1342. $this->assertEquals($expected, $result);
  1343. $array = array(
  1344. 0 => 'foo',
  1345. 1 => array(
  1346. 0 => 'baz'
  1347. )
  1348. );
  1349. $expected = $array;
  1350. $result = Hash::remove($array, '{n}.part');
  1351. $this->assertEquals($expected, $result);
  1352. $result = Hash::remove($array, '{n}.{n}.part');
  1353. $this->assertEquals($expected, $result);
  1354. }
  1355. /**
  1356. * Test removing multiple values.
  1357. *
  1358. * @return void
  1359. */
  1360. public function testRemoveMulti() {
  1361. $data = self::articleData();
  1362. $result = Hash::remove($data, '{n}.Article.title');
  1363. $this->assertFalse(isset($result[0]['Article']['title']));
  1364. $this->assertFalse(isset($result[1]['Article']['title']));
  1365. $result = Hash::remove($data, '{n}.Article.{s}');
  1366. $this->assertFalse(isset($result[0]['Article']['id']));
  1367. $this->assertFalse(isset($result[0]['Article']['user_id']));
  1368. $this->assertFalse(isset($result[0]['Article']['title']));
  1369. $this->assertFalse(isset($result[0]['Article']['body']));
  1370. $data = array(
  1371. 0 => array('Item' => array('id' => 1, 'title' => 'first')),
  1372. 1 => array('Item' => array('id' => 2, 'title' => 'second')),
  1373. 2 => array('Item' => array('id' => 3, 'title' => 'third')),
  1374. 3 => array('Item' => array('id' => 4, 'title' => 'fourth')),
  1375. 4 => array('Item' => array('id' => 5, 'title' => 'fifth')),
  1376. );
  1377. $result = Hash::remove($data, '{n}.Item[id=/\b2|\b4/]');
  1378. $expected = array(
  1379. 0 => array('Item' => array('id' => 1, 'title' => 'first')),
  1380. 2 => array('Item' => array('id' => 3, 'title' => 'third')),
  1381. 4 => array('Item' => array('id' => 5, 'title' => 'fifth')),
  1382. );
  1383. $this->assertEquals($expected, $result);
  1384. }
  1385. /**
  1386. * testCheck method
  1387. *
  1388. * @return void
  1389. */
  1390. public function testCheck() {
  1391. $set = array(
  1392. 'My Index 1' => array('First' => 'The first item')
  1393. );
  1394. $this->assertTrue(Hash::check($set, 'My Index 1.First'));
  1395. $this->assertTrue(Hash::check($set, 'My Index 1'));
  1396. $set = array(
  1397. 'My Index 1' => array(
  1398. 'First' => array(
  1399. 'Second' => array(
  1400. 'Third' => array(
  1401. 'Fourth' => 'Heavy. Nesting.'
  1402. )
  1403. )
  1404. )
  1405. )
  1406. );
  1407. $this->assertTrue(Hash::check($set, 'My Index 1.First.Second'));
  1408. $this->assertTrue(Hash::check($set, 'My Index 1.First.Second.Third'));
  1409. $this->assertTrue(Hash::check($set, 'My Index 1.First.Second.Third.Fourth'));
  1410. $this->assertFalse(Hash::check($set, 'My Index 1.First.Seconds.Third.Fourth'));
  1411. }
  1412. /**
  1413. * testCombine method
  1414. *
  1415. * @return void
  1416. */
  1417. public function testCombine() {
  1418. $result = Hash::combine(array(), '{n}.User.id', '{n}.User.Data');
  1419. $this->assertTrue(empty($result));
  1420. $a = self::userData();
  1421. $result = Hash::combine($a, '{n}.User.id');
  1422. $expected = array(2 => null, 14 => null, 25 => null);
  1423. $this->assertEquals($expected, $result);
  1424. $result = Hash::combine($a, '{n}.User.id', '{n}.User.non-existant');
  1425. $expected = array(2 => null, 14 => null, 25 => null);
  1426. $this->assertEquals($expected, $result);
  1427. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data');
  1428. $expected = array(
  1429. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1430. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'),
  1431. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
  1432. $this->assertEquals($expected, $result);
  1433. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name');
  1434. $expected = array(
  1435. 2 => 'Mariano Iglesias',
  1436. 14 => 'Larry E. Masters',
  1437. 25 => 'The Gwoo');
  1438. $this->assertEquals($expected, $result);
  1439. }
  1440. /**
  1441. * test combine() giving errors on key/value length mismatches.
  1442. *
  1443. * @expectedException CakeException
  1444. * @return void
  1445. */
  1446. public function testCombineErrorMissingValue() {
  1447. $data = array(
  1448. array('User' => array('id' => 1, 'name' => 'mark')),
  1449. array('User' => array('name' => 'jose')),
  1450. );
  1451. Hash::combine($data, '{n}.User.id', '{n}.User.name');
  1452. }
  1453. /**
  1454. * test combine() giving errors on key/value length mismatches.
  1455. *
  1456. * @expectedException CakeException
  1457. * @return void
  1458. */
  1459. public function testCombineErrorMissingKey() {
  1460. $data = array(
  1461. array('User' => array('id' => 1, 'name' => 'mark')),
  1462. array('User' => array('id' => 2)),
  1463. );
  1464. Hash::combine($data, '{n}.User.id', '{n}.User.name');
  1465. }
  1466. /**
  1467. * test combine() with a group path.
  1468. *
  1469. * @return void
  1470. */
  1471. public function testCombineWithGroupPath() {
  1472. $a = self::userData();
  1473. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  1474. $expected = array(
  1475. 1 => array(
  1476. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1477. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')
  1478. ),
  1479. 2 => array(
  1480. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')
  1481. )
  1482. );
  1483. $this->assertEquals($expected, $result);
  1484. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  1485. $expected = array(
  1486. 1 => array(
  1487. 2 => 'Mariano Iglesias',
  1488. 25 => 'The Gwoo'
  1489. ),
  1490. 2 => array(
  1491. 14 => 'Larry E. Masters'
  1492. )
  1493. );
  1494. $this->assertEquals($expected, $result);
  1495. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  1496. $expected = array(
  1497. 1 => array(
  1498. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1499. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')
  1500. ),
  1501. 2 => array(
  1502. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')
  1503. )
  1504. );
  1505. $this->assertEquals($expected, $result);
  1506. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  1507. $expected = array(
  1508. 1 => array(
  1509. 2 => 'Mariano Iglesias',
  1510. 25 => 'The Gwoo'
  1511. ),
  1512. 2 => array(
  1513. 14 => 'Larry E. Masters'
  1514. )
  1515. );
  1516. $this->assertEquals($expected, $result);
  1517. }
  1518. /**
  1519. * Test combine with formatting rules.
  1520. *
  1521. * @return void
  1522. */
  1523. public function testCombineWithFormatting() {
  1524. $a = self::userData();
  1525. $result = Hash::combine(
  1526. $a,
  1527. '{n}.User.id',
  1528. array('%1$s: %2$s', '{n}.User.Data.user', '{n}.User.Data.name'),
  1529. '{n}.User.group_id'
  1530. );
  1531. $expected = array(
  1532. 1 => array(
  1533. 2 => 'mariano.iglesias: Mariano Iglesias',
  1534. 25 => 'gwoo: The Gwoo'
  1535. ),
  1536. 2 => array(
  1537. 14 => 'phpnut: Larry E. Masters'
  1538. )
  1539. );
  1540. $this->assertEquals($expected, $result);
  1541. $result = Hash::combine(
  1542. $a,
  1543. array(
  1544. '%s: %s',
  1545. '{n}.User.Data.user',
  1546. '{n}.User.Data.name'
  1547. ),
  1548. '{n}.User.id'
  1549. );
  1550. $expected = array(
  1551. 'mariano.iglesias: Mariano Iglesias' => 2,
  1552. 'phpnut: Larry E. Masters' => 14,
  1553. 'gwoo: The Gwoo' => 25
  1554. );
  1555. $this->assertEquals($expected, $result);
  1556. $result = Hash::combine(
  1557. $a,
  1558. array('%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'),
  1559. '{n}.User.Data.name'
  1560. );
  1561. $expected = array(
  1562. 'mariano.iglesias: 2' => 'Mariano Iglesias',
  1563. 'phpnut: 14' => 'Larry E. Masters',
  1564. 'gwoo: 25' => 'The Gwoo'
  1565. );
  1566. $this->assertEquals($expected, $result);
  1567. $result = Hash::combine(
  1568. $a,
  1569. array('%2$d: %1$s', '{n}.User.Data.user', '{n}.User.id'),
  1570. '{n}.User.Data.name'
  1571. );
  1572. $expected = array(
  1573. '2: mariano.iglesias' => 'Mariano Iglesias',
  1574. '14: phpnut' => 'Larry E. Masters',
  1575. '25: gwoo' => 'The Gwoo'
  1576. );
  1577. $this->assertEquals($expected, $result);
  1578. }
  1579. /**
  1580. * testFormat method
  1581. *
  1582. * @return void
  1583. */
  1584. public function testFormat() {
  1585. $data = self::userData();
  1586. $result = Hash::format(
  1587. $data,
  1588. array('{n}.User.Data.user', '{n}.User.id'),
  1589. '%s, %s'
  1590. );
  1591. $expected = array(
  1592. 'mariano.iglesias, 2',
  1593. 'phpnut, 14',
  1594. 'gwoo, 25'
  1595. );
  1596. $this->assertEquals($expected, $result);
  1597. $result = Hash::format(
  1598. $data,
  1599. array('{n}.User.Data.user', '{n}.User.id'),
  1600. '%2$s, %1$s'
  1601. );
  1602. $expected = array(
  1603. '2, mariano.iglesias',
  1604. '14, phpnut',
  1605. '25, gwoo'
  1606. );
  1607. $this->assertEquals($expected, $result);
  1608. }
  1609. /**
  1610. * testFormattingNullValues method
  1611. *
  1612. * @return void
  1613. */
  1614. public function testFormatNullValues() {
  1615. $data = array(
  1616. array('Person' => array(
  1617. 'first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42'
  1618. )),
  1619. array('Person' => array(
  1620. 'first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => null
  1621. )),
  1622. array('Person' => array(
  1623. 'first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => null
  1624. ))
  1625. );
  1626. $result = Hash::format($data, array('{n}.Person.something'), '%s');
  1627. $expected = array('42', '', '');
  1628. $this->assertEquals($expected, $result);
  1629. $result = Hash::format($data, array('{n}.Person.city', '{n}.Person.something'), '%s, %s');
  1630. $expected = array('Boston, 42', 'Boondock, ', 'Venice Beach, ');
  1631. $this->assertEquals($expected, $result);
  1632. }
  1633. /**
  1634. * Test map()
  1635. *
  1636. * @return void
  1637. */
  1638. public function testMap() {
  1639. $data = self::articleData();
  1640. $result = Hash::map($data, '{n}.Article.id', array($this, 'mapCallback'));
  1641. $expected = array(2, 4, 6, 8, 10);
  1642. $this->assertEquals($expected, $result);
  1643. }
  1644. /**
  1645. * testApply
  1646. *
  1647. * @return void
  1648. */
  1649. public function testApply() {
  1650. $data = self::articleData();
  1651. $result = Hash::apply($data, '{n}.Article.id', 'array_sum');
  1652. $this->assertEquals(15, $result);
  1653. }
  1654. /**
  1655. * Test reduce()
  1656. *
  1657. * @return void
  1658. */
  1659. public function testReduce() {
  1660. $data = self::articleData();
  1661. $result = Hash::reduce($data, '{n}.Article.id', array($this, 'reduceCallback'));
  1662. $this->assertEquals(15, $result);
  1663. }
  1664. /**
  1665. * testing method for map callbacks.
  1666. *
  1667. * @param mixed $value Value
  1668. * @return mixed
  1669. */
  1670. public function mapCallback($value) {
  1671. return $value * 2;
  1672. }
  1673. /**
  1674. * testing method for reduce callbacks.
  1675. *
  1676. * @param mixed $one First param
  1677. * @param mixed $two Second param
  1678. * @return mixed
  1679. */
  1680. public function reduceCallback($one, $two) {
  1681. return $one + $two;
  1682. }
  1683. /**
  1684. * test Hash nest with a normal model result set. For kicks rely on Hash nest detecting the key names
  1685. * automatically
  1686. *
  1687. * @return void
  1688. */
  1689. public function testNestModel() {
  1690. $input = array(
  1691. array(
  1692. 'ModelName' => array(
  1693. 'id' => 1,
  1694. 'parent_id' => null
  1695. ),
  1696. ),
  1697. array(
  1698. 'ModelName' => array(
  1699. 'id' => 2,
  1700. 'parent_id' => 1
  1701. ),
  1702. ),
  1703. array(
  1704. 'ModelName' => array(
  1705. 'id' => 3,
  1706. 'parent_id' => 1
  1707. ),
  1708. ),
  1709. array(
  1710. 'ModelName' => array(
  1711. 'id' => 4,
  1712. 'parent_id' => 1
  1713. ),
  1714. ),
  1715. array(
  1716. 'ModelName' => array(
  1717. 'id' => 5,
  1718. 'parent_id' => 1
  1719. ),
  1720. ),
  1721. array(
  1722. 'ModelName' => array(
  1723. 'id' => 6,
  1724. 'parent_id' => null
  1725. ),
  1726. ),
  1727. array(
  1728. 'ModelName' => array(
  1729. 'id' => 7,
  1730. 'parent_id' => 6
  1731. ),
  1732. ),
  1733. array(
  1734. 'ModelName' => array(
  1735. 'id' => 8,
  1736. 'parent_id' => 6
  1737. ),
  1738. ),
  1739. array(
  1740. 'ModelName' => array(
  1741. 'id' => 9,
  1742. 'parent_id' => 6
  1743. ),
  1744. ),
  1745. array(
  1746. 'ModelName' => array(
  1747. 'id' => 10,
  1748. 'parent_id' => 6
  1749. )
  1750. )
  1751. );
  1752. $expected = array(
  1753. array(
  1754. 'ModelName' => array(
  1755. 'id' => 1,
  1756. 'parent_id' => null
  1757. ),
  1758. 'children' => array(
  1759. array(
  1760. 'ModelName' => array(
  1761. 'id' => 2,
  1762. 'parent_id' => 1
  1763. ),
  1764. 'children' => array()
  1765. ),
  1766. array(
  1767. 'ModelName' => array(
  1768. 'id' => 3,
  1769. 'parent_id' => 1
  1770. ),
  1771. 'children' => array()
  1772. ),
  1773. array(
  1774. 'ModelName' => array(
  1775. 'id' => 4,
  1776. 'parent_id' => 1
  1777. ),
  1778. 'children' => array()
  1779. ),
  1780. array(
  1781. 'ModelName' => array(
  1782. 'id' => 5,
  1783. 'parent_id' => 1
  1784. ),
  1785. 'children' => array()
  1786. ),
  1787. )
  1788. ),
  1789. array(
  1790. 'ModelName' => array(
  1791. 'id' => 6,
  1792. 'parent_id' => null
  1793. ),
  1794. 'children' => array(
  1795. array(
  1796. 'ModelName' => array(
  1797. 'id' => 7,
  1798. 'parent_id' => 6
  1799. ),
  1800. 'children' => array()
  1801. ),
  1802. array(
  1803. 'ModelName' => array(
  1804. 'id' => 8,
  1805. 'parent_id' => 6
  1806. ),
  1807. 'children' => array()
  1808. ),
  1809. array(
  1810. 'ModelName' => array(
  1811. 'id' => 9,
  1812. 'parent_id' => 6
  1813. ),
  1814. 'children' => array()
  1815. ),
  1816. array(
  1817. 'ModelName' => array(
  1818. 'id' => 10,
  1819. 'parent_id' => 6
  1820. ),
  1821. 'children' => array()
  1822. )
  1823. )
  1824. )
  1825. );
  1826. $result = Hash::nest($input);
  1827. $this->assertEquals($expected, $result);
  1828. }
  1829. /**
  1830. * test Hash nest with a normal model result set, and a nominated root id
  1831. *
  1832. * @return void
  1833. */
  1834. public function testNestModelExplicitRoot() {
  1835. $input = array(
  1836. array(
  1837. 'ModelName' => array(
  1838. 'id' => 1,
  1839. 'parent_id' => null
  1840. ),
  1841. ),
  1842. array(
  1843. 'ModelName' => array(
  1844. 'id' => 2,
  1845. 'parent_id' => 1
  1846. ),
  1847. ),
  1848. array(
  1849. 'ModelName' => array(
  1850. 'id' => 3,
  1851. 'parent_id' => 1
  1852. ),
  1853. ),
  1854. array(
  1855. 'ModelName' => array(
  1856. 'id' => 4,
  1857. 'parent_id' => 1
  1858. ),
  1859. ),
  1860. array(
  1861. 'ModelName' => array(
  1862. 'id' => 5,
  1863. 'parent_id' => 1
  1864. ),
  1865. ),
  1866. array(
  1867. 'ModelName' => array(
  1868. 'id' => 6,
  1869. 'parent_id' => null
  1870. ),
  1871. ),
  1872. array(
  1873. 'ModelName' => array(
  1874. 'id' => 7,
  1875. 'parent_id' => 6
  1876. ),
  1877. ),
  1878. array(
  1879. 'ModelName' => array(
  1880. 'id' => 8,
  1881. 'parent_id' => 6
  1882. ),
  1883. ),
  1884. array(
  1885. 'ModelName' => array(
  1886. 'id' => 9,
  1887. 'parent_id' => 6
  1888. ),
  1889. ),
  1890. array(
  1891. 'ModelName' => array(
  1892. 'id' => 10,
  1893. 'parent_id' => 6
  1894. )
  1895. )
  1896. );
  1897. $expected = array(
  1898. array(
  1899. 'ModelName' => array(
  1900. 'id' => 6,
  1901. 'parent_id' => null
  1902. ),
  1903. 'children' => array(
  1904. array(
  1905. 'ModelName' => array(
  1906. 'id' => 7,
  1907. 'parent_id' => 6
  1908. ),
  1909. 'children' => array()
  1910. ),
  1911. array(
  1912. 'ModelName' => array(
  1913. 'id' => 8,
  1914. 'parent_id' => 6
  1915. ),
  1916. 'children' => array()
  1917. ),
  1918. array(
  1919. 'ModelName' => array(
  1920. 'id' => 9,
  1921. 'parent_id' => 6
  1922. ),
  1923. 'children' => array()
  1924. ),
  1925. array(
  1926. 'ModelName' => array(
  1927. 'id' => 10,
  1928. 'parent_id' => 6
  1929. ),
  1930. 'children' => array()
  1931. )
  1932. )
  1933. )
  1934. );
  1935. $result = Hash::nest($input, array('root' => 6));
  1936. $this->assertEquals($expected, $result);
  1937. }
  1938. /**
  1939. * test Hash nest with a 1d array - this method should be able to handle any type of array input
  1940. *
  1941. * @return void
  1942. */
  1943. public function testNest1Dimensional() {
  1944. $input = array(
  1945. array(
  1946. 'id' => 1,
  1947. 'parent_id' => null
  1948. ),
  1949. array(
  1950. 'id' => 2,
  1951. 'parent_id' => 1
  1952. ),
  1953. array(
  1954. 'id' => 3,
  1955. 'parent_id' => 1
  1956. ),
  1957. array(
  1958. 'id' => 4,
  1959. 'parent_id' => 1
  1960. ),
  1961. array(
  1962. 'id' => 5,
  1963. 'parent_id' => 1
  1964. ),
  1965. array(
  1966. 'id' => 6,
  1967. 'parent_id' => null
  1968. ),
  1969. array(
  1970. 'id' => 7,
  1971. 'parent_id' => 6
  1972. ),
  1973. array(
  1974. 'id' => 8,
  1975. 'parent_id' => 6
  1976. ),
  1977. array(
  1978. 'id' => 9,
  1979. 'parent_id' => 6
  1980. ),
  1981. array(
  1982. 'id' => 10,
  1983. 'parent_id' => 6
  1984. )
  1985. );
  1986. $expected = array(
  1987. array(
  1988. 'id' => 1,
  1989. 'parent_id' => null,
  1990. 'children' => array(
  1991. array(
  1992. 'id' => 2,
  1993. 'parent_id' => 1,
  1994. 'children' => array()
  1995. ),
  1996. array(
  1997. 'id' => 3,
  1998. 'parent_id' => 1,
  1999. 'children' => array()
  2000. ),
  2001. array(
  2002. 'id' => 4,
  2003. 'parent_id' => 1,
  2004. 'children' => array()
  2005. ),
  2006. array(
  2007. 'id' => 5,
  2008. 'parent_id' => 1,
  2009. 'children' => array()
  2010. ),
  2011. )
  2012. ),
  2013. array(
  2014. 'id' => 6,
  2015. 'parent_id' => null,
  2016. 'children' => array(
  2017. array(
  2018. 'id' => 7,
  2019. 'parent_id' => 6,
  2020. 'children' => array()
  2021. ),
  2022. array(
  2023. 'id' => 8,
  2024. 'parent_id' => 6,
  2025. 'children' => array()
  2026. ),
  2027. array(
  2028. 'id' => 9,
  2029. 'parent_id' => 6,
  2030. 'children' => array()
  2031. ),
  2032. array(
  2033. 'id' => 10,
  2034. 'parent_id' => 6,
  2035. 'children' => array()
  2036. )
  2037. )
  2038. )
  2039. );
  2040. $result = Hash::nest($input, array('idPath' => '{n}.id', 'parentPath' => '{n}.parent_id'));
  2041. $this->assertEquals($expected, $result);
  2042. }
  2043. /**
  2044. * test Hash nest with no specified parent data.
  2045. *
  2046. * The result should be the same as the input.
  2047. * For an easier comparison, unset all the empty children arrays from the result
  2048. *
  2049. * @return void
  2050. */
  2051. public function testMissingParent() {
  2052. $input = array(
  2053. array(
  2054. 'id' => 1,
  2055. ),
  2056. array(
  2057. 'id' => 2,
  2058. ),
  2059. array(
  2060. 'id' => 3,
  2061. ),
  2062. array(
  2063. 'id' => 4,
  2064. ),
  2065. array(
  2066. 'id' => 5,
  2067. ),
  2068. array(
  2069. 'id' => 6,
  2070. ),
  2071. array(
  2072. 'id' => 7,
  2073. ),
  2074. array(
  2075. 'id' => 8,
  2076. ),
  2077. array(
  2078. 'id' => 9,
  2079. ),
  2080. array(
  2081. 'id' => 10,
  2082. )
  2083. );
  2084. $result = Hash::nest($input, array('idPath' => '{n}.id', 'parentPath' => '{n}.parent_id'));
  2085. foreach ($result as &$row) {
  2086. if (empty($row['children'])) {
  2087. unset($row['children']);
  2088. }
  2089. }
  2090. $this->assertEquals($input, $result);
  2091. }
  2092. /**
  2093. * Tests that nest() throws an InvalidArgumentException when providing an invalid input.
  2094. *
  2095. * @expectedException InvalidArgumentException
  2096. * @return void
  2097. */
  2098. public function testNestInvalid() {
  2099. $input = array(
  2100. array(
  2101. 'ParentCategory' => array(
  2102. 'id' => '1',
  2103. 'name' => 'Lorem ipsum dolor sit amet',
  2104. 'parent_id' => '1'
  2105. )
  2106. )
  2107. );
  2108. Hash::nest($input);
  2109. }
  2110. /**
  2111. * testMergeDiff method
  2112. *
  2113. * @return void
  2114. */
  2115. public function testMergeDiff() {
  2116. $first = array(
  2117. 'ModelOne' => array(
  2118. 'id' => 1001,
  2119. 'field_one' => 'a1.m1.f1',
  2120. 'field_two' => 'a1.m1.f2'
  2121. )
  2122. );
  2123. $second = array(
  2124. 'ModelTwo' => array(
  2125. 'id' => 1002,
  2126. 'field_one' => 'a2.m2.f1',
  2127. 'field_two' => 'a2.m2.f2'
  2128. )
  2129. );
  2130. $result = Hash::mergeDiff($first, $second);
  2131. $this->assertEquals($result, $first + $second);
  2132. $result = Hash::mergeDiff($first, array());
  2133. $this->assertEquals($result, $first);
  2134. $result = Hash::mergeDiff(array(), $first);
  2135. $this->assertEquals($result, $first);
  2136. $third = array(
  2137. 'ModelOne' => array(
  2138. 'id' => 1003,
  2139. 'field_one' => 'a3.m1.f1',
  2140. 'field_two' => 'a3.m1.f2',
  2141. 'field_three' => 'a3.m1.f3'
  2142. )
  2143. );
  2144. $result = Hash::mergeDiff($first, $third);
  2145. $expected = array(
  2146. 'ModelOne' => array(
  2147. 'id' => 1001,
  2148. 'field_one' => 'a1.m1.f1',
  2149. 'field_two' => 'a1.m1.f2',
  2150. 'field_three' => 'a3.m1.f3'
  2151. )
  2152. );
  2153. $this->assertEquals($expected, $result);
  2154. $first = array(
  2155. 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2')),
  2156. 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2'))
  2157. );
  2158. $second = array(
  2159. 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's2.0.m1.f1', 'field_two' => 's2.0.m1.f2')),
  2160. 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's2.1.m2.f2', 'field_two' => 's2.1.m2.f2'))
  2161. );
  2162. $result = Hash::mergeDiff($first, $second);
  2163. $this->assertEquals($result, $first);
  2164. $third = array(
  2165. 0 => array(
  2166. 'ModelThree' => array(
  2167. 'id' => 1003,
  2168. 'field_one' => 's3.0.m3.f1',
  2169. 'field_two' => 's3.0.m3.f2'
  2170. )
  2171. )
  2172. );
  2173. $result = Hash::mergeDiff($first, $third);
  2174. $expected = array(
  2175. 0 => array(
  2176. 'ModelOne' => array(
  2177. 'id' => 1001,
  2178. 'field_one' => 's1.0.m1.f1',
  2179. 'field_two' => 's1.0.m1.f2'
  2180. ),
  2181. 'ModelThree' => array(
  2182. 'id' => 1003,
  2183. 'field_one' => 's3.0.m3.f1',
  2184. 'field_two' => 's3.0.m3.f2'
  2185. )
  2186. ),
  2187. 1 => array(
  2188. 'ModelTwo' => array(
  2189. 'id' => 1002,
  2190. 'field_one' => 's1.1.m2.f2',
  2191. 'field_two' => 's1.1.m2.f2'
  2192. )
  2193. )
  2194. );
  2195. $this->assertEquals($expected, $result);
  2196. $result = Hash::mergeDiff($first, null);
  2197. $this->assertEquals($result, $first);
  2198. $result = Hash::mergeDiff($first, $second);
  2199. $this->assertEquals($result, $first + $second);
  2200. }
  2201. /**
  2202. * Tests Hash::expand
  2203. *
  2204. * @return void
  2205. */
  2206. public function testExpand() {
  2207. $data = array('My', 'Array', 'To', 'Flatten');
  2208. $flat = Hash::flatten($data);
  2209. $result = Hash::expand($flat);
  2210. $this->assertEquals($data, $result);
  2211. $data = array(
  2212. '0.Post.id' => '1', '0.Post.author_id' => '1', '0.Post.title' => 'First Post', '0.Author.id' => '1',
  2213. '0.Author.user' => 'nate', '0.Author.password' => 'foo', '1.Post.id' => '2', '1.Post.author_id' => '3',
  2214. '1.Post.title' => 'Second Post', '1.Post.body' => 'Second Post Body', '1.Author.id' => '3',
  2215. '1.Author.user' => 'larry', '1.Author.password' => null
  2216. );
  2217. $result = Hash::expand($data);
  2218. $expected = array(
  2219. array(
  2220. 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post'),
  2221. 'Author' => array('id' => '1', 'user' => 'nate', 'password' => 'foo'),
  2222. ),
  2223. array(
  2224. 'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body'),
  2225. 'Author' => array('id' => '3', 'user' => 'larry', 'password' => null),
  2226. )
  2227. );
  2228. $this->assertEquals($expected, $result);
  2229. $data = array(
  2230. '0/Post/id' => 1,
  2231. '0/Post/name' => 'test post'
  2232. );
  2233. $result = Hash::expand($data, '/');
  2234. $expected = array(
  2235. array(
  2236. 'Post' => array(
  2237. 'id' => 1,
  2238. 'name' => 'test post'
  2239. )
  2240. )
  2241. );
  2242. $this->assertEquals($expected, $result);
  2243. $data = array('a.b.100.a' => null, 'a.b.200.a' => null);
  2244. $expected = array(
  2245. 'a' => array(
  2246. 'b' => array(
  2247. 100 => array('a' => null),
  2248. 200 => array('a' => null)
  2249. )
  2250. )
  2251. );
  2252. $result = Hash::expand($data);
  2253. $this->assertEquals($expected, $result);
  2254. }
  2255. /**
  2256. * Test that flattening a large complex set doesn't loop forever.
  2257. *
  2258. * @return void
  2259. */
  2260. public function testFlattenInfiniteLoop() {
  2261. $data = array(
  2262. 'Order.ASI' => '0',
  2263. 'Order.Accounting' => '0',
  2264. 'Order.Admin' => '0',
  2265. 'Order.Art' => '0',
  2266. 'Order.ArtChecker' => '0',
  2267. 'Order.Canned' => '0',
  2268. 'Order.Customer_Tags' => '',
  2269. 'Order.Embroidery' => '0',
  2270. 'Order.Item.0.Product.style_number' => 'a11222',
  2271. 'Order.Item.0.Product.slug' => 'a11222',
  2272. 'Order.Item.0.Product._id' => '4ff8b8d3d7bbe8ad30000000',
  2273. 'Order.Item.0.Product.Color.slug' => 'kelly_green',
  2274. 'Order.Item.0.Product.ColorSizes.0.Color.color' => 'Sport Grey',
  2275. 'Order.Item.0.Product.ColorSizes.0.Color.slug' => 'sport_grey',
  2276. 'Order.Item.0.Product.ColorSizes.1.Color.color' => 'Kelly Green',
  2277. 'Order.Item.0.Product.ColorSizes.1.Color.slug' => 'kelly_green',
  2278. 'Order.Item.0.Product.ColorSizes.2.Color.color' => 'Orange',
  2279. 'Order.Item.0.Product.ColorSizes.2.Color.slug' => 'orange',
  2280. 'Order.Item.0.Product.ColorSizes.3.Color.color' => 'Yellow Haze',
  2281. 'Order.Item.0.Product.ColorSizes.3.Color.slug' => 'yellow_haze',
  2282. 'Order.Item.0.Product.brand' => 'OUTER BANKS',
  2283. 'Order.Item.0.Product.style' => 'T-shirt',
  2284. 'Order.Item.0.Product.description' => 'uhiuhuih oin ooi ioo ioio',
  2285. 'Order.Item.0.Product.sizes.0.Size.qty' => '',
  2286. 'Order.Item.0.Product.sizes.0.Size.size' => '0-3mo',
  2287. 'Order.Item.0.Product.sizes.0.Size.id' => '38',
  2288. 'Order.Item.0.Product.sizes.1.Size.qty' => '',
  2289. 'Order.Item.0.Product.sizes.1.Size.size' => '3-6mo',
  2290. 'Order.Item.0.Product.sizes.1.Size.id' => '39',
  2291. 'Order.Item.0.Product.sizes.2.Size.qty' => '78',
  2292. 'Order.Item.0.Product.sizes.2.Size.size' => '6-9mo',
  2293. 'Order.Item.0.Product.sizes.2.Size.id' => '40',
  2294. 'Order.Item.0.Product.sizes.3.Size.qty' => '',
  2295. 'Order.Item.0.Product.sizes.3.Size.size' => '6-12mo',
  2296. 'Order.Item.0.Product.sizes.3.Size.id' => '41',
  2297. 'Order.Item.0.Product.sizes.4.Size.qty' => '',
  2298. 'Order.Item.0.Product.sizes.4.Size.size' => '12-18mo',
  2299. 'Order.Item.0.Product.sizes.4.Size.id' => '42',
  2300. 'Order.Item.0.Art.imprint_locations.0.id' => 2,
  2301. 'Order.Item.0.Art.imprint_locations.0.name' => 'Left Chest',
  2302. 'Order.Item.0.Art.imprint_locations.0.imprint_type.id' => 7,
  2303. 'Order.Item.0.Art.imprint_locations.0.imprint_type.type' => 'Embroidery',
  2304. 'Order.Item.0.Art.imprint_locations.0.art' => '',
  2305. 'Order.Item.0.Art.imprint_locations.0.num_colors' => 3,
  2306. 'Order.Item.0.Art.imprint_locations.0.description' => 'Wooo! This is Embroidery!!',
  2307. 'Order.Item.0.Art.imprint_locations.0.lines.0' => 'Platen',
  2308. 'Order.Item.0.Art.imprint_locations.0.lines.1' => 'Logo',
  2309. 'Order.Item.0.Art.imprint_locations.0.height' => 4,
  2310. 'Order.Item.0.Art.imprint_locations.0.width' => 5,
  2311. 'Order.Item.0.Art.imprint_locations.0.stitch_density' => 'Light',
  2312. 'Order.Item.0.Art.imprint_locations.0.metallic_thread' => true,
  2313. 'Order.Item.0.Art.imprint_locations.1.id' => 4,
  2314. 'Order.Item.0.Art.imprint_locations.1.name' => 'Full Back',
  2315. 'Order.Item.0.Art.imprint_locations.1.imprint_type.id' => 6,
  2316. 'Order.Item.0.Art.imprint_locations.1.imprint_type.type' => 'Screenprinting',
  2317. 'Order.Item.0.Art.imprint_locations.1.art' => '',
  2318. 'Order.Item.0.Art.imprint_locations.1.num_colors' => 3,
  2319. 'Order.Item.0.Art.imprint_locations.1.description' => 'Wooo! This is Screenprinting!!',
  2320. 'Order.Item.0.Art.imprint_locations.1.lines.0' => 'Platen',
  2321. 'Order.Item.0.Art.imprint_locations.1.lines.1' => 'Logo',
  2322. 'Order.Item.0.Art.imprint_locations.2.id' => 26,
  2323. 'Order.Item.0.Art.imprint_locations.2.name' => 'HS - JSY Name Below',
  2324. 'Order.Item.0.Art.imprint_locations.2.imprint_type.id' => 9,
  2325. 'Order.Item.0.Art.imprint_locations.2.imprint_type.type' => 'Names',
  2326. 'Order.Item.0.Art.imprint_locations.2.description' => 'Wooo! This is Names!!',
  2327. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.active' => 1,
  2328. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.name' => 'Benjamin Talavera',
  2329. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.color' => 'Red',
  2330. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.height' => '3',
  2331. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.layout' => 'Arched',
  2332. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.style' => 'Classic',
  2333. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.active' => 0,
  2334. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.name' => 'Rishi Narayan',
  2335. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.color' => 'Cardinal',
  2336. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.height' => '4',
  2337. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.layout' => 'Straight',
  2338. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.style' => 'Team US',
  2339. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.active' => 1,
  2340. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.name' => 'Brandon Plasters',
  2341. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.color' => 'Red',
  2342. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.height' => '3',
  2343. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.layout' => 'Arched',
  2344. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.style' => 'Classic',
  2345. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.active' => 0,
  2346. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.name' => 'Andrew Reed',
  2347. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.color' => 'Cardinal',
  2348. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.height' => '4',
  2349. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.layout' => 'Straight',
  2350. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.style' => 'Team US',
  2351. 'Order.Job.0._id' => 'job-1',
  2352. 'Order.Job.0.type' => 'screenprinting',
  2353. 'Order.Job.0.postPress' => 'job-2',
  2354. 'Order.Job.1._id' => 'job-2',
  2355. 'Order.Job.1.type' => 'embroidery',
  2356. 'Order.Postpress' => '0',
  2357. 'Order.PriceAdjustment.0._id' => 'price-adjustment-1',
  2358. 'Order.PriceAdjustment.0.adjustment' => '-20',
  2359. 'Order.PriceAdjustment.0.adjustment_type' => 'percent',
  2360. 'Order.PriceAdjustment.0.type' => 'grand_total',
  2361. 'Order.PriceAdjustment.1.adjustment' => '20',
  2362. 'Order.PriceAdjustment.1.adjustment_type' => 'flat',
  2363. 'Order.PriceAdjustment.1.min-items' => '10',
  2364. 'Order.PriceAdjustment.1.type' => 'min-items',
  2365. 'Order.PriceAdjustment.1._id' => 'another-test-adjustment',
  2366. 'Order.Purchasing' => '0',
  2367. 'Order.QualityControl' => '0',
  2368. 'Order.Receiving' => '0',
  2369. 'Order.ScreenPrinting' => '0',
  2370. 'Order.Stage.art_approval' => 0,
  2371. 'Order.Stage.draft' => 1,
  2372. 'Order.Stage.quote' => 1,
  2373. 'Order.Stage.order' => 1,
  2374. 'Order.StoreLiason' => '0',
  2375. 'Order.Tag_UI_Email' => '',
  2376. 'Order.Tags' => '',
  2377. 'Order._id' => 'test-2',
  2378. 'Order.add_print_location' => '',
  2379. 'Order.created' => '2011-Dec-29 05:40:18',
  2380. 'Order.force_admin' => '0',
  2381. 'Order.modified' => '2012-Jul-25 01:24:49',
  2382. 'Order.name' => 'towering power',
  2383. 'Order.order_id' => '135961',
  2384. 'Order.slug' => 'test-2',
  2385. 'Order.title' => 'test job 2',
  2386. 'Order.type' => 'ttt'
  2387. );
  2388. $expanded = Hash::expand($data);
  2389. $flattened = Hash::flatten($expanded);
  2390. $this->assertEquals($data, $flattened);
  2391. }
  2392. }