RevisionBehaviorTest.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724
  1. <?php
  2. App::uses('RevisionBehavior', 'Tools.Model/Behavior');
  3. App::uses('Controller', 'Controller');
  4. App::uses('CakeRequest', 'Network');
  5. App::uses('CakeResponse', 'Network');
  6. class RevisionBehaviorTest extends CakeTestCase {
  7. public $RevisionBehavior;
  8. public $autoFixtures = false;
  9. public $fixtures = [
  10. 'plugin.tools.revision_article',
  11. 'plugin.tools.revision_articles_rev',
  12. 'plugin.tools.revision_post',
  13. 'plugin.tools.revision_posts_rev',
  14. 'plugin.tools.revision_user',
  15. 'plugin.tools.revision_comment',
  16. 'plugin.tools.revision_comments_rev',
  17. 'plugin.tools.revision_vote',
  18. 'plugin.tools.revision_votes_rev',
  19. 'plugin.tools.revision_comments_revision_tag',
  20. 'plugin.tools.revision_comments_revision_tags_rev',
  21. 'plugin.tools.revision_tag',
  22. 'plugin.tools.revision_tags_rev'];
  23. public function setUp() {
  24. parent::setUp();
  25. $this->skipIf(true, 'Deprecated');
  26. $this->RevisionBehavior = new RevisionBehavior();
  27. }
  28. public function testObject() {
  29. $this->assertTrue(is_object($this->RevisionBehavior));
  30. $this->assertInstanceOf('RevisionBehavior', $this->RevisionBehavior);
  31. }
  32. public function tearDown($method = null) {
  33. unset($this->RevisionBehavior);
  34. parent::tearDown($method);
  35. }
  36. /**
  37. * RevisionBehaviorTest::testSavePost()
  38. *
  39. * @return void
  40. */
  41. public function testSavePost() {
  42. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  43. $Post = new RevisionPost();
  44. $data = ['Post' => ['title' => 'New Post', 'content' => 'First post!']];
  45. $Post->save($data);
  46. $Post->id = 4;
  47. $result = $Post->newest(['fields' => [
  48. 'id',
  49. 'title',
  50. 'content',
  51. 'version_id']]);
  52. $expected = ['Post' => [
  53. 'id' => 4,
  54. 'title' => 'New Post',
  55. 'content' => 'First post!',
  56. 'version_id' => 4]];
  57. $this->assertEquals($expected, $result);
  58. }
  59. /**
  60. * RevisionBehaviorTest::testSaveWithoutChange()
  61. *
  62. * @return void
  63. */
  64. public function testSaveWithoutChange() {
  65. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  66. $Post = new RevisionPost();
  67. $Post->id = 1;
  68. $this->assertTrue((bool)$Post->createRevision());
  69. $Post->id = 1;
  70. $count = $Post->ShadowModel->find('count', ['conditions' => ['id' => 1]]);
  71. $this->assertEquals($count, 2);
  72. $Post->id = 1;
  73. $data = $Post->read();
  74. $Post->save($data);
  75. $Post->id = 1;
  76. $count = $Post->ShadowModel->find('count', ['conditions' => ['id' => 1]]);
  77. $this->assertEquals($count, 2);
  78. }
  79. /**
  80. * RevisionBehaviorTest::testEditPost()
  81. *
  82. * @return void
  83. */
  84. public function testEditPost() {
  85. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  86. $Post = new RevisionPost();
  87. $data = ['Post' => ['title' => 'New Post']];
  88. $Post->create();
  89. $Post->save($data);
  90. $Post->create();
  91. $data = ['Post' => ['id' => 1, 'title' => 'Edited Post']];
  92. $Post->save($data);
  93. $Post->id = 1;
  94. $result = $Post->newest(['fields' => [
  95. 'id',
  96. 'title',
  97. 'content',
  98. 'version_id']]);
  99. $expected = ['Post' => [
  100. 'id' => 1,
  101. 'title' => 'Edited Post',
  102. 'content' => 'Lorem ipsum dolor sit amet, aliquet feugiat.',
  103. 'version_id' => 5]];
  104. $this->assertEquals($expected, $result);
  105. }
  106. /**
  107. * RevisionBehaviorTest::testShadow()
  108. *
  109. * @return void
  110. */
  111. public function testShadow() {
  112. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  113. $Post = new RevisionPost();
  114. $Post->create(['Post' => ['title' => 'Non Used Post', 'content' => 'Whatever']]);
  115. $Post->save();
  116. $postId = $Post->id;
  117. $Post->create(['Post' => ['title' => 'New Post 1', 'content' => 'nada']]);
  118. $Post->save();
  119. $Post->save(['Post' => ['id' => 5, 'title' => 'Edit Post 2']]);
  120. $Post->save(['Post' => ['id' => 5, 'title' => 'Edit Post 3']]);
  121. $result = $Post->ShadowModel->find('first', ['fields' => [
  122. 'version_id',
  123. 'id',
  124. 'title',
  125. 'content']]);
  126. $expected = ['Post' => [
  127. 'version_id' => 7,
  128. 'id' => 5,
  129. 'title' => 'Edit Post 3',
  130. 'content' => 'nada']];
  131. $this->assertEquals($expected, $result);
  132. $Post->id = $postId;
  133. $result = $Post->newest();
  134. $this->assertEquals($result['Post']['title'], 'Non Used Post');
  135. $this->assertEquals($result['Post']['version_id'], 4);
  136. $result = $Post->ShadowModel->find('first', ['conditions' => ['version_id' => 4], 'fields' => [
  137. 'version_id',
  138. 'id',
  139. 'title',
  140. 'content']]);
  141. $expected = ['Post' => [
  142. 'version_id' => 4,
  143. 'id' => 4,
  144. 'title' => 'Non Used Post',
  145. 'content' => 'Whatever']];
  146. $this->assertEquals($expected, $result);
  147. }
  148. /**
  149. * RevisionBehaviorTest::testCurrentPost()
  150. *
  151. * @return void
  152. */
  153. public function testCurrentPost() {
  154. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  155. $Post = new RevisionPost();
  156. $Post->create();
  157. $data = ['Post' => ['id' => 1, 'title' => 'Edited Post']];
  158. $Post->save($data);
  159. $Post->create();
  160. $data = ['Post' => ['id' => 1, 'title' => 'Re-edited Post']];
  161. $Post->save($data);
  162. $Post->id = 1;
  163. $result = $Post->newest(['fields' => [
  164. 'id',
  165. 'title',
  166. 'content',
  167. 'version_id']]);
  168. $expected = ['Post' => [
  169. 'id' => 1,
  170. 'title' => 'Re-edited Post',
  171. 'content' => 'Lorem ipsum dolor sit amet, aliquet feugiat.',
  172. 'version_id' => 5]];
  173. $this->assertEquals($expected, $result);
  174. }
  175. /**
  176. * RevisionBehaviorTest::testRevisionsPost()
  177. *
  178. * @return void
  179. */
  180. public function testRevisionsPost() {
  181. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  182. $Post = new RevisionPost();
  183. $Post->create();
  184. $data = ['Post' => ['id' => 1, 'title' => 'Edited Post']];
  185. $Post->save($data);
  186. $Post->create();
  187. $data = ['Post' => ['id' => 1, 'title' => 'Re-edited Post']];
  188. $Post->save($data);
  189. $Post->create();
  190. $data = ['Post' => ['id' => 1, 'title' => 'Newest edited Post']];
  191. $Post->save($data);
  192. $Post->id = 1;
  193. $result = $Post->revisions(['fields' => [
  194. 'id',
  195. 'title',
  196. 'content',
  197. 'version_id']]);
  198. $expected = [
  199. 0 => ['Post' => [
  200. 'id' => 1,
  201. 'title' => 'Re-edited Post',
  202. 'content' => 'Lorem ipsum dolor sit amet, aliquet feugiat.',
  203. 'version_id' => 5]],
  204. 1 => ['Post' => [
  205. 'id' => 1,
  206. 'title' => 'Edited Post',
  207. 'content' => 'Lorem ipsum dolor sit amet, aliquet feugiat.',
  208. 'version_id' => 4], ],
  209. 2 => ['Post' => [
  210. 'id' => 1,
  211. 'title' => 'Lorem ipsum dolor sit amet',
  212. 'content' => 'Lorem ipsum dolor sit amet, aliquet feugiat.',
  213. 'version_id' => 1], ]];
  214. $this->assertEquals($expected, $result);
  215. $Post->id = 1;
  216. $result = $Post->revisions(['fields' => [
  217. 'id',
  218. 'title',
  219. 'content',
  220. 'version_id']], true);
  221. $expected = [
  222. 0 => ['Post' => [
  223. 'id' => 1,
  224. 'title' => 'Newest edited Post',
  225. 'content' => 'Lorem ipsum dolor sit amet, aliquet feugiat.',
  226. 'version_id' => 6]],
  227. 1 => ['Post' => [
  228. 'id' => 1,
  229. 'title' => 'Re-edited Post',
  230. 'content' => 'Lorem ipsum dolor sit amet, aliquet feugiat.',
  231. 'version_id' => 5]],
  232. 2 => ['Post' => [
  233. 'id' => 1,
  234. 'title' => 'Edited Post',
  235. 'content' => 'Lorem ipsum dolor sit amet, aliquet feugiat.',
  236. 'version_id' => 4], ],
  237. 3 => ['Post' => [
  238. 'id' => 1,
  239. 'title' => 'Lorem ipsum dolor sit amet',
  240. 'content' => 'Lorem ipsum dolor sit amet, aliquet feugiat.',
  241. 'version_id' => 1], ]];
  242. $this->assertEquals($expected, $result);
  243. }
  244. /**
  245. * RevisionBehaviorTest::testDiff()
  246. *
  247. * @return void
  248. */
  249. public function testDiff() {
  250. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  251. $Post = new RevisionPost();
  252. $data = ['Post' => ['id' => 1, 'title' => 'Edited Post 1']];
  253. $Post->save($data);
  254. $data = ['Post' => ['id' => 1, 'title' => 'Edited Post 2']];
  255. $Post->save($data);
  256. $data = ['Post' => ['id' => 1, 'title' => 'Edited Post 3']];
  257. $Post->save($data);
  258. $Post->id = 1;
  259. $result = $Post->diff(null, null, ['fields' => [
  260. 'version_id',
  261. 'id',
  262. 'title',
  263. 'content']]);
  264. $expected = ['Post' => [
  265. 'version_id' => [
  266. 6,
  267. 5,
  268. 4,
  269. 1],
  270. 'id' => 1,
  271. 'title' => [
  272. 'Edited Post 3',
  273. 'Edited Post 2',
  274. 'Edited Post 1',
  275. 'Lorem ipsum dolor sit amet'],
  276. 'content' => 'Lorem ipsum dolor sit amet, aliquet feugiat.']];
  277. $this->assertEquals($expected, $result);
  278. }
  279. /**
  280. * RevisionBehaviorTest::testDiffMultipleFields()
  281. *
  282. * @return void
  283. */
  284. public function testDiffMultipleFields() {
  285. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  286. $Post = new RevisionPost();
  287. $data = ['Post' => ['id' => 1, 'title' => 'Edited title 1']];
  288. $Post->save($data);
  289. $data = ['Post' => ['id' => 1, 'content' => 'Edited content']];
  290. $Post->save($data);
  291. $data = ['Post' => ['id' => 1, 'title' => 'Edited title 2']];
  292. $Post->save($data);
  293. $Post->id = 1;
  294. $result = $Post->diff(null, null, ['fields' => [
  295. 'version_id',
  296. 'id',
  297. 'title',
  298. 'content']]);
  299. $expected = ['Post' => [
  300. 'version_id' => [
  301. 6,
  302. 5,
  303. 4,
  304. 1],
  305. 'id' => 1,
  306. 'title' => [
  307. 0 => 'Edited title 2',
  308. 2 => 'Edited title 1',
  309. 3 => 'Lorem ipsum dolor sit amet'],
  310. 'content' => [1 => 'Edited content', 3 => 'Lorem ipsum dolor sit amet, aliquet feugiat.']]];
  311. $this->assertEquals($expected, $result);
  312. }
  313. /**
  314. * RevisionBehaviorTest::testPrevious()
  315. *
  316. * @return void
  317. */
  318. public function testPrevious() {
  319. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  320. $Post = new RevisionPost();
  321. $Post->id = 1;
  322. $this->assertSame([], $Post->previous());
  323. $data = ['Post' => ['id' => 1, 'title' => 'Edited Post 2']];
  324. $Post->save($data);
  325. $data = ['Post' => ['id' => 1, 'title' => 'Edited Post 3']];
  326. $Post->save($data);
  327. $Post->id = 1;
  328. $result = $Post->previous(['fields' => [
  329. 'version_id',
  330. 'id',
  331. 'title']]);
  332. $expected = ['Post' => [
  333. 'version_id' => 4,
  334. 'id' => 1,
  335. 'title' => 'Edited Post 2']];
  336. $this->assertEquals($expected, $result);
  337. }
  338. /**
  339. * RevisionBehaviorTest::testUndoEdit()
  340. *
  341. * @return void
  342. */
  343. public function testUndoEdit() {
  344. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  345. $Post = new RevisionPost();
  346. $data = ['Post' => ['id' => 1, 'title' => 'Edited Post 1']];
  347. $Post->save($data);
  348. $data = ['Post' => ['id' => 1, 'title' => 'Edited Post 2']];
  349. $Post->save($data);
  350. $data = ['Post' => ['id' => 1, 'title' => 'Edited Post 3']];
  351. $Post->save($data);
  352. $Post->id = 1;
  353. $success = $Post->undo();
  354. $this->assertTrue((bool)$success);
  355. $result = $Post->find('first', ['fields' => [
  356. 'id',
  357. 'title',
  358. 'content']]);
  359. $expected = ['Post' => [
  360. 'id' => 1,
  361. 'title' => 'Edited Post 2',
  362. 'content' => 'Lorem ipsum dolor sit amet, aliquet feugiat.']];
  363. $this->assertEquals($expected, $result);
  364. }
  365. /**
  366. * RevisionBehaviorTest::testUndoCreate()
  367. *
  368. * @return void
  369. */
  370. public function testUndoCreate() {
  371. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  372. $Post = new RevisionPost();
  373. $Post->create(['Post' => ['title' => 'New post', 'content' => 'asd']]);
  374. $Post->save();
  375. $result = $Post->read();
  376. $this->assertEquals($result['Post']['title'], 'New post');
  377. $id = $Post->id;
  378. $Post->undo();
  379. $Post->id = $id;
  380. $this->assertEmpty($Post->read());
  381. $Post->undelete();
  382. $result = $Post->read();
  383. $this->assertEquals($result['Post']['title'], 'New post');
  384. }
  385. /**
  386. * RevisionBehaviorTest::testRevertTo()
  387. *
  388. * @return void
  389. */
  390. public function testRevertTo() {
  391. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  392. $Post = new RevisionPost();
  393. $Post->save(['Post' => ['id' => 1, 'title' => 'Edited Post 1']]);
  394. $Post->save(['Post' => ['id' => 1, 'title' => 'Edited Post 2']]);
  395. $Post->save(['Post' => ['id' => 1, 'title' => 'Edited Post 3']]);
  396. $Post->id = 1;
  397. $result = $Post->previous();
  398. $this->assertEquals($result['Post']['title'], 'Edited Post 2');
  399. $versionId = $result['Post']['version_id'];
  400. $result = $Post->revertTo($versionId);
  401. $this->assertTrue((bool)$result);
  402. $result = $Post->find('first', ['fields' => [
  403. 'id',
  404. 'title',
  405. 'content']]);
  406. $this->assertEquals($result['Post']['title'], 'Edited Post 2');
  407. }
  408. /**
  409. * RevisionBehaviorTest::testLimit()
  410. *
  411. * @return void
  412. */
  413. public function testLimit() {
  414. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  415. $Post = new RevisionPost();
  416. $data = ['Post' => ['id' => 2, 'title' => 'Edited Post 1']];
  417. $Post->save($data);
  418. $data = ['Post' => ['id' => 2, 'title' => 'Edited Post 2']];
  419. $Post->save($data);
  420. $data = ['Post' => ['id' => 2, 'title' => 'Edited Post 3']];
  421. $Post->save($data);
  422. $data = ['Post' => ['id' => 2, 'title' => 'Edited Post 4']];
  423. $Post->save($data);
  424. $data = ['Post' => ['id' => 2, 'title' => 'Edited Post 5']];
  425. $Post->save($data);
  426. $data = ['Post' => ['id' => 2, 'title' => 'Edited Post 6']];
  427. $Post->save($data);
  428. $data = ['Post' => ['id' => 2, 'title' => 'Edited Post 6']];
  429. $Post->save($data);
  430. $Post->id = 2;
  431. $result = $Post->revisions(['fields' => [
  432. 'id',
  433. 'title',
  434. 'content',
  435. 'version_id']], true);
  436. $expected = [
  437. 0 => ['Post' => [
  438. 'id' => 2,
  439. 'title' => 'Edited Post 6',
  440. 'content' => 'Lorem ipsum dolor sit.',
  441. 'version_id' => 9]],
  442. 1 => ['Post' => [
  443. 'id' => 2,
  444. 'title' => 'Edited Post 5',
  445. 'content' => 'Lorem ipsum dolor sit.',
  446. 'version_id' => 8], ],
  447. 2 => ['Post' => [
  448. 'id' => 2,
  449. 'title' => 'Edited Post 4',
  450. 'content' => 'Lorem ipsum dolor sit.',
  451. 'version_id' => 7]],
  452. 3 => ['Post' => [
  453. 'id' => 2,
  454. 'title' => 'Edited Post 3',
  455. 'content' => 'Lorem ipsum dolor sit.',
  456. 'version_id' => 6], ],
  457. 4 => ['Post' => [
  458. 'id' => 2,
  459. 'title' => 'Edited Post 2',
  460. 'content' => 'Lorem ipsum dolor sit.',
  461. 'version_id' => 5]]];
  462. $this->assertEquals($expected, $result);
  463. }
  464. /**
  465. * RevisionBehaviorTest::testTree()
  466. *
  467. * @return void
  468. */
  469. public function testTree() {
  470. $this->loadFixtures('RevisionArticle', 'RevisionArticlesRev');
  471. $Article = new RevisionArticle();
  472. $Article->initializeRevisions();
  473. $Article->save(['Article' => ['id' => 3, 'content' => 'Re-edited Article']]);
  474. $this->assertNoErrors('Save() with tree problem : %s');
  475. $Article->moveUp(3);
  476. $this->assertNoErrors('moveUp() with tree problem : %s');
  477. $Article->id = 3;
  478. $result = $Article->newest(['fields' => ['id', 'version_id']]);
  479. $this->assertEquals($result['Article']['version_id'], 4);
  480. $Article->create([
  481. 'title' => 'midten',
  482. 'content' => 'stuff',
  483. 'parent_id' => 2]);
  484. $Article->save();
  485. $this->assertNoErrors('Save() with tree problem : %s');
  486. $result = $Article->find('all', ['fields' => [
  487. 'id',
  488. 'lft',
  489. 'rght',
  490. 'parent_id']]);
  491. $expected = [
  492. 'id' => 1,
  493. 'lft' => 1,
  494. 'rght' => 8,
  495. 'parent_id' => null];
  496. $this->assertEquals($result[0]['Article'], $expected);
  497. $expected = [
  498. 'id' => 2,
  499. 'lft' => 4,
  500. 'rght' => 7,
  501. 'parent_id' => 1];
  502. $this->assertEquals($result[1]['Article'], $expected);
  503. $expected = [
  504. 'id' => 3,
  505. 'lft' => 2,
  506. 'rght' => 3,
  507. 'parent_id' => 1];
  508. $this->assertEquals($result[2]['Article'], $expected);
  509. $expected = [
  510. 'id' => 4,
  511. 'lft' => 5,
  512. 'rght' => 6,
  513. 'parent_id' => 2];
  514. $this->assertEquals($result[3]['Article'], $expected);
  515. }
  516. /**
  517. * RevisionBehaviorTest::testIgnore()
  518. *
  519. * @return void
  520. */
  521. public function testIgnore() {
  522. $this->loadFixtures('RevisionArticle', 'RevisionArticlesRev');
  523. $Article = new RevisionArticle();
  524. $data = ['Article' => [
  525. 'id' => 3,
  526. 'title' => 'New title',
  527. 'content' => 'Edited']];
  528. $Article->save($data);
  529. $data = ['Article' => ['id' => 3, 'title' => 'Re-edited title']];
  530. $Article->save($data);
  531. $Article->id = 3;
  532. $result = $Article->newest(['fields' => [
  533. 'id',
  534. 'title',
  535. 'content',
  536. 'version_id']]);
  537. $expected = ['Article' => [
  538. 'id' => 3,
  539. 'title' => 'New title',
  540. 'content' => 'Edited',
  541. 'version_id' => 1]];
  542. $this->assertEquals($expected, $result);
  543. }
  544. /**
  545. * RevisionBehaviorTest::testWithoutShadowTable()
  546. *
  547. * @return void
  548. */
  549. public function testWithoutShadowTable() {
  550. $this->loadFixtures('RevisionUser');
  551. $User = new RevisionUser();
  552. $data = ['User' => ['id' => 1, 'name' => 'New name']];
  553. $success = $User->save($data);
  554. $this->assertNoErrors();
  555. $this->assertTrue((bool)$success);
  556. }
  557. /**
  558. * RevisionBehaviorTest::testRevertToDate()
  559. *
  560. * @return void
  561. */
  562. public function testRevertToDate() {
  563. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  564. $Post = new RevisionPost();
  565. $data = ['Post' => ['id' => 3, 'title' => 'Edited Post 6']];
  566. $Post->save($data);
  567. $result = $Post->revertToDate(date('Y-m-d H:i:s', strtotime('yesterday')));
  568. $this->assertTrue((bool)$result);
  569. $result = $Post->newest(['fields' => [
  570. 'id',
  571. 'title',
  572. 'content',
  573. 'version_id']]);
  574. $expected = ['Post' => [
  575. 'id' => 3,
  576. 'title' => 'Post 3',
  577. 'content' => 'Lorem ipsum dolor sit.',
  578. 'version_id' => 5]];
  579. $this->assertEquals($expected, $result);
  580. }
  581. /**
  582. * RevisionBehaviorTest::testCascade()
  583. *
  584. * @return void
  585. */
  586. public function testCascade() {
  587. $this->loadFixtures('RevisionComment', 'RevisionCommentsRev', 'RevisionVote', 'RevisionVotesRev');
  588. $Comment = new RevisionComment();
  589. $originalComments = $Comment->find('all');
  590. $data = ['Vote' => [
  591. 'id' => 3,
  592. 'title' => 'Edited Vote',
  593. 'revision_comment_id' => 1]];
  594. $Comment->Vote->save($data);
  595. $this->assertTrue((bool)$Comment->Vote->revertToDate('2008-12-09'));
  596. $Comment->Vote->id = 3;
  597. $result = $Comment->Vote->newest(['fields' => [
  598. 'id',
  599. 'title',
  600. 'content',
  601. 'version_id']]);
  602. $expected = ['Vote' => [
  603. 'id' => 3,
  604. 'title' => 'Stuff',
  605. 'content' => 'Lorem ipsum dolor sit.',
  606. 'version_id' => 5]];
  607. $this->assertEquals($expected, $result);
  608. $data = ['Comment' => ['id' => 2, 'title' => 'Edited Comment']];
  609. $Comment->save($data);
  610. $this->assertTrue((bool)$Comment->revertToDate('2008-12-09'));
  611. $revertedComments = $Comment->find('all');
  612. $this->assertEquals($originalComments, $revertedComments);
  613. }
  614. /**
  615. * RevisionBehaviorTest::testCreateRevision()
  616. *
  617. * @return void
  618. */
  619. public function testCreateRevision() {
  620. $this->loadFixtures('RevisionArticle', 'RevisionArticlesRev');
  621. $Article = new RevisionArticle();
  622. $data = ['Article' => [
  623. 'id' => 3,
  624. 'title' => 'New title',
  625. 'content' => 'Edited']];
  626. $Article->save($data);
  627. $data = ['Article' => ['id' => 3, 'title' => 'Re-edited title']];
  628. $Article->save($data);
  629. $Article->id = 3;
  630. $result = $Article->newest(['fields' => [
  631. 'id',
  632. 'title',
  633. 'content',
  634. 'version_id']]);
  635. $expected = ['Article' => [
  636. 'id' => 3,
  637. 'title' => 'New title',
  638. 'content' => 'Edited',
  639. 'version_id' => 1]];
  640. $this->assertEquals($expected, $result);
  641. $Article->id = 3;
  642. $this->assertTrue((bool)$Article->createRevision());
  643. $result = $Article->newest(['fields' => [
  644. 'id',
  645. 'title',
  646. 'content',
  647. 'version_id']]);
  648. $expected = ['Article' => [
  649. 'id' => 3,
  650. 'title' => 'Re-edited title',
  651. 'content' => 'Edited',
  652. 'version_id' => 2]];
  653. $this->assertEquals($expected, $result);
  654. }
  655. /**
  656. * RevisionBehaviorTest::testUndelete()
  657. *
  658. * @return void
  659. */
  660. public function testUndelete() {
  661. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  662. $Post = new RevisionPost();
  663. $Post->id = 3;
  664. $result = $Post->undelete();
  665. $this->assertFalse($result);
  666. $Post->delete(3);
  667. $result = $Post->find('count', ['conditions' => ['id' => 3]]);
  668. $this->assertEquals($result, 0);
  669. $Post->id = 3;
  670. $Post->undelete();
  671. $result = $Post->find('first', ['conditions' => ['id' => 3], 'fields' => [
  672. 'id',
  673. 'title',
  674. 'content']]);
  675. $expected = ['Post' => [
  676. 'id' => 3,
  677. 'title' => 'Post 3',
  678. 'content' => 'Lorem ipsum dolor sit.']];
  679. $this->assertEquals($expected, $result);
  680. }
  681. /**
  682. * RevisionBehaviorTest::testUndeleteCallbacks()
  683. *
  684. * @return void
  685. */
  686. public function testUndeleteCallbacks() {
  687. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  688. $Post = new RevisionPost();
  689. $Post->id = 3;
  690. $result = $Post->undelete();
  691. $this->assertFalse($result);
  692. $Post->delete(3);
  693. $result = $Post->find('first', ['conditions' => ['id' => 3]]);
  694. $this->assertEmpty($result);
  695. $Post->id = 3;
  696. $this->assertTrue($Post->undelete());
  697. $this->assertTrue($Post->beforeUndelete);
  698. $this->assertTrue($Post->afterUndelete);
  699. $result = $Post->find('first', ['conditions' => ['id' => 3]]);
  700. $expected = ['Post' => [
  701. 'id' => 3,
  702. 'title' => 'Post 3',
  703. 'content' => 'Lorem ipsum dolor sit.',
  704. ]];
  705. $this->assertEquals($expected, $result);
  706. $this->assertNoErrors();
  707. }
  708. /**
  709. * RevisionBehaviorTest::testUndeleteTree1()
  710. *
  711. * @return void
  712. */
  713. public function testUndeleteTree1() {
  714. $this->loadFixtures('RevisionArticle', 'RevisionArticlesRev');
  715. $Article = new RevisionArticle();
  716. $Article->initializeRevisions();
  717. $Article->delete(3);
  718. $Article->id = 3;
  719. $Article->undelete();
  720. $result = $Article->find('all');
  721. $this->assertEquals(sizeof($result), 3);
  722. $this->assertEquals($result[0]['Article']['lft'], 1);
  723. $this->assertEquals($result[0]['Article']['rght'], 6);
  724. $this->assertEquals($result[1]['Article']['lft'], 2);
  725. $this->assertEquals($result[1]['Article']['rght'], 3);
  726. $this->assertEquals($result[2]['Article']['id'], 3);
  727. $this->assertEquals($result[2]['Article']['lft'], 4);
  728. $this->assertEquals($result[2]['Article']['rght'], 5);
  729. }
  730. /**
  731. * RevisionBehaviorTest::testUndeleteTree2()
  732. *
  733. * @return void
  734. */
  735. public function testUndeleteTree2() {
  736. $this->loadFixtures('RevisionArticle', 'RevisionArticlesRev');
  737. $Article = new RevisionArticle();
  738. $Article->initializeRevisions();
  739. $Article->create([
  740. 'title' => 'f�rste barn',
  741. 'content' => 'stuff',
  742. 'parent_id' => 3,
  743. 'user_id' => 1]);
  744. $Article->save();
  745. $Article->create([
  746. 'title' => 'andre barn',
  747. 'content' => 'stuff',
  748. 'parent_id' => 4,
  749. 'user_id' => 1]);
  750. $Article->save();
  751. $Article->delete(3);
  752. $Article->id = 3;
  753. $Article->undelete();
  754. $result = $Article->find('all');
  755. // Test that children are also "returned" to their undeleted father
  756. $this->assertEquals(sizeof($result), 5);
  757. $this->assertEquals($result[0]['Article']['lft'], 1);
  758. $this->assertEquals($result[0]['Article']['rght'], 10);
  759. $this->assertEquals($result[1]['Article']['lft'], 2);
  760. $this->assertEquals($result[1]['Article']['rght'], 3);
  761. $this->assertEquals($result[2]['Article']['id'], 3);
  762. $this->assertEquals($result[2]['Article']['lft'], 4);
  763. $this->assertEquals($result[2]['Article']['rght'], 9);
  764. $this->assertEquals($result[3]['Article']['id'], 4);
  765. $this->assertEquals($result[3]['Article']['lft'], 5);
  766. $this->assertEquals($result[3]['Article']['rght'], 8);
  767. $this->assertEquals($result[4]['Article']['id'], 5);
  768. $this->assertEquals($result[4]['Article']['lft'], 6);
  769. $this->assertEquals($result[4]['Article']['rght'], 7);
  770. }
  771. /**
  772. * RevisionBehaviorTest::testInitializeRevisionsWithLimit()
  773. *
  774. * @return void
  775. */
  776. public function testInitializeRevisionsWithLimit() {
  777. $this->loadFixtures('RevisionPost', 'RevisionPostsRev', 'RevisionArticle', 'RevisionArticlesRev', 'RevisionComment',
  778. 'RevisionCommentsRev', 'RevisionCommentsRevisionTag', 'RevisionVote', 'RevisionVotesRev', 'RevisionTag',
  779. 'RevisionTagsRev');
  780. $Comment = new RevisionComment();
  781. $Post = new RevisionPost();
  782. $Article = new RevisionArticle();
  783. $Comment->bindModel(['hasAndBelongsToMany' => ['Tag' => ['className' => 'RevisionTag', 'with' =>
  784. 'CommentsTag']]], false);
  785. $this->assertFalse($Post->initializeRevisions());
  786. $this->assertTrue($Article->initializeRevisions());
  787. $this->assertFalse($Comment->initializeRevisions());
  788. $this->assertFalse($Comment->Vote->initializeRevisions());
  789. $this->assertFalse($Comment->Tag->initializeRevisions());
  790. }
  791. /**
  792. * RevisionBehaviorTest::testInitializeRevisions()
  793. *
  794. * @return void
  795. */
  796. public function testInitializeRevisions() {
  797. $this->loadFixtures('RevisionPost');
  798. $Post = new RevisionPost();
  799. $this->assertTrue($Post->initializeRevisions(2));
  800. $result = $Post->ShadowModel->find('all');
  801. $this->assertEquals(sizeof($result), 3);
  802. }
  803. /**
  804. * RevisionBehaviorTest::testRevertAll()
  805. *
  806. * @return void
  807. */
  808. public function testRevertAll() {
  809. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  810. $Post = new RevisionPost();
  811. $Post->save(['id' => 1, 'title' => 'tullball1']);
  812. $Post->save(['id' => 3, 'title' => 'tullball3']);
  813. $Post->create(['title' => 'new post', 'content' => 'stuff']);
  814. $Post->save();
  815. $result = $Post->find('all');
  816. $this->assertEquals($result[0]['Post']['title'], 'tullball1');
  817. $this->assertEquals($result[1]['Post']['title'], 'Post 2');
  818. $this->assertEquals($result[2]['Post']['title'], 'tullball3');
  819. $this->assertEquals($result[3]['Post']['title'], 'new post');
  820. $this->assertTrue($Post->revertAll(['date' => date('Y-m-d H:i:s', strtotime('yesterday'))]));
  821. $result = $Post->find('all');
  822. $this->assertEquals($result[0]['Post']['title'], 'Lorem ipsum dolor sit amet');
  823. $this->assertEquals($result[1]['Post']['title'], 'Post 2');
  824. $this->assertEquals($result[2]['Post']['title'], 'Post 3');
  825. $this->assertEquals(sizeof($result), 3);
  826. }
  827. /**
  828. * RevisionBehaviorTest::testRevertAllConditions()
  829. *
  830. * @return void
  831. */
  832. public function testRevertAllConditions() {
  833. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  834. $Post = new RevisionPost();
  835. $Post->save(['id' => 1, 'title' => 'tullball1']);
  836. $Post->save(['id' => 3, 'title' => 'tullball3']);
  837. $Post->create();
  838. $Post->save(['title' => 'new post', 'content' => 'stuff']);
  839. $result = $Post->find('all');
  840. $this->assertEquals($result[0]['Post']['title'], 'tullball1');
  841. $this->assertEquals($result[1]['Post']['title'], 'Post 2');
  842. $this->assertEquals($result[2]['Post']['title'], 'tullball3');
  843. $this->assertEquals($result[3]['Post']['title'], 'new post');
  844. $this->assertTrue($Post->revertAll(['conditions' => ['Post.id' => [
  845. 1,
  846. 2,
  847. 4]], 'date' => date('Y-m-d H:i:s', strtotime('yesterday'))]));
  848. $result = $Post->find('all');
  849. $this->assertEquals($result[0]['Post']['title'], 'Lorem ipsum dolor sit amet');
  850. $this->assertEquals($result[1]['Post']['title'], 'Post 2');
  851. $this->assertEquals($result[2]['Post']['title'], 'tullball3');
  852. $this->assertEquals(sizeof($result), 3);
  853. }
  854. /**
  855. * RevisionBehaviorTest::testOnWithModel()
  856. *
  857. * @return void
  858. */
  859. public function testOnWithModel() {
  860. $this->loadFixtures('RevisionComment', 'RevisionCommentsRev', 'RevisionCommentsRevisionTag',
  861. 'RevisionCommentsRevisionTagsRev', 'RevisionTag', 'RevisionTagsRev');
  862. $Comment = new RevisionComment();
  863. $Comment->bindModel(['hasAndBelongsToMany' => ['Tag' => ['className' => 'RevisionTag', 'with' =>
  864. 'CommentsTag']]], false);
  865. $result = $Comment->find('first', ['contain' => ['Tag' => ['id', 'title']]]);
  866. $this->assertEquals(sizeof($result['Tag']), 3);
  867. $this->assertEquals($result['Tag'][0]['title'], 'Fun');
  868. $this->assertEquals($result['Tag'][1]['title'], 'Hard');
  869. $this->assertEquals($result['Tag'][2]['title'], 'Trick');
  870. }
  871. /**
  872. * RevisionBehaviorTest::testHABTMRelatedUndoed()
  873. *
  874. * @return void
  875. */
  876. public function testHABTMRelatedUndoed() {
  877. $this->loadFixtures('RevisionComment', 'RevisionCommentsRev', 'RevisionCommentsRevisionTag',
  878. 'RevisionCommentsRevisionTagsRev', 'RevisionTag', 'RevisionTagsRev');
  879. $Comment = new RevisionComment();
  880. $Comment->bindModel(['hasAndBelongsToMany' => ['Tag' => ['className' => 'RevisionTag', 'with' =>
  881. 'CommentsTag']]], false);
  882. $Comment->Tag->id = 3;
  883. $Comment->Tag->undo();
  884. $result = $Comment->find('first', ['contain' => ['Tag' => ['id', 'title']]]);
  885. $this->assertEquals($result['Tag'][2]['title'], 'Tricks');
  886. }
  887. /**
  888. * RevisionBehaviorTest::testOnWithModelUndoed()
  889. *
  890. * @return void
  891. */
  892. public function testOnWithModelUndoed() {
  893. $this->loadFixtures('RevisionComment', 'RevisionCommentsRev', 'RevisionCommentsRevisionTag',
  894. 'RevisionCommentsRevisionTagsRev', 'RevisionTag', 'RevisionTagsRev');
  895. $Comment = new RevisionComment();
  896. $Comment->bindModel(['hasAndBelongsToMany' => ['Tag' => ['className' => 'RevisionTag', 'with' =>
  897. 'CommentsTag']]], false);
  898. $Comment->CommentsTag->delete(3);
  899. $result = $Comment->find('first', ['contain' => ['Tag' => ['id', 'title']]]);
  900. $this->assertEquals(sizeof($result['Tag']), 2);
  901. $this->assertEquals($result['Tag'][0]['title'], 'Fun');
  902. $this->assertEquals($result['Tag'][1]['title'], 'Hard');
  903. $Comment->CommentsTag->id = 3;
  904. $this->assertTrue($Comment->CommentsTag->undelete(), 'Undelete unsuccessful');
  905. $result = $Comment->find('first', ['contain' => ['Tag' => ['id', 'title']]]);
  906. $this->assertEquals(sizeof($result['Tag']), 3);
  907. $this->assertEquals($result['Tag'][0]['title'], 'Fun');
  908. $this->assertEquals($result['Tag'][1]['title'], 'Hard');
  909. $this->assertEquals($result['Tag'][2]['title'], 'Trick');
  910. $this->assertNoErrors('Third Tag not back : %s');
  911. }
  912. /**
  913. * RevisionBehaviorTest::testHabtmRevSave()
  914. *
  915. * @return void
  916. */
  917. public function testHabtmRevSave() {
  918. $this->loadFixtures('RevisionComment', 'RevisionCommentsRev', 'RevisionCommentsRevisionTag',
  919. 'RevisionCommentsRevisionTagsRev', 'RevisionTag', 'RevisionTagsRev');
  920. $Comment = new RevisionComment();
  921. $Comment->bindModel(['hasAndBelongsToMany' => ['Tag' => ['className' => 'RevisionTag']]], false);
  922. $result = $Comment->find('first', ['contain' => ['Tag' => ['id', 'title']]]);
  923. $this->assertEquals(sizeof($result['Tag']), 3);
  924. $this->assertEquals($result['Tag'][0]['title'], 'Fun');
  925. $this->assertEquals($result['Tag'][1]['title'], 'Hard');
  926. $this->assertEquals($result['Tag'][2]['title'], 'Trick');
  927. $currentIds = Set::extract($result, 'Tag.{n}.id');
  928. $expected = implode(',', $currentIds);
  929. $Comment->id = 1;
  930. $result = $Comment->newest();
  931. $this->assertEquals($expected, $result['Comment']['Tag']);
  932. $Comment->save(['Comment' => ['id' => 1], 'Tag' => ['Tag' => [2, 4]]]);
  933. $result = $Comment->find('first', ['contain' => ['Tag' => ['id', 'title']]]);
  934. $this->assertEquals(sizeof($result['Tag']), 2);
  935. //TODO: assert
  936. $this->assertEquals($result['Tag'][0]['title'], 'News');
  937. $this->assertEquals($result['Tag'][1]['title'], 'Hard');
  938. $currentIds = Set::extract($result, 'Tag.{n}.id');
  939. $expected = implode(',', $currentIds);
  940. $Comment->id = 1;
  941. $result = $Comment->newest();
  942. $this->assertEquals(4, $result['Comment']['version_id']);
  943. $this->assertEquals($expected, $result['Comment']['Tag']);
  944. }
  945. /**
  946. * RevisionBehaviorTest::testHabtmRevCreate()
  947. *
  948. * @return void
  949. */
  950. public function testHabtmRevCreate() {
  951. $this->loadFixtures('RevisionComment', 'RevisionCommentsRev', 'RevisionCommentsRevisionTag',
  952. 'RevisionCommentsRevisionTagsRev', 'RevisionTag', 'RevisionTagsRev');
  953. $Comment = new RevisionComment();
  954. $Comment->bindModel(['hasAndBelongsToMany' => ['Tag' => ['className' => 'RevisionTag']]], false);
  955. $result = $Comment->find('first', ['contain' => ['Tag' => ['id', 'title']]]);
  956. $this->assertEquals(sizeof($result['Tag']), 3);
  957. $this->assertEquals($result['Tag'][0]['title'], 'Fun');
  958. $this->assertEquals($result['Tag'][1]['title'], 'Hard');
  959. $this->assertEquals($result['Tag'][2]['title'], 'Trick');
  960. $Comment->create(['Comment' => ['title' => 'Comment 4'], 'Tag' => ['Tag' => [2, 4]]]);
  961. $Comment->save();
  962. $result = $Comment->newest();
  963. $this->assertEquals('2,4', $result['Comment']['Tag']);
  964. }
  965. /**
  966. * RevisionBehaviorTest::testHabtmRevIgnore()
  967. *
  968. * @return void
  969. */
  970. public function testHabtmRevIgnore() {
  971. $this->loadFixtures('RevisionComment', 'RevisionCommentsRev', 'RevisionCommentsRevisionTag',
  972. 'RevisionCommentsRevisionTagsRev', 'RevisionTag', 'RevisionTagsRev');
  973. $Comment = new RevisionComment();
  974. $Comment->Behaviors->unload('Revision');
  975. $Comment->Behaviors->load('Revision', ['ignore' => ['Tag']]);
  976. $Comment->bindModel(['hasAndBelongsToMany' => ['Tag' => ['className' => 'RevisionTag']]], false);
  977. $Comment->id = 1;
  978. $originalResult = $Comment->newest();
  979. $Comment->save(['Comment' => ['id' => 1], 'Tag' => ['Tag' => [2, 4]]]);
  980. $result = $Comment->newest();
  981. $this->assertEquals($originalResult, $result);
  982. }
  983. /**
  984. * RevisionBehaviorTest::testHabtmRevUndo()
  985. *
  986. * @return void
  987. */
  988. public function testHabtmRevUndo() {
  989. $this->loadFixtures('RevisionComment', 'RevisionCommentsRev', 'RevisionCommentsRevisionTag',
  990. 'RevisionCommentsRevisionTagsRev', 'RevisionTag', 'RevisionTagsRev');
  991. $Comment = new RevisionComment();
  992. $Comment->bindModel(['hasAndBelongsToMany' => ['Tag' => ['className' => 'RevisionTag']]], false);
  993. $Comment->save(['Comment' => ['id' => 1, 'title' => 'edit'], 'Tag' => ['Tag' => [2, 4]]]);
  994. $Comment->id = 1;
  995. $Comment->undo();
  996. $result = $Comment->find('first', ['recursive' => 1]); //'contain' => array('Tag' => array('id','title'))));
  997. $this->assertEquals(sizeof($result['Tag']), 3);
  998. //TODO: assert
  999. $this->assertEquals($result['Tag'][0]['title'], 'Trick');
  1000. $this->assertEquals($result['Tag'][1]['title'], 'Hard');
  1001. $this->assertEquals($result['Tag'][2]['title'], 'Fun');
  1002. $this->assertNoErrors('3 tags : %s');
  1003. }
  1004. /**
  1005. * RevisionBehaviorTest::testHabtmRevUndoJustHabtmChanges()
  1006. *
  1007. * @return void
  1008. */
  1009. public function testHabtmRevUndoJustHabtmChanges() {
  1010. $this->loadFixtures('RevisionComment', 'RevisionCommentsRev', 'RevisionCommentsRevisionTag',
  1011. 'RevisionCommentsRevisionTagsRev', 'RevisionTag', 'RevisionTagsRev');
  1012. $Comment = new RevisionComment();
  1013. $Comment->bindModel(['hasAndBelongsToMany' => ['Tag' => ['className' => 'RevisionTag']]], false);
  1014. $Comment->save(['Comment' => ['id' => 1], 'Tag' => ['Tag' => [2, 4]]]);
  1015. $Comment->id = 1;
  1016. $Comment->undo();
  1017. $result = $Comment->find('first', ['recursive' => 1]); //'contain' => array('Tag' => array('id','title'))));
  1018. $this->assertEquals(sizeof($result['Tag']), 3);
  1019. $this->assertEquals($result['Tag'][0]['title'], 'Trick');
  1020. $this->assertEquals($result['Tag'][1]['title'], 'Hard');
  1021. $this->assertEquals($result['Tag'][2]['title'], 'Fun');
  1022. $this->assertNoErrors('3 tags : %s');
  1023. }
  1024. /**
  1025. * RevisionBehaviorTest::testHabtmRevRevert()
  1026. *
  1027. * @return void
  1028. */
  1029. public function testHabtmRevRevert() {
  1030. $this->loadFixtures('RevisionComment', 'RevisionCommentsRev', 'RevisionCommentsRevisionTag',
  1031. 'RevisionCommentsRevisionTagsRev', 'RevisionTag', 'RevisionTagsRev');
  1032. $Comment = new RevisionComment();
  1033. $Comment->bindModel(['hasAndBelongsToMany' => ['Tag' => ['className' => 'RevisionTag']]], false);
  1034. $Comment->save(['Comment' => ['id' => 1], 'Tag' => ['Tag' => [2, 4]]]);
  1035. $Comment->id = 1;
  1036. $Comment->revertTo(1);
  1037. $result = $Comment->find('first', ['recursive' => 1]); //'contain' => array('Tag' => array('id','title'))));
  1038. $this->assertEquals(sizeof($result['Tag']), 3);
  1039. $this->assertEquals($result['Tag'][0]['title'], 'Trick');
  1040. $this->assertEquals($result['Tag'][1]['title'], 'Hard');
  1041. $this->assertEquals($result['Tag'][2]['title'], 'Fun');
  1042. $this->assertNoErrors('3 tags : %s');
  1043. }
  1044. /**
  1045. * RevisionBehaviorTest::testRevertToHabtm2()
  1046. *
  1047. * @return void
  1048. */
  1049. public function testRevertToHabtm2() {
  1050. $this->loadFixtures('RevisionComment', 'RevisionCommentsRev', 'RevisionCommentsRevisionTag',
  1051. 'RevisionCommentsRevisionTagsRev', 'RevisionTag', 'RevisionTagsRev');
  1052. $Comment = new RevisionComment();
  1053. $Comment->bindModel(['hasAndBelongsToMany' => ['Tag' => ['className' => 'RevisionTag']]], false);
  1054. $commentOne = $Comment->find('first', ['conditions' => ['Comment.id' => 1], 'contain' => 'Tag']);
  1055. $this->assertEquals($commentOne['Comment']['title'], 'Comment 1');
  1056. $this->assertEquals(Set::extract($commentOne, 'Tag.{n}.id'), [
  1057. 1,
  1058. 2,
  1059. 3]);
  1060. $Comment->id = 1;
  1061. $revOne = $Comment->newest();
  1062. $this->assertEquals($revOne['Comment']['title'], 'Comment 1');
  1063. $this->assertEquals($revOne['Comment']['Tag'], '1,2,3');
  1064. $versionId = $revOne['Comment']['version_id'];
  1065. $Comment->create(['Comment' => ['id' => 1, 'title' => 'Edited']]);
  1066. $Comment->save();
  1067. $commentOne = $Comment->find('first', ['conditions' => ['Comment.id' => 1], 'contain' => 'Tag']);
  1068. $this->assertEquals($commentOne['Comment']['title'], 'Edited');
  1069. $result = Set::extract($commentOne, 'Tag.{n}.id');
  1070. $expected = [
  1071. 1,
  1072. 2,
  1073. 3];
  1074. $this->assertEquals($expected, $result);
  1075. $Comment->id = 1;
  1076. $revOne = $Comment->newest();
  1077. $this->assertEquals($revOne['Comment']['title'], 'Edited');
  1078. $this->assertEquals($revOne['Comment']['Tag'], '1,2,3');
  1079. $Comment->revertTo(1);
  1080. $commentOne = $Comment->find('first', ['conditions' => ['Comment.id' => 1], 'contain' => 'Tag']);
  1081. $this->assertEquals($commentOne['Comment']['title'], 'Comment 1');
  1082. $result = Set::extract($commentOne, 'Tag.{n}.id');
  1083. //TODO: assert
  1084. $this->assertEquals($result, [
  1085. 3,
  1086. 2,
  1087. 1]);
  1088. $Comment->id = 1;
  1089. $revOne = $Comment->newest();
  1090. $this->assertEquals($revOne['Comment']['title'], 'Comment 1');
  1091. $this->assertEquals($revOne['Comment']['Tag'], '1,2,3');
  1092. }
  1093. /**
  1094. * RevisionBehaviorTest::testHabtmRevRevertToDate()
  1095. *
  1096. * @return void
  1097. */
  1098. public function testHabtmRevRevertToDate() {
  1099. $this->loadFixtures('RevisionComment', 'RevisionCommentsRev', 'RevisionCommentsRevisionTag',
  1100. 'RevisionCommentsRevisionTagsRev', 'RevisionTag', 'RevisionTagsRev');
  1101. $Comment = new RevisionComment();
  1102. $Comment->bindModel(['hasAndBelongsToMany' => ['Tag' => ['className' => 'RevisionTag']]], false);
  1103. $Comment->save(['Comment' => ['id' => 1], 'Tag' => ['Tag' => [2, 4]]]);
  1104. $Comment->id = 1;
  1105. $Comment->revertToDate(date('Y-m-d H:i:s', strtotime('yesterday')));
  1106. $result = $Comment->find('first', ['recursive' => 1]);
  1107. $this->assertEquals(sizeof($result['Tag']), 3);
  1108. //TODO: assert
  1109. $this->assertEquals($result['Tag'][0]['title'], 'Trick');
  1110. $this->assertEquals($result['Tag'][1]['title'], 'Hard');
  1111. $this->assertEquals($result['Tag'][2]['title'], 'Fun');
  1112. $this->assertNoErrors('3 tags : %s');
  1113. }
  1114. /**
  1115. * RevisionBehaviorTest::testRevertToTheTagsCommentHadBefore()
  1116. *
  1117. * @return void
  1118. */
  1119. public function testRevertToTheTagsCommentHadBefore() {
  1120. $this->loadFixtures('RevisionComment', 'RevisionCommentsRev', 'RevisionCommentsRevisionTag',
  1121. 'RevisionCommentsRevisionTagsRev', 'RevisionTag', 'RevisionTagsRev');
  1122. $Comment = new RevisionComment();
  1123. $Comment->bindModel(['hasAndBelongsToMany' => ['Tag' => ['className' => 'RevisionTag']]], false);
  1124. $result = $Comment->find('first', ['conditions' => ['Comment.id' => 2], 'contain' => ['Tag' => ['id',
  1125. 'title']]]);
  1126. $this->assertEquals(sizeof($result['Tag']), 2);
  1127. $this->assertEquals($result['Tag'][0]['title'], 'Fun');
  1128. $this->assertEquals($result['Tag'][1]['title'], 'Trick');
  1129. $Comment->save(['Comment' => ['id' => 2], 'Tag' => ['Tag' => [
  1130. 2,
  1131. 3,
  1132. 4]]]);
  1133. $result = $Comment->find('first', ['conditions' => ['Comment.id' => 2], 'contain' => ['Tag' => ['id',
  1134. 'title']]]);
  1135. $this->assertEquals(sizeof($result['Tag']), 3);
  1136. //TODO: assert
  1137. $this->assertEquals($result['Tag'][0]['title'], 'Trick');
  1138. $this->assertEquals($result['Tag'][1]['title'], 'Hard');
  1139. $this->assertEquals($result['Tag'][2]['title'], 'News');
  1140. // revert Tags on comment logic
  1141. $Comment->id = 2;
  1142. $this->assertTrue((bool)$Comment->revertToDate(date('Y-m-d H:i:s', strtotime('yesterday'))),
  1143. 'revertHabtmToDate unsuccessful : %s');
  1144. $result = $Comment->find('first', ['conditions' => ['Comment.id' => 2], 'contain' => ['Tag' => ['id',
  1145. 'title']]]);
  1146. $this->assertEquals(sizeof($result['Tag']), 2);
  1147. //TODO: assert
  1148. $this->assertEquals($result['Tag'][0]['title'], 'Trick');
  1149. $this->assertEquals($result['Tag'][1]['title'], 'Fun');
  1150. }
  1151. /**
  1152. * RevisionBehaviorTest::testSaveWithOutTags()
  1153. *
  1154. * @return void
  1155. */
  1156. public function testSaveWithOutTags() {
  1157. $this->loadFixtures('RevisionComment', 'RevisionCommentsRev', 'RevisionCommentsRevisionTag',
  1158. 'RevisionCommentsRevisionTagsRev', 'RevisionTag', 'RevisionTagsRev');
  1159. $Comment = new RevisionComment();
  1160. $Comment->bindModel(['hasAndBelongsToMany' => ['Tag' => ['className' => 'RevisionTag']]], false);
  1161. $Comment->id = 1;
  1162. $newest = $Comment->newest();
  1163. $Comment->save(['Comment' => ['id' => 1, 'title' => 'spam']]);
  1164. $result = $Comment->newest();
  1165. $this->assertEquals($newest['Comment']['Tag'], $result['Comment']['Tag']);
  1166. }
  1167. /**
  1168. * RevisionBehaviorTest::testRevertToDeletedTag()
  1169. *
  1170. * @return void
  1171. */
  1172. public function testRevertToDeletedTag() {
  1173. $this->loadFixtures('RevisionComment', 'RevisionCommentsRev', 'RevisionCommentsRevisionTag',
  1174. 'RevisionCommentsRevisionTagsRev', 'RevisionTag', 'RevisionTagsRev');
  1175. $Comment = new RevisionComment();
  1176. $Comment->bindModel(['hasAndBelongsToMany' => ['Tag' => ['className' => 'RevisionTag', 'with' =>
  1177. 'CommentsTag']]], false);
  1178. $Comment->Tag->delete(1);
  1179. $result = $Comment->ShadowModel->find('all', ['conditions' => ['version_id' => [4, 5]]]);
  1180. //TODO: assert/fixme
  1181. //debug($result);
  1182. //$this->assertEquals($result[0]['Comment']['Tag'], '3');
  1183. //$this->assertEquals($result[1]['Comment']['Tag'], '2,3');
  1184. }
  1185. /**
  1186. * @expectedException PHPUNIT_FRAMEWORK_ERROR_WARNING
  1187. * @return void
  1188. */
  1189. public function testBadKittyForgotId() {
  1190. $Comment = new RevisionComment();
  1191. $this->assertNull($Comment->createRevision(), 'createRevision() : %s');
  1192. $this->assertError(true);
  1193. $this->assertNull($Comment->diff(), 'diff() : %s');
  1194. $this->assertError(true);
  1195. $this->assertNull($Comment->undelete(), 'undelete() : %s');
  1196. $this->assertError(true);
  1197. $this->assertNull($Comment->undo(), 'undo() : %s');
  1198. $this->assertError(true);
  1199. $this->assertNull($Comment->newest(), 'newest() : %s');
  1200. $this->assertError(true);
  1201. $this->assertNull($Comment->oldest(), 'oldest() : %s');
  1202. $this->assertError(true);
  1203. $this->assertNull($Comment->previous(), 'previous() : %s');
  1204. $this->assertError(true);
  1205. $this->assertNull($Comment->revertTo(10), 'revertTo() : %s');
  1206. $this->assertError(true);
  1207. $this->assertNull($Comment->revertToDate(date('Y-m-d H:i:s', strtotime('yesterday')), 'revertTo() : %s'));
  1208. $this->assertError(true);
  1209. $this->assertNull($Comment->revisions(), 'revisions() : %s');
  1210. $this->assertError(true);
  1211. }
  1212. /**
  1213. * RevisionBehaviorTest::testBadKittyMakesUpStuff()
  1214. *
  1215. * @return void
  1216. */
  1217. public function testBadKittyMakesUpStuff() {
  1218. $this->loadFixtures('RevisionComment', 'RevisionCommentsRev', 'RevisionCommentsRevisionTag',
  1219. 'RevisionCommentsRevisionTagsRev', 'RevisionTag', 'RevisionTagsRev');
  1220. $Comment = new RevisionComment();
  1221. $Comment->id = 1;
  1222. $this->assertFalse($Comment->revertTo(10), 'revertTo() : %s');
  1223. $this->assertSame([], $Comment->diff(1, 4), 'diff() between existing and non-existing : %s');
  1224. $this->assertSame([], $Comment->diff(10, 4), 'diff() between two non existing : %s');
  1225. }
  1226. /**
  1227. * @expectedException PHPUNIT_FRAMEWORK_ERROR_WARNING
  1228. * @return void
  1229. */
  1230. public function testMethodsOnNonRevisedModel() {
  1231. $User = new RevisionUser();
  1232. $User->id = 1;
  1233. $this->assertFalse($User->createRevision());
  1234. $this->assertError();
  1235. $this->assertNull($User->diff());
  1236. $this->assertError();
  1237. $this->assertFalse($User->initializeRevisions());
  1238. $this->assertError();
  1239. $this->assertNull($User->newest());
  1240. $this->assertError();
  1241. $this->assertNull($User->oldest());
  1242. $this->assertError();
  1243. $this->assertFalse($User->previous());
  1244. $this->assertError();
  1245. $this->assertFalse($User->revertAll(['date' => '1970-01-01']));
  1246. $this->assertError();
  1247. $this->assertFalse($User->revertTo(2));
  1248. $this->assertError();
  1249. $this->assertTrue((bool)$User->revertToDate('1970-01-01'));
  1250. $this->assertNoErrors();
  1251. $this->assertFalse($User->revisions());
  1252. $this->assertError();
  1253. $this->assertFalse($User->undo());
  1254. $this->assertError();
  1255. $this->assertFalse($User->undelete());
  1256. $this->assertError();
  1257. $this->assertFalse($User->updateRevisions());
  1258. $this->assertError();
  1259. }
  1260. /**
  1261. * RevisionBehaviorTest::testRevisions()
  1262. *
  1263. * @return void
  1264. */
  1265. public function testRevisions() {
  1266. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  1267. $Post = new RevisionPost();
  1268. $Post->create(['Post' => ['title' => 'Stuff (1)', 'content' => 'abc']]);
  1269. $Post->save();
  1270. $postID = $Post->id;
  1271. $Post->data = null;
  1272. $Post->id = null;
  1273. $Post->save(['Post' => ['id' => $postID, 'title' => 'Things (2)']]);
  1274. $Post->data = null;
  1275. $Post->id = null;
  1276. $Post->save(['Post' => ['id' => $postID, 'title' => 'Machines (3)']]);
  1277. $Post->bindModel(['hasMany' => ['Revision' => [
  1278. 'className' => 'RevisionPostsRev',
  1279. 'foreignKey' => 'id',
  1280. 'order' => 'version_id DESC']]]);
  1281. $result = $Post->read(null, $postID);
  1282. $this->assertEquals('Machines (3)', $result['Post']['title']);
  1283. $this->assertSame(3, sizeof($result['Revision']));
  1284. $this->assertEquals('Machines (3)', $result['Revision'][0]['title']);
  1285. $this->assertEquals('Things (2)', $result['Revision'][1]['title']);
  1286. $this->assertEquals('Stuff (1)', $result['Revision'][2]['title']);
  1287. $result = $Post->revisions();
  1288. $this->assertSame(2, sizeof($result));
  1289. $this->assertEquals('Things (2)', $result[0]['Post']['title']);
  1290. $this->assertEquals('Stuff (1)', $result[1]['Post']['title']);
  1291. $result = $Post->revisions([], true);
  1292. $this->assertSame(3, sizeof($result));
  1293. $this->assertEquals('Machines (3)', $result[0]['Post']['title']);
  1294. $this->assertEquals('Things (2)', $result[1]['Post']['title']);
  1295. $this->assertEquals('Stuff (1)', $result[2]['Post']['title']);
  1296. }
  1297. /**
  1298. * RevisionBehaviorTest::testFoo()
  1299. *
  1300. * @return void
  1301. */
  1302. public function testNoAlias() {
  1303. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  1304. $this->Controller = new Controller(new CakeRequest(null, false), new CakeResponse());
  1305. $this->Controller->loadModel('RevisionPost');
  1306. $this->Controller->RevisionPost->validate['title'] = [
  1307. 'minLength' => [
  1308. 'rule' => ['minLength', 6],
  1309. 'message' => 'mL',
  1310. ]
  1311. ];
  1312. $data = ['Post' => ['title' => 'S (1)', 'content' => 'abc']];
  1313. $this->Controller->RevisionPost->create();
  1314. $result = $this->Controller->RevisionPost->save($data);
  1315. $this->assertFalse($result);
  1316. $expected = ['title' => ['mL']];
  1317. $this->assertEquals($expected, $this->Controller->RevisionPost->validationErrors);
  1318. $this->Controller->render(false, false);
  1319. $this->assertEquals([], $this->Controller->View->validationErrors['Post']);
  1320. }
  1321. /**
  1322. * RevisionBehaviorTest::testFoo()
  1323. *
  1324. * @return void
  1325. */
  1326. public function testAlias() {
  1327. $this->loadFixtures('RevisionPost', 'RevisionPostsRev');
  1328. Configure::write('Revision.alias', true);
  1329. $this->Controller = new Controller(new CakeRequest(null, false), new CakeResponse());
  1330. $this->Controller->loadModel('RevisionPost');
  1331. $this->Controller->RevisionPost->validate['title'] = [
  1332. 'minLength' => [
  1333. 'rule' => ['minLength', 6],
  1334. 'message' => 'mL',
  1335. ]
  1336. ];
  1337. $data = ['Post' => ['title' => 'S (1)', 'content' => 'abc']];
  1338. $this->Controller->RevisionPost->create();
  1339. $result = $this->Controller->RevisionPost->save($data);
  1340. $this->assertFalse($result);
  1341. $expected = ['title' => ['mL']];
  1342. $this->assertEquals($expected, $this->Controller->RevisionPost->validationErrors);
  1343. $this->Controller->render(false, false);
  1344. $this->assertEquals($expected, $this->Controller->View->validationErrors['Post']);
  1345. $this->assertEquals([], $this->Controller->View->validationErrors['PostShadow']);
  1346. }
  1347. }
  1348. class RevisionTestModel extends CakeTestModel {
  1349. public $logableAction;
  1350. }
  1351. class RevisionPost extends RevisionTestModel {
  1352. public $name = 'RevisionPost';
  1353. public $alias = 'Post';
  1354. public $actsAs = ['Revision' => ['limit' => 5]];
  1355. public function beforeUndelete() {
  1356. $this->beforeUndelete = true;
  1357. return true;
  1358. }
  1359. public function afterUndelete() {
  1360. $this->afterUndelete = true;
  1361. return true;
  1362. }
  1363. }
  1364. class RevisionArticle extends RevisionTestModel {
  1365. public $name = 'RevisionArticle';
  1366. public $alias = 'Article';
  1367. public $actsAs = ['Tree', 'Revision' => ['ignore' => ['title']]];
  1368. /**
  1369. * Example of using this callback to undelete children
  1370. * of a deleted node.
  1371. */
  1372. public function afterUndelete() {
  1373. $formerChildren = $this->ShadowModel->find('list', [
  1374. 'conditions' => ['parent_id' => $this->id],
  1375. 'distinct' => true,
  1376. 'order' => 'version_created DESC, version_id DESC']);
  1377. foreach (array_keys($formerChildren) as $cid) {
  1378. $this->id = $cid;
  1379. $this->undelete();
  1380. }
  1381. }
  1382. }
  1383. class RevisionUser extends RevisionTestModel {
  1384. public $name = 'RevisionUser';
  1385. public $alias = 'User';
  1386. public $actsAs = ['Revision'];
  1387. }
  1388. class RevisionComment extends RevisionTestModel {
  1389. public $name = 'RevisionComment';
  1390. public $alias = 'Comment';
  1391. public $actsAs = ['Containable', 'Revision'];
  1392. public $hasMany = ['Vote' => [
  1393. 'className' => 'RevisionVote',
  1394. 'foreignKey' => 'revision_comment_id',
  1395. 'dependent' => true]];
  1396. }
  1397. class RevisionVote extends RevisionTestModel {
  1398. public $name = 'RevisionVote';
  1399. public $alias = 'Vote';
  1400. public $actsAs = ['Revision'];
  1401. }
  1402. class RevisionTag extends RevisionTestModel {
  1403. public $name = 'RevisionTag';
  1404. public $alias = 'Tag';
  1405. public $actsAs = ['Revision'];
  1406. public $hasAndBelongsToMany = ['Comment' => ['className' => 'RevisionComment']];
  1407. }
  1408. class CommentsTag extends RevisionTestModel {
  1409. public $name = 'CommentsTag';
  1410. public $useTable = 'revision_comments_revision_tags';
  1411. public $actsAs = ['Revision'];
  1412. }