]> BookStack Code Mirror - bookstack/commitdiff
Prevented drafts from showing up in a book sort
authorDan Brown <redacted>
Fri, 15 Apr 2016 18:51:27 +0000 (19:51 +0100)
committerDan Brown <redacted>
Fri, 15 Apr 2016 18:51:27 +0000 (19:51 +0100)
Added tests to cover regresssion.
In reference to #100.

app/Http/Controllers/BookController.php
app/Repos/BookRepo.php
app/Repos/PageRepo.php
tests/Entity/SortTest.php [new file with mode: 0644]

index 7f743db41e4031b1abb182ea6a9fb5344b97ec72..91c9651450478d9a0be15e538bc1ed96b22da3ee 100644 (file)
@@ -151,7 +151,7 @@ class BookController extends Controller
     {
         $book = $this->bookRepo->getBySlug($bookSlug);
         $this->checkOwnablePermission('book-update', $book);
-        $bookChildren = $this->bookRepo->getChildren($book);
+        $bookChildren = $this->bookRepo->getChildren($book, true);
         $books = $this->bookRepo->getAll(false);
         $this->setPageTitle('Sort Book ' . $book->getShortName());
         return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
index b74821d312d395e0246ec7cd2e0346b23ed21c57..1a56843aee6042a4d2b0fa9387e616ca39d6d08c 100644 (file)
@@ -198,16 +198,23 @@ class BookRepo extends EntityRepo
      * Returns a sorted collection of Pages and Chapters.
      * Loads the bookslug onto child elements to prevent access database access for getting the slug.
      * @param Book $book
+     * @param bool $filterDrafts
      * @return mixed
      */
-    public function getChildren(Book $book)
+    public function getChildren(Book $book, $filterDrafts = false)
     {
         $pageQuery = $book->pages()->where('chapter_id', '=', 0);
         $pageQuery = $this->restrictionService->enforcePageRestrictions($pageQuery, 'view');
+
+        if ($filterDrafts) {
+            $pageQuery = $pageQuery->where('draft', '=', false);
+        }
+
         $pages = $pageQuery->get();
 
-        $chapterQuery = $book->chapters()->with(['pages' => function($query) {
+        $chapterQuery = $book->chapters()->with(['pages' => function($query) use ($filterDrafts) {
             $this->restrictionService->enforcePageRestrictions($query, 'view');
+            if ($filterDrafts) $query->where('draft', '=', false);
         }]);
         $chapterQuery = $this->restrictionService->enforceChapterRestrictions($chapterQuery, 'view');
         $chapters = $chapterQuery->get();
index 9a750275476651f5a3a258fdbca4d5073987d3df..ef470c01deb1ae6147c99b359f761a51e643ef16 100644 (file)
@@ -154,10 +154,10 @@ class PageRepo extends EntityRepo
     /**
      * Get a new draft page instance.
      * @param Book $book
-     * @param Chapter|null $chapter
+     * @param Chapter|bool $chapter
      * @return static
      */
-    public function getDraftPage(Book $book, $chapter)
+    public function getDraftPage(Book $book, $chapter = false)
     {
         $page = $this->page->newInstance();
         $page->name = 'New Page';
diff --git a/tests/Entity/SortTest.php b/tests/Entity/SortTest.php
new file mode 100644 (file)
index 0000000..8792a0a
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+
+class SortTest extends TestCase
+{
+    protected $book;
+
+    public function setUp()
+    {
+        parent::setUp();
+        $this->book = \BookStack\Book::first();
+    }
+
+    public function test_drafts_do_not_show_up()
+    {
+        $this->asAdmin();
+        $pageRepo = app('\BookStack\Repos\PageRepo');
+        $draft = $pageRepo->getDraftPage($this->book);
+
+        $this->visit($this->book->getUrl())
+            ->see($draft->name)
+            ->visit($this->book->getUrl() . '/sort')
+            ->dontSee($draft->name);
+    }
+
+}
\ No newline at end of file