5 use BookStack\Services\PermissionService;
6 use Illuminate\Contracts\Console\Kernel;
7 use Illuminate\Foundation\Testing\DatabaseTransactions;
8 use Laravel\BrowserKitTesting\TestCase;
9 use Symfony\Component\DomCrawler\Crawler;
11 abstract class BrowserKitTest extends TestCase
14 use DatabaseTransactions;
15 use SharedTestHelpers;
18 * The base URL to use while testing the application.
21 protected $baseUrl = 'http://localhost';
23 public function tearDown()
30 * Creates the application.
32 * @return \Illuminate\Foundation\Application
34 public function createApplication()
36 $app = require __DIR__.'/../bootstrap/app.php';
38 $app->make(Kernel::class)->bootstrap();
45 * Get a user that's not a system user such as the guest user.
47 public function getNormalUser()
49 return \BookStack\User::where('system_name', '=', null)->get()->last();
53 * Quickly sets an array of settings.
54 * @param $settingsArray
56 protected function setSettings($settingsArray)
58 $settings = app('BookStack\Services\SettingService');
59 foreach ($settingsArray as $key => $value) {
60 $settings->put($key, $value);
65 * Create a group of entities that belong to a specific user.
70 protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
72 if ($updaterUser === false) $updaterUser = $creatorUser;
73 $book = factory(\BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
74 $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
75 $page = factory(\BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id, 'chapter_id' => $chapter->id]);
76 $restrictionService = $this->app[PermissionService::class];
77 $restrictionService->buildJointPermissionsForEntity($book);
80 'chapter' => $chapter,
86 * Helper for updating entity permissions.
87 * @param Entity $entity
89 protected function updateEntityPermissions(Entity $entity)
91 $restrictionService = $this->app[PermissionService::class];
92 $restrictionService->buildJointPermissionsForEntity($entity);
97 * Quick way to create a new user without any permissions
98 * @param array $attributes
101 protected function getNewBlankUser($attributes = [])
103 $user = factory(\BookStack\User::class)->create($attributes);
108 * Assert that a given string is seen inside an element.
110 * @param bool|string|null $element
111 * @param integer $position
112 * @param string $text
113 * @param bool $negate
116 protected function seeInNthElement($element, $position, $text, $negate = false)
118 $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
120 $rawPattern = preg_quote($text, '/');
122 $escapedPattern = preg_quote(e($text), '/');
124 $content = $this->crawler->filter($element)->eq($position)->html();
126 $pattern = $rawPattern == $escapedPattern
127 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
129 $this->$method("/$pattern/i", $content);
135 * Assert that the current page matches a given URI.
140 protected function seePageUrlIs($uri)
143 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
150 * Do a forced visit that does not error out on exception.
152 * @param array $parameters
153 * @param array $cookies
154 * @param array $files
157 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
160 $uri = $this->prepareUrlForRequest($uri);
161 $this->call($method, $uri, $parameters, $cookies, $files);
162 $this->clearInputs()->followRedirects();
163 $this->currentUri = $this->app->make('request')->fullUrl();
164 $this->crawler = new Crawler($this->response->getContent(), $uri);
169 * Click the text within the selected element.
170 * @param $parentElement
174 protected function clickInElement($parentElement, $linkText)
176 $elem = $this->crawler->filter($parentElement);
177 $link = $elem->selectLink($linkText);
178 $this->visit($link->link()->getUri());
183 * Check if the page contains the given element.
184 * @param string $selector
186 protected function pageHasElement($selector)
188 $elements = $this->crawler->filter($selector);
189 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
194 * Check if the page contains the given element.
195 * @param string $selector
197 protected function pageNotHasElement($selector)
199 $elements = $this->crawler->filter($selector);
200 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);