Fixed page editor back button sometimes going nowhere

Updated the back button to be a proper link instead of a reference to
the last viewed URL since it could break if the last page was the
current one (On validation for example).

Includes test to cover.
Also applied some styleCI changes.

Fixes #2834
This commit is contained in:
Dan Brown
2021-11-15 11:17:27 +00:00
parent 88e6f93abf
commit b546098b36
4 changed files with 33 additions and 7 deletions

View File

@ -3,6 +3,7 @@
namespace Tests\Entity;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
use Tests\TestCase;
@ -74,4 +75,31 @@ class PageEditorTest extends TestCase
'draft' => false,
]);
}
public function test_back_link_in_editor_has_correct_url()
{
/** @var Book $book */
$book = Book::query()->whereHas('pages')->whereHas('chapters')->firstOrFail();
$this->asEditor()->get($book->getUrl('/create-page'));
/** @var Chapter $chapter */
$chapter = $book->chapters()->firstOrFail();
/** @var Page $draft */
$draft = $book->pages()->where('draft', '=', true)->firstOrFail();
// Book draft goes back to book
$resp = $this->get($book->getUrl("/draft/{$draft->id}"));
$resp->assertElementContains('a[href="' . $book->getUrl() . '"]', 'Back');
// Chapter draft goes back to chapter
$draft->chapter_id = $chapter->id;
$draft->save();
$resp = $this->get($book->getUrl("/draft/{$draft->id}"));
$resp->assertElementContains('a[href="' . $chapter->getUrl() . '"]', 'Back');
// Saved page goes back to page
$this->post($book->getUrl("/draft/{$draft->id}"), ['name' => 'Updated', 'html' => 'Updated']);
$draft->refresh();
$resp = $this->get($draft->getUrl('/edit'));
$resp->assertElementContains('a[href="' . $draft->getUrl() . '"]', 'Back');
}
}