1 <?php namespace Tests\Entity;
3 use BookStack\Entities\Models\Book;
4 use BookStack\Entities\Models\Page;
7 class PageTest extends TestCase
10 public function test_page_view_when_creator_is_deleted_but_owner_exists()
12 $page = Page::query()->first();
13 $user = $this->getViewer();
14 $owner = $this->getEditor();
15 $page->created_by = $user->id;
16 $page->owned_by = $owner->id;
20 $resp = $this->asAdmin()->get($page->getUrl());
21 $resp->assertStatus(200);
22 $resp->assertSeeText('Owned by ' . $owner->name);
25 public function test_page_creation_with_markdown_content()
27 $this->setSettings(['app-editor' => 'markdown']);
28 $book = Book::query()->first();
30 $this->asEditor()->get($book->getUrl('/create-page'));
31 $draft = Page::query()->where('book_id', '=', $book->id)
32 ->where('draft', '=', true)->first();
35 'markdown' => '# a title',
36 'html' => '<h1>a title</h1>',
39 $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
40 $resp->assertRedirect();
42 $this->assertDatabaseHas('pages', [
43 'markdown' => $details['markdown'],
44 'name' => $details['name'],
50 $resp = $this->get($draft->getUrl("/edit"));
51 $resp->assertSee("# a title");
54 public function test_page_delete()
56 $page = Page::query()->first();
57 $this->assertNull($page->deleted_at);
59 $deleteViewReq = $this->asEditor()->get($page->getUrl('/delete'));
60 $deleteViewReq->assertSeeText('Are you sure you want to delete this page?');
62 $deleteReq = $this->delete($page->getUrl());
63 $deleteReq->assertRedirect($page->getParent()->getUrl());
64 $this->assertActivityExists('page_delete', $page);
67 $this->assertNotNull($page->deleted_at);
68 $this->assertTrue($page->deletions()->count() === 1);
70 $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
71 $redirectReq->assertNotificationContains('Page Successfully Deleted');
74 public function test_page_copy()
76 $page = Page::first();
77 $page->html = '<p>This is some test content</p>';
80 $currentBook = $page->book;
81 $newBook = Book::where('id', '!=', $currentBook->id)->first();
83 $resp = $this->asEditor()->get($page->getUrl('/copy'));
84 $resp->assertSee('Copy Page');
86 $movePageResp = $this->post($page->getUrl('/copy'), [
87 'entity_selection' => 'book:' . $newBook->id,
88 'name' => 'My copied test page'
90 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
92 $movePageResp->assertRedirect($pageCopy->getUrl());
93 $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
94 $this->assertStringContainsString('This is some test content', $pageCopy->html);
97 public function test_page_copy_with_markdown_has_both_html_and_markdown()
99 $page = Page::first();
100 $page->html = '<h1>This is some test content</h1>';
101 $page->markdown = '# This is some test content';
103 $newBook = Book::where('id', '!=', $page->book->id)->first();
105 $this->asEditor()->post($page->getUrl('/copy'), [
106 'entity_selection' => 'book:' . $newBook->id,
107 'name' => 'My copied test page'
109 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
111 $this->assertStringContainsString('This is some test content', $pageCopy->html);
112 $this->assertEquals('# This is some test content', $pageCopy->markdown);
115 public function test_page_copy_with_no_destination()
117 $page = Page::first();
118 $currentBook = $page->book;
120 $resp = $this->asEditor()->get($page->getUrl('/copy'));
121 $resp->assertSee('Copy Page');
123 $movePageResp = $this->post($page->getUrl('/copy'), [
124 'name' => 'My copied test page'
127 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
129 $movePageResp->assertRedirect($pageCopy->getUrl());
130 $this->assertTrue($pageCopy->book->id == $currentBook->id, 'Page was copied to correct book');
131 $this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');
134 public function test_page_can_be_copied_without_edit_permission()
136 $page = Page::first();
137 $currentBook = $page->book;
138 $newBook = Book::where('id', '!=', $currentBook->id)->first();
139 $viewer = $this->getViewer();
141 $resp = $this->actingAs($viewer)->get($page->getUrl());
142 $resp->assertDontSee($page->getUrl('/copy'));
144 $newBook->owned_by = $viewer->id;
146 $this->giveUserPermissions($viewer, ['page-create-own']);
147 $this->regenEntityPermissions($newBook);
149 $resp = $this->actingAs($viewer)->get($page->getUrl());
150 $resp->assertSee($page->getUrl('/copy'));
152 $movePageResp = $this->post($page->getUrl('/copy'), [
153 'entity_selection' => 'book:' . $newBook->id,
154 'name' => 'My copied test page'
156 $movePageResp->assertRedirect();
158 $this->assertDatabaseHas('pages', [
159 'name' => 'My copied test page',
160 'created_by' => $viewer->id,
161 'book_id' => $newBook->id,