From 4d113dd041c5af2fc995512f40774e48b9a6e046 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 20 Feb 2016 17:15:58 +1100 Subject: [PATCH] Write collision tests for sluggable Refs #1793 Test concatenated slugs while we're here --- .../plugins/database/tester/models/Post.php | 3 +- .../tester/updates/create_posts_table.php | 1 + .../plugins/database/SluggableModelTest.php | 49 ++++++++++++++++++- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/tests/fixtures/plugins/database/tester/models/Post.php b/tests/fixtures/plugins/database/tester/models/Post.php index cc54d9b01..86603810d 100644 --- a/tests/fixtures/plugins/database/tester/models/Post.php +++ b/tests/fixtures/plugins/database/tester/models/Post.php @@ -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'] ]; } diff --git a/tests/fixtures/plugins/database/tester/updates/create_posts_table.php b/tests/fixtures/plugins/database/tester/updates/create_posts_table.php index 694ef36ca..9a4a24798 100644 --- a/tests/fixtures/plugins/database/tester/updates/create_posts_table.php +++ b/tests/fixtures/plugins/database/tester/updates/create_posts_table.php @@ -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(); diff --git a/tests/unit/plugins/database/SluggableModelTest.php b/tests/unit/plugins/database/SluggableModelTest.php index c15b18f05..f1f7aac71 100644 --- a/tests/unit/plugins/database/SluggableModelTest.php +++ b/tests/unit/plugins/database/SluggableModelTest.php @@ -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); } -} \ No newline at end of file + + 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); + } +}