1 <?php namespace Tests\Entity;
3 use BookStack\Entities\Models\Book;
4 use BookStack\Entities\Models\Page;
7 class PageTest extends TestCase
9 public function test_page_creation_with_markdown_content()
11 $this->setSettings(['app-editor' => 'markdown']);
12 $book = Book::query()->first();
14 $this->asEditor()->get($book->getUrl('/create-page'));
15 $draft = Page::query()->where('book_id', '=', $book->id)
16 ->where('draft', '=', true)->first();
19 'markdown' => '# a title',
20 'html' => '<h1>a title</h1>',
23 $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
24 $resp->assertRedirect();
26 $this->assertDatabaseHas('pages', [
27 'markdown' => $details['markdown'],
28 'name' => $details['name'],
34 $resp = $this->get($draft->getUrl("/edit"));
35 $resp->assertSee("# a title");
38 public function test_page_delete()
40 $page = Page::query()->first();
41 $this->assertNull($page->deleted_at);
43 $deleteViewReq = $this->asEditor()->get($page->getUrl('/delete'));
44 $deleteViewReq->assertSeeText('Are you sure you want to delete this page?');
46 $deleteReq = $this->delete($page->getUrl());
47 $deleteReq->assertRedirect($page->getParent()->getUrl());
48 $this->assertActivityExists('page_delete', $page);
51 $this->assertNotNull($page->deleted_at);
52 $this->assertTrue($page->deletions()->count() === 1);
54 $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
55 $redirectReq->assertNotificationContains('Page Successfully Deleted');
58 public function test_page_copy()
60 $page = Page::first();
61 $page->html = '<p>This is some test content</p>';
64 $currentBook = $page->book;
65 $newBook = Book::where('id', '!=', $currentBook->id)->first();
67 $resp = $this->asEditor()->get($page->getUrl('/copy'));
68 $resp->assertSee('Copy Page');
70 $movePageResp = $this->post($page->getUrl('/copy'), [
71 'entity_selection' => 'book:' . $newBook->id,
72 'name' => 'My copied test page'
74 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
76 $movePageResp->assertRedirect($pageCopy->getUrl());
77 $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
78 $this->assertStringContainsString('This is some test content', $pageCopy->html);
81 public function test_page_copy_with_markdown_has_both_html_and_markdown()
83 $page = Page::first();
84 $page->html = '<h1>This is some test content</h1>';
85 $page->markdown = '# This is some test content';
87 $newBook = Book::where('id', '!=', $page->book->id)->first();
89 $this->asEditor()->post($page->getUrl('/copy'), [
90 'entity_selection' => 'book:' . $newBook->id,
91 'name' => 'My copied test page'
93 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
95 $this->assertStringContainsString('This is some test content', $pageCopy->html);
96 $this->assertEquals('# This is some test content', $pageCopy->markdown);
99 public function test_page_copy_with_no_destination()
101 $page = Page::first();
102 $currentBook = $page->book;
104 $resp = $this->asEditor()->get($page->getUrl('/copy'));
105 $resp->assertSee('Copy Page');
107 $movePageResp = $this->post($page->getUrl('/copy'), [
108 'name' => 'My copied test page'
111 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
113 $movePageResp->assertRedirect($pageCopy->getUrl());
114 $this->assertTrue($pageCopy->book->id == $currentBook->id, 'Page was copied to correct book');
115 $this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');
118 public function test_page_can_be_copied_without_edit_permission()
120 $page = Page::first();
121 $currentBook = $page->book;
122 $newBook = Book::where('id', '!=', $currentBook->id)->first();
123 $viewer = $this->getViewer();
125 $resp = $this->actingAs($viewer)->get($page->getUrl());
126 $resp->assertDontSee($page->getUrl('/copy'));
128 $newBook->owned_by = $viewer->id;
130 $this->giveUserPermissions($viewer, ['page-create-own']);
131 $this->regenEntityPermissions($newBook);
133 $resp = $this->actingAs($viewer)->get($page->getUrl());
134 $resp->assertSee($page->getUrl('/copy'));
136 $movePageResp = $this->post($page->getUrl('/copy'), [
137 'entity_selection' => 'book:' . $newBook->id,
138 'name' => 'My copied test page'
140 $movePageResp->assertRedirect();
142 $this->assertDatabaseHas('pages', [
143 'name' => 'My copied test page',
144 'created_by' => $viewer->id,
145 'book_id' => $newBook->id,