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 * Quickly sets an array of settings.
71 * @param $settingsArray
73 protected function setSettings($settingsArray)
75 $settings = app('BookStack\Services\SettingService');
76 foreach ($settingsArray as $key => $value) {
77 $settings->put($key, $value);
82 * Create a group of entities that belong to a specific user.
87 protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
89 if ($updaterUser === false) $updaterUser = $creatorUser;
90 $book = factory(BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
91 $chapter = factory(BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
92 $page = factory(BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
93 $book->chapters()->saveMany([$chapter]);
94 $chapter->pages()->saveMany([$page]);
95 $restrictionService = $this->app[\BookStack\Services\PermissionService::class];
96 $restrictionService->buildJointPermissionsForEntity($book);
99 'chapter' => $chapter,
105 * Quick way to create a new user
106 * @param array $attributes
109 protected function getEditor($attributes = [])
111 $user = factory(\BookStack\User::class)->create($attributes);
112 $role = \BookStack\Role::getRole('editor');
113 $user->attachRole($role);;
118 * Quick way to create a new user without any permissions
119 * @param array $attributes
122 protected function getNewBlankUser($attributes = [])
124 $user = factory(\BookStack\User::class)->create($attributes);
129 * Assert that a given string is seen inside an element.
131 * @param bool|string|null $element
132 * @param integer $position
133 * @param string $text
134 * @param bool $negate
137 protected function seeInNthElement($element, $position, $text, $negate = false)
139 $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
141 $rawPattern = preg_quote($text, '/');
143 $escapedPattern = preg_quote(e($text), '/');
145 $content = $this->crawler->filter($element)->eq($position)->html();
147 $pattern = $rawPattern == $escapedPattern
148 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
150 $this->$method("/$pattern/i", $content);
156 * Assert that the current page matches a given URI.
161 protected function seePageUrlIs($uri)
164 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
171 * Do a forced visit that does not error out on exception.
173 * @param array $parameters
174 * @param array $cookies
175 * @param array $files
178 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
181 $uri = $this->prepareUrlForRequest($uri);
182 $this->call($method, $uri, $parameters, $cookies, $files);
183 $this->clearInputs()->followRedirects();
184 $this->currentUri = $this->app->make('request')->fullUrl();
185 $this->crawler = new Crawler($this->response->getContent(), $uri);
190 * Click the text within the selected element.
191 * @param $parentElement
195 protected function clickInElement($parentElement, $linkText)
197 $elem = $this->crawler->filter($parentElement);
198 $link = $elem->selectLink($linkText);
199 $this->visit($link->link()->getUri());
204 * Check if the page contains the given element.
205 * @param string $selector
208 protected function pageHasElement($selector)
210 $elements = $this->crawler->filter($selector);
211 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
216 * Check if the page contains the given element.
217 * @param string $selector
220 protected function pageNotHasElement($selector)
222 $elements = $this->crawler->filter($selector);
223 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);