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 * Create a group of entities that belong to a specific user.
67 protected function createEntityChainBelongingToUser(User $creatorUser, ?User $updaterUser = null): array
69 if (empty($updaterUser)) {
70 $updaterUser = $creatorUser;
73 $userAttrs = ['created_by' => $creatorUser->id, 'owned_by' => $creatorUser->id, 'updated_by' => $updaterUser->id];
74 $book = factory(Book::class)->create($userAttrs);
75 $chapter = factory(Chapter::class)->create(array_merge(['book_id' => $book->id], $userAttrs));
76 $page = factory(Page::class)->create(array_merge(['book_id' => $book->id, 'chapter_id' => $chapter->id], $userAttrs));
77 $restrictionService = $this->app[PermissionService::class];
78 $restrictionService->buildJointPermissionsForEntity($book);
80 return compact('book', 'chapter', 'page');
84 * Helper for updating entity permissions.
86 * @param Entity $entity
88 protected function updateEntityPermissions(Entity $entity)
90 $restrictionService = $this->app[PermissionService::class];
91 $restrictionService->buildJointPermissionsForEntity($entity);
95 * Quick way to create a new user without any permissions.
97 * @param array $attributes
101 protected function getNewBlankUser($attributes = [])
103 $user = factory(User::class)->create($attributes);
109 * Assert that a given string is seen inside an element.
111 * @param bool|string|null $element
112 * @param int $position
113 * @param string $text
114 * @param bool $negate
118 protected function seeInNthElement($element, $position, $text, $negate = false)
120 $method = $negate ? 'assertDoesNotMatchRegularExpression' : 'assertMatchesRegularExpression';
122 $rawPattern = preg_quote($text, '/');
124 $escapedPattern = preg_quote(e($text), '/');
126 $content = $this->crawler->filter($element)->eq($position)->html();
128 $pattern = $rawPattern == $escapedPattern
129 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
131 $this->$method("/$pattern/i", $content);
137 * Assert that the current page matches a given URI.
143 protected function seePageUrlIs($uri)
148 "Did not land on expected page [{$uri}].\n"
155 * Do a forced visit that does not error out on exception.
158 * @param array $parameters
159 * @param array $cookies
160 * @param array $files
164 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
167 $uri = $this->prepareUrlForRequest($uri);
168 $this->call($method, $uri, $parameters, $cookies, $files);
169 $this->clearInputs()->followRedirects();
170 $this->currentUri = $this->app->make('request')->fullUrl();
171 $this->crawler = new Crawler($this->response->getContent(), $uri);
177 * Click the text within the selected element.
179 * @param $parentElement
184 protected function clickInElement($parentElement, $linkText)
186 $elem = $this->crawler->filter($parentElement);
187 $link = $elem->selectLink($linkText);
188 $this->visit($link->link()->getUri());
194 * Check if the page contains the given element.
196 * @param string $selector
198 protected function pageHasElement($selector)
200 $elements = $this->crawler->filter($selector);
201 $this->assertTrue(count($elements) > 0, 'The page does not contain an element matching ' . $selector);
207 * Check if the page contains the given element.
209 * @param string $selector
211 protected function pageNotHasElement($selector)
213 $elements = $this->crawler->filter($selector);
214 $this->assertFalse(count($elements) > 0, 'The page contains ' . count($elements) . ' elements matching ' . $selector);