]> BookStack Code Mirror - bookstack/blob - tests/TestCase.php
Converted sort tests to non browserkit testing
[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\TestCase as BaseTestCase;
8
9 abstract class TestCase extends BaseTestCase
10 {
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     }
54 }