Skip to content

Commit

Permalink
Converted sort tests to non browserkit testing
Browse files Browse the repository at this point in the history
Added testing to cover book sort endpoint.
Closes #283
  • Loading branch information
ssddanbrown committed Feb 5, 2017
1 parent 33a2999 commit 7c9937e
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 25 deletions.
4 changes: 2 additions & 2 deletions resources/views/books/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion tests/BrowserKitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function asAdmin()
*/
public function getAdmin() {
if($this->admin === null) {
$adminRole = Role::getRole('admin');
$adminRole = Role::getSystemRole('admin');
$this->admin = $adminRole->users->first();
}
return $this->admin;
Expand Down
99 changes: 77 additions & 22 deletions tests/Entity/SortTest.php
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,31 +17,36 @@ public function setUp()
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()
{
$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()
Expand All @@ -47,22 +56,68 @@ public function test_chapter_move()
$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);
}

}
46 changes: 46 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 7c9937e

Please sign in to comment.