PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Test/Case/Model/Behavior/SluggableTest.php

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