]> BookStack Code Mirror - bookstack/blob - tests/BrowserKitTest.php
Add socialite authentication for okta
[bookstack] / tests / BrowserKitTest.php
1 <?php namespace Tests;
2
3 use BookStack\Entity;
4 use BookStack\Role;
5 use BookStack\Services\PermissionService;
6 use BookStack\User;
7 use Illuminate\Contracts\Console\Kernel;
8 use Illuminate\Foundation\Testing\DatabaseTransactions;
9 use Laravel\BrowserKitTesting\TestCase;
10 use Symfony\Component\DomCrawler\Crawler;
11
12 abstract class BrowserKitTest extends TestCase
13 {
14
15     use DatabaseTransactions;
16
17     /**
18      * The base URL to use while testing the application.
19      *
20      * @var string
21      */
22     protected $baseUrl = 'http://localhost';
23
24     // Local user instances
25     private $admin;
26     private $editor;
27
28     public function tearDown()
29     {
30         \DB::disconnect();
31         parent::tearDown();
32     }
33
34     /**
35      * Creates the application.
36      *
37      * @return \Illuminate\Foundation\Application
38      */
39     public function createApplication()
40     {
41         $app = require __DIR__.'/../bootstrap/app.php';
42
43         $app->make(Kernel::class)->bootstrap();
44
45         return $app;
46     }
47
48     /**
49      * Set the current user context to be an admin.
50      * @return $this
51      */
52     public function asAdmin()
53     {
54         return $this->actingAs($this->getAdmin());
55     }
56
57     /**
58      * Get the current admin user.
59      * @return mixed
60      */
61     public function getAdmin() {
62         if($this->admin === null) {
63             $adminRole = Role::getSystemRole('admin');
64             $this->admin = $adminRole->users->first();
65         }
66         return $this->admin;
67     }
68
69     /**
70      * Set the current editor context to be an editor.
71      * @return $this
72      */
73     public function asEditor()
74     {
75         if ($this->editor === null) {
76             $this->editor = $this->getEditor();
77         }
78         return $this->actingAs($this->editor);
79     }
80
81     /**
82      * Get a user that's not a system user such as the guest user.
83      */
84     public function getNormalUser()
85     {
86         return \BookStack\User::where('system_name', '=', null)->get()->last();
87     }
88
89     /**
90      * Quickly sets an array of settings.
91      * @param $settingsArray
92      */
93     protected function setSettings($settingsArray)
94     {
95         $settings = app('BookStack\Services\SettingService');
96         foreach ($settingsArray as $key => $value) {
97             $settings->put($key, $value);
98         }
99     }
100
101     /**
102      * Create a group of entities that belong to a specific user.
103      * @param $creatorUser
104      * @param $updaterUser
105      * @return array
106      */
107     protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
108     {
109         if ($updaterUser === false) $updaterUser = $creatorUser;
110         $book = factory(\BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
111         $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
112         $page = factory(\BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id, 'chapter_id' => $chapter->id]);
113         $restrictionService = $this->app[PermissionService::class];
114         $restrictionService->buildJointPermissionsForEntity($book);
115         return [
116             'book' => $book,
117             'chapter' => $chapter,
118             'page' => $page
119         ];
120     }
121
122     /**
123      * Helper for updating entity permissions.
124      * @param Entity $entity
125      */
126     protected function updateEntityPermissions(Entity $entity)
127     {
128         $restrictionService = $this->app[PermissionService::class];
129         $restrictionService->buildJointPermissionsForEntity($entity);
130     }
131
132     /**
133      * Get an instance of a user with 'editor' permissions
134      * @param array $attributes
135      * @return mixed
136      */
137     protected function getEditor($attributes = [])
138     {
139         $user = \BookStack\Role::getRole('editor')->users()->first();
140         if (!empty($attributes)) $user->forceFill($attributes)->save();
141         return $user;
142     }
143
144     /**
145      * Get an instance of a user with 'viewer' permissions
146      * @return mixed
147      */
148     protected function getViewer()
149     {
150         $user = \BookStack\Role::getRole('viewer')->users()->first();
151         if (!empty($attributes)) $user->forceFill($attributes)->save();
152         return $user;
153     }
154
155     /**
156      * Quick way to create a new user without any permissions
157      * @param array $attributes
158      * @return mixed
159      */
160     protected function getNewBlankUser($attributes = [])
161     {
162         $user = factory(\BookStack\User::class)->create($attributes);
163         return $user;
164     }
165
166     /**
167      * Assert that a given string is seen inside an element.
168      *
169      * @param  bool|string|null $element
170      * @param  integer          $position
171      * @param  string           $text
172      * @param  bool             $negate
173      * @return $this
174      */
175     protected function seeInNthElement($element, $position, $text, $negate = false)
176     {
177         $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
178
179         $rawPattern = preg_quote($text, '/');
180
181         $escapedPattern = preg_quote(e($text), '/');
182
183         $content = $this->crawler->filter($element)->eq($position)->html();
184
185         $pattern = $rawPattern == $escapedPattern
186             ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
187
188         $this->$method("/$pattern/i", $content);
189
190         return $this;
191     }
192
193     /**
194      * Assert that the current page matches a given URI.
195      *
196      * @param  string  $uri
197      * @return $this
198      */
199     protected function seePageUrlIs($uri)
200     {
201         $this->assertEquals(
202             $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
203         );
204
205         return $this;
206     }
207
208     /**
209      * Do a forced visit that does not error out on exception.
210      * @param string $uri
211      * @param array $parameters
212      * @param array $cookies
213      * @param array $files
214      * @return $this
215      */
216     protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
217     {
218         $method = 'GET';
219         $uri = $this->prepareUrlForRequest($uri);
220         $this->call($method, $uri, $parameters, $cookies, $files);
221         $this->clearInputs()->followRedirects();
222         $this->currentUri = $this->app->make('request')->fullUrl();
223         $this->crawler = new Crawler($this->response->getContent(), $uri);
224         return $this;
225     }
226
227     /**
228      * Click the text within the selected element.
229      * @param $parentElement
230      * @param $linkText
231      * @return $this
232      */
233     protected function clickInElement($parentElement, $linkText)
234     {
235         $elem = $this->crawler->filter($parentElement);
236         $link = $elem->selectLink($linkText);
237         $this->visit($link->link()->getUri());
238         return $this;
239     }
240
241     /**
242      * Check if the page contains the given element.
243      * @param  string  $selector
244      */
245     protected function pageHasElement($selector)
246     {
247         $elements = $this->crawler->filter($selector);
248         $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
249         return $this;
250     }
251
252     /**
253      * Check if the page contains the given element.
254      * @param  string  $selector
255      */
256     protected function pageNotHasElement($selector)
257     {
258         $elements = $this->crawler->filter($selector);
259         $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);
260         return $this;
261     }
262 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.