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\Role;
8 use BookStack\Auth\Permissions\PermissionService;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Settings\SettingService;
12 use Illuminate\Contracts\Console\Kernel;
13 use Illuminate\Foundation\Application;
14 use Illuminate\Foundation\Testing\DatabaseTransactions;
15 use Laravel\BrowserKitTesting\TestCase;
16 use Symfony\Component\DomCrawler\Crawler;
18 abstract class BrowserKitTest extends TestCase
21 use DatabaseTransactions;
22 use SharedTestHelpers;
25 * The base URL to use while testing the application.
28 protected $baseUrl = 'http://localhost';
30 public function tearDown() : void
37 * Creates the application.
41 public function createApplication()
43 $app = require __DIR__.'/../bootstrap/app.php';
45 $app->make(Kernel::class)->bootstrap();
52 * Get a user that's not a system user such as the guest user.
54 public function getNormalUser()
56 return User::where('system_name', '=', null)->get()->last();
60 * Quickly sets an array of settings.
61 * @param $settingsArray
63 protected function setSettings($settingsArray)
65 $settings = app(SettingService::class);
66 foreach ($settingsArray as $key => $value) {
67 $settings->put($key, $value);
72 * Create a group of entities that belong to a specific user.
74 protected function createEntityChainBelongingToUser(User $creatorUser, ?User $updaterUser = null): array
76 if (empty($updaterUser)) {
77 $updaterUser = $creatorUser;
80 $userAttrs = ['created_by' => $creatorUser->id, 'owned_by' => $creatorUser->id, 'updated_by' => $updaterUser->id];
81 $book = factory(Book::class)->create($userAttrs);
82 $chapter = factory(Chapter::class)->create(array_merge(['book_id' => $book->id], $userAttrs));
83 $page = factory(Page::class)->create(array_merge(['book_id' => $book->id, 'chapter_id' => $chapter->id], $userAttrs));
84 $restrictionService = $this->app[PermissionService::class];
85 $restrictionService->buildJointPermissionsForEntity($book);
87 return compact('book', 'chapter', 'page');
91 * Helper for updating entity permissions.
92 * @param Entity $entity
94 protected function updateEntityPermissions(Entity $entity)
96 $restrictionService = $this->app[PermissionService::class];
97 $restrictionService->buildJointPermissionsForEntity($entity);
102 * Quick way to create a new user without any permissions
103 * @param array $attributes
106 protected function getNewBlankUser($attributes = [])
108 $user = factory(User::class)->create($attributes);
113 * Assert that a given string is seen inside an element.
115 * @param bool|string|null $element
116 * @param integer $position
117 * @param string $text
118 * @param bool $negate
121 protected function seeInNthElement($element, $position, $text, $negate = false)
123 $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
125 $rawPattern = preg_quote($text, '/');
127 $escapedPattern = preg_quote(e($text), '/');
129 $content = $this->crawler->filter($element)->eq($position)->html();
131 $pattern = $rawPattern == $escapedPattern
132 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
134 $this->$method("/$pattern/i", $content);
140 * Assert that the current page matches a given URI.
145 protected function seePageUrlIs($uri)
148 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
155 * Do a forced visit that does not error out on exception.
157 * @param array $parameters
158 * @param array $cookies
159 * @param array $files
162 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
165 $uri = $this->prepareUrlForRequest($uri);
166 $this->call($method, $uri, $parameters, $cookies, $files);
167 $this->clearInputs()->followRedirects();
168 $this->currentUri = $this->app->make('request')->fullUrl();
169 $this->crawler = new Crawler($this->response->getContent(), $uri);
174 * Click the text within the selected element.
175 * @param $parentElement
179 protected function clickInElement($parentElement, $linkText)
181 $elem = $this->crawler->filter($parentElement);
182 $link = $elem->selectLink($linkText);
183 $this->visit($link->link()->getUri());
188 * Check if the page contains the given element.
189 * @param string $selector
191 protected function pageHasElement($selector)
193 $elements = $this->crawler->filter($selector);
194 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
199 * Check if the page contains the given element.
200 * @param string $selector
202 protected function pageNotHasElement($selector)
204 $elements = $this->crawler->filter($selector);
205 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);