Moved more tests out of EntityTest

This commit is contained in:
Dan Brown 2021-09-15 22:18:37 +01:00
parent 121a746d59
commit de8cceb0f7
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
6 changed files with 138 additions and 128 deletions

View File

@ -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);
}
}

View File

@ -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());
}
}

View File

@ -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,
]);
}
}

View File

@ -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' => '<p></p>'
]);
$page->refresh();
$pageUrl = $page->getUrl();
$this->put($pageUrl, [
'name' => 'super test page',
'html' => '<p></p>'
]);
$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);
}
}

View File

@ -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 */

View File

@ -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
{