| 
									
										
										
										
											2024-11-16 21:57:41 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Tests\Exports; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use BookStack\Exports\Import; | 
					
						
							|  |  |  | use Illuminate\Http\UploadedFile; | 
					
						
							|  |  |  | use ZipArchive; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ZipTestHelper | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-17 00:12:45 +08:00
										 |  |  |     public static function importFromData(array $importData, array $zipData, array $files = []): Import | 
					
						
							| 
									
										
										
										
											2024-11-16 21:57:41 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (isset($zipData['book'])) { | 
					
						
							|  |  |  |             $importData['type'] = 'book'; | 
					
						
							|  |  |  |         } else if (isset($zipData['chapter'])) { | 
					
						
							|  |  |  |             $importData['type'] = 'chapter'; | 
					
						
							|  |  |  |         } else if (isset($zipData['page'])) { | 
					
						
							|  |  |  |             $importData['type'] = 'page'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $import = Import::factory()->create($importData); | 
					
						
							| 
									
										
										
										
											2024-11-17 00:12:45 +08:00
										 |  |  |         $zip = static::zipUploadFromData($zipData, $files); | 
					
						
							| 
									
										
										
										
											2024-11-26 00:23:59 +08:00
										 |  |  |         $targetPath = storage_path($import->path); | 
					
						
							|  |  |  |         $targetDir = dirname($targetPath); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!file_exists($targetDir)) { | 
					
						
							|  |  |  |             mkdir($targetDir); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         rename($zip->getRealPath(), $targetPath); | 
					
						
							| 
									
										
										
										
											2024-11-16 21:57:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $import; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function deleteZipForImport(Import $import): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $path = storage_path($import->path); | 
					
						
							|  |  |  |         if (file_exists($path)) { | 
					
						
							|  |  |  |             unlink($path); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-17 00:12:45 +08:00
										 |  |  |     public static function zipUploadFromData(array $data, array $files = []): UploadedFile | 
					
						
							| 
									
										
										
										
											2024-11-16 21:57:41 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $zipFile = tempnam(sys_get_temp_dir(), 'bstest-'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $zip = new ZipArchive(); | 
					
						
							|  |  |  |         $zip->open($zipFile, ZipArchive::CREATE); | 
					
						
							|  |  |  |         $zip->addFromString('data.json', json_encode($data)); | 
					
						
							| 
									
										
										
										
											2024-11-17 00:12:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         foreach ($files as $name => $file) { | 
					
						
							|  |  |  |             $zip->addFile($file, "files/$name"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-16 21:57:41 +08:00
										 |  |  |         $zip->close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return new UploadedFile($zipFile, 'upload.zip', 'application/zip', null, true); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |