]> BookStack Code Mirror - bookstack/blob - tests/TestCase.php
PSR2 fixes after running `./vendor/bin/phpcbf`
[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 BookStack\Services\SettingService;
8 use Illuminate\Foundation\Testing\DatabaseTransactions;
9 use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
10
11 abstract class TestCase extends BaseTestCase
12 {
13     use CreatesApplication;
14     use DatabaseTransactions;
15
16     protected $admin;
17     protected $editor;
18
19     /**
20      * The base URL to use while testing the application.
21      * @var string
22      */
23     protected $baseUrl = 'http://localhost';
24
25     /**
26      * Set the current user context to be an admin.
27      * @return $this
28      */
29     public function asAdmin()
30     {
31         return $this->actingAs($this->getAdmin());
32     }
33
34     /**
35      * Get the current admin user.
36      * @return mixed
37      */
38     public function getAdmin() {
39         if($this->admin === null) {
40             $adminRole = Role::getSystemRole('admin');
41             $this->admin = $adminRole->users->first();
42         }
43         return $this->admin;
44     }
45
46     /**
47      * Set the current user context to be an editor.
48      * @return $this
49      */
50     public function asEditor()
51     {
52         return $this->actingAs($this->getEditor());
53     }
54
55
56     /**
57      * Get a editor user.
58      * @return mixed
59      */
60     public function getEditor() {
61         if($this->editor === null) {
62             $editorRole = Role::getRole('editor');
63             $this->editor = $editorRole->users->first();
64         }
65         return $this->editor;
66     }
67
68     /**
69      * Get an instance of a user with 'viewer' permissions
70      * @param $attributes
71      * @return mixed
72      */
73     protected function getViewer($attributes = [])
74     {
75         $user = \BookStack\Role::getRole('viewer')->users()->first();
76         if (!empty($attributes)) $user->forceFill($attributes)->save();
77         return $user;
78     }
79
80     /**
81      * Create and return a new book.
82      * @param array $input
83      * @return Book
84      */
85     public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
86         return $this->app[EntityRepo::class]->createFromInput('book', $input, false);
87     }
88
89     /**
90      * Create and return a new test chapter
91      * @param array $input
92      * @param Book $book
93      * @return Chapter
94      */
95     public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) {
96         return $this->app[EntityRepo::class]->createFromInput('chapter', $input, $book);
97     }
98
99     /**
100      * Create and return a new test page
101      * @param array $input
102      * @return Chapter
103      */
104     public function newPage($input = ['name' => 'test page', 'html' => 'My new test page']) {
105         $book = Book::first();
106         $entityRepo = $this->app[EntityRepo::class];
107         $draftPage = $entityRepo->getDraftPage($book);
108         return $entityRepo->publishPageDraft($draftPage, $input);
109     }
110
111     /**
112      * Quickly sets an array of settings.
113      * @param $settingsArray
114      */
115     protected function setSettings($settingsArray)
116     {
117         $settings = app(SettingService::class);
118         foreach ($settingsArray as $key => $value) {
119             $settings->put($key, $value);
120         }
121     }
122 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.