2021-06-26 11:23:15 -04:00
< ? php
namespace Tests\Entity ;
2016-03-12 11:31:02 -05:00
2021-09-13 17:54:21 -04:00
use BookStack\Entities\Models\Book ;
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
{
2021-09-13 17:54:21 -04:00
/**
* @ var Page
*/
2016-03-12 11:31:02 -05:00
protected $page ;
2019-10-05 07:55:01 -04:00
/**
* @ var PageRepo
*/
2018-10-13 06:27:55 -04:00
protected $pageRepo ;
2016-03-12 11:31:02 -05:00
2019-09-13 18:58:40 -04:00
public function setUp () : void
2016-03-12 11:31:02 -05:00
{
parent :: setUp ();
2021-09-13 17:54:21 -04:00
$this -> page = Page :: query () -> first ();
$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
$this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ))
-> 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 ]);
2021-09-13 17:54:21 -04:00
$this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ))
-> 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>' ;
2021-09-13 17:54:21 -04:00
$this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ))
-> 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
2021-09-13 17:54:21 -04:00
$this -> actingAs ( $newUser ) -> get ( $this -> page -> getUrl ( '/edit' ))
-> 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>' ;
2021-09-13 17:54:21 -04:00
$this -> asAdmin () -> get ( $this -> page -> getUrl ( '/edit' ))
-> 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 ();
2021-09-13 17:54:21 -04:00
$this -> get ( $nonEditedPage -> getUrl () . '/edit' )
-> 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 ();
/** @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 08:04:08 -04:00
public function test_draft_pages_show_on_homepage ()
{
2021-09-13 17:54:21 -04: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 08:04:08 -04:00
}
public function test_draft_pages_not_visible_by_others ()
{
2021-09-13 17:54:21 -04:00
/** @var Book $book */
$book = Book :: query () -> first ();
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' ));
$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 08:04:08 -04:00
}
2020-07-05 16:18:17 -04:00
public function test_page_html_in_ajax_fetch_response ()
{
$this -> asAdmin ();
2021-09-13 17:54:21 -04:00
/** @var Page $page */
2020-07-05 16:18:17 -04:00
$page = Page :: query () -> first ();
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 ,
]);
}
2016-03-12 11:31:02 -05:00
}