]> BookStack Code Mirror - bookstack/blob - tests/TestCase.php
Merge bugfixes from branch 'v0.14'
[bookstack] / tests / TestCase.php
1 <?php namespace Tests;
2
3 use BookStack\Book;
4 use BookStack\Chapter;
5 use BookStack\Repos\EntityRepo;
6 use BookStack\Role;
7 use Illuminate\Foundation\Testing\DatabaseTransactions;
8 use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
9
10 abstract class TestCase extends BaseTestCase
11 {
12     use CreatesApplication;
13     use DatabaseTransactions;
14
15     protected $admin;
16
17     /**
18      * Set the current user context to be an admin.
19      * @return $this
20      */
21     public function asAdmin()
22     {
23         return $this->actingAs($this->getAdmin());
24     }
25
26     /**
27      * Get the current admin user.
28      * @return mixed
29      */
30     public function getAdmin() {
31         if($this->admin === null) {
32             $adminRole = Role::getSystemRole('admin');
33             $this->admin = $adminRole->users->first();
34         }
35         return $this->admin;
36     }
37
38     /**
39      * Create and return a new book.
40      * @param array $input
41      * @return Book
42      */
43     public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
44         return $this->app[EntityRepo::class]->createFromInput('book', $input, false);
45     }
46
47     /**
48      * Create and return a new test chapter
49      * @param array $input
50      * @param Book $book
51      * @return Chapter
52      */
53     public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) {
54         return $this->app[EntityRepo::class]->createFromInput('chapter', $input, $book);
55     }
56 }