Dan Brown

Prevented drafts from showing up in a book sort

Added tests to cover regresssion.
In reference to #100.
...@@ -151,7 +151,7 @@ class BookController extends Controller ...@@ -151,7 +151,7 @@ class BookController extends Controller
151 { 151 {
152 $book = $this->bookRepo->getBySlug($bookSlug); 152 $book = $this->bookRepo->getBySlug($bookSlug);
153 $this->checkOwnablePermission('book-update', $book); 153 $this->checkOwnablePermission('book-update', $book);
154 - $bookChildren = $this->bookRepo->getChildren($book); 154 + $bookChildren = $this->bookRepo->getChildren($book, true);
155 $books = $this->bookRepo->getAll(false); 155 $books = $this->bookRepo->getAll(false);
156 $this->setPageTitle('Sort Book ' . $book->getShortName()); 156 $this->setPageTitle('Sort Book ' . $book->getShortName());
157 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]); 157 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
......
...@@ -198,16 +198,23 @@ class BookRepo extends EntityRepo ...@@ -198,16 +198,23 @@ class BookRepo extends EntityRepo
198 * Returns a sorted collection of Pages and Chapters. 198 * Returns a sorted collection of Pages and Chapters.
199 * Loads the bookslug onto child elements to prevent access database access for getting the slug. 199 * Loads the bookslug onto child elements to prevent access database access for getting the slug.
200 * @param Book $book 200 * @param Book $book
201 + * @param bool $filterDrafts
201 * @return mixed 202 * @return mixed
202 */ 203 */
203 - public function getChildren(Book $book) 204 + public function getChildren(Book $book, $filterDrafts = false)
204 { 205 {
205 $pageQuery = $book->pages()->where('chapter_id', '=', 0); 206 $pageQuery = $book->pages()->where('chapter_id', '=', 0);
206 $pageQuery = $this->restrictionService->enforcePageRestrictions($pageQuery, 'view'); 207 $pageQuery = $this->restrictionService->enforcePageRestrictions($pageQuery, 'view');
208 +
209 + if ($filterDrafts) {
210 + $pageQuery = $pageQuery->where('draft', '=', false);
211 + }
212 +
207 $pages = $pageQuery->get(); 213 $pages = $pageQuery->get();
208 214
209 - $chapterQuery = $book->chapters()->with(['pages' => function($query) { 215 + $chapterQuery = $book->chapters()->with(['pages' => function($query) use ($filterDrafts) {
210 $this->restrictionService->enforcePageRestrictions($query, 'view'); 216 $this->restrictionService->enforcePageRestrictions($query, 'view');
217 + if ($filterDrafts) $query->where('draft', '=', false);
211 }]); 218 }]);
212 $chapterQuery = $this->restrictionService->enforceChapterRestrictions($chapterQuery, 'view'); 219 $chapterQuery = $this->restrictionService->enforceChapterRestrictions($chapterQuery, 'view');
213 $chapters = $chapterQuery->get(); 220 $chapters = $chapterQuery->get();
......
...@@ -154,10 +154,10 @@ class PageRepo extends EntityRepo ...@@ -154,10 +154,10 @@ class PageRepo extends EntityRepo
154 /** 154 /**
155 * Get a new draft page instance. 155 * Get a new draft page instance.
156 * @param Book $book 156 * @param Book $book
157 - * @param Chapter|null $chapter 157 + * @param Chapter|bool $chapter
158 * @return static 158 * @return static
159 */ 159 */
160 - public function getDraftPage(Book $book, $chapter) 160 + public function getDraftPage(Book $book, $chapter = false)
161 { 161 {
162 $page = $this->page->newInstance(); 162 $page = $this->page->newInstance();
163 $page->name = 'New Page'; 163 $page->name = 'New Page';
......
1 +<?php
2 +
3 +class SortTest extends TestCase
4 +{
5 + protected $book;
6 +
7 + public function setUp()
8 + {
9 + parent::setUp();
10 + $this->book = \BookStack\Book::first();
11 + }
12 +
13 + public function test_drafts_do_not_show_up()
14 + {
15 + $this->asAdmin();
16 + $pageRepo = app('\BookStack\Repos\PageRepo');
17 + $draft = $pageRepo->getDraftPage($this->book);
18 +
19 + $this->visit($this->book->getUrl())
20 + ->see($draft->name)
21 + ->visit($this->book->getUrl() . '/sort')
22 + ->dontSee($draft->name);
23 + }
24 +
25 +}
...\ No newline at end of file ...\ No newline at end of file