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