5 use BookStack\Services\PermissionService;
6 use Illuminate\Contracts\Console\Kernel;
7 use Illuminate\Foundation\Testing\DatabaseTransactions;
8 use Laravel\BrowserKitTesting\TestCase;
9 use Symfony\Component\DomCrawler\Crawler;
11 abstract class BrowserKitTest extends TestCase
14 use DatabaseTransactions;
17 * The base URL to use while testing the application.
21 protected $baseUrl = 'http://localhost';
23 // Local user instances
27 public function tearDown()
34 * Creates the application.
36 * @return \Illuminate\Foundation\Application
38 public function createApplication()
40 $app = require __DIR__.'/../bootstrap/app.php';
42 $app->make(Kernel::class)->bootstrap();
48 * Set the current user context to be an admin.
51 public function asAdmin()
53 return $this->actingAs($this->getAdmin());
57 * Get the current admin user.
60 public function getAdmin() {
61 if($this->admin === null) {
62 $adminRole = Role::getSystemRole('admin');
63 $this->admin = $adminRole->users->first();
69 * Set the current editor context to be an editor.
72 public function asEditor()
74 if ($this->editor === null) {
75 $this->editor = $this->getEditor();
77 return $this->actingAs($this->editor);
81 * Get a user that's not a system user such as the guest user.
83 public function getNormalUser()
85 return \BookStack\User::where('system_name', '=', null)->get()->last();
89 * Quickly sets an array of settings.
90 * @param $settingsArray
92 protected function setSettings($settingsArray)
94 $settings = app('BookStack\Services\SettingService');
95 foreach ($settingsArray as $key => $value) {
96 $settings->put($key, $value);
101 * Create a group of entities that belong to a specific user.
102 * @param $creatorUser
103 * @param $updaterUser
106 protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
108 if ($updaterUser === false) $updaterUser = $creatorUser;
109 $book = factory(\BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
110 $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
111 $page = factory(\BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id, 'chapter_id' => $chapter->id]);
112 $restrictionService = $this->app[PermissionService::class];
113 $restrictionService->buildJointPermissionsForEntity($book);
116 'chapter' => $chapter,
122 * Helper for updating entity permissions.
123 * @param Entity $entity
125 protected function updateEntityPermissions(Entity $entity)
127 $restrictionService = $this->app[PermissionService::class];
128 $restrictionService->buildJointPermissionsForEntity($entity);
132 * Get an instance of a user with 'editor' permissions
133 * @param array $attributes
136 protected function getEditor($attributes = [])
138 $user = \BookStack\Role::getRole('editor')->users()->first();
139 if (!empty($attributes)) $user->forceFill($attributes)->save();
144 * Get an instance of a user with 'viewer' permissions
147 protected function getViewer()
149 $user = \BookStack\Role::getRole('viewer')->users()->first();
150 if (!empty($attributes)) $user->forceFill($attributes)->save();
155 * Quick way to create a new user without any permissions
156 * @param array $attributes
159 protected function getNewBlankUser($attributes = [])
161 $user = factory(\BookStack\User::class)->create($attributes);
166 * Assert that a given string is seen inside an element.
168 * @param bool|string|null $element
169 * @param integer $position
170 * @param string $text
171 * @param bool $negate
174 protected function seeInNthElement($element, $position, $text, $negate = false)
176 $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
178 $rawPattern = preg_quote($text, '/');
180 $escapedPattern = preg_quote(e($text), '/');
182 $content = $this->crawler->filter($element)->eq($position)->html();
184 $pattern = $rawPattern == $escapedPattern
185 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
187 $this->$method("/$pattern/i", $content);
193 * Assert that the current page matches a given URI.
198 protected function seePageUrlIs($uri)
201 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
208 * Do a forced visit that does not error out on exception.
210 * @param array $parameters
211 * @param array $cookies
212 * @param array $files
215 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
218 $uri = $this->prepareUrlForRequest($uri);
219 $this->call($method, $uri, $parameters, $cookies, $files);
220 $this->clearInputs()->followRedirects();
221 $this->currentUri = $this->app->make('request')->fullUrl();
222 $this->crawler = new Crawler($this->response->getContent(), $uri);
227 * Click the text within the selected element.
228 * @param $parentElement
232 protected function clickInElement($parentElement, $linkText)
234 $elem = $this->crawler->filter($parentElement);
235 $link = $elem->selectLink($linkText);
236 $this->visit($link->link()->getUri());
241 * Check if the page contains the given element.
242 * @param string $selector
244 protected function pageHasElement($selector)
246 $elements = $this->crawler->filter($selector);
247 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
252 * Check if the page contains the given element.
253 * @param string $selector
255 protected function pageNotHasElement($selector)
257 $elements = $this->crawler->filter($selector);
258 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);