Write collision tests for sluggable

Refs #1793
Test concatenated slugs while we're here
This commit is contained in:
Samuel Georges 2016-02-20 17:15:58 +11:00
parent 202e542985
commit 4d113dd041
3 changed files with 51 additions and 2 deletions

View File

@ -64,7 +64,8 @@ class SluggablePost extends Post
* @var array List of attributes to automatically generate unique URL names (slugs) for.
*/
protected $slugs = [
'slug' => 'title'
'slug' => 'title',
'long_slug' => ['title', 'description']
];
}

View File

@ -14,6 +14,7 @@ class CreatePostsTable extends Migration
$table->increments('id');
$table->string('title')->nullable();
$table->string('slug')->nullable()->index();
$table->text('long_slug')->nullable();
$table->text('description')->nullable();
$table->boolean('is_published')->default(false);
$table->timestamp('published_at')->nullable();

View File

@ -38,6 +38,16 @@ class SluggableModelTest extends PluginTestCase
$this->assertEquals('war-is-pain', $post->slug);
}
public function testConcatenatedSlug()
{
$post = new SluggablePost;
$post->title = 'Sweetness and Light';
$post->description = 'Itchee and Scratchee';
$post->save();
$this->assertEquals('sweetness-and-light-itchee-and-scratchee', $post->long_slug);
}
public function testDuplicateSlug()
{
$post1 = SluggablePost::create(['title' => 'Pace yourself']);
@ -48,4 +58,41 @@ class SluggableModelTest extends PluginTestCase
$this->assertEquals('pace-yourself-2', $post2->slug);
$this->assertEquals('pace-yourself-3', $post3->slug);
}
}
public function testCollisionWithSelf()
{
$post1 = SluggablePost::create(['title' => 'Watch yourself']);
$post2 = SluggablePost::create(['title' => 'Watch yourself']);
$post3 = SluggablePost::create(['title' => 'Watch yourself']);
$this->assertEquals('watch-yourself', $post1->slug);
$this->assertEquals('watch-yourself-2', $post2->slug);
$this->assertEquals('watch-yourself-3', $post3->slug);
$post3->slugAttributes();
$post3->save();
$post2->slugAttributes();
$post2->save();
$post1->slugAttributes();
$post1->save();
$this->assertEquals('watch-yourself', $post1->slug);
$this->assertEquals('watch-yourself-2', $post2->slug);
$this->assertEquals('watch-yourself-3', $post3->slug);
}
public function testSuffixCollision()
{
$post1 = SluggablePost::create(['title' => 'Type 1']);
$post2 = SluggablePost::create(['title' => 'Type 2']);
$post3 = SluggablePost::create(['title' => 'Type 3']);
$post4 = SluggablePost::create(['title' => 'Type 3']);
$post5 = SluggablePost::create(['title' => 'Type 3']);
$this->assertEquals('type-1', $post1->slug);
$this->assertEquals('type-2', $post2->slug);
$this->assertEquals('type-3', $post3->slug);
$this->assertEquals('type-3-2', $post4->slug);
$this->assertEquals('type-3-3', $post5->slug);
}
}