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;
15 trait SharedTestHelpers
22 * Set the current user context to be an admin.
25 public function asAdmin()
27 return $this->actingAs($this->getAdmin());
31 * Get the current admin user.
34 public function getAdmin() {
35 if($this->admin === null) {
36 $adminRole = Role::getSystemRole('admin');
37 $this->admin = $adminRole->users->first();
43 * Set the current user context to be an editor.
46 public function asEditor()
48 return $this->actingAs($this->getEditor());
56 protected function getEditor() {
57 if($this->editor === null) {
58 $editorRole = Role::getRole('editor');
59 $this->editor = $editorRole->users->first();
65 * Get an instance of a user with 'viewer' permissions
69 protected function getViewer($attributes = [])
71 $user = \BookStack\Auth\Role::getRole('viewer')->users()->first();
72 if (!empty($attributes)) $user->forceFill($attributes)->save();
77 * Regenerate the permission for an entity.
78 * @param Entity $entity
80 protected function regenEntityPermissions(Entity $entity)
82 app(PermissionService::class)->buildJointPermissionsForEntity($entity);
83 $entity->load('jointPermissions');
87 * Create and return a new bookshelf.
89 * @return \BookStack\Entities\Bookshelf
91 public function newShelf($input = ['name' => 'test shelf', 'description' => 'My new test shelf']) {
92 return app(EntityRepo::class)->createFromInput('bookshelf', $input, false);
96 * Create and return a new book.
100 public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
101 return app(EntityRepo::class)->createFromInput('book', $input, false);
105 * Create and return a new test chapter
106 * @param array $input
108 * @return \BookStack\Entities\Chapter
110 public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) {
111 return app(EntityRepo::class)->createFromInput('chapter', $input, $book);
115 * Create and return a new test page
116 * @param array $input
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);
127 * Quickly sets an array of settings.
128 * @param $settingsArray
130 protected function setSettings($settingsArray)
132 $settings = app(SettingService::class);
133 foreach ($settingsArray as $key => $value) {
134 $settings->put($key, $value);
139 * Manually set some permissions on an entity.
140 * @param Entity $entity
141 * @param array $actions
142 * @param array $roles
144 protected function setEntityRestrictions(Entity $entity, $actions = [], $roles = [])
146 $entity->restricted = true;
147 $entity->permissions()->delete();
150 foreach ($actions as $action) {
151 foreach ($roles as $role) {
153 'role_id' => $role->id,
154 'action' => strtolower($action)
158 $entity->permissions()->createMany($permissions);
161 $entity->load('permissions');
162 $this->app[PermissionService::class]->buildJointPermissionsForEntity($entity);
163 $entity->load('jointPermissions');
167 * Give the given user some permissions.
168 * @param \BookStack\Auth\User $user
169 * @param array $permissions
171 protected function giveUserPermissions(\BookStack\Auth\User $user, $permissions = [])
173 $newRole = $this->createNewRole($permissions);
174 $user->attachRole($newRole);
175 $user->load('roles');
176 $user->permissions(false);
180 * Create a new basic role for testing purposes.
181 * @param array $permissions
184 protected function createNewRole($permissions = [])
186 $permissionRepo = app(PermissionsRepo::class);
187 $roleData = factory(Role::class)->make()->toArray();
188 $roleData['permissions'] = array_flip($permissions);
189 return $permissionRepo->saveNewRole($roleData);