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