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\Permissions\PermissionService;
8 use BookStack\Entities\Models\Page;
9 use BookStack\Settings\SettingService;
11 use Illuminate\Contracts\Console\Kernel;
12 use Illuminate\Foundation\Application;
13 use Illuminate\Foundation\Testing\DatabaseTransactions;
14 use Laravel\BrowserKitTesting\TestCase;
15 use Symfony\Component\DomCrawler\Crawler;
17 abstract class BrowserKitTest extends TestCase
20 use DatabaseTransactions;
21 use SharedTestHelpers;
24 * The base URL to use while testing the application.
27 protected $baseUrl = 'http://localhost';
29 public function tearDown() : void
36 * Creates the application.
40 public function createApplication()
42 $app = require __DIR__.'/../bootstrap/app.php';
44 $app->make(Kernel::class)->bootstrap();
51 * Quickly sets an array of settings.
52 * @param $settingsArray
54 protected function setSettings($settingsArray)
56 $settings = app(SettingService::class);
57 foreach ($settingsArray as $key => $value) {
58 $settings->put($key, $value);
63 * Create a group of entities that belong to a specific user.
65 protected function createEntityChainBelongingToUser(User $creatorUser, ?User $updaterUser = null): array
67 if (empty($updaterUser)) {
68 $updaterUser = $creatorUser;
71 $userAttrs = ['created_by' => $creatorUser->id, 'owned_by' => $creatorUser->id, 'updated_by' => $updaterUser->id];
72 $book = factory(Book::class)->create($userAttrs);
73 $chapter = factory(Chapter::class)->create(array_merge(['book_id' => $book->id], $userAttrs));
74 $page = factory(Page::class)->create(array_merge(['book_id' => $book->id, 'chapter_id' => $chapter->id], $userAttrs));
75 $restrictionService = $this->app[PermissionService::class];
76 $restrictionService->buildJointPermissionsForEntity($book);
78 return compact('book', 'chapter', 'page');
82 * Helper for updating entity permissions.
83 * @param Entity $entity
85 protected function updateEntityPermissions(Entity $entity)
87 $restrictionService = $this->app[PermissionService::class];
88 $restrictionService->buildJointPermissionsForEntity($entity);
93 * Quick way to create a new user without any permissions
94 * @param array $attributes
97 protected function getNewBlankUser($attributes = [])
99 $user = factory(User::class)->create($attributes);
104 * Assert that a given string is seen inside an element.
106 * @param bool|string|null $element
107 * @param integer $position
108 * @param string $text
109 * @param bool $negate
112 protected function seeInNthElement($element, $position, $text, $negate = false)
114 $method = $negate ? 'assertDoesNotMatchRegularExpression' : 'assertMatchesRegularExpression';
116 $rawPattern = preg_quote($text, '/');
118 $escapedPattern = preg_quote(e($text), '/');
120 $content = $this->crawler->filter($element)->eq($position)->html();
122 $pattern = $rawPattern == $escapedPattern
123 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
125 $this->$method("/$pattern/i", $content);
131 * Assert that the current page matches a given URI.
136 protected function seePageUrlIs($uri)
139 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
146 * Do a forced visit that does not error out on exception.
148 * @param array $parameters
149 * @param array $cookies
150 * @param array $files
153 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
156 $uri = $this->prepareUrlForRequest($uri);
157 $this->call($method, $uri, $parameters, $cookies, $files);
158 $this->clearInputs()->followRedirects();
159 $this->currentUri = $this->app->make('request')->fullUrl();
160 $this->crawler = new Crawler($this->response->getContent(), $uri);
165 * Click the text within the selected element.
166 * @param $parentElement
170 protected function clickInElement($parentElement, $linkText)
172 $elem = $this->crawler->filter($parentElement);
173 $link = $elem->selectLink($linkText);
174 $this->visit($link->link()->getUri());
179 * Check if the page contains the given element.
180 * @param string $selector
182 protected function pageHasElement($selector)
184 $elements = $this->crawler->filter($selector);
185 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
190 * Check if the page contains the given element.
191 * @param string $selector
193 protected function pageNotHasElement($selector)
195 $elements = $this->crawler->filter($selector);
196 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);