Dan Brown

Converted sort tests to non browserkit testing

Added testing to cover book sort endpoint.
Closes #283
...@@ -5,10 +5,10 @@ ...@@ -5,10 +5,10 @@
5 <div class="faded-small toolbar"> 5 <div class="faded-small toolbar">
6 <div class="container"> 6 <div class="container">
7 <div class="row"> 7 <div class="row">
8 - <div class="col-md-6 faded"> 8 + <div class="col-sm-6 faded">
9 @include('books._breadcrumbs', ['book' => $book]) 9 @include('books._breadcrumbs', ['book' => $book])
10 </div> 10 </div>
11 - <div class="col-md-6"> 11 + <div class="col-sm-6">
12 <div class="action-buttons faded"> 12 <div class="action-buttons faded">
13 @if(userCan('page-create', $book)) 13 @if(userCan('page-create', $book))
14 <a href="{{ $book->getUrl('/page/create') }}" class="text-pos text-button"><i class="zmdi zmdi-plus"></i>{{ trans('entities.pages_new') }}</a> 14 <a href="{{ $book->getUrl('/page/create') }}" class="text-pos text-button"><i class="zmdi zmdi-plus"></i>{{ trans('entities.pages_new') }}</a>
......
...@@ -51,7 +51,7 @@ abstract class BrowserKitTest extends TestCase ...@@ -51,7 +51,7 @@ abstract class BrowserKitTest extends TestCase
51 */ 51 */
52 public function getAdmin() { 52 public function getAdmin() {
53 if($this->admin === null) { 53 if($this->admin === null) {
54 - $adminRole = Role::getRole('admin'); 54 + $adminRole = Role::getSystemRole('admin');
55 $this->admin = $adminRole->users->first(); 55 $this->admin = $adminRole->users->first();
56 } 56 }
57 return $this->admin; 57 return $this->admin;
......
1 <?php namespace Tests; 1 <?php namespace Tests;
2 2
3 -class SortTest extends BrowserKitTest 3 +use BookStack\Book;
4 +use BookStack\Page;
5 +use BookStack\Repos\EntityRepo;
6 +
7 +class SortTest extends TestCase
4 { 8 {
5 protected $book; 9 protected $book;
6 10
...@@ -13,13 +17,14 @@ class SortTest extends BrowserKitTest ...@@ -13,13 +17,14 @@ class SortTest extends BrowserKitTest
13 public function test_drafts_do_not_show_up() 17 public function test_drafts_do_not_show_up()
14 { 18 {
15 $this->asAdmin(); 19 $this->asAdmin();
16 - $entityRepo = app('\BookStack\Repos\EntityRepo'); 20 + $entityRepo = app(EntityRepo::class);
17 $draft = $entityRepo->getDraftPage($this->book); 21 $draft = $entityRepo->getDraftPage($this->book);
18 22
19 - $this->visit($this->book->getUrl()) 23 + $resp = $this->get($this->book->getUrl());
20 - ->see($draft->name) 24 + $resp->assertSee($draft->name);
21 - ->visit($this->book->getUrl() . '/sort') 25 +
22 - ->dontSee($draft->name); 26 + $resp = $this->get($this->book->getUrl() . '/sort');
27 + $resp->assertDontSee($draft->name);
23 } 28 }
24 29
25 public function test_page_move() 30 public function test_page_move()
...@@ -27,17 +32,21 @@ class SortTest extends BrowserKitTest ...@@ -27,17 +32,21 @@ class SortTest extends BrowserKitTest
27 $page = \BookStack\Page::first(); 32 $page = \BookStack\Page::first();
28 $currentBook = $page->book; 33 $currentBook = $page->book;
29 $newBook = \BookStack\Book::where('id', '!=', $currentBook->id)->first(); 34 $newBook = \BookStack\Book::where('id', '!=', $currentBook->id)->first();
30 - $this->asAdmin()->visit($page->getUrl() . '/move')
31 - ->see('Move Page')
32 - ->type('book:' . $newBook->id, 'entity_selection')->press('Move Page');
33 35
36 + $resp = $this->asAdmin()->get($page->getUrl() . '/move');
37 + $resp->assertSee('Move Page');
38 +
39 + $movePageResp = $this->put($page->getUrl() . '/move', [
40 + 'entity_selection' => 'book:' . $newBook->id
41 + ]);
34 $page = \BookStack\Page::find($page->id); 42 $page = \BookStack\Page::find($page->id);
35 - $this->seePageIs($page->getUrl()); 43 +
44 + $movePageResp->assertRedirect($page->getUrl());
36 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book'); 45 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
37 46
38 - $this->visit($newBook->getUrl()) 47 + $newBookResp = $this->get($newBook->getUrl());
39 - ->seeInNthElement('.activity-list-item', 0, 'moved page') 48 + $newBookResp->assertSee('moved page');
40 - ->seeInNthElement('.activity-list-item', 0, $page->name); 49 + $newBookResp->assertSee($page->name);
41 } 50 }
42 51
43 public function test_chapter_move() 52 public function test_chapter_move()
...@@ -47,22 +56,68 @@ class SortTest extends BrowserKitTest ...@@ -47,22 +56,68 @@ class SortTest extends BrowserKitTest
47 $pageToCheck = $chapter->pages->first(); 56 $pageToCheck = $chapter->pages->first();
48 $newBook = \BookStack\Book::where('id', '!=', $currentBook->id)->first(); 57 $newBook = \BookStack\Book::where('id', '!=', $currentBook->id)->first();
49 58
50 - $this->asAdmin()->visit($chapter->getUrl() . '/move') 59 + $chapterMoveResp = $this->asAdmin()->get($chapter->getUrl() . '/move');
51 - ->see('Move Chapter') 60 + $chapterMoveResp->assertSee('Move Chapter');
52 - ->type('book:' . $newBook->id, 'entity_selection')->press('Move Chapter'); 61 +
62 + $moveChapterResp = $this->put($chapter->getUrl() . '/move', [
63 + 'entity_selection' => 'book:' . $newBook->id
64 + ]);
53 65
54 $chapter = \BookStack\Chapter::find($chapter->id); 66 $chapter = \BookStack\Chapter::find($chapter->id);
55 - $this->seePageIs($chapter->getUrl()); 67 + $moveChapterResp->assertRedirect($chapter->getUrl());
56 $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book'); 68 $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
57 69
58 - $this->visit($newBook->getUrl()) 70 + $newBookResp = $this->get($newBook->getUrl());
59 - ->seeInNthElement('.activity-list-item', 0, 'moved chapter') 71 + $newBookResp->assertSee('moved chapter');
60 - ->seeInNthElement('.activity-list-item', 0, $chapter->name); 72 + $newBookResp->assertSee($chapter->name);
61 73
62 $pageToCheck = \BookStack\Page::find($pageToCheck->id); 74 $pageToCheck = \BookStack\Page::find($pageToCheck->id);
63 $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book'); 75 $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
64 - $this->visit($pageToCheck->getUrl()) 76 + $pageCheckResp = $this->get($pageToCheck->getUrl());
65 - ->see($newBook->name); 77 + $pageCheckResp->assertSee($newBook->name);
78 + }
79 +
80 + public function test_book_sort()
81 + {
82 + $oldBook = Book::query()->first();
83 + $chapterToMove = $this->newChapter(['name' => 'chapter to move'], $oldBook);
84 + $newBook = $this->newBook(['name' => 'New sort book']);
85 + $pagesToMove = Page::query()->take(5)->get();
86 +
87 + // Create request data
88 + $reqData = [
89 + [
90 + 'id' => $chapterToMove->id,
91 + 'sort' => 0,
92 + 'parentChapter' => false,
93 + 'type' => 'chapter',
94 + 'book' => $newBook->id
95 + ]
96 + ];
97 + foreach ($pagesToMove as $index => $page) {
98 + $reqData[] = [
99 + 'id' => $page->id,
100 + 'sort' => $index,
101 + 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
102 + 'type' => 'page',
103 + 'book' => $newBook->id
104 + ];
105 + }
106 +
107 + $sortResp = $this->asAdmin()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
108 + $sortResp->assertRedirect($newBook->getUrl());
109 + $sortResp->assertStatus(302);
110 + $this->assertDatabaseHas('chapters', [
111 + 'id' => $chapterToMove->id,
112 + 'book_id' => $newBook->id,
113 + 'priority' => 0
114 + ]);
115 + $this->assertTrue($newBook->chapters()->count() === 1);
116 + $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
117 +
118 + $checkPage = $pagesToMove[1];
119 + $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
120 + $checkResp->assertSee($newBook->name);
66 } 121 }
67 122
68 } 123 }
...\ No newline at end of file ...\ No newline at end of file
......
1 <?php namespace Tests; 1 <?php namespace Tests;
2 2
3 +use BookStack\Book;
4 +use BookStack\Chapter;
5 +use BookStack\Repos\EntityRepo;
6 +use BookStack\Role;
3 use Illuminate\Foundation\Testing\TestCase as BaseTestCase; 7 use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
4 8
5 abstract class TestCase extends BaseTestCase 9 abstract class TestCase extends BaseTestCase
6 { 10 {
7 use CreatesApplication; 11 use CreatesApplication;
12 +
13 + protected $admin;
14 +
15 + /**
16 + * Set the current user context to be an admin.
17 + * @return $this
18 + */
19 + public function asAdmin()
20 + {
21 + return $this->actingAs($this->getAdmin());
22 + }
23 +
24 + /**
25 + * Get the current admin user.
26 + * @return mixed
27 + */
28 + public function getAdmin() {
29 + if($this->admin === null) {
30 + $adminRole = Role::getSystemRole('admin');
31 + $this->admin = $adminRole->users->first();
32 + }
33 + return $this->admin;
34 + }
35 +
36 + /**
37 + * Create and return a new book.
38 + * @param array $input
39 + * @return Book
40 + */
41 + public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
42 + return $this->app[EntityRepo::class]->createFromInput('book', $input, false);
43 + }
44 +
45 + /**
46 + * Create and return a new test chapter
47 + * @param array $input
48 + * @param Book $book
49 + * @return Chapter
50 + */
51 + public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) {
52 + return $this->app[EntityRepo::class]->createFromInput('chapter', $input, $book);
53 + }
8 } 54 }
...\ No newline at end of file ...\ No newline at end of file
......