PageRenderTime 17ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

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