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;
16 trait SharedTestHelpers
23 * Set the current user context to be an admin.
26 public function asAdmin()
28 return $this->actingAs($this->getAdmin());
32 * Get the current admin user.
35 public function getAdmin() {
36 if($this->admin === null) {
37 $adminRole = Role::getSystemRole('admin');
38 $this->admin = $adminRole->users->first();
44 * Set the current user context to be an editor.
47 public function asEditor()
49 return $this->actingAs($this->getEditor());
57 protected function getEditor() {
58 if($this->editor === null) {
59 $editorRole = Role::getRole('editor');
60 $this->editor = $editorRole->users->first();
66 * Get an instance of a user with 'viewer' permissions
70 protected function getViewer($attributes = [])
72 $user = \BookStack\Auth\Role::getRole('viewer')->users()->first();
73 if (!empty($attributes)) $user->forceFill($attributes)->save();
78 * Regenerate the permission for an entity.
79 * @param Entity $entity
81 protected function regenEntityPermissions(Entity $entity)
83 app(PermissionService::class)->buildJointPermissionsForEntity($entity);
84 $entity->load('jointPermissions');
88 * Create and return a new bookshelf.
90 * @return \BookStack\Entities\Bookshelf
92 public function newShelf($input = ['name' => 'test shelf', 'description' => 'My new test shelf']) {
93 return app(EntityRepo::class)->createFromInput('bookshelf', $input, false);
97 * Create and return a new book.
101 public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
102 return app(EntityRepo::class)->createFromInput('book', $input, false);
106 * Create and return a new test chapter
107 * @param array $input
109 * @return \BookStack\Entities\Chapter
111 public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) {
112 return app(EntityRepo::class)->createFromInput('chapter', $input, $book);
116 * Create and return a new test page
117 * @param array $input
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);
128 * Quickly sets an array of settings.
129 * @param $settingsArray
131 protected function setSettings($settingsArray)
133 $settings = app(SettingService::class);
134 foreach ($settingsArray as $key => $value) {
135 $settings->put($key, $value);
140 * Manually set some permissions on an entity.
141 * @param Entity $entity
142 * @param array $actions
143 * @param array $roles
145 protected function setEntityRestrictions(Entity $entity, $actions = [], $roles = [])
147 $entity->restricted = true;
148 $entity->permissions()->delete();
151 foreach ($actions as $action) {
152 foreach ($roles as $role) {
154 'role_id' => $role->id,
155 'action' => strtolower($action)
159 $entity->permissions()->createMany($permissions);
162 $entity->load('permissions');
163 $this->app[PermissionService::class]->buildJointPermissionsForEntity($entity);
164 $entity->load('jointPermissions');
168 * Give the given user some permissions.
169 * @param \BookStack\Auth\User $user
170 * @param array $permissions
172 protected function giveUserPermissions(\BookStack\Auth\User $user, $permissions = [])
174 $newRole = $this->createNewRole($permissions);
175 $user->attachRole($newRole);
176 $user->load('roles');
177 $user->permissions(false);
181 * Create a new basic role for testing purposes.
182 * @param array $permissions
185 protected function createNewRole($permissions = [])
187 $permissionRepo = app(PermissionsRepo::class);
188 $roleData = factory(Role::class)->make()->toArray();
189 $roleData['permissions'] = array_flip($permissions);
190 return $permissionRepo->saveNewRole($roleData);
194 * Mock the HttpFetcher service and return the given data on fetch.
198 protected function mockHttpFetch($returnData, int $times = 1)
200 $mockHttp = \Mockery::mock(HttpFetcher::class);
201 $this->app[HttpFetcher::class] = $mockHttp;
202 $mockHttp->shouldReceive('fetch')
204 ->andReturn($returnData);