]> BookStack Code Mirror - bookstack/blob - tests/SharedTestHelpers.php
Fix pt_BR translations
[bookstack] / tests / SharedTestHelpers.php
1 <?php namespace Tests;
2
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Bookshelf;
5 use BookStack\Entities\Chapter;
6 use BookStack\Entities\Entity;
7 use BookStack\Entities\Page;
8 use BookStack\Entities\Repos\EntityRepo;
9 use BookStack\Auth\Permissions\PermissionsRepo;
10 use BookStack\Auth\Role;
11 use BookStack\Auth\Permissions\PermissionService;
12 use BookStack\Entities\Repos\PageRepo;
13 use BookStack\Settings\SettingService;
14
15 trait SharedTestHelpers
16 {
17
18     protected $admin;
19     protected $editor;
20
21     /**
22      * Set the current user context to be an admin.
23      * @return $this
24      */
25     public function asAdmin()
26     {
27         return $this->actingAs($this->getAdmin());
28     }
29
30     /**
31      * Get the current admin user.
32      * @return mixed
33      */
34     public function getAdmin() {
35         if($this->admin === null) {
36             $adminRole = Role::getSystemRole('admin');
37             $this->admin = $adminRole->users->first();
38         }
39         return $this->admin;
40     }
41
42     /**
43      * Set the current user context to be an editor.
44      * @return $this
45      */
46     public function asEditor()
47     {
48         return $this->actingAs($this->getEditor());
49     }
50
51
52     /**
53      * Get a editor user.
54      * @return mixed
55      */
56     protected function getEditor() {
57         if($this->editor === null) {
58             $editorRole = Role::getRole('editor');
59             $this->editor = $editorRole->users->first();
60         }
61         return $this->editor;
62     }
63
64     /**
65      * Get an instance of a user with 'viewer' permissions
66      * @param $attributes
67      * @return mixed
68      */
69     protected function getViewer($attributes = [])
70     {
71         $user = \BookStack\Auth\Role::getRole('viewer')->users()->first();
72         if (!empty($attributes)) $user->forceFill($attributes)->save();
73         return $user;
74     }
75
76     /**
77      * Regenerate the permission for an entity.
78      * @param Entity $entity
79      */
80     protected function regenEntityPermissions(Entity $entity)
81     {
82         app(PermissionService::class)->buildJointPermissionsForEntity($entity);
83         $entity->load('jointPermissions');
84     }
85
86     /**
87      * Create and return a new bookshelf.
88      * @param array $input
89      * @return \BookStack\Entities\Bookshelf
90      */
91     public function newShelf($input = ['name' => 'test shelf', 'description' => 'My new test shelf']) {
92         return app(EntityRepo::class)->createFromInput('bookshelf', $input, false);
93     }
94
95     /**
96      * Create and return a new book.
97      * @param array $input
98      * @return Book
99      */
100     public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
101         return app(EntityRepo::class)->createFromInput('book', $input, false);
102     }
103
104     /**
105      * Create and return a new test chapter
106      * @param array $input
107      * @param Book $book
108      * @return \BookStack\Entities\Chapter
109      */
110     public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) {
111         return app(EntityRepo::class)->createFromInput('chapter', $input, $book);
112     }
113
114     /**
115      * Create and return a new test page
116      * @param array $input
117      * @return Page
118      */
119     public function newPage($input = ['name' => 'test page', 'html' => 'My new test page']) {
120         $book = Book::first();
121         $pageRepo = app(PageRepo::class);
122         $draftPage = $pageRepo->getDraftPage($book);
123         return $pageRepo->publishPageDraft($draftPage, $input);
124     }
125
126     /**
127      * Quickly sets an array of settings.
128      * @param $settingsArray
129      */
130     protected function setSettings($settingsArray)
131     {
132         $settings = app(SettingService::class);
133         foreach ($settingsArray as $key => $value) {
134             $settings->put($key, $value);
135         }
136     }
137
138     /**
139      * Manually set some permissions on an entity.
140      * @param Entity $entity
141      * @param array $actions
142      * @param array $roles
143      */
144     protected function setEntityRestrictions(Entity $entity, $actions = [], $roles = [])
145     {
146         $entity->restricted = true;
147         $entity->permissions()->delete();
148
149         $permissions = [];
150         foreach ($actions as $action) {
151             foreach ($roles as $role) {
152                 $permissions[] = [
153                     'role_id' => $role->id,
154                     'action' => strtolower($action)
155                 ];
156             }
157         }
158         $entity->permissions()->createMany($permissions);
159
160         $entity->save();
161         $entity->load('permissions');
162         $this->app[PermissionService::class]->buildJointPermissionsForEntity($entity);
163         $entity->load('jointPermissions');
164     }
165
166     /**
167      * Give the given user some permissions.
168      * @param \BookStack\Auth\User $user
169      * @param array $permissions
170      */
171     protected function giveUserPermissions(\BookStack\Auth\User $user, $permissions = [])
172     {
173         $newRole = $this->createNewRole($permissions);
174         $user->attachRole($newRole);
175         $user->load('roles');
176         $user->permissions(false);
177     }
178
179     /**
180      * Create a new basic role for testing purposes.
181      * @param array $permissions
182      * @return Role
183      */
184     protected function createNewRole($permissions = [])
185     {
186         $permissionRepo = app(PermissionsRepo::class);
187         $roleData = factory(Role::class)->make()->toArray();
188         $roleData['permissions'] = array_flip($permissions);
189         return $permissionRepo->saveNewRole($roleData);
190     }
191
192 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.