0
0
Fork 0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-04-05 21:35:24 +00:00

Removed browserkit from a couple of classess

Done a little reorganisation while there of misplaced tests.
Moved MarkdownTest to a new PageEditorTest to avoid confusion with
other markdown elements and to align with other page tests.
This commit is contained in:
Dan Brown 2021-09-13 22:54:21 +01:00
parent 8565187138
commit badaf08e55
No known key found for this signature in database
GPG key ID: 46D9F943C24A2EF9
9 changed files with 220 additions and 221 deletions

View file

@ -15,6 +15,9 @@ use Illuminate\Support\Collection;
* @property string $description * @property string $description
* @property int $image_id * @property int $image_id
* @property Image|null $cover * @property Image|null $cover
* @property \Illuminate\Database\Eloquent\Collection $chapters
* @property \Illuminate\Database\Eloquent\Collection $pages
* @property \Illuminate\Database\Eloquent\Collection $directPages
*/ */
class Book extends Entity implements HasCoverImage class Book extends Entity implements HasCoverImage
{ {

View file

@ -61,25 +61,6 @@ abstract class BrowserKitTest extends TestCase
} }
} }
/**
* Create a group of entities that belong to a specific user.
*/
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 = factory(Book::class)->create($userAttrs);
$chapter = factory(Chapter::class)->create(array_merge(['book_id' => $book->id], $userAttrs));
$page = factory(Page::class)->create(array_merge(['book_id' => $book->id, 'chapter_id' => $chapter->id], $userAttrs));
$restrictionService = $this->app[PermissionService::class];
$restrictionService->buildJointPermissionsForEntity($book);
return compact('book', 'chapter', 'page');
}
/** /**
* Helper for updating entity permissions. * Helper for updating entity permissions.
* *
@ -91,20 +72,6 @@ abstract class BrowserKitTest extends TestCase
$restrictionService->buildJointPermissionsForEntity($entity); $restrictionService->buildJointPermissionsForEntity($entity);
} }
/**
* Quick way to create a new user without any permissions.
*
* @param array $attributes
*
* @return mixed
*/
protected function getNewBlankUser($attributes = [])
{
$user = factory(User::class)->create($attributes);
return $user;
}
/** /**
* Assert that a given string is seen inside an element. * Assert that a given string is seen inside an element.
* *

View file

@ -1,53 +0,0 @@
<?php
namespace Tests\Entity;
use Tests\BrowserKitTest;
class MarkdownTest extends BrowserKitTest
{
protected $page;
public function setUp(): void
{
parent::setUp();
$this->page = \BookStack\Entities\Models\Page::first();
}
protected function setMarkdownEditor()
{
$this->setSettings(['app-editor' => 'markdown']);
}
public function test_default_editor_is_wysiwyg()
{
$this->assertEquals(setting('app-editor'), 'wysiwyg');
$this->asAdmin()->visit($this->page->getUrl() . '/edit')
->pageHasElement('#html-editor');
}
public function test_markdown_setting_shows_markdown_editor()
{
$this->setMarkdownEditor();
$this->asAdmin()->visit($this->page->getUrl() . '/edit')
->pageNotHasElement('#html-editor')
->pageHasElement('#markdown-editor');
}
public function test_markdown_content_given_to_editor()
{
$this->setMarkdownEditor();
$mdContent = '# hello. This is a test';
$this->page->markdown = $mdContent;
$this->page->save();
$this->asAdmin()->visit($this->page->getUrl() . '/edit')
->seeInField('markdown', $mdContent);
}
public function test_html_content_given_to_editor_if_no_markdown()
{
$this->setMarkdownEditor();
$this->asAdmin()->visit($this->page->getUrl() . '/edit')
->seeInField('markdown', $this->page->html);
}
}

View file

@ -2,12 +2,16 @@
namespace Tests\Entity; namespace Tests\Entity;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Page; use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\PageRepo; use BookStack\Entities\Repos\PageRepo;
use Tests\BrowserKitTest; use Tests\TestCase;
class PageDraftTest extends BrowserKitTest class PageDraftTest extends TestCase
{ {
/**
* @var Page
*/
protected $page; protected $page;
/** /**
@ -18,99 +22,101 @@ class PageDraftTest extends BrowserKitTest
public function setUp(): void public function setUp(): void
{ {
parent::setUp(); parent::setUp();
$this->page = \BookStack\Entities\Models\Page::first(); $this->page = Page::query()->first();
$this->pageRepo = app(PageRepo::class); $this->pageRepo = app()->make(PageRepo::class);
} }
public function test_draft_content_shows_if_available() public function test_draft_content_shows_if_available()
{ {
$addedContent = '<p>test message content</p>'; $addedContent = '<p>test message content</p>';
$this->asAdmin()->visit($this->page->getUrl('/edit'))
->dontSeeInField('html', $addedContent); $this->asAdmin()->get($this->page->getUrl('/edit'))
->assertElementNotContains('[name="html"]', $addedContent);
$newContent = $this->page->html . $addedContent; $newContent = $this->page->html . $addedContent;
$this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]); $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
$this->asAdmin()->visit($this->page->getUrl('/edit')) $this->asAdmin()->get($this->page->getUrl('/edit'))
->seeInField('html', $newContent); ->assertElementContains('[name="html"]', $newContent);
} }
public function test_draft_not_visible_by_others() public function test_draft_not_visible_by_others()
{ {
$addedContent = '<p>test message content</p>'; $addedContent = '<p>test message content</p>';
$this->asAdmin()->visit($this->page->getUrl('/edit')) $this->asAdmin()->get($this->page->getUrl('/edit'))
->dontSeeInField('html', $addedContent); ->assertElementNotContains('[name="html"]', $addedContent);
$newContent = $this->page->html . $addedContent; $newContent = $this->page->html . $addedContent;
$newUser = $this->getEditor(); $newUser = $this->getEditor();
$this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]); $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
$this->actingAs($newUser)->visit($this->page->getUrl('/edit')) $this->actingAs($newUser)->get($this->page->getUrl('/edit'))
->dontSeeInField('html', $newContent); ->assertElementNotContains('[name="html"]', $newContent);
} }
public function test_alert_message_shows_if_editing_draft() public function test_alert_message_shows_if_editing_draft()
{ {
$this->asAdmin(); $this->asAdmin();
$this->pageRepo->updatePageDraft($this->page, ['html' => 'test content']); $this->pageRepo->updatePageDraft($this->page, ['html' => 'test content']);
$this->asAdmin()->visit($this->page->getUrl('/edit')) $this->asAdmin()->get($this->page->getUrl('/edit'))
->see('You are currently editing a draft'); ->assertSee('You are currently editing a draft');
} }
public function test_alert_message_shows_if_someone_else_editing() public function test_alert_message_shows_if_someone_else_editing()
{ {
$nonEditedPage = \BookStack\Entities\Models\Page::take(10)->get()->last(); $nonEditedPage = Page::query()->take(10)->get()->last();
$addedContent = '<p>test message content</p>'; $addedContent = '<p>test message content</p>';
$this->asAdmin()->visit($this->page->getUrl('/edit')) $this->asAdmin()->get($this->page->getUrl('/edit'))
->dontSeeInField('html', $addedContent); ->assertElementNotContains('[name="html"]', $addedContent);
$newContent = $this->page->html . $addedContent; $newContent = $this->page->html . $addedContent;
$newUser = $this->getEditor(); $newUser = $this->getEditor();
$this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]); $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
$this->actingAs($newUser) $this->actingAs($newUser)
->visit($this->page->getUrl('/edit')) ->get($this->page->getUrl('/edit'))
->see('Admin has started editing this page'); ->assertSee('Admin has started editing this page');
$this->flushSession(); $this->flushSession();
$this->visit($nonEditedPage->getUrl() . '/edit') $this->get($nonEditedPage->getUrl() . '/edit')
->dontSeeInElement('.notification', 'Admin has started editing this page'); ->assertElementNotContains('.notification', 'Admin has started editing this page');
} }
public function test_draft_pages_show_on_homepage() public function test_draft_pages_show_on_homepage()
{ {
$book = \BookStack\Entities\Models\Book::first(); /** @var Book $book */
$this->asAdmin()->visit('/') $book = Book::query()->first();
->dontSeeInElement('#recent-drafts', 'New Page') $this->asAdmin()->get('/')
->visit($book->getUrl() . '/create-page') ->assertElementNotContains('#recent-drafts', 'New Page');
->visit('/')
->seeInElement('#recent-drafts', 'New Page'); $this->get($book->getUrl() . '/create-page');
$this->get('/')->assertElementContains('#recent-drafts', 'New Page');
} }
public function test_draft_pages_not_visible_by_others() public function test_draft_pages_not_visible_by_others()
{ {
$book = \BookStack\Entities\Models\Book::first(); /** @var Book $book */
$book = Book::query()->first();
$chapter = $book->chapters->first(); $chapter = $book->chapters->first();
$newUser = $this->getEditor(); $newUser = $this->getEditor();
$this->actingAs($newUser)->visit('/') $this->actingAs($newUser)->get($book->getUrl('/create-page'));
->visit($book->getUrl('/create-page')) $this->get($chapter->getUrl('/create-page'));
->visit($chapter->getUrl('/create-page')) $this->get($book->getUrl())
->visit($book->getUrl()) ->assertElementContains('.book-contents', 'New Page');
->seeInElement('.book-contents', 'New Page');
$this->asAdmin() $this->asAdmin()->get($book->getUrl())
->visit($book->getUrl()) ->assertElementNotContains('.book-contents', 'New Page');
->dontSeeInElement('.book-contents', 'New Page') $this->get($chapter->getUrl())
->visit($chapter->getUrl()) ->assertElementNotContains('.book-contents', 'New Page');
->dontSeeInElement('.book-contents', 'New Page');
} }
public function test_page_html_in_ajax_fetch_response() public function test_page_html_in_ajax_fetch_response()
{ {
$this->asAdmin(); $this->asAdmin();
/** @var Page $page */
$page = Page::query()->first(); $page = Page::query()->first();
$this->getJson('/ajax/page/' . $page->id); $this->getJson('/ajax/page/' . $page->id)->assertJson([
$this->seeJson([
'html' => $page->html, 'html' => $page->html,
]); ]);
} }

View file

@ -0,0 +1,52 @@
<?php
namespace Tests\Entity;
use BookStack\Entities\Models\Page;
use Tests\TestCase;
class PageEditorTest extends TestCase
{
/** @var Page */
protected $page;
public function setUp(): void
{
parent::setUp();
$this->page = Page::query()->first();
}
public function test_default_editor_is_wysiwyg()
{
$this->assertEquals('wysiwyg', setting('app-editor'));
$this->asAdmin()->get($this->page->getUrl() . '/edit')
->assertElementExists('#html-editor');
}
public function test_markdown_setting_shows_markdown_editor()
{
$this->setSettings(['app-editor' => 'markdown']);
$this->asAdmin()->get($this->page->getUrl() . '/edit')
->assertElementNotExists('#html-editor')
->assertElementExists('#markdown-editor');
}
public function test_markdown_content_given_to_editor()
{
$this->setSettings(['app-editor' => 'markdown']);
$mdContent = '# hello. This is a test';
$this->page->markdown = $mdContent;
$this->page->save();
$this->asAdmin()->get($this->page->getUrl() . '/edit')
->assertElementContains('[name="markdown"]', $mdContent);
}
public function test_html_content_given_to_editor_if_no_markdown()
{
$this->setSettings(['app-editor' => 'markdown']);
$this->asAdmin()->get($this->page->getUrl() . '/edit')
->assertElementContains('[name="markdown"]', $this->page->html);
}
}

View file

@ -211,6 +211,25 @@ trait SharedTestHelpers
return $permissionRepo->saveNewRole($roleData); return $permissionRepo->saveNewRole($roleData);
} }
/**
* Create a group of entities that belong to a specific user.
*/
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 = factory(Book::class)->create($userAttrs);
$chapter = factory(Chapter::class)->create(array_merge(['book_id' => $book->id], $userAttrs));
$page = factory(Page::class)->create(array_merge(['book_id' => $book->id, 'chapter_id' => $chapter->id], $userAttrs));
$restrictionService = $this->app[PermissionService::class];
$restrictionService->buildJointPermissionsForEntity($book);
return compact('book', 'chapter', 'page');
}
/** /**
* Mock the HttpFetcher service and return the given data on fetch. * Mock the HttpFetcher service and return the given data on fetch.
*/ */

View file

@ -42,4 +42,26 @@ class UserManagementTest extends TestCase
'owned_by' => $newOwner->id, 'owned_by' => $newOwner->id,
]); ]);
} }
public function test_guest_profile_shows_limited_form()
{
$guest = User::getDefault();
$resp = $this->asAdmin()->get('/settings/users/' . $guest->id);
$resp->assertSee('Guest');
$resp->assertElementNotExists('#password');
}
public function test_guest_profile_cannot_be_deleted()
{
$guestUser = User::getDefault();
$resp = $this->asAdmin()->get('/settings/users/' . $guestUser->id . '/delete');
$resp->assertSee('Delete User');
$resp->assertSee('Guest');
$resp->assertElementContains('form[action$="/settings/users/' . $guestUser->id . '"] button', 'Confirm');
$resp = $this->delete('/settings/users/' . $guestUser->id);
$resp->assertRedirect('/settings/users/' . $guestUser->id);
$resp = $this->followRedirects($resp);
$resp->assertSee('cannot delete the guest user');
}
} }

View file

@ -2,6 +2,7 @@
namespace Tests\User; namespace Tests\User;
use BookStack\Entities\Models\Bookshelf;
use Tests\TestCase; use Tests\TestCase;
class UserPreferencesTest extends TestCase class UserPreferencesTest extends TestCase
@ -106,4 +107,44 @@ class UserPreferencesTest extends TestCase
$home = $this->get('/login'); $home = $this->get('/login');
$home->assertElementExists('.dark-mode'); $home->assertElementExists('.dark-mode');
} }
public function test_books_view_type_preferences_when_list()
{
$editor = $this->getEditor();
setting()->putUser($editor, 'books_view_type', 'list');
$this->actingAs($editor)->get('/books')
->assertElementNotExists('.featured-image-container')
->assertElementExists('.content-wrap .entity-list-item');
}
public function test_books_view_type_preferences_when_grid()
{
$editor = $this->getEditor();
setting()->putUser($editor, 'books_view_type', 'grid');
$this->actingAs($editor)->get('/books')
->assertElementExists('.featured-image-container');
}
public function test_shelf_view_type_change()
{
$editor = $this->getEditor();
/** @var Bookshelf $shelf */
$shelf = Bookshelf::query()->first();
setting()->putUser($editor, 'bookshelf_view_type', 'list');
$this->actingAs($editor)->get($shelf->getUrl())
->assertElementNotExists('.featured-image-container')
->assertElementExists('.content-wrap .entity-list-item')
->assertSee('Grid View');
$req = $this->patch("/settings/users/{$editor->id}/switch-shelf-view", ['view_type' => 'grid']);
$req->assertRedirect($shelf->getUrl());
$this->actingAs($editor)->get($shelf->getUrl())
->assertElementExists('.featured-image-container')
->assertElementNotExists('.content-wrap .entity-list-item')
->assertSee('List View');
}
} }

View file

@ -5,11 +5,13 @@ namespace Tests\User;
use Activity; use Activity;
use BookStack\Actions\ActivityType; use BookStack\Actions\ActivityType;
use BookStack\Auth\User; use BookStack\Auth\User;
use BookStack\Entities\Models\Bookshelf; use Tests\TestCase;
use Tests\BrowserKitTest;
class UserProfileTest extends BrowserKitTest class UserProfileTest extends TestCase
{ {
/**
* @var User
*/
protected $user; protected $user;
public function setUp(): void public function setUp(): void
@ -21,74 +23,73 @@ class UserProfileTest extends BrowserKitTest
public function test_profile_page_shows_name() public function test_profile_page_shows_name()
{ {
$this->asAdmin() $this->asAdmin()
->visit('/user/' . $this->user->slug) ->get('/user/' . $this->user->slug)
->see($this->user->name); ->assertSee($this->user->name);
} }
public function test_profile_page_shows_recent_entities() public function test_profile_page_shows_recent_entities()
{ {
$content = $this->createEntityChainBelongingToUser($this->user, $this->user); $content = $this->createEntityChainBelongingToUser($this->user, $this->user);
$this->asAdmin() $resp = $this->asAdmin()->get('/user/' . $this->user->slug);
->visit('/user/' . $this->user->slug) // Check the recently created page is shown
// Check the recently created page is shown $resp->assertSee($content['page']->name);
->see($content['page']->name) // Check the recently created chapter is shown
// Check the recently created chapter is shown $resp->assertSee($content['chapter']->name);
->see($content['chapter']->name) // Check the recently created book is shown
// Check the recently created book is shown $resp->assertSee($content['book']->name);
->see($content['book']->name);
} }
public function test_profile_page_shows_created_content_counts() public function test_profile_page_shows_created_content_counts()
{ {
$newUser = $this->getNewBlankUser(); $newUser = factory(User::class)->create();
$this->asAdmin()->visit('/user/' . $newUser->slug) $this->asAdmin()->get('/user/' . $newUser->slug)
->see($newUser->name) ->assertSee($newUser->name)
->seeInElement('#content-counts', '0 Books') ->assertElementContains('#content-counts', '0 Books')
->seeInElement('#content-counts', '0 Chapters') ->assertElementContains('#content-counts', '0 Chapters')
->seeInElement('#content-counts', '0 Pages'); ->assertElementContains('#content-counts', '0 Pages');
$this->createEntityChainBelongingToUser($newUser, $newUser); $this->createEntityChainBelongingToUser($newUser, $newUser);
$this->asAdmin()->visit('/user/' . $newUser->slug) $this->asAdmin()->get('/user/' . $newUser->slug)
->see($newUser->name) ->assertSee($newUser->name)
->seeInElement('#content-counts', '1 Book') ->assertElementContains('#content-counts', '1 Book')
->seeInElement('#content-counts', '1 Chapter') ->assertElementContains('#content-counts', '1 Chapter')
->seeInElement('#content-counts', '1 Page'); ->assertElementContains('#content-counts', '1 Page');
} }
public function test_profile_page_shows_recent_activity() public function test_profile_page_shows_recent_activity()
{ {
$newUser = $this->getNewBlankUser(); $newUser = factory(User::class)->create();
$this->actingAs($newUser); $this->actingAs($newUser);
$entities = $this->createEntityChainBelongingToUser($newUser, $newUser); $entities = $this->createEntityChainBelongingToUser($newUser, $newUser);
Activity::addForEntity($entities['book'], ActivityType::BOOK_UPDATE); Activity::addForEntity($entities['book'], ActivityType::BOOK_UPDATE);
Activity::addForEntity($entities['page'], ActivityType::PAGE_CREATE); Activity::addForEntity($entities['page'], ActivityType::PAGE_CREATE);
$this->asAdmin()->visit('/user/' . $newUser->slug) $this->asAdmin()->get('/user/' . $newUser->slug)
->seeInElement('#recent-user-activity', 'updated book') ->assertElementContains('#recent-user-activity', 'updated book')
->seeInElement('#recent-user-activity', 'created page') ->assertElementContains('#recent-user-activity', 'created page')
->seeInElement('#recent-user-activity', $entities['page']->name); ->assertElementContains('#recent-user-activity', $entities['page']->name);
} }
public function test_clicking_user_name_in_activity_leads_to_profile_page() public function test_user_activity_has_link_leading_to_profile()
{ {
$newUser = $this->getNewBlankUser(); $newUser = factory(User::class)->create();
$this->actingAs($newUser); $this->actingAs($newUser);
$entities = $this->createEntityChainBelongingToUser($newUser, $newUser); $entities = $this->createEntityChainBelongingToUser($newUser, $newUser);
Activity::addForEntity($entities['book'], ActivityType::BOOK_UPDATE); Activity::addForEntity($entities['book'], ActivityType::BOOK_UPDATE);
Activity::addForEntity($entities['page'], ActivityType::PAGE_CREATE); Activity::addForEntity($entities['page'], ActivityType::PAGE_CREATE);
$this->asAdmin()->visit('/')->clickInElement('#recent-activity', $newUser->name) $linkSelector = '#recent-activity a[href$="/user/' . $newUser->slug . '"]';
->seePageIs('/user/' . $newUser->slug) $this->asAdmin()->get('/')
->see($newUser->name); ->assertElementContains($linkSelector, $newUser->name);
} }
public function test_profile_has_search_links_in_created_entity_lists() public function test_profile_has_search_links_in_created_entity_lists()
{ {
$user = $this->getEditor(); $user = $this->getEditor();
$resp = $this->actingAs($this->getAdmin())->visit('/user/' . $user->slug); $resp = $this->actingAs($this->getAdmin())->get('/user/' . $user->slug);
$expectedLinks = [ $expectedLinks = [
'/search?term=%7Bcreated_by%3A' . $user->slug . '%7D+%7Btype%3Apage%7D', '/search?term=%7Bcreated_by%3A' . $user->slug . '%7D+%7Btype%3Apage%7D',
@ -98,66 +99,7 @@ class UserProfileTest extends BrowserKitTest
]; ];
foreach ($expectedLinks as $link) { foreach ($expectedLinks as $link) {
$resp->seeInElement('[href$="' . $link . '"]', 'View All'); $resp->assertElementContains('[href$="' . $link . '"]', 'View All');
} }
} }
public function test_guest_profile_shows_limited_form()
{
$this->asAdmin()
->visit('/settings/users')
->click('Guest')
->dontSeeElement('#password');
}
public function test_guest_profile_cannot_be_deleted()
{
$guestUser = User::getDefault();
$this->asAdmin()->visit('/settings/users/' . $guestUser->id . '/delete')
->see('Delete User')->see('Guest')
->press('Confirm')
->seePageIs('/settings/users/' . $guestUser->id)
->see('cannot delete the guest user');
}
public function test_books_view_is_list()
{
$editor = $this->getEditor();
setting()->putUser($editor, 'books_view_type', 'list');
$this->actingAs($editor)
->visit('/books')
->pageNotHasElement('.featured-image-container')
->pageHasElement('.content-wrap .entity-list-item');
}
public function test_books_view_is_grid()
{
$editor = $this->getEditor();
setting()->putUser($editor, 'books_view_type', 'grid');
$this->actingAs($editor)
->visit('/books')
->pageHasElement('.featured-image-container');
}
public function test_shelf_view_type_change()
{
$editor = $this->getEditor();
$shelf = Bookshelf::query()->first();
setting()->putUser($editor, 'bookshelf_view_type', 'list');
$this->actingAs($editor)->visit($shelf->getUrl())
->pageNotHasElement('.featured-image-container')
->pageHasElement('.content-wrap .entity-list-item')
->see('Grid View');
$req = $this->patch("/settings/users/{$editor->id}/switch-shelf-view", ['view_type' => 'grid']);
$req->assertRedirectedTo($shelf->getUrl());
$this->actingAs($editor)->visit($shelf->getUrl())
->pageHasElement('.featured-image-container')
->pageNotHasElement('.content-wrap .entity-list-item')
->see('List View');
}
} }