]> BookStack Code Mirror - bookstack/blob - tests/SharedTestHelpers.php
Fixes a corner case with exclamation in the ID.
[bookstack] / tests / SharedTestHelpers.php
1 <?php namespace Tests;
2
3 use BookStack\Book;
4 use BookStack\Chapter;
5 use BookStack\Entity;
6 use BookStack\Repos\EntityRepo;
7 use BookStack\Role;
8 use BookStack\Services\PermissionService;
9 use BookStack\Services\SettingService;
10
11 trait SharedTestHelpers
12 {
13
14     protected $admin;
15     protected $editor;
16
17     /**
18      * Set the current user context to be an admin.
19      * @return $this
20      */
21     public function asAdmin()
22     {
23         return $this->actingAs($this->getAdmin());
24     }
25
26     /**
27      * Get the current admin user.
28      * @return mixed
29      */
30     public function getAdmin() {
31         if($this->admin === null) {
32             $adminRole = Role::getSystemRole('admin');
33             $this->admin = $adminRole->users->first();
34         }
35         return $this->admin;
36     }
37
38     /**
39      * Set the current user context to be an editor.
40      * @return $this
41      */
42     public function asEditor()
43     {
44         return $this->actingAs($this->getEditor());
45     }
46
47
48     /**
49      * Get a editor user.
50      * @return mixed
51      */
52     protected function getEditor() {
53         if($this->editor === null) {
54             $editorRole = Role::getRole('editor');
55             $this->editor = $editorRole->users->first();
56         }
57         return $this->editor;
58     }
59
60     /**
61      * Get an instance of a user with 'viewer' permissions
62      * @param $attributes
63      * @return mixed
64      */
65     protected function getViewer($attributes = [])
66     {
67         $user = \BookStack\Role::getRole('viewer')->users()->first();
68         if (!empty($attributes)) $user->forceFill($attributes)->save();
69         return $user;
70     }
71
72     /**
73      * Create and return a new book.
74      * @param array $input
75      * @return Book
76      */
77     public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
78         return $this->app[EntityRepo::class]->createFromInput('book', $input, false);
79     }
80
81     /**
82      * Create and return a new test chapter
83      * @param array $input
84      * @param Book $book
85      * @return Chapter
86      */
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);
89     }
90
91     /**
92      * Create and return a new test page
93      * @param array $input
94      * @return Chapter
95      */
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);
101     }
102
103     /**
104      * Quickly sets an array of settings.
105      * @param $settingsArray
106      */
107     protected function setSettings($settingsArray)
108     {
109         $settings = app(SettingService::class);
110         foreach ($settingsArray as $key => $value) {
111             $settings->put($key, $value);
112         }
113     }
114
115     /**
116      * Manually set some permissions on an entity.
117      * @param Entity $entity
118      * @param array $actions
119      * @param array $roles
120      */
121     protected function setEntityRestrictions(Entity $entity, $actions = [], $roles = [])
122     {
123         $entity->restricted = true;
124         $entity->permissions()->delete();
125
126         $permissions = [];
127         foreach ($actions as $action) {
128             foreach ($roles as $role) {
129                 $permissions[] = [
130                     'role_id' => $role->id,
131                     'action' => strtolower($action)
132                 ];
133             }
134         }
135         $entity->permissions()->createMany($permissions);
136
137         $entity->save();
138         $entity->load('permissions');
139         $this->app[PermissionService::class]->buildJointPermissionsForEntity($entity);
140         $entity->load('jointPermissions');
141     }
142
143 }