]> BookStack Code Mirror - bookstack/blob - tests/TestCase.php
Merge pull request #679 from marcusforsberg/master
[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      * Create and return a new book.
70      * @param array $input
71      * @return Book
72      */
73     public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
74         return $this->app[EntityRepo::class]->createFromInput('book', $input, false);
75     }
76
77     /**
78      * Create and return a new test chapter
79      * @param array $input
80      * @param Book $book
81      * @return Chapter
82      */
83     public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) {
84         return $this->app[EntityRepo::class]->createFromInput('chapter', $input, $book);
85     }
86
87     /**
88      * Create and return a new test page
89      * @param array $input
90      * @return Chapter
91      */
92     public function newPage($input = ['name' => 'test page', 'html' => 'My new test page']) {
93         $book = Book::first();
94         $entityRepo = $this->app[EntityRepo::class];
95         $draftPage = $entityRepo->getDraftPage($book);
96         return $entityRepo->publishPageDraft($draftPage, $input);
97     }
98
99     /**
100      * Quickly sets an array of settings.
101      * @param $settingsArray
102      */
103     protected function setSettings($settingsArray)
104     {
105         $settings = app(SettingService::class);
106         foreach ($settingsArray as $key => $value) {
107             $settings->put($key, $value);
108         }
109     }
110 }