]> BookStack Code Mirror - bookstack/blob - tests/Entity/PageTest.php
Added testing to favourite system
[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
10     public function test_page_view_when_creator_is_deleted_but_owner_exists()
11     {
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;
17         $page->save();
18         $user->delete();
19
20         $resp = $this->asAdmin()->get($page->getUrl());
21         $resp->assertStatus(200);
22         $resp->assertSeeText('Owned by ' . $owner->name);
23     }
24
25     public function test_page_creation_with_markdown_content()
26     {
27         $this->setSettings(['app-editor' => 'markdown']);
28         $book = Book::query()->first();
29
30         $this->asEditor()->get($book->getUrl('/create-page'));
31         $draft = Page::query()->where('book_id', '=', $book->id)
32             ->where('draft', '=', true)->first();
33
34         $details = [
35             'markdown' => '# a title',
36             'html' => '<h1>a title</h1>',
37             'name' => 'my page',
38         ];
39         $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
40         $resp->assertRedirect();
41
42         $this->assertDatabaseHas('pages', [
43             'markdown' => $details['markdown'],
44             'name' => $details['name'],
45             'id' => $draft->id,
46             'draft' => false
47         ]);
48
49         $draft->refresh();
50         $resp = $this->get($draft->getUrl("/edit"));
51         $resp->assertSee("# a title");
52     }
53
54     public function test_page_delete()
55     {
56         $page = Page::query()->first();
57         $this->assertNull($page->deleted_at);
58
59         $deleteViewReq = $this->asEditor()->get($page->getUrl('/delete'));
60         $deleteViewReq->assertSeeText('Are you sure you want to delete this page?');
61
62         $deleteReq = $this->delete($page->getUrl());
63         $deleteReq->assertRedirect($page->getParent()->getUrl());
64         $this->assertActivityExists('page_delete', $page);
65
66         $page->refresh();
67         $this->assertNotNull($page->deleted_at);
68         $this->assertTrue($page->deletions()->count() === 1);
69
70         $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
71         $redirectReq->assertNotificationContains('Page Successfully Deleted');
72     }
73
74     public function test_page_copy()
75     {
76         $page = Page::first();
77         $page->html = '<p>This is some test content</p>';
78         $page->save();
79
80         $currentBook = $page->book;
81         $newBook = Book::where('id', '!=', $currentBook->id)->first();
82
83         $resp = $this->asEditor()->get($page->getUrl('/copy'));
84         $resp->assertSee('Copy Page');
85
86         $movePageResp = $this->post($page->getUrl('/copy'), [
87             'entity_selection' => 'book:' . $newBook->id,
88             'name' => 'My copied test page'
89         ]);
90         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
91
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);
95     }
96
97     public function test_page_copy_with_markdown_has_both_html_and_markdown()
98     {
99         $page = Page::first();
100         $page->html = '<h1>This is some test content</h1>';
101         $page->markdown = '# This is some test content';
102         $page->save();
103         $newBook = Book::where('id', '!=', $page->book->id)->first();
104
105         $this->asEditor()->post($page->getUrl('/copy'), [
106             'entity_selection' => 'book:' . $newBook->id,
107             'name' => 'My copied test page'
108         ]);
109         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
110
111         $this->assertStringContainsString('This is some test content', $pageCopy->html);
112         $this->assertEquals('# This is some test content', $pageCopy->markdown);
113     }
114
115     public function test_page_copy_with_no_destination()
116     {
117         $page = Page::first();
118         $currentBook = $page->book;
119
120         $resp = $this->asEditor()->get($page->getUrl('/copy'));
121         $resp->assertSee('Copy Page');
122
123         $movePageResp = $this->post($page->getUrl('/copy'), [
124             'name' => 'My copied test page'
125         ]);
126
127         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
128
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');
132     }
133
134     public function test_page_can_be_copied_without_edit_permission()
135     {
136         $page = Page::first();
137         $currentBook = $page->book;
138         $newBook = Book::where('id', '!=', $currentBook->id)->first();
139         $viewer = $this->getViewer();
140
141         $resp = $this->actingAs($viewer)->get($page->getUrl());
142         $resp->assertDontSee($page->getUrl('/copy'));
143
144         $newBook->owned_by = $viewer->id;
145         $newBook->save();
146         $this->giveUserPermissions($viewer, ['page-create-own']);
147         $this->regenEntityPermissions($newBook);
148
149         $resp = $this->actingAs($viewer)->get($page->getUrl());
150         $resp->assertSee($page->getUrl('/copy'));
151
152         $movePageResp = $this->post($page->getUrl('/copy'), [
153             'entity_selection' => 'book:' . $newBook->id,
154             'name' => 'My copied test page'
155         ]);
156         $movePageResp->assertRedirect();
157
158         $this->assertDatabaseHas('pages', [
159             'name' => 'My copied test page',
160             'created_by' => $viewer->id,
161             'book_id' => $newBook->id,
162         ]);
163     }
164
165     public function test_empty_markdown_still_saves_without_error()
166     {
167         $this->setSettings(['app-editor' => 'markdown']);
168         $book = Book::query()->first();
169
170         $this->asEditor()->get($book->getUrl('/create-page'));
171         $draft = Page::query()->where('book_id', '=', $book->id)
172             ->where('draft', '=', true)->first();
173
174         $details = [
175             'name' => 'my page',
176             'markdown' => '',
177         ];
178         $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
179         $resp->assertRedirect();
180
181         $this->assertDatabaseHas('pages', [
182             'markdown' => $details['markdown'],
183             'id' => $draft->id,
184             'draft' => false
185         ]);
186     }
187 }