6 use BookStack\Repos\EntityRepo;
8 use BookStack\Services\PermissionService;
9 use BookStack\Services\SettingService;
11 trait SharedTestHelpers
18 * Set the current user context to be an admin.
21 public function asAdmin()
23 return $this->actingAs($this->getAdmin());
27 * Get the current admin user.
30 public function getAdmin() {
31 if($this->admin === null) {
32 $adminRole = Role::getSystemRole('admin');
33 $this->admin = $adminRole->users->first();
39 * Set the current user context to be an editor.
42 public function asEditor()
44 return $this->actingAs($this->getEditor());
52 protected function getEditor() {
53 if($this->editor === null) {
54 $editorRole = Role::getRole('editor');
55 $this->editor = $editorRole->users->first();
61 * Get an instance of a user with 'viewer' permissions
65 protected function getViewer($attributes = [])
67 $user = \BookStack\Role::getRole('viewer')->users()->first();
68 if (!empty($attributes)) $user->forceFill($attributes)->save();
73 * Create and return a new book.
77 public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
78 return $this->app[EntityRepo::class]->createFromInput('book', $input, false);
82 * Create and return a new test chapter
87 public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) {
88 return $this->app[EntityRepo::class]->createFromInput('chapter', $input, $book);
92 * Create and return a new test page
96 public function newPage($input = ['name' => 'test page', 'html' => 'My new test page']) {
97 $book = Book::first();
98 $entityRepo = $this->app[EntityRepo::class];
99 $draftPage = $entityRepo->getDraftPage($book);
100 return $entityRepo->publishPageDraft($draftPage, $input);
104 * Quickly sets an array of settings.
105 * @param $settingsArray
107 protected function setSettings($settingsArray)
109 $settings = app(SettingService::class);
110 foreach ($settingsArray as $key => $value) {
111 $settings->put($key, $value);
116 * Manually set some permissions on an entity.
117 * @param Entity $entity
118 * @param array $actions
119 * @param array $roles
121 protected function setEntityRestrictions(Entity $entity, $actions = [], $roles = [])
123 $entity->restricted = true;
124 $entity->permissions()->delete();
127 foreach ($actions as $action) {
128 foreach ($roles as $role) {
130 'role_id' => $role->id,
131 'action' => strtolower($action)
135 $entity->permissions()->createMany($permissions);
138 $entity->load('permissions');
139 $this->app[PermissionService::class]->buildJointPermissionsForEntity($entity);
140 $entity->load('jointPermissions');