From c45448e8a2c5ff66b07efec87f3da4c7ecc4d93d Mon Sep 17 00:00:00 2001 From: DE-dylan-lauzon Date: Wed, 13 Nov 2024 10:58:02 -0500 Subject: [PATCH 1/7] feat(apit)!: added endpoint for batch source string editing. --- src/CrowdinApiClient/Api/SourceStringApi.php | 18 +++++++++ .../Api/SourceStringApiTest.php | 39 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/CrowdinApiClient/Api/SourceStringApi.php b/src/CrowdinApiClient/Api/SourceStringApi.php index a381af0b..89dc6d1f 100644 --- a/src/CrowdinApiClient/Api/SourceStringApi.php +++ b/src/CrowdinApiClient/Api/SourceStringApi.php @@ -92,4 +92,22 @@ public function delete(int $projectId, int $stringId) $path = sprintf('projects/%d/strings/%d', $projectId, $stringId); return $this->_delete($path); } + + /** + * Batch Update Strings + * @link https://support.crowdin.com/developer/api/v2/#tag/Source-Strings/operation/api.projects.strings.batchPatch API Documentation + * @link https://support.crowdin.com/developer/enterprise/api/v2/#tag/Source-Strings/operation/api.projects.strings.batchPatch API Documentation Enterprise + * + * @param int $projectId + * @param array $data + * string $data[op] required Patch operation to perform (replace, add, remove)
+ * string $data[path] required + * value $data[value] required object or string or bool. + * @return mixed + */ + public function batch(int $projectId, array $data) + { + $path = sprintf('projects/%d/strings', $projectId); + return $this->_patch($path, SourceString::class, $data); + } } diff --git a/tests/CrowdinApiClient/Api/SourceStringApiTest.php b/tests/CrowdinApiClient/Api/SourceStringApiTest.php index 8dec7c76..f887c376 100644 --- a/tests/CrowdinApiClient/Api/SourceStringApiTest.php +++ b/tests/CrowdinApiClient/Api/SourceStringApiTest.php @@ -155,4 +155,43 @@ public function testDelete() $this->crowdin->sourceString->delete(2, 2814); } + + public function testBatch() + { + $this->mockRequest([ + + 'path' => '/projects/2/strings', + 'method' => 'patch', + ]); + + $batchResult = $this->crowdin->sourceString->batch(2, [ + [ + 'op' => 'replace', + 'path' => '/2814/isHidden', + 'value' => true + ], + [ + 'op' => 'replace', + 'path' => '/2814/context', + 'value' => 'some context' + ], + [ + 'op' => 'add', + 'path' => '/-', + 'value' => [ + 'text' => 'new added string', + 'identifier' => 'a.b.c', + 'context' => 'context for new string', + 'fileId' => 5, + 'isHidden' => false + ] + ], + [ + 'op' => 'remove', + 'path' => '/2815' + ] + ]); + + $this->assertInstanceOf(SourceString::class, $batchResult); + } } From e2889caa90a3119e16c5f0f8ae9bf17761077716 Mon Sep 17 00:00:00 2001 From: DE-dylan-lauzon Date: Wed, 13 Nov 2024 12:04:04 -0500 Subject: [PATCH 2/7] Updated unit test with actual response data from successful API call. --- .../Api/SourceStringApiTest.php | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/tests/CrowdinApiClient/Api/SourceStringApiTest.php b/tests/CrowdinApiClient/Api/SourceStringApiTest.php index f887c376..42d31bda 100644 --- a/tests/CrowdinApiClient/Api/SourceStringApiTest.php +++ b/tests/CrowdinApiClient/Api/SourceStringApiTest.php @@ -159,9 +159,37 @@ public function testDelete() public function testBatch() { $this->mockRequest([ - 'path' => '/projects/2/strings', 'method' => 'patch', + 'response' => '{ + "data":[ + {"data": + { + "id":2814, + "projectId": 2, + "branchId":null, + "identifier":"a.b.c", + "text":"new added string", + "type":"text", + "context":"a.b.c\ncontext for new string", + "maxLength":0, + "isHidden":false, + "isDuplicate":false, + "masterStringId":null, + "hasPlurals":false, + "isIcu":false, + "labelIds":[], + "webUrl":"https://example.crowdin.com/editor/1/all/en-pl?filter=basic&value=0&view=comfortable#2", + "createdAt":"2024-11-13T16:56:18+00:00", + "updatedAt":null, + "fileId":48, + "directoryId":null, + "revision":1 + } + } + ] + } + ' ]); $batchResult = $this->crowdin->sourceString->batch(2, [ @@ -182,16 +210,17 @@ public function testBatch() 'text' => 'new added string', 'identifier' => 'a.b.c', 'context' => 'context for new string', - 'fileId' => 5, + 'fileId' => 8, 'isHidden' => false ] ], [ 'op' => 'remove', - 'path' => '/2815' + 'path' => '/2814' ] ]); + fwrite(STDERR, print_r($batchResult->getData(), TRUE)); $this->assertInstanceOf(SourceString::class, $batchResult); } } From b3bbf6f755c5148b1e23a541e7913ad3c17070ed Mon Sep 17 00:00:00 2001 From: DE-dylan-lauzon Date: Thu, 14 Nov 2024 08:53:54 -0500 Subject: [PATCH 3/7] Update src/CrowdinApiClient/Api/SourceStringApi.php Co-authored-by: Andrii Bodnar --- src/CrowdinApiClient/Api/SourceStringApi.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CrowdinApiClient/Api/SourceStringApi.php b/src/CrowdinApiClient/Api/SourceStringApi.php index 89dc6d1f..56170057 100644 --- a/src/CrowdinApiClient/Api/SourceStringApi.php +++ b/src/CrowdinApiClient/Api/SourceStringApi.php @@ -105,7 +105,7 @@ public function delete(int $projectId, int $stringId) * value $data[value] required object or string or bool. * @return mixed */ - public function batch(int $projectId, array $data) + public function batchOperations(int $projectId, array $data) { $path = sprintf('projects/%d/strings', $projectId); return $this->_patch($path, SourceString::class, $data); From 548dffe086224e19a473d9a4d157359892b628ed Mon Sep 17 00:00:00 2001 From: DE-dylan-lauzon Date: Thu, 14 Nov 2024 08:54:10 -0500 Subject: [PATCH 4/7] Update src/CrowdinApiClient/Api/SourceStringApi.php Co-authored-by: Andrii Bodnar --- src/CrowdinApiClient/Api/SourceStringApi.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CrowdinApiClient/Api/SourceStringApi.php b/src/CrowdinApiClient/Api/SourceStringApi.php index 56170057..caa34ee9 100644 --- a/src/CrowdinApiClient/Api/SourceStringApi.php +++ b/src/CrowdinApiClient/Api/SourceStringApi.php @@ -94,7 +94,7 @@ public function delete(int $projectId, int $stringId) } /** - * Batch Update Strings + * String Batch Operations * @link https://support.crowdin.com/developer/api/v2/#tag/Source-Strings/operation/api.projects.strings.batchPatch API Documentation * @link https://support.crowdin.com/developer/enterprise/api/v2/#tag/Source-Strings/operation/api.projects.strings.batchPatch API Documentation Enterprise * From 6d86bf5b15dacb37a7b295d295cd16255080a748 Mon Sep 17 00:00:00 2001 From: DE-dylan-lauzon Date: Thu, 14 Nov 2024 08:54:22 -0500 Subject: [PATCH 5/7] Update src/CrowdinApiClient/Api/SourceStringApi.php Co-authored-by: Andrii Bodnar --- src/CrowdinApiClient/Api/SourceStringApi.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CrowdinApiClient/Api/SourceStringApi.php b/src/CrowdinApiClient/Api/SourceStringApi.php index caa34ee9..6dae6776 100644 --- a/src/CrowdinApiClient/Api/SourceStringApi.php +++ b/src/CrowdinApiClient/Api/SourceStringApi.php @@ -102,7 +102,7 @@ public function delete(int $projectId, int $stringId) * @param array $data * string $data[op] required Patch operation to perform (replace, add, remove)
* string $data[path] required - * value $data[value] required object or string or bool. + * value $data[value] required object, string, int, or bool * @return mixed */ public function batchOperations(int $projectId, array $data) From e52b8e5211f42c064ebca25c80adb76df3448b7d Mon Sep 17 00:00:00 2001 From: DDEV User Date: Thu, 14 Nov 2024 14:02:25 +0000 Subject: [PATCH 6/7] cleaned up json formatting (matched structure of testList, removed debug line --- .gitignore | 2 ++ .../CrowdinApiClient/Api/SourceStringApiTest.php | 16 +++++++--------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 5e2b2b20..a3e49b5d 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ coverage.xml phpdoc phpDocumentor.phar .phpunit.result.cache +/.ddev +/.vscode \ No newline at end of file diff --git a/tests/CrowdinApiClient/Api/SourceStringApiTest.php b/tests/CrowdinApiClient/Api/SourceStringApiTest.php index 42d31bda..677a1d17 100644 --- a/tests/CrowdinApiClient/Api/SourceStringApiTest.php +++ b/tests/CrowdinApiClient/Api/SourceStringApiTest.php @@ -162,9 +162,9 @@ public function testBatch() 'path' => '/projects/2/strings', 'method' => 'patch', 'response' => '{ - "data":[ - {"data": - { + "data":[ + { + "data":{ "id":2814, "projectId": 2, "branchId":null, @@ -185,14 +185,13 @@ public function testBatch() "fileId":48, "directoryId":null, "revision":1 - } } - ] - } - ' + } + ] + }' ]); - $batchResult = $this->crowdin->sourceString->batch(2, [ + $batchResult = $this->crowdin->sourceString->batchOperations(2, [ [ 'op' => 'replace', 'path' => '/2814/isHidden', @@ -220,7 +219,6 @@ public function testBatch() ] ]); - fwrite(STDERR, print_r($batchResult->getData(), TRUE)); $this->assertInstanceOf(SourceString::class, $batchResult); } } From a93d9ecf027698d99961998c9333c23502b76759 Mon Sep 17 00:00:00 2001 From: DE-dylan-lauzon Date: Thu, 14 Nov 2024 10:56:29 -0500 Subject: [PATCH 7/7] Ran php-cs-fixer locally, found a bit of extra whitespace that was missing. --- src/CrowdinApiClient/Api/SourceStringApi.php | 2 +- tests/CrowdinApiClient/Api/SourceStringApiTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/CrowdinApiClient/Api/SourceStringApi.php b/src/CrowdinApiClient/Api/SourceStringApi.php index 6dae6776..e84abc5c 100644 --- a/src/CrowdinApiClient/Api/SourceStringApi.php +++ b/src/CrowdinApiClient/Api/SourceStringApi.php @@ -101,7 +101,7 @@ public function delete(int $projectId, int $stringId) * @param int $projectId * @param array $data * string $data[op] required Patch operation to perform (replace, add, remove)
- * string $data[path] required + * string $data[path] required * value $data[value] required object, string, int, or bool * @return mixed */ diff --git a/tests/CrowdinApiClient/Api/SourceStringApiTest.php b/tests/CrowdinApiClient/Api/SourceStringApiTest.php index 677a1d17..8d4a8182 100644 --- a/tests/CrowdinApiClient/Api/SourceStringApiTest.php +++ b/tests/CrowdinApiClient/Api/SourceStringApiTest.php @@ -156,7 +156,7 @@ public function testDelete() $this->crowdin->sourceString->delete(2, 2814); } - public function testBatch() + public function testBatch() { $this->mockRequest([ 'path' => '/projects/2/strings', @@ -191,7 +191,7 @@ public function testBatch() }' ]); - $batchResult = $this->crowdin->sourceString->batchOperations(2, [ + $batchResult = $this->crowdin->sourceString->batchOperations(2, [ [ 'op' => 'replace', 'path' => '/2814/isHidden', @@ -219,6 +219,6 @@ public function testBatch() ] ]); - $this->assertInstanceOf(SourceString::class, $batchResult); + $this->assertInstanceOf(SourceString::class, $batchResult); } }