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