From 6313b8e7e6579998de50c873a97072b64de5358d Mon Sep 17 00:00:00 2001 From: ANKUR DWIVEDI Date: Fri, 23 Aug 2024 14:01:38 +0530 Subject: [PATCH] added publish --- README.md | 22 +++++++++ src/ImageKit/Constants/ErrorMessages.php | 1 + src/ImageKit/ImageKit.php | 11 +++-- tests/ImageKit/Manage/FileTest.php | 63 ++++++++++++++++++++++++ tests/ImageKit/Upload/UploadTest.php | 27 +++++++++- 5 files changed, 118 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 20e20df..68d69b8 100644 --- a/README.md +++ b/README.md @@ -660,6 +660,7 @@ $uploadFile = $imageKit->uploadFile([ ] ], 'checks' => '"file.size" < "1mb"', // optional `checks` parameters can be used to run server-side checks before files are uploaded to the Media Library. + 'isPublished' => true, // "customMetadata" => [ // "SKU" => "VS882HJ2JD", // "price" => 599.99, @@ -776,6 +777,27 @@ $updateFileDetails = $imageKit->updateFileDetails( ); ``` +**Update publish status** + +If any parameter other than `publish` is included in the update options, an error will be returned: `Your request cannot contain any other parameters when publish is present.`. + +#### Example +```php +// Update parameters +$updateData = [ + "publish" => [ + "isPublished" => true, + "includeFileVersions" => true + ] +]; + +// Attempt Update +$updateFileDetails = $imageKit->updateFileDetails( + 'file_id', + $updateData +); +``` + ### 6. Add Tags (Bulk) API Add tags to multiple files in a single request. The method accepts an array of `fileIds` of the files and an array of `tags` that have to be added to those files. diff --git a/src/ImageKit/Constants/ErrorMessages.php b/src/ImageKit/Constants/ErrorMessages.php index db0f412..835751d 100644 --- a/src/ImageKit/Constants/ErrorMessages.php +++ b/src/ImageKit/Constants/ErrorMessages.php @@ -50,6 +50,7 @@ class ErrorMessages public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_PRE_TRANSFORMATION = [ "message" => "Invalid pre transformation parameter.", "help" => "For support kindly contact us at support@imagekit.io ."]; public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_POST_TRANSFORMATION = [ "message" => "Invalid post transformation parameter.", "help" => "For support kindly contact us at support@imagekit.io ."]; public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_CHECKS = [ "message" => "The value provided for the checks parameter is invalid.", "help" => "For support kindly contact us at support@imagekit.io ."]; + public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_PUBLISH_STATUS = [ "message" => "isPublished must be boolean.", "help" => "For support kindly contact us at support@imagekit.io ."]; public static $MISSING_UPLOAD_DATA = ['message' => 'Missing data for upload', 'help' => 'For support kindly contact us at support@imagekit.io .']; public static $MISSING_UPLOAD_FILE_PARAMETER = ['message' => 'Missing file parameter for upload', 'help' => 'For support kindly contact us at support@imagekit.io .']; public static $MISSING_UPLOAD_FILENAME_PARAMETER = ['message' => 'Missing fileName parameter for upload', 'help' => 'For support kindly contact us at support@imagekit.io .']; diff --git a/src/ImageKit/ImageKit.php b/src/ImageKit/ImageKit.php index 7105910..3308934 100644 --- a/src/ImageKit/ImageKit.php +++ b/src/ImageKit/ImageKit.php @@ -216,12 +216,13 @@ public function uploadFile($options=null) return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_POST_TRANSFORMATION)); } } - if(isset($options['checks']) && !is_string($options['checks'])){ - return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_CHECKS)); - } } - - + if(isset($options['checks']) && !is_string($options['checks'])){ + return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_CHECKS)); + } + if(isset($options['isPublished']) && !is_bool($options['isPublished'])){ + return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_PUBLISH_STATUS)); + } $this->httpClient->setUri(Endpoints::getUploadFileEndpoint()); return Upload::upload($options, $this->httpClient); } diff --git a/tests/ImageKit/Manage/FileTest.php b/tests/ImageKit/Manage/FileTest.php index 7f81c96..d53ecfd 100644 --- a/tests/ImageKit/Manage/FileTest.php +++ b/tests/ImageKit/Manage/FileTest.php @@ -517,6 +517,69 @@ public function testUpdateFileDetails() FileTest::assertEquals($requestMethod,'PATCH'); } + /** + * + */ + public function testUpdateFilePublishStatus() + { + $fileId = '5df36759adf3f523d81dd94f'; + + $updateData = [ + "publish" => [ + "isPublished" => true, + "includeFileVersions" => true + ] + ]; + + $responseBody = [ + 'fileId' => '598821f949c0a938d57563bd', + 'type' => 'file', + 'name' => 'file1.jpg', + 'filePath' => '/images/products/file1.jpg', + 'tags' => ['t-shirt', 'round-neck', 'sale2019'], + 'isPrivateFile' => false, + 'isPublished' => true, + 'customCoordinates' => null, + 'url' => 'https://ik.imagekit.io/your_imagekit_id/images/products/file1.jpg', + 'thumbnail' => 'https://ik.imagekit.io/your_imagekit_id/tr:n-media_library_thumbnail/images/products/file1.jpg', + 'fileType' => 'image' + ]; + + $mockBodyResponse = Utils::streamFor(json_encode($responseBody)); + + $mock = new MockHandler([ + new Response(200, ['X-Foo' => 'Bar'], $mockBodyResponse) + ]); + + $handlerStack = HandlerStack::create($mock); + + $container = []; + $history = Middleware::history($container); + + $handlerStack->push($history); + + $this->createMockClient($handlerStack); + + $response = $this->mockClient->updateFileDetails($fileId, $updateData); + + $request = $container[0]['request']; + $requestPath = $request->getUri()->getPath(); + $requestBody = $request->getBody(); + $stream = Utils::streamFor($requestBody)->getContents(); + + // Request Check + FileTest::assertEquals("/v1/files/{$fileId}/details",$requestPath); + FileTest::assertEquals($stream,json_encode($updateData)); + + // Response Check + FileTest::assertNull($response->error); + FileTest::assertEquals(json_encode($responseBody), json_encode($response->result)); + + // Assert Method + $requestMethod = $container[0]['request']->getMethod(); + FileTest::assertEquals($requestMethod,'PATCH'); + } + public function testUpdateFileDetailsWithInvalidTags() { $fileId = '5df36759adf3f523d81dd94f'; diff --git a/tests/ImageKit/Upload/UploadTest.php b/tests/ImageKit/Upload/UploadTest.php index 16e36fb..8c8b1df 100644 --- a/tests/ImageKit/Upload/UploadTest.php +++ b/tests/ImageKit/Upload/UploadTest.php @@ -172,7 +172,8 @@ public function testFileUploadIfSuccessful() ] ] ], - 'checks' => "'request.folder' : '/sample-folder'" + 'checks' => "'request.folder' : '/sample-folder'", + 'isPublished' => true ]; $mockBodyResponse = Utils::streamFor(json_encode($this->uploadSuccessResponseObj)); @@ -217,6 +218,7 @@ public function testFileUploadIfSuccessful() $this->checkFormData($stream,$boundary,"customMetadata",json_encode($fileOptions['customMetadata'])); $this->checkFormData($stream,$boundary,"transformation",json_encode($fileOptions['transformation'])); $this->checkFormData($stream,$boundary,"checks",$fileOptions['checks']); + $this->checkFormData($stream,$boundary,"isPublished","true"); // Assert Method $requestMethod = $container[0]['request']->getMethod(); @@ -814,6 +816,29 @@ public function testFileUploadWithInvalidChecks() UploadTest::assertEquals(json_encode($error),json_encode($response->error)); } + public function testFileUploadWithInvalidPublishStatus() + { + $fileOptions = [ + 'file' => 'http://lorempixel.com/640/480/', + 'fileName' => 'test_file_name', + "useUniqueFileName" => true, // true|false + "responseFields" => implode(",", ["tags", "customMetadata"]), // Comma Separated, check docs for more responseFields + 'isPublished' => '' + ]; + + $error = [ + "message" => "isPublished must be boolean.", + "help" => "For support kindly contact us at support@imagekit.io ." + ]; + + $this->stubHttpClient(new Response(403, ['X-Foo' => 'Bar'], json_encode($error))); + + $response = $this->client->uploadFile($fileOptions); + + // Request Body Check + UploadTest::assertEquals(json_encode($error),json_encode($response->error)); + } + protected function setUp() { $this->client = new ImageKit(