]> BookStack Code Mirror - bookstack/commitdiff
Prevented mulitple hypens incorrectly in slug
authorDan Brown <redacted>
Sat, 11 Nov 2017 16:27:29 +0000 (16:27 +0000)
committerDan Brown <redacted>
Sat, 11 Nov 2017 16:27:29 +0000 (16:27 +0000)
Added test to check slug format.
Fixes #589

app/Repos/EntityRepo.php
tests/Entity/EntityTest.php

index 944c0bcfc2c9b71fdcc71c1920c601c92d911c33..d390f3e99a0975e841297f72729375a1c2909db9 100644 (file)
@@ -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;
     }
index 3f23475a0a18b8e8d8edae47ad779b24d2b5268e..a43f65b5efd568c58d1ce6a8e9c9343276d1be07 100644 (file)
@@ -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);
+    }
+
 }