Dan Brown

Fixed entities created with blank slugs.

Fixes #156.
...@@ -216,12 +216,10 @@ class BookRepo extends EntityRepo ...@@ -216,12 +216,10 @@ class BookRepo extends EntityRepo
216 */ 216 */
217 public function findSuitableSlug($name, $currentId = false) 217 public function findSuitableSlug($name, $currentId = false)
218 { 218 {
219 - $originalSlug = Str::slug($name); 219 + $slug = Str::slug($name);
220 - $slug = $originalSlug; 220 + if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
221 - $count = 2;
222 while ($this->doesSlugExist($slug, $currentId)) { 221 while ($this->doesSlugExist($slug, $currentId)) {
223 - $slug = $originalSlug . '-' . $count; 222 + $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
224 - $count++;
225 } 223 }
226 return $slug; 224 return $slug;
227 } 225 }
...@@ -229,7 +227,7 @@ class BookRepo extends EntityRepo ...@@ -229,7 +227,7 @@ class BookRepo extends EntityRepo
229 /** 227 /**
230 * Get all child objects of a book. 228 * Get all child objects of a book.
231 * Returns a sorted collection of Pages and Chapters. 229 * Returns a sorted collection of Pages and Chapters.
232 - * Loads the bookslug onto child elements to prevent access database access for getting the slug. 230 + * Loads the book slug onto child elements to prevent access database access for getting the slug.
233 * @param Book $book 231 * @param Book $book
234 * @param bool $filterDrafts 232 * @param bool $filterDrafts
235 * @return mixed 233 * @return mixed
...@@ -245,7 +243,7 @@ class BookRepo extends EntityRepo ...@@ -245,7 +243,7 @@ class BookRepo extends EntityRepo
245 243
246 $pages = $pageQuery->get(); 244 $pages = $pageQuery->get();
247 245
248 - $chapterQuery = $book->chapters()->with(['pages' => function($query) use ($filterDrafts) { 246 + $chapterQuery = $book->chapters()->with(['pages' => function ($query) use ($filterDrafts) {
249 $this->permissionService->enforcePageRestrictions($query, 'view'); 247 $this->permissionService->enforcePageRestrictions($query, 'view');
250 if ($filterDrafts) $query->where('draft', '=', false); 248 if ($filterDrafts) $query->where('draft', '=', false);
251 }]); 249 }]);
...@@ -263,7 +261,7 @@ class BookRepo extends EntityRepo ...@@ -263,7 +261,7 @@ class BookRepo extends EntityRepo
263 $child->pages->each(function ($page) use ($bookSlug) { 261 $child->pages->each(function ($page) use ($bookSlug) {
264 $page->setAttribute('bookSlug', $bookSlug); 262 $page->setAttribute('bookSlug', $bookSlug);
265 }); 263 });
266 - $child->pages = $child->pages->sortBy(function($child, $key) { 264 + $child->pages = $child->pages->sortBy(function ($child, $key) {
267 $score = $child->priority; 265 $score = $child->priority;
268 if ($child->draft) $score -= 100; 266 if ($child->draft) $score -= 100;
269 return $score; 267 return $score;
...@@ -272,7 +270,7 @@ class BookRepo extends EntityRepo ...@@ -272,7 +270,7 @@ class BookRepo extends EntityRepo
272 }); 270 });
273 271
274 // Sort items with drafts first then by priority. 272 // Sort items with drafts first then by priority.
275 - return $children->sortBy(function($child, $key) { 273 + return $children->sortBy(function ($child, $key) {
276 $score = $child->priority; 274 $score = $child->priority;
277 if ($child->isA('page') && $child->draft) $score -= 100; 275 if ($child->isA('page') && $child->draft) $score -= 100;
278 return $score; 276 return $score;
......
...@@ -81,7 +81,7 @@ class ChapterRepo extends EntityRepo ...@@ -81,7 +81,7 @@ class ChapterRepo extends EntityRepo
81 { 81 {
82 $pages = $this->permissionService->enforcePageRestrictions($chapter->pages())->get(); 82 $pages = $this->permissionService->enforcePageRestrictions($chapter->pages())->get();
83 // Sort items with drafts first then by priority. 83 // Sort items with drafts first then by priority.
84 - return $pages->sortBy(function($child, $key) { 84 + return $pages->sortBy(function ($child, $key) {
85 $score = $child->priority; 85 $score = $child->priority;
86 if ($child->draft) $score -= 100; 86 if ($child->draft) $score -= 100;
87 return $score; 87 return $score;
...@@ -151,6 +151,7 @@ class ChapterRepo extends EntityRepo ...@@ -151,6 +151,7 @@ class ChapterRepo extends EntityRepo
151 public function findSuitableSlug($name, $bookId, $currentId = false) 151 public function findSuitableSlug($name, $bookId, $currentId = false)
152 { 152 {
153 $slug = Str::slug($name); 153 $slug = Str::slug($name);
154 + if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
154 while ($this->doesSlugExist($slug, $bookId, $currentId)) { 155 while ($this->doesSlugExist($slug, $bookId, $currentId)) {
155 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3); 156 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
156 } 157 }
......
...@@ -147,7 +147,7 @@ class PageRepo extends EntityRepo ...@@ -147,7 +147,7 @@ class PageRepo extends EntityRepo
147 $draftPage->fill($input); 147 $draftPage->fill($input);
148 148
149 // Save page tags if present 149 // Save page tags if present
150 - if(isset($input['tags'])) { 150 + if (isset($input['tags'])) {
151 $this->tagRepo->saveTagsToEntity($draftPage, $input['tags']); 151 $this->tagRepo->saveTagsToEntity($draftPage, $input['tags']);
152 } 152 }
153 153
...@@ -319,7 +319,7 @@ class PageRepo extends EntityRepo ...@@ -319,7 +319,7 @@ class PageRepo extends EntityRepo
319 } 319 }
320 320
321 // Save page tags if present 321 // Save page tags if present
322 - if(isset($input['tags'])) { 322 + if (isset($input['tags'])) {
323 $this->tagRepo->saveTagsToEntity($page, $input['tags']); 323 $this->tagRepo->saveTagsToEntity($page, $input['tags']);
324 } 324 }
325 325
...@@ -591,14 +591,15 @@ class PageRepo extends EntityRepo ...@@ -591,14 +591,15 @@ class PageRepo extends EntityRepo
591 591
592 /** 592 /**
593 * Gets a suitable slug for the resource 593 * Gets a suitable slug for the resource
594 - * @param $name 594 + * @param string $name
595 - * @param $bookId 595 + * @param int $bookId
596 * @param bool|false $currentId 596 * @param bool|false $currentId
597 * @return string 597 * @return string
598 */ 598 */
599 public function findSuitableSlug($name, $bookId, $currentId = false) 599 public function findSuitableSlug($name, $bookId, $currentId = false)
600 { 600 {
601 $slug = Str::slug($name); 601 $slug = Str::slug($name);
602 + if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
602 while ($this->doesSlugExist($slug, $bookId, $currentId)) { 603 while ($this->doesSlugExist($slug, $bookId, $currentId)) {
603 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3); 604 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
604 } 605 }
......
...@@ -151,8 +151,10 @@ class EntityTest extends TestCase ...@@ -151,8 +151,10 @@ class EntityTest extends TestCase
151 ->visit('/books/create') 151 ->visit('/books/create')
152 ->type($book->name, '#name') 152 ->type($book->name, '#name')
153 ->type($book->description, '#description') 153 ->type($book->description, '#description')
154 - ->press('Save Book') 154 + ->press('Save Book');
155 - ->seePageIs('/books/my-first-book-2'); 155 +
156 + $expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
157 + $this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
156 158
157 $book = \BookStack\Book::where('slug', '=', 'my-first-book')->first(); 159 $book = \BookStack\Book::where('slug', '=', 'my-first-book')->first();
158 return $book; 160 return $book;
......