HashTest.php 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588
  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 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 = static::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 = static::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 = static::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 = static::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 = static::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 = static::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 = static::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 = static::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. $items = array(
  1104. array('Item' => array('image' => 'img1.jpg')),
  1105. array('Item' => array('image' => 'img99.jpg')),
  1106. array('Item' => array('image' => 'img12.jpg')),
  1107. array('Item' => array('image' => 'img10.jpg')),
  1108. array('Item' => array('image' => 'img2.jpg')),
  1109. );
  1110. $result = Hash::sort($items, '{n}.Item.image', 'desc', 'natural');
  1111. $expected = array(
  1112. array('Item' => array('image' => 'img99.jpg')),
  1113. array('Item' => array('image' => 'img12.jpg')),
  1114. array('Item' => array('image' => 'img10.jpg')),
  1115. array('Item' => array('image' => 'img2.jpg')),
  1116. array('Item' => array('image' => 'img1.jpg')),
  1117. );
  1118. $this->assertEquals($expected, $result);
  1119. $result = Hash::sort($items, '{n}.Item.image', 'asc', 'natural');
  1120. $expected = array(
  1121. array('Item' => array('image' => 'img1.jpg')),
  1122. array('Item' => array('image' => 'img2.jpg')),
  1123. array('Item' => array('image' => 'img10.jpg')),
  1124. array('Item' => array('image' => 'img12.jpg')),
  1125. array('Item' => array('image' => 'img99.jpg')),
  1126. );
  1127. $this->assertEquals($expected, $result);
  1128. }
  1129. /**
  1130. * Test that sort() with 'natural' type will fallback to 'regular' as SORT_NATURAL is introduced in PHP 5.4
  1131. *
  1132. * @return void
  1133. */
  1134. public function testSortNaturalFallbackToRegular() {
  1135. $a = array(
  1136. 0 => array('Person' => array('name' => 'Jeff')),
  1137. 1 => array('Shirt' => array('color' => 'black'))
  1138. );
  1139. $b = array(
  1140. 0 => array('Shirt' => array('color' => 'black')),
  1141. 1 => array('Person' => array('name' => 'Jeff')),
  1142. );
  1143. $sorted = Hash::sort($a, '{n}.Person.name', 'asc', 'natural');
  1144. $this->assertEquals($sorted, $b);
  1145. }
  1146. /**
  1147. * test sorting with out of order keys.
  1148. *
  1149. * @return void
  1150. */
  1151. public function testSortWithOutOfOrderKeys() {
  1152. $data = array(
  1153. 9 => array('class' => 510, 'test2' => 2),
  1154. 1 => array('class' => 500, 'test2' => 1),
  1155. 2 => array('class' => 600, 'test2' => 2),
  1156. 5 => array('class' => 625, 'test2' => 4),
  1157. 0 => array('class' => 605, 'test2' => 3),
  1158. );
  1159. $expected = array(
  1160. array('class' => 500, 'test2' => 1),
  1161. array('class' => 510, 'test2' => 2),
  1162. array('class' => 600, 'test2' => 2),
  1163. array('class' => 605, 'test2' => 3),
  1164. array('class' => 625, 'test2' => 4),
  1165. );
  1166. $result = Hash::sort($data, '{n}.class', 'asc');
  1167. $this->assertEquals($expected, $result);
  1168. $result = Hash::sort($data, '{n}.test2', 'asc');
  1169. $this->assertEquals($expected, $result);
  1170. }
  1171. /**
  1172. * test sorting with string keys.
  1173. *
  1174. * @return void
  1175. */
  1176. public function testSortString() {
  1177. $toSort = array(
  1178. 'four' => array('number' => 4, 'some' => 'foursome'),
  1179. 'six' => array('number' => 6, 'some' => 'sixsome'),
  1180. 'five' => array('number' => 5, 'some' => 'fivesome'),
  1181. 'two' => array('number' => 2, 'some' => 'twosome'),
  1182. 'three' => array('number' => 3, 'some' => 'threesome')
  1183. );
  1184. $sorted = Hash::sort($toSort, '{s}.number', 'asc');
  1185. $expected = array(
  1186. 'two' => array('number' => 2, 'some' => 'twosome'),
  1187. 'three' => array('number' => 3, 'some' => 'threesome'),
  1188. 'four' => array('number' => 4, 'some' => 'foursome'),
  1189. 'five' => array('number' => 5, 'some' => 'fivesome'),
  1190. 'six' => array('number' => 6, 'some' => 'sixsome')
  1191. );
  1192. $this->assertEquals($expected, $sorted);
  1193. $menus = array(
  1194. 'blogs' => array('title' => 'Blogs', 'weight' => 3),
  1195. 'comments' => array('title' => 'Comments', 'weight' => 2),
  1196. 'users' => array('title' => 'Users', 'weight' => 1),
  1197. );
  1198. $expected = array(
  1199. 'users' => array('title' => 'Users', 'weight' => 1),
  1200. 'comments' => array('title' => 'Comments', 'weight' => 2),
  1201. 'blogs' => array('title' => 'Blogs', 'weight' => 3),
  1202. );
  1203. $result = Hash::sort($menus, '{s}.weight', 'ASC');
  1204. $this->assertEquals($expected, $result);
  1205. }
  1206. /**
  1207. * Test insert()
  1208. *
  1209. * @return void
  1210. */
  1211. public function testInsertSimple() {
  1212. $a = array(
  1213. 'pages' => array('name' => 'page')
  1214. );
  1215. $result = Hash::insert($a, 'files', array('name' => 'files'));
  1216. $expected = array(
  1217. 'pages' => array('name' => 'page'),
  1218. 'files' => array('name' => 'files')
  1219. );
  1220. $this->assertEquals($expected, $result);
  1221. $a = array(
  1222. 'pages' => array('name' => 'page')
  1223. );
  1224. $result = Hash::insert($a, 'pages.name', array());
  1225. $expected = array(
  1226. 'pages' => array('name' => array()),
  1227. );
  1228. $this->assertEquals($expected, $result);
  1229. }
  1230. /**
  1231. * Test inserting with multiple values.
  1232. *
  1233. * @return void
  1234. */
  1235. public function testInsertMulti() {
  1236. $data = static::articleData();
  1237. $result = Hash::insert($data, '{n}.Article.insert', 'value');
  1238. $this->assertEquals('value', $result[0]['Article']['insert']);
  1239. $this->assertEquals('value', $result[1]['Article']['insert']);
  1240. $result = Hash::insert($data, '{n}.Comment.{n}.insert', 'value');
  1241. $this->assertEquals('value', $result[0]['Comment'][0]['insert']);
  1242. $this->assertEquals('value', $result[0]['Comment'][1]['insert']);
  1243. $data = array(
  1244. 0 => array('Item' => array('id' => 1, 'title' => 'first')),
  1245. 1 => array('Item' => array('id' => 2, 'title' => 'second')),
  1246. 2 => array('Item' => array('id' => 3, 'title' => 'third')),
  1247. 3 => array('Item' => array('id' => 4, 'title' => 'fourth')),
  1248. 4 => array('Item' => array('id' => 5, 'title' => 'fifth')),
  1249. );
  1250. $result = Hash::insert($data, '{n}.Item[id=/\b2|\b4/]', array('test' => 2));
  1251. $expected = array(
  1252. 0 => array('Item' => array('id' => 1, 'title' => 'first')),
  1253. 1 => array('Item' => array('id' => 2, 'title' => 'second', 'test' => 2)),
  1254. 2 => array('Item' => array('id' => 3, 'title' => 'third')),
  1255. 3 => array('Item' => array('id' => 4, 'title' => 'fourth', 'test' => 2)),
  1256. 4 => array('Item' => array('id' => 5, 'title' => 'fifth')),
  1257. );
  1258. $this->assertEquals($expected, $result);
  1259. }
  1260. /**
  1261. * Test that insert() can insert data over a string value.
  1262. *
  1263. * @return void
  1264. */
  1265. public function testInsertOverwriteStringValue() {
  1266. $data = array(
  1267. 'Some' => array(
  1268. 'string' => 'value'
  1269. )
  1270. );
  1271. $result = Hash::insert($data, 'Some.string.value', array('values'));
  1272. $expected = array(
  1273. 'Some' => array(
  1274. 'string' => array(
  1275. 'value' => array('values')
  1276. )
  1277. )
  1278. );
  1279. $this->assertEquals($expected, $result);
  1280. }
  1281. /**
  1282. * Test remove() method.
  1283. *
  1284. * @return void
  1285. */
  1286. public function testRemove() {
  1287. $a = array(
  1288. 'pages' => array('name' => 'page'),
  1289. 'files' => array('name' => 'files')
  1290. );
  1291. $result = Hash::remove($a, 'files');
  1292. $expected = array(
  1293. 'pages' => array('name' => 'page')
  1294. );
  1295. $this->assertEquals($expected, $result);
  1296. $a = array(
  1297. 'pages' => array(
  1298. 0 => array('name' => 'main'),
  1299. 1 => array(
  1300. 'name' => 'about',
  1301. 'vars' => array('title' => 'page title')
  1302. )
  1303. )
  1304. );
  1305. $result = Hash::remove($a, 'pages.1.vars');
  1306. $expected = array(
  1307. 'pages' => array(
  1308. 0 => array('name' => 'main'),
  1309. 1 => array('name' => 'about')
  1310. )
  1311. );
  1312. $this->assertEquals($expected, $result);
  1313. $result = Hash::remove($a, 'pages.2.vars');
  1314. $expected = $a;
  1315. $this->assertEquals($expected, $result);
  1316. $a = array(
  1317. 0 => array(
  1318. 'name' => 'pages'
  1319. ),
  1320. 1 => array(
  1321. 'name' => 'files'
  1322. )
  1323. );
  1324. $result = Hash::remove($a, '{n}[name=files]');
  1325. $expected = array(
  1326. 0 => array(
  1327. 'name' => 'pages'
  1328. )
  1329. );
  1330. $this->assertEquals($expected, $result);
  1331. }
  1332. /**
  1333. * Test removing multiple values.
  1334. *
  1335. * @return void
  1336. */
  1337. public function testRemoveMulti() {
  1338. $data = static::articleData();
  1339. $result = Hash::remove($data, '{n}.Article.title');
  1340. $this->assertFalse(isset($result[0]['Article']['title']));
  1341. $this->assertFalse(isset($result[1]['Article']['title']));
  1342. $result = Hash::remove($data, '{n}.Article.{s}');
  1343. $this->assertFalse(isset($result[0]['Article']['id']));
  1344. $this->assertFalse(isset($result[0]['Article']['user_id']));
  1345. $this->assertFalse(isset($result[0]['Article']['title']));
  1346. $this->assertFalse(isset($result[0]['Article']['body']));
  1347. $data = array(
  1348. 0 => array('Item' => array('id' => 1, 'title' => 'first')),
  1349. 1 => array('Item' => array('id' => 2, 'title' => 'second')),
  1350. 2 => array('Item' => array('id' => 3, 'title' => 'third')),
  1351. 3 => array('Item' => array('id' => 4, 'title' => 'fourth')),
  1352. 4 => array('Item' => array('id' => 5, 'title' => 'fifth')),
  1353. );
  1354. $result = Hash::remove($data, '{n}.Item[id=/\b2|\b4/]');
  1355. $expected = array(
  1356. 0 => array('Item' => array('id' => 1, 'title' => 'first')),
  1357. 2 => array('Item' => array('id' => 3, 'title' => 'third')),
  1358. 4 => array('Item' => array('id' => 5, 'title' => 'fifth')),
  1359. );
  1360. $this->assertEquals($expected, $result);
  1361. }
  1362. /**
  1363. * testCheck method
  1364. *
  1365. * @return void
  1366. */
  1367. public function testCheck() {
  1368. $set = array(
  1369. 'My Index 1' => array('First' => 'The first item')
  1370. );
  1371. $this->assertTrue(Hash::check($set, 'My Index 1.First'));
  1372. $this->assertTrue(Hash::check($set, 'My Index 1'));
  1373. $set = array(
  1374. 'My Index 1' => array(
  1375. 'First' => array(
  1376. 'Second' => array(
  1377. 'Third' => array(
  1378. 'Fourth' => 'Heavy. Nesting.'
  1379. )
  1380. )
  1381. )
  1382. )
  1383. );
  1384. $this->assertTrue(Hash::check($set, 'My Index 1.First.Second'));
  1385. $this->assertTrue(Hash::check($set, 'My Index 1.First.Second.Third'));
  1386. $this->assertTrue(Hash::check($set, 'My Index 1.First.Second.Third.Fourth'));
  1387. $this->assertFalse(Hash::check($set, 'My Index 1.First.Seconds.Third.Fourth'));
  1388. }
  1389. /**
  1390. * testCombine method
  1391. *
  1392. * @return void
  1393. */
  1394. public function testCombine() {
  1395. $result = Hash::combine(array(), '{n}.User.id', '{n}.User.Data');
  1396. $this->assertTrue(empty($result));
  1397. $a = static::userData();
  1398. $result = Hash::combine($a, '{n}.User.id');
  1399. $expected = array(2 => null, 14 => null, 25 => null);
  1400. $this->assertEquals($expected, $result);
  1401. $result = Hash::combine($a, '{n}.User.id', '{n}.User.non-existant');
  1402. $expected = array(2 => null, 14 => null, 25 => null);
  1403. $this->assertEquals($expected, $result);
  1404. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data');
  1405. $expected = array(
  1406. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1407. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'),
  1408. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
  1409. $this->assertEquals($expected, $result);
  1410. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name');
  1411. $expected = array(
  1412. 2 => 'Mariano Iglesias',
  1413. 14 => 'Larry E. Masters',
  1414. 25 => 'The Gwoo');
  1415. $this->assertEquals($expected, $result);
  1416. }
  1417. /**
  1418. * test combine() giving errors on key/value length mismatches.
  1419. *
  1420. * @expectedException RuntimeException
  1421. * @return void
  1422. */
  1423. public function testCombineErrorMissingValue() {
  1424. $data = array(
  1425. array('User' => array('id' => 1, 'name' => 'mark')),
  1426. array('User' => array('name' => 'jose')),
  1427. );
  1428. Hash::combine($data, '{n}.User.id', '{n}.User.name');
  1429. }
  1430. /**
  1431. * test combine() giving errors on key/value length mismatches.
  1432. *
  1433. * @expectedException RuntimeException
  1434. * @return void
  1435. */
  1436. public function testCombineErrorMissingKey() {
  1437. $data = array(
  1438. array('User' => array('id' => 1, 'name' => 'mark')),
  1439. array('User' => array('id' => 2)),
  1440. );
  1441. Hash::combine($data, '{n}.User.id', '{n}.User.name');
  1442. }
  1443. /**
  1444. * test combine() with a group path.
  1445. *
  1446. * @return void
  1447. */
  1448. public function testCombineWithGroupPath() {
  1449. $a = static::userData();
  1450. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  1451. $expected = array(
  1452. 1 => array(
  1453. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1454. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')
  1455. ),
  1456. 2 => array(
  1457. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')
  1458. )
  1459. );
  1460. $this->assertEquals($expected, $result);
  1461. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  1462. $expected = array(
  1463. 1 => array(
  1464. 2 => 'Mariano Iglesias',
  1465. 25 => 'The Gwoo'
  1466. ),
  1467. 2 => array(
  1468. 14 => 'Larry E. Masters'
  1469. )
  1470. );
  1471. $this->assertEquals($expected, $result);
  1472. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  1473. $expected = array(
  1474. 1 => array(
  1475. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1476. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')
  1477. ),
  1478. 2 => array(
  1479. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')
  1480. )
  1481. );
  1482. $this->assertEquals($expected, $result);
  1483. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  1484. $expected = array(
  1485. 1 => array(
  1486. 2 => 'Mariano Iglesias',
  1487. 25 => 'The Gwoo'
  1488. ),
  1489. 2 => array(
  1490. 14 => 'Larry E. Masters'
  1491. )
  1492. );
  1493. $this->assertEquals($expected, $result);
  1494. }
  1495. /**
  1496. * Test combine with formatting rules.
  1497. *
  1498. * @return void
  1499. */
  1500. public function testCombineWithFormatting() {
  1501. $a = static::userData();
  1502. $result = Hash::combine(
  1503. $a,
  1504. '{n}.User.id',
  1505. array('%1$s: %2$s', '{n}.User.Data.user', '{n}.User.Data.name'),
  1506. '{n}.User.group_id'
  1507. );
  1508. $expected = array(
  1509. 1 => array(
  1510. 2 => 'mariano.iglesias: Mariano Iglesias',
  1511. 25 => 'gwoo: The Gwoo'
  1512. ),
  1513. 2 => array(
  1514. 14 => 'phpnut: Larry E. Masters'
  1515. )
  1516. );
  1517. $this->assertEquals($expected, $result);
  1518. $result = Hash::combine(
  1519. $a,
  1520. array(
  1521. '%s: %s',
  1522. '{n}.User.Data.user',
  1523. '{n}.User.Data.name'
  1524. ),
  1525. '{n}.User.id'
  1526. );
  1527. $expected = array(
  1528. 'mariano.iglesias: Mariano Iglesias' => 2,
  1529. 'phpnut: Larry E. Masters' => 14,
  1530. 'gwoo: The Gwoo' => 25
  1531. );
  1532. $this->assertEquals($expected, $result);
  1533. $result = Hash::combine(
  1534. $a,
  1535. array('%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'),
  1536. '{n}.User.Data.name'
  1537. );
  1538. $expected = array(
  1539. 'mariano.iglesias: 2' => 'Mariano Iglesias',
  1540. 'phpnut: 14' => 'Larry E. Masters',
  1541. 'gwoo: 25' => 'The Gwoo'
  1542. );
  1543. $this->assertEquals($expected, $result);
  1544. $result = Hash::combine(
  1545. $a,
  1546. array('%2$d: %1$s', '{n}.User.Data.user', '{n}.User.id'),
  1547. '{n}.User.Data.name'
  1548. );
  1549. $expected = array(
  1550. '2: mariano.iglesias' => 'Mariano Iglesias',
  1551. '14: phpnut' => 'Larry E. Masters',
  1552. '25: gwoo' => 'The Gwoo'
  1553. );
  1554. $this->assertEquals($expected, $result);
  1555. }
  1556. /**
  1557. * testFormat method
  1558. *
  1559. * @return void
  1560. */
  1561. public function testFormat() {
  1562. $data = static::userData();
  1563. $result = Hash::format(
  1564. $data,
  1565. array('{n}.User.Data.user', '{n}.User.id'),
  1566. '%s, %s'
  1567. );
  1568. $expected = array(
  1569. 'mariano.iglesias, 2',
  1570. 'phpnut, 14',
  1571. 'gwoo, 25'
  1572. );
  1573. $this->assertEquals($expected, $result);
  1574. $result = Hash::format(
  1575. $data,
  1576. array('{n}.User.Data.user', '{n}.User.id'),
  1577. '%2$s, %1$s'
  1578. );
  1579. $expected = array(
  1580. '2, mariano.iglesias',
  1581. '14, phpnut',
  1582. '25, gwoo'
  1583. );
  1584. $this->assertEquals($expected, $result);
  1585. }
  1586. /**
  1587. * testFormattingNullValues method
  1588. *
  1589. * @return void
  1590. */
  1591. public function testFormatNullValues() {
  1592. $data = array(
  1593. array('Person' => array(
  1594. 'first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42'
  1595. )),
  1596. array('Person' => array(
  1597. 'first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => null
  1598. )),
  1599. array('Person' => array(
  1600. 'first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => null
  1601. ))
  1602. );
  1603. $result = Hash::format($data, array('{n}.Person.something'), '%s');
  1604. $expected = array('42', '', '');
  1605. $this->assertEquals($expected, $result);
  1606. $result = Hash::format($data, array('{n}.Person.city', '{n}.Person.something'), '%s, %s');
  1607. $expected = array('Boston, 42', 'Boondock, ', 'Venice Beach, ');
  1608. $this->assertEquals($expected, $result);
  1609. }
  1610. /**
  1611. * Test map()
  1612. *
  1613. * @return void
  1614. */
  1615. public function testMap() {
  1616. $data = static::articleData();
  1617. $result = Hash::map($data, '{n}.Article.id', array($this, 'mapCallback'));
  1618. $expected = array(2, 4, 6, 8, 10);
  1619. $this->assertEquals($expected, $result);
  1620. }
  1621. /**
  1622. * testApply
  1623. *
  1624. * @return void
  1625. */
  1626. public function testApply() {
  1627. $data = static::articleData();
  1628. $result = Hash::apply($data, '{n}.Article.id', 'array_sum');
  1629. $this->assertEquals(15, $result);
  1630. }
  1631. /**
  1632. * Test reduce()
  1633. *
  1634. * @return void
  1635. */
  1636. public function testReduce() {
  1637. $data = static::articleData();
  1638. $result = Hash::reduce($data, '{n}.Article.id', array($this, 'reduceCallback'));
  1639. $this->assertEquals(15, $result);
  1640. }
  1641. /**
  1642. * testing method for map callbacks.
  1643. *
  1644. * @param mixed $value Value
  1645. * @return mixed
  1646. */
  1647. public function mapCallback($value) {
  1648. return $value * 2;
  1649. }
  1650. /**
  1651. * testing method for reduce callbacks.
  1652. *
  1653. * @param mixed $one First param
  1654. * @param mixed $two Second param
  1655. * @return mixed
  1656. */
  1657. public function reduceCallback($one, $two) {
  1658. return $one + $two;
  1659. }
  1660. /**
  1661. * test Hash nest with a normal model result set. For kicks rely on Hash nest detecting the key names
  1662. * automatically
  1663. *
  1664. * @return void
  1665. */
  1666. public function testNestModel() {
  1667. $input = array(
  1668. array(
  1669. 'ModelName' => array(
  1670. 'id' => 1,
  1671. 'parent_id' => null
  1672. ),
  1673. ),
  1674. array(
  1675. 'ModelName' => array(
  1676. 'id' => 2,
  1677. 'parent_id' => 1
  1678. ),
  1679. ),
  1680. array(
  1681. 'ModelName' => array(
  1682. 'id' => 3,
  1683. 'parent_id' => 1
  1684. ),
  1685. ),
  1686. array(
  1687. 'ModelName' => array(
  1688. 'id' => 4,
  1689. 'parent_id' => 1
  1690. ),
  1691. ),
  1692. array(
  1693. 'ModelName' => array(
  1694. 'id' => 5,
  1695. 'parent_id' => 1
  1696. ),
  1697. ),
  1698. array(
  1699. 'ModelName' => array(
  1700. 'id' => 6,
  1701. 'parent_id' => null
  1702. ),
  1703. ),
  1704. array(
  1705. 'ModelName' => array(
  1706. 'id' => 7,
  1707. 'parent_id' => 6
  1708. ),
  1709. ),
  1710. array(
  1711. 'ModelName' => array(
  1712. 'id' => 8,
  1713. 'parent_id' => 6
  1714. ),
  1715. ),
  1716. array(
  1717. 'ModelName' => array(
  1718. 'id' => 9,
  1719. 'parent_id' => 6
  1720. ),
  1721. ),
  1722. array(
  1723. 'ModelName' => array(
  1724. 'id' => 10,
  1725. 'parent_id' => 6
  1726. )
  1727. )
  1728. );
  1729. $expected = array(
  1730. array(
  1731. 'ModelName' => array(
  1732. 'id' => 1,
  1733. 'parent_id' => null
  1734. ),
  1735. 'children' => array(
  1736. array(
  1737. 'ModelName' => array(
  1738. 'id' => 2,
  1739. 'parent_id' => 1
  1740. ),
  1741. 'children' => array()
  1742. ),
  1743. array(
  1744. 'ModelName' => array(
  1745. 'id' => 3,
  1746. 'parent_id' => 1
  1747. ),
  1748. 'children' => array()
  1749. ),
  1750. array(
  1751. 'ModelName' => array(
  1752. 'id' => 4,
  1753. 'parent_id' => 1
  1754. ),
  1755. 'children' => array()
  1756. ),
  1757. array(
  1758. 'ModelName' => array(
  1759. 'id' => 5,
  1760. 'parent_id' => 1
  1761. ),
  1762. 'children' => array()
  1763. ),
  1764. )
  1765. ),
  1766. array(
  1767. 'ModelName' => array(
  1768. 'id' => 6,
  1769. 'parent_id' => null
  1770. ),
  1771. 'children' => array(
  1772. array(
  1773. 'ModelName' => array(
  1774. 'id' => 7,
  1775. 'parent_id' => 6
  1776. ),
  1777. 'children' => array()
  1778. ),
  1779. array(
  1780. 'ModelName' => array(
  1781. 'id' => 8,
  1782. 'parent_id' => 6
  1783. ),
  1784. 'children' => array()
  1785. ),
  1786. array(
  1787. 'ModelName' => array(
  1788. 'id' => 9,
  1789. 'parent_id' => 6
  1790. ),
  1791. 'children' => array()
  1792. ),
  1793. array(
  1794. 'ModelName' => array(
  1795. 'id' => 10,
  1796. 'parent_id' => 6
  1797. ),
  1798. 'children' => array()
  1799. )
  1800. )
  1801. )
  1802. );
  1803. $result = Hash::nest($input);
  1804. $this->assertEquals($expected, $result);
  1805. }
  1806. /**
  1807. * test Hash nest with a normal model result set, and a nominated root id
  1808. *
  1809. * @return void
  1810. */
  1811. public function testNestModelExplicitRoot() {
  1812. $input = array(
  1813. array(
  1814. 'ModelName' => array(
  1815. 'id' => 1,
  1816. 'parent_id' => null
  1817. ),
  1818. ),
  1819. array(
  1820. 'ModelName' => array(
  1821. 'id' => 2,
  1822. 'parent_id' => 1
  1823. ),
  1824. ),
  1825. array(
  1826. 'ModelName' => array(
  1827. 'id' => 3,
  1828. 'parent_id' => 1
  1829. ),
  1830. ),
  1831. array(
  1832. 'ModelName' => array(
  1833. 'id' => 4,
  1834. 'parent_id' => 1
  1835. ),
  1836. ),
  1837. array(
  1838. 'ModelName' => array(
  1839. 'id' => 5,
  1840. 'parent_id' => 1
  1841. ),
  1842. ),
  1843. array(
  1844. 'ModelName' => array(
  1845. 'id' => 6,
  1846. 'parent_id' => null
  1847. ),
  1848. ),
  1849. array(
  1850. 'ModelName' => array(
  1851. 'id' => 7,
  1852. 'parent_id' => 6
  1853. ),
  1854. ),
  1855. array(
  1856. 'ModelName' => array(
  1857. 'id' => 8,
  1858. 'parent_id' => 6
  1859. ),
  1860. ),
  1861. array(
  1862. 'ModelName' => array(
  1863. 'id' => 9,
  1864. 'parent_id' => 6
  1865. ),
  1866. ),
  1867. array(
  1868. 'ModelName' => array(
  1869. 'id' => 10,
  1870. 'parent_id' => 6
  1871. )
  1872. )
  1873. );
  1874. $expected = array(
  1875. array(
  1876. 'ModelName' => array(
  1877. 'id' => 6,
  1878. 'parent_id' => null
  1879. ),
  1880. 'children' => array(
  1881. array(
  1882. 'ModelName' => array(
  1883. 'id' => 7,
  1884. 'parent_id' => 6
  1885. ),
  1886. 'children' => array()
  1887. ),
  1888. array(
  1889. 'ModelName' => array(
  1890. 'id' => 8,
  1891. 'parent_id' => 6
  1892. ),
  1893. 'children' => array()
  1894. ),
  1895. array(
  1896. 'ModelName' => array(
  1897. 'id' => 9,
  1898. 'parent_id' => 6
  1899. ),
  1900. 'children' => array()
  1901. ),
  1902. array(
  1903. 'ModelName' => array(
  1904. 'id' => 10,
  1905. 'parent_id' => 6
  1906. ),
  1907. 'children' => array()
  1908. )
  1909. )
  1910. )
  1911. );
  1912. $result = Hash::nest($input, array('root' => 6));
  1913. $this->assertEquals($expected, $result);
  1914. }
  1915. /**
  1916. * test Hash nest with a 1d array - this method should be able to handle any type of array input
  1917. *
  1918. * @return void
  1919. */
  1920. public function testNest1Dimensional() {
  1921. $input = array(
  1922. array(
  1923. 'id' => 1,
  1924. 'parent_id' => null
  1925. ),
  1926. array(
  1927. 'id' => 2,
  1928. 'parent_id' => 1
  1929. ),
  1930. array(
  1931. 'id' => 3,
  1932. 'parent_id' => 1
  1933. ),
  1934. array(
  1935. 'id' => 4,
  1936. 'parent_id' => 1
  1937. ),
  1938. array(
  1939. 'id' => 5,
  1940. 'parent_id' => 1
  1941. ),
  1942. array(
  1943. 'id' => 6,
  1944. 'parent_id' => null
  1945. ),
  1946. array(
  1947. 'id' => 7,
  1948. 'parent_id' => 6
  1949. ),
  1950. array(
  1951. 'id' => 8,
  1952. 'parent_id' => 6
  1953. ),
  1954. array(
  1955. 'id' => 9,
  1956. 'parent_id' => 6
  1957. ),
  1958. array(
  1959. 'id' => 10,
  1960. 'parent_id' => 6
  1961. )
  1962. );
  1963. $expected = array(
  1964. array(
  1965. 'id' => 1,
  1966. 'parent_id' => null,
  1967. 'children' => array(
  1968. array(
  1969. 'id' => 2,
  1970. 'parent_id' => 1,
  1971. 'children' => array()
  1972. ),
  1973. array(
  1974. 'id' => 3,
  1975. 'parent_id' => 1,
  1976. 'children' => array()
  1977. ),
  1978. array(
  1979. 'id' => 4,
  1980. 'parent_id' => 1,
  1981. 'children' => array()
  1982. ),
  1983. array(
  1984. 'id' => 5,
  1985. 'parent_id' => 1,
  1986. 'children' => array()
  1987. ),
  1988. )
  1989. ),
  1990. array(
  1991. 'id' => 6,
  1992. 'parent_id' => null,
  1993. 'children' => array(
  1994. array(
  1995. 'id' => 7,
  1996. 'parent_id' => 6,
  1997. 'children' => array()
  1998. ),
  1999. array(
  2000. 'id' => 8,
  2001. 'parent_id' => 6,
  2002. 'children' => array()
  2003. ),
  2004. array(
  2005. 'id' => 9,
  2006. 'parent_id' => 6,
  2007. 'children' => array()
  2008. ),
  2009. array(
  2010. 'id' => 10,
  2011. 'parent_id' => 6,
  2012. 'children' => array()
  2013. )
  2014. )
  2015. )
  2016. );
  2017. $result = Hash::nest($input, array('idPath' => '{n}.id', 'parentPath' => '{n}.parent_id'));
  2018. $this->assertEquals($expected, $result);
  2019. }
  2020. /**
  2021. * test Hash nest with no specified parent data.
  2022. *
  2023. * The result should be the same as the input.
  2024. * For an easier comparison, unset all the empty children arrays from the result
  2025. *
  2026. * @return void
  2027. */
  2028. public function testMissingParent() {
  2029. $input = array(
  2030. array(
  2031. 'id' => 1,
  2032. ),
  2033. array(
  2034. 'id' => 2,
  2035. ),
  2036. array(
  2037. 'id' => 3,
  2038. ),
  2039. array(
  2040. 'id' => 4,
  2041. ),
  2042. array(
  2043. 'id' => 5,
  2044. ),
  2045. array(
  2046. 'id' => 6,
  2047. ),
  2048. array(
  2049. 'id' => 7,
  2050. ),
  2051. array(
  2052. 'id' => 8,
  2053. ),
  2054. array(
  2055. 'id' => 9,
  2056. ),
  2057. array(
  2058. 'id' => 10,
  2059. )
  2060. );
  2061. $result = Hash::nest($input, array('idPath' => '{n}.id', 'parentPath' => '{n}.parent_id'));
  2062. foreach ($result as &$row) {
  2063. if (empty($row['children'])) {
  2064. unset($row['children']);
  2065. }
  2066. }
  2067. $this->assertEquals($input, $result);
  2068. }
  2069. /**
  2070. * Tests that nest() throws an InvalidArgumentException when providing an invalid input.
  2071. *
  2072. * @expectedException \InvalidArgumentException
  2073. * @return void
  2074. */
  2075. public function testNestInvalid() {
  2076. $input = array(
  2077. array(
  2078. 'ParentCategory' => array(
  2079. 'id' => '1',
  2080. 'name' => 'Lorem ipsum dolor sit amet',
  2081. 'parent_id' => '1'
  2082. )
  2083. )
  2084. );
  2085. Hash::nest($input);
  2086. }
  2087. /**
  2088. * testMergeDiff method
  2089. *
  2090. * @return void
  2091. */
  2092. public function testMergeDiff() {
  2093. $first = array(
  2094. 'ModelOne' => array(
  2095. 'id' => 1001,
  2096. 'field_one' => 'a1.m1.f1',
  2097. 'field_two' => 'a1.m1.f2'
  2098. )
  2099. );
  2100. $second = array(
  2101. 'ModelTwo' => array(
  2102. 'id' => 1002,
  2103. 'field_one' => 'a2.m2.f1',
  2104. 'field_two' => 'a2.m2.f2'
  2105. )
  2106. );
  2107. $result = Hash::mergeDiff($first, $second);
  2108. $this->assertEquals($result, $first + $second);
  2109. $result = Hash::mergeDiff($first, array());
  2110. $this->assertEquals($result, $first);
  2111. $result = Hash::mergeDiff(array(), $first);
  2112. $this->assertEquals($result, $first);
  2113. $third = array(
  2114. 'ModelOne' => array(
  2115. 'id' => 1003,
  2116. 'field_one' => 'a3.m1.f1',
  2117. 'field_two' => 'a3.m1.f2',
  2118. 'field_three' => 'a3.m1.f3'
  2119. )
  2120. );
  2121. $result = Hash::mergeDiff($first, $third);
  2122. $expected = array(
  2123. 'ModelOne' => array(
  2124. 'id' => 1001,
  2125. 'field_one' => 'a1.m1.f1',
  2126. 'field_two' => 'a1.m1.f2',
  2127. 'field_three' => 'a3.m1.f3'
  2128. )
  2129. );
  2130. $this->assertEquals($expected, $result);
  2131. $first = array(
  2132. 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2')),
  2133. 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2'))
  2134. );
  2135. $second = array(
  2136. 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's2.0.m1.f1', 'field_two' => 's2.0.m1.f2')),
  2137. 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's2.1.m2.f2', 'field_two' => 's2.1.m2.f2'))
  2138. );
  2139. $result = Hash::mergeDiff($first, $second);
  2140. $this->assertEquals($result, $first);
  2141. $third = array(
  2142. 0 => array(
  2143. 'ModelThree' => array(
  2144. 'id' => 1003,
  2145. 'field_one' => 's3.0.m3.f1',
  2146. 'field_two' => 's3.0.m3.f2'
  2147. )
  2148. )
  2149. );
  2150. $result = Hash::mergeDiff($first, $third);
  2151. $expected = array(
  2152. 0 => array(
  2153. 'ModelOne' => array(
  2154. 'id' => 1001,
  2155. 'field_one' => 's1.0.m1.f1',
  2156. 'field_two' => 's1.0.m1.f2'
  2157. ),
  2158. 'ModelThree' => array(
  2159. 'id' => 1003,
  2160. 'field_one' => 's3.0.m3.f1',
  2161. 'field_two' => 's3.0.m3.f2'
  2162. )
  2163. ),
  2164. 1 => array(
  2165. 'ModelTwo' => array(
  2166. 'id' => 1002,
  2167. 'field_one' => 's1.1.m2.f2',
  2168. 'field_two' => 's1.1.m2.f2'
  2169. )
  2170. )
  2171. );
  2172. $this->assertEquals($expected, $result);
  2173. $result = Hash::mergeDiff($first, array());
  2174. $this->assertEquals($result, $first);
  2175. $result = Hash::mergeDiff($first, $second);
  2176. $this->assertEquals($result, $first + $second);
  2177. }
  2178. /**
  2179. * Tests Hash::expand
  2180. *
  2181. * @return void
  2182. */
  2183. public function testExpand() {
  2184. $data = array('My', 'Array', 'To', 'Flatten');
  2185. $flat = Hash::flatten($data);
  2186. $result = Hash::expand($flat);
  2187. $this->assertEquals($data, $result);
  2188. $data = array(
  2189. '0.Post.id' => '1', '0.Post.author_id' => '1', '0.Post.title' => 'First Post', '0.Author.id' => '1',
  2190. '0.Author.user' => 'nate', '0.Author.password' => 'foo', '1.Post.id' => '2', '1.Post.author_id' => '3',
  2191. '1.Post.title' => 'Second Post', '1.Post.body' => 'Second Post Body', '1.Author.id' => '3',
  2192. '1.Author.user' => 'larry', '1.Author.password' => null
  2193. );
  2194. $result = Hash::expand($data);
  2195. $expected = array(
  2196. array(
  2197. 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post'),
  2198. 'Author' => array('id' => '1', 'user' => 'nate', 'password' => 'foo'),
  2199. ),
  2200. array(
  2201. 'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body'),
  2202. 'Author' => array('id' => '3', 'user' => 'larry', 'password' => null),
  2203. )
  2204. );
  2205. $this->assertEquals($expected, $result);
  2206. $data = array(
  2207. '0/Post/id' => 1,
  2208. '0/Post/name' => 'test post'
  2209. );
  2210. $result = Hash::expand($data, '/');
  2211. $expected = array(
  2212. array(
  2213. 'Post' => array(
  2214. 'id' => 1,
  2215. 'name' => 'test post'
  2216. )
  2217. )
  2218. );
  2219. $this->assertEquals($expected, $result);
  2220. $data = array('a.b.100.a' => null, 'a.b.200.a' => null);
  2221. $expected = array(
  2222. 'a' => array(
  2223. 'b' => array(
  2224. 100 => array('a' => null),
  2225. 200 => array('a' => null)
  2226. )
  2227. )
  2228. );
  2229. $result = Hash::expand($data);
  2230. $this->assertEquals($expected, $result);
  2231. }
  2232. /**
  2233. * Test that flattening a large complex set doesn't loop forever.
  2234. *
  2235. * @return void
  2236. */
  2237. public function testFlattenInfiniteLoop() {
  2238. $data = array(
  2239. 'Order.ASI' => '0',
  2240. 'Order.Accounting' => '0',
  2241. 'Order.Admin' => '0',
  2242. 'Order.Art' => '0',
  2243. 'Order.ArtChecker' => '0',
  2244. 'Order.Canned' => '0',
  2245. 'Order.Customer_Tags' => '',
  2246. 'Order.Embroidery' => '0',
  2247. 'Order.Item.0.Product.style_number' => 'a11222',
  2248. 'Order.Item.0.Product.slug' => 'a11222',
  2249. 'Order.Item.0.Product._id' => '4ff8b8d3d7bbe8ad30000000',
  2250. 'Order.Item.0.Product.Color.slug' => 'kelly_green',
  2251. 'Order.Item.0.Product.ColorSizes.0.Color.color' => 'Sport Grey',
  2252. 'Order.Item.0.Product.ColorSizes.0.Color.slug' => 'sport_grey',
  2253. 'Order.Item.0.Product.ColorSizes.1.Color.color' => 'Kelly Green',
  2254. 'Order.Item.0.Product.ColorSizes.1.Color.slug' => 'kelly_green',
  2255. 'Order.Item.0.Product.ColorSizes.2.Color.color' => 'Orange',
  2256. 'Order.Item.0.Product.ColorSizes.2.Color.slug' => 'orange',
  2257. 'Order.Item.0.Product.ColorSizes.3.Color.color' => 'Yellow Haze',
  2258. 'Order.Item.0.Product.ColorSizes.3.Color.slug' => 'yellow_haze',
  2259. 'Order.Item.0.Product.brand' => 'OUTER BANKS',
  2260. 'Order.Item.0.Product.style' => 'T-shirt',
  2261. 'Order.Item.0.Product.description' => 'uhiuhuih oin ooi ioo ioio',
  2262. 'Order.Item.0.Product.sizes.0.Size.qty' => '',
  2263. 'Order.Item.0.Product.sizes.0.Size.size' => '0-3mo',
  2264. 'Order.Item.0.Product.sizes.0.Size.id' => '38',
  2265. 'Order.Item.0.Product.sizes.1.Size.qty' => '',
  2266. 'Order.Item.0.Product.sizes.1.Size.size' => '3-6mo',
  2267. 'Order.Item.0.Product.sizes.1.Size.id' => '39',
  2268. 'Order.Item.0.Product.sizes.2.Size.qty' => '78',
  2269. 'Order.Item.0.Product.sizes.2.Size.size' => '6-9mo',
  2270. 'Order.Item.0.Product.sizes.2.Size.id' => '40',
  2271. 'Order.Item.0.Product.sizes.3.Size.qty' => '',
  2272. 'Order.Item.0.Product.sizes.3.Size.size' => '6-12mo',
  2273. 'Order.Item.0.Product.sizes.3.Size.id' => '41',
  2274. 'Order.Item.0.Product.sizes.4.Size.qty' => '',
  2275. 'Order.Item.0.Product.sizes.4.Size.size' => '12-18mo',
  2276. 'Order.Item.0.Product.sizes.4.Size.id' => '42',
  2277. 'Order.Item.0.Art.imprint_locations.0.id' => 2,
  2278. 'Order.Item.0.Art.imprint_locations.0.name' => 'Left Chest',
  2279. 'Order.Item.0.Art.imprint_locations.0.imprint_type.id' => 7,
  2280. 'Order.Item.0.Art.imprint_locations.0.imprint_type.type' => 'Embroidery',
  2281. 'Order.Item.0.Art.imprint_locations.0.art' => '',
  2282. 'Order.Item.0.Art.imprint_locations.0.num_colors' => 3,
  2283. 'Order.Item.0.Art.imprint_locations.0.description' => 'Wooo! This is Embroidery!!',
  2284. 'Order.Item.0.Art.imprint_locations.0.lines.0' => 'Platen',
  2285. 'Order.Item.0.Art.imprint_locations.0.lines.1' => 'Logo',
  2286. 'Order.Item.0.Art.imprint_locations.0.height' => 4,
  2287. 'Order.Item.0.Art.imprint_locations.0.width' => 5,
  2288. 'Order.Item.0.Art.imprint_locations.0.stitch_density' => 'Light',
  2289. 'Order.Item.0.Art.imprint_locations.0.metallic_thread' => true,
  2290. 'Order.Item.0.Art.imprint_locations.1.id' => 4,
  2291. 'Order.Item.0.Art.imprint_locations.1.name' => 'Full Back',
  2292. 'Order.Item.0.Art.imprint_locations.1.imprint_type.id' => 6,
  2293. 'Order.Item.0.Art.imprint_locations.1.imprint_type.type' => 'Screenprinting',
  2294. 'Order.Item.0.Art.imprint_locations.1.art' => '',
  2295. 'Order.Item.0.Art.imprint_locations.1.num_colors' => 3,
  2296. 'Order.Item.0.Art.imprint_locations.1.description' => 'Wooo! This is Screenprinting!!',
  2297. 'Order.Item.0.Art.imprint_locations.1.lines.0' => 'Platen',
  2298. 'Order.Item.0.Art.imprint_locations.1.lines.1' => 'Logo',
  2299. 'Order.Item.0.Art.imprint_locations.2.id' => 26,
  2300. 'Order.Item.0.Art.imprint_locations.2.name' => 'HS - JSY Name Below',
  2301. 'Order.Item.0.Art.imprint_locations.2.imprint_type.id' => 9,
  2302. 'Order.Item.0.Art.imprint_locations.2.imprint_type.type' => 'Names',
  2303. 'Order.Item.0.Art.imprint_locations.2.description' => 'Wooo! This is Names!!',
  2304. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.active' => 1,
  2305. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.name' => 'Benjamin Talavera',
  2306. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.color' => 'Red',
  2307. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.height' => '3',
  2308. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.layout' => 'Arched',
  2309. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.style' => 'Classic',
  2310. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.active' => 0,
  2311. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.name' => 'Rishi Narayan',
  2312. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.color' => 'Cardinal',
  2313. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.height' => '4',
  2314. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.layout' => 'Straight',
  2315. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.style' => 'Team US',
  2316. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.active' => 1,
  2317. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.name' => 'Brandon Plasters',
  2318. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.color' => 'Red',
  2319. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.height' => '3',
  2320. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.layout' => 'Arched',
  2321. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.style' => 'Classic',
  2322. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.active' => 0,
  2323. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.name' => 'Andrew Reed',
  2324. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.color' => 'Cardinal',
  2325. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.height' => '4',
  2326. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.layout' => 'Straight',
  2327. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.style' => 'Team US',
  2328. 'Order.Job.0._id' => 'job-1',
  2329. 'Order.Job.0.type' => 'screenprinting',
  2330. 'Order.Job.0.postPress' => 'job-2',
  2331. 'Order.Job.1._id' => 'job-2',
  2332. 'Order.Job.1.type' => 'embroidery',
  2333. 'Order.Postpress' => '0',
  2334. 'Order.PriceAdjustment.0._id' => 'price-adjustment-1',
  2335. 'Order.PriceAdjustment.0.adjustment' => '-20',
  2336. 'Order.PriceAdjustment.0.adjustment_type' => 'percent',
  2337. 'Order.PriceAdjustment.0.type' => 'grand_total',
  2338. 'Order.PriceAdjustment.1.adjustment' => '20',
  2339. 'Order.PriceAdjustment.1.adjustment_type' => 'flat',
  2340. 'Order.PriceAdjustment.1.min-items' => '10',
  2341. 'Order.PriceAdjustment.1.type' => 'min-items',
  2342. 'Order.PriceAdjustment.1._id' => 'another-test-adjustment',
  2343. 'Order.Purchasing' => '0',
  2344. 'Order.QualityControl' => '0',
  2345. 'Order.Receiving' => '0',
  2346. 'Order.ScreenPrinting' => '0',
  2347. 'Order.Stage.art_approval' => 0,
  2348. 'Order.Stage.draft' => 1,
  2349. 'Order.Stage.quote' => 1,
  2350. 'Order.Stage.order' => 1,
  2351. 'Order.StoreLiason' => '0',
  2352. 'Order.Tag_UI_Email' => '',
  2353. 'Order.Tags' => '',
  2354. 'Order._id' => 'test-2',
  2355. 'Order.add_print_location' => '',
  2356. 'Order.created' => '2011-Dec-29 05:40:18',
  2357. 'Order.force_admin' => '0',
  2358. 'Order.modified' => '2012-Jul-25 01:24:49',
  2359. 'Order.name' => 'towering power',
  2360. 'Order.order_id' => '135961',
  2361. 'Order.slug' => 'test-2',
  2362. 'Order.title' => 'test job 2',
  2363. 'Order.type' => 'ttt'
  2364. );
  2365. $expanded = Hash::expand($data);
  2366. $flattened = Hash::flatten($expanded);
  2367. $this->assertEquals($data, $flattened);
  2368. }
  2369. }