propel-orm /test/testsuite/generator/behavior/sluggable/SluggableBehaviorTest.php

Language PHP Lines 299
MD5 Hash cace7261b16301ba2c1f674695f43901
Repository https://github.com/nextbigsound/propel-orm.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php

/*
 *	$Id: TimestampableBehaviorTest.php 1460 2010-01-17 22:36:48Z francois $
 * This file is part of the Propel package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @license    MIT License
 */

require_once 'tools/helpers/bookstore/BookstoreTestBase.php';

/**
 * Tests for SluggableBehavior class
 *
 * @author     Fran莽ois Zaninotto
 * @version    $Revision$
 * @package    generator.behavior.sluggable
 */
class SluggableBehaviorTest extends BookstoreTestBase
{
	public function testParameters()
	{
		$table13 = Table13Peer::getTableMap();
		$this->assertEquals(count($table13->getColumns()), 3, 'Sluggable adds one columns by default');
		$this->assertTrue(method_exists('Table13', 'getSlug'), 'Sluggable adds a slug column by default');
		$table14 = Table14Peer::getTableMap();
		$this->assertEquals(count($table14->getColumns()), 3, 'Sluggable does not add a column when it already exists');
		$this->assertTrue(method_exists('Table14', 'getUrl'), 'Sluggable allows customization of slug_column name');
		$this->assertTrue(method_exists('Table14', 'getSlug'), 'Sluggable adds a standard getter for the slug column');
	}
	
	public function testObjectGetter()
	{
		$this->assertTrue(method_exists('Table13', 'getSlug'), 'Sluggable adds a getter for the slug column');
		$t = new Table13();
		$t->setSlug('foo');
		$this->assertEquals('foo', $t->getSlug(), 'getSlug() returns the object slug');
		$this->assertTrue(method_exists('Table14', 'getSlug'), 'Sluggable adds a getter for the slug column, even if the column does not have the default name');
		$t = new Table14();
		$t->setUrl('foo');
		$this->assertEquals('foo', $t->getSlug(), 'getSlug() returns the object slug');
	}

	public function testObjectSetter()
	{
		$this->assertTrue(method_exists('Table13', 'setSlug'), 'Sluggable adds a setter for the slug column');
		$t = new Table13();
		$t->setSlug('foo');
		$this->assertEquals('foo', $t->getSlug(), 'setSlug() sets the object slug');
		$this->assertTrue(method_exists('Table14', 'setSlug'), 'Sluggable adds a setter for the slug column, even if the column does not have the default name');
		$t = new Table14();
		$t->setSlug('foo');
		$this->assertEquals('foo', $t->getUrl(), 'setSlug() sets the object slug');
	}
	
	public function testObjectCreateRawSlug()
	{
		$t = new TestableTable13();
		$this->assertEquals('n-a', $t->createRawSlug(), 'createRawSlug() returns an empty string for an empty object with no pattern');
		$t->setTitle('Hello, World');
		$this->assertEquals('hello-world', $t->createRawSlug(), 'createRawSlug() returns the cleaned up object string representation by default');
		
		$t = new TestableTable14();
		$this->assertEquals('/foo/n-a/bar', $t->createRawSlug(), 'createRawSlug() returns a slug for an empty object with a pattern');
		$t->setTitle('Hello, World');
		$this->assertEquals('/foo/hello-world/bar', $t->createRawSlug(), 'createRawSlug() returns a slug based on a pattern');
	}
	
	public static function cleanupSlugProvider()
	{
		return array(
			array('', 'n-a'),
			array('foo', 'foo'),
			array('foo bar', 'foo-bar'),
			array('foo  bar', 'foo-bar'),
			array('FoO', 'foo'),
			array('f么o', 'foo'),
			array(' foo ', 'foo'),
			array('f/o:o', 'f-o-o'),
			array('foo1', 'foo1'),
		);
	}
	
	/**
	 * @dataProvider cleanupSlugProvider
	 */
	public function testObjectCleanupSlugPart($in, $out)
	{
		$t = new TestableTable13();
		$this->assertEquals($out, $t->cleanupSlugPart($in), 'cleanupSlugPart() cleans up the slug part');
	}

	public static function limitSlugSizeProvider()
	{
		return array(
			array('123', '123'),
			array(str_repeat('*', 80), str_repeat('*', 80)),
			array(str_repeat('*', 97), str_repeat('*', 97)),
			array(str_repeat('*', 98), str_repeat('*', 97)),
			array(str_repeat('*', 99), str_repeat('*', 97)),
			array(str_repeat('*', 100), str_repeat('*', 97)),
			array(str_repeat('*', 150), str_repeat('*', 97)),
		);
	}
	
	/**
	 * @dataProvider limitSlugSizeProvider
	 */
	public function testObjectLimitSlugSize($in, $out)
	{
		$t = new TestableTable14();
		$this->assertEquals($out, $t->limitSlugSize($in), 'limitSlugsize() limits the slug size');
	}
	
	public function testObjectMakeSlugUnique()
	{
		Table13Query::create()->deleteAll();
		$t = new TestableTable13();
		$this->assertEquals('', $t->makeSlugUnique(''), 'makeSlugUnique() returns the input slug when the input is empty');
		$this->assertEquals('foo', $t->makeSlugUnique('foo'), 'makeSlugUnique() returns the input slug when the table is empty');
		$t->setSlug('foo');
		$t->save();
		$t = new TestableTable13();
		$this->assertEquals('bar', $t->makeSlugUnique('bar'), 'makeSlugUnique() returns the input slug when the table does not contain a similar slug');
		$t->save();
		$t = new TestableTable13();
		$this->assertEquals('foo-1', $t->makeSlugUnique('foo'), 'makeSlugUnique() returns an incremented input when it already exists');
		$t->setSlug('foo-1');
		$t->save();
		$t = new TestableTable13();
		$this->assertEquals('foo-2', $t->makeSlugUnique('foo'), 'makeSlugUnique() returns an incremented input when it already exists');
	}
	
	public function testObjectCreateSlug()
	{
		Table13Query::create()->deleteAll();
		$t = new TestableTable13();
		$this->assertEquals('n-a', $t->createSlug(), 'createSlug() returns n-a for an empty object');
		$t->setTitle('Hello, World!');
		$this->assertEquals('hello-world', $t->createSlug(), 'createSlug() returns a cleaned up slug');
		$t->setSlug('hello-world');
		$t->save();
		$t = new TestableTable13();
		$t->setTitle('Hello; wOrld');
		$this->assertEquals('hello-world-1', $t->createSlug(), 'createSlug() returns a unique slug');

		Table14Query::create()->deleteAll();
		$t = new TestableTable14();
		$this->assertEquals('/foo/n-a/bar', $t->createSlug(), 'createSlug() returns a slug for an empty object with a pattern');
		$t->setTitle('Hello, World!');
		$this->assertEquals('/foo/hello-world/bar', $t->createSlug(), 'createSlug() returns a cleaned up slug');
		$t->setSlug('/foo/hello-world/bar');
		$t->save();
		$t = new TestableTable14();
		$t->setTitle('Hello; wOrld:');
		$this->assertEquals('/foo/hello-world/bar/1', $t->createSlug(), 'createSlug() returns a unique slug');
	}
	
	public function testObjectPreSave()
	{
		Table14Query::create()->deleteAll();
		$t = new Table14();
		$t->save();
		$this->assertEquals('/foo/n-a/bar', $t->getSlug(), 'preSave() sets a default slug for empty objects');
		$t = new Table14();
		$t->setTitle('Hello, World');
		$t->save();
		$this->assertEquals('/foo/hello-world/bar', $t->getSlug(), 'preSave() sets a cleanued up slug for objects');
		$t = new Table14();
		$t->setTitle('Hello, World');
		$t->save();
		$this->assertEquals('/foo/hello-world/bar/1', $t->getSlug(), 'preSave() sets a unique slug for objects');
		$t = new Table14();
		$t->setTitle('Hello, World');
		$t->setSlug('/foo/custom/bar');
		$t->save();
		$this->assertEquals('/foo/custom/bar', $t->getSlug(), 'preSave() uses the given slug if it exists');
		$t = new Table14();
		$t->setTitle('Hello, World');
		$t->setSlug('/foo/custom/bar');
		$t->save();
		$this->assertEquals('/foo/custom/bar/1', $t->getSlug(), 'preSave() uses the given slug if it exists and makes it unique');
	}
	
	public function testObjectSlugLifecycle()
	{
		Table13Query::create()->deleteAll();
		$t = new Table13();
		$t->setTitle('Hello, World');
		$t->save();
		$this->assertEquals('hello-world', $t->getSlug(), 'preSave() creates a slug for new objects');
		$t->setSlug('hello-bar');
		$t->save();
		$this->assertEquals('hello-bar', $t->getSlug(), 'setSlug() allows to override default slug');
		$t->setSlug('');
		$t->save();
		$this->assertEquals('hello-world', $t->getSlug(), 'setSlug(null) relaunches the slug generation');

		Table14Query::create()->deleteAll();
		$t = new Table14();
		$t->setTitle('Hello, World2');
		$t->setSlug('hello-bar2');
		$t->save();
		$this->assertEquals('hello-bar2', $t->getSlug(), 'setSlug() allows to override default slug, even before save');
		$t->setSlug('');
		$t->save();
		$this->assertEquals('/foo/hello-world2/bar', $t->getSlug(), 'setSlug(null) relaunches the slug generation');
	}
	
	public function testObjectSlugAutoUpdate()
	{
		Table13Query::create()->deleteAll();
		$t = new Table13();
		$t->setTitle('Hello, World');
		$t->save();
		$this->assertEquals('hello-world', $t->getSlug(), 'preSave() creates a slug for new objects');
		$t->setTitle('Hello, My World');
		$t->save();
		$this->assertEquals('hello-my-world', $t->getSlug(), 'preSave() autoupdates slug on object change');
		$t->setTitle('Hello, My Whole New World');
		$t->setSlug('hello-bar');
		$t->save();
		$this->assertEquals('hello-bar', $t->getSlug(), 'preSave() does not autoupdate slug when it was set by the user');
	}

	public function testObjectSlugAutoUpdatePermanent()
	{
		Table14Query::create()->deleteAll();
		$t = new Table14();
		$t->setTitle('Hello, World');
		$t->save();
		$this->assertEquals('/foo/hello-world/bar', $t->getSlug(), 'preSave() creates a slug for new objects');
		$t->setTitle('Hello, My World');
		$t->save();
		$this->assertEquals('/foo/hello-world/bar', $t->getSlug(), 'preSave() does not autoupdate slug on object change for permanent slugs');
		$t->setSlug('hello-bar');
		$t->save();
		$this->assertEquals('hello-bar', $t->getSlug(), 'setSlug() still works for permanent slugs');
	}

	public function testQueryFindOneBySlug()
	{
		$this->assertTrue(method_exists('Table13Query', 'findOneBySlug'), 'The generated query provides a findOneBySlug() method');
		$this->assertTrue(method_exists('Table14Query', 'findOneBySlug'), 'The generated query provides a findOneBySlug() method even if the slug column doesnt have the default name');
		
		Table14Query::create()->deleteAll();
		$t1 = new Table14();
		$t1->setTitle('Hello, World');
		$t1->save();
		$t2 = new Table14();
		$t2->setTitle('Hello, Cruel World');
		$t2->save();
		$t = Table14Query::create()->findOneBySlug('/foo/hello-world/bar');
		$this->assertEquals($t1, $t, 'findOneBySlug() returns a single object matching the slug');
	}
}

class TestableTable13 extends Table13
{
	public function createSlug()
	{
		return parent::createSlug();
	}
	
	public function createRawSlug()
	{
		return parent::createRawSlug();
	}
	
	public static function cleanupSlugPart($slug, $separator = '-')
	{
		return parent::cleanupSlugPart($slug, $separator);
	}
	
	public function makeSlugUnique($slug, $separator = '-', $increment = 0)
	{
		return parent::makeSlugUnique($slug, $separator, $increment);
	}
}

class TestableTable14 extends Table14
{
	public function createSlug()
	{
		return parent::createSlug();
	}
	
	public function createRawSlug()
	{
		return parent::createRawSlug();
	}
	
	public static function limitSlugSize($slug, $incrementReservedSpace = 3)
	{
		return parent::limitSlugSize($slug, $incrementReservedSpace);
	}
}
Back to Top