5 use BookStack\Services\PermissionService;
7 use Illuminate\Contracts\Console\Kernel;
8 use Illuminate\Foundation\Testing\DatabaseTransactions;
9 use Laravel\BrowserKitTesting\TestCase;
10 use Symfony\Component\DomCrawler\Crawler;
12 abstract class BrowserKitTest extends TestCase
15 use DatabaseTransactions;
18 * The base URL to use while testing the application.
22 protected $baseUrl = 'http://localhost';
24 // Local user instances
28 public function tearDown()
35 * Creates the application.
37 * @return \Illuminate\Foundation\Application
39 public function createApplication()
41 $app = require __DIR__.'/../bootstrap/app.php';
43 $app->make(Kernel::class)->bootstrap();
49 * Set the current user context to be an admin.
52 public function asAdmin()
54 return $this->actingAs($this->getAdmin());
58 * Get the current admin user.
61 public function getAdmin() {
62 if($this->admin === null) {
63 $adminRole = Role::getSystemRole('admin');
64 $this->admin = $adminRole->users->first();
70 * Set the current editor context to be an editor.
73 public function asEditor()
75 if ($this->editor === null) {
76 $this->editor = $this->getEditor();
78 return $this->actingAs($this->editor);
82 * Get a user that's not a system user such as the guest user.
84 public function getNormalUser()
86 return \BookStack\User::where('system_name', '=', null)->get()->last();
90 * Quickly sets an array of settings.
91 * @param $settingsArray
93 protected function setSettings($settingsArray)
95 $settings = app('BookStack\Services\SettingService');
96 foreach ($settingsArray as $key => $value) {
97 $settings->put($key, $value);
102 * Create a group of entities that belong to a specific user.
103 * @param $creatorUser
104 * @param $updaterUser
107 protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
109 if ($updaterUser === false) $updaterUser = $creatorUser;
110 $book = factory(\BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
111 $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
112 $page = factory(\BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id, 'chapter_id' => $chapter->id]);
113 $restrictionService = $this->app[PermissionService::class];
114 $restrictionService->buildJointPermissionsForEntity($book);
117 'chapter' => $chapter,
123 * Helper for updating entity permissions.
124 * @param Entity $entity
126 protected function updateEntityPermissions(Entity $entity)
128 $restrictionService = $this->app[PermissionService::class];
129 $restrictionService->buildJointPermissionsForEntity($entity);
133 * Get an instance of a user with 'editor' permissions
134 * @param array $attributes
137 protected function getEditor($attributes = [])
139 $user = \BookStack\Role::getRole('editor')->users()->first();
140 if (!empty($attributes)) $user->forceFill($attributes)->save();
145 * Get an instance of a user with 'viewer' permissions
148 protected function getViewer()
150 $user = \BookStack\Role::getRole('viewer')->users()->first();
151 if (!empty($attributes)) $user->forceFill($attributes)->save();
156 * Quick way to create a new user without any permissions
157 * @param array $attributes
160 protected function getNewBlankUser($attributes = [])
162 $user = factory(\BookStack\User::class)->create($attributes);
167 * Assert that a given string is seen inside an element.
169 * @param bool|string|null $element
170 * @param integer $position
171 * @param string $text
172 * @param bool $negate
175 protected function seeInNthElement($element, $position, $text, $negate = false)
177 $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
179 $rawPattern = preg_quote($text, '/');
181 $escapedPattern = preg_quote(e($text), '/');
183 $content = $this->crawler->filter($element)->eq($position)->html();
185 $pattern = $rawPattern == $escapedPattern
186 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
188 $this->$method("/$pattern/i", $content);
194 * Assert that the current page matches a given URI.
199 protected function seePageUrlIs($uri)
202 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
209 * Do a forced visit that does not error out on exception.
211 * @param array $parameters
212 * @param array $cookies
213 * @param array $files
216 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
219 $uri = $this->prepareUrlForRequest($uri);
220 $this->call($method, $uri, $parameters, $cookies, $files);
221 $this->clearInputs()->followRedirects();
222 $this->currentUri = $this->app->make('request')->fullUrl();
223 $this->crawler = new Crawler($this->response->getContent(), $uri);
228 * Click the text within the selected element.
229 * @param $parentElement
233 protected function clickInElement($parentElement, $linkText)
235 $elem = $this->crawler->filter($parentElement);
236 $link = $elem->selectLink($linkText);
237 $this->visit($link->link()->getUri());
242 * Check if the page contains the given element.
243 * @param string $selector
245 protected function pageHasElement($selector)
247 $elements = $this->crawler->filter($selector);
248 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
253 * Check if the page contains the given element.
254 * @param string $selector
256 protected function pageNotHasElement($selector)
258 $elements = $this->crawler->filter($selector);
259 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);