]> BookStack Code Mirror - bookstack/blob - tests/SharedTestHelpers.php
Updated 404 test to not fail based on random long name
[bookstack] / tests / SharedTestHelpers.php
1 <?php namespace Tests;
2
3 use BookStack\Book;
4 use BookStack\Bookshelf;
5 use BookStack\Chapter;
6 use BookStack\Entity;
7 use BookStack\Repos\EntityRepo;
8 use BookStack\Repos\PermissionsRepo;
9 use BookStack\Role;
10 use BookStack\Services\PermissionService;
11 use BookStack\Services\SettingService;
12
13 trait SharedTestHelpers
14 {
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     protected 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      * Get an instance of a user with 'viewer' permissions
64      * @param $attributes
65      * @return mixed
66      */
67     protected function getViewer($attributes = [])
68     {
69         $user = \BookStack\Role::getRole('viewer')->users()->first();
70         if (!empty($attributes)) $user->forceFill($attributes)->save();
71         return $user;
72     }
73
74     /**
75      * Regenerate the permission for an entity.
76      * @param Entity $entity
77      */
78     protected function regenEntityPermissions(Entity $entity)
79     {
80         $this->app[PermissionService::class]->buildJointPermissionsForEntity($entity);
81         $entity->load('jointPermissions');
82     }
83
84     /**
85      * Create and return a new bookshelf.
86      * @param array $input
87      * @return Bookshelf
88      */
89     public function newShelf($input = ['name' => 'test shelf', 'description' => 'My new test shelf']) {
90         return $this->app[EntityRepo::class]->createFromInput('bookshelf', $input, false);
91     }
92
93     /**
94      * Create and return a new book.
95      * @param array $input
96      * @return Book
97      */
98     public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
99         return $this->app[EntityRepo::class]->createFromInput('book', $input, false);
100     }
101
102     /**
103      * Create and return a new test chapter
104      * @param array $input
105      * @param Book $book
106      * @return Chapter
107      */
108     public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) {
109         return $this->app[EntityRepo::class]->createFromInput('chapter', $input, $book);
110     }
111
112     /**
113      * Create and return a new test page
114      * @param array $input
115      * @return Chapter
116      */
117     public function newPage($input = ['name' => 'test page', 'html' => 'My new test page']) {
118         $book = Book::first();
119         $entityRepo = $this->app[EntityRepo::class];
120         $draftPage = $entityRepo->getDraftPage($book);
121         return $entityRepo->publishPageDraft($draftPage, $input);
122     }
123
124     /**
125      * Quickly sets an array of settings.
126      * @param $settingsArray
127      */
128     protected function setSettings($settingsArray)
129     {
130         $settings = app(SettingService::class);
131         foreach ($settingsArray as $key => $value) {
132             $settings->put($key, $value);
133         }
134     }
135
136     /**
137      * Manually set some permissions on an entity.
138      * @param Entity $entity
139      * @param array $actions
140      * @param array $roles
141      */
142     protected function setEntityRestrictions(Entity $entity, $actions = [], $roles = [])
143     {
144         $entity->restricted = true;
145         $entity->permissions()->delete();
146
147         $permissions = [];
148         foreach ($actions as $action) {
149             foreach ($roles as $role) {
150                 $permissions[] = [
151                     'role_id' => $role->id,
152                     'action' => strtolower($action)
153                 ];
154             }
155         }
156         $entity->permissions()->createMany($permissions);
157
158         $entity->save();
159         $entity->load('permissions');
160         $this->app[PermissionService::class]->buildJointPermissionsForEntity($entity);
161         $entity->load('jointPermissions');
162     }
163
164     /**
165      * Give the given user some permissions.
166      * @param \BookStack\User $user
167      * @param array $permissions
168      */
169     protected function giveUserPermissions(\BookStack\User $user, $permissions = [])
170     {
171         $newRole = $this->createNewRole($permissions);
172         $user->attachRole($newRole);
173         $user->load('roles');
174         $user->permissions(false);
175     }
176
177     /**
178      * Create a new basic role for testing purposes.
179      * @param array $permissions
180      * @return Role
181      */
182     protected function createNewRole($permissions = [])
183     {
184         $permissionRepo = app(PermissionsRepo::class);
185         $roleData = factory(Role::class)->make()->toArray();
186         $roleData['permissions'] = array_flip($permissions);
187         return $permissionRepo->saveNewRole($roleData);
188     }
189
190 }