From: Dan Brown Date: Sat, 11 Nov 2017 16:27:29 +0000 (+0000) Subject: Prevented mulitple hypens incorrectly in slug X-Git-Tag: v0.19.0~1^2~27 X-Git-Url: http://source.bookstackapp.com/bookstack/commitdiff_plain/711ba258f11633f747a652806ad2c19e1743ac7c Prevented mulitple hypens incorrectly in slug Added test to check slug format. Fixes #589 --- 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); + } + }