]> BookStack Code Mirror - bookstack/blob - tests/Entity/PageTest.php
fix image delete confirm text
[bookstack] / tests / Entity / PageTest.php
1 <?php namespace Tests\Entity;
2
3 use BookStack\Entities\Models\Book;
4 use BookStack\Entities\Models\Page;
5 use Tests\TestCase;
6
7 class PageTest extends TestCase
8 {
9     public function test_page_creation_with_markdown_content()
10     {
11         $this->setSettings(['app-editor' => 'markdown']);
12         $book = Book::query()->first();
13
14         $this->asEditor()->get($book->getUrl('/create-page'));
15         $draft = Page::query()->where('book_id', '=', $book->id)
16             ->where('draft', '=', true)->first();
17
18         $details = [
19             'markdown' => '# a title',
20             'html' => '<h1>a title</h1>',
21             'name' => 'my page',
22         ];
23         $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
24         $resp->assertRedirect();
25
26         $this->assertDatabaseHas('pages', [
27             'markdown' => $details['markdown'],
28             'name' => $details['name'],
29             'id' => $draft->id,
30             'draft' => false
31         ]);
32
33         $draft->refresh();
34         $resp = $this->get($draft->getUrl("/edit"));
35         $resp->assertSee("# a title");
36     }
37
38     public function test_page_delete()
39     {
40         $page = Page::query()->first();
41         $this->assertNull($page->deleted_at);
42
43         $deleteViewReq = $this->asEditor()->get($page->getUrl('/delete'));
44         $deleteViewReq->assertSeeText('Are you sure you want to delete this page?');
45
46         $deleteReq = $this->delete($page->getUrl());
47         $deleteReq->assertRedirect($page->getParent()->getUrl());
48         $this->assertActivityExists('page_delete', $page);
49
50         $page->refresh();
51         $this->assertNotNull($page->deleted_at);
52         $this->assertTrue($page->deletions()->count() === 1);
53
54         $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
55         $redirectReq->assertNotificationContains('Page Successfully Deleted');
56     }
57
58     public function test_page_copy()
59     {
60         $page = Page::first();
61         $page->html = '<p>This is some test content</p>';
62         $page->save();
63
64         $currentBook = $page->book;
65         $newBook = Book::where('id', '!=', $currentBook->id)->first();
66
67         $resp = $this->asEditor()->get($page->getUrl('/copy'));
68         $resp->assertSee('Copy Page');
69
70         $movePageResp = $this->post($page->getUrl('/copy'), [
71             'entity_selection' => 'book:' . $newBook->id,
72             'name' => 'My copied test page'
73         ]);
74         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
75
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);
79     }
80
81     public function test_page_copy_with_markdown_has_both_html_and_markdown()
82     {
83         $page = Page::first();
84         $page->html = '<h1>This is some test content</h1>';
85         $page->markdown = '# This is some test content';
86         $page->save();
87         $newBook = Book::where('id', '!=', $page->book->id)->first();
88
89         $this->asEditor()->post($page->getUrl('/copy'), [
90             'entity_selection' => 'book:' . $newBook->id,
91             'name' => 'My copied test page'
92         ]);
93         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
94
95         $this->assertStringContainsString('This is some test content', $pageCopy->html);
96         $this->assertEquals('# This is some test content', $pageCopy->markdown);
97     }
98
99     public function test_page_copy_with_no_destination()
100     {
101         $page = Page::first();
102         $currentBook = $page->book;
103
104         $resp = $this->asEditor()->get($page->getUrl('/copy'));
105         $resp->assertSee('Copy Page');
106
107         $movePageResp = $this->post($page->getUrl('/copy'), [
108             'name' => 'My copied test page'
109         ]);
110
111         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
112
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');
116     }
117
118     public function test_page_can_be_copied_without_edit_permission()
119     {
120         $page = Page::first();
121         $currentBook = $page->book;
122         $newBook = Book::where('id', '!=', $currentBook->id)->first();
123         $viewer = $this->getViewer();
124
125         $resp = $this->actingAs($viewer)->get($page->getUrl());
126         $resp->assertDontSee($page->getUrl('/copy'));
127
128         $newBook->owned_by = $viewer->id;
129         $newBook->save();
130         $this->giveUserPermissions($viewer, ['page-create-own']);
131         $this->regenEntityPermissions($newBook);
132
133         $resp = $this->actingAs($viewer)->get($page->getUrl());
134         $resp->assertSee($page->getUrl('/copy'));
135
136         $movePageResp = $this->post($page->getUrl('/copy'), [
137             'entity_selection' => 'book:' . $newBook->id,
138             'name' => 'My copied test page'
139         ]);
140         $movePageResp->assertRedirect();
141
142         $this->assertDatabaseHas('pages', [
143             'name' => 'My copied test page',
144             'created_by' => $viewer->id,
145             'book_id' => $newBook->id,
146         ]);
147     }
148 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.