| 
									
										
										
										
											2022-08-16 18:27:22 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Tests; | 
					
						
							| 
									
										
										
										
											2021-05-16 08:07:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\Activity\Models\Favourite; | 
					
						
							|  |  |  | use BookStack\Users\Models\User; | 
					
						
							| 
									
										
										
										
											2021-05-16 08:07:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class FavouriteTest extends TestCase | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function test_page_add_favourite_flow() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $page = $this->entities->page(); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $editor = $this->users->editor(); | 
					
						
							| 
									
										
										
										
											2021-05-16 08:07:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->actingAs($editor)->get($page->getUrl()); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('button', 'Favourite'); | 
					
						
							| 
									
										
										
										
											2023-09-10 22:18:31 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementExists('form[method="POST"][action$="/favourites/add"] input[name="type"][value="page"]'); | 
					
						
							| 
									
										
										
										
											2021-05-16 08:07:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->post('/favourites/add', [ | 
					
						
							| 
									
										
										
										
											2023-09-10 22:18:31 +08:00
										 |  |  |             'type' => $page->getMorphClass(), | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'id'   => $page->id, | 
					
						
							| 
									
										
										
										
											2021-05-16 08:07:20 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |         $resp->assertRedirect($page->getUrl()); | 
					
						
							|  |  |  |         $resp->assertSessionHas('success', "\"{$page->name}\" has been added to your favourites"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertDatabaseHas('favourites', [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'user_id'           => $editor->id, | 
					
						
							| 
									
										
										
										
											2021-05-16 08:07:20 +08:00
										 |  |  |             'favouritable_type' => $page->getMorphClass(), | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'favouritable_id'   => $page->id, | 
					
						
							| 
									
										
										
										
											2021-05-16 08:07:20 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_page_remove_favourite_flow() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $page = $this->entities->page(); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $editor = $this->users->editor(); | 
					
						
							| 
									
										
										
										
											2021-05-16 08:07:20 +08:00
										 |  |  |         Favourite::query()->forceCreate([ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'user_id'           => $editor->id, | 
					
						
							|  |  |  |             'favouritable_id'   => $page->id, | 
					
						
							| 
									
										
										
										
											2021-05-16 08:07:20 +08:00
										 |  |  |             'favouritable_type' => $page->getMorphClass(), | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->actingAs($editor)->get($page->getUrl()); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('button', 'Unfavourite'); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementExists('form[method="POST"][action$="/favourites/remove"]'); | 
					
						
							| 
									
										
										
										
											2021-05-16 08:07:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->post('/favourites/remove', [ | 
					
						
							| 
									
										
										
										
											2023-09-10 22:18:31 +08:00
										 |  |  |             'type' => $page->getMorphClass(), | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'id'   => $page->id, | 
					
						
							| 
									
										
										
										
											2021-05-16 08:07:20 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |         $resp->assertRedirect($page->getUrl()); | 
					
						
							|  |  |  |         $resp->assertSessionHas('success', "\"{$page->name}\" has been removed from your favourites"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertDatabaseMissing('favourites', [ | 
					
						
							|  |  |  |             'user_id' => $editor->id, | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-10 15:06:48 +08:00
										 |  |  |     public function test_favourite_flow_with_own_permissions() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2022-08-10 15:06:48 +08:00
										 |  |  |         $user = User::factory()->create(); | 
					
						
							|  |  |  |         $book->owned_by = $user->id; | 
					
						
							|  |  |  |         $book->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $this->permissions->grantUserRolePermissions($user, ['book-view-own']); | 
					
						
							| 
									
										
										
										
											2022-08-10 15:06:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->actingAs($user)->get($book->getUrl()); | 
					
						
							|  |  |  |         $resp = $this->post('/favourites/add', [ | 
					
						
							| 
									
										
										
										
											2023-09-10 22:18:31 +08:00
										 |  |  |             'type' => $book->getMorphClass(), | 
					
						
							| 
									
										
										
										
											2022-08-10 15:06:48 +08:00
										 |  |  |             'id'   => $book->id, | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         $resp->assertRedirect($book->getUrl()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertDatabaseHas('favourites', [ | 
					
						
							|  |  |  |             'user_id'           => $user->id, | 
					
						
							|  |  |  |             'favouritable_type' => $book->getMorphClass(), | 
					
						
							|  |  |  |             'favouritable_id'   => $book->id, | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-30 05:11:16 +08:00
										 |  |  |     public function test_each_entity_type_shows_favourite_button() | 
					
						
							| 
									
										
										
										
											2021-05-23 21:34:36 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $this->actingAs($this->users->editor()); | 
					
						
							| 
									
										
										
										
											2021-05-23 21:34:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-30 05:11:16 +08:00
										 |  |  |         foreach ($this->entities->all() as $entity) { | 
					
						
							| 
									
										
										
										
											2021-05-23 21:34:36 +08:00
										 |  |  |             $resp = $this->get($entity->getUrl()); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |             $this->withHtml($resp)->assertElementExists('form[method="POST"][action$="/favourites/add"]'); | 
					
						
							| 
									
										
										
										
											2021-05-23 21:34:36 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_header_contains_link_to_favourites_page_when_logged_in() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:11:06 +08:00
										 |  |  |         $resp = $this->get('/'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementNotContains('header', 'My Favourites'); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $resp = $this->actingAs($this->users->viewer())->get('/'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('header a', 'My Favourites'); | 
					
						
							| 
									
										
										
										
											2021-05-23 21:34:36 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_favourites_shown_on_homepage() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $editor = $this->users->editor(); | 
					
						
							| 
									
										
										
										
											2021-05-23 21:34:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->actingAs($editor)->get('/'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementNotExists('#top-favourites'); | 
					
						
							| 
									
										
										
										
											2021-05-23 21:34:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $page = $this->entities->page(); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id])); | 
					
						
							| 
									
										
										
										
											2021-05-23 21:34:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get('/'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementExists('#top-favourites'); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementContains('#top-favourites', $page->name); | 
					
						
							| 
									
										
										
										
											2021-05-23 21:34:36 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_favourites_list_page_shows_favourites_and_has_working_pagination() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $page = $this->entities->page(); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $editor = $this->users->editor(); | 
					
						
							| 
									
										
										
										
											2021-05-23 21:34:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->actingAs($editor)->get('/favourites'); | 
					
						
							|  |  |  |         $resp->assertDontSee($page->name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id])); | 
					
						
							| 
									
										
										
										
											2021-05-23 21:34:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get('/favourites'); | 
					
						
							|  |  |  |         $resp->assertSee($page->name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get('/favourites?page=2'); | 
					
						
							|  |  |  |         $resp->assertDontSee($page->name); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | } |