]> BookStack Code Mirror - bookstack/blob - tests/TestCase.php
Major permission naming refactor and database migration cleanup
[bookstack] / tests / TestCase.php
1 <?php
2
3 use Illuminate\Foundation\Testing\DatabaseTransactions;
4 use Symfony\Component\DomCrawler\Crawler;
5
6 class TestCase extends Illuminate\Foundation\Testing\TestCase
7 {
8
9     use DatabaseTransactions;
10
11     /**
12      * The base URL to use while testing the application.
13      *
14      * @var string
15      */
16     protected $baseUrl = 'http://localhost';
17     private $admin;
18
19     /**
20      * Creates the application.
21      *
22      * @return \Illuminate\Foundation\Application
23      */
24     public function createApplication()
25     {
26         $app = require __DIR__.'/../bootstrap/app.php';
27
28         $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
29
30         return $app;
31     }
32
33     public function asAdmin()
34     {
35         if($this->admin === null) {
36             $adminRole = \BookStack\Role::getRole('admin');
37             $this->admin = $adminRole->users->first();
38         }
39         return $this->actingAs($this->admin);
40     }
41
42     /**
43      * Quickly sets an array of settings.
44      * @param $settingsArray
45      */
46     protected function setSettings($settingsArray)
47     {
48         $settings = app('BookStack\Services\SettingService');
49         foreach ($settingsArray as $key => $value) {
50             $settings->put($key, $value);
51         }
52     }
53
54     /**
55      * Create a group of entities that belong to a specific user.
56      * @param $creatorUser
57      * @param $updaterUser
58      * @return array
59      */
60     protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
61     {
62         if ($updaterUser === false) $updaterUser = $creatorUser;
63         $book = factory(BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
64         $chapter = factory(BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
65         $page = factory(BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
66         $book->chapters()->saveMany([$chapter]);
67         $chapter->pages()->saveMany([$page]);
68         $restrictionService = $this->app[\BookStack\Services\PermissionService::class];
69         $restrictionService->buildJointPermissionsForEntity($book);
70         return [
71             'book' => $book,
72             'chapter' => $chapter,
73             'page' => $page
74         ];
75     }
76
77     /**
78      * Quick way to create a new user
79      * @param array $attributes
80      * @return mixed
81      */
82     protected function getNewUser($attributes = [])
83     {
84         $user = factory(\BookStack\User::class)->create($attributes);
85         $role = \BookStack\Role::getRole('editor');
86         $user->attachRole($role);;
87         return $user;
88     }
89
90     /**
91      * Quick way to create a new user without any permissions
92      * @param array $attributes
93      * @return mixed
94      */
95     protected function getNewBlankUser($attributes = [])
96     {
97         $user = factory(\BookStack\User::class)->create($attributes);
98         return $user;
99     }
100
101     /**
102      * Assert that a given string is seen inside an element.
103      *
104      * @param  bool|string|null $element
105      * @param  integer          $position
106      * @param  string           $text
107      * @param  bool             $negate
108      * @return $this
109      */
110     protected function seeInNthElement($element, $position, $text, $negate = false)
111     {
112         $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
113
114         $rawPattern = preg_quote($text, '/');
115
116         $escapedPattern = preg_quote(e($text), '/');
117
118         $content = $this->crawler->filter($element)->eq($position)->html();
119
120         $pattern = $rawPattern == $escapedPattern
121             ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
122
123         $this->$method("/$pattern/i", $content);
124
125         return $this;
126     }
127
128     /**
129      * Assert that the current page matches a given URI.
130      *
131      * @param  string  $uri
132      * @return $this
133      */
134     protected function seePageUrlIs($uri)
135     {
136         $this->assertEquals(
137             $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
138         );
139
140         return $this;
141     }
142
143     /**
144      * Do a forced visit that does not error out on exception.
145      * @param string $uri
146      * @param array $parameters
147      * @param array $cookies
148      * @param array $files
149      * @return $this
150      */
151     protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
152     {
153         $method = 'GET';
154         $uri = $this->prepareUrlForRequest($uri);
155         $this->call($method, $uri, $parameters, $cookies, $files);
156         $this->clearInputs()->followRedirects();
157         $this->currentUri = $this->app->make('request')->fullUrl();
158         $this->crawler = new Crawler($this->response->getContent(), $uri);
159         return $this;
160     }
161
162     /**
163      * Click the text within the selected element.
164      * @param $parentElement
165      * @param $linkText
166      * @return $this
167      */
168     protected function clickInElement($parentElement, $linkText)
169     {
170         $elem = $this->crawler->filter($parentElement);
171         $link = $elem->selectLink($linkText);
172         $this->visit($link->link()->getUri());
173         return $this;
174     }
175
176     /**
177      * Check if the page contains the given element.
178      * @param  string  $selector
179      * @return bool
180      */
181     protected function pageHasElement($selector)
182     {
183         $elements = $this->crawler->filter($selector);
184         $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
185         return $this;
186     }
187
188     /**
189      * Check if the page contains the given element.
190      * @param  string  $selector
191      * @return bool
192      */
193     protected function pageNotHasElement($selector)
194     {
195         $elements = $this->crawler->filter($selector);
196         $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);
197         return $this;
198     }
199 }