-<?php
+<?php namespace Tests;
+use BookStack\Entity;
+use BookStack\Role;
+use BookStack\Services\PermissionService;
+use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Testing\DatabaseTransactions;
+use Laravel\BrowserKitTesting\TestCase;
use Symfony\Component\DomCrawler\Crawler;
-abstract class BrowserKitTest extends \Laravel\BrowserKitTesting\TestCase
+abstract class BrowserKitTest extends TestCase
{
use DatabaseTransactions;
+ // Local user instances
+ private $admin;
+ private $editor;
+
/**
* The base URL to use while testing the application.
- *
* @var string
*/
protected $baseUrl = 'http://localhost';
- // Local user instances
- private $admin;
- private $editor;
+ public function tearDown()
+ {
+ \DB::disconnect();
+ parent::tearDown();
+ }
/**
* Creates the application.
{
$app = require __DIR__.'/../bootstrap/app.php';
- $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
+ $app->make(Kernel::class)->bootstrap();
return $app;
}
*/
public function getAdmin() {
if($this->admin === null) {
- $adminRole = \BookStack\Role::getRole('admin');
+ $adminRole = Role::getSystemRole('admin');
$this->admin = $adminRole->users->first();
}
return $this->admin;
protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
{
if ($updaterUser === false) $updaterUser = $creatorUser;
- $book = factory(BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
- $chapter = factory(BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
- $page = factory(BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
- $book->chapters()->saveMany([$chapter]);
- $chapter->pages()->saveMany([$page]);
- $restrictionService = $this->app[\BookStack\Services\PermissionService::class];
+ $book = factory(\BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
+ $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
+ $page = factory(\BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id, 'chapter_id' => $chapter->id]);
+ $restrictionService = $this->app[PermissionService::class];
$restrictionService->buildJointPermissionsForEntity($book);
return [
'book' => $book,
}
/**
- * Quick way to create a new user
+ * Helper for updating entity permissions.
+ * @param Entity $entity
+ */
+ protected function updateEntityPermissions(Entity $entity)
+ {
+ $restrictionService = $this->app[PermissionService::class];
+ $restrictionService->buildJointPermissionsForEntity($entity);
+ }
+
+ /**
+ * Get an instance of a user with 'editor' permissions
* @param array $attributes
* @return mixed
*/
protected function getEditor($attributes = [])
{
- $user = factory(\BookStack\User::class)->create($attributes);
- $role = \BookStack\Role::getRole('editor');
- $user->attachRole($role);;
+ $user = \BookStack\Role::getRole('editor')->users()->first();
+ if (!empty($attributes)) $user->forceFill($attributes)->save();
+ return $user;
+ }
+
+ /**
+ * Get an instance of a user with 'viewer' permissions
+ * @return mixed
+ */
+ protected function getViewer()
+ {
+ $user = \BookStack\Role::getRole('viewer')->users()->first();
+ if (!empty($attributes)) $user->forceFill($attributes)->save();
return $user;
}
/**
* Check if the page contains the given element.
* @param string $selector
- * @return bool
*/
protected function pageHasElement($selector)
{
/**
* Check if the page contains the given element.
* @param string $selector
- * @return bool
*/
protected function pageNotHasElement($selector)
{