3 use BookStack\Entities\Book;
4 use BookStack\Entities\Entity;
5 use BookStack\Entities\Page;
6 use BookStack\Entities\Repos\EntityRepo;
7 use BookStack\Auth\Permissions\PermissionsRepo;
8 use BookStack\Auth\Role;
9 use BookStack\Auth\Permissions\PermissionService;
10 use BookStack\Entities\Repos\PageRepo;
11 use BookStack\Settings\SettingService;
12 use BookStack\Uploads\HttpFetcher;
13 use Illuminate\Support\Env;
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
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
121 public function newPage($input = ['name' => 'test page', 'html' => 'My new test page']) {
122 $book = Book::first();
123 $pageRepo = app(PageRepo::class);
124 $draftPage = $pageRepo->getDraftPage($book);
125 return $pageRepo->publishPageDraft($draftPage, $input);
129 * Quickly sets an array of settings.
130 * @param $settingsArray
132 protected function setSettings($settingsArray)
134 $settings = app(SettingService::class);
135 foreach ($settingsArray as $key => $value) {
136 $settings->put($key, $value);
141 * Manually set some permissions on an entity.
142 * @param Entity $entity
143 * @param array $actions
144 * @param array $roles
146 protected function setEntityRestrictions(Entity $entity, $actions = [], $roles = [])
148 $entity->restricted = true;
149 $entity->permissions()->delete();
152 foreach ($actions as $action) {
153 foreach ($roles as $role) {
155 'role_id' => $role->id,
156 'action' => strtolower($action)
160 $entity->permissions()->createMany($permissions);
163 $entity->load('permissions');
164 $this->app[PermissionService::class]->buildJointPermissionsForEntity($entity);
165 $entity->load('jointPermissions');
169 * Give the given user some permissions.
170 * @param \BookStack\Auth\User $user
171 * @param array $permissions
173 protected function giveUserPermissions(\BookStack\Auth\User $user, $permissions = [])
175 $newRole = $this->createNewRole($permissions);
176 $user->attachRole($newRole);
177 $user->load('roles');
178 $user->permissions(false);
182 * Create a new basic role for testing purposes.
183 * @param array $permissions
186 protected function createNewRole($permissions = [])
188 $permissionRepo = app(PermissionsRepo::class);
189 $roleData = factory(Role::class)->make()->toArray();
190 $roleData['permissions'] = array_flip($permissions);
191 return $permissionRepo->saveNewRole($roleData);
195 * Mock the HttpFetcher service and return the given data on fetch.
199 protected function mockHttpFetch($returnData, int $times = 1)
201 $mockHttp = \Mockery::mock(HttpFetcher::class);
202 $this->app[HttpFetcher::class] = $mockHttp;
203 $mockHttp->shouldReceive('fetch')
205 ->andReturn($returnData);
209 * Run a set test with the given env variable.
210 * Remembers the original and resets the value after test.
211 * @param string $name
213 * @param callable $callback
215 protected function runWithEnv(string $name, $value, callable $callback)
217 Env::disablePutenv();
218 $originalVal = $_ENV[$name] ?? null;
220 if (is_null($value)) {
222 unset($_SERVER[$name]);
224 $_ENV[$name] = $value;
225 $_SERVER[$name] = $value;
228 $this->refreshApplication();
231 if (is_null($originalVal)) {
232 unset($_SERVER[$name]);
235 $_SERVER[$name] = $originalVal;
236 $_ENV[$name] = $originalVal;
241 * Check the keys and properties in the given map to include
242 * exist, albeit not exclusively, within the map to check.
243 * @param array $mapToInclude
244 * @param array $mapToCheck
245 * @param string $message
247 protected function assertArrayMapIncludes(array $mapToInclude, array $mapToCheck, string $message = '') : void
251 foreach ($mapToInclude as $key => $value) {
252 if (!isset($mapToCheck[$key]) || $mapToCheck[$key] !== $mapToInclude[$key]) {
257 $toIncludeStr = print_r($mapToInclude, true);
258 $toCheckStr = print_r($mapToCheck, true);
259 self::assertThat($passed, self::isTrue(), "Failed asserting that given map:\n\n{$toCheckStr}\n\nincludes:\n\n{$toIncludeStr}");