| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Tests\Entity; | 
					
						
							| 
									
										
										
										
											2020-11-06 20:54:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Book; | 
					
						
							| 
									
										
										
										
											2021-12-20 03:20:31 +08:00
										 |  |  | use BookStack\Entities\Models\BookChild; | 
					
						
							| 
									
										
										
										
											2022-09-28 21:14:51 +08:00
										 |  |  | use BookStack\Entities\Models\Bookshelf; | 
					
						
							| 
									
										
										
										
											2021-12-20 03:20:31 +08:00
										 |  |  | use BookStack\Entities\Repos\BookRepo; | 
					
						
							| 
									
										
										
										
											2020-11-06 20:54:39 +08:00
										 |  |  | use Tests\TestCase; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class BookTest extends TestCase | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  |     public function test_create() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-10-31 04:29:59 +08:00
										 |  |  |         $book = Book::factory()->make([ | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  |             'name' => 'My First Book', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->asEditor()->get('/books'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('a[href="' . url('/create-book') . '"]', 'Create New Book'); | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get('/create-book'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('form[action="' . url('/books') . '"][method="POST"]', 'Save Book'); | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-19 23:10:29 +08:00
										 |  |  |         $resp = $this->post('/books', $book->only('name', 'description_html')); | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  |         $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-31 04:29:59 +08:00
										 |  |  |         $book = Book::factory()->make([ | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  |             'name' => 'My First Book', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-19 23:10:29 +08:00
										 |  |  |         $this->asEditor()->post('/books', $book->only('name', 'description_html')); | 
					
						
							|  |  |  |         $this->asEditor()->post('/books', $book->only('name', 'description_html')); | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $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 20:46:55 +08:00
										 |  |  |     public function test_create_sets_tags() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Cheeky initial update to refresh slug
 | 
					
						
							|  |  |  |         $this->asEditor()->post('books', [ | 
					
						
							| 
									
										
										
										
											2023-12-19 23:10:29 +08:00
										 |  |  |             'name'             => 'My book with tags', | 
					
						
							|  |  |  |             'description_html' => '<p>A book with tags</p>', | 
					
						
							|  |  |  |             'tags'             => [ | 
					
						
							| 
									
										
										
										
											2022-06-25 20:46:55 +08:00
										 |  |  |                 [ | 
					
						
							| 
									
										
										
										
											2022-06-25 21:27:32 +08:00
										 |  |  |                     'name'  => 'Category', | 
					
						
							| 
									
										
										
										
											2022-06-25 20:46:55 +08:00
										 |  |  |                     'value' => 'Donkey Content', | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |                 [ | 
					
						
							| 
									
										
										
										
											2022-06-25 21:27:32 +08:00
										 |  |  |                     'name'  => 'Level', | 
					
						
							| 
									
										
										
										
											2022-06-25 20:46:55 +08:00
										 |  |  |                     'value' => '5', | 
					
						
							| 
									
										
										
										
											2022-06-25 21:27:32 +08:00
										 |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2022-06-25 20:46:55 +08: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-18 04:29:16 +08:00
										 |  |  |     public function test_update() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  |         // Cheeky initial update to refresh slug
 | 
					
						
							| 
									
										
										
										
											2023-12-19 23:10:29 +08:00
										 |  |  |         $this->asEditor()->put($book->getUrl(), ['name' => $book->name . '5', 'description_html' => $book->description_html]); | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  |         $book->refresh(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $newName = $book->name . ' Updated'; | 
					
						
							| 
									
										
										
										
											2023-12-19 23:10:29 +08:00
										 |  |  |         $newDesc = $book->description_html . '<p>with more content</p>'; | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get($book->getUrl('/edit')); | 
					
						
							|  |  |  |         $resp->assertSee($book->name); | 
					
						
							| 
									
										
										
										
											2023-12-19 23:10:29 +08:00
										 |  |  |         $resp->assertSee($book->description_html); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('form[action="' . $book->getUrl() . '"]', 'Save Book'); | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-19 23:10:29 +08:00
										 |  |  |         $resp = $this->put($book->getUrl(), ['name' => $newName, 'description_html' => $newDesc]); | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  |         $resp->assertRedirect($book->getUrl() . '-updated'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get($book->getUrl() . '-updated'); | 
					
						
							|  |  |  |         $resp->assertSee($newName); | 
					
						
							| 
									
										
										
										
											2023-12-19 23:10:29 +08:00
										 |  |  |         $resp->assertSee($newDesc, false); | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-25 20:46:55 +08:00
										 |  |  |     public function test_update_sets_tags() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2022-06-25 20:46:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->assertEquals(0, $book->tags()->count()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Cheeky initial update to refresh slug
 | 
					
						
							|  |  |  |         $this->asEditor()->put($book->getUrl(), [ | 
					
						
							|  |  |  |             'name' => $book->name, | 
					
						
							|  |  |  |             'tags' => [ | 
					
						
							|  |  |  |                 [ | 
					
						
							| 
									
										
										
										
											2022-06-25 21:27:32 +08:00
										 |  |  |                     'name'  => 'Category', | 
					
						
							| 
									
										
										
										
											2022-06-25 20:46:55 +08:00
										 |  |  |                     'value' => 'Dolphin Content', | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |                 [ | 
					
						
							| 
									
										
										
										
											2022-06-25 21:27:32 +08:00
										 |  |  |                     'name'  => 'Level', | 
					
						
							| 
									
										
										
										
											2022-06-25 20:46:55 +08:00
										 |  |  |                     'value' => '5', | 
					
						
							| 
									
										
										
										
											2022-06-25 21:27:32 +08:00
										 |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2022-06-25 20:46:55 +08: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-18 04:29:16 +08:00
										 |  |  |     public function test_delete() | 
					
						
							| 
									
										
										
										
											2020-11-06 20:54:39 +08: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 22:10:18 +08:00
										 |  |  |         $this->assertNotificationContains($redirectReq, 'Book Successfully Deleted'); | 
					
						
							| 
									
										
										
										
											2020-11-06 20:54:39 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-29 19:49:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-14 06:26:39 +08:00
										 |  |  |     public function test_cancel_on_create_page_leads_back_to_books_listing() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $resp = $this->asEditor()->get('/create-book'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('form a[href="' . url('/books') . '"]', 'Cancel'); | 
					
						
							| 
									
										
										
										
											2021-09-14 06:26:39 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_cancel_on_edit_book_page_leads_back_to_book() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2021-09-14 06:26:39 +08:00
										 |  |  |         $resp = $this->asEditor()->get($book->getUrl('/edit')); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('form a[href="' . $book->getUrl() . '"]', 'Cancel'); | 
					
						
							| 
									
										
										
										
											2021-09-14 06:26:39 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 19:49:10 +08:00
										 |  |  |     public function test_next_previous_navigation_controls_show_within_book_content() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2021-05-29 19:49:10 +08:00
										 |  |  |         $chapter = $book->chapters->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->asEditor()->get($chapter->getUrl()); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('#sibling-navigation', 'Next'); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementContains('#sibling-navigation', substr($chapter->pages[0]->name, 0, 20)); | 
					
						
							| 
									
										
										
										
											2021-05-29 19:49:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get($chapter->pages[0]->getUrl()); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08: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 19:49:10 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-14 06:26:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function test_recently_viewed_books_updates_as_expected() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-19 23:10:29 +08:00
										 |  |  |         $books = Book::take(2)->get(); | 
					
						
							| 
									
										
										
										
											2021-09-14 06:26:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->asAdmin()->get('/books'); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementNotContains('#recents', $books[0]->name) | 
					
						
							| 
									
										
										
										
											2021-09-14 06:26:39 +08:00
										 |  |  |             ->assertElementNotContains('#recents', $books[1]->name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->get($books[0]->getUrl()); | 
					
						
							|  |  |  |         $this->get($books[1]->getUrl()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->get('/books'); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementContains('#recents', $books[0]->name) | 
					
						
							| 
									
										
										
										
											2021-09-14 06:26:39 +08:00
										 |  |  |             ->assertElementContains('#recents', $books[1]->name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_popular_books_updates_upon_visits() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-19 23:10:29 +08:00
										 |  |  |         $books = Book::take(2)->get(); | 
					
						
							| 
									
										
										
										
											2021-09-14 06:26:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->asAdmin()->get('/books'); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementNotContains('#popular', $books[0]->name) | 
					
						
							| 
									
										
										
										
											2021-09-14 06:26:39 +08:00
										 |  |  |             ->assertElementNotContains('#popular', $books[1]->name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->get($books[0]->getUrl()); | 
					
						
							|  |  |  |         $this->get($books[1]->getUrl()); | 
					
						
							|  |  |  |         $this->get($books[0]->getUrl()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->get('/books'); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementContains('#popular .book:nth-child(1)', $books[0]->name) | 
					
						
							| 
									
										
										
										
											2021-09-14 06:26:39 +08:00
										 |  |  |             ->assertElementContains('#popular .book:nth-child(2)', $books[1]->name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_books_view_shows_view_toggle_option() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Book $book */ | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $editor = $this->users->editor(); | 
					
						
							| 
									
										
										
										
											2021-09-14 06:26:39 +08:00
										 |  |  |         setting()->putUser($editor, 'books_view_type', 'list'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->actingAs($editor)->get('/books'); | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('form[action$="/preferences/change-view/books"]', 'Grid View'); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementExists('button[name="view"][value="grid"]'); | 
					
						
							| 
									
										
										
										
											2021-09-14 06:26:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |         $resp = $this->patch("/preferences/change-view/books", ['view' => 'grid']); | 
					
						
							| 
									
										
										
										
											2021-09-14 06:26:39 +08:00
										 |  |  |         $resp->assertRedirect(); | 
					
						
							|  |  |  |         $this->assertEquals('grid', setting()->getUser($editor, 'books_view_type')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->actingAs($editor)->get('/books'); | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('form[action$="/preferences/change-view/books"]', 'List View'); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementExists('button[name="view"][value="list"]'); | 
					
						
							| 
									
										
										
										
											2021-09-14 06:26:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |         $resp = $this->patch("/preferences/change-view/books", ['view_type' => 'list']); | 
					
						
							| 
									
										
										
										
											2021-09-14 06:26:39 +08:00
										 |  |  |         $resp->assertRedirect(); | 
					
						
							|  |  |  |         $this->assertEquals('list', setting()->getUser($editor, 'books_view_type')); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-16 05:18:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function test_slug_multi_byte_url_safe() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-29 23:49:25 +08:00
										 |  |  |         $book = $this->entities->newBook([ | 
					
						
							| 
									
										
										
										
											2021-09-16 05:18:37 +08:00
										 |  |  |             'name' => 'информация', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-07 04:41:33 +08:00
										 |  |  |         $this->assertEquals('informaciia', $book->slug); | 
					
						
							| 
									
										
										
										
											2021-09-16 05:18:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-29 23:49:25 +08:00
										 |  |  |         $book = $this->entities->newBook([ | 
					
						
							| 
									
										
										
										
											2021-09-16 05:18:37 +08:00
										 |  |  |             'name' => '¿Qué?', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertEquals('que', $book->slug); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_slug_format() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-29 23:49:25 +08:00
										 |  |  |         $book = $this->entities->newBook([ | 
					
						
							| 
									
										
										
										
											2021-09-16 05:18:37 +08:00
										 |  |  |             'name' => 'PartA / PartB / PartC', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertEquals('parta-partb-partc', $book->slug); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-20 03:20:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-19 23:10:29 +08:00
										 |  |  |     public function test_description_limited_to_specific_html() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $book = $this->entities->book(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 23:46:53 +08:00
										 |  |  |         $input = '<h1>Test</h1><p id="abc" href="beans">Content<a href="#cat" target="_blank" data-a="b">a</a><section>Hello</section></p>'; | 
					
						
							|  |  |  |         $expected = '<p>Content<a href="#cat" target="_blank">a</a></p>'; | 
					
						
							| 
									
										
										
										
											2023-12-19 23:10:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->asEditor()->put($book->getUrl(), [ | 
					
						
							|  |  |  |             'name' => $book->name, | 
					
						
							|  |  |  |             'description_html' => $input | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $book->refresh(); | 
					
						
							|  |  |  |         $this->assertEquals($expected, $book->description_html); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-21 01:40:58 +08:00
										 |  |  |     public function test_show_view_displays_description_if_no_description_html_set() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $book = $this->entities->book(); | 
					
						
							|  |  |  |         $book->description_html = ''; | 
					
						
							|  |  |  |         $book->description = "My great\ndescription\n\nwith newlines"; | 
					
						
							|  |  |  |         $book->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->asEditor()->get($book->getUrl()); | 
					
						
							|  |  |  |         $resp->assertSee("<p>My great<br>\ndescription<br>\n<br>\nwith newlines</p>", false); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 03:20:31 +08:00
										 |  |  |     public function test_show_view_has_copy_button() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2021-12-20 03:20:31 +08:00
										 |  |  |         $resp = $this->asEditor()->get($book->getUrl()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains("a[href=\"{$book->getUrl('/copy')}\"]", 'Copy'); | 
					
						
							| 
									
										
										
										
											2021-12-20 03:20:31 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_copy_view() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2021-12-20 03:20:31 +08:00
										 |  |  |         $resp = $this->asEditor()->get($book->getUrl('/copy')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp->assertOk(); | 
					
						
							|  |  |  |         $resp->assertSee('Copy Book'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementExists("input[name=\"name\"][value=\"{$book->name}\"]"); | 
					
						
							| 
									
										
										
										
											2021-12-20 03:20:31 +08: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()); | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |         $this->assertEquals($book->getDirectVisibleChildren()->count(), $copy->getDirectVisibleChildren()->count()); | 
					
						
							| 
									
										
										
										
											2023-12-21 01:21:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->get($copy->getUrl())->assertSee($book->description_html, false); | 
					
						
							| 
									
										
										
										
											2021-12-20 03:20:31 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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 */ | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |         foreach ($book->getDirectVisibleChildren() as $child) { | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |             $this->permissions->setEntityPermissions($child, [], []); | 
					
						
							| 
									
										
										
										
											2021-12-20 03:20:31 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']); | 
					
						
							|  |  |  |         /** @var Book $copy */ | 
					
						
							|  |  |  |         $copy = Book::query()->where('name', '=', 'My copy book')->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |         $this->assertEquals(0, $copy->getDirectVisibleChildren()->count()); | 
					
						
							| 
									
										
										
										
											2021-12-20 03:20:31 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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(); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $viewer = $this->users->viewer(); | 
					
						
							|  |  |  |         $this->permissions->grantUserRolePermissions($viewer, ['book-create-all']); | 
					
						
							| 
									
										
										
										
											2021-12-20 03:20:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $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() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2021-12-20 03:20:31 +08:00
										 |  |  |         $bookRepo = $this->app->make(BookRepo::class); | 
					
						
							| 
									
										
										
										
											2023-02-08 22:39:13 +08:00
										 |  |  |         $coverImageFile = $this->files->uploadedImage('cover.png'); | 
					
						
							| 
									
										
										
										
											2021-12-20 03:20:31 +08:00
										 |  |  |         $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-20 01:44:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 03:20:31 +08:00
										 |  |  |         $this->assertNotNull($copy->cover); | 
					
						
							|  |  |  |         $this->assertNotEquals($book->cover->id, $copy->cover->id); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-28 21:14:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function test_copy_adds_book_to_shelves_if_edit_permissions_allows() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Bookshelf $shelfA */ | 
					
						
							|  |  |  |         /** @var Bookshelf $shelfB */ | 
					
						
							|  |  |  |         [$shelfA, $shelfB] = Bookshelf::query()->take(2)->get(); | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2022-09-28 21:14:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $shelfA->appendBook($book); | 
					
						
							|  |  |  |         $shelfB->appendBook($book); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $viewer = $this->users->viewer(); | 
					
						
							|  |  |  |         $this->permissions->grantUserRolePermissions($viewer, ['book-update-all', 'book-create-all', 'bookshelf-update-all']); | 
					
						
							|  |  |  |         $this->permissions->setEntityPermissions($shelfB); | 
					
						
							| 
									
										
										
										
											2022-09-28 21:14:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']); | 
					
						
							|  |  |  |         /** @var Book $copy */ | 
					
						
							|  |  |  |         $copy = Book::query()->where('name', '=', 'My copy book')->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertTrue($copy->shelves()->where('id', '=', $shelfA->id)->exists()); | 
					
						
							|  |  |  |         $this->assertFalse($copy->shelves()->where('id', '=', $shelfB->id)->exists()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | } |