PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/app/tests/cases/behaviors/sluggable.test.php

https://bitbucket.org/webpolis/liiv
PHP | 526 lines | 358 code | 73 blank | 95 comment | 0 complexity | 6f135fb1fbbd3b13a1b9d8018878a187 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /* SVN FILE: $Id: sluggable.test.php 36 2007-11-26 15:10:14Z mgiglesias $ */
  3. /**
  4. * Test cases for Sluggable Behavior, which are basically testing methods to test several
  5. * aspects of slug functionality.
  6. *
  7. * Go to the Sluggable Behavior page at Cake Syrup to learn more about it:
  8. *
  9. * http://cake-syrup.sourceforge.net/ingredients/sluggable-behavior/
  10. *
  11. * @filesource
  12. * @author Mariano Iglesias
  13. * @link http://cake-syrup.sourceforge.net/ingredients/sluggable-behavior/
  14. * @version $Revision: 36 $
  15. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  16. * @package app.tests
  17. * @subpackage app.tests.cases.behaviors
  18. */
  19. App::import('Behavior', 'sluggable');
  20. /**
  21. * Base model that to load Sluggable behavior on every test model.
  22. *
  23. * @package app.tests
  24. * @subpackage app.tests.cases.behaviors
  25. */
  26. class SluggableTestModel extends CakeTestModel
  27. {
  28. /**
  29. * Behaviors for this model
  30. *
  31. * @var array
  32. * @access public
  33. */
  34. var $actsAs = array('Sluggable');
  35. }
  36. /**
  37. * Model used in test case.
  38. *
  39. * @package app.tests
  40. * @subpackage app.tests.cases.behaviors
  41. */
  42. class SlugArticle extends SluggableTestModel
  43. {
  44. /**
  45. * Name for this model
  46. *
  47. * @var string
  48. * @access public
  49. */
  50. var $name = 'SlugArticle';
  51. }
  52. /**
  53. * Test case for Sluggable Behavior
  54. *
  55. * @package app.tests
  56. * @subpackage app.tests.cases.models
  57. */
  58. class SluggableTestCase extends CakeTestCase
  59. {
  60. /**
  61. * Fixtures associated with this test case
  62. *
  63. * @var array
  64. * @access public
  65. */
  66. var $fixtures = array('app.slug_article');
  67. /**
  68. * Method executed before each test
  69. *
  70. * @access public
  71. */
  72. function startTest()
  73. {
  74. $this->SlugArticle =& new SlugArticle();
  75. }
  76. /**
  77. * Method executed after each test
  78. *
  79. * @access public
  80. */
  81. function endTest()
  82. {
  83. unset($this->SlugArticle);
  84. ClassRegistry::flush();
  85. }
  86. /**
  87. * Test slug generation
  88. *
  89. * @access public
  90. */
  91. function testGeneration()
  92. {
  93. $Sluggable =& new SluggableBehavior();
  94. $result = $Sluggable->__slug('title', array('separator' => '-', 'length' => 100));
  95. $expected = 'title';
  96. $this->assertEqual($result, $expected);
  97. $result = $Sluggable->__slug('my title', array('separator' => '-', 'length' => 100));
  98. $expected = 'my-title';
  99. $this->assertEqual($result, $expected);
  100. $result = $Sluggable->__slug('MY TITle', array('separator' => '-', 'length' => 100));
  101. $expected = 'my-title';
  102. $this->assertEqual($result, $expected);
  103. $result = $Sluggable->__slug('my long title with some extra spaces ', array('separator' => '-', 'length' => 100));
  104. $expected = 'my-long-title-with-some-extra-spaces';
  105. $this->assertEqual($result, $expected);
  106. $result = $Sluggable->__slug('my long title! with@ "some" extra spaces & weird chars ', array('separator' => '-', 'length' => 100));
  107. $expected = 'my-long-title-with-some-extra-spaces-weird-chars';
  108. $this->assertEqual($result, $expected);
  109. $result = $Sluggable->__slug('-my - long title! with@ "some" extra spaces & weird chars ', array('separator' => '-', 'length' => 100));
  110. $expected = 'my-long-title-with-some-extra-spaces-weird-chars';
  111. $this->assertEqual($result, $expected);
  112. $result = $Sluggable->__slug('my long title! with@ "some" extra spaces & weird chars ', array('separator' => '-', 'length' => 10));
  113. $expected = 'my-long-ti';
  114. $this->assertEqual($result, $expected);
  115. $result = $Sluggable->__slug('my long title! with@ "some" extra spaces & weird chars ', array('separator' => '-', 'length' => 18));
  116. $expected = 'my-long-title-with';
  117. $this->assertEqual($result, $expected);
  118. $result = $Sluggable->__slug('my long title! with@ "some" extra spaces & weird chars ', array('separator' => '_', 'length' => 18));
  119. $expected = 'my_long_title_with';
  120. $this->assertEqual($result, $expected);
  121. unset($Sluggable);
  122. }
  123. /**
  124. * Test slug generation with translation tables
  125. *
  126. * @access public
  127. */
  128. function testGenerationWithTranslation()
  129. {
  130. $Sluggable =& new SluggableBehavior();
  131. // Predefined: UTF-8
  132. $result = $Sluggable->__slug('normal string for slug', array('separator' => '-', 'length' => 100, 'translation' => 'utf-8'));
  133. $expected = 'normal-string-for-slug';
  134. $this->assertEqual($result, $expected);
  135. $result = $Sluggable->__slug('-my - long title! with@ "some" extra spaces & weird chars ', array('separator' => '-', 'length' => 100, 'translation' => 'utf-8'));
  136. $expected = 'my-long-title-with-some-extra-spaces-weird-chars';
  137. $this->assertEqual($result, $expected);
  138. $result = $Sluggable->__slug('H' . chr(196).chr(146) . 're C' . chr(195).chr(182) . 'mes', array('separator' => '-', 'length' => 100, 'translation' => 'utf-8'));
  139. $expected = 'here-comes';
  140. $this->assertEqual($result, $expected);
  141. $result = $Sluggable->__slug('H' . chr(196).chr(155) . 're C' . chr(195).chr(182) . 'mes ' . chr(196).chr(129) . ' mix ' . chr(197).chr(165).chr(196).chr(164) . 'under', array('separator' => '-', 'length' => 100, 'translation' => 'utf-8'));
  142. $expected = 'here-comes-a-mix-thunder';
  143. $this->assertEqual($result, $expected);
  144. // Predefined: ISO-8859-1
  145. $result = $Sluggable->__slug('normal string for slug', array('separator' => '-', 'length' => 100, 'translation' => 'iso-8859-1'));
  146. $expected = 'normal-string-for-slug';
  147. $this->assertEqual($result, $expected);
  148. $result = $Sluggable->__slug('-my - long title! with@ "some" extra spaces & weird chars ', array('separator' => '-', 'length' => 100, 'translation' => 'iso-8859-1'));
  149. $expected = 'my-long-title-with-some-extra-spaces-weird-chars';
  150. $this->assertEqual($result, $expected);
  151. $result = $Sluggable->__slug('H' . chr(128) . 're C' . chr(245) . 'mes', array('separator' => '-', 'length' => 100, 'translation' => 'iso-8859-1'));
  152. $expected = 'here-comes';
  153. $this->assertEqual($result, $expected);
  154. $result = $Sluggable->__slug('H' . chr(128) . 're C' . chr(245) . 'mes ' . chr(226) . ' mix ' . chr(254) . 'under', array('separator' => '-', 'length' => 100, 'translation' => 'iso-8859-1'));
  155. $expected = 'here-comes-a-mix-thunder';
  156. $this->assertEqual($result, $expected);
  157. // UTF-8
  158. $settings = array('separator' => '-', 'length' => 100, 'translation' => array(
  159. array(
  160. // Decompositions for Latin-1 Supplement
  161. chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
  162. chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
  163. chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
  164. chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
  165. chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
  166. chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
  167. chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
  168. chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
  169. chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
  170. chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
  171. chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
  172. chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
  173. chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
  174. chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
  175. chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
  176. chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
  177. chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
  178. chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
  179. chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
  180. chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
  181. chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
  182. chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
  183. chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
  184. chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
  185. chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
  186. chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
  187. chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
  188. chr(195).chr(191) => 'y',
  189. // Decompositions for Latin Extended-A
  190. chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
  191. chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
  192. chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
  193. chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
  194. chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
  195. chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
  196. chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
  197. chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
  198. chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
  199. chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
  200. chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
  201. chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
  202. chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
  203. chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
  204. chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
  205. chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
  206. chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
  207. chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
  208. chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
  209. chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
  210. chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
  211. chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
  212. chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
  213. chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
  214. chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
  215. chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
  216. chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
  217. chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
  218. chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
  219. chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
  220. chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
  221. chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
  222. chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
  223. chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
  224. chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
  225. chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
  226. chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
  227. chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
  228. chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
  229. chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
  230. chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
  231. chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
  232. chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
  233. chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
  234. chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
  235. chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
  236. chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
  237. chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
  238. chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
  239. chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
  240. chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
  241. chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
  242. chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
  243. chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
  244. chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
  245. chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
  246. chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
  247. chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
  248. chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
  249. chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
  250. chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
  251. chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
  252. chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
  253. chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
  254. // Euro Sign
  255. chr(226).chr(130).chr(172) => 'E'
  256. )
  257. ));
  258. $result = $Sluggable->__slug('normal string for slug', $settings);
  259. $expected = 'normal-string-for-slug';
  260. $this->assertEqual($result, $expected);
  261. $result = $Sluggable->__slug('-my - long title! with@ "some" extra spaces & weird chars ', $settings);
  262. $expected = 'my-long-title-with-some-extra-spaces-weird-chars';
  263. $this->assertEqual($result, $expected);
  264. $result = $Sluggable->__slug('H' . chr(196).chr(146) . 're C' . chr(195).chr(182) . 'mes', $settings);
  265. $expected = 'here-comes';
  266. $this->assertEqual($result, $expected);
  267. $result = $Sluggable->__slug('H' . chr(196).chr(155) . 're C' . chr(195).chr(182) . 'mes ' . chr(196).chr(129) . ' mix ' . chr(197).chr(165).chr(196).chr(164) . 'under', $settings);
  268. $expected = 'here-comes-a-mix-thunder';
  269. $this->assertEqual($result, $expected);
  270. // ISO-8859-1 translation table
  271. $settings = array('separator' => '-', 'length' => 100, 'translation' => array(
  272. chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
  273. .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
  274. .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
  275. .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
  276. .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
  277. .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
  278. .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
  279. .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
  280. .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
  281. .chr(252).chr(253).chr(255),
  282. 'EfSZsz' . 'YcYuAAA' . 'AAACEEE' . 'EIIIINO' . 'OOOOOUU' . 'UUYaaaa' . 'aaceeee' . 'iiiinoo' . 'oooouuu' . 'uyy',
  283. array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254)),
  284. array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th')
  285. ));
  286. $result = $Sluggable->__slug('normal string for slug', $settings);
  287. $expected = 'normal-string-for-slug';
  288. $this->assertEqual($result, $expected);
  289. $result = $Sluggable->__slug('-my - long title! with@ "some" extra spaces & weird chars ', $settings);
  290. $expected = 'my-long-title-with-some-extra-spaces-weird-chars';
  291. $this->assertEqual($result, $expected);
  292. $result = $Sluggable->__slug('H' . chr(128) . 're C' . chr(245) . 'mes', $settings);
  293. $expected = 'here-comes';
  294. $this->assertEqual($result, $expected);
  295. $result = $Sluggable->__slug('H' . chr(128) . 're C' . chr(245) . 'mes ' . chr(226) . ' mix ' . chr(254) . 'under', $settings);
  296. $expected = 'here-comes-a-mix-thunder';
  297. $this->assertEqual($result, $expected);
  298. unset($Sluggable);
  299. }
  300. /**
  301. * Test slug being added before save operation
  302. *
  303. * @access public
  304. */
  305. function testBeforeSave()
  306. {
  307. $Sluggable =& new SluggableBehavior();
  308. $Sluggable->setup($this->SlugArticle, array('separator' => '-', 'length' => 100));
  309. $this->SlugArticle->data = array('SlugArticle' => array('title' => 'My test title'));
  310. $result = $Sluggable->beforeSave($this->SlugArticle);
  311. $this->assertTrue($result !== false);
  312. $result = $this->SlugArticle->data;
  313. $expected = array('SlugArticle' => array('title' => 'My test title', 'slug' => 'my-test-title'));
  314. $this->assertEqual($result, $expected);
  315. $this->SlugArticle->data = array('SlugArticle' => array('title' => 'First Article'));
  316. $result = $Sluggable->beforeSave($this->SlugArticle);
  317. $this->assertTrue($result !== false);
  318. $result = $this->SlugArticle->data;
  319. $expected = array('SlugArticle' => array('title' => 'First Article', 'slug' => 'first-article-1'));
  320. $this->assertEqual($result, $expected);
  321. $this->SlugArticle->data = array('SlugArticle' => array('title' => 'First Article Unique'));
  322. $result = $Sluggable->beforeSave($this->SlugArticle);
  323. $this->assertTrue($result !== false);
  324. $result = $this->SlugArticle->data;
  325. $expected = array('SlugArticle' => array('title' => 'First Article Unique', 'slug' => 'first-article-unique'));
  326. $this->assertEqual($result, $expected);
  327. $this->SlugArticle->data = array('SlugArticle' => array('body' => 'Just Body'));
  328. $result = $Sluggable->beforeSave($this->SlugArticle);
  329. $this->assertTrue($result !== false);
  330. $result = $this->SlugArticle->data;
  331. $expected = array('SlugArticle' => array('body' => 'Just Body'));
  332. $this->assertEqual($result, $expected);
  333. $Sluggable->setup($this->SlugArticle, array('label' => array('title', 'subtitle'), 'separator' => '-', 'length' => 100));
  334. $this->SlugArticle->data = array('SlugArticle' => array('title' => 'My test title'));
  335. $result = $Sluggable->beforeSave($this->SlugArticle);
  336. $this->assertTrue($result !== false);
  337. $result = $this->SlugArticle->data;
  338. $expected = array('SlugArticle' => array('title' => 'My test title', 'slug' => 'my-test-title'));
  339. $this->assertEqual($result, $expected);
  340. $this->SlugArticle->data = array('SlugArticle' => array('title' => 'My test title', 'subtitle' => ''));
  341. $result = $Sluggable->beforeSave($this->SlugArticle);
  342. $this->assertTrue($result !== false);
  343. $result = $this->SlugArticle->data;
  344. $expected = array('SlugArticle' => array('title' => 'My test title', 'subtitle' => '', 'slug' => 'my-test-title'));
  345. $this->assertEqual($result, $expected);
  346. $this->SlugArticle->data = array('SlugArticle' => array('title' => 'My test title', 'subtitle' => 'My subtitle'));
  347. $result = $Sluggable->beforeSave($this->SlugArticle);
  348. $this->assertTrue($result !== false);
  349. $result = $this->SlugArticle->data;
  350. $expected = array('SlugArticle' => array('title' => 'My test title', 'subtitle' => 'My subtitle', 'slug' => 'my-test-title-my-subtitle'));
  351. $this->assertEqual($result, $expected);
  352. $Sluggable->setup($this->SlugArticle, array('overwrite' => false));
  353. $this->SlugArticle->id = 1;
  354. $this->SlugArticle->data = array('SlugArticle' => array('title' => 'New First Article'));
  355. $result = $Sluggable->beforeSave($this->SlugArticle);
  356. $this->assertTrue($result !== false);
  357. $result = $this->SlugArticle->data;
  358. $expected = array('SlugArticle' => array('title' => 'New First Article'));
  359. $this->assertEqual($result, $expected);
  360. $this->SlugArticle->id = null;
  361. $Sluggable->setup($this->SlugArticle, array('overwrite' => true));
  362. $this->SlugArticle->id = 1;
  363. $this->SlugArticle->data = array('SlugArticle' => array('title' => 'New First Article'));
  364. $result = $Sluggable->beforeSave($this->SlugArticle);
  365. $this->assertTrue($result !== false);
  366. $result = $this->SlugArticle->data;
  367. $expected = array('SlugArticle' => array('title' => 'New First Article', 'slug' => 'new-first-article'));
  368. $this->assertEqual($result, $expected);
  369. $this->SlugArticle->id = null;
  370. unset($Sluggable);
  371. }
  372. /**
  373. * Test slug in save operations
  374. *
  375. * @access public
  376. */
  377. function testSave()
  378. {
  379. $data = array('SlugArticle' => array('title' => 'New Article', 'subtitle' => '', 'body' => 'New Body 1'));
  380. $result = $this->SlugArticle->create();
  381. $this->assertTrue($result !== false);
  382. $result = $this->SlugArticle->save($data);
  383. $this->assertTrue($result !== false);
  384. $result = $this->SlugArticle->read(array('slug', 'title'), 4);
  385. $expected = array('SlugArticle' => array('slug' => 'new-article', 'title' => 'New Article'));
  386. $data = array('SlugArticle' => array('title' => 'New Article', 'subtitle' => 'Second Version', 'body' => 'New Body 2'));
  387. $result = $this->SlugArticle->create();
  388. $this->assertTrue($result !== false);
  389. $result = $this->SlugArticle->save($data);
  390. $this->assertTrue($result !== false);
  391. $result = $this->SlugArticle->read(array('slug', 'title'), 5);
  392. $expected = array('SlugArticle' => array('slug' => 'new-article-1', 'title' => 'New Article'));
  393. $data = array('SlugArticle' => array('title' => 'New Article', 'subtitle' => 'Third Version', 'body' => 'New Body 3'));
  394. $result = $this->SlugArticle->create();
  395. $this->assertTrue($result !== false);
  396. $result = $this->SlugArticle->save($data);
  397. $this->assertTrue($result !== false);
  398. $result = $this->SlugArticle->read(array('slug', 'title'), 6);
  399. $expected = array('SlugArticle' => array('slug' => 'new-article-2', 'title' => 'New Article'));
  400. $data = array('SlugArticle' => array('title' => 'Brand New Article', 'subtitle' => '', 'body' => 'Brand New Body'));
  401. $result = $this->SlugArticle->create();
  402. $this->assertTrue($result !== false);
  403. $result = $this->SlugArticle->save($data);
  404. $this->assertTrue($result !== false);
  405. $result = $this->SlugArticle->read(array('slug', 'title'), 7);
  406. $expected = array('SlugArticle' => array('slug' => 'brand-new-article', 'title' => 'Brand New Article'));
  407. $data = array('SlugArticle' => array('id' => 2, 'title' => 'New Title for Second Article'));
  408. $result = $this->SlugArticle->create();
  409. $this->assertTrue($result !== false);
  410. $result = $this->SlugArticle->save($data);
  411. $this->assertTrue($result !== false);
  412. $result = $this->SlugArticle->read(array('slug', 'title'), 2);
  413. $expected = array('SlugArticle' => array('slug' => 'second-article', 'title' => 'New Title for Second Article'));
  414. $data = array('SlugArticle' => array('title' => 'Article with whitelist', 'body' => 'Brand New Body'));
  415. $this->assertTrue($result !== false);
  416. $result = $this->SlugArticle->create();
  417. $this->assertTrue($result !== false);
  418. $result = $this->SlugArticle->save($data, true, array('title', 'body'));
  419. $this->assertTrue($result !== false);
  420. $result = $this->SlugArticle->read(array('slug', 'title'), 8);
  421. $expected = array('SlugArticle' => array('slug' => 'article-with-whitelist', 'title' => 'Article with whitelist'));
  422. }
  423. /**
  424. * Test slug integrity when using saveField
  425. *
  426. * @access public
  427. */
  428. function testSaveField()
  429. {
  430. $expected = 'New body for first article';
  431. $this->SlugArticle->id = 1;
  432. $result = $this->SlugArticle->saveField('body', $expected);
  433. $this->assertTrue($result);
  434. $result = $this->SlugArticle->field('body');
  435. $this->assertEqual($result, $expected);
  436. $result = $this->SlugArticle->field('title');
  437. $expected = 'First Article';
  438. $this->assertEqual($result, $expected);
  439. $result = $this->SlugArticle->field('slug');
  440. $expected = 'first-article';
  441. $this->assertEqual($result, $expected);
  442. $expected = 'New title for first article';
  443. $this->SlugArticle->id = 1;
  444. $result = $this->SlugArticle->saveField('title', $expected);
  445. $this->assertTrue($result);
  446. $result = $this->SlugArticle->field('title');
  447. $this->assertEqual($result, $expected);
  448. $result = $this->SlugArticle->field('slug');
  449. $expected = 'first-article';
  450. $this->assertEqual($result, $expected);
  451. }
  452. }
  453. ?>