]> BookStack Code Mirror - bookstack/blob - tests/TestCase.php
Added a whole load of permission & role tests
[bookstack] / tests / TestCase.php
1 <?php
2
3 use Illuminate\Foundation\Testing\DatabaseTransactions;
4
5 class TestCase extends Illuminate\Foundation\Testing\TestCase
6 {
7
8     use DatabaseTransactions;
9
10     /**
11      * The base URL to use while testing the application.
12      *
13      * @var string
14      */
15     protected $baseUrl = 'http://localhost';
16     private $admin;
17
18     /**
19      * Creates the application.
20      *
21      * @return \Illuminate\Foundation\Application
22      */
23     public function createApplication()
24     {
25         $app = require __DIR__.'/../bootstrap/app.php';
26
27         $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
28
29         return $app;
30     }
31
32     public function asAdmin()
33     {
34         if($this->admin === null) {
35             $adminRole = \BookStack\Role::getRole('admin');
36             $this->admin = $adminRole->users->first();
37         }
38         return $this->actingAs($this->admin);
39     }
40
41     /**
42      * Quickly sets an array of settings.
43      * @param $settingsArray
44      */
45     protected function setSettings($settingsArray)
46     {
47         $settings = app('BookStack\Services\SettingService');
48         foreach ($settingsArray as $key => $value) {
49             $settings->put($key, $value);
50         }
51     }
52
53     /**
54      * Create a group of entities that belong to a specific user.
55      * @param $creatorUser
56      * @param $updaterUser
57      * @return array
58      */
59     protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
60     {
61         if ($updaterUser === false) $updaterUser = $creatorUser;
62         $book = factory(BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
63         $chapter = factory(BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
64         $page = factory(BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
65         $book->chapters()->saveMany([$chapter]);
66         $chapter->pages()->saveMany([$page]);
67         return [
68             'book' => $book,
69             'chapter' => $chapter,
70             'page' => $page
71         ];
72     }
73
74     /**
75      * Quick way to create a new user
76      * @param array $attributes
77      * @return mixed
78      */
79     protected function getNewUser($attributes = [])
80     {
81         $user = factory(\BookStack\User::class)->create($attributes);
82         $role = \BookStack\Role::getRole('editor');
83         $user->attachRole($role);;
84         return $user;
85     }
86
87     /**
88      * Quick way to create a new user without any permissions
89      * @param array $attributes
90      * @return mixed
91      */
92     protected function getNewBlankUser($attributes = [])
93     {
94         $user = factory(\BookStack\User::class)->create($attributes);
95         return $user;
96     }
97
98     /**
99      * Assert that a given string is seen inside an element.
100      *
101      * @param  bool|string|null $element
102      * @param  integer          $position
103      * @param  string           $text
104      * @param  bool             $negate
105      * @return $this
106      */
107     protected function seeInNthElement($element, $position, $text, $negate = false)
108     {
109         $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
110
111         $rawPattern = preg_quote($text, '/');
112
113         $escapedPattern = preg_quote(e($text), '/');
114
115         $content = $this->crawler->filter($element)->eq($position)->html();
116
117         $pattern = $rawPattern == $escapedPattern
118             ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
119
120         $this->$method("/$pattern/i", $content);
121
122         return $this;
123     }
124
125     /**
126      * Click the text within the selected element.
127      * @param $parentElement
128      * @param $linkText
129      * @return $this
130      */
131     protected function clickInElement($parentElement, $linkText)
132     {
133         $elem = $this->crawler->filter($parentElement);
134         $link = $elem->selectLink($linkText);
135         $this->visit($link->link()->getUri());
136         return $this;
137     }
138 }