2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Entity;
|
2020-11-06 07:54:39 -05:00
|
|
|
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Models\Book;
|
2021-12-19 14:20:31 -05:00
|
|
|
use BookStack\Entities\Models\BookChild;
|
|
|
|
use BookStack\Entities\Repos\BookRepo;
|
2020-11-06 07:54:39 -05:00
|
|
|
use Tests\TestCase;
|
2021-12-19 14:20:31 -05:00
|
|
|
use Tests\Uploads\UsesImages;
|
2020-11-06 07:54:39 -05:00
|
|
|
|
|
|
|
class BookTest extends TestCase
|
|
|
|
{
|
2021-12-19 14:20:31 -05:00
|
|
|
use UsesImages;
|
|
|
|
|
2021-09-17 16:29:16 -04:00
|
|
|
public function test_create()
|
|
|
|
{
|
2021-10-30 16:29:59 -04:00
|
|
|
$book = Book::factory()->make([
|
2021-09-17 16:29:16 -04:00
|
|
|
'name' => 'My First Book',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$resp = $this->asEditor()->get('/books');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('a[href="' . url('/create-book') . '"]', 'Create New Book');
|
2021-09-17 16:29:16 -04:00
|
|
|
|
|
|
|
$resp = $this->get('/create-book');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('form[action="' . url('/books') . '"][method="POST"]', 'Save Book');
|
2021-09-17 16:29:16 -04:00
|
|
|
|
|
|
|
$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()
|
|
|
|
{
|
2021-10-30 16:29:59 -04:00
|
|
|
$book = Book::factory()->make([
|
2021-09-17 16:29:16 -04:00
|
|
|
'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);
|
|
|
|
}
|
|
|
|
|
2022-06-25 08:46:55 -04:00
|
|
|
public function test_create_sets_tags()
|
|
|
|
{
|
|
|
|
// Cheeky initial update to refresh slug
|
|
|
|
$this->asEditor()->post('books', [
|
2022-06-25 09:27:32 -04:00
|
|
|
'name' => 'My book with tags',
|
2022-06-25 08:46:55 -04:00
|
|
|
'description' => 'A book with tags',
|
2022-06-25 09:27:32 -04:00
|
|
|
'tags' => [
|
2022-06-25 08:46:55 -04:00
|
|
|
[
|
2022-06-25 09:27:32 -04:00
|
|
|
'name' => 'Category',
|
2022-06-25 08:46:55 -04:00
|
|
|
'value' => 'Donkey Content',
|
|
|
|
],
|
|
|
|
[
|
2022-06-25 09:27:32 -04:00
|
|
|
'name' => 'Level',
|
2022-06-25 08:46:55 -04:00
|
|
|
'value' => '5',
|
2022-06-25 09:27:32 -04:00
|
|
|
],
|
2022-06-25 08:46:55 -04:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
/** @var Book $book */
|
|
|
|
$book = Book::query()->where('name', '=', 'My book with tags')->firstOrFail();
|
|
|
|
$tags = $book->tags()->get();
|
|
|
|
|
|
|
|
$this->assertEquals(2, $tags->count());
|
|
|
|
$this->assertEquals('Donkey Content', $tags[0]->value);
|
|
|
|
$this->assertEquals('Level', $tags[1]->name);
|
|
|
|
}
|
|
|
|
|
2021-09-17 16:29:16 -04:00
|
|
|
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);
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('form[action="' . $book->getUrl() . '"]', 'Save Book');
|
2021-09-17 16:29:16 -04:00
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2022-06-25 08:46:55 -04:00
|
|
|
public function test_update_sets_tags()
|
|
|
|
{
|
|
|
|
/** @var Book $book */
|
|
|
|
$book = Book::query()->first();
|
|
|
|
|
|
|
|
$this->assertEquals(0, $book->tags()->count());
|
|
|
|
|
|
|
|
// Cheeky initial update to refresh slug
|
|
|
|
$this->asEditor()->put($book->getUrl(), [
|
|
|
|
'name' => $book->name,
|
|
|
|
'tags' => [
|
|
|
|
[
|
2022-06-25 09:27:32 -04:00
|
|
|
'name' => 'Category',
|
2022-06-25 08:46:55 -04:00
|
|
|
'value' => 'Dolphin Content',
|
|
|
|
],
|
|
|
|
[
|
2022-06-25 09:27:32 -04:00
|
|
|
'name' => 'Level',
|
2022-06-25 08:46:55 -04:00
|
|
|
'value' => '5',
|
2022-06-25 09:27:32 -04:00
|
|
|
],
|
2022-06-25 08:46:55 -04:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$book->refresh();
|
|
|
|
$tags = $book->tags()->get();
|
|
|
|
|
|
|
|
$this->assertEquals(2, $tags->count());
|
|
|
|
$this->assertEquals('Dolphin Content', $tags[0]->value);
|
|
|
|
$this->assertEquals('Level', $tags[1]->name);
|
|
|
|
}
|
|
|
|
|
2021-09-17 16:29:16 -04:00
|
|
|
public function test_delete()
|
2020-11-06 07:54:39 -05:00
|
|
|
{
|
|
|
|
$book = Book::query()->whereHas('pages')->whereHas('chapters')->first();
|
|
|
|
$this->assertNull($book->deleted_at);
|
|
|
|
$pageCount = $book->pages()->count();
|
|
|
|
$chapterCount = $book->chapters()->count();
|
|
|
|
|
|
|
|
$deleteViewReq = $this->asEditor()->get($book->getUrl('/delete'));
|
|
|
|
$deleteViewReq->assertSeeText('Are you sure you want to delete this book?');
|
|
|
|
|
|
|
|
$deleteReq = $this->delete($book->getUrl());
|
|
|
|
$deleteReq->assertRedirect(url('/books'));
|
|
|
|
$this->assertActivityExists('book_delete', $book);
|
|
|
|
|
|
|
|
$book->refresh();
|
|
|
|
$this->assertNotNull($book->deleted_at);
|
|
|
|
|
|
|
|
$this->assertTrue($book->pages()->count() === 0);
|
|
|
|
$this->assertTrue($book->chapters()->count() === 0);
|
|
|
|
$this->assertTrue($book->pages()->withTrashed()->count() === $pageCount);
|
|
|
|
$this->assertTrue($book->chapters()->withTrashed()->count() === $chapterCount);
|
|
|
|
$this->assertTrue($book->deletions()->count() === 1);
|
|
|
|
|
|
|
|
$redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->assertNotificationContains($redirectReq, 'Book Successfully Deleted');
|
2020-11-06 07:54:39 -05:00
|
|
|
}
|
2021-05-29 07:49:10 -04:00
|
|
|
|
2021-09-13 18:26:39 -04:00
|
|
|
public function test_cancel_on_create_page_leads_back_to_books_listing()
|
|
|
|
{
|
|
|
|
$resp = $this->asEditor()->get('/create-book');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('form a[href="' . url('/books') . '"]', 'Cancel');
|
2021-09-13 18:26:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_cancel_on_edit_book_page_leads_back_to_book()
|
|
|
|
{
|
|
|
|
/** @var Book $book */
|
|
|
|
$book = Book::query()->first();
|
|
|
|
$resp = $this->asEditor()->get($book->getUrl('/edit'));
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('form a[href="' . $book->getUrl() . '"]', 'Cancel');
|
2021-09-13 18:26:39 -04:00
|
|
|
}
|
|
|
|
|
2021-05-29 07:49:10 -04:00
|
|
|
public function test_next_previous_navigation_controls_show_within_book_content()
|
|
|
|
{
|
|
|
|
$book = Book::query()->first();
|
|
|
|
$chapter = $book->chapters->first();
|
|
|
|
|
|
|
|
$resp = $this->asEditor()->get($chapter->getUrl());
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('#sibling-navigation', 'Next');
|
|
|
|
$this->withHtml($resp)->assertElementContains('#sibling-navigation', substr($chapter->pages[0]->name, 0, 20));
|
2021-05-29 07:49:10 -04:00
|
|
|
|
|
|
|
$resp = $this->get($chapter->pages[0]->getUrl());
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('#sibling-navigation', substr($chapter->pages[1]->name, 0, 20));
|
|
|
|
$this->withHtml($resp)->assertElementContains('#sibling-navigation', 'Previous');
|
|
|
|
$this->withHtml($resp)->assertElementContains('#sibling-navigation', substr($chapter->name, 0, 20));
|
2021-05-29 07:49:10 -04:00
|
|
|
}
|
2021-09-13 18:26:39 -04:00
|
|
|
|
|
|
|
public function test_recently_viewed_books_updates_as_expected()
|
|
|
|
{
|
|
|
|
$books = Book::all()->take(2);
|
|
|
|
|
2022-07-23 10:10:18 -04:00
|
|
|
$resp = $this->asAdmin()->get('/books');
|
|
|
|
$this->withHtml($resp)->assertElementNotContains('#recents', $books[0]->name)
|
2021-09-13 18:26:39 -04:00
|
|
|
->assertElementNotContains('#recents', $books[1]->name);
|
|
|
|
|
|
|
|
$this->get($books[0]->getUrl());
|
|
|
|
$this->get($books[1]->getUrl());
|
|
|
|
|
2022-07-23 10:10:18 -04:00
|
|
|
$resp = $this->get('/books');
|
|
|
|
$this->withHtml($resp)->assertElementContains('#recents', $books[0]->name)
|
2021-09-13 18:26:39 -04:00
|
|
|
->assertElementContains('#recents', $books[1]->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_popular_books_updates_upon_visits()
|
|
|
|
{
|
|
|
|
$books = Book::all()->take(2);
|
|
|
|
|
2022-07-23 10:10:18 -04:00
|
|
|
$resp = $this->asAdmin()->get('/books');
|
|
|
|
$this->withHtml($resp)->assertElementNotContains('#popular', $books[0]->name)
|
2021-09-13 18:26:39 -04:00
|
|
|
->assertElementNotContains('#popular', $books[1]->name);
|
|
|
|
|
|
|
|
$this->get($books[0]->getUrl());
|
|
|
|
$this->get($books[1]->getUrl());
|
|
|
|
$this->get($books[0]->getUrl());
|
|
|
|
|
2022-07-23 10:10:18 -04:00
|
|
|
$resp = $this->get('/books');
|
|
|
|
$this->withHtml($resp)->assertElementContains('#popular .book:nth-child(1)', $books[0]->name)
|
2021-09-13 18:26:39 -04:00
|
|
|
->assertElementContains('#popular .book:nth-child(2)', $books[1]->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_books_view_shows_view_toggle_option()
|
|
|
|
{
|
|
|
|
/** @var Book $book */
|
|
|
|
$editor = $this->getEditor();
|
|
|
|
setting()->putUser($editor, 'books_view_type', 'list');
|
|
|
|
|
|
|
|
$resp = $this->actingAs($editor)->get('/books');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('form[action$="/settings/users/' . $editor->id . '/switch-books-view"]', 'Grid View');
|
|
|
|
$this->withHtml($resp)->assertElementExists('input[name="view_type"][value="grid"]');
|
2021-09-13 18:26:39 -04:00
|
|
|
|
|
|
|
$resp = $this->patch("/settings/users/{$editor->id}/switch-books-view", ['view_type' => 'grid']);
|
|
|
|
$resp->assertRedirect();
|
|
|
|
$this->assertEquals('grid', setting()->getUser($editor, 'books_view_type'));
|
|
|
|
|
|
|
|
$resp = $this->actingAs($editor)->get('/books');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('form[action$="/settings/users/' . $editor->id . '/switch-books-view"]', 'List View');
|
|
|
|
$this->withHtml($resp)->assertElementExists('input[name="view_type"][value="list"]');
|
2021-09-13 18:26:39 -04:00
|
|
|
|
|
|
|
$resp = $this->patch("/settings/users/{$editor->id}/switch-books-view", ['view_type' => 'list']);
|
|
|
|
$resp->assertRedirect();
|
|
|
|
$this->assertEquals('list', setting()->getUser($editor, 'books_view_type'));
|
|
|
|
}
|
2021-09-15 17:18:37 -04:00
|
|
|
|
|
|
|
public function test_slug_multi_byte_url_safe()
|
|
|
|
{
|
|
|
|
$book = $this->newBook([
|
|
|
|
'name' => 'информация',
|
|
|
|
]);
|
|
|
|
|
2021-10-26 17:04:18 -04:00
|
|
|
$this->assertEquals('informaciya', $book->slug);
|
2021-09-15 17:18:37 -04:00
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
2021-12-19 14:20:31 -05:00
|
|
|
|
|
|
|
public function test_show_view_has_copy_button()
|
|
|
|
{
|
|
|
|
/** @var Book $book */
|
|
|
|
$book = Book::query()->first();
|
|
|
|
$resp = $this->asEditor()->get($book->getUrl());
|
|
|
|
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains("a[href=\"{$book->getUrl('/copy')}\"]", 'Copy');
|
2021-12-19 14:20:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_copy_view()
|
|
|
|
{
|
|
|
|
/** @var Book $book */
|
|
|
|
$book = Book::query()->first();
|
|
|
|
$resp = $this->asEditor()->get($book->getUrl('/copy'));
|
|
|
|
|
|
|
|
$resp->assertOk();
|
|
|
|
$resp->assertSee('Copy Book');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementExists("input[name=\"name\"][value=\"{$book->name}\"]");
|
2021-12-19 14:20:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_copy()
|
|
|
|
{
|
|
|
|
/** @var Book $book */
|
|
|
|
$book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
|
|
|
|
$resp = $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
|
|
|
|
|
|
|
|
/** @var Book $copy */
|
|
|
|
$copy = Book::query()->where('name', '=', 'My copy book')->first();
|
|
|
|
|
|
|
|
$resp->assertRedirect($copy->getUrl());
|
|
|
|
$this->assertEquals($book->getDirectChildren()->count(), $copy->getDirectChildren()->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_copy_does_not_copy_non_visible_content()
|
|
|
|
{
|
|
|
|
/** @var Book $book */
|
|
|
|
$book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
|
|
|
|
|
|
|
|
// Hide child content
|
|
|
|
/** @var BookChild $page */
|
|
|
|
foreach ($book->getDirectChildren() as $child) {
|
|
|
|
$child->restricted = true;
|
|
|
|
$child->save();
|
|
|
|
$this->regenEntityPermissions($child);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
|
|
|
|
/** @var Book $copy */
|
|
|
|
$copy = Book::query()->where('name', '=', 'My copy book')->first();
|
|
|
|
|
|
|
|
$this->assertEquals(0, $copy->getDirectChildren()->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_copy_does_not_copy_pages_or_chapters_if_user_cant_create()
|
|
|
|
{
|
|
|
|
/** @var Book $book */
|
|
|
|
$book = Book::query()->whereHas('chapters')->whereHas('directPages')->whereHas('chapters')->first();
|
|
|
|
$viewer = $this->getViewer();
|
|
|
|
$this->giveUserPermissions($viewer, ['book-create-all']);
|
|
|
|
|
|
|
|
$this->actingAs($viewer)->post($book->getUrl('/copy'), ['name' => 'My copy book']);
|
|
|
|
/** @var Book $copy */
|
|
|
|
$copy = Book::query()->where('name', '=', 'My copy book')->first();
|
|
|
|
|
|
|
|
$this->assertEquals(0, $copy->pages()->count());
|
|
|
|
$this->assertEquals(0, $copy->chapters()->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_copy_clones_cover_image_if_existing()
|
|
|
|
{
|
|
|
|
/** @var Book $book */
|
|
|
|
$book = Book::query()->first();
|
|
|
|
$bookRepo = $this->app->make(BookRepo::class);
|
|
|
|
$coverImageFile = $this->getTestImage('cover.png');
|
|
|
|
$bookRepo->updateCoverImage($book, $coverImageFile);
|
|
|
|
|
|
|
|
$this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
|
|
|
|
|
|
|
|
/** @var Book $copy */
|
|
|
|
$copy = Book::query()->where('name', '=', 'My copy book')->first();
|
2022-06-19 13:44:34 -04:00
|
|
|
|
2021-12-19 14:20:31 -05:00
|
|
|
$this->assertNotNull($copy->cover);
|
|
|
|
$this->assertNotEquals($book->cover->id, $copy->cover->id);
|
|
|
|
}
|
2021-06-26 11:23:15 -04:00
|
|
|
}
|