]> BookStack Code Mirror - bookstack/blob - tests/BrowserKitTest.php
Typo fix
[bookstack] / tests / BrowserKitTest.php
1 <?php namespace Tests;
2
3 use BookStack\Entities\Entity;
4 use BookStack\Auth\Role;
5 use BookStack\Auth\Permissions\PermissionService;
6 use BookStack\Settings\SettingService;
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     use SharedTestHelpers;
17
18     /**
19      * The base URL to use while testing the application.
20      * @var string
21      */
22     protected $baseUrl = 'http://localhost';
23
24     public function tearDown()
25     {
26         \DB::disconnect();
27         parent::tearDown();
28     }
29
30     /**
31      * Creates the application.
32      *
33      * @return \Illuminate\Foundation\Application
34      */
35     public function createApplication()
36     {
37         $app = require __DIR__.'/../bootstrap/app.php';
38
39         $app->make(Kernel::class)->bootstrap();
40
41         return $app;
42     }
43
44
45     /**
46      * Get a user that's not a system user such as the guest user.
47      */
48     public function getNormalUser()
49     {
50         return \BookStack\Auth\User::where('system_name', '=', null)->get()->last();
51     }
52
53     /**
54      * Quickly sets an array of settings.
55      * @param $settingsArray
56      */
57     protected function setSettings($settingsArray)
58     {
59         $settings = app(SettingService::class);
60         foreach ($settingsArray as $key => $value) {
61             $settings->put($key, $value);
62         }
63     }
64
65     /**
66      * Create a group of entities that belong to a specific user.
67      * @param $creatorUser
68      * @param $updaterUser
69      * @return array
70      */
71     protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
72     {
73         if ($updaterUser === false) $updaterUser = $creatorUser;
74         $book = factory(\BookStack\Entities\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
75         $chapter = factory(\BookStack\Entities\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
76         $page = factory(\BookStack\Entities\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id, 'chapter_id' => $chapter->id]);
77         $restrictionService = $this->app[PermissionService::class];
78         $restrictionService->buildJointPermissionsForEntity($book);
79         return [
80             'book' => $book,
81             'chapter' => $chapter,
82             'page' => $page
83         ];
84     }
85
86     /**
87      * Helper for updating entity permissions.
88      * @param Entity $entity
89      */
90     protected function updateEntityPermissions(Entity $entity)
91     {
92         $restrictionService = $this->app[PermissionService::class];
93         $restrictionService->buildJointPermissionsForEntity($entity);
94     }
95
96
97     /**
98      * Quick way to create a new user without any permissions
99      * @param array $attributes
100      * @return mixed
101      */
102     protected function getNewBlankUser($attributes = [])
103     {
104         $user = factory(\BookStack\Auth\User::class)->create($attributes);
105         return $user;
106     }
107
108     /**
109      * Assert that a given string is seen inside an element.
110      *
111      * @param  bool|string|null $element
112      * @param  integer          $position
113      * @param  string           $text
114      * @param  bool             $negate
115      * @return $this
116      */
117     protected function seeInNthElement($element, $position, $text, $negate = false)
118     {
119         $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
120
121         $rawPattern = preg_quote($text, '/');
122
123         $escapedPattern = preg_quote(e($text), '/');
124
125         $content = $this->crawler->filter($element)->eq($position)->html();
126
127         $pattern = $rawPattern == $escapedPattern
128             ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
129
130         $this->$method("/$pattern/i", $content);
131
132         return $this;
133     }
134
135     /**
136      * Assert that the current page matches a given URI.
137      *
138      * @param  string  $uri
139      * @return $this
140      */
141     protected function seePageUrlIs($uri)
142     {
143         $this->assertEquals(
144             $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
145         );
146
147         return $this;
148     }
149
150     /**
151      * Do a forced visit that does not error out on exception.
152      * @param string $uri
153      * @param array $parameters
154      * @param array $cookies
155      * @param array $files
156      * @return $this
157      */
158     protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
159     {
160         $method = 'GET';
161         $uri = $this->prepareUrlForRequest($uri);
162         $this->call($method, $uri, $parameters, $cookies, $files);
163         $this->clearInputs()->followRedirects();
164         $this->currentUri = $this->app->make('request')->fullUrl();
165         $this->crawler = new Crawler($this->response->getContent(), $uri);
166         return $this;
167     }
168
169     /**
170      * Click the text within the selected element.
171      * @param $parentElement
172      * @param $linkText
173      * @return $this
174      */
175     protected function clickInElement($parentElement, $linkText)
176     {
177         $elem = $this->crawler->filter($parentElement);
178         $link = $elem->selectLink($linkText);
179         $this->visit($link->link()->getUri());
180         return $this;
181     }
182
183     /**
184      * Check if the page contains the given element.
185      * @param  string  $selector
186      */
187     protected function pageHasElement($selector)
188     {
189         $elements = $this->crawler->filter($selector);
190         $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
191         return $this;
192     }
193
194     /**
195      * Check if the page contains the given element.
196      * @param  string  $selector
197      */
198     protected function pageNotHasElement($selector)
199     {
200         $elements = $this->crawler->filter($selector);
201         $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);
202         return $this;
203     }
204 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.