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