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
25 public function tearDown()
27 \DB::disconnect('mysql_testing');
32 * Creates the application.
34 * @return \Illuminate\Foundation\Application
36 public function createApplication()
38 $app = require __DIR__.'/../bootstrap/app.php';
40 $app->make(Kernel::class)->bootstrap();
46 * Set the current user context to be an admin.
49 public function asAdmin()
51 return $this->actingAs($this->getAdmin());
55 * Get the current admin user.
58 public function getAdmin() {
59 if($this->admin === null) {
60 $adminRole = Role::getSystemRole('admin');
61 $this->admin = $adminRole->users->first();
67 * Set the current editor context to be an editor.
70 public function asEditor()
72 if ($this->editor === null) {
73 $this->editor = $this->getEditor();
75 return $this->actingAs($this->editor);
79 * Get a user that's not a system user such as the guest user.
81 public function getNormalUser()
83 return \BookStack\User::where('system_name', '=', null)->get()->last();
87 * Quickly sets an array of settings.
88 * @param $settingsArray
90 protected function setSettings($settingsArray)
92 $settings = app('BookStack\Services\SettingService');
93 foreach ($settingsArray as $key => $value) {
94 $settings->put($key, $value);
99 * Create a group of entities that belong to a specific user.
100 * @param $creatorUser
101 * @param $updaterUser
104 protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
106 if ($updaterUser === false) $updaterUser = $creatorUser;
107 $book = factory(\BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
108 $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
109 $page = factory(\BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
110 $book->chapters()->saveMany([$chapter]);
111 $chapter->pages()->saveMany([$page]);
112 $restrictionService = $this->app[\BookStack\Services\PermissionService::class];
113 $restrictionService->buildJointPermissionsForEntity($book);
116 'chapter' => $chapter,
122 * Quick way to create a new user
123 * @param array $attributes
126 protected function getEditor($attributes = [])
128 $user = factory(\BookStack\User::class)->create($attributes);
129 $role = Role::getRole('editor');
130 $user->attachRole($role);;
135 * Quick way to create a new user without any permissions
136 * @param array $attributes
139 protected function getNewBlankUser($attributes = [])
141 $user = factory(\BookStack\User::class)->create($attributes);
146 * Assert that a given string is seen inside an element.
148 * @param bool|string|null $element
149 * @param integer $position
150 * @param string $text
151 * @param bool $negate
154 protected function seeInNthElement($element, $position, $text, $negate = false)
156 $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
158 $rawPattern = preg_quote($text, '/');
160 $escapedPattern = preg_quote(e($text), '/');
162 $content = $this->crawler->filter($element)->eq($position)->html();
164 $pattern = $rawPattern == $escapedPattern
165 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
167 $this->$method("/$pattern/i", $content);
173 * Assert that the current page matches a given URI.
178 protected function seePageUrlIs($uri)
181 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
188 * Do a forced visit that does not error out on exception.
190 * @param array $parameters
191 * @param array $cookies
192 * @param array $files
195 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
198 $uri = $this->prepareUrlForRequest($uri);
199 $this->call($method, $uri, $parameters, $cookies, $files);
200 $this->clearInputs()->followRedirects();
201 $this->currentUri = $this->app->make('request')->fullUrl();
202 $this->crawler = new Crawler($this->response->getContent(), $uri);
207 * Click the text within the selected element.
208 * @param $parentElement
212 protected function clickInElement($parentElement, $linkText)
214 $elem = $this->crawler->filter($parentElement);
215 $link = $elem->selectLink($linkText);
216 $this->visit($link->link()->getUri());
221 * Check if the page contains the given element.
222 * @param string $selector
224 protected function pageHasElement($selector)
226 $elements = $this->crawler->filter($selector);
227 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
232 * Check if the page contains the given element.
233 * @param string $selector
235 protected function pageNotHasElement($selector)
237 $elements = $this->crawler->filter($selector);
238 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);