X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/de8cceb0f72f19ed0b3aa43d7a139639fa634bd2..refs/pull/3012/head:/tests/Entity/BookTest.php diff --git a/tests/Entity/BookTest.php b/tests/Entity/BookTest.php index a3effe779..2894fbb98 100644 --- a/tests/Entity/BookTest.php +++ b/tests/Entity/BookTest.php @@ -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 = Book::factory()->make([ + 'name' => 'My First Book', + ]); + + $resp = $this->asEditor()->get('/books'); + $resp->assertElementContains('a[href="' . url('/http/source.bookstackapp.com/create-book') . '"]', 'Create New Book'); + + $resp = $this->get('/create-book'); + $resp->assertElementContains('form[action="' . url('/http/source.bookstackapp.com/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 = Book::factory()->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); @@ -126,7 +187,7 @@ class BookTest extends TestCase 'name' => 'информация', ]); - $this->assertEquals('informatsiya', $book->slug); + $this->assertEquals('informaciya', $book->slug); $book = $this->newBook([ 'name' => '¿Qué?',