2021-06-26 23:23:15 +08:00
< ? php
namespace Tests\Entity ;
2016-03-13 00:31:02 +08:00
2020-11-22 08:17:45 +08:00
use BookStack\Entities\Models\Page ;
2021-10-05 03:26:55 +08:00
use BookStack\Entities\Models\PageRevision ;
2018-10-13 18:27:55 +08:00
use BookStack\Entities\Repos\PageRepo ;
2021-09-14 05:54:21 +08:00
use Tests\TestCase ;
2018-10-13 18:27:55 +08:00
2021-09-14 05:54:21 +08:00
class PageDraftTest extends TestCase
2016-03-13 00:31:02 +08:00
{
2022-09-30 05:11:16 +08:00
protected Page $page ;
protected PageRepo $pageRepo ;
2016-03-13 00:31:02 +08:00
2021-10-31 04:29:59 +08:00
protected function setUp () : void
2016-03-13 00:31:02 +08:00
{
parent :: setUp ();
2022-09-30 05:11:16 +08:00
$this -> page = $this -> entities -> page ();
2021-09-14 05:54:21 +08:00
$this -> pageRepo = app () -> make ( PageRepo :: class );
2016-03-13 00:31:02 +08:00
}
public function test_draft_content_shows_if_available ()
{
$addedContent = '<p>test message content</p>' ;
2021-09-14 05:54:21 +08:00
2022-07-23 22:10:18 +08:00
$resp = $this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ));
$this -> withHtml ( $resp ) -> assertElementNotContains ( '[name="html"]' , $addedContent );
2016-03-13 00:31:02 +08:00
$newContent = $this -> page -> html . $addedContent ;
2018-10-13 18:27:55 +08:00
$this -> pageRepo -> updatePageDraft ( $this -> page , [ 'html' => $newContent ]);
2022-07-23 22:10:18 +08:00
$resp = $this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ));
$this -> withHtml ( $resp ) -> assertElementContains ( '[name="html"]' , $newContent );
2016-03-13 00:31:02 +08:00
}
public function test_draft_not_visible_by_others ()
{
$addedContent = '<p>test message content</p>' ;
2022-07-23 22:10:18 +08:00
$resp = $this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ));
$this -> withHtml ( $resp ) -> assertElementNotContains ( '[name="html"]' , $addedContent );
2016-03-13 00:31:02 +08:00
$newContent = $this -> page -> html . $addedContent ;
2023-01-21 19:08:34 +08:00
$newUser = $this -> users -> editor ();
2018-10-13 18:27:55 +08:00
$this -> pageRepo -> updatePageDraft ( $this -> page , [ 'html' => $newContent ]);
2019-09-20 07:18:28 +08:00
2022-07-23 22:10:18 +08:00
$resp = $this -> actingAs ( $newUser ) -> get ( $this -> page -> getUrl ( '/edit' ));
$this -> withHtml ( $resp ) -> assertElementNotContains ( '[name="html"]' , $newContent );
2016-03-13 00:31:02 +08:00
}
public function test_alert_message_shows_if_editing_draft ()
{
$this -> asAdmin ();
2018-10-13 18:27:55 +08:00
$this -> pageRepo -> updatePageDraft ( $this -> page , [ 'html' => 'test content' ]);
2021-09-14 05:54:21 +08:00
$this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ))
-> assertSee ( 'You are currently editing a draft' );
2016-03-13 00:31:02 +08:00
}
public function test_alert_message_shows_if_someone_else_editing ()
{
2021-09-14 05:54:21 +08:00
$nonEditedPage = Page :: query () -> take ( 10 ) -> get () -> last ();
2016-03-13 00:31:02 +08:00
$addedContent = '<p>test message content</p>' ;
2022-07-23 22:10:18 +08:00
$resp = $this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ));
$this -> withHtml ( $resp ) -> assertElementNotContains ( '[name="html"]' , $addedContent );
2016-03-13 00:31:02 +08:00
$newContent = $this -> page -> html . $addedContent ;
2023-01-21 19:08:34 +08:00
$newUser = $this -> users -> editor ();
2018-10-13 18:27:55 +08:00
$this -> pageRepo -> updatePageDraft ( $this -> page , [ 'html' => $newContent ]);
2016-03-13 22:33:43 +08:00
$this -> actingAs ( $newUser )
2021-09-14 05:54:21 +08:00
-> get ( $this -> page -> getUrl ( '/edit' ))
-> assertSee ( 'Admin has started editing this page' );
2021-06-26 23:23:15 +08:00
$this -> flushSession ();
2022-07-23 22:10:18 +08:00
$resp = $this -> get ( $nonEditedPage -> getUrl () . '/edit' );
$this -> withHtml ( $resp ) -> assertElementNotContains ( '.notification' , 'Admin has started editing this page' );
2016-03-13 00:31:02 +08:00
}
2021-10-05 03:26:55 +08:00
public function test_draft_save_shows_alert_if_draft_older_than_last_page_update ()
{
2023-01-21 19:08:34 +08:00
$admin = $this -> users -> admin ();
$editor = $this -> users -> editor ();
2022-09-30 00:31:38 +08:00
$page = $this -> entities -> page ();
2021-10-05 03:26:55 +08:00
$this -> actingAs ( $editor ) -> put ( '/ajax/page/' . $page -> id . '/save-draft' , [
'name' => $page -> name ,
'html' => '<p>updated draft</p>' ,
]);
/** @var PageRevision $draft */
$draft = $page -> allRevisions ()
-> where ( 'type' , '=' , 'update_draft' )
-> where ( 'created_by' , '=' , $editor -> id )
-> first ();
$draft -> created_at = now () -> subMinute ( 1 );
$draft -> save ();
$this -> actingAs ( $admin ) -> put ( $page -> refresh () -> getUrl (), [
'name' => $page -> name ,
'html' => '<p>admin update</p>' ,
]);
$resp = $this -> actingAs ( $editor ) -> put ( '/ajax/page/' . $page -> id . '/save-draft' , [
'name' => $page -> name ,
'html' => '<p>updated draft again</p>' ,
]);
$resp -> assertJson ([
'warning' => 'This page has been updated since this draft was created. It is recommended that you discard this draft or take care not to overwrite any page changes.' ,
]);
}
public function test_draft_save_shows_alert_if_draft_edit_started_by_someone_else ()
{
2023-01-21 19:08:34 +08:00
$admin = $this -> users -> admin ();
$editor = $this -> users -> editor ();
2022-09-30 00:31:38 +08:00
$page = $this -> entities -> page ();
2021-10-05 03:26:55 +08:00
$this -> actingAs ( $admin ) -> put ( '/ajax/page/' . $page -> id . '/save-draft' , [
'name' => $page -> name ,
'html' => '<p>updated draft</p>' ,
]);
$resp = $this -> actingAs ( $editor ) -> put ( '/ajax/page/' . $page -> id . '/save-draft' , [
'name' => $page -> name ,
'html' => '<p>updated draft again</p>' ,
]);
$resp -> assertJson ([
'warning' => 'Admin has started editing this page in the last 60 minutes. Take care not to overwrite each other\'s updates!' ,
]);
}
2016-03-13 20:04:08 +08:00
public function test_draft_pages_show_on_homepage ()
{
2022-09-30 00:31:38 +08:00
$book = $this -> entities -> book ();
2022-07-23 22:10:18 +08:00
$resp = $this -> asAdmin () -> get ( '/' );
$this -> withHtml ( $resp ) -> assertElementNotContains ( '#recent-drafts' , 'New Page' );
2021-09-14 05:54:21 +08:00
$this -> get ( $book -> getUrl () . '/create-page' );
2022-07-23 22:10:18 +08:00
$this -> withHtml ( $this -> get ( '/' )) -> assertElementContains ( '#recent-drafts' , 'New Page' );
2016-03-13 20:04:08 +08:00
}
public function test_draft_pages_not_visible_by_others ()
{
2022-09-30 00:31:38 +08:00
$book = $this -> entities -> book ();
2016-03-13 20:04:08 +08:00
$chapter = $book -> chapters -> first ();
2023-01-21 19:08:34 +08:00
$newUser = $this -> users -> editor ();
2016-03-13 20:04:08 +08:00
2021-09-14 05:54:21 +08:00
$this -> actingAs ( $newUser ) -> get ( $book -> getUrl ( '/create-page' ));
$this -> get ( $chapter -> getUrl ( '/create-page' ));
2022-07-23 22:10:18 +08:00
$resp = $this -> get ( $book -> getUrl ());
$this -> withHtml ( $resp ) -> assertElementContains ( '.book-contents' , 'New Page' );
2021-09-14 05:54:21 +08:00
2022-07-23 22:10:18 +08:00
$resp = $this -> asAdmin () -> get ( $book -> getUrl ());
$this -> withHtml ( $resp ) -> assertElementNotContains ( '.book-contents' , 'New Page' );
$resp = $this -> get ( $chapter -> getUrl ());
$this -> withHtml ( $resp ) -> assertElementNotContains ( '.book-contents' , 'New Page' );
2016-03-13 20:04:08 +08:00
}
2020-07-06 04:18:17 +08:00
public function test_page_html_in_ajax_fetch_response ()
{
$this -> asAdmin ();
2022-09-30 00:31:38 +08:00
$page = $this -> entities -> page ();
2020-07-06 04:18:17 +08:00
2021-09-14 05:54:21 +08:00
$this -> getJson ( '/ajax/page/' . $page -> id ) -> assertJson ([
2020-07-06 04:18:17 +08:00
'html' => $page -> html ,
]);
}
2021-11-14 20:17:22 +08:00
2023-06-13 22:13:07 +08:00
public function test_user_draft_removed_on_user_drafts_delete_call ()
{
$editor = $this -> users -> editor ();
$page = $this -> entities -> page ();
$this -> actingAs ( $editor ) -> put ( '/ajax/page/' . $page -> id . '/save-draft' , [
'name' => $page -> name ,
'html' => '<p>updated draft again</p>' ,
]);
$revisionData = [
'type' => 'update_draft' ,
'created_by' => $editor -> id ,
'page_id' => $page -> id ,
];
$this -> assertDatabaseHas ( 'page_revisions' , $revisionData );
$resp = $this -> delete ( " /page-revisions/user-drafts/ { $page -> id } " );
$resp -> assertOk ();
$this -> assertDatabaseMissing ( 'page_revisions' , $revisionData );
}
2021-11-14 20:17:22 +08:00
public function test_updating_page_draft_with_markdown_retains_markdown_content ()
{
2022-09-30 00:31:38 +08:00
$book = $this -> entities -> book ();
2021-11-14 20:17:22 +08:00
$this -> asEditor () -> get ( $book -> getUrl ( '/create-page' ));
/** @var Page $draft */
$draft = Page :: query () -> where ( 'draft' , '=' , true ) -> where ( 'book_id' , '=' , $book -> id ) -> firstOrFail ();
$resp = $this -> put ( '/ajax/page/' . $draft -> id . '/save-draft' , [
2021-11-14 23:16:18 +08:00
'name' => 'My updated draft' ,
2021-11-14 20:17:22 +08:00
'markdown' => " # My markdown page \n \n [A link](https://example.com) " ,
2021-11-14 23:16:18 +08:00
'html' => '<p>checking markdown takes priority over this</p>' ,
2021-11-14 20:17:22 +08:00
]);
$resp -> assertOk ();
$this -> assertDatabaseHas ( 'pages' , [
2021-11-14 23:16:18 +08:00
'id' => $draft -> id ,
'draft' => true ,
'name' => 'My updated draft' ,
2021-11-14 20:17:22 +08:00
'markdown' => " # My markdown page \n \n [A link](https://example.com) " ,
]);
$draft -> refresh ();
$this -> assertStringContainsString ( 'href="https://example.com"' , $draft -> html );
}
2022-09-01 19:53:34 +08:00
public function test_slug_generated_on_draft_publish_to_page_when_no_name_change ()
{
2022-09-30 00:31:38 +08:00
$book = $this -> entities -> book ();
2022-09-01 19:53:34 +08:00
$this -> asEditor () -> get ( $book -> getUrl ( '/create-page' ));
/** @var Page $draft */
$draft = Page :: query () -> where ( 'draft' , '=' , true ) -> where ( 'book_id' , '=' , $book -> id ) -> firstOrFail ();
$this -> put ( '/ajax/page/' . $draft -> id . '/save-draft' , [
'name' => 'My page' ,
2022-09-02 21:47:44 +08:00
'markdown' => 'Update test' ,
2022-09-01 19:53:34 +08:00
]) -> assertOk ();
$draft -> refresh ();
$this -> assertEmpty ( $draft -> slug );
$this -> post ( $draft -> getUrl (), [
'name' => 'My page' ,
2022-09-02 21:47:44 +08:00
'markdown' => '# My markdown page' ,
2022-09-01 19:53:34 +08:00
]);
$this -> assertDatabaseHas ( 'pages' , [
'id' => $draft -> id ,
'draft' => false ,
'slug' => 'my-page' ,
]);
}
2016-03-13 00:31:02 +08:00
}