2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Entity;
|
2020-11-06 07:54:39 -05:00
|
|
|
|
2021-01-04 12:52:08 -05:00
|
|
|
use BookStack\Entities\Models\Book;
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Models\Page;
|
2021-09-15 17:18:37 -04:00
|
|
|
use Carbon\Carbon;
|
2020-11-06 07:54:39 -05:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class PageTest extends TestCase
|
|
|
|
{
|
2021-09-17 16:29:16 -04:00
|
|
|
public function test_create()
|
|
|
|
{
|
2022-09-29 12:31:38 -04:00
|
|
|
$chapter = $this->entities->chapter();
|
2021-10-30 16:29:59 -04:00
|
|
|
$page = Page::factory()->make([
|
2021-09-17 16:29:16 -04:00
|
|
|
'name' => 'My First Page',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$resp = $this->asEditor()->get($chapter->getUrl());
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('a[href="' . $chapter->getUrl('/create-page') . '"]', 'New Page');
|
2021-09-17 16:29:16 -04:00
|
|
|
|
|
|
|
$resp = $this->get($chapter->getUrl('/create-page'));
|
|
|
|
/** @var Page $draftPage */
|
|
|
|
$draftPage = Page::query()
|
|
|
|
->where('draft', '=', true)
|
|
|
|
->orderBy('created_at', 'desc')
|
|
|
|
->first();
|
|
|
|
$resp->assertRedirect($draftPage->getUrl());
|
|
|
|
|
|
|
|
$resp = $this->get($draftPage->getUrl());
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('form[action="' . $draftPage->getUrl() . '"][method="POST"]', 'Save Page');
|
2021-09-17 16:29:16 -04:00
|
|
|
|
|
|
|
$resp = $this->post($draftPage->getUrl(), $draftPage->only('name', 'html'));
|
|
|
|
$draftPage->refresh();
|
|
|
|
$resp->assertRedirect($draftPage->getUrl());
|
|
|
|
}
|
|
|
|
|
2021-04-20 16:03:44 -04:00
|
|
|
public function test_page_view_when_creator_is_deleted_but_owner_exists()
|
|
|
|
{
|
2022-09-29 12:31:38 -04:00
|
|
|
$page = $this->entities->page();
|
2023-01-21 06:08:34 -05:00
|
|
|
$user = $this->users->viewer();
|
|
|
|
$owner = $this->users->editor();
|
2021-04-20 16:03:44 -04:00
|
|
|
$page->created_by = $user->id;
|
|
|
|
$page->owned_by = $owner->id;
|
|
|
|
$page->save();
|
|
|
|
$user->delete();
|
|
|
|
|
|
|
|
$resp = $this->asAdmin()->get($page->getUrl());
|
|
|
|
$resp->assertStatus(200);
|
|
|
|
$resp->assertSeeText('Owned by ' . $owner->name);
|
|
|
|
}
|
|
|
|
|
2023-05-31 11:38:20 -04:00
|
|
|
public function test_page_show_includes_pointer_section_select_mode_button()
|
|
|
|
{
|
|
|
|
$page = $this->entities->page();
|
|
|
|
$resp = $this->asEditor()->get($page->getUrl());
|
|
|
|
$this->withHtml($resp)->assertElementContains('.content-wrap button.screen-reader-only', 'Enter section select mode');
|
|
|
|
}
|
|
|
|
|
2021-01-04 12:52:08 -05:00
|
|
|
public function test_page_creation_with_markdown_content()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-editor' => 'markdown']);
|
2022-09-29 12:31:38 -04:00
|
|
|
$book = $this->entities->book();
|
2021-01-04 12:52:08 -05:00
|
|
|
|
|
|
|
$this->asEditor()->get($book->getUrl('/create-page'));
|
|
|
|
$draft = Page::query()->where('book_id', '=', $book->id)
|
|
|
|
->where('draft', '=', true)->first();
|
|
|
|
|
|
|
|
$details = [
|
|
|
|
'markdown' => '# a title',
|
2021-06-26 11:23:15 -04:00
|
|
|
'html' => '<h1>a title</h1>',
|
|
|
|
'name' => 'my page',
|
2021-01-04 12:52:08 -05:00
|
|
|
];
|
|
|
|
$resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
|
|
|
|
$resp->assertRedirect();
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('pages', [
|
|
|
|
'markdown' => $details['markdown'],
|
2021-06-26 11:23:15 -04:00
|
|
|
'name' => $details['name'],
|
|
|
|
'id' => $draft->id,
|
|
|
|
'draft' => false,
|
2021-01-04 12:52:08 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
$draft->refresh();
|
2021-06-26 11:23:15 -04:00
|
|
|
$resp = $this->get($draft->getUrl('/edit'));
|
|
|
|
$resp->assertSee('# a title');
|
2021-01-04 12:52:08 -05:00
|
|
|
}
|
|
|
|
|
2024-06-09 12:18:23 -04:00
|
|
|
public function test_page_creation_allows_summary_to_be_set()
|
|
|
|
{
|
|
|
|
$book = $this->entities->book();
|
|
|
|
|
|
|
|
$this->asEditor()->get($book->getUrl('/create-page'));
|
|
|
|
$draft = Page::query()->where('book_id', '=', $book->id)
|
|
|
|
->where('draft', '=', true)->first();
|
|
|
|
|
|
|
|
$details = [
|
|
|
|
'html' => '<h1>a title</h1>',
|
|
|
|
'name' => 'My page with summary',
|
|
|
|
'summary' => 'Here is my changelog message for a new page!',
|
|
|
|
];
|
|
|
|
$resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
|
|
|
|
$resp->assertRedirect();
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('page_revisions', [
|
|
|
|
'page_id' => $draft->id,
|
|
|
|
'summary' => 'Here is my changelog message for a new page!',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$draft->refresh();
|
|
|
|
$resp = $this->get($draft->getUrl('/revisions'));
|
|
|
|
$resp->assertSee('Here is my changelog message for a new page!');
|
|
|
|
}
|
|
|
|
|
2020-11-06 07:54:39 -05:00
|
|
|
public function test_page_delete()
|
|
|
|
{
|
2022-09-29 12:31:38 -04:00
|
|
|
$page = $this->entities->page();
|
2020-11-06 07:54:39 -05:00
|
|
|
$this->assertNull($page->deleted_at);
|
|
|
|
|
|
|
|
$deleteViewReq = $this->asEditor()->get($page->getUrl('/delete'));
|
|
|
|
$deleteViewReq->assertSeeText('Are you sure you want to delete this page?');
|
|
|
|
|
|
|
|
$deleteReq = $this->delete($page->getUrl());
|
|
|
|
$deleteReq->assertRedirect($page->getParent()->getUrl());
|
|
|
|
$this->assertActivityExists('page_delete', $page);
|
|
|
|
|
|
|
|
$page->refresh();
|
|
|
|
$this->assertNotNull($page->deleted_at);
|
|
|
|
$this->assertTrue($page->deletions()->count() === 1);
|
|
|
|
|
|
|
|
$redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->assertNotificationContains($redirectReq, 'Page Successfully Deleted');
|
2020-11-06 07:54:39 -05:00
|
|
|
}
|
2021-01-09 14:39:09 -05:00
|
|
|
|
2021-05-26 11:40:56 -04:00
|
|
|
public function test_page_full_delete_removes_all_revisions()
|
|
|
|
{
|
2022-09-29 12:31:38 -04:00
|
|
|
$page = $this->entities->page();
|
2021-05-26 11:40:56 -04:00
|
|
|
$page->revisions()->create([
|
|
|
|
'html' => '<p>ducks</p>',
|
|
|
|
'name' => 'my page revision',
|
|
|
|
'type' => 'draft',
|
|
|
|
]);
|
|
|
|
$page->revisions()->create([
|
|
|
|
'html' => '<p>ducks</p>',
|
|
|
|
'name' => 'my page revision',
|
|
|
|
'type' => 'revision',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('page_revisions', [
|
|
|
|
'page_id' => $page->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->asEditor()->delete($page->getUrl());
|
|
|
|
$this->asAdmin()->post('/settings/recycle-bin/empty');
|
|
|
|
|
|
|
|
$this->assertDatabaseMissing('page_revisions', [
|
|
|
|
'page_id' => $page->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-01-09 14:39:09 -05:00
|
|
|
public function test_page_copy()
|
|
|
|
{
|
2022-09-29 17:11:16 -04:00
|
|
|
$page = $this->entities->page();
|
2021-01-09 14:39:09 -05:00
|
|
|
$page->html = '<p>This is some test content</p>';
|
|
|
|
$page->save();
|
|
|
|
|
|
|
|
$currentBook = $page->book;
|
|
|
|
$newBook = Book::where('id', '!=', $currentBook->id)->first();
|
|
|
|
|
|
|
|
$resp = $this->asEditor()->get($page->getUrl('/copy'));
|
|
|
|
$resp->assertSee('Copy Page');
|
|
|
|
|
|
|
|
$movePageResp = $this->post($page->getUrl('/copy'), [
|
|
|
|
'entity_selection' => 'book:' . $newBook->id,
|
2021-06-26 11:23:15 -04:00
|
|
|
'name' => 'My copied test page',
|
2021-01-09 14:39:09 -05:00
|
|
|
]);
|
|
|
|
$pageCopy = Page::where('name', '=', 'My copied test page')->first();
|
|
|
|
|
|
|
|
$movePageResp->assertRedirect($pageCopy->getUrl());
|
|
|
|
$this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
|
|
|
|
$this->assertStringContainsString('This is some test content', $pageCopy->html);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_page_copy_with_markdown_has_both_html_and_markdown()
|
|
|
|
{
|
2022-09-29 17:11:16 -04:00
|
|
|
$page = $this->entities->page();
|
2021-01-09 14:39:09 -05:00
|
|
|
$page->html = '<h1>This is some test content</h1>';
|
|
|
|
$page->markdown = '# This is some test content';
|
|
|
|
$page->save();
|
|
|
|
$newBook = Book::where('id', '!=', $page->book->id)->first();
|
|
|
|
|
|
|
|
$this->asEditor()->post($page->getUrl('/copy'), [
|
|
|
|
'entity_selection' => 'book:' . $newBook->id,
|
2021-06-26 11:23:15 -04:00
|
|
|
'name' => 'My copied test page',
|
2021-01-09 14:39:09 -05:00
|
|
|
]);
|
|
|
|
$pageCopy = Page::where('name', '=', 'My copied test page')->first();
|
|
|
|
|
|
|
|
$this->assertStringContainsString('This is some test content', $pageCopy->html);
|
|
|
|
$this->assertEquals('# This is some test content', $pageCopy->markdown);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_page_copy_with_no_destination()
|
|
|
|
{
|
2022-09-29 17:11:16 -04:00
|
|
|
$page = $this->entities->page();
|
2021-01-09 14:39:09 -05:00
|
|
|
$currentBook = $page->book;
|
|
|
|
|
|
|
|
$resp = $this->asEditor()->get($page->getUrl('/copy'));
|
|
|
|
$resp->assertSee('Copy Page');
|
|
|
|
|
|
|
|
$movePageResp = $this->post($page->getUrl('/copy'), [
|
2021-06-26 11:23:15 -04:00
|
|
|
'name' => 'My copied test page',
|
2021-01-09 14:39:09 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
$pageCopy = Page::where('name', '=', 'My copied test page')->first();
|
|
|
|
|
|
|
|
$movePageResp->assertRedirect($pageCopy->getUrl());
|
|
|
|
$this->assertTrue($pageCopy->book->id == $currentBook->id, 'Page was copied to correct book');
|
|
|
|
$this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_page_can_be_copied_without_edit_permission()
|
|
|
|
{
|
2022-09-29 17:11:16 -04:00
|
|
|
$page = $this->entities->page();
|
2021-01-09 14:39:09 -05:00
|
|
|
$currentBook = $page->book;
|
|
|
|
$newBook = Book::where('id', '!=', $currentBook->id)->first();
|
2023-01-21 06:08:34 -05:00
|
|
|
$viewer = $this->users->viewer();
|
2021-01-09 14:39:09 -05:00
|
|
|
|
|
|
|
$resp = $this->actingAs($viewer)->get($page->getUrl());
|
|
|
|
$resp->assertDontSee($page->getUrl('/copy'));
|
|
|
|
|
|
|
|
$newBook->owned_by = $viewer->id;
|
|
|
|
$newBook->save();
|
2023-01-21 06:08:34 -05:00
|
|
|
$this->permissions->grantUserRolePermissions($viewer, ['page-create-own']);
|
|
|
|
$this->permissions->regenerateForEntity($newBook);
|
2021-01-09 14:39:09 -05:00
|
|
|
|
|
|
|
$resp = $this->actingAs($viewer)->get($page->getUrl());
|
|
|
|
$resp->assertSee($page->getUrl('/copy'));
|
|
|
|
|
|
|
|
$movePageResp = $this->post($page->getUrl('/copy'), [
|
|
|
|
'entity_selection' => 'book:' . $newBook->id,
|
2021-06-26 11:23:15 -04:00
|
|
|
'name' => 'My copied test page',
|
2021-01-09 14:39:09 -05:00
|
|
|
]);
|
|
|
|
$movePageResp->assertRedirect();
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('pages', [
|
2021-06-26 11:23:15 -04:00
|
|
|
'name' => 'My copied test page',
|
2021-01-09 14:39:09 -05:00
|
|
|
'created_by' => $viewer->id,
|
2021-06-26 11:23:15 -04:00
|
|
|
'book_id' => $newBook->id,
|
2021-01-09 14:39:09 -05:00
|
|
|
]);
|
|
|
|
}
|
2021-05-15 12:33:53 -04:00
|
|
|
|
2021-09-15 17:18:37 -04:00
|
|
|
public function test_old_page_slugs_redirect_to_new_pages()
|
2021-05-15 12:33:53 -04:00
|
|
|
{
|
2022-09-29 12:31:38 -04:00
|
|
|
$page = $this->entities->page();
|
2021-05-15 12:33:53 -04:00
|
|
|
|
2021-09-15 17:18:37 -04:00
|
|
|
// Need to save twice since revisions are not generated in seeder.
|
|
|
|
$this->asAdmin()->put($page->getUrl(), [
|
|
|
|
'name' => 'super test',
|
2021-09-18 16:21:44 -04:00
|
|
|
'html' => '<p></p>',
|
2021-09-15 17:18:37 -04:00
|
|
|
]);
|
2021-05-15 12:33:53 -04:00
|
|
|
|
2021-09-15 17:18:37 -04:00
|
|
|
$page->refresh();
|
|
|
|
$pageUrl = $page->getUrl();
|
2021-05-15 12:33:53 -04:00
|
|
|
|
2021-09-15 17:18:37 -04:00
|
|
|
$this->put($pageUrl, [
|
|
|
|
'name' => 'super test page',
|
2021-09-18 16:21:44 -04:00
|
|
|
'html' => '<p></p>',
|
2021-05-15 12:33:53 -04:00
|
|
|
]);
|
2021-09-15 17:18:37 -04:00
|
|
|
|
|
|
|
$this->get($pageUrl)
|
|
|
|
->assertRedirect("/books/{$page->book->slug}/page/super-test-page");
|
2021-05-15 12:33:53 -04:00
|
|
|
}
|
2021-09-15 17:18:37 -04:00
|
|
|
|
|
|
|
public function test_page_within_chapter_deletion_returns_to_chapter()
|
|
|
|
{
|
2022-09-29 12:31:38 -04:00
|
|
|
$chapter = $this->entities->chapter();
|
2021-09-15 17:18:37 -04:00
|
|
|
$page = $chapter->pages()->first();
|
|
|
|
|
|
|
|
$this->asEditor()->delete($page->getUrl())
|
|
|
|
->assertRedirect($chapter->getUrl());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_recently_updated_pages_view()
|
|
|
|
{
|
2023-01-21 06:08:34 -05:00
|
|
|
$user = $this->users->editor();
|
2022-09-29 11:49:25 -04:00
|
|
|
$content = $this->entities->createChainBelongingToUser($user);
|
2021-09-15 17:18:37 -04:00
|
|
|
|
2022-07-23 10:10:18 -04:00
|
|
|
$resp = $this->asAdmin()->get('/pages/recently-updated');
|
|
|
|
$this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', $content['page']->name);
|
2021-09-15 17:18:37 -04:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:23:16 -05:00
|
|
|
public function test_recently_updated_pages_view_shows_updated_by_details()
|
|
|
|
{
|
2023-01-21 06:08:34 -05:00
|
|
|
$user = $this->users->editor();
|
2022-09-29 12:31:38 -04:00
|
|
|
$page = $this->entities->page();
|
2022-01-24 13:23:16 -05:00
|
|
|
|
|
|
|
$this->actingAs($user)->put($page->getUrl(), [
|
|
|
|
'name' => 'Updated title',
|
|
|
|
'html' => '<p>Updated content</p>',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$resp = $this->asAdmin()->get('/pages/recently-updated');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', 'Updated 1 second ago by ' . $user->name);
|
2022-01-24 13:23:16 -05:00
|
|
|
}
|
|
|
|
|
2022-01-24 16:21:30 -05:00
|
|
|
public function test_recently_updated_pages_view_shows_parent_chain()
|
|
|
|
{
|
2023-01-21 06:08:34 -05:00
|
|
|
$user = $this->users->editor();
|
2022-09-29 17:11:16 -04:00
|
|
|
$page = $this->entities->pageWithinChapter();
|
2022-01-24 16:21:30 -05:00
|
|
|
|
|
|
|
$this->actingAs($user)->put($page->getUrl(), [
|
|
|
|
'name' => 'Updated title',
|
|
|
|
'html' => '<p>Updated content</p>',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$resp = $this->asAdmin()->get('/pages/recently-updated');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', $page->chapter->getShortName(42));
|
|
|
|
$this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', $page->book->getShortName(42));
|
2022-01-24 16:21:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_recently_updated_pages_view_does_not_show_parent_if_not_visible()
|
|
|
|
{
|
2023-01-21 06:08:34 -05:00
|
|
|
$user = $this->users->editor();
|
2022-09-29 17:11:16 -04:00
|
|
|
$page = $this->entities->pageWithinChapter();
|
2022-01-24 16:21:30 -05:00
|
|
|
|
|
|
|
$this->actingAs($user)->put($page->getUrl(), [
|
|
|
|
'name' => 'Updated title',
|
|
|
|
'html' => '<p>Updated content</p>',
|
|
|
|
]);
|
|
|
|
|
2023-01-21 06:08:34 -05:00
|
|
|
$this->permissions->setEntityPermissions($page->book);
|
|
|
|
$this->permissions->setEntityPermissions($page, ['view'], [$user->roles->first()]);
|
2022-01-24 16:21:30 -05:00
|
|
|
|
|
|
|
$resp = $this->get('/pages/recently-updated');
|
|
|
|
$resp->assertDontSee($page->book->getShortName(42));
|
|
|
|
$resp->assertDontSee($page->chapter->getShortName(42));
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', 'Updated title');
|
2022-01-24 16:21:30 -05:00
|
|
|
}
|
|
|
|
|
2021-09-15 17:18:37 -04:00
|
|
|
public function test_recently_updated_pages_on_home()
|
|
|
|
{
|
|
|
|
/** @var Page $page */
|
|
|
|
$page = Page::query()->orderBy('updated_at', 'asc')->first();
|
|
|
|
Page::query()->where('id', '!=', $page->id)->update([
|
|
|
|
'updated_at' => Carbon::now()->subSecond(1),
|
|
|
|
]);
|
|
|
|
|
2022-07-23 10:10:18 -04:00
|
|
|
$resp = $this->asAdmin()->get('/');
|
|
|
|
$this->withHtml($resp)->assertElementNotContains('#recently-updated-pages', $page->name);
|
2021-09-15 17:18:37 -04:00
|
|
|
|
|
|
|
$this->put($page->getUrl(), [
|
|
|
|
'name' => $page->name,
|
|
|
|
'html' => $page->html,
|
|
|
|
]);
|
|
|
|
|
2022-07-23 10:10:18 -04:00
|
|
|
$resp = $this->get('/');
|
|
|
|
$this->withHtml($resp)->assertElementContains('#recently-updated-pages', $page->name);
|
2021-09-15 17:18:37 -04:00
|
|
|
}
|
2021-06-26 11:23:15 -04:00
|
|
|
}
|