HashTest.php 65 KB

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