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;
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;
19 abstract class BrowserKitTest extends TestCase
21 use DatabaseTransactions;
22 use SharedTestHelpers;
25 * The base URL to use while testing the application.
29 protected $baseUrl = 'http://localhost';
31 public function tearDown(): void
38 * Creates the application.
42 public function createApplication()
44 $app = require __DIR__ . '/../bootstrap/app.php';
46 $app->make(Kernel::class)->bootstrap();
52 * Quickly sets an array of settings.
54 * @param $settingsArray
56 protected function setSettings($settingsArray)
58 $settings = app(SettingService::class);
59 foreach ($settingsArray as $key => $value) {
60 $settings->put($key, $value);
65 * Helper for updating entity permissions.
67 * @param Entity $entity
69 protected function updateEntityPermissions(Entity $entity)
71 $restrictionService = $this->app[PermissionService::class];
72 $restrictionService->buildJointPermissionsForEntity($entity);
76 * Assert that a given string is seen inside an element.
78 * @param bool|string|null $element
79 * @param int $position
85 protected function seeInNthElement($element, $position, $text, $negate = false)
87 $method = $negate ? 'assertDoesNotMatchRegularExpression' : 'assertMatchesRegularExpression';
89 $rawPattern = preg_quote($text, '/');
91 $escapedPattern = preg_quote(e($text), '/');
93 $content = $this->crawler->filter($element)->eq($position)->html();
95 $pattern = $rawPattern == $escapedPattern
96 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
98 $this->$method("/$pattern/i", $content);
104 * Assert that the current page matches a given URI.
110 protected function seePageUrlIs($uri)
115 "Did not land on expected page [{$uri}].\n"
122 * Do a forced visit that does not error out on exception.
125 * @param array $parameters
126 * @param array $cookies
127 * @param array $files
131 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
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);
144 * Click the text within the selected element.
146 * @param $parentElement
151 protected function clickInElement($parentElement, $linkText)
153 $elem = $this->crawler->filter($parentElement);
154 $link = $elem->selectLink($linkText);
155 $this->visit($link->link()->getUri());
161 * Check if the page contains the given element.
163 * @param string $selector
165 protected function pageHasElement($selector)
167 $elements = $this->crawler->filter($selector);
168 $this->assertTrue(count($elements) > 0, 'The page does not contain an element matching ' . $selector);
174 * Check if the page contains the given element.
176 * @param string $selector
178 protected function pageNotHasElement($selector)
180 $elements = $this->crawler->filter($selector);
181 $this->assertFalse(count($elements) > 0, 'The page contains ' . count($elements) . ' elements matching ' . $selector);