Finished moving EntityTests out to new TestCase files

This commit is contained in:
Dan Brown 2021-09-17 21:29:16 +01:00
parent de8cceb0f7
commit 5d93dd258e
No known key found for this signature in database
GPG key ID: 46D9F943C24A2EF9
5 changed files with 167 additions and 167 deletions

View file

@ -3,12 +3,73 @@
namespace Tests\Entity;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use Tests\TestCase;
class BookTest extends TestCase
{
public function test_book_delete()
public function test_create()
{
$book = factory(Book::class)->make([
'name' => 'My First Book',
]);
$resp = $this->asEditor()->get('/books');
$resp->assertElementContains('a[href="' . url('/create-book') . '"]', 'Create New Book');
$resp = $this->get('/create-book');
$resp->assertElementContains('form[action="' . url('/books') . '"][method="POST"]', 'Save Book');
$resp = $this->post('/books', $book->only('name', 'description'));
$resp->assertRedirect('/books/my-first-book');
$resp = $this->get('/books/my-first-book');
$resp->assertSee($book->name);
$resp->assertSee($book->description);
}
public function test_create_uses_different_slugs_when_name_reused()
{
$book = factory(Book::class)->make([
'name' => 'My First Book',
]);
$this->asEditor()->post('/books', $book->only('name', 'description'));
$this->asEditor()->post('/books', $book->only('name', 'description'));
$books = Book::query()->where('name', '=', $book->name)
->orderBy('id', 'desc')
->take(2)
->get();
$this->assertMatchesRegularExpression('/my-first-book-[0-9a-zA-Z]{3}/', $books[0]->slug);
$this->assertEquals('my-first-book', $books[1]->slug);
}
public function test_update()
{
/** @var Book $book */
$book = Book::query()->first();
// Cheeky initial update to refresh slug
$this->asEditor()->put($book->getUrl(), ['name' => $book->name . '5', 'description' => $book->description]);
$book->refresh();
$newName = $book->name . ' Updated';
$newDesc = $book->description . ' with more content';
$resp = $this->get($book->getUrl('/edit'));
$resp->assertSee($book->name);
$resp->assertSee($book->description);
$resp->assertElementContains('form[action="' . $book->getUrl() . '"]', 'Save Book');
$resp = $this->put($book->getUrl(), ['name' => $newName, 'description' => $newDesc]);
$resp->assertRedirect($book->getUrl() . '-updated');
$resp = $this->get($book->getUrl() . '-updated');
$resp->assertSee($newName);
$resp->assertSee($newDesc);
}
public function test_delete()
{
$book = Book::query()->whereHas('pages')->whereHas('chapters')->first();
$this->assertNull($book->deleted_at);

View file

@ -2,12 +2,36 @@
namespace Tests\Entity;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
use Tests\TestCase;
class ChapterTest extends TestCase
{
public function test_chapter_delete()
public function test_create()
{
/** @var Book $book */
$book = Book::query()->first();
$chapter = factory(Chapter::class)->make([
'name' => 'My First Chapter',
]);
$resp = $this->asEditor()->get($book->getUrl());
$resp->assertElementContains('a[href="' . $book->getUrl('/create-chapter') . '"]', 'New Chapter');
$resp = $this->get($book->getUrl('/create-chapter'));
$resp->assertElementContains('form[action="' . $book->getUrl('/create-chapter') . '"][method="POST"]', 'Save Chapter');
$resp = $this->post($book->getUrl('/create-chapter'), $chapter->only('name', 'description'));
$resp->assertRedirect($book->getUrl('/chapter/my-first-chapter'));
$resp = $this->get($book->getUrl('/chapter/my-first-chapter'));
$resp->assertSee($chapter->name);
$resp->assertSee($chapter->description);
}
public function test_delete()
{
$chapter = Chapter::query()->whereHas('pages')->first();
$this->assertNull($chapter->deleted_at);

View file

@ -0,0 +1,52 @@
<?php
namespace Tests\Entity;
use BookStack\Auth\UserRepo;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Repos\PageRepo;
use Tests\TestCase;
class EntityAccessTest extends TestCase
{
public function test_entities_viewable_after_creator_deletion()
{
// Create required assets and revisions
$creator = $this->getEditor();
$updater = $this->getViewer();
$entities = $this->createEntityChainBelongingToUser($creator, $updater);
app()->make(UserRepo::class)->destroy($creator);
app()->make(PageRepo::class)->update($entities['page'], ['html' => '<p>hello!</p>>']);
$this->checkEntitiesViewable($entities);
}
public function test_entities_viewable_after_updater_deletion()
{
// Create required assets and revisions
$creator = $this->getViewer();
$updater = $this->getEditor();
$entities = $this->createEntityChainBelongingToUser($creator, $updater);
app()->make(UserRepo::class)->destroy($updater);
app()->make(PageRepo::class)->update($entities['page'], ['html' => '<p>Hello there!</p>']);
$this->checkEntitiesViewable($entities);
}
/**
* @param array<string, Entity> $entities
*/
private function checkEntitiesViewable(array $entities)
{
// Check pages and books are visible.
$this->asAdmin();
foreach ($entities as $entity) {
$this->get($entity->getUrl())
->assertStatus(200)
->assertSee($entity->name);
}
// Check revision listing shows no errors.
$this->get($entities['page']->getUrl('/revisions'))->assertStatus(200);
}
}

View file

@ -1,164 +0,0 @@
<?php
namespace Tests\Entity;
use BookStack\Auth\UserRepo;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\PageRepo;
use Tests\BrowserKitTest;
class EntityTest extends BrowserKitTest
{
public function test_entity_creation()
{
// Test Creation
$book = $this->bookCreation();
$chapter = $this->chapterCreation($book);
$this->pageCreation($chapter);
// Test Updating
$this->bookUpdate($book);
}
public function bookUpdate(Book $book)
{
$newName = $book->name . ' Updated';
$this->asAdmin()
// Go to edit screen
->visit($book->getUrl() . '/edit')
->see($book->name)
// Submit new name
->type($newName, '#name')
->press('Save Book')
// Check page url and text
->seePageIs($book->getUrl() . '-updated')
->see($newName);
return Book::find($book->id);
}
public function pageCreation($chapter)
{
$page = factory(Page::class)->make([
'name' => 'My First Page',
]);
$this->asAdmin()
// Navigate to page create form
->visit($chapter->getUrl())
->click('New Page');
$draftPage = Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first();
$this->seePageIs($draftPage->getUrl())
// Fill out form
->type($page->name, '#name')
->type($page->html, '#html')
->press('Save Page')
// Check redirect and page
->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
->see($page->name);
$page = Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
return $page;
}
public function chapterCreation(Book $book)
{
$chapter = factory(Chapter::class)->make([
'name' => 'My First Chapter',
]);
$this->asAdmin()
// Navigate to chapter create page
->visit($book->getUrl())
->click('New Chapter')
->seePageIs($book->getUrl() . '/create-chapter')
// Fill out form
->type($chapter->name, '#name')
->type($chapter->description, '#description')
->press('Save Chapter')
// Check redirect and landing page
->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
->see($chapter->name)->see($chapter->description);
$chapter = Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
return $chapter;
}
public function bookCreation()
{
$book = factory(Book::class)->make([
'name' => 'My First Book',
]);
$this->asAdmin()
->visit('/books')
// Choose to create a book
->click('Create New Book')
->seePageIs('/create-book')
// Fill out form & save
->type($book->name, '#name')
->type($book->description, '#description')
->press('Save Book')
// Check it redirects correctly
->seePageIs('/books/my-first-book')
->see($book->name)->see($book->description);
// Ensure duplicate names are given different slugs
$this->asAdmin()
->visit('/create-book')
->type($book->name, '#name')
->type($book->description, '#description')
->press('Save Book');
$expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
$this->assertMatchesRegularExpression($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
$book = Book::where('slug', '=', 'my-first-book')->first();
return $book;
}
public function test_entities_viewable_after_creator_deletion()
{
// Create required assets and revisions
$creator = $this->getEditor();
$updater = $this->getEditor();
$entities = $this->createEntityChainBelongingToUser($creator, $updater);
$this->actingAs($creator);
app(UserRepo::class)->destroy($creator);
app(PageRepo::class)->update($entities['page'], ['html' => '<p>hello!</p>>']);
$this->checkEntitiesViewable($entities);
}
public function test_entities_viewable_after_updater_deletion()
{
// Create required assets and revisions
$creator = $this->getEditor();
$updater = $this->getEditor();
$entities = $this->createEntityChainBelongingToUser($creator, $updater);
$this->actingAs($updater);
app(UserRepo::class)->destroy($updater);
app(PageRepo::class)->update($entities['page'], ['html' => '<p>Hello there!</p>']);
$this->checkEntitiesViewable($entities);
}
private function checkEntitiesViewable($entities)
{
// Check pages and books are visible.
$this->asAdmin();
$this->visit($entities['book']->getUrl())->seeStatusCode(200)
->visit($entities['chapter']->getUrl())->seeStatusCode(200)
->visit($entities['page']->getUrl())->seeStatusCode(200);
// Check revision listing shows no errors.
$this->visit($entities['page']->getUrl())
->click('Revisions')->seeStatusCode(200);
}
}

View file

@ -10,6 +10,33 @@ use Tests\TestCase;
class PageTest extends TestCase
{
public function test_create()
{
/** @var Chapter $chapter */
$chapter = Chapter::query()->first();
$page = factory(Page::class)->make([
'name' => 'My First Page',
]);
$resp = $this->asEditor()->get($chapter->getUrl());
$resp->assertElementContains('a[href="' . $chapter->getUrl('/create-page') . '"]', 'New Page');
$resp = $this->get($chapter->getUrl('/create-page'));
/** @var Page $draftPage */
$draftPage = Page::query()
->where('draft', '=', true)
->orderBy('created_at', 'desc')
->first();
$resp->assertRedirect($draftPage->getUrl());
$resp = $this->get($draftPage->getUrl());
$resp->assertElementContains('form[action="' . $draftPage->getUrl() . '"][method="POST"]', 'Save Page');
$resp = $this->post($draftPage->getUrl(), $draftPage->only('name', 'html'));
$draftPage->refresh();
$resp->assertRedirect($draftPage->getUrl());
}
public function test_page_view_when_creator_is_deleted_but_owner_exists()
{
$page = Page::query()->first();