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 * Get a user that's not a system user such as the guest user.
53 public function getNormalUser()
55 return User::where('system_name', '=', null)->get()->last();
59 * Quickly sets an array of settings.
60 * @param $settingsArray
62 protected function setSettings($settingsArray)
64 $settings = app(SettingService::class);
65 foreach ($settingsArray as $key => $value) {
66 $settings->put($key, $value);
71 * Create a group of entities that belong to a specific user.
73 protected function createEntityChainBelongingToUser(User $creatorUser, ?User $updaterUser = null): array
75 if (empty($updaterUser)) {
76 $updaterUser = $creatorUser;
79 $userAttrs = ['created_by' => $creatorUser->id, 'owned_by' => $creatorUser->id, 'updated_by' => $updaterUser->id];
80 $book = factory(Book::class)->create($userAttrs);
81 $chapter = factory(Chapter::class)->create(array_merge(['book_id' => $book->id], $userAttrs));
82 $page = factory(Page::class)->create(array_merge(['book_id' => $book->id, 'chapter_id' => $chapter->id], $userAttrs));
83 $restrictionService = $this->app[PermissionService::class];
84 $restrictionService->buildJointPermissionsForEntity($book);
86 return compact('book', 'chapter', 'page');
90 * Helper for updating entity permissions.
91 * @param Entity $entity
93 protected function updateEntityPermissions(Entity $entity)
95 $restrictionService = $this->app[PermissionService::class];
96 $restrictionService->buildJointPermissionsForEntity($entity);
101 * Quick way to create a new user without any permissions
102 * @param array $attributes
105 protected function getNewBlankUser($attributes = [])
107 $user = factory(User::class)->create($attributes);
112 * Assert that a given string is seen inside an element.
114 * @param bool|string|null $element
115 * @param integer $position
116 * @param string $text
117 * @param bool $negate
120 protected function seeInNthElement($element, $position, $text, $negate = false)
122 $method = $negate ? 'assertDoesNotMatchRegularExpression' : 'assertMatchesRegularExpression';
124 $rawPattern = preg_quote($text, '/');
126 $escapedPattern = preg_quote(e($text), '/');
128 $content = $this->crawler->filter($element)->eq($position)->html();
130 $pattern = $rawPattern == $escapedPattern
131 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
133 $this->$method("/$pattern/i", $content);
139 * Assert that the current page matches a given URI.
144 protected function seePageUrlIs($uri)
147 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
154 * Do a forced visit that does not error out on exception.
156 * @param array $parameters
157 * @param array $cookies
158 * @param array $files
161 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
164 $uri = $this->prepareUrlForRequest($uri);
165 $this->call($method, $uri, $parameters, $cookies, $files);
166 $this->clearInputs()->followRedirects();
167 $this->currentUri = $this->app->make('request')->fullUrl();
168 $this->crawler = new Crawler($this->response->getContent(), $uri);
173 * Click the text within the selected element.
174 * @param $parentElement
178 protected function clickInElement($parentElement, $linkText)
180 $elem = $this->crawler->filter($parentElement);
181 $link = $elem->selectLink($linkText);
182 $this->visit($link->link()->getUri());
187 * Check if the page contains the given element.
188 * @param string $selector
190 protected function pageHasElement($selector)
192 $elements = $this->crawler->filter($selector);
193 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
198 * Check if the page contains the given element.
199 * @param string $selector
201 protected function pageNotHasElement($selector)
203 $elements = $this->crawler->filter($selector);
204 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);