Showing
24 changed files
with
276 additions
and
245 deletions
| ... | @@ -3,9 +3,9 @@ | ... | @@ -3,9 +3,9 @@ |
| 3 | namespace BookStack\Exceptions; | 3 | namespace BookStack\Exceptions; |
| 4 | 4 | ||
| 5 | use Exception; | 5 | use Exception; |
| 6 | -use Illuminate\Contracts\Validation\ValidationException; | 6 | +use Illuminate\Auth\AuthenticationException; |
| 7 | +use Illuminate\Validation\ValidationException; | ||
| 7 | use Illuminate\Database\Eloquent\ModelNotFoundException; | 8 | use Illuminate\Database\Eloquent\ModelNotFoundException; |
| 8 | -use PhpSpec\Exception\Example\ErrorException; | ||
| 9 | use Symfony\Component\HttpKernel\Exception\HttpException; | 9 | use Symfony\Component\HttpKernel\Exception\HttpException; |
| 10 | use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; | 10 | use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
| 11 | use Illuminate\Auth\Access\AuthorizationException; | 11 | use Illuminate\Auth\Access\AuthorizationException; | ... | ... |
| ... | @@ -4,7 +4,7 @@ namespace BookStack\Http\Controllers; | ... | @@ -4,7 +4,7 @@ namespace BookStack\Http\Controllers; |
| 4 | 4 | ||
| 5 | use BookStack\Ownable; | 5 | use BookStack\Ownable; |
| 6 | use Illuminate\Foundation\Bus\DispatchesJobs; | 6 | use Illuminate\Foundation\Bus\DispatchesJobs; |
| 7 | -use Illuminate\Http\Exception\HttpResponseException; | 7 | +use Illuminate\Http\Exceptions\HttpResponseException; |
| 8 | use Illuminate\Http\Request; | 8 | use Illuminate\Http\Request; |
| 9 | use Illuminate\Routing\Controller as BaseController; | 9 | use Illuminate\Routing\Controller as BaseController; |
| 10 | use Illuminate\Foundation\Validation\ValidatesRequests; | 10 | use Illuminate\Foundation\Validation\ValidatesRequests; | ... | ... |
| ... | @@ -6,17 +6,18 @@ | ... | @@ -6,17 +6,18 @@ |
| 6 | "type": "project", | 6 | "type": "project", |
| 7 | "require": { | 7 | "require": { |
| 8 | "php": ">=5.6.4", | 8 | "php": ">=5.6.4", |
| 9 | - "laravel/framework": "^5.3.4", | 9 | + "laravel/framework": "5.4.*", |
| 10 | "ext-tidy": "*", | 10 | "ext-tidy": "*", |
| 11 | "intervention/image": "^2.3", | 11 | "intervention/image": "^2.3", |
| 12 | - "laravel/socialite": "^2.0", | 12 | + "laravel/socialite": "^3.0", |
| 13 | - "barryvdh/laravel-ide-helper": "^2.1", | 13 | + "barryvdh/laravel-ide-helper": "^2.2.3", |
| 14 | - "barryvdh/laravel-debugbar": "^2.2.3", | 14 | + "barryvdh/laravel-debugbar": "^2.3.2", |
| 15 | "league/flysystem-aws-s3-v3": "^1.0", | 15 | "league/flysystem-aws-s3-v3": "^1.0", |
| 16 | "barryvdh/laravel-dompdf": "^0.7", | 16 | "barryvdh/laravel-dompdf": "^0.7", |
| 17 | "predis/predis": "^1.1", | 17 | "predis/predis": "^1.1", |
| 18 | "gathercontent/htmldiff": "^0.2.1", | 18 | "gathercontent/htmldiff": "^0.2.1", |
| 19 | - "barryvdh/laravel-snappy": "^0.3.1" | 19 | + "barryvdh/laravel-snappy": "^0.3.1", |
| 20 | + "laravel/browser-kit-testing": "^1.0" | ||
| 20 | }, | 21 | }, |
| 21 | "require-dev": { | 22 | "require-dev": { |
| 22 | "fzaninotto/faker": "~1.4", | 23 | "fzaninotto/faker": "~1.4", |
| ... | @@ -35,7 +36,8 @@ | ... | @@ -35,7 +36,8 @@ |
| 35 | }, | 36 | }, |
| 36 | "autoload-dev": { | 37 | "autoload-dev": { |
| 37 | "classmap": [ | 38 | "classmap": [ |
| 38 | - "tests/TestCase.php" | 39 | + "tests/TestCase.php", |
| 40 | + "tests/BrowserKitTest.php" | ||
| 39 | ] | 41 | ] |
| 40 | }, | 42 | }, |
| 41 | "scripts": { | 43 | "scripts": { | ... | ... |
This diff could not be displayed because it is too large.
| 1 | <?php | 1 | <?php |
| 2 | 2 | ||
| 3 | -use Illuminate\Foundation\Testing\WithoutMiddleware; | ||
| 4 | -use Illuminate\Foundation\Testing\DatabaseMigrations; | ||
| 5 | -use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
| 6 | 3 | ||
| 7 | -class ActivityTrackingTest extends TestCase | 4 | +class ActivityTrackingTest extends BrowserKitTest |
| 8 | { | 5 | { |
| 9 | 6 | ||
| 10 | public function test_recently_viewed_books() | 7 | public function test_recently_viewed_books() | ... | ... |
| ... | @@ -3,7 +3,7 @@ | ... | @@ -3,7 +3,7 @@ |
| 3 | use BookStack\Notifications\ConfirmEmail; | 3 | use BookStack\Notifications\ConfirmEmail; |
| 4 | use Illuminate\Support\Facades\Notification; | 4 | use Illuminate\Support\Facades\Notification; |
| 5 | 5 | ||
| 6 | -class AuthTest extends TestCase | 6 | +class AuthTest extends BrowserKitTest |
| 7 | { | 7 | { |
| 8 | 8 | ||
| 9 | public function test_auth_working() | 9 | public function test_auth_working() |
| ... | @@ -220,6 +220,9 @@ class AuthTest extends TestCase | ... | @@ -220,6 +220,9 @@ class AuthTest extends TestCase |
| 220 | 220 | ||
| 221 | public function test_reset_password_flow() | 221 | public function test_reset_password_flow() |
| 222 | { | 222 | { |
| 223 | + | ||
| 224 | + Notification::fake(); | ||
| 225 | + | ||
| 223 | $this->visit('/login')->click('Forgot Password?') | 226 | $this->visit('/login')->click('Forgot Password?') |
| 224 | ->seePageIs('/password/email') | 227 | ->seePageIs('/password/email') |
| 225 | ->type('admin@admin.com', 'email') | 228 | ->type('admin@admin.com', 'email') |
| ... | @@ -230,8 +233,13 @@ class AuthTest extends TestCase | ... | @@ -230,8 +233,13 @@ class AuthTest extends TestCase |
| 230 | 'email' => 'admin@admin.com' | 233 | 'email' => 'admin@admin.com' |
| 231 | ]); | 234 | ]); |
| 232 | 235 | ||
| 236 | + $user = \BookStack\User::where('email', '=', 'admin@admin.com')->first(); | ||
| 237 | + | ||
| 238 | + Notification::assertSentTo($user, \BookStack\Notifications\ResetPassword::class); | ||
| 239 | + $n = Notification::sent($user, \BookStack\Notifications\ResetPassword::class); | ||
| 240 | + | ||
| 233 | $reset = DB::table('password_resets')->where('email', '=', 'admin@admin.com')->first(); | 241 | $reset = DB::table('password_resets')->where('email', '=', 'admin@admin.com')->first(); |
| 234 | - $this->visit('/password/reset/' . $reset->token) | 242 | + $this->visit('/password/reset/' . $n->first()->token) |
| 235 | ->see('Reset Password') | 243 | ->see('Reset Password') |
| 236 | ->submitForm('Reset Password', [ | 244 | ->submitForm('Reset Password', [ |
| 237 | 'email' => 'admin@admin.com', | 245 | 'email' => 'admin@admin.com', | ... | ... |
tests/BrowserKitTest.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
| 4 | +use Symfony\Component\DomCrawler\Crawler; | ||
| 5 | + | ||
| 6 | +abstract class BrowserKitTest extends \Laravel\BrowserKitTesting\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 | + | ||
| 18 | + // Local user instances | ||
| 19 | + private $admin; | ||
| 20 | + private $editor; | ||
| 21 | + | ||
| 22 | + /** | ||
| 23 | + * Creates the application. | ||
| 24 | + * | ||
| 25 | + * @return \Illuminate\Foundation\Application | ||
| 26 | + */ | ||
| 27 | + public function createApplication() | ||
| 28 | + { | ||
| 29 | + $app = require __DIR__.'/../bootstrap/app.php'; | ||
| 30 | + | ||
| 31 | + $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); | ||
| 32 | + | ||
| 33 | + return $app; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + /** | ||
| 37 | + * Set the current user context to be an admin. | ||
| 38 | + * @return $this | ||
| 39 | + */ | ||
| 40 | + public function asAdmin() | ||
| 41 | + { | ||
| 42 | + return $this->actingAs($this->getAdmin()); | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + /** | ||
| 46 | + * Get the current admin user. | ||
| 47 | + * @return mixed | ||
| 48 | + */ | ||
| 49 | + public function getAdmin() { | ||
| 50 | + if($this->admin === null) { | ||
| 51 | + $adminRole = \BookStack\Role::getRole('admin'); | ||
| 52 | + $this->admin = $adminRole->users->first(); | ||
| 53 | + } | ||
| 54 | + return $this->admin; | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + /** | ||
| 58 | + * Set the current editor context to be an editor. | ||
| 59 | + * @return $this | ||
| 60 | + */ | ||
| 61 | + public function asEditor() | ||
| 62 | + { | ||
| 63 | + if ($this->editor === null) { | ||
| 64 | + $this->editor = $this->getEditor(); | ||
| 65 | + } | ||
| 66 | + return $this->actingAs($this->editor); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + /** | ||
| 70 | + * Get a user that's not a system user such as the guest user. | ||
| 71 | + */ | ||
| 72 | + public function getNormalUser() | ||
| 73 | + { | ||
| 74 | + return \BookStack\User::where('system_name', '=', null)->get()->last(); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + /** | ||
| 78 | + * Quickly sets an array of settings. | ||
| 79 | + * @param $settingsArray | ||
| 80 | + */ | ||
| 81 | + protected function setSettings($settingsArray) | ||
| 82 | + { | ||
| 83 | + $settings = app('BookStack\Services\SettingService'); | ||
| 84 | + foreach ($settingsArray as $key => $value) { | ||
| 85 | + $settings->put($key, $value); | ||
| 86 | + } | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + /** | ||
| 90 | + * Create a group of entities that belong to a specific user. | ||
| 91 | + * @param $creatorUser | ||
| 92 | + * @param $updaterUser | ||
| 93 | + * @return array | ||
| 94 | + */ | ||
| 95 | + protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false) | ||
| 96 | + { | ||
| 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); | ||
| 105 | + return [ | ||
| 106 | + 'book' => $book, | ||
| 107 | + 'chapter' => $chapter, | ||
| 108 | + 'page' => $page | ||
| 109 | + ]; | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + /** | ||
| 113 | + * Quick way to create a new user | ||
| 114 | + * @param array $attributes | ||
| 115 | + * @return mixed | ||
| 116 | + */ | ||
| 117 | + protected function getEditor($attributes = []) | ||
| 118 | + { | ||
| 119 | + $user = factory(\BookStack\User::class)->create($attributes); | ||
| 120 | + $role = \BookStack\Role::getRole('editor'); | ||
| 121 | + $user->attachRole($role);; | ||
| 122 | + return $user; | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + /** | ||
| 126 | + * Quick way to create a new user without any permissions | ||
| 127 | + * @param array $attributes | ||
| 128 | + * @return mixed | ||
| 129 | + */ | ||
| 130 | + protected function getNewBlankUser($attributes = []) | ||
| 131 | + { | ||
| 132 | + $user = factory(\BookStack\User::class)->create($attributes); | ||
| 133 | + return $user; | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + /** | ||
| 137 | + * Assert that a given string is seen inside an element. | ||
| 138 | + * | ||
| 139 | + * @param bool|string|null $element | ||
| 140 | + * @param integer $position | ||
| 141 | + * @param string $text | ||
| 142 | + * @param bool $negate | ||
| 143 | + * @return $this | ||
| 144 | + */ | ||
| 145 | + protected function seeInNthElement($element, $position, $text, $negate = false) | ||
| 146 | + { | ||
| 147 | + $method = $negate ? 'assertNotRegExp' : 'assertRegExp'; | ||
| 148 | + | ||
| 149 | + $rawPattern = preg_quote($text, '/'); | ||
| 150 | + | ||
| 151 | + $escapedPattern = preg_quote(e($text), '/'); | ||
| 152 | + | ||
| 153 | + $content = $this->crawler->filter($element)->eq($position)->html(); | ||
| 154 | + | ||
| 155 | + $pattern = $rawPattern == $escapedPattern | ||
| 156 | + ? $rawPattern : "({$rawPattern}|{$escapedPattern})"; | ||
| 157 | + | ||
| 158 | + $this->$method("/$pattern/i", $content); | ||
| 159 | + | ||
| 160 | + return $this; | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + /** | ||
| 164 | + * Assert that the current page matches a given URI. | ||
| 165 | + * | ||
| 166 | + * @param string $uri | ||
| 167 | + * @return $this | ||
| 168 | + */ | ||
| 169 | + protected function seePageUrlIs($uri) | ||
| 170 | + { | ||
| 171 | + $this->assertEquals( | ||
| 172 | + $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n" | ||
| 173 | + ); | ||
| 174 | + | ||
| 175 | + return $this; | ||
| 176 | + } | ||
| 177 | + | ||
| 178 | + /** | ||
| 179 | + * Do a forced visit that does not error out on exception. | ||
| 180 | + * @param string $uri | ||
| 181 | + * @param array $parameters | ||
| 182 | + * @param array $cookies | ||
| 183 | + * @param array $files | ||
| 184 | + * @return $this | ||
| 185 | + */ | ||
| 186 | + protected function forceVisit($uri, $parameters = [], $cookies = [], $files = []) | ||
| 187 | + { | ||
| 188 | + $method = 'GET'; | ||
| 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); | ||
| 194 | + return $this; | ||
| 195 | + } | ||
| 196 | + | ||
| 197 | + /** | ||
| 198 | + * Click the text within the selected element. | ||
| 199 | + * @param $parentElement | ||
| 200 | + * @param $linkText | ||
| 201 | + * @return $this | ||
| 202 | + */ | ||
| 203 | + protected function clickInElement($parentElement, $linkText) | ||
| 204 | + { | ||
| 205 | + $elem = $this->crawler->filter($parentElement); | ||
| 206 | + $link = $elem->selectLink($linkText); | ||
| 207 | + $this->visit($link->link()->getUri()); | ||
| 208 | + return $this; | ||
| 209 | + } | ||
| 210 | + | ||
| 211 | + /** | ||
| 212 | + * Check if the page contains the given element. | ||
| 213 | + * @param string $selector | ||
| 214 | + * @return bool | ||
| 215 | + */ | ||
| 216 | + protected function pageHasElement($selector) | ||
| 217 | + { | ||
| 218 | + $elements = $this->crawler->filter($selector); | ||
| 219 | + $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector); | ||
| 220 | + return $this; | ||
| 221 | + } | ||
| 222 | + | ||
| 223 | + /** | ||
| 224 | + * Check if the page contains the given element. | ||
| 225 | + * @param string $selector | ||
| 226 | + * @return bool | ||
| 227 | + */ | ||
| 228 | + protected function pageNotHasElement($selector) | ||
| 229 | + { | ||
| 230 | + $elements = $this->crawler->filter($selector); | ||
| 231 | + $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector); | ||
| 232 | + return $this; | ||
| 233 | + } | ||
| 234 | +} |
| 1 | -<?php namespace Entity; | 1 | +<?php |
| 2 | 2 | ||
| 3 | use BookStack\Tag; | 3 | use BookStack\Tag; |
| 4 | use BookStack\Page; | 4 | use BookStack\Page; |
| 5 | use BookStack\Services\PermissionService; | 5 | use BookStack\Services\PermissionService; |
| 6 | 6 | ||
| 7 | -class TagTest extends \TestCase | 7 | +class TagTest extends BrowserKitTest |
| 8 | { | 8 | { |
| 9 | 9 | ||
| 10 | protected $defaultTagCount = 20; | 10 | protected $defaultTagCount = 20; | ... | ... |
| ... | @@ -9,15 +9,11 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase | ... | @@ -9,15 +9,11 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase |
| 9 | use DatabaseTransactions; | 9 | use DatabaseTransactions; |
| 10 | 10 | ||
| 11 | /** | 11 | /** |
| 12 | - * The base URL to use while testing the application. | 12 | + * The base URL of the application. |
| 13 | * | 13 | * |
| 14 | * @var string | 14 | * @var string |
| 15 | */ | 15 | */ |
| 16 | - protected $baseUrl = 'http://localhost'; | 16 | + public $baseUrl = 'http://localhost'; |
| 17 | - | ||
| 18 | - // Local user instances | ||
| 19 | - private $admin; | ||
| 20 | - private $editor; | ||
| 21 | 17 | ||
| 22 | /** | 18 | /** |
| 23 | * Creates the application. | 19 | * Creates the application. |
| ... | @@ -28,207 +24,8 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase | ... | @@ -28,207 +24,8 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase |
| 28 | { | 24 | { |
| 29 | $app = require __DIR__.'/../bootstrap/app.php'; | 25 | $app = require __DIR__.'/../bootstrap/app.php'; |
| 30 | 26 | ||
| 31 | - $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); | 27 | + $app->make(Kernel::class)->bootstrap(); |
| 32 | 28 | ||
| 33 | return $app; | 29 | return $app; |
| 34 | } | 30 | } |
| 35 | - | ||
| 36 | - /** | ||
| 37 | - * Set the current user context to be an admin. | ||
| 38 | - * @return $this | ||
| 39 | - */ | ||
| 40 | - public function asAdmin() | ||
| 41 | - { | ||
| 42 | - return $this->actingAs($this->getAdmin()); | ||
| 43 | - } | ||
| 44 | - | ||
| 45 | - /** | ||
| 46 | - * Get the current admin user. | ||
| 47 | - * @return mixed | ||
| 48 | - */ | ||
| 49 | - public function getAdmin() { | ||
| 50 | - if($this->admin === null) { | ||
| 51 | - $adminRole = \BookStack\Role::getRole('admin'); | ||
| 52 | - $this->admin = $adminRole->users->first(); | ||
| 53 | - } | ||
| 54 | - return $this->admin; | ||
| 55 | - } | ||
| 56 | - | ||
| 57 | - /** | ||
| 58 | - * Set the current editor context to be an editor. | ||
| 59 | - * @return $this | ||
| 60 | - */ | ||
| 61 | - public function asEditor() | ||
| 62 | - { | ||
| 63 | - if ($this->editor === null) { | ||
| 64 | - $this->editor = $this->getEditor(); | ||
| 65 | - } | ||
| 66 | - return $this->actingAs($this->editor); | ||
| 67 | - } | ||
| 68 | - | ||
| 69 | - /** | ||
| 70 | - * Get a user that's not a system user such as the guest user. | ||
| 71 | - */ | ||
| 72 | - public function getNormalUser() | ||
| 73 | - { | ||
| 74 | - return \BookStack\User::where('system_name', '=', null)->get()->last(); | ||
| 75 | - } | ||
| 76 | - | ||
| 77 | - /** | ||
| 78 | - * Quickly sets an array of settings. | ||
| 79 | - * @param $settingsArray | ||
| 80 | - */ | ||
| 81 | - protected function setSettings($settingsArray) | ||
| 82 | - { | ||
| 83 | - $settings = app('BookStack\Services\SettingService'); | ||
| 84 | - foreach ($settingsArray as $key => $value) { | ||
| 85 | - $settings->put($key, $value); | ||
| 86 | - } | ||
| 87 | - } | ||
| 88 | - | ||
| 89 | - /** | ||
| 90 | - * Create a group of entities that belong to a specific user. | ||
| 91 | - * @param $creatorUser | ||
| 92 | - * @param $updaterUser | ||
| 93 | - * @return array | ||
| 94 | - */ | ||
| 95 | - protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false) | ||
| 96 | - { | ||
| 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); | ||
| 105 | - return [ | ||
| 106 | - 'book' => $book, | ||
| 107 | - 'chapter' => $chapter, | ||
| 108 | - 'page' => $page | ||
| 109 | - ]; | ||
| 110 | - } | ||
| 111 | - | ||
| 112 | - /** | ||
| 113 | - * Quick way to create a new user | ||
| 114 | - * @param array $attributes | ||
| 115 | - * @return mixed | ||
| 116 | - */ | ||
| 117 | - protected function getEditor($attributes = []) | ||
| 118 | - { | ||
| 119 | - $user = factory(\BookStack\User::class)->create($attributes); | ||
| 120 | - $role = \BookStack\Role::getRole('editor'); | ||
| 121 | - $user->attachRole($role);; | ||
| 122 | - return $user; | ||
| 123 | - } | ||
| 124 | - | ||
| 125 | - /** | ||
| 126 | - * Quick way to create a new user without any permissions | ||
| 127 | - * @param array $attributes | ||
| 128 | - * @return mixed | ||
| 129 | - */ | ||
| 130 | - protected function getNewBlankUser($attributes = []) | ||
| 131 | - { | ||
| 132 | - $user = factory(\BookStack\User::class)->create($attributes); | ||
| 133 | - return $user; | ||
| 134 | - } | ||
| 135 | - | ||
| 136 | - /** | ||
| 137 | - * Assert that a given string is seen inside an element. | ||
| 138 | - * | ||
| 139 | - * @param bool|string|null $element | ||
| 140 | - * @param integer $position | ||
| 141 | - * @param string $text | ||
| 142 | - * @param bool $negate | ||
| 143 | - * @return $this | ||
| 144 | - */ | ||
| 145 | - protected function seeInNthElement($element, $position, $text, $negate = false) | ||
| 146 | - { | ||
| 147 | - $method = $negate ? 'assertNotRegExp' : 'assertRegExp'; | ||
| 148 | - | ||
| 149 | - $rawPattern = preg_quote($text, '/'); | ||
| 150 | - | ||
| 151 | - $escapedPattern = preg_quote(e($text), '/'); | ||
| 152 | - | ||
| 153 | - $content = $this->crawler->filter($element)->eq($position)->html(); | ||
| 154 | - | ||
| 155 | - $pattern = $rawPattern == $escapedPattern | ||
| 156 | - ? $rawPattern : "({$rawPattern}|{$escapedPattern})"; | ||
| 157 | - | ||
| 158 | - $this->$method("/$pattern/i", $content); | ||
| 159 | - | ||
| 160 | - return $this; | ||
| 161 | - } | ||
| 162 | - | ||
| 163 | - /** | ||
| 164 | - * Assert that the current page matches a given URI. | ||
| 165 | - * | ||
| 166 | - * @param string $uri | ||
| 167 | - * @return $this | ||
| 168 | - */ | ||
| 169 | - protected function seePageUrlIs($uri) | ||
| 170 | - { | ||
| 171 | - $this->assertEquals( | ||
| 172 | - $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n" | ||
| 173 | - ); | ||
| 174 | - | ||
| 175 | - return $this; | ||
| 176 | - } | ||
| 177 | - | ||
| 178 | - /** | ||
| 179 | - * Do a forced visit that does not error out on exception. | ||
| 180 | - * @param string $uri | ||
| 181 | - * @param array $parameters | ||
| 182 | - * @param array $cookies | ||
| 183 | - * @param array $files | ||
| 184 | - * @return $this | ||
| 185 | - */ | ||
| 186 | - protected function forceVisit($uri, $parameters = [], $cookies = [], $files = []) | ||
| 187 | - { | ||
| 188 | - $method = 'GET'; | ||
| 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); | ||
| 194 | - return $this; | ||
| 195 | - } | ||
| 196 | - | ||
| 197 | - /** | ||
| 198 | - * Click the text within the selected element. | ||
| 199 | - * @param $parentElement | ||
| 200 | - * @param $linkText | ||
| 201 | - * @return $this | ||
| 202 | - */ | ||
| 203 | - protected function clickInElement($parentElement, $linkText) | ||
| 204 | - { | ||
| 205 | - $elem = $this->crawler->filter($parentElement); | ||
| 206 | - $link = $elem->selectLink($linkText); | ||
| 207 | - $this->visit($link->link()->getUri()); | ||
| 208 | - return $this; | ||
| 209 | - } | ||
| 210 | - | ||
| 211 | - /** | ||
| 212 | - * Check if the page contains the given element. | ||
| 213 | - * @param string $selector | ||
| 214 | - * @return bool | ||
| 215 | - */ | ||
| 216 | - protected function pageHasElement($selector) | ||
| 217 | - { | ||
| 218 | - $elements = $this->crawler->filter($selector); | ||
| 219 | - $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector); | ||
| 220 | - return $this; | ||
| 221 | - } | ||
| 222 | - | ||
| 223 | - /** | ||
| 224 | - * Check if the page contains the given element. | ||
| 225 | - * @param string $selector | ||
| 226 | - * @return bool | ||
| 227 | - */ | ||
| 228 | - protected function pageNotHasElement($selector) | ||
| 229 | - { | ||
| 230 | - $elements = $this->crawler->filter($selector); | ||
| 231 | - $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector); | ||
| 232 | - return $this; | ||
| 233 | - } | ||
| 234 | } | 31 | } | ... | ... |
-
Please register or sign in to post a comment