2021-06-26 11:23:15 -04:00
< ? php
namespace Tests\Entity ;
2016-03-12 11:31:02 -05:00
2020-11-21 19:17:45 -05:00
use BookStack\Entities\Models\Page ;
2021-10-04 15:26:55 -04:00
use BookStack\Entities\Models\PageRevision ;
2018-10-13 06:27:55 -04:00
use BookStack\Entities\Repos\PageRepo ;
2021-09-13 17:54:21 -04:00
use Tests\TestCase ;
2018-10-13 06:27:55 -04:00
2021-09-13 17:54:21 -04:00
class PageDraftTest extends TestCase
2016-03-12 11:31:02 -05:00
{
2022-09-29 17:11:16 -04:00
protected Page $page ;
protected PageRepo $pageRepo ;
2016-03-12 11:31:02 -05:00
2021-10-30 16:29:59 -04:00
protected function setUp () : void
2016-03-12 11:31:02 -05:00
{
parent :: setUp ();
2022-09-29 17:11:16 -04:00
$this -> page = $this -> entities -> page ();
2021-09-13 17:54:21 -04:00
$this -> pageRepo = app () -> make ( PageRepo :: class );
2016-03-12 11:31:02 -05:00
}
public function test_draft_content_shows_if_available ()
{
$addedContent = '<p>test message content</p>' ;
2021-09-13 17:54:21 -04:00
2022-07-23 10:10:18 -04:00
$resp = $this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ));
$this -> withHtml ( $resp ) -> assertElementNotContains ( '[name="html"]' , $addedContent );
2016-03-12 11:31:02 -05:00
$newContent = $this -> page -> html . $addedContent ;
2018-10-13 06:27:55 -04:00
$this -> pageRepo -> updatePageDraft ( $this -> page , [ 'html' => $newContent ]);
2022-07-23 10:10:18 -04:00
$resp = $this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ));
$this -> withHtml ( $resp ) -> assertElementContains ( '[name="html"]' , $newContent );
2016-03-12 11:31:02 -05:00
}
public function test_draft_not_visible_by_others ()
{
$addedContent = '<p>test message content</p>' ;
2022-07-23 10:10:18 -04:00
$resp = $this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ));
$this -> withHtml ( $resp ) -> assertElementNotContains ( '[name="html"]' , $addedContent );
2016-03-12 11:31:02 -05:00
$newContent = $this -> page -> html . $addedContent ;
2016-05-07 09:29:43 -04:00
$newUser = $this -> getEditor ();
2018-10-13 06:27:55 -04:00
$this -> pageRepo -> updatePageDraft ( $this -> page , [ 'html' => $newContent ]);
2019-09-19 19:18:28 -04:00
2022-07-23 10:10:18 -04:00
$resp = $this -> actingAs ( $newUser ) -> get ( $this -> page -> getUrl ( '/edit' ));
$this -> withHtml ( $resp ) -> assertElementNotContains ( '[name="html"]' , $newContent );
2016-03-12 11:31:02 -05:00
}
public function test_alert_message_shows_if_editing_draft ()
{
$this -> asAdmin ();
2018-10-13 06:27:55 -04:00
$this -> pageRepo -> updatePageDraft ( $this -> page , [ 'html' => 'test content' ]);
2021-09-13 17:54:21 -04:00
$this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ))
-> assertSee ( 'You are currently editing a draft' );
2016-03-12 11:31:02 -05:00
}
public function test_alert_message_shows_if_someone_else_editing ()
{
2021-09-13 17:54:21 -04:00
$nonEditedPage = Page :: query () -> take ( 10 ) -> get () -> last ();
2016-03-12 11:31:02 -05:00
$addedContent = '<p>test message content</p>' ;
2022-07-23 10:10:18 -04:00
$resp = $this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ));
$this -> withHtml ( $resp ) -> assertElementNotContains ( '[name="html"]' , $addedContent );
2016-03-12 11:31:02 -05:00
$newContent = $this -> page -> html . $addedContent ;
2016-05-07 09:29:43 -04:00
$newUser = $this -> getEditor ();
2018-10-13 06:27:55 -04:00
$this -> pageRepo -> updatePageDraft ( $this -> page , [ 'html' => $newContent ]);
2016-03-13 10:33:43 -04:00
$this -> actingAs ( $newUser )
2021-09-13 17:54:21 -04:00
-> get ( $this -> page -> getUrl ( '/edit' ))
-> assertSee ( 'Admin has started editing this page' );
2021-06-26 11:23:15 -04:00
$this -> flushSession ();
2022-07-23 10:10:18 -04:00
$resp = $this -> get ( $nonEditedPage -> getUrl () . '/edit' );
$this -> withHtml ( $resp ) -> assertElementNotContains ( '.notification' , 'Admin has started editing this page' );
2016-03-12 11:31:02 -05:00
}
2021-10-04 15:26:55 -04:00
public function test_draft_save_shows_alert_if_draft_older_than_last_page_update ()
{
$admin = $this -> getAdmin ();
$editor = $this -> getEditor ();
2022-09-29 12:31:38 -04:00
$page = $this -> entities -> page ();
2021-10-04 15:26:55 -04: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 ()
{
$admin = $this -> getAdmin ();
$editor = $this -> getEditor ();
2022-09-29 12:31:38 -04:00
$page = $this -> entities -> page ();
2021-10-04 15:26:55 -04: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 08:04:08 -04:00
public function test_draft_pages_show_on_homepage ()
{
2022-09-29 12:31:38 -04:00
$book = $this -> entities -> book ();
2022-07-23 10:10:18 -04:00
$resp = $this -> asAdmin () -> get ( '/' );
$this -> withHtml ( $resp ) -> assertElementNotContains ( '#recent-drafts' , 'New Page' );
2021-09-13 17:54:21 -04:00
$this -> get ( $book -> getUrl () . '/create-page' );
2022-07-23 10:10:18 -04:00
$this -> withHtml ( $this -> get ( '/' )) -> assertElementContains ( '#recent-drafts' , 'New Page' );
2016-03-13 08:04:08 -04:00
}
public function test_draft_pages_not_visible_by_others ()
{
2022-09-29 12:31:38 -04:00
$book = $this -> entities -> book ();
2016-03-13 08:04:08 -04:00
$chapter = $book -> chapters -> first ();
2016-05-07 09:29:43 -04:00
$newUser = $this -> getEditor ();
2016-03-13 08:04:08 -04:00
2021-09-13 17:54:21 -04:00
$this -> actingAs ( $newUser ) -> get ( $book -> getUrl ( '/create-page' ));
$this -> get ( $chapter -> getUrl ( '/create-page' ));
2022-07-23 10:10:18 -04:00
$resp = $this -> get ( $book -> getUrl ());
$this -> withHtml ( $resp ) -> assertElementContains ( '.book-contents' , 'New Page' );
2021-09-13 17:54:21 -04:00
2022-07-23 10:10:18 -04: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 08:04:08 -04:00
}
2020-07-05 16:18:17 -04:00
public function test_page_html_in_ajax_fetch_response ()
{
$this -> asAdmin ();
2022-09-29 12:31:38 -04:00
$page = $this -> entities -> page ();
2020-07-05 16:18:17 -04:00
2021-09-13 17:54:21 -04:00
$this -> getJson ( '/ajax/page/' . $page -> id ) -> assertJson ([
2020-07-05 16:18:17 -04:00
'html' => $page -> html ,
]);
}
2021-11-14 07:17:22 -05:00
public function test_updating_page_draft_with_markdown_retains_markdown_content ()
{
2022-09-29 12:31:38 -04:00
$book = $this -> entities -> book ();
2021-11-14 07:17:22 -05: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 10:16:18 -05:00
'name' => 'My updated draft' ,
2021-11-14 07:17:22 -05:00
'markdown' => " # My markdown page \n \n [A link](https://example.com) " ,
2021-11-14 10:16:18 -05:00
'html' => '<p>checking markdown takes priority over this</p>' ,
2021-11-14 07:17:22 -05:00
]);
$resp -> assertOk ();
$this -> assertDatabaseHas ( 'pages' , [
2021-11-14 10:16:18 -05:00
'id' => $draft -> id ,
'draft' => true ,
'name' => 'My updated draft' ,
2021-11-14 07:17:22 -05: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 07:53:34 -04:00
public function test_slug_generated_on_draft_publish_to_page_when_no_name_change ()
{
2022-09-29 12:31:38 -04:00
$book = $this -> entities -> book ();
2022-09-01 07:53:34 -04: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 09:47:44 -04:00
'markdown' => 'Update test' ,
2022-09-01 07:53:34 -04:00
]) -> assertOk ();
$draft -> refresh ();
$this -> assertEmpty ( $draft -> slug );
$this -> post ( $draft -> getUrl (), [
'name' => 'My page' ,
2022-09-02 09:47:44 -04:00
'markdown' => '# My markdown page' ,
2022-09-01 07:53:34 -04:00
]);
$this -> assertDatabaseHas ( 'pages' , [
'id' => $draft -> id ,
'draft' => false ,
'slug' => 'my-page' ,
]);
}
2016-03-12 11:31:02 -05:00
}