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