4 use Illuminate\Contracts\Console\Kernel;
5 use Illuminate\Foundation\Testing\DatabaseTransactions;
6 use Laravel\BrowserKitTesting\TestCase;
7 use Symfony\Component\DomCrawler\Crawler;
9 abstract class BrowserKitTest extends TestCase
12 use DatabaseTransactions;
15 * The base URL to use while testing the application.
19 protected $baseUrl = 'http://localhost';
21 // Local user instances
26 * Creates the application.
28 * @return \Illuminate\Foundation\Application
30 public function createApplication()
32 $app = require __DIR__.'/../bootstrap/app.php';
34 $app->make(Kernel::class)->bootstrap();
40 * Set the current user context to be an admin.
43 public function asAdmin()
45 return $this->actingAs($this->getAdmin());
49 * Get the current admin user.
52 public function getAdmin() {
53 if($this->admin === null) {
54 $adminRole = Role::getSystemRole('admin');
55 $this->admin = $adminRole->users->first();
61 * Set the current editor context to be an editor.
64 public function asEditor()
66 if ($this->editor === null) {
67 $this->editor = $this->getEditor();
69 return $this->actingAs($this->editor);
73 * Get a user that's not a system user such as the guest user.
75 public function getNormalUser()
77 return \BookStack\User::where('system_name', '=', null)->get()->last();
81 * Quickly sets an array of settings.
82 * @param $settingsArray
84 protected function setSettings($settingsArray)
86 $settings = app('BookStack\Services\SettingService');
87 foreach ($settingsArray as $key => $value) {
88 $settings->put($key, $value);
93 * Create a group of entities that belong to a specific user.
98 protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
100 if ($updaterUser === false) $updaterUser = $creatorUser;
101 $book = factory(\BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
102 $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
103 $page = factory(\BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
104 $book->chapters()->saveMany([$chapter]);
105 $chapter->pages()->saveMany([$page]);
106 $restrictionService = $this->app[\BookStack\Services\PermissionService::class];
107 $restrictionService->buildJointPermissionsForEntity($book);
110 'chapter' => $chapter,
116 * Quick way to create a new user
117 * @param array $attributes
120 protected function getEditor($attributes = [])
122 $user = factory(\BookStack\User::class)->create($attributes);
123 $role = Role::getRole('editor');
124 $user->attachRole($role);;
129 * Quick way to create a new user without any permissions
130 * @param array $attributes
133 protected function getNewBlankUser($attributes = [])
135 $user = factory(\BookStack\User::class)->create($attributes);
140 * Assert that a given string is seen inside an element.
142 * @param bool|string|null $element
143 * @param integer $position
144 * @param string $text
145 * @param bool $negate
148 protected function seeInNthElement($element, $position, $text, $negate = false)
150 $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
152 $rawPattern = preg_quote($text, '/');
154 $escapedPattern = preg_quote(e($text), '/');
156 $content = $this->crawler->filter($element)->eq($position)->html();
158 $pattern = $rawPattern == $escapedPattern
159 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
161 $this->$method("/$pattern/i", $content);
167 * Assert that the current page matches a given URI.
172 protected function seePageUrlIs($uri)
175 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
182 * Do a forced visit that does not error out on exception.
184 * @param array $parameters
185 * @param array $cookies
186 * @param array $files
189 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
192 $uri = $this->prepareUrlForRequest($uri);
193 $this->call($method, $uri, $parameters, $cookies, $files);
194 $this->clearInputs()->followRedirects();
195 $this->currentUri = $this->app->make('request')->fullUrl();
196 $this->crawler = new Crawler($this->response->getContent(), $uri);
201 * Click the text within the selected element.
202 * @param $parentElement
206 protected function clickInElement($parentElement, $linkText)
208 $elem = $this->crawler->filter($parentElement);
209 $link = $elem->selectLink($linkText);
210 $this->visit($link->link()->getUri());
215 * Check if the page contains the given element.
216 * @param string $selector
218 protected function pageHasElement($selector)
220 $elements = $this->crawler->filter($selector);
221 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
226 * Check if the page contains the given element.
227 * @param string $selector
229 protected function pageNotHasElement($selector)
231 $elements = $this->crawler->filter($selector);
232 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);