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