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;
16 // Local user instances
21 * The base URL to use while testing the application.
24 protected $baseUrl = 'http://localhost';
26 public function tearDown()
33 * Creates the application.
35 * @return \Illuminate\Foundation\Application
37 public function createApplication()
39 $app = require __DIR__.'/../bootstrap/app.php';
41 $app->make(Kernel::class)->bootstrap();
47 * Set the current user context to be an admin.
50 public function asAdmin()
52 return $this->actingAs($this->getAdmin());
56 * Get the current admin user.
59 public function getAdmin() {
60 if($this->admin === null) {
61 $adminRole = Role::getSystemRole('admin');
62 $this->admin = $adminRole->users->first();
68 * Set the current editor context to be an editor.
71 public function asEditor()
73 if ($this->editor === null) {
74 $this->editor = $this->getEditor();
76 return $this->actingAs($this->editor);
80 * Get a user that's not a system user such as the guest user.
82 public function getNormalUser()
84 return \BookStack\User::where('system_name', '=', null)->get()->last();
88 * Quickly sets an array of settings.
89 * @param $settingsArray
91 protected function setSettings($settingsArray)
93 $settings = app('BookStack\Services\SettingService');
94 foreach ($settingsArray as $key => $value) {
95 $settings->put($key, $value);
100 * Create a group of entities that belong to a specific user.
101 * @param $creatorUser
102 * @param $updaterUser
105 protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
107 if ($updaterUser === false) $updaterUser = $creatorUser;
108 $book = factory(\BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
109 $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
110 $page = factory(\BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id, 'chapter_id' => $chapter->id]);
111 $restrictionService = $this->app[PermissionService::class];
112 $restrictionService->buildJointPermissionsForEntity($book);
115 'chapter' => $chapter,
121 * Helper for updating entity permissions.
122 * @param Entity $entity
124 protected function updateEntityPermissions(Entity $entity)
126 $restrictionService = $this->app[PermissionService::class];
127 $restrictionService->buildJointPermissionsForEntity($entity);
131 * Get an instance of a user with 'editor' permissions
132 * @param array $attributes
135 protected function getEditor($attributes = [])
137 $user = \BookStack\Role::getRole('editor')->users()->first();
138 if (!empty($attributes)) $user->forceFill($attributes)->save();
143 * Get an instance of a user with 'viewer' permissions
146 protected function getViewer()
148 $user = \BookStack\Role::getRole('viewer')->users()->first();
149 if (!empty($attributes)) $user->forceFill($attributes)->save();
154 * Quick way to create a new user without any permissions
155 * @param array $attributes
158 protected function getNewBlankUser($attributes = [])
160 $user = factory(\BookStack\User::class)->create($attributes);
165 * Assert that a given string is seen inside an element.
167 * @param bool|string|null $element
168 * @param integer $position
169 * @param string $text
170 * @param bool $negate
173 protected function seeInNthElement($element, $position, $text, $negate = false)
175 $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
177 $rawPattern = preg_quote($text, '/');
179 $escapedPattern = preg_quote(e($text), '/');
181 $content = $this->crawler->filter($element)->eq($position)->html();
183 $pattern = $rawPattern == $escapedPattern
184 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
186 $this->$method("/$pattern/i", $content);
192 * Assert that the current page matches a given URI.
197 protected function seePageUrlIs($uri)
200 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
207 * Do a forced visit that does not error out on exception.
209 * @param array $parameters
210 * @param array $cookies
211 * @param array $files
214 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
217 $uri = $this->prepareUrlForRequest($uri);
218 $this->call($method, $uri, $parameters, $cookies, $files);
219 $this->clearInputs()->followRedirects();
220 $this->currentUri = $this->app->make('request')->fullUrl();
221 $this->crawler = new Crawler($this->response->getContent(), $uri);
226 * Click the text within the selected element.
227 * @param $parentElement
231 protected function clickInElement($parentElement, $linkText)
233 $elem = $this->crawler->filter($parentElement);
234 $link = $elem->selectLink($linkText);
235 $this->visit($link->link()->getUri());
240 * Check if the page contains the given element.
241 * @param string $selector
243 protected function pageHasElement($selector)
245 $elements = $this->crawler->filter($selector);
246 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
251 * Check if the page contains the given element.
252 * @param string $selector
254 protected function pageNotHasElement($selector)
256 $elements = $this->crawler->filter($selector);
257 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);