PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/propel_16/vendor/propel/test/testsuite/generator/behavior/sluggable/SluggableBehaviorTest.php

http://github.com/eventhorizonpl/forked-php-orm-benchmark
PHP | 427 lines | 349 code | 58 blank | 20 comment | 2 complexity | cde6098acbf20944c405fe21a3671ee9 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. require_once dirname(__FILE__) . '/../../../../tools/helpers/bookstore/BookstoreTestBase.php';
  10. /**
  11. * Tests for SluggableBehavior class
  12. *
  13. * @author Fran?ois Zaninotto
  14. * @version $Revision$
  15. * @package generator.behavior.sluggable
  16. */
  17. class SluggableBehaviorTest extends BookstoreTestBase
  18. {
  19. public function testParameters()
  20. {
  21. $table13 = Table13Peer::getTableMap();
  22. $this->assertEquals(count($table13->getColumns()), 3, 'Sluggable adds one columns by default');
  23. $this->assertTrue(method_exists('Table13', 'getSlug'), 'Sluggable adds a slug column by default');
  24. $table14 = Table14Peer::getTableMap();
  25. $this->assertEquals(count($table14->getColumns()), 3, 'Sluggable does not add a column when it already exists');
  26. $this->assertTrue(method_exists('Table14', 'getUrl'), 'Sluggable allows customization of slug_column name');
  27. $this->assertTrue(method_exists('Table14', 'getSlug'), 'Sluggable adds a standard getter for the slug column');
  28. }
  29. public function testObjectGetter()
  30. {
  31. $this->assertTrue(method_exists('Table13', 'getSlug'), 'Sluggable adds a getter for the slug column');
  32. $t = new Table13();
  33. $t->setSlug('foo');
  34. $this->assertEquals('foo', $t->getSlug(), 'getSlug() returns the object slug');
  35. $this->assertTrue(method_exists('Table14', 'getSlug'), 'Sluggable adds a getter for the slug column, even if the column does not have the default name');
  36. $t = new Table14();
  37. $t->setUrl('foo');
  38. $this->assertEquals('foo', $t->getSlug(), 'getSlug() returns the object slug');
  39. }
  40. public function testObjectSetter()
  41. {
  42. $this->assertTrue(method_exists('Table13', 'setSlug'), 'Sluggable adds a setter for the slug column');
  43. $t = new Table13();
  44. $t->setSlug('foo');
  45. $this->assertEquals('foo', $t->getSlug(), 'setSlug() sets the object slug');
  46. $this->assertTrue(method_exists('Table14', 'setSlug'), 'Sluggable adds a setter for the slug column, even if the column does not have the default name');
  47. $t = new Table14();
  48. $t->setSlug('foo');
  49. $this->assertEquals('foo', $t->getUrl(), 'setSlug() sets the object slug');
  50. }
  51. public function testObjectCreateRawSlug()
  52. {
  53. $t = new TestableTable13();
  54. $this->assertEquals('n-a', $t->createRawSlug(), 'createRawSlug() returns an empty string for an empty object with no pattern');
  55. $t->setTitle('Hello, World');
  56. $this->assertEquals('hello-world', $t->createRawSlug(), 'createRawSlug() returns the cleaned up object string representation by default');
  57. $t = new TestableTable14();
  58. $this->assertEquals('/foo/n-a/bar', $t->createRawSlug(), 'createRawSlug() returns a slug for an empty object with a pattern');
  59. $t->setTitle('Hello, World');
  60. $this->assertEquals('/foo/hello-world/bar', $t->createRawSlug(), 'createRawSlug() returns a slug based on a pattern');
  61. }
  62. public static function cleanupSlugProvider()
  63. {
  64. return array(
  65. array('', 'n-a'),
  66. array('foo', 'foo'),
  67. array('foo bar', 'foo-bar'),
  68. array('foo bar', 'foo-bar'),
  69. array('FoO', 'foo'),
  70. array('f?o', 'foo'),
  71. array(' foo ', 'foo'),
  72. array('f/o:o', 'f-o-o'),
  73. array('foo1', 'foo1'),
  74. );
  75. }
  76. /**
  77. * @dataProvider cleanupSlugProvider
  78. */
  79. public function testObjectCleanupSlugPart($in, $out)
  80. {
  81. $t = new TestableTable13();
  82. $this->assertEquals($out, $t->cleanupSlugPart($in), 'cleanupSlugPart() cleans up the slug part');
  83. }
  84. public static function limitSlugSizeProvider()
  85. {
  86. return array(
  87. array('123', '123'),
  88. array(str_repeat('*', 80), str_repeat('*', 80)),
  89. array(str_repeat('*', 97), str_repeat('*', 97)),
  90. array(str_repeat('*', 98), str_repeat('*', 97)),
  91. array(str_repeat('*', 99), str_repeat('*', 97)),
  92. array(str_repeat('*', 100), str_repeat('*', 97)),
  93. array(str_repeat('*', 150), str_repeat('*', 97)),
  94. );
  95. }
  96. /**
  97. * @dataProvider limitSlugSizeProvider
  98. */
  99. public function testObjectLimitSlugSize($in, $out)
  100. {
  101. $t = new TestableTable14();
  102. $this->assertEquals($out, $t->limitSlugSize($in), 'limitSlugsize() limits the slug size');
  103. }
  104. public function testObjectMakeSlugUnique()
  105. {
  106. Table13Query::create()->deleteAll();
  107. $t = new TestableTable13();
  108. $this->assertEquals('', $t->makeSlugUnique(''), 'makeSlugUnique() returns the input slug when the input is empty');
  109. $this->assertEquals('foo', $t->makeSlugUnique('foo'), 'makeSlugUnique() returns the input slug when the table is empty');
  110. $t->setSlug('foo');
  111. $t->save();
  112. $t = new TestableTable13();
  113. $this->assertEquals('bar', $t->makeSlugUnique('bar'), 'makeSlugUnique() returns the input slug when the table does not contain a similar slug');
  114. $t->save();
  115. $t = new TestableTable13();
  116. $this->assertEquals('foo-1', $t->makeSlugUnique('foo'), 'makeSlugUnique() returns an incremented input when it already exists');
  117. $t->setSlug('foo-1');
  118. $t->save();
  119. $t = new TestableTable13();
  120. $this->assertEquals('foo-2', $t->makeSlugUnique('foo'), 'makeSlugUnique() returns an incremented input when it already exists');
  121. }
  122. public function testObjectCreateSlug()
  123. {
  124. Table13Query::create()->deleteAll();
  125. $t = new TestableTable13();
  126. $this->assertEquals('n-a', $t->createSlug(), 'createSlug() returns n-a for an empty object');
  127. $t->setTitle('Hello, World!');
  128. $this->assertEquals('hello-world', $t->createSlug(), 'createSlug() returns a cleaned up slug');
  129. $t->setSlug('hello-world');
  130. $t->save();
  131. $t = new TestableTable13();
  132. $t->setTitle('Hello; wOrld');
  133. $this->assertEquals('hello-world-1', $t->createSlug(), 'createSlug() returns a unique slug');
  134. Table14Query::create()->deleteAll();
  135. $t = new TestableTable14();
  136. $this->assertEquals('/foo/n-a/bar', $t->createSlug(), 'createSlug() returns a slug for an empty object with a pattern');
  137. $t->setTitle('Hello, World!');
  138. $this->assertEquals('/foo/hello-world/bar', $t->createSlug(), 'createSlug() returns a cleaned up slug');
  139. $t->setSlug('/foo/hello-world/bar');
  140. $t->save();
  141. $t = new TestableTable14();
  142. $t->setTitle('Hello; wOrld:');
  143. $this->assertEquals('/foo/hello-world/bar/1', $t->createSlug(), 'createSlug() returns a unique slug');
  144. }
  145. public function testObjectPreSave()
  146. {
  147. Table14Query::create()->deleteAll();
  148. $t = new Table14();
  149. $t->save();
  150. $this->assertEquals('/foo/n-a/bar', $t->getSlug(), 'preSave() sets a default slug for empty objects');
  151. $t = new Table14();
  152. $t->setTitle('Hello, World');
  153. $t->save();
  154. $this->assertEquals('/foo/hello-world/bar', $t->getSlug(), 'preSave() sets a cleanued up slug for objects');
  155. $t = new Table14();
  156. $t->setTitle('Hello, World');
  157. $t->save();
  158. $this->assertEquals('/foo/hello-world/bar/1', $t->getSlug(), 'preSave() sets a unique slug for objects');
  159. $t = new Table14();
  160. $t->setTitle('Hello, World');
  161. $t->setSlug('/foo/custom/bar');
  162. $t->save();
  163. $this->assertEquals('/foo/custom/bar', $t->getSlug(), 'preSave() uses the given slug if it exists');
  164. $t = new Table14();
  165. $t->setTitle('Hello, World');
  166. $t->setSlug('/foo/custom/bar');
  167. $t->save();
  168. $this->assertEquals('/foo/custom/bar/1', $t->getSlug(), 'preSave() uses the given slug if it exists and makes it unique');
  169. }
  170. public function testObjectSlugLifecycle()
  171. {
  172. Table13Query::create()->deleteAll();
  173. $t = new Table13();
  174. $t->setTitle('Hello, World');
  175. $t->save();
  176. $this->assertEquals('hello-world', $t->getSlug(), 'preSave() creates a slug for new objects');
  177. $t->setSlug('hello-bar');
  178. $t->save();
  179. $this->assertEquals('hello-bar', $t->getSlug(), 'setSlug() allows to override default slug');
  180. $t->setSlug('');
  181. $t->save();
  182. $this->assertEquals('hello-world', $t->getSlug(), 'setSlug(null) relaunches the slug generation');
  183. Table14Query::create()->deleteAll();
  184. $t = new Table14();
  185. $t->setTitle('Hello, World2');
  186. $t->setSlug('hello-bar2');
  187. $t->save();
  188. $this->assertEquals('hello-bar2', $t->getSlug(), 'setSlug() allows to override default slug, even before save');
  189. $t->setSlug('');
  190. $t->save();
  191. $this->assertEquals('/foo/hello-world2/bar', $t->getSlug(), 'setSlug(null) relaunches the slug generation');
  192. }
  193. public function testObjectSlugAutoUpdate()
  194. {
  195. Table13Query::create()->deleteAll();
  196. $t = new Table13();
  197. $t->setTitle('Hello, World');
  198. $t->save();
  199. $this->assertEquals('hello-world', $t->getSlug(), 'preSave() creates a slug for new objects');
  200. $t->setTitle('Hello, My World');
  201. $t->save();
  202. $this->assertEquals('hello-my-world', $t->getSlug(), 'preSave() autoupdates slug on object change');
  203. $t->setTitle('Hello, My Whole New World');
  204. $t->setSlug('hello-bar');
  205. $t->save();
  206. $this->assertEquals('hello-bar', $t->getSlug(), 'preSave() does not autoupdate slug when it was set by the user');
  207. }
  208. public function testObjectSlugAutoUpdatePermanent()
  209. {
  210. Table14Query::create()->deleteAll();
  211. $t = new Table14();
  212. $t->setTitle('Hello, World');
  213. $t->save();
  214. $this->assertEquals('/foo/hello-world/bar', $t->getSlug(), 'preSave() creates a slug for new objects');
  215. $t->setTitle('Hello, My World');
  216. $t->save();
  217. $this->assertEquals('/foo/hello-world/bar', $t->getSlug(), 'preSave() does not autoupdate slug on object change for permanent slugs');
  218. $t->setSlug('hello-bar');
  219. $t->save();
  220. $this->assertEquals('hello-bar', $t->getSlug(), 'setSlug() still works for permanent slugs');
  221. }
  222. public function testQueryFindOneBySlug()
  223. {
  224. $this->assertTrue(method_exists('Table13Query', 'findOneBySlug'), 'The generated query provides a findOneBySlug() method');
  225. $this->assertTrue(method_exists('Table14Query', 'findOneBySlug'), 'The generated query provides a findOneBySlug() method even if the slug column doesnt have the default name');
  226. Table14Query::create()->deleteAll();
  227. $t1 = new Table14();
  228. $t1->setTitle('Hello, World');
  229. $t1->save();
  230. $t2 = new Table14();
  231. $t2->setTitle('Hello, Cruel World');
  232. $t2->save();
  233. $t = Table14Query::create()->findOneBySlug('/foo/hello-world/bar');
  234. $this->assertEquals($t1, $t, 'findOneBySlug() returns a single object matching the slug');
  235. }
  236. public function testUniqueViolationWithoutScope()
  237. {
  238. TableWithScopeQuery::create()->deleteAll();
  239. $t = new TableWithScope();
  240. $t->setTitle('Hello, World');
  241. $t->save();
  242. $this->assertEquals('hello-world', $t->getSlug());
  243. try {
  244. $t = new TableWithScope();
  245. $t->setTitle('Hello, World');
  246. $t->save();
  247. $this->fail('Exception expected');
  248. } catch (Exception $e) {
  249. $this->assertTrue(true, 'Exception successfully thrown');
  250. }
  251. }
  252. public function testNoUniqueViolationWithScope()
  253. {
  254. TableWithScopeQuery::create()->deleteAll();
  255. $t = new TableWithScope();
  256. $t->setTitle('Hello, World');
  257. $t->save();
  258. $this->assertEquals('hello-world', $t->getSlug());
  259. try {
  260. $t = new TableWithScope();
  261. $t->setTitle('Hello, World');
  262. $t->setScope(1);
  263. $t->save();
  264. $this->assertEquals('hello-world', $t->getSlug());
  265. } catch (Exception $e) {
  266. $this->fail($e->getMessage());
  267. }
  268. }
  269. public function testPatternNonPermanent()
  270. {
  271. Table14PatternQuery::create()->deleteAll();
  272. $t = new Table14Pattern();
  273. $t->setTitle('Hello, World');
  274. $t->setTags('test,tag,php');
  275. $t->save();
  276. $this->assertEquals('/foo/hello-world/bar/test-tag-php', $t->getUrl());
  277. $t->setTags('php,propel');
  278. $t->save();
  279. $this->assertEquals('/foo/hello-world/bar/php-propel', $t->getUrl());
  280. $t->setTitle('Title2');
  281. $t->save();
  282. $this->assertEquals('/foo/title2/bar/php-propel', $t->getUrl());
  283. }
  284. public function testNumberOfQueriesForMakeUniqSlug()
  285. {
  286. Table13Query::create()->deleteAll();
  287. $con = Propel::getConnection(Table13Peer::DATABASE_NAME);
  288. for ($i=0; $i < 5; $i++) {
  289. $nbQuery = $con->getQueryCount();
  290. $t = new Table13();
  291. $t->setTitle('Hello, World');
  292. $t->save($con);
  293. $this->assertLessThanOrEqual(4, $con->getQueryCount() - $nbQuery, 'no more than 4 query to get a slug when it already exist');
  294. }
  295. }
  296. public function testSlugRegexp()
  297. {
  298. Table13Query::create()->deleteAll();
  299. $con = Propel::getConnection(Table13Peer::DATABASE_NAME);
  300. for ($i=0; $i < 3; $i++) {
  301. $t = new Table13();
  302. $t->setTitle('Hello, World');
  303. $t->save($con);
  304. }
  305. $this->assertEquals('hello-world-2', $t->getSlug());
  306. $t = new Table13();
  307. $t->setTitle('World');
  308. $t->save($con);
  309. $this->assertEquals('world', $t->getSlug());
  310. $t = new Table13();
  311. $t->setTitle('World');
  312. $t->save($con);
  313. $this->assertEquals('world-1', $t->getSlug());
  314. $t = new Table13();
  315. $t->setTitle('Hello, World');
  316. $t->save($con);
  317. $this->assertEquals('hello-world-3', $t->getSlug());
  318. $t = new Table13();
  319. $t->setTitle('World');
  320. $t->save($con);
  321. $this->assertEquals('world-2', $t->getSlug());
  322. $t = new Table13();
  323. $t->setTitle('World 000');
  324. $t->save($con);
  325. $this->assertEquals('world-000', $t->getSlug());
  326. $t = new Table13();
  327. $t->setTitle('World');
  328. $t->save($con);
  329. $this->assertEquals('world-101', $t->getSlug());
  330. $t = new Table13();
  331. $t->setTitle('World');
  332. $t->save($con);
  333. $this->assertEquals('world-102', $t->getSlug());
  334. }
  335. }
  336. class TestableTable13 extends Table13
  337. {
  338. public function createSlug()
  339. {
  340. return parent::createSlug();
  341. }
  342. public function createRawSlug()
  343. {
  344. return parent::createRawSlug();
  345. }
  346. public static function cleanupSlugPart($slug, $separator = '-')
  347. {
  348. return parent::cleanupSlugPart($slug, $separator);
  349. }
  350. public function makeSlugUnique($slug, $separator = '-', $increment = 0)
  351. {
  352. return parent::makeSlugUnique($slug, $separator, $increment);
  353. }
  354. }
  355. class TestableTable14 extends Table14
  356. {
  357. public function createSlug()
  358. {
  359. return parent::createSlug();
  360. }
  361. public function createRawSlug()
  362. {
  363. return parent::createRawSlug();
  364. }
  365. public static function limitSlugSize($slug, $incrementReservedSpace = 3)
  366. {
  367. return parent::limitSlugSize($slug, $incrementReservedSpace);
  368. }
  369. }