3 use Illuminate\Foundation\Testing\DatabaseTransactions;
4 use Symfony\Component\DomCrawler\Crawler;
6 class TestCase extends Illuminate\Foundation\Testing\TestCase
9 use DatabaseTransactions;
12 * The base URL to use while testing the application.
16 protected $baseUrl = 'http://localhost';
18 // Local user instances
23 * Creates the application.
25 * @return \Illuminate\Foundation\Application
27 public function createApplication()
29 $app = require __DIR__.'/../bootstrap/app.php';
31 $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
37 * Set the current user context to be an admin.
40 public function asAdmin()
42 return $this->actingAs($this->getAdmin());
46 * Get the current admin user.
49 public function getAdmin() {
50 if($this->admin === null) {
51 $adminRole = \BookStack\Role::getRole('admin');
52 $this->admin = $adminRole->users->first();
58 * Set the current editor context to be an editor.
61 public function asEditor()
63 if($this->editor === null) {
64 $this->editor = $this->getEditor();
66 return $this->actingAs($this->editor);
70 * Get a user that's not a system user such as the guest user.
72 public function getNormalUser()
74 return \BookStack\User::where('system_name', '=', null)->get()->last();
78 * Quickly sets an array of settings.
79 * @param $settingsArray
81 protected function setSettings($settingsArray)
83 $settings = app('BookStack\Services\SettingService');
84 foreach ($settingsArray as $key => $value) {
85 $settings->put($key, $value);
90 * Create a group of entities that belong to a specific user.
95 protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
97 if ($updaterUser === false) $updaterUser = $creatorUser;
98 $book = factory(BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
99 $chapter = factory(BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
100 $page = factory(BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
101 $book->chapters()->saveMany([$chapter]);
102 $chapter->pages()->saveMany([$page]);
103 $restrictionService = $this->app[\BookStack\Services\PermissionService::class];
104 $restrictionService->buildJointPermissionsForEntity($book);
107 'chapter' => $chapter,
113 * Quick way to create a new user
114 * @param array $attributes
117 protected function getEditor($attributes = [])
119 $user = factory(\BookStack\User::class)->create($attributes);
120 $role = \BookStack\Role::getRole('editor');
121 $user->attachRole($role);;
126 * Quick way to create a new user without any permissions
127 * @param array $attributes
130 protected function getNewBlankUser($attributes = [])
132 $user = factory(\BookStack\User::class)->create($attributes);
137 * Assert that a given string is seen inside an element.
139 * @param bool|string|null $element
140 * @param integer $position
141 * @param string $text
142 * @param bool $negate
145 protected function seeInNthElement($element, $position, $text, $negate = false)
147 $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
149 $rawPattern = preg_quote($text, '/');
151 $escapedPattern = preg_quote(e($text), '/');
153 $content = $this->crawler->filter($element)->eq($position)->html();
155 $pattern = $rawPattern == $escapedPattern
156 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
158 $this->$method("/$pattern/i", $content);
164 * Assert that the current page matches a given URI.
169 protected function seePageUrlIs($uri)
172 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
179 * Do a forced visit that does not error out on exception.
181 * @param array $parameters
182 * @param array $cookies
183 * @param array $files
186 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
189 $uri = $this->prepareUrlForRequest($uri);
190 $this->call($method, $uri, $parameters, $cookies, $files);
191 $this->clearInputs()->followRedirects();
192 $this->currentUri = $this->app->make('request')->fullUrl();
193 $this->crawler = new Crawler($this->response->getContent(), $uri);
198 * Click the text within the selected element.
199 * @param $parentElement
203 protected function clickInElement($parentElement, $linkText)
205 $elem = $this->crawler->filter($parentElement);
206 $link = $elem->selectLink($linkText);
207 $this->visit($link->link()->getUri());
212 * Check if the page contains the given element.
213 * @param string $selector
216 protected function pageHasElement($selector)
218 $elements = $this->crawler->filter($selector);
219 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
224 * Check if the page contains the given element.
225 * @param string $selector
228 protected function pageNotHasElement($selector)
230 $elements = $this->crawler->filter($selector);
231 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);