| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Tests\Api; | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-31 03:55:24 +08:00
										 |  |  | use BookStack\Entities\Models\Book; | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Chapter; | 
					
						
							| 
									
										
										
										
											2022-04-05 00:24:05 +08:00
										 |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use Illuminate\Support\Facades\DB; | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  | use Tests\TestCase; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ChaptersApiTest extends TestCase | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     use TestsApi; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-05 00:24:05 +08:00
										 |  |  |     protected string $baseEndpoint = '/api/chapters'; | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function test_index_endpoint_returns_expected_chapter() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							|  |  |  |         $firstChapter = Chapter::query()->orderBy('id', 'asc')->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id'); | 
					
						
							|  |  |  |         $resp->assertJson(['data' => [ | 
					
						
							|  |  |  |             [ | 
					
						
							| 
									
										
										
										
											2024-01-16 20:06:13 +08:00
										 |  |  |                 'id'        => $firstChapter->id, | 
					
						
							|  |  |  |                 'name'      => $firstChapter->name, | 
					
						
							|  |  |  |                 'slug'      => $firstChapter->slug, | 
					
						
							|  |  |  |                 'book_id'   => $firstChapter->book->id, | 
					
						
							|  |  |  |                 'priority'  => $firstChapter->priority, | 
					
						
							|  |  |  |                 'book_slug' => $firstChapter->book->slug, | 
					
						
							| 
									
										
										
										
											2024-02-11 23:42:37 +08:00
										 |  |  |                 'owned_by'   => $firstChapter->owned_by, | 
					
						
							|  |  |  |                 'created_by' => $firstChapter->created_by, | 
					
						
							|  |  |  |                 'updated_by' => $firstChapter->updated_by, | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             ], | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         ]]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_create_endpoint() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2024-02-01 20:22:16 +08:00
										 |  |  |         $templatePage = $this->entities->templatePage(); | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         $details = [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'name'        => 'My API chapter', | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |             'description' => 'A chapter created via the API', | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'book_id'     => $book->id, | 
					
						
							|  |  |  |             'tags'        => [ | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |                 [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |                     'name'  => 'tagname', | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |                     'value' => 'tagvalue', | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2023-08-21 22:40:53 +08:00
										 |  |  |             'priority' => 15, | 
					
						
							| 
									
										
										
										
											2024-02-01 20:22:16 +08:00
										 |  |  |             'default_template_id' => $templatePage->id, | 
					
						
							| 
									
										
										
										
											2023-07-11 20:11:13 +08:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->postJson($this->baseEndpoint, $details); | 
					
						
							|  |  |  |         $resp->assertStatus(200); | 
					
						
							|  |  |  |         $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first(); | 
					
						
							| 
									
										
										
										
											2023-12-22 21:17:23 +08:00
										 |  |  |         $resp->assertJson(array_merge($details, [ | 
					
						
							|  |  |  |             'id' => $newItem->id, | 
					
						
							|  |  |  |             'slug' => $newItem->slug, | 
					
						
							|  |  |  |             'description_html' => '<p>A chapter created via the API</p>', | 
					
						
							|  |  |  |         ])); | 
					
						
							| 
									
										
										
										
											2023-07-11 20:11:13 +08:00
										 |  |  |         $this->assertDatabaseHas('tags', [ | 
					
						
							|  |  |  |             'entity_id'   => $newItem->id, | 
					
						
							|  |  |  |             'entity_type' => $newItem->getMorphClass(), | 
					
						
							|  |  |  |             'name'        => 'tagname', | 
					
						
							|  |  |  |             'value'       => 'tagvalue', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         $resp->assertJsonMissing(['pages' => []]); | 
					
						
							|  |  |  |         $this->assertActivityExists('chapter_create', $newItem); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-22 21:17:23 +08:00
										 |  |  |     public function test_create_endpoint_with_html() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							|  |  |  |         $book = $this->entities->book(); | 
					
						
							|  |  |  |         $details = [ | 
					
						
							|  |  |  |             'name'             => 'My API chapter', | 
					
						
							|  |  |  |             'description_html' => '<p>A chapter <strong>created</strong> via the API</p>', | 
					
						
							|  |  |  |             'book_id'          => $book->id, | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->postJson($this->baseEndpoint, $details); | 
					
						
							|  |  |  |         $resp->assertStatus(200); | 
					
						
							|  |  |  |         $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $expectedDetails = array_merge($details, [ | 
					
						
							|  |  |  |             'id'          => $newItem->id, | 
					
						
							|  |  |  |             'description' => 'A chapter created via the API', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         $resp->assertJson($expectedDetails); | 
					
						
							|  |  |  |         $this->assertDatabaseHas('chapters', $expectedDetails); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |     public function test_chapter_name_needed_to_create() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         $details = [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'book_id'     => $book->id, | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |             'description' => 'A chapter created via the API', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->postJson($this->baseEndpoint, $details); | 
					
						
							|  |  |  |         $resp->assertStatus(422); | 
					
						
							|  |  |  |         $resp->assertJson($this->validationResponse([ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'name' => ['The name field is required.'], | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         ])); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_chapter_book_id_needed_to_create() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							|  |  |  |         $details = [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'name'        => 'My api chapter', | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |             'description' => 'A chapter created via the API', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->postJson($this->baseEndpoint, $details); | 
					
						
							|  |  |  |         $resp->assertStatus(422); | 
					
						
							|  |  |  |         $resp->assertJson($this->validationResponse([ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'book_id' => ['The book id field is required.'], | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         ])); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_read_endpoint() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							| 
									
										
										
										
											2022-09-30 05:11:16 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         $page = $chapter->pages()->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}"); | 
					
						
							|  |  |  |         $resp->assertStatus(200); | 
					
						
							|  |  |  |         $resp->assertJson([ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'id'         => $chapter->id, | 
					
						
							|  |  |  |             'slug'       => $chapter->slug, | 
					
						
							| 
									
										
										
										
											2024-01-16 20:06:13 +08:00
										 |  |  |             'book_slug'  => $chapter->book->slug, | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |             'created_by' => [ | 
					
						
							|  |  |  |                 'name' => $chapter->createdBy->name, | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'book_id'    => $chapter->book_id, | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |             'updated_by' => [ | 
					
						
							|  |  |  |                 'name' => $chapter->createdBy->name, | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2021-01-04 06:29:58 +08:00
										 |  |  |             'owned_by' => [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |                 'name' => $chapter->ownedBy->name, | 
					
						
							| 
									
										
										
										
											2021-01-04 06:29:58 +08:00
										 |  |  |             ], | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |             'pages' => [ | 
					
						
							|  |  |  |                 [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |                     'id'   => $page->id, | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |                     'slug' => $page->slug, | 
					
						
							|  |  |  |                     'name' => $page->name, | 
					
						
							| 
									
										
										
										
											2024-02-11 23:42:37 +08:00
										 |  |  |                     'owned_by' => $page->owned_by, | 
					
						
							|  |  |  |                     'created_by' => $page->created_by, | 
					
						
							|  |  |  |                     'updated_by' => $page->updated_by, | 
					
						
							|  |  |  |                     'book_id' => $page->id, | 
					
						
							|  |  |  |                     'chapter_id' => $chapter->id, | 
					
						
							|  |  |  |                     'priority' => $page->priority, | 
					
						
							|  |  |  |                     'book_slug' => $chapter->book->slug, | 
					
						
							|  |  |  |                     'draft' => $page->draft, | 
					
						
							|  |  |  |                     'template' => $page->template, | 
					
						
							|  |  |  |                     'editor' => $page->editor, | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |             ], | 
					
						
							| 
									
										
										
										
											2024-02-01 20:22:16 +08:00
										 |  |  |             'default_template_id' => null, | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2024-01-16 20:06:13 +08:00
										 |  |  |         $resp->assertJsonMissingPath('book'); | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         $resp->assertJsonCount($chapter->pages()->count(), 'pages'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_update_endpoint() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							| 
									
										
										
										
											2022-09-30 05:11:16 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2024-02-01 20:22:16 +08:00
										 |  |  |         $templatePage = $this->entities->templatePage(); | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         $details = [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'name'        => 'My updated API chapter', | 
					
						
							| 
									
										
										
										
											2023-12-22 21:17:23 +08:00
										 |  |  |             'description' => 'A chapter updated via the API', | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'tags'        => [ | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |                 [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |                     'name'  => 'freshtag', | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |                     'value' => 'freshtagval', | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |             ], | 
					
						
							| 
									
										
										
										
											2023-07-11 20:11:13 +08:00
										 |  |  |             'priority'    => 15, | 
					
						
							| 
									
										
										
										
											2024-02-01 20:22:16 +08:00
										 |  |  |             'default_template_id' => $templatePage->id, | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details); | 
					
						
							|  |  |  |         $chapter->refresh(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp->assertStatus(200); | 
					
						
							|  |  |  |         $resp->assertJson(array_merge($details, [ | 
					
						
							| 
									
										
										
										
											2023-12-22 21:17:23 +08:00
										 |  |  |             'id' => $chapter->id, | 
					
						
							|  |  |  |             'slug' => $chapter->slug, | 
					
						
							|  |  |  |             'book_id' => $chapter->book_id, | 
					
						
							|  |  |  |             'description_html' => '<p>A chapter updated via the API</p>', | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         ])); | 
					
						
							|  |  |  |         $this->assertActivityExists('chapter_update', $chapter); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-22 21:17:23 +08:00
										 |  |  |     public function test_update_endpoint_with_html() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							|  |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							|  |  |  |         $details = [ | 
					
						
							|  |  |  |             'name'             => 'My updated API chapter', | 
					
						
							|  |  |  |             'description_html' => '<p>A chapter <em>updated</em> via the API</p>', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details); | 
					
						
							|  |  |  |         $resp->assertStatus(200); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertDatabaseHas('chapters', array_merge($details, [ | 
					
						
							|  |  |  |             'id' => $chapter->id, 'description' => 'A chapter updated via the API' | 
					
						
							|  |  |  |         ])); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-05 00:24:05 +08:00
										 |  |  |     public function test_update_increments_updated_date_if_only_tags_are_sent() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							| 
									
										
										
										
											2022-09-30 05:11:16 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2022-04-05 00:24:05 +08:00
										 |  |  |         DB::table('chapters')->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $details = [ | 
					
						
							| 
									
										
										
										
											2022-04-25 01:22:40 +08:00
										 |  |  |             'tags' => [['name' => 'Category', 'value' => 'Testing']], | 
					
						
							| 
									
										
										
										
											2022-04-05 00:24:05 +08:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details); | 
					
						
							|  |  |  |         $chapter->refresh(); | 
					
						
							|  |  |  |         $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $chapter->updated_at->unix()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-31 03:55:24 +08:00
										 |  |  |     public function test_update_with_book_id_moves_chapter() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							|  |  |  |         $chapter = $this->entities->chapterHasPages(); | 
					
						
							|  |  |  |         $page = $chapter->pages()->first(); | 
					
						
							|  |  |  |         $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]); | 
					
						
							|  |  |  |         $resp->assertOk(); | 
					
						
							|  |  |  |         $chapter->refresh(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertDatabaseHas('chapters', ['id' => $chapter->id, 'book_id' => $newBook->id]); | 
					
						
							|  |  |  |         $this->assertDatabaseHas('pages', ['id' => $page->id, 'book_id' => $newBook->id, 'chapter_id' => $chapter->id]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_update_with_new_book_id_requires_delete_permission() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $editor = $this->users->editor(); | 
					
						
							|  |  |  |         $this->permissions->removeUserRolePermissions($editor, ['chapter-delete-all', 'chapter-delete-own']); | 
					
						
							|  |  |  |         $this->actingAs($editor); | 
					
						
							|  |  |  |         $chapter = $this->entities->chapterHasPages(); | 
					
						
							|  |  |  |         $newBook = Book::query()->where('id', '!=', $chapter->book_id)->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]); | 
					
						
							|  |  |  |         $this->assertPermissionError($resp); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |     public function test_delete_endpoint() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							| 
									
										
										
										
											2022-09-30 05:11:16 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp->assertStatus(204); | 
					
						
							|  |  |  |         $this->assertActivityExists('chapter_delete'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_export_html_endpoint() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							| 
									
										
										
										
											2022-09-30 05:11:16 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html"); | 
					
						
							|  |  |  |         $resp->assertStatus(200); | 
					
						
							|  |  |  |         $resp->assertSee($chapter->name); | 
					
						
							|  |  |  |         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_export_plain_text_endpoint() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							| 
									
										
										
										
											2022-09-30 05:11:16 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext"); | 
					
						
							|  |  |  |         $resp->assertStatus(200); | 
					
						
							|  |  |  |         $resp->assertSee($chapter->name); | 
					
						
							|  |  |  |         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_export_pdf_endpoint() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							| 
									
										
										
										
											2022-09-30 05:11:16 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf"); | 
					
						
							|  |  |  |         $resp->assertStatus(200); | 
					
						
							|  |  |  |         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-23 04:32:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function test_export_markdown_endpoint() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							|  |  |  |         $chapter = Chapter::visible()->has('pages')->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown"); | 
					
						
							|  |  |  |         $resp->assertStatus(200); | 
					
						
							|  |  |  |         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"'); | 
					
						
							|  |  |  |         $resp->assertSee('# ' . $chapter->name); | 
					
						
							|  |  |  |         $resp->assertSee('# ' . $chapter->pages()->first()->name); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-08-29 04:48:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function test_cant_export_when_not_have_permission() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $types = ['html', 'plaintext', 'pdf', 'markdown']; | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']); | 
					
						
							| 
									
										
										
										
											2021-08-29 04:48:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $chapter = Chapter::visible()->has('pages')->first(); | 
					
						
							|  |  |  |         foreach ($types as $type) { | 
					
						
							|  |  |  |             $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}"); | 
					
						
							|  |  |  |             $this->assertPermissionError($resp); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | } |