Fixed entities created with blank slugs.

Fixes #156.
This commit is contained in:
Dan Brown 2016-08-13 13:53:04 +01:00
parent 07c7d5af17
commit 2d958e88bf
4 changed files with 19 additions and 17 deletions

View File

@ -216,12 +216,10 @@ class BookRepo extends EntityRepo
*/ */
public function findSuitableSlug($name, $currentId = false) public function findSuitableSlug($name, $currentId = false)
{ {
$originalSlug = Str::slug($name); $slug = Str::slug($name);
$slug = $originalSlug; if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
$count = 2;
while ($this->doesSlugExist($slug, $currentId)) { while ($this->doesSlugExist($slug, $currentId)) {
$slug = $originalSlug . '-' . $count; $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
$count++;
} }
return $slug; return $slug;
} }

View File

@ -151,6 +151,7 @@ class ChapterRepo extends EntityRepo
public function findSuitableSlug($name, $bookId, $currentId = false) public function findSuitableSlug($name, $bookId, $currentId = false)
{ {
$slug = Str::slug($name); $slug = Str::slug($name);
if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
while ($this->doesSlugExist($slug, $bookId, $currentId)) { while ($this->doesSlugExist($slug, $bookId, $currentId)) {
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3); $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
} }

View File

@ -591,14 +591,15 @@ class PageRepo extends EntityRepo
/** /**
* Gets a suitable slug for the resource * Gets a suitable slug for the resource
* @param $name * @param string $name
* @param $bookId * @param int $bookId
* @param bool|false $currentId * @param bool|false $currentId
* @return string * @return string
*/ */
public function findSuitableSlug($name, $bookId, $currentId = false) public function findSuitableSlug($name, $bookId, $currentId = false)
{ {
$slug = Str::slug($name); $slug = Str::slug($name);
if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
while ($this->doesSlugExist($slug, $bookId, $currentId)) { while ($this->doesSlugExist($slug, $bookId, $currentId)) {
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3); $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
} }

View File

@ -151,8 +151,10 @@ class EntityTest extends TestCase
->visit('/books/create') ->visit('/books/create')
->type($book->name, '#name') ->type($book->name, '#name')
->type($book->description, '#description') ->type($book->description, '#description')
->press('Save Book') ->press('Save Book');
->seePageIs('/books/my-first-book-2');
$expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
$this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
$book = \BookStack\Book::where('slug', '=', 'my-first-book')->first(); $book = \BookStack\Book::where('slug', '=', 'my-first-book')->first();
return $book; return $book;