]> BookStack Code Mirror - bookstack/blob - tests/TestCase.php
Merge branch 'master' of git://github.com/Abijeet/BookStack into Abijeet-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      * Set the current user context to be an admin.
21      * @return $this
22      */
23     public function asAdmin()
24     {
25         return $this->actingAs($this->getAdmin());
26     }
27
28     /**
29      * Get the current admin user.
30      * @return mixed
31      */
32     public function getAdmin() {
33         if($this->admin === null) {
34             $adminRole = Role::getSystemRole('admin');
35             $this->admin = $adminRole->users->first();
36         }
37         return $this->admin;
38     }
39
40     /**
41      * Set the current user context to be an editor.
42      * @return $this
43      */
44     public function asEditor()
45     {
46         return $this->actingAs($this->getEditor());
47     }
48
49
50     /**
51      * Get a editor user.
52      * @return mixed
53      */
54     public function getEditor() {
55         if($this->editor === null) {
56             $editorRole = Role::getRole('editor');
57             $this->editor = $editorRole->users->first();
58         }
59         return $this->editor;
60     }
61
62     /**
63      * Create and return a new book.
64      * @param array $input
65      * @return Book
66      */
67     public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
68         return $this->app[EntityRepo::class]->createFromInput('book', $input, false);
69     }
70
71     /**
72      * Create and return a new test chapter
73      * @param array $input
74      * @param Book $book
75      * @return Chapter
76      */
77     public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) {
78         return $this->app[EntityRepo::class]->createFromInput('chapter', $input, $book);
79     }
80
81     /**
82      * Create and return a new test page
83      * @param array $input
84      * @return Chapter
85      */
86     public function newPage($input = ['name' => 'test page', 'html' => 'My new test page']) {
87         $book = Book::first();
88         $entityRepo = $this->app[EntityRepo::class];
89         $draftPage = $entityRepo->getDraftPage($book);
90         return $entityRepo->publishPageDraft($draftPage, $input);
91     }
92
93     /**
94      * Quickly sets an array of settings.
95      * @param $settingsArray
96      */
97     protected function setSettings($settingsArray)
98     {
99         $settings = app(SettingService::class);
100         foreach ($settingsArray as $key => $value) {
101             $settings->put($key, $value);
102         }
103     }
104 }