X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/0bb5654f80a8ba2eeab50ec6d7d5f37080e83258..refs/pull/3918/head:/tests/TestCase.php diff --git a/tests/TestCase.php b/tests/TestCase.php index 92ae33a4e..d0dd7d772 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -7,31 +7,27 @@ use BookStack\Auth\Permissions\PermissionsRepo; use BookStack\Auth\Permissions\RolePermission; use BookStack\Auth\Role; use BookStack\Auth\User; -use BookStack\Entities\Models\Book; -use BookStack\Entities\Models\Bookshelf; -use BookStack\Entities\Models\Chapter; use BookStack\Entities\Models\Entity; -use BookStack\Entities\Models\Page; -use BookStack\Entities\Repos\BookRepo; -use BookStack\Entities\Repos\BookshelfRepo; -use BookStack\Entities\Repos\ChapterRepo; -use BookStack\Entities\Repos\PageRepo; use BookStack\Settings\SettingService; use BookStack\Uploads\HttpFetcher; use GuzzleHttp\Client; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Middleware; +use Illuminate\Contracts\Console\Kernel; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; use Illuminate\Http\JsonResponse; use Illuminate\Support\Env; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Illuminate\Testing\Assert as PHPUnit; use Monolog\Handler\TestHandler; use Monolog\Logger; use Psr\Http\Client\ClientInterface; use Ssddanbrown\AssertHtml\TestsHtml; +use Tests\Helpers\EntityProvider; +use Tests\Helpers\TestServiceProvider; abstract class TestCase extends BaseTestCase { @@ -41,12 +37,34 @@ abstract class TestCase extends BaseTestCase protected ?User $admin = null; protected ?User $editor = null; + protected EntityProvider $entities; + + protected function setUp(): void + { + $this->entities = new EntityProvider(); + parent::setUp(); + } /** * The base URL to use while testing the application. */ protected string $baseUrl = 'http://localhost'; + /** + * Creates the application. + * + * @return \Illuminate\Foundation\Application + */ + public function createApplication() + { + /** @var \Illuminate\Foundation\Application $app */ + $app = require __DIR__ . '/../bootstrap/app.php'; + $app->register(TestServiceProvider::class); + $app->make(Kernel::class)->bootstrap(); + + return $app; + } + /** * Set the current user context to be an admin. */ @@ -89,6 +107,14 @@ abstract class TestCase extends BaseTestCase return $this->editor; } + /** + * Set the current user context to be a viewer. + */ + public function asViewer() + { + return $this->actingAs($this->getViewer()); + } + /** * Get an instance of a user with 'viewer' permissions. */ @@ -110,51 +136,6 @@ abstract class TestCase extends BaseTestCase return User::query()->where('system_name', '=', null)->get()->last(); } - /** - * Regenerate the permission for an entity. - */ - protected function regenEntityPermissions(Entity $entity): void - { - $entity->rebuildPermissions(); - $entity->load('jointPermissions'); - } - - /** - * Create and return a new bookshelf. - */ - public function newShelf(array $input = ['name' => 'test shelf', 'description' => 'My new test shelf']): Bookshelf - { - return app(BookshelfRepo::class)->create($input, []); - } - - /** - * Create and return a new book. - */ - public function newBook(array $input = ['name' => 'test book', 'description' => 'My new test book']): Book - { - return app(BookRepo::class)->create($input); - } - - /** - * Create and return a new test chapter. - */ - public function newChapter(array $input, Book $book): Chapter - { - return app(ChapterRepo::class)->create($input, $book); - } - - /** - * Create and return a new test page. - */ - public function newPage(array $input = ['name' => 'test page', 'html' => 'My new test page']): Page - { - $book = Book::query()->first(); - $pageRepo = app(PageRepo::class); - $draftPage = $pageRepo->getNewDraftPage($book); - - return $pageRepo->publishDraft($draftPage, $input); - } - /** * Quickly sets an array of settings. */ @@ -166,31 +147,6 @@ abstract class TestCase extends BaseTestCase } } - /** - * Manually set some permissions on an entity. - */ - protected function setEntityRestrictions(Entity $entity, array $actions = [], array $roles = []): void - { - $entity->restricted = true; - $entity->permissions()->delete(); - - $permissions = []; - foreach ($actions as $action) { - foreach ($roles as $role) { - $permissions[] = [ - 'role_id' => $role->id, - 'action' => strtolower($action), - ]; - } - } - $entity->permissions()->createMany($permissions); - - $entity->save(); - $entity->load('permissions'); - $this->app->make(JointPermissionBuilder::class)->rebuildForEntity($entity); - $entity->load('jointPermissions'); - } - /** * Give the given user some permissions. */ @@ -237,27 +193,6 @@ abstract class TestCase extends BaseTestCase return $permissionRepo->saveNewRole($roleData); } - /** - * Create a group of entities that belong to a specific user. - * - * @return array{book: Book, chapter: Chapter, page: Page} - */ - protected function createEntityChainBelongingToUser(User $creatorUser, ?User $updaterUser = null): array - { - if (empty($updaterUser)) { - $updaterUser = $creatorUser; - } - - $userAttrs = ['created_by' => $creatorUser->id, 'owned_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]; - $book = Book::factory()->create($userAttrs); - $chapter = Chapter::factory()->create(array_merge(['book_id' => $book->id], $userAttrs)); - $page = Page::factory()->create(array_merge(['book_id' => $book->id, 'chapter_id' => $chapter->id], $userAttrs)); - - $this->app->make(JointPermissionBuilder::class)->rebuildForEntity($book); - - return compact('book', 'chapter', 'page'); - } - /** * Mock the HttpFetcher service and return the given data on fetch. */ @@ -291,6 +226,8 @@ abstract class TestCase extends BaseTestCase /** * Run a set test with the given env variable. * Remembers the original and resets the value after test. + * Database config is juggled so the value can be restored when + * parallel testing are used, where multiple databases exist. */ protected function runWithEnv(string $name, $value, callable $callback) { @@ -303,7 +240,12 @@ abstract class TestCase extends BaseTestCase $_SERVER[$name] = $value; } + $database = config('database.connections.mysql_testing.database'); $this->refreshApplication(); + + DB::purge(); + config()->set('database.connections.mysql_testing.database', $database); + $callback(); if (is_null($originalVal)) { @@ -388,7 +330,7 @@ abstract class TestCase extends BaseTestCase protected function assertNotificationContains(\Illuminate\Testing\TestResponse $resp, string $text) { - return $this->withHtml($resp)->assertElementContains('[notification]', $text); + return $this->withHtml($resp)->assertElementContains('.notification[role="alert"]', $text); } /** @@ -428,17 +370,4 @@ abstract class TestCase extends BaseTestCase $this->assertDatabaseHas('activities', $detailsToCheck); } - - /** - * @return Entity[] - */ - protected function getEachEntityType(): array - { - return [ - 'page' => Page::query()->first(), - 'chapter' => Chapter::query()->first(), - 'book' => Book::query()->first(), - 'bookshelf' => Bookshelf::query()->first(), - ]; - } }