From 711ba258f11633f747a652806ad2c19e1743ac7c Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sat, 11 Nov 2017 16:27:29 +0000 Subject: [PATCH] Prevented mulitple hypens incorrectly in slug Added test to check slug format. Fixes #589 --- app/Repos/EntityRepo.php | 5 +++-- tests/Entity/EntityTest.php | 11 ++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/Repos/EntityRepo.php b/app/Repos/EntityRepo.php index 944c0bcfc..d390f3e99 100644 --- a/app/Repos/EntityRepo.php +++ b/app/Repos/EntityRepo.php @@ -553,8 +553,9 @@ class EntityRepo */ protected function nameToSlug($name) { - $slug = str_replace(' ', '-', mb_strtolower($name)); - $slug = preg_replace('/[\+\/\\\?\@\}\{\.\,\=\[\]\#\&\!\*\'\;\:\$\%]/', '', $slug); + $slug = preg_replace('/[\+\/\\\?\@\}\{\.\,\=\[\]\#\&\!\*\'\;\:\$\%]/', '', mb_strtolower($name)); + $slug = preg_replace('/\s{2,}/', ' ', $slug); + $slug = str_replace(' ', '-', $slug); if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5); return $slug; } diff --git a/tests/Entity/EntityTest.php b/tests/Entity/EntityTest.php index 3f23475a0..a43f65b5e 100644 --- a/tests/Entity/EntityTest.php +++ b/tests/Entity/EntityTest.php @@ -11,7 +11,6 @@ class EntityTest extends BrowserKitTest public function test_entity_creation() { - // Test Creation $book = $this->bookCreation(); $chapter = $this->chapterCreation($book); @@ -268,4 +267,14 @@ class EntityTest extends BrowserKitTest } + public function test_slug_format() + { + $entityRepo = app(EntityRepo::class); + $book = $entityRepo->createFromInput('book', [ + 'name' => 'PartA / PartB / PartC' + ]); + + $this->assertEquals('parta-partb-partc', $book->slug); + } + }