|
2 | 2 |
|
3 | 3 | namespace Tests\Exports;
|
4 | 4 |
|
| 5 | +use BookStack\Activity\ActivityType; |
| 6 | +use BookStack\Exports\Import; |
5 | 7 | use Illuminate\Http\UploadedFile;
|
6 | 8 | use Illuminate\Testing\TestResponse;
|
7 | 9 | use Tests\TestCase;
|
@@ -35,6 +37,25 @@ public function test_permissions_needed_for_import_page()
|
35 | 37 | $resp->assertSeeText('Select ZIP file to upload');
|
36 | 38 | }
|
37 | 39 |
|
| 40 | + public function test_import_page_pending_import_visibility_limited() |
| 41 | + { |
| 42 | + $user = $this->users->viewer(); |
| 43 | + $admin = $this->users->admin(); |
| 44 | + $userImport = Import::factory()->create(['name' => 'MySuperUserImport', 'created_by' => $user->id]); |
| 45 | + $adminImport = Import::factory()->create(['name' => 'MySuperAdminImport', 'created_by' => $admin->id]); |
| 46 | + $this->permissions->grantUserRolePermissions($user, ['content-import']); |
| 47 | + |
| 48 | + $resp = $this->actingAs($user)->get('/import'); |
| 49 | + $resp->assertSeeText('MySuperUserImport'); |
| 50 | + $resp->assertDontSeeText('MySuperAdminImport'); |
| 51 | + |
| 52 | + $this->permissions->grantUserRolePermissions($user, ['settings-manage']); |
| 53 | + |
| 54 | + $resp = $this->actingAs($user)->get('/import'); |
| 55 | + $resp->assertSeeText('MySuperUserImport'); |
| 56 | + $resp->assertSeeText('MySuperAdminImport'); |
| 57 | + } |
| 58 | + |
38 | 59 | public function test_zip_read_errors_are_shown_on_validation()
|
39 | 60 | {
|
40 | 61 | $invalidUpload = $this->files->uploadedImage('image.zip');
|
@@ -105,6 +126,125 @@ public function test_zip_data_validation_messages_shown()
|
105 | 126 | $resp->assertSeeText('[book.pages.1.tags.0.value]: The value must be a string.');
|
106 | 127 | }
|
107 | 128 |
|
| 129 | + public function test_import_upload_success() |
| 130 | + { |
| 131 | + $admin = $this->users->admin(); |
| 132 | + $this->actingAs($admin); |
| 133 | + $resp = $this->runImportFromFile($this->zipUploadFromData([ |
| 134 | + 'book' => [ |
| 135 | + 'name' => 'My great book name', |
| 136 | + 'chapters' => [ |
| 137 | + [ |
| 138 | + 'name' => 'my chapter', |
| 139 | + 'pages' => [ |
| 140 | + [ |
| 141 | + 'name' => 'my chapter page', |
| 142 | + ] |
| 143 | + ] |
| 144 | + ] |
| 145 | + ], |
| 146 | + 'pages' => [ |
| 147 | + [ |
| 148 | + 'name' => 'My page', |
| 149 | + ] |
| 150 | + ], |
| 151 | + ], |
| 152 | + ])); |
| 153 | + |
| 154 | + $this->assertDatabaseHas('imports', [ |
| 155 | + 'name' => 'My great book name', |
| 156 | + 'book_count' => 1, |
| 157 | + 'chapter_count' => 1, |
| 158 | + 'page_count' => 2, |
| 159 | + 'created_by' => $admin->id, |
| 160 | + ]); |
| 161 | + |
| 162 | + /** @var Import $import */ |
| 163 | + $import = Import::query()->latest()->first(); |
| 164 | + $resp->assertRedirect("/import/{$import->id}"); |
| 165 | + $this->assertFileExists(storage_path($import->path)); |
| 166 | + $this->assertActivityExists(ActivityType::IMPORT_CREATE); |
| 167 | + } |
| 168 | + |
| 169 | + public function test_import_show_page() |
| 170 | + { |
| 171 | + $import = Import::factory()->create(['name' => 'MySuperAdminImport']); |
| 172 | + |
| 173 | + $resp = $this->asAdmin()->get("/import/{$import->id}"); |
| 174 | + $resp->assertOk(); |
| 175 | + $resp->assertSee('MySuperAdminImport'); |
| 176 | + } |
| 177 | + |
| 178 | + public function test_import_show_page_access_limited() |
| 179 | + { |
| 180 | + $user = $this->users->viewer(); |
| 181 | + $admin = $this->users->admin(); |
| 182 | + $userImport = Import::factory()->create(['name' => 'MySuperUserImport', 'created_by' => $user->id]); |
| 183 | + $adminImport = Import::factory()->create(['name' => 'MySuperAdminImport', 'created_by' => $admin->id]); |
| 184 | + $this->actingAs($user); |
| 185 | + |
| 186 | + $this->get("/import/{$userImport->id}")->assertRedirect('/'); |
| 187 | + $this->get("/import/{$adminImport->id}")->assertRedirect('/'); |
| 188 | + |
| 189 | + $this->permissions->grantUserRolePermissions($user, ['content-import']); |
| 190 | + |
| 191 | + $this->get("/import/{$userImport->id}")->assertOk(); |
| 192 | + $this->get("/import/{$adminImport->id}")->assertStatus(404); |
| 193 | + |
| 194 | + $this->permissions->grantUserRolePermissions($user, ['settings-manage']); |
| 195 | + |
| 196 | + $this->get("/import/{$userImport->id}")->assertOk(); |
| 197 | + $this->get("/import/{$adminImport->id}")->assertOk(); |
| 198 | + } |
| 199 | + |
| 200 | + public function test_import_delete() |
| 201 | + { |
| 202 | + $this->asAdmin(); |
| 203 | + $this->runImportFromFile($this->zipUploadFromData([ |
| 204 | + 'book' => [ |
| 205 | + 'name' => 'My great book name' |
| 206 | + ], |
| 207 | + ])); |
| 208 | + |
| 209 | + /** @var Import $import */ |
| 210 | + $import = Import::query()->latest()->first(); |
| 211 | + $this->assertDatabaseHas('imports', [ |
| 212 | + 'id' => $import->id, |
| 213 | + 'name' => 'My great book name' |
| 214 | + ]); |
| 215 | + $this->assertFileExists(storage_path($import->path)); |
| 216 | + |
| 217 | + $resp = $this->delete("/import/{$import->id}"); |
| 218 | + |
| 219 | + $resp->assertRedirect('/import'); |
| 220 | + $this->assertActivityExists(ActivityType::IMPORT_DELETE); |
| 221 | + $this->assertDatabaseMissing('imports', [ |
| 222 | + 'id' => $import->id, |
| 223 | + ]); |
| 224 | + $this->assertFileDoesNotExist(storage_path($import->path)); |
| 225 | + } |
| 226 | + |
| 227 | + public function test_import_delete_access_limited() |
| 228 | + { |
| 229 | + $user = $this->users->viewer(); |
| 230 | + $admin = $this->users->admin(); |
| 231 | + $userImport = Import::factory()->create(['name' => 'MySuperUserImport', 'created_by' => $user->id]); |
| 232 | + $adminImport = Import::factory()->create(['name' => 'MySuperAdminImport', 'created_by' => $admin->id]); |
| 233 | + $this->actingAs($user); |
| 234 | + |
| 235 | + $this->delete("/import/{$userImport->id}")->assertRedirect('/'); |
| 236 | + $this->delete("/import/{$adminImport->id}")->assertRedirect('/'); |
| 237 | + |
| 238 | + $this->permissions->grantUserRolePermissions($user, ['content-import']); |
| 239 | + |
| 240 | + $this->delete("/import/{$userImport->id}")->assertRedirect('/import'); |
| 241 | + $this->delete("/import/{$adminImport->id}")->assertStatus(404); |
| 242 | + |
| 243 | + $this->permissions->grantUserRolePermissions($user, ['settings-manage']); |
| 244 | + |
| 245 | + $this->delete("/import/{$adminImport->id}")->assertRedirect('/import'); |
| 246 | + } |
| 247 | + |
108 | 248 | protected function runImportFromFile(UploadedFile $file): TestResponse
|
109 | 249 | {
|
110 | 250 | return $this->call('POST', '/import', [], [], ['file' => $file]);
|
|
0 commit comments