X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/eb76e882c5544e319e5084fedcc7cdac0bdf6c2d..refs/pull/3593/head:/tests/Entity/PageTest.php diff --git a/tests/Entity/PageTest.php b/tests/Entity/PageTest.php index a6f6f9d50..734516e87 100644 --- a/tests/Entity/PageTest.php +++ b/tests/Entity/PageTest.php @@ -1,11 +1,41 @@ -first(); + $page = Page::factory()->make([ + 'name' => 'My First Page', + ]); + + $resp = $this->asEditor()->get($chapter->getUrl()); + $this->withHtml($resp)->assertElementContains('a[href="' . $chapter->getUrl('/create-page') . '"]', 'New Page'); + + $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()); + $this->withHtml($resp)->assertElementContains('form[action="' . $draftPage->getUrl() . '"][method="POST"]', 'Save Page'); + + $resp = $this->post($draftPage->getUrl(), $draftPage->only('name', 'html')); + $draftPage->refresh(); + $resp->assertRedirect($draftPage->getUrl()); + } public function test_page_view_when_creator_is_deleted_but_owner_exists() { @@ -33,22 +63,22 @@ class PageTest extends TestCase $details = [ 'markdown' => '# a title', - 'html' => '

a title

', - 'name' => 'my page', + 'html' => '

a title

', + 'name' => 'my page', ]; $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details); $resp->assertRedirect(); $this->assertDatabaseHas('pages', [ 'markdown' => $details['markdown'], - 'name' => $details['name'], - 'id' => $draft->id, - 'draft' => false + 'name' => $details['name'], + 'id' => $draft->id, + 'draft' => false, ]); $draft->refresh(); - $resp = $this->get($draft->getUrl("/edit")); - $resp->assertSee("# a title"); + $resp = $this->get($draft->getUrl('/edit')); + $resp->assertSee('# a title'); } public function test_page_delete() @@ -68,7 +98,7 @@ class PageTest extends TestCase $this->assertTrue($page->deletions()->count() === 1); $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location')); - $redirectReq->assertNotificationContains('Page Successfully Deleted'); + $this->assertNotificationContains($redirectReq, 'Page Successfully Deleted'); } public function test_page_full_delete_removes_all_revisions() @@ -112,7 +142,7 @@ class PageTest extends TestCase $movePageResp = $this->post($page->getUrl('/copy'), [ 'entity_selection' => 'book:' . $newBook->id, - 'name' => 'My copied test page' + 'name' => 'My copied test page', ]); $pageCopy = Page::where('name', '=', 'My copied test page')->first(); @@ -131,7 +161,7 @@ class PageTest extends TestCase $this->asEditor()->post($page->getUrl('/copy'), [ 'entity_selection' => 'book:' . $newBook->id, - 'name' => 'My copied test page' + 'name' => 'My copied test page', ]); $pageCopy = Page::where('name', '=', 'My copied test page')->first(); @@ -148,7 +178,7 @@ class PageTest extends TestCase $resp->assertSee('Copy Page'); $movePageResp = $this->post($page->getUrl('/copy'), [ - 'name' => 'My copied test page' + 'name' => 'My copied test page', ]); $pageCopy = Page::where('name', '=', 'My copied test page')->first(); @@ -178,37 +208,127 @@ class PageTest extends TestCase $movePageResp = $this->post($page->getUrl('/copy'), [ 'entity_selection' => 'book:' . $newBook->id, - 'name' => 'My copied test page' + 'name' => 'My copied test page', ]); $movePageResp->assertRedirect(); $this->assertDatabaseHas('pages', [ - 'name' => 'My copied test page', + 'name' => 'My copied test page', 'created_by' => $viewer->id, - 'book_id' => $newBook->id, + 'book_id' => $newBook->id, ]); } - public function test_empty_markdown_still_saves_without_error() + public function test_old_page_slugs_redirect_to_new_pages() { - $this->setSettings(['app-editor' => 'markdown']); - $book = Book::query()->first(); + /** @var Page $page */ + $page = Page::query()->first(); - $this->asEditor()->get($book->getUrl('/create-page')); - $draft = Page::query()->where('book_id', '=', $book->id) - ->where('draft', '=', true)->first(); + // Need to save twice since revisions are not generated in seeder. + $this->asAdmin()->put($page->getUrl(), [ + 'name' => 'super test', + 'html' => '

', + ]); - $details = [ - 'name' => 'my page', - 'markdown' => '', - ]; - $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details); - $resp->assertRedirect(); + $page->refresh(); + $pageUrl = $page->getUrl(); - $this->assertDatabaseHas('pages', [ - 'markdown' => $details['markdown'], - 'id' => $draft->id, - 'draft' => false + $this->put($pageUrl, [ + 'name' => 'super test page', + 'html' => '

', + ]); + + $this->get($pageUrl) + ->assertRedirect("/books/{$page->book->slug}/page/super-test-page"); + } + + public function test_page_within_chapter_deletion_returns_to_chapter() + { + /** @var Chapter $chapter */ + $chapter = Chapter::query()->first(); + $page = $chapter->pages()->first(); + + $this->asEditor()->delete($page->getUrl()) + ->assertRedirect($chapter->getUrl()); + } + + public function test_recently_updated_pages_view() + { + $user = $this->getEditor(); + $content = $this->createEntityChainBelongingToUser($user); + + $resp = $this->asAdmin()->get('/pages/recently-updated'); + $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', $content['page']->name); + } + + public function test_recently_updated_pages_view_shows_updated_by_details() + { + $user = $this->getEditor(); + /** @var Page $page */ + $page = Page::query()->first(); + + $this->actingAs($user)->put($page->getUrl(), [ + 'name' => 'Updated title', + 'html' => '

Updated content

', ]); + + $resp = $this->asAdmin()->get('/pages/recently-updated'); + $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', 'Updated 1 second ago by ' . $user->name); + } + + public function test_recently_updated_pages_view_shows_parent_chain() + { + $user = $this->getEditor(); + /** @var Page $page */ + $page = Page::query()->whereNotNull('chapter_id')->first(); + + $this->actingAs($user)->put($page->getUrl(), [ + 'name' => 'Updated title', + 'html' => '

Updated content

', + ]); + + $resp = $this->asAdmin()->get('/pages/recently-updated'); + $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)); + } + + public function test_recently_updated_pages_view_does_not_show_parent_if_not_visible() + { + $user = $this->getEditor(); + /** @var Page $page */ + $page = Page::query()->whereNotNull('chapter_id')->first(); + + $this->actingAs($user)->put($page->getUrl(), [ + 'name' => 'Updated title', + 'html' => '

Updated content

', + ]); + + $this->setEntityRestrictions($page->book); + $this->setEntityRestrictions($page, ['view'], [$user->roles->first()]); + + $resp = $this->get('/pages/recently-updated'); + $resp->assertDontSee($page->book->getShortName(42)); + $resp->assertDontSee($page->chapter->getShortName(42)); + $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', 'Updated title'); + } + + 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), + ]); + + $resp = $this->asAdmin()->get('/'); + $this->withHtml($resp)->assertElementNotContains('#recently-updated-pages', $page->name); + + $this->put($page->getUrl(), [ + 'name' => $page->name, + 'html' => $page->html, + ]); + + $resp = $this->get('/'); + $this->withHtml($resp)->assertElementContains('#recently-updated-pages', $page->name); } -} \ No newline at end of file +}