4 use BookStack\Bookshelf;
7 use BookStack\Repos\EntityRepo;
8 use BookStack\Repos\PermissionsRepo;
10 use BookStack\Services\PermissionService;
11 use BookStack\Services\SettingService;
13 trait SharedTestHelpers
20 * Set the current user context to be an admin.
23 public function asAdmin()
25 return $this->actingAs($this->getAdmin());
29 * Get the current admin user.
32 public function getAdmin() {
33 if($this->admin === null) {
34 $adminRole = Role::getSystemRole('admin');
35 $this->admin = $adminRole->users->first();
41 * Set the current user context to be an editor.
44 public function asEditor()
46 return $this->actingAs($this->getEditor());
54 protected function getEditor() {
55 if($this->editor === null) {
56 $editorRole = Role::getRole('editor');
57 $this->editor = $editorRole->users->first();
63 * Get an instance of a user with 'viewer' permissions
67 protected function getViewer($attributes = [])
69 $user = \BookStack\Role::getRole('viewer')->users()->first();
70 if (!empty($attributes)) $user->forceFill($attributes)->save();
75 * Regenerate the permission for an entity.
76 * @param Entity $entity
78 protected function regenEntityPermissions(Entity $entity)
80 $this->app[PermissionService::class]->buildJointPermissionsForEntity($entity);
81 $entity->load('jointPermissions');
85 * Create and return a new bookshelf.
89 public function newShelf($input = ['name' => 'test shelf', 'description' => 'My new test shelf']) {
90 return $this->app[EntityRepo::class]->createFromInput('bookshelf', $input, false);
94 * Create and return a new book.
98 public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
99 return $this->app[EntityRepo::class]->createFromInput('book', $input, false);
103 * Create and return a new test chapter
104 * @param array $input
108 public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) {
109 return $this->app[EntityRepo::class]->createFromInput('chapter', $input, $book);
113 * Create and return a new test page
114 * @param array $input
117 public function newPage($input = ['name' => 'test page', 'html' => 'My new test page']) {
118 $book = Book::first();
119 $entityRepo = $this->app[EntityRepo::class];
120 $draftPage = $entityRepo->getDraftPage($book);
121 return $entityRepo->publishPageDraft($draftPage, $input);
125 * Quickly sets an array of settings.
126 * @param $settingsArray
128 protected function setSettings($settingsArray)
130 $settings = app(SettingService::class);
131 foreach ($settingsArray as $key => $value) {
132 $settings->put($key, $value);
137 * Manually set some permissions on an entity.
138 * @param Entity $entity
139 * @param array $actions
140 * @param array $roles
142 protected function setEntityRestrictions(Entity $entity, $actions = [], $roles = [])
144 $entity->restricted = true;
145 $entity->permissions()->delete();
148 foreach ($actions as $action) {
149 foreach ($roles as $role) {
151 'role_id' => $role->id,
152 'action' => strtolower($action)
156 $entity->permissions()->createMany($permissions);
159 $entity->load('permissions');
160 $this->app[PermissionService::class]->buildJointPermissionsForEntity($entity);
161 $entity->load('jointPermissions');
165 * Give the given user some permissions.
166 * @param \BookStack\User $user
167 * @param array $permissions
169 protected function giveUserPermissions(\BookStack\User $user, $permissions = [])
171 $newRole = $this->createNewRole($permissions);
172 $user->attachRole($newRole);
173 $user->load('roles');
174 $user->permissions(false);
178 * Create a new basic role for testing purposes.
179 * @param array $permissions
182 protected function createNewRole($permissions = [])
184 $permissionRepo = app(PermissionsRepo::class);
185 $roleData = factory(Role::class)->make()->toArray();
186 $roleData['permissions'] = array_flip($permissions);
187 return $permissionRepo->saveNewRole($roleData);