From 7c9937e9240b9656fe865de85a89c97b734656ab Mon Sep 17 00:00:00 2001
From: Dan Brown <ssddanbrown@googlemail.com>
Date: Sun, 5 Feb 2017 14:20:59 +0000
Subject: [PATCH] Converted sort tests to non browserkit testing

Added testing to cover book sort endpoint.
Closes #283
---
 resources/views/books/show.blade.php |  4 +-
 tests/BrowserKitTest.php             |  2 +-
 tests/Entity/SortTest.php            | 99 +++++++++++++++++++++-------
 tests/TestCase.php                   | 46 +++++++++++++
 4 files changed, 126 insertions(+), 25 deletions(-)

diff --git a/resources/views/books/show.blade.php b/resources/views/books/show.blade.php
index 6a18302bc..6b4e7f88a 100644
--- a/resources/views/books/show.blade.php
+++ b/resources/views/books/show.blade.php
@@ -5,10 +5,10 @@
     <div class="faded-small toolbar">
         <div class="container">
             <div class="row">
-                <div class="col-md-6 faded">
+                <div class="col-sm-6 faded">
                     @include('books._breadcrumbs', ['book' => $book])
                 </div>
-                <div class="col-md-6">
+                <div class="col-sm-6">
                     <div class="action-buttons faded">
                         @if(userCan('page-create', $book))
                             <a href="{{ $book->getUrl('/page/create') }}" class="text-pos text-button"><i class="zmdi zmdi-plus"></i>{{ trans('entities.pages_new') }}</a>
diff --git a/tests/BrowserKitTest.php b/tests/BrowserKitTest.php
index 674aef984..8e02cb5f1 100644
--- a/tests/BrowserKitTest.php
+++ b/tests/BrowserKitTest.php
@@ -51,7 +51,7 @@ abstract class BrowserKitTest extends TestCase
      */
     public function getAdmin() {
         if($this->admin === null) {
-            $adminRole = Role::getRole('admin');
+            $adminRole = Role::getSystemRole('admin');
             $this->admin = $adminRole->users->first();
         }
         return $this->admin;
diff --git a/tests/Entity/SortTest.php b/tests/Entity/SortTest.php
index d61b3f56f..3b0831029 100644
--- a/tests/Entity/SortTest.php
+++ b/tests/Entity/SortTest.php
@@ -1,6 +1,10 @@
 <?php namespace Tests;
 
-class SortTest extends BrowserKitTest
+use BookStack\Book;
+use BookStack\Page;
+use BookStack\Repos\EntityRepo;
+
+class SortTest extends TestCase
 {
     protected $book;
 
@@ -13,13 +17,14 @@ class SortTest extends BrowserKitTest
     public function test_drafts_do_not_show_up()
     {
         $this->asAdmin();
-        $entityRepo = app('\BookStack\Repos\EntityRepo');
+        $entityRepo = app(EntityRepo::class);
         $draft = $entityRepo->getDraftPage($this->book);
 
-        $this->visit($this->book->getUrl())
-            ->see($draft->name)
-            ->visit($this->book->getUrl() . '/sort')
-            ->dontSee($draft->name);
+        $resp = $this->get($this->book->getUrl());
+        $resp->assertSee($draft->name);
+
+        $resp = $this->get($this->book->getUrl() . '/sort');
+        $resp->assertDontSee($draft->name);
     }
 
     public function test_page_move()
@@ -27,17 +32,21 @@ class SortTest extends BrowserKitTest
         $page = \BookStack\Page::first();
         $currentBook = $page->book;
         $newBook = \BookStack\Book::where('id', '!=', $currentBook->id)->first();
-        $this->asAdmin()->visit($page->getUrl() . '/move')
-            ->see('Move Page')
-            ->type('book:' . $newBook->id, 'entity_selection')->press('Move Page');
 
+        $resp = $this->asAdmin()->get($page->getUrl() . '/move');
+        $resp->assertSee('Move Page');
+
+        $movePageResp = $this->put($page->getUrl() . '/move', [
+            'entity_selection' => 'book:' . $newBook->id
+        ]);
         $page = \BookStack\Page::find($page->id);
-        $this->seePageIs($page->getUrl());
+
+        $movePageResp->assertRedirect($page->getUrl());
         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
 
-        $this->visit($newBook->getUrl())
-            ->seeInNthElement('.activity-list-item', 0, 'moved page')
-            ->seeInNthElement('.activity-list-item', 0, $page->name);
+        $newBookResp = $this->get($newBook->getUrl());
+        $newBookResp->assertSee('moved page');
+        $newBookResp->assertSee($page->name);
     }
 
     public function test_chapter_move()
@@ -47,22 +56,68 @@ class SortTest extends BrowserKitTest
         $pageToCheck = $chapter->pages->first();
         $newBook = \BookStack\Book::where('id', '!=', $currentBook->id)->first();
 
-        $this->asAdmin()->visit($chapter->getUrl() . '/move')
-            ->see('Move Chapter')
-            ->type('book:' . $newBook->id, 'entity_selection')->press('Move Chapter');
+        $chapterMoveResp = $this->asAdmin()->get($chapter->getUrl() . '/move');
+        $chapterMoveResp->assertSee('Move Chapter');
+
+        $moveChapterResp = $this->put($chapter->getUrl() . '/move', [
+            'entity_selection' => 'book:' . $newBook->id
+        ]);
 
         $chapter = \BookStack\Chapter::find($chapter->id);
-        $this->seePageIs($chapter->getUrl());
+        $moveChapterResp->assertRedirect($chapter->getUrl());
         $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
 
-        $this->visit($newBook->getUrl())
-            ->seeInNthElement('.activity-list-item', 0, 'moved chapter')
-            ->seeInNthElement('.activity-list-item', 0, $chapter->name);
+        $newBookResp = $this->get($newBook->getUrl());
+        $newBookResp->assertSee('moved chapter');
+        $newBookResp->assertSee($chapter->name);
 
         $pageToCheck = \BookStack\Page::find($pageToCheck->id);
         $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
-        $this->visit($pageToCheck->getUrl())
-            ->see($newBook->name);
+        $pageCheckResp = $this->get($pageToCheck->getUrl());
+        $pageCheckResp->assertSee($newBook->name);
+    }
+
+    public function test_book_sort()
+    {
+        $oldBook = Book::query()->first();
+        $chapterToMove = $this->newChapter(['name' => 'chapter to move'], $oldBook);
+        $newBook = $this->newBook(['name' => 'New sort book']);
+        $pagesToMove = Page::query()->take(5)->get();
+
+        // Create request data
+        $reqData = [
+            [
+                'id' => $chapterToMove->id,
+                'sort' => 0,
+                'parentChapter' => false,
+                'type' => 'chapter',
+                'book' => $newBook->id
+            ]
+        ];
+        foreach ($pagesToMove as $index => $page) {
+            $reqData[] = [
+                'id' => $page->id,
+                'sort' => $index,
+                'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
+                'type' => 'page',
+                'book' => $newBook->id
+            ];
+        }
+
+        $sortResp = $this->asAdmin()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
+        $sortResp->assertRedirect($newBook->getUrl());
+        $sortResp->assertStatus(302);
+        $this->assertDatabaseHas('chapters', [
+            'id' => $chapterToMove->id,
+            'book_id' => $newBook->id,
+            'priority' => 0
+        ]);
+        $this->assertTrue($newBook->chapters()->count() === 1);
+        $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
+
+        $checkPage = $pagesToMove[1];
+        $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
+        $checkResp->assertSee($newBook->name);
     }
 
 }
\ No newline at end of file
diff --git a/tests/TestCase.php b/tests/TestCase.php
index 6b97682e8..d52e991e3 100644
--- a/tests/TestCase.php
+++ b/tests/TestCase.php
@@ -1,8 +1,54 @@
 <?php namespace Tests;
 
+use BookStack\Book;
+use BookStack\Chapter;
+use BookStack\Repos\EntityRepo;
+use BookStack\Role;
 use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
 
 abstract class TestCase extends BaseTestCase
 {
     use CreatesApplication;
+
+    protected $admin;
+
+    /**
+     * Set the current user context to be an admin.
+     * @return $this
+     */
+    public function asAdmin()
+    {
+        return $this->actingAs($this->getAdmin());
+    }
+
+    /**
+     * Get the current admin user.
+     * @return mixed
+     */
+    public function getAdmin() {
+        if($this->admin === null) {
+            $adminRole = Role::getSystemRole('admin');
+            $this->admin = $adminRole->users->first();
+        }
+        return $this->admin;
+    }
+
+    /**
+     * Create and return a new book.
+     * @param array $input
+     * @return Book
+     */
+    public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
+        return $this->app[EntityRepo::class]->createFromInput('book', $input, false);
+    }
+
+    /**
+     * Create and return a new test chapter
+     * @param array $input
+     * @param Book $book
+     * @return Chapter
+     */
+    public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) {
+        return $this->app[EntityRepo::class]->createFromInput('chapter', $input, $book);
+    }
 }
\ No newline at end of file