forked from propelorm/Propel2
/
SluggableBehaviorTest.php
345 lines (307 loc) · 13.5 KB
/
SluggableBehaviorTest.php
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/**
* 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
*/
namespace Propel\Tests\Generator\Behavior\Sluggable;
use Propel\Tests\Helpers\Bookstore\BookstoreTestBase;
use Propel\Tests\Bookstore\Behavior\Table13;
use Propel\Tests\Bookstore\Behavior\Table13Peer;
use Propel\Tests\Bookstore\Behavior\Table13Query;
use Propel\Tests\Bookstore\Behavior\Table14;
use Propel\Tests\Bookstore\Behavior\Table14Peer;
use Propel\Tests\Bookstore\Behavior\Table14Query;
use Propel\Tests\Bookstore\Behavior\TableWithScope;
use Propel\Tests\Bookstore\Behavior\TableWithScopeQuery;
/**
* Tests for SluggableBehavior class
*
* @author François Zaninotto
*/
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('\Propel\Tests\Bookstore\Behavior\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('\Propel\Tests\Bookstore\Behavior\Table14', 'getUrl'), 'Sluggable allows customization of slug_column name');
$this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table14', 'getSlug'), 'Sluggable adds a standard getter for the slug column');
}
public function testObjectGetter()
{
$this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\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('\Propel\Tests\Bookstore\Behavior\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('\Propel\Tests\Bookstore\Behavior\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('\Propel\Tests\Bookstore\Behavior\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 cleaned 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('\Propel\Tests\Bookstore\Behavior\Table13Query', 'findOneBySlug'), 'The generated query provides a findOneBySlug() method');
$this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table14Query', 'findOneBySlug'), 'The generated query provides a findOneBySlug() method even if the slug column doesn\'t 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');
}
public function testUniqueViolationWithoutScope()
{
$this->markTestSkipped('Skipping...');
TableWithScopeQuery::create()->deleteAll();
$t = new TableWithScope();
$t->setTitle('Hello, World');
$t->save();
$this->assertEquals('hello-world', $t->getSlug());
$this->setExpectedException('Propel\Runtime\Exception\PropelException');
$t = new TableWithScope();
$t->setTitle('Hello, World');
$t->save();
}
public function testNoUniqueViolationWithScope()
{
TableWithScopeQuery::create()->deleteAll();
$t = new TableWithScope();
$t->setTitle('Hello, World');
$t->save();
$this->assertEquals('hello-world', $t->getSlug());
try {
$t = new TableWithScope();
$t->setTitle('Hello, World');
$t->setScope(1);
$t->save();
$this->assertEquals('hello-world', $t->getSlug());
} catch (Exception $e) {
$this->fail($e->getMessage());
}
}
}
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);
}
}