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