4 use BookStack\Bookshelf;
8 use BookStack\Repos\EntityRepo;
9 use BookStack\Repos\PermissionsRepo;
11 use BookStack\Services\PermissionService;
12 use BookStack\Services\SettingService;
14 trait SharedTestHelpers
21 * Set the current user context to be an admin.
24 public function asAdmin()
26 return $this->actingAs($this->getAdmin());
30 * Get the current admin user.
33 public function getAdmin() {
34 if($this->admin === null) {
35 $adminRole = Role::getSystemRole('admin');
36 $this->admin = $adminRole->users->first();
42 * Set the current user context to be an editor.
45 public function asEditor()
47 return $this->actingAs($this->getEditor());
55 protected function getEditor() {
56 if($this->editor === null) {
57 $editorRole = Role::getRole('editor');
58 $this->editor = $editorRole->users->first();
64 * Get an instance of a user with 'viewer' permissions
68 protected function getViewer($attributes = [])
70 $user = \BookStack\Role::getRole('viewer')->users()->first();
71 if (!empty($attributes)) $user->forceFill($attributes)->save();
76 * Regenerate the permission for an entity.
77 * @param Entity $entity
79 protected function regenEntityPermissions(Entity $entity)
81 $this->app[PermissionService::class]->buildJointPermissionsForEntity($entity);
82 $entity->load('jointPermissions');
86 * Create and return a new bookshelf.
90 public function newShelf($input = ['name' => 'test shelf', 'description' => 'My new test shelf']) {
91 return $this->app[EntityRepo::class]->createFromInput('bookshelf', $input, false);
95 * Create and return a new book.
99 public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
100 return $this->app[EntityRepo::class]->createFromInput('book', $input, false);
104 * Create and return a new test chapter
105 * @param array $input
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);
114 * Create and return a new test page
115 * @param array $input
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);
126 * Quickly sets an array of settings.
127 * @param $settingsArray
129 protected function setSettings($settingsArray)
131 $settings = app(SettingService::class);
132 foreach ($settingsArray as $key => $value) {
133 $settings->put($key, $value);
138 * Manually set some permissions on an entity.
139 * @param Entity $entity
140 * @param array $actions
141 * @param array $roles
143 protected function setEntityRestrictions(Entity $entity, $actions = [], $roles = [])
145 $entity->restricted = true;
146 $entity->permissions()->delete();
149 foreach ($actions as $action) {
150 foreach ($roles as $role) {
152 'role_id' => $role->id,
153 'action' => strtolower($action)
157 $entity->permissions()->createMany($permissions);
160 $entity->load('permissions');
161 $this->app[PermissionService::class]->buildJointPermissionsForEntity($entity);
162 $entity->load('jointPermissions');
166 * Give the given user some permissions.
167 * @param \BookStack\User $user
168 * @param array $permissions
170 protected function giveUserPermissions(\BookStack\User $user, $permissions = [])
172 $newRole = $this->createNewRole($permissions);
173 $user->attachRole($newRole);
174 $user->load('roles');
175 $user->permissions(false);
179 * Create a new basic role for testing purposes.
180 * @param array $permissions
183 protected function createNewRole($permissions = [])
185 $permissionRepo = app(PermissionsRepo::class);
186 $roleData = factory(Role::class)->make()->toArray();
187 $roleData['permissions'] = array_flip($permissions);
188 return $permissionRepo->saveNewRole($roleData);