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