| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Tests\Entity; | 
					
						
							| 
									
										
										
										
											2019-07-07 20:45:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Page; | 
					
						
							| 
									
										
										
										
											2019-07-07 20:45:46 +08:00
										 |  |  | use Tests\TestCase; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class PageTemplateTest extends TestCase | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function test_active_templates_visible_on_page_view() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 05:11:16 +08:00
										 |  |  |         $page = $this->entities->page(); | 
					
						
							| 
									
										
										
										
											2019-07-07 20:45:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->asEditor(); | 
					
						
							|  |  |  |         $templateView = $this->get($page->getUrl()); | 
					
						
							|  |  |  |         $templateView->assertDontSee('Page Template'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $page->template = true; | 
					
						
							|  |  |  |         $page->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $templateView = $this->get($page->getUrl()); | 
					
						
							|  |  |  |         $templateView->assertSee('Page Template'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_manage_templates_permission_required_to_change_page_template_status() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 05:11:16 +08:00
										 |  |  |         $page = $this->entities->page(); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $editor = $this->users->editor(); | 
					
						
							| 
									
										
										
										
											2019-07-07 20:45:46 +08:00
										 |  |  |         $this->actingAs($editor); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $pageUpdateData = [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'name'     => $page->name, | 
					
						
							|  |  |  |             'html'     => $page->html, | 
					
						
							| 
									
										
										
										
											2019-07-07 20:45:46 +08:00
										 |  |  |             'template' => 'true', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->put($page->getUrl(), $pageUpdateData); | 
					
						
							|  |  |  |         $this->assertDatabaseHas('pages', [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'id'       => $page->id, | 
					
						
							| 
									
										
										
										
											2019-07-07 20:45:46 +08:00
										 |  |  |             'template' => false, | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $this->permissions->grantUserRolePermissions($editor, ['templates-manage']); | 
					
						
							| 
									
										
										
										
											2019-07-07 20:45:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->put($page->getUrl(), $pageUpdateData); | 
					
						
							|  |  |  |         $this->assertDatabaseHas('pages', [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'id'       => $page->id, | 
					
						
							| 
									
										
										
										
											2019-07-07 20:45:46 +08:00
										 |  |  |             'template' => true, | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 03:04:43 +08:00
										 |  |  |     public function test_templates_content_should_be_fetchable_only_if_page_marked_as_template() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $content = '<div>my_custom_template_content</div>'; | 
					
						
							| 
									
										
										
										
											2022-09-30 05:11:16 +08:00
										 |  |  |         $page = $this->entities->page(); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $editor = $this->users->editor(); | 
					
						
							| 
									
										
										
										
											2019-08-12 03:04:43 +08:00
										 |  |  |         $this->actingAs($editor); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $templateFetch = $this->get('/templates/' . $page->id); | 
					
						
							|  |  |  |         $templateFetch->assertStatus(404); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $page->html = $content; | 
					
						
							|  |  |  |         $page->template = true; | 
					
						
							|  |  |  |         $page->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $templateFetch = $this->get('/templates/' . $page->id); | 
					
						
							|  |  |  |         $templateFetch->assertStatus(200); | 
					
						
							|  |  |  |         $templateFetch->assertJson([ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'html'     => $content, | 
					
						
							| 
									
										
										
										
											2019-08-12 03:04:43 +08:00
										 |  |  |             'markdown' => '', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_template_endpoint_returns_paginated_list_of_templates() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $editor = $this->users->editor(); | 
					
						
							| 
									
										
										
										
											2019-08-12 03:04:43 +08:00
										 |  |  |         $this->actingAs($editor); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 03:10:27 +08:00
										 |  |  |         $toBeTemplates = Page::query()->orderBy('name', 'asc')->take(12)->get(); | 
					
						
							| 
									
										
										
										
											2019-08-12 03:04:43 +08:00
										 |  |  |         $page = $toBeTemplates->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $emptyTemplatesFetch = $this->get('/templates'); | 
					
						
							|  |  |  |         $emptyTemplatesFetch->assertDontSee($page->name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Page::query()->whereIn('id', $toBeTemplates->pluck('id')->toArray())->update(['template' => true]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $templatesFetch = $this->get('/templates'); | 
					
						
							|  |  |  |         $templatesFetch->assertSee($page->name); | 
					
						
							|  |  |  |         $templatesFetch->assertSee('pagination'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | } |