| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Tests\User; | 
					
						
							| 
									
										
										
										
											2020-04-04 08:16:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-14 05:54:21 +08:00
										 |  |  | use BookStack\Entities\Models\Bookshelf; | 
					
						
							| 
									
										
										
										
											2020-04-04 08:16:05 +08:00
										 |  |  | use Tests\TestCase; | 
					
						
							| 
									
										
										
										
											2019-04-14 20:01:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class UserPreferencesTest extends TestCase | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function test_update_sort_preference() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $editor = $this->getEditor(); | 
					
						
							|  |  |  |         $this->actingAs($editor); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         $updateRequest = $this->patch('/settings/users/' . $editor->id . '/change-sort/books', [ | 
					
						
							|  |  |  |             'sort'  => 'created_at', | 
					
						
							|  |  |  |             'order' => 'desc', | 
					
						
							| 
									
										
										
										
											2019-04-14 20:01:51 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |         $updateRequest->assertStatus(302); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertDatabaseHas('settings', [ | 
					
						
							|  |  |  |             'setting_key' => 'user:' . $editor->id . ':books_sort', | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'value'       => 'created_at', | 
					
						
							| 
									
										
										
										
											2019-04-14 20:01:51 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |         $this->assertDatabaseHas('settings', [ | 
					
						
							|  |  |  |             'setting_key' => 'user:' . $editor->id . ':books_sort_order', | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'value'       => 'desc', | 
					
						
							| 
									
										
										
										
											2019-04-14 20:01:51 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |         $this->assertEquals('created_at', setting()->getForCurrentUser('books_sort')); | 
					
						
							|  |  |  |         $this->assertEquals('desc', setting()->getForCurrentUser('books_sort_order')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_update_sort_preference_defaults() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $editor = $this->getEditor(); | 
					
						
							|  |  |  |         $this->actingAs($editor); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         $updateRequest = $this->patch('/settings/users/' . $editor->id . '/change-sort/bookshelves', [ | 
					
						
							|  |  |  |             'sort'  => 'cat', | 
					
						
							|  |  |  |             'order' => 'dog', | 
					
						
							| 
									
										
										
										
											2019-04-14 20:01:51 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |         $updateRequest->assertStatus(302); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertEquals('name', setting()->getForCurrentUser('bookshelves_sort')); | 
					
						
							|  |  |  |         $this->assertEquals('asc', setting()->getForCurrentUser('bookshelves_sort_order')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_update_sort_bad_entity_type_handled() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $editor = $this->getEditor(); | 
					
						
							|  |  |  |         $this->actingAs($editor); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         $updateRequest = $this->patch('/settings/users/' . $editor->id . '/change-sort/dogs', [ | 
					
						
							|  |  |  |             'sort'  => 'name', | 
					
						
							|  |  |  |             'order' => 'asc', | 
					
						
							| 
									
										
										
										
											2019-04-14 20:01:51 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |         $updateRequest->assertStatus(500); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertNotEmpty('name', setting()->getForCurrentUser('bookshelves_sort')); | 
					
						
							|  |  |  |         $this->assertNotEmpty('asc', setting()->getForCurrentUser('bookshelves_sort_order')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_update_expansion_preference() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $editor = $this->getEditor(); | 
					
						
							|  |  |  |         $this->actingAs($editor); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         $updateRequest = $this->patch('/settings/users/' . $editor->id . '/update-expansion-preference/home-details', ['expand' => 'true']); | 
					
						
							| 
									
										
										
										
											2019-04-14 20:01:51 +08:00
										 |  |  |         $updateRequest->assertStatus(204); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertDatabaseHas('settings', [ | 
					
						
							|  |  |  |             'setting_key' => 'user:' . $editor->id . ':section_expansion#home-details', | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'value'       => 'true', | 
					
						
							| 
									
										
										
										
											2019-04-14 20:01:51 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |         $this->assertEquals(true, setting()->getForCurrentUser('section_expansion#home-details')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         $invalidKeyRequest = $this->patch('/settings/users/' . $editor->id . '/update-expansion-preference/my-home-details', ['expand' => 'true']); | 
					
						
							| 
									
										
										
										
											2019-04-14 20:01:51 +08:00
										 |  |  |         $invalidKeyRequest->assertStatus(500); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-12 03:44:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function test_toggle_dark_mode() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $home = $this->actingAs($this->getEditor())->get('/'); | 
					
						
							|  |  |  |         $home->assertSee('Dark Mode'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($home)->assertElementNotExists('.dark-mode'); | 
					
						
							| 
									
										
										
										
											2020-04-12 03:44:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled', false)); | 
					
						
							|  |  |  |         $prefChange = $this->patch('/settings/users/toggle-dark-mode'); | 
					
						
							|  |  |  |         $prefChange->assertRedirect(); | 
					
						
							|  |  |  |         $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $home = $this->actingAs($this->getEditor())->get('/'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($home)->assertElementExists('.dark-mode'); | 
					
						
							| 
									
										
										
										
											2020-04-12 03:44:23 +08:00
										 |  |  |         $home->assertDontSee('Dark Mode'); | 
					
						
							|  |  |  |         $home->assertSee('Light Mode'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-02-08 07:12:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function test_dark_mode_defaults_to_config_option() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         config()->set('setting-defaults.user.dark-mode-enabled', false); | 
					
						
							|  |  |  |         $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled')); | 
					
						
							|  |  |  |         $home = $this->get('/login'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($home)->assertElementNotExists('.dark-mode'); | 
					
						
							| 
									
										
										
										
											2021-02-08 07:12:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         config()->set('setting-defaults.user.dark-mode-enabled', true); | 
					
						
							|  |  |  |         $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled')); | 
					
						
							|  |  |  |         $home = $this->get('/login'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($home)->assertElementExists('.dark-mode'); | 
					
						
							| 
									
										
										
										
											2021-02-08 07:12:05 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-14 05:54:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function test_books_view_type_preferences_when_list() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $editor = $this->getEditor(); | 
					
						
							|  |  |  |         setting()->putUser($editor, 'books_view_type', 'list'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->actingAs($editor)->get('/books'); | 
					
						
							|  |  |  |         $this->withHtml($resp) | 
					
						
							| 
									
										
										
										
											2021-09-14 05:54:21 +08:00
										 |  |  |             ->assertElementNotExists('.featured-image-container') | 
					
						
							|  |  |  |             ->assertElementExists('.content-wrap .entity-list-item'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_books_view_type_preferences_when_grid() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $editor = $this->getEditor(); | 
					
						
							|  |  |  |         setting()->putUser($editor, 'books_view_type', 'grid'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->actingAs($editor)->get('/books'); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementExists('.featured-image-container'); | 
					
						
							| 
									
										
										
										
											2021-09-14 05:54:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_shelf_view_type_change() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $editor = $this->getEditor(); | 
					
						
							|  |  |  |         /** @var Bookshelf $shelf */ | 
					
						
							|  |  |  |         $shelf = Bookshelf::query()->first(); | 
					
						
							|  |  |  |         setting()->putUser($editor, 'bookshelf_view_type', 'list'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->actingAs($editor)->get($shelf->getUrl())->assertSee('Grid View'); | 
					
						
							|  |  |  |         $this->withHtml($resp) | 
					
						
							| 
									
										
										
										
											2021-09-14 05:54:21 +08:00
										 |  |  |             ->assertElementNotExists('.featured-image-container') | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |             ->assertElementExists('.content-wrap .entity-list-item'); | 
					
						
							| 
									
										
										
										
											2021-09-14 05:54:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $req = $this->patch("/settings/users/{$editor->id}/switch-shelf-view", ['view_type' => 'grid']); | 
					
						
							|  |  |  |         $req->assertRedirect($shelf->getUrl()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->actingAs($editor)->get($shelf->getUrl()) | 
					
						
							| 
									
										
										
										
											2021-09-14 05:54:21 +08:00
										 |  |  |             ->assertSee('List View'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->withHtml($resp) | 
					
						
							|  |  |  |             ->assertElementExists('.featured-image-container') | 
					
						
							|  |  |  |             ->assertElementNotExists('.content-wrap .entity-list-item'); | 
					
						
							| 
									
										
										
										
											2021-09-14 05:54:21 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | } |