PageRenderTime 63ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 1ms

/test/testsuite/generator/behavior/sluggable/SluggableBehaviorTest.php

https://github.com/nextbigsound/propel-orm
PHP | 299 lines | 250 code | 28 blank | 21 comment | 0 complexity | c38daca637d7e2521145a3f6027f662f MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: TimestampableBehaviorTest.php 1460 2010-01-17 22:36:48Z francois $
  4. * This file is part of the Propel package.
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @license MIT License
  9. */
  10. require_once 'tools/helpers/bookstore/BookstoreTestBase.php';
  11. /**
  12. * Tests for SluggableBehavior class
  13. *
  14. * @author François Zaninotto
  15. * @version $Revision$
  16. * @package generator.behavior.sluggable
  17. */
  18. class SluggableBehaviorTest extends BookstoreTestBase
  19. {
  20. public function testParameters()
  21. {
  22. $table13 = Table13Peer::getTableMap();
  23. $this->assertEquals(count($table13->getColumns()), 3, 'Sluggable adds one columns by default');
  24. $this->assertTrue(method_exists('Table13', 'getSlug'), 'Sluggable adds a slug column by default');
  25. $table14 = Table14Peer::getTableMap();
  26. $this->assertEquals(count($table14->getColumns()), 3, 'Sluggable does not add a column when it already exists');
  27. $this->assertTrue(method_exists('Table14', 'getUrl'), 'Sluggable allows customization of slug_column name');
  28. $this->assertTrue(method_exists('Table14', 'getSlug'), 'Sluggable adds a standard getter for the slug column');
  29. }
  30. public function testObjectGetter()
  31. {
  32. $this->assertTrue(method_exists('Table13', 'getSlug'), 'Sluggable adds a getter for the slug column');
  33. $t = new Table13();
  34. $t->setSlug('foo');
  35. $this->assertEquals('foo', $t->getSlug(), 'getSlug() returns the object slug');
  36. $this->assertTrue(method_exists('Table14', 'getSlug'), 'Sluggable adds a getter for the slug column, even if the column does not have the default name');
  37. $t = new Table14();
  38. $t->setUrl('foo');
  39. $this->assertEquals('foo', $t->getSlug(), 'getSlug() returns the object slug');
  40. }
  41. public function testObjectSetter()
  42. {
  43. $this->assertTrue(method_exists('Table13', 'setSlug'), 'Sluggable adds a setter for the slug column');
  44. $t = new Table13();
  45. $t->setSlug('foo');
  46. $this->assertEquals('foo', $t->getSlug(), 'setSlug() sets the object slug');
  47. $this->assertTrue(method_exists('Table14', 'setSlug'), 'Sluggable adds a setter for the slug column, even if the column does not have the default name');
  48. $t = new Table14();
  49. $t->setSlug('foo');
  50. $this->assertEquals('foo', $t->getUrl(), 'setSlug() sets the object slug');
  51. }
  52. public function testObjectCreateRawSlug()
  53. {
  54. $t = new TestableTable13();
  55. $this->assertEquals('n-a', $t->createRawSlug(), 'createRawSlug() returns an empty string for an empty object with no pattern');
  56. $t->setTitle('Hello, World');
  57. $this->assertEquals('hello-world', $t->createRawSlug(), 'createRawSlug() returns the cleaned up object string representation by default');
  58. $t = new TestableTable14();
  59. $this->assertEquals('/foo/n-a/bar', $t->createRawSlug(), 'createRawSlug() returns a slug for an empty object with a pattern');
  60. $t->setTitle('Hello, World');
  61. $this->assertEquals('/foo/hello-world/bar', $t->createRawSlug(), 'createRawSlug() returns a slug based on a pattern');
  62. }
  63. public static function cleanupSlugProvider()
  64. {
  65. return array(
  66. array('', 'n-a'),
  67. array('foo', 'foo'),
  68. array('foo bar', 'foo-bar'),
  69. array('foo bar', 'foo-bar'),
  70. array('FoO', 'foo'),
  71. array('fôo', 'foo'),
  72. array(' foo ', 'foo'),
  73. array('f/o:o', 'f-o-o'),
  74. array('foo1', 'foo1'),
  75. );
  76. }
  77. /**
  78. * @dataProvider cleanupSlugProvider
  79. */
  80. public function testObjectCleanupSlugPart($in, $out)
  81. {
  82. $t = new TestableTable13();
  83. $this->assertEquals($out, $t->cleanupSlugPart($in), 'cleanupSlugPart() cleans up the slug part');
  84. }
  85. public static function limitSlugSizeProvider()
  86. {
  87. return array(
  88. array('123', '123'),
  89. array(str_repeat('*', 80), str_repeat('*', 80)),
  90. array(str_repeat('*', 97), str_repeat('*', 97)),
  91. array(str_repeat('*', 98), str_repeat('*', 97)),
  92. array(str_repeat('*', 99), str_repeat('*', 97)),
  93. array(str_repeat('*', 100), str_repeat('*', 97)),
  94. array(str_repeat('*', 150), str_repeat('*', 97)),
  95. );
  96. }
  97. /**
  98. * @dataProvider limitSlugSizeProvider
  99. */
  100. public function testObjectLimitSlugSize($in, $out)
  101. {
  102. $t = new TestableTable14();
  103. $this->assertEquals($out, $t->limitSlugSize($in), 'limitSlugsize() limits the slug size');
  104. }
  105. public function testObjectMakeSlugUnique()
  106. {
  107. Table13Query::create()->deleteAll();
  108. $t = new TestableTable13();
  109. $this->assertEquals('', $t->makeSlugUnique(''), 'makeSlugUnique() returns the input slug when the input is empty');
  110. $this->assertEquals('foo', $t->makeSlugUnique('foo'), 'makeSlugUnique() returns the input slug when the table is empty');
  111. $t->setSlug('foo');
  112. $t->save();
  113. $t = new TestableTable13();
  114. $this->assertEquals('bar', $t->makeSlugUnique('bar'), 'makeSlugUnique() returns the input slug when the table does not contain a similar slug');
  115. $t->save();
  116. $t = new TestableTable13();
  117. $this->assertEquals('foo-1', $t->makeSlugUnique('foo'), 'makeSlugUnique() returns an incremented input when it already exists');
  118. $t->setSlug('foo-1');
  119. $t->save();
  120. $t = new TestableTable13();
  121. $this->assertEquals('foo-2', $t->makeSlugUnique('foo'), 'makeSlugUnique() returns an incremented input when it already exists');
  122. }
  123. public function testObjectCreateSlug()
  124. {
  125. Table13Query::create()->deleteAll();
  126. $t = new TestableTable13();
  127. $this->assertEquals('n-a', $t->createSlug(), 'createSlug() returns n-a for an empty object');
  128. $t->setTitle('Hello, World!');
  129. $this->assertEquals('hello-world', $t->createSlug(), 'createSlug() returns a cleaned up slug');
  130. $t->setSlug('hello-world');
  131. $t->save();
  132. $t = new TestableTable13();
  133. $t->setTitle('Hello; wOrld');
  134. $this->assertEquals('hello-world-1', $t->createSlug(), 'createSlug() returns a unique slug');
  135. Table14Query::create()->deleteAll();
  136. $t = new TestableTable14();
  137. $this->assertEquals('/foo/n-a/bar', $t->createSlug(), 'createSlug() returns a slug for an empty object with a pattern');
  138. $t->setTitle('Hello, World!');
  139. $this->assertEquals('/foo/hello-world/bar', $t->createSlug(), 'createSlug() returns a cleaned up slug');
  140. $t->setSlug('/foo/hello-world/bar');
  141. $t->save();
  142. $t = new TestableTable14();
  143. $t->setTitle('Hello; wOrld:');
  144. $this->assertEquals('/foo/hello-world/bar/1', $t->createSlug(), 'createSlug() returns a unique slug');
  145. }
  146. public function testObjectPreSave()
  147. {
  148. Table14Query::create()->deleteAll();
  149. $t = new Table14();
  150. $t->save();
  151. $this->assertEquals('/foo/n-a/bar', $t->getSlug(), 'preSave() sets a default slug for empty objects');
  152. $t = new Table14();
  153. $t->setTitle('Hello, World');
  154. $t->save();
  155. $this->assertEquals('/foo/hello-world/bar', $t->getSlug(), 'preSave() sets a cleanued up slug for objects');
  156. $t = new Table14();
  157. $t->setTitle('Hello, World');
  158. $t->save();
  159. $this->assertEquals('/foo/hello-world/bar/1', $t->getSlug(), 'preSave() sets a unique slug for objects');
  160. $t = new Table14();
  161. $t->setTitle('Hello, World');
  162. $t->setSlug('/foo/custom/bar');
  163. $t->save();
  164. $this->assertEquals('/foo/custom/bar', $t->getSlug(), 'preSave() uses the given slug if it exists');
  165. $t = new Table14();
  166. $t->setTitle('Hello, World');
  167. $t->setSlug('/foo/custom/bar');
  168. $t->save();
  169. $this->assertEquals('/foo/custom/bar/1', $t->getSlug(), 'preSave() uses the given slug if it exists and makes it unique');
  170. }
  171. public function testObjectSlugLifecycle()
  172. {
  173. Table13Query::create()->deleteAll();
  174. $t = new Table13();
  175. $t->setTitle('Hello, World');
  176. $t->save();
  177. $this->assertEquals('hello-world', $t->getSlug(), 'preSave() creates a slug for new objects');
  178. $t->setSlug('hello-bar');
  179. $t->save();
  180. $this->assertEquals('hello-bar', $t->getSlug(), 'setSlug() allows to override default slug');
  181. $t->setSlug('');
  182. $t->save();
  183. $this->assertEquals('hello-world', $t->getSlug(), 'setSlug(null) relaunches the slug generation');
  184. Table14Query::create()->deleteAll();
  185. $t = new Table14();
  186. $t->setTitle('Hello, World2');
  187. $t->setSlug('hello-bar2');
  188. $t->save();
  189. $this->assertEquals('hello-bar2', $t->getSlug(), 'setSlug() allows to override default slug, even before save');
  190. $t->setSlug('');
  191. $t->save();
  192. $this->assertEquals('/foo/hello-world2/bar', $t->getSlug(), 'setSlug(null) relaunches the slug generation');
  193. }
  194. public function testObjectSlugAutoUpdate()
  195. {
  196. Table13Query::create()->deleteAll();
  197. $t = new Table13();
  198. $t->setTitle('Hello, World');
  199. $t->save();
  200. $this->assertEquals('hello-world', $t->getSlug(), 'preSave() creates a slug for new objects');
  201. $t->setTitle('Hello, My World');
  202. $t->save();
  203. $this->assertEquals('hello-my-world', $t->getSlug(), 'preSave() autoupdates slug on object change');
  204. $t->setTitle('Hello, My Whole New World');
  205. $t->setSlug('hello-bar');
  206. $t->save();
  207. $this->assertEquals('hello-bar', $t->getSlug(), 'preSave() does not autoupdate slug when it was set by the user');
  208. }
  209. public function testObjectSlugAutoUpdatePermanent()
  210. {
  211. Table14Query::create()->deleteAll();
  212. $t = new Table14();
  213. $t->setTitle('Hello, World');
  214. $t->save();
  215. $this->assertEquals('/foo/hello-world/bar', $t->getSlug(), 'preSave() creates a slug for new objects');
  216. $t->setTitle('Hello, My World');
  217. $t->save();
  218. $this->assertEquals('/foo/hello-world/bar', $t->getSlug(), 'preSave() does not autoupdate slug on object change for permanent slugs');
  219. $t->setSlug('hello-bar');
  220. $t->save();
  221. $this->assertEquals('hello-bar', $t->getSlug(), 'setSlug() still works for permanent slugs');
  222. }
  223. public function testQueryFindOneBySlug()
  224. {
  225. $this->assertTrue(method_exists('Table13Query', 'findOneBySlug'), 'The generated query provides a findOneBySlug() method');
  226. $this->assertTrue(method_exists('Table14Query', 'findOneBySlug'), 'The generated query provides a findOneBySlug() method even if the slug column doesnt have the default name');
  227. Table14Query::create()->deleteAll();
  228. $t1 = new Table14();
  229. $t1->setTitle('Hello, World');
  230. $t1->save();
  231. $t2 = new Table14();
  232. $t2->setTitle('Hello, Cruel World');
  233. $t2->save();
  234. $t = Table14Query::create()->findOneBySlug('/foo/hello-world/bar');
  235. $this->assertEquals($t1, $t, 'findOneBySlug() returns a single object matching the slug');
  236. }
  237. }
  238. class TestableTable13 extends Table13
  239. {
  240. public function createSlug()
  241. {
  242. return parent::createSlug();
  243. }
  244. public function createRawSlug()
  245. {
  246. return parent::createRawSlug();
  247. }
  248. public static function cleanupSlugPart($slug, $separator = '-')
  249. {
  250. return parent::cleanupSlugPart($slug, $separator);
  251. }
  252. public function makeSlugUnique($slug, $separator = '-', $increment = 0)
  253. {
  254. return parent::makeSlugUnique($slug, $separator, $increment);
  255. }
  256. }
  257. class TestableTable14 extends Table14
  258. {
  259. public function createSlug()
  260. {
  261. return parent::createSlug();
  262. }
  263. public function createRawSlug()
  264. {
  265. return parent::createRawSlug();
  266. }
  267. public static function limitSlugSize($slug, $incrementReservedSpace = 3)
  268. {
  269. return parent::limitSlugSize($slug, $incrementReservedSpace);
  270. }
  271. }