diff --git a/tests/Entity/BookTest.php b/tests/Entity/BookTest.php index a2d4aee20..a3effe779 100644 --- a/tests/Entity/BookTest.php +++ b/tests/Entity/BookTest.php @@ -119,4 +119,28 @@ class BookTest extends TestCase $resp->assertRedirect(); $this->assertEquals('list', setting()->getUser($editor, 'books_view_type')); } + + public function test_slug_multi_byte_url_safe() + { + $book = $this->newBook([ + 'name' => 'информация', + ]); + + $this->assertEquals('informatsiya', $book->slug); + + $book = $this->newBook([ + 'name' => '¿Qué?', + ]); + + $this->assertEquals('que', $book->slug); + } + + public function test_slug_format() + { + $book = $this->newBook([ + 'name' => 'PartA / PartB / PartC', + ]); + + $this->assertEquals('parta-partb-partc', $book->slug); + } } diff --git a/tests/Entity/EntityTest.php b/tests/Entity/EntityTest.php index 60d66d07f..16f21bd5d 100644 --- a/tests/Entity/EntityTest.php +++ b/tests/Entity/EntityTest.php @@ -4,11 +4,9 @@ namespace Tests\Entity; use BookStack\Auth\UserRepo; use BookStack\Entities\Models\Book; -use BookStack\Entities\Models\Bookshelf; use BookStack\Entities\Models\Chapter; use BookStack\Entities\Models\Page; use BookStack\Entities\Repos\PageRepo; -use Carbon\Carbon; use Tests\BrowserKitTest; class EntityTest extends BrowserKitTest @@ -41,32 +39,6 @@ class EntityTest extends BrowserKitTest return Book::find($book->id); } - public function test_book_sort_page_shows() - { - $books = Book::all(); - $bookToSort = $books[0]; - $this->asAdmin() - ->visit($bookToSort->getUrl()) - ->click('Sort') - ->seePageIs($bookToSort->getUrl() . '/sort') - ->seeStatusCode(200) - ->see($bookToSort->name); - } - - public function test_book_sort_item_returns_book_content() - { - $books = Book::all(); - $bookToSort = $books[0]; - $firstPage = $bookToSort->pages[0]; - $firstChapter = $bookToSort->chapters[0]; - $this->asAdmin() - ->visit($bookToSort->getUrl() . '/sort-item') - // Ensure book details are returned - ->see($bookToSort->name) - ->see($firstPage->name) - ->see($firstChapter->name); - } - public function pageCreation($chapter) { $page = factory(Page::class)->make([ @@ -189,86 +161,4 @@ class EntityTest extends BrowserKitTest ->click('Revisions')->seeStatusCode(200); } - public function test_recently_updated_pages_view() - { - $user = $this->getEditor(); - $content = $this->createEntityChainBelongingToUser($user); - - $this->asAdmin()->visit('/pages/recently-updated') - ->seeInNthElement('.entity-list .page', 0, $content['page']->name); - } - - public function test_old_page_slugs_redirect_to_new_pages() - { - $page = Page::first(); - $pageUrl = $page->getUrl(); - $newPageUrl = '/books/' . $page->book->slug . '/page/super-test-page'; - // Need to save twice since revisions are not generated in seeder. - $this->asAdmin()->visit($pageUrl) - ->clickInElement('#content', 'Edit') - ->type('super test', '#name') - ->press('Save Page'); - - $page = Page::first(); - $pageUrl = $page->getUrl(); - - // Second Save - $this->visit($pageUrl) - ->clickInElement('#content', 'Edit') - ->type('super test page', '#name') - ->press('Save Page') - // Check redirect - ->seePageIs($newPageUrl); - - $this->visit($pageUrl) - ->seePageIs($newPageUrl); - } - - public function test_recently_updated_pages_on_home() - { - $page = Page::orderBy('updated_at', 'asc')->first(); - Page::where('id', '!=', $page->id)->update([ - 'updated_at' => Carbon::now()->subSecond(1), - ]); - $this->asAdmin()->visit('/') - ->dontSeeInElement('#recently-updated-pages', $page->name); - $this->visit($page->getUrl() . '/edit') - ->press('Save Page') - ->visit('/') - ->seeInElement('#recently-updated-pages', $page->name); - } - - public function test_slug_multi_byte_url_safe() - { - $book = $this->newBook([ - 'name' => 'информация', - ]); - - $this->assertEquals('informatsiya', $book->slug); - - $book = $this->newBook([ - 'name' => '¿Qué?', - ]); - - $this->assertEquals('que', $book->slug); - } - - public function test_slug_format() - { - $book = $this->newBook([ - 'name' => 'PartA / PartB / PartC', - ]); - - $this->assertEquals('parta-partb-partc', $book->slug); - } - - public function test_page_within_chapter_deletion_returns_to_chapter() - { - $chapter = Chapter::query()->first(); - $page = $chapter->pages()->first(); - - $this->asEditor()->visit($page->getUrl('/delete')) - ->submitForm('Confirm') - ->seePageIs($chapter->getUrl()); - } } diff --git a/tests/Entity/PageEditorTest.php b/tests/Entity/PageEditorTest.php index 8d1c56e16..588de4f17 100644 --- a/tests/Entity/PageEditorTest.php +++ b/tests/Entity/PageEditorTest.php @@ -2,6 +2,7 @@ namespace Tests\Entity; +use BookStack\Entities\Models\Book; use BookStack\Entities\Models\Page; use Tests\TestCase; @@ -49,4 +50,28 @@ class PageEditorTest extends TestCase $this->asAdmin()->get($this->page->getUrl() . '/edit') ->assertElementContains('[name="markdown"]', $this->page->html); } + + public function test_empty_markdown_still_saves_without_error() + { + $this->setSettings(['app-editor' => 'markdown']); + /** @var Book $book */ + $book = Book::query()->first(); + + $this->asEditor()->get($book->getUrl('/create-page')); + $draft = Page::query()->where('book_id', '=', $book->id) + ->where('draft', '=', true)->first(); + + $details = [ + 'name' => 'my page', + 'markdown' => '', + ]; + $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details); + $resp->assertRedirect(); + + $this->assertDatabaseHas('pages', [ + 'markdown' => $details['markdown'], + 'id' => $draft->id, + 'draft' => false, + ]); + } } \ No newline at end of file diff --git a/tests/Entity/PageTest.php b/tests/Entity/PageTest.php index 2721c225c..3fb847e42 100644 --- a/tests/Entity/PageTest.php +++ b/tests/Entity/PageTest.php @@ -3,7 +3,9 @@ namespace Tests\Entity; use BookStack\Entities\Models\Book; +use BookStack\Entities\Models\Chapter; use BookStack\Entities\Models\Page; +use Carbon\Carbon; use Tests\TestCase; class PageTest extends TestCase @@ -190,26 +192,66 @@ class PageTest extends TestCase ]); } - public function test_empty_markdown_still_saves_without_error() + public function test_old_page_slugs_redirect_to_new_pages() { - $this->setSettings(['app-editor' => 'markdown']); - $book = Book::query()->first(); + /** @var Page $page */ + $page = Page::query()->first(); - $this->asEditor()->get($book->getUrl('/create-page')); - $draft = Page::query()->where('book_id', '=', $book->id) - ->where('draft', '=', true)->first(); - - $details = [ - 'name' => 'my page', - 'markdown' => '', - ]; - $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details); - $resp->assertRedirect(); - - $this->assertDatabaseHas('pages', [ - 'markdown' => $details['markdown'], - 'id' => $draft->id, - 'draft' => false, + // Need to save twice since revisions are not generated in seeder. + $this->asAdmin()->put($page->getUrl(), [ + 'name' => 'super test', + 'html' => '

' ]); + + $page->refresh(); + $pageUrl = $page->getUrl(); + + $this->put($pageUrl, [ + 'name' => 'super test page', + 'html' => '

' + ]); + + $this->get($pageUrl) + ->assertRedirect("/books/{$page->book->slug}/page/super-test-page"); } + + public function test_page_within_chapter_deletion_returns_to_chapter() + { + /** @var Chapter $chapter */ + $chapter = Chapter::query()->first(); + $page = $chapter->pages()->first(); + + $this->asEditor()->delete($page->getUrl()) + ->assertRedirect($chapter->getUrl()); + } + + public function test_recently_updated_pages_view() + { + $user = $this->getEditor(); + $content = $this->createEntityChainBelongingToUser($user); + + $this->asAdmin()->get('/pages/recently-updated') + ->assertElementContains('.entity-list .page:nth-child(1)', $content['page']->name); + } + + public function test_recently_updated_pages_on_home() + { + /** @var Page $page */ + $page = Page::query()->orderBy('updated_at', 'asc')->first(); + Page::query()->where('id', '!=', $page->id)->update([ + 'updated_at' => Carbon::now()->subSecond(1), + ]); + + $this->asAdmin()->get('/') + ->assertElementNotContains('#recently-updated-pages', $page->name); + + $this->put($page->getUrl(), [ + 'name' => $page->name, + 'html' => $page->html, + ]); + + $this->get('/') + ->assertElementContains('#recently-updated-pages', $page->name); + } + } diff --git a/tests/Entity/SortTest.php b/tests/Entity/SortTest.php index e058b39aa..5cfc5c3c5 100644 --- a/tests/Entity/SortTest.php +++ b/tests/Entity/SortTest.php @@ -216,6 +216,19 @@ class SortTest extends TestCase $this->assertEquals($newBook->id, $pageToCheck->book_id); } + public function test_book_sort_page_shows() + { + /** @var Book $bookToSort */ + $bookToSort = Book::query()->first(); + + $resp = $this->asAdmin()->get($bookToSort->getUrl()); + $resp->assertElementExists('a[href="' . $bookToSort->getUrl('/sort') . '"]'); + + $resp = $this->get($bookToSort->getUrl('/sort')); + $resp->assertStatus(200); + $resp->assertSee($bookToSort->name); + } + public function test_book_sort() { $oldBook = Book::query()->first(); @@ -259,6 +272,21 @@ class SortTest extends TestCase $checkResp->assertSee($newBook->name); } + public function test_book_sort_item_returns_book_content() + { + $books = Book::all(); + $bookToSort = $books[0]; + $firstPage = $bookToSort->pages[0]; + $firstChapter = $bookToSort->chapters[0]; + + $resp = $this->asAdmin()->get($bookToSort->getUrl() . '/sort-item'); + + // Ensure book details are returned + $resp->assertSee($bookToSort->name); + $resp->assertSee($firstPage->name); + $resp->assertSee($firstChapter->name); + } + public function test_pages_in_book_show_sorted_by_priority() { /** @var Book $book */ diff --git a/tests/SharedTestHelpers.php b/tests/SharedTestHelpers.php index b39509b06..cd0d244c9 100644 --- a/tests/SharedTestHelpers.php +++ b/tests/SharedTestHelpers.php @@ -213,6 +213,7 @@ trait SharedTestHelpers /** * Create a group of entities that belong to a specific user. + * @return array{book: Book, chapter: Chapter, page: Page} */ protected function createEntityChainBelongingToUser(User $creatorUser, ?User $updaterUser = null): array {