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
20 public function tearDown()
27 * Creates the application.
29 * @return \Illuminate\Foundation\Application
31 public function createApplication()
33 $app = require __DIR__.'/../bootstrap/app.php';
35 $app->make(Kernel::class)->bootstrap();
41 * Set the current user context to be an admin.
44 public function asAdmin()
46 return $this->actingAs($this->getAdmin());
50 * Get the current admin user.
53 public function getAdmin() {
54 if($this->admin === null) {
55 $adminRole = Role::getSystemRole('admin');
56 $this->admin = $adminRole->users->first();
62 * Set the current editor context to be an editor.
65 public function asEditor()
67 if ($this->editor === null) {
68 $this->editor = $this->getEditor();
70 return $this->actingAs($this->editor);
74 * Get a user that's not a system user such as the guest user.
76 public function getNormalUser()
78 return \BookStack\User::where('system_name', '=', null)->get()->last();
82 * Quickly sets an array of settings.
83 * @param $settingsArray
85 protected function setSettings($settingsArray)
87 $settings = app('BookStack\Services\SettingService');
88 foreach ($settingsArray as $key => $value) {
89 $settings->put($key, $value);
94 * Create a group of entities that belong to a specific user.
99 protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
101 if ($updaterUser === false) $updaterUser = $creatorUser;
102 $book = factory(\BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
103 $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
104 $page = factory(\BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id, 'chapter_id' => $chapter->id]);
105 $restrictionService = $this->app[PermissionService::class];
106 $restrictionService->buildJointPermissionsForEntity($book);
109 'chapter' => $chapter,
115 * Helper for updating entity permissions.
116 * @param Entity $entity
118 protected function updateEntityPermissions(Entity $entity)
120 $restrictionService = $this->app[PermissionService::class];
121 $restrictionService->buildJointPermissionsForEntity($entity);
125 * Get an instance of a user with 'editor' permissions
126 * @param array $attributes
129 protected function getEditor($attributes = [])
131 $user = \BookStack\Role::getRole('editor')->users()->first();
132 if (!empty($attributes)) $user->forceFill($attributes)->save();
137 * Get an instance of a user with 'viewer' permissions
140 protected function getViewer()
142 $user = \BookStack\Role::getRole('viewer')->users()->first();
143 if (!empty($attributes)) $user->forceFill($attributes)->save();
148 * Quick way to create a new user without any permissions
149 * @param array $attributes
152 protected function getNewBlankUser($attributes = [])
154 $user = factory(\BookStack\User::class)->create($attributes);
159 * Assert that a given string is seen inside an element.
161 * @param bool|string|null $element
162 * @param integer $position
163 * @param string $text
164 * @param bool $negate
167 protected function seeInNthElement($element, $position, $text, $negate = false)
169 $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
171 $rawPattern = preg_quote($text, '/');
173 $escapedPattern = preg_quote(e($text), '/');
175 $content = $this->crawler->filter($element)->eq($position)->html();
177 $pattern = $rawPattern == $escapedPattern
178 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
180 $this->$method("/$pattern/i", $content);
186 * Assert that the current page matches a given URI.
191 protected function seePageUrlIs($uri)
194 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
201 * Do a forced visit that does not error out on exception.
203 * @param array $parameters
204 * @param array $cookies
205 * @param array $files
208 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
211 $uri = $this->prepareUrlForRequest($uri);
212 $this->call($method, $uri, $parameters, $cookies, $files);
213 $this->clearInputs()->followRedirects();
214 $this->currentUri = $this->app->make('request')->fullUrl();
215 $this->crawler = new Crawler($this->response->getContent(), $uri);
220 * Click the text within the selected element.
221 * @param $parentElement
225 protected function clickInElement($parentElement, $linkText)
227 $elem = $this->crawler->filter($parentElement);
228 $link = $elem->selectLink($linkText);
229 $this->visit($link->link()->getUri());
234 * Check if the page contains the given element.
235 * @param string $selector
237 protected function pageHasElement($selector)
239 $elements = $this->crawler->filter($selector);
240 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
245 * Check if the page contains the given element.
246 * @param string $selector
248 protected function pageNotHasElement($selector)
250 $elements = $this->crawler->filter($selector);
251 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);