2021-06-26 23:23:15 +08:00
< ? php
namespace Tests\Entity ;
2016-03-13 00:31:02 +08:00
2021-09-14 05:54:21 +08:00
use BookStack\Entities\Models\Book ;
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
{
2021-09-14 05:54:21 +08:00
/**
* @ var Page
*/
2016-03-13 00:31:02 +08:00
protected $page ;
2019-10-05 19:55:01 +08:00
/**
* @ var PageRepo
*/
2018-10-13 18:27:55 +08:00
protected $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 ();
2021-09-14 05:54:21 +08:00
$this -> page = Page :: query () -> first ();
$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
$this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ))
-> 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 ]);
2021-09-14 05:54:21 +08:00
$this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ))
-> 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>' ;
2021-09-14 05:54:21 +08:00
$this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ))
-> assertElementNotContains ( '[name="html"]' , $addedContent );
2016-03-13 00:31:02 +08:00
$newContent = $this -> page -> html . $addedContent ;
2016-05-07 21:29:43 +08:00
$newUser = $this -> getEditor ();
2018-10-13 18:27:55 +08:00
$this -> pageRepo -> updatePageDraft ( $this -> page , [ 'html' => $newContent ]);
2019-09-20 07:18:28 +08:00
2021-09-14 05:54:21 +08:00
$this -> actingAs ( $newUser ) -> get ( $this -> page -> getUrl ( '/edit' ))
-> 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>' ;
2021-09-14 05:54:21 +08:00
$this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ))
-> assertElementNotContains ( '[name="html"]' , $addedContent );
2016-03-13 00:31:02 +08:00
$newContent = $this -> page -> html . $addedContent ;
2016-05-07 21:29:43 +08:00
$newUser = $this -> getEditor ();
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 ();
2021-09-14 05:54:21 +08:00
$this -> get ( $nonEditedPage -> getUrl () . '/edit' )
-> 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 ()
{
$admin = $this -> getAdmin ();
$editor = $this -> getEditor ();
/** @var Page $page */
$page = Page :: query () -> first ();
$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 ();
/** @var Page $page */
$page = Page :: query () -> first ();
$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 ()
{
2021-09-14 05:54:21 +08:00
/** @var Book $book */
$book = Book :: query () -> first ();
$this -> asAdmin () -> get ( '/' )
-> assertElementNotContains ( '#recent-drafts' , 'New Page' );
$this -> get ( $book -> getUrl () . '/create-page' );
$this -> get ( '/' ) -> assertElementContains ( '#recent-drafts' , 'New Page' );
2016-03-13 20:04:08 +08:00
}
public function test_draft_pages_not_visible_by_others ()
{
2021-09-14 05:54:21 +08:00
/** @var Book $book */
$book = Book :: query () -> first ();
2016-03-13 20:04:08 +08:00
$chapter = $book -> chapters -> first ();
2016-05-07 21:29:43 +08:00
$newUser = $this -> getEditor ();
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' ));
$this -> get ( $book -> getUrl ())
-> assertElementContains ( '.book-contents' , 'New Page' );
$this -> asAdmin () -> get ( $book -> getUrl ())
-> assertElementNotContains ( '.book-contents' , 'New Page' );
$this -> get ( $chapter -> getUrl ())
-> 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 ();
2021-09-14 05:54:21 +08:00
/** @var Page $page */
2020-07-06 04:18:17 +08:00
$page = Page :: query () -> first ();
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
public function test_updating_page_draft_with_markdown_retains_markdown_content ()
{
/** @var Book $book */
$book = Book :: query () -> first ();
$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 );
}
2016-03-13 00:31:02 +08:00
}