4 use BookStack\Services\PermissionService;
5 use Illuminate\Contracts\Console\Kernel;
6 use Illuminate\Foundation\Testing\DatabaseTransactions;
7 use Laravel\BrowserKitTesting\TestCase;
8 use Symfony\Component\DomCrawler\Crawler;
10 abstract class BrowserKitTest extends TestCase
13 use DatabaseTransactions;
16 * The base URL to use while testing the application.
20 protected $baseUrl = 'http://localhost';
22 // Local user instances
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 * Quick way to create a new user
122 * @param array $attributes
125 protected function getEditor($attributes = [])
127 $user = factory(\BookStack\User::class)->create($attributes);
128 $role = Role::getRole('editor');
129 $user->attachRole($role);;
134 * Quick way to create a new user without any permissions
135 * @param array $attributes
138 protected function getNewBlankUser($attributes = [])
140 $user = factory(\BookStack\User::class)->create($attributes);
145 * Assert that a given string is seen inside an element.
147 * @param bool|string|null $element
148 * @param integer $position
149 * @param string $text
150 * @param bool $negate
153 protected function seeInNthElement($element, $position, $text, $negate = false)
155 $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
157 $rawPattern = preg_quote($text, '/');
159 $escapedPattern = preg_quote(e($text), '/');
161 $content = $this->crawler->filter($element)->eq($position)->html();
163 $pattern = $rawPattern == $escapedPattern
164 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
166 $this->$method("/$pattern/i", $content);
172 * Assert that the current page matches a given URI.
177 protected function seePageUrlIs($uri)
180 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
187 * Do a forced visit that does not error out on exception.
189 * @param array $parameters
190 * @param array $cookies
191 * @param array $files
194 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
197 $uri = $this->prepareUrlForRequest($uri);
198 $this->call($method, $uri, $parameters, $cookies, $files);
199 $this->clearInputs()->followRedirects();
200 $this->currentUri = $this->app->make('request')->fullUrl();
201 $this->crawler = new Crawler($this->response->getContent(), $uri);
206 * Click the text within the selected element.
207 * @param $parentElement
211 protected function clickInElement($parentElement, $linkText)
213 $elem = $this->crawler->filter($parentElement);
214 $link = $elem->selectLink($linkText);
215 $this->visit($link->link()->getUri());
220 * Check if the page contains the given element.
221 * @param string $selector
223 protected function pageHasElement($selector)
225 $elements = $this->crawler->filter($selector);
226 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
231 * Check if the page contains the given element.
232 * @param string $selector
234 protected function pageNotHasElement($selector)
236 $elements = $this->crawler->filter($selector);
237 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);