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 * Quick way to create a new user
133 * @param array $attributes
136 protected function getEditor($attributes = [])
138 $user = factory(\BookStack\User::class)->create($attributes);
139 $role = Role::getRole('editor');
140 $user->attachRole($role);;
145 * Quick way to create a new user without any permissions
146 * @param array $attributes
149 protected function getNewBlankUser($attributes = [])
151 $user = factory(\BookStack\User::class)->create($attributes);
156 * Assert that a given string is seen inside an element.
158 * @param bool|string|null $element
159 * @param integer $position
160 * @param string $text
161 * @param bool $negate
164 protected function seeInNthElement($element, $position, $text, $negate = false)
166 $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
168 $rawPattern = preg_quote($text, '/');
170 $escapedPattern = preg_quote(e($text), '/');
172 $content = $this->crawler->filter($element)->eq($position)->html();
174 $pattern = $rawPattern == $escapedPattern
175 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
177 $this->$method("/$pattern/i", $content);
183 * Assert that the current page matches a given URI.
188 protected function seePageUrlIs($uri)
191 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
198 * Do a forced visit that does not error out on exception.
200 * @param array $parameters
201 * @param array $cookies
202 * @param array $files
205 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
208 $uri = $this->prepareUrlForRequest($uri);
209 $this->call($method, $uri, $parameters, $cookies, $files);
210 $this->clearInputs()->followRedirects();
211 $this->currentUri = $this->app->make('request')->fullUrl();
212 $this->crawler = new Crawler($this->response->getContent(), $uri);
217 * Click the text within the selected element.
218 * @param $parentElement
222 protected function clickInElement($parentElement, $linkText)
224 $elem = $this->crawler->filter($parentElement);
225 $link = $elem->selectLink($linkText);
226 $this->visit($link->link()->getUri());
231 * Check if the page contains the given element.
232 * @param string $selector
234 protected function pageHasElement($selector)
236 $elements = $this->crawler->filter($selector);
237 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
242 * Check if the page contains the given element.
243 * @param string $selector
245 protected function pageNotHasElement($selector)
247 $elements = $this->crawler->filter($selector);
248 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);