From fdb33dc8b01686877c93610d3839e21562910554 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Thu, 5 Sep 2024 13:24:41 +0200 Subject: [PATCH 01/24] feat: use fork for web-tools --- composer.json | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index f3a47cde2..abecb0cc2 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "require": { "php": ">=7.4", "bedita/i18n": "^4.4.3", - "bedita/web-tools": "^3.10.1", + "bedita/web-tools": "dev-feat/new-bedita-client", "cakephp/authentication": "^2.9", "cakephp/cakephp": "~4.5.0", "cakephp/plugin-installer": "^1.3", @@ -75,6 +75,7 @@ "coverage": "vendor/bin/phpunit --colors=always --coverage-html coverage" }, "prefer-stable": true, + "minimum-stability": "dev", "config": { "sort-packages": true, "allow-plugins": { @@ -99,5 +100,15 @@ "merge-extra-deep": false, "merge-scripts": false } - } + }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/didoda/php-sdk" + }, + { + "type": "vcs", + "url": "https://github.com/didoda/web-tools" + } + ] } From 3ecf97fe0d60d881ffce2de12de9f697c0b7772d Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Thu, 5 Sep 2024 14:57:56 +0200 Subject: [PATCH 02/24] test --- composer.json | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/composer.json b/composer.json index abecb0cc2..f3a47cde2 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "require": { "php": ">=7.4", "bedita/i18n": "^4.4.3", - "bedita/web-tools": "dev-feat/new-bedita-client", + "bedita/web-tools": "^3.10.1", "cakephp/authentication": "^2.9", "cakephp/cakephp": "~4.5.0", "cakephp/plugin-installer": "^1.3", @@ -75,7 +75,6 @@ "coverage": "vendor/bin/phpunit --colors=always --coverage-html coverage" }, "prefer-stable": true, - "minimum-stability": "dev", "config": { "sort-packages": true, "allow-plugins": { @@ -100,15 +99,5 @@ "merge-extra-deep": false, "merge-scripts": false } - }, - "repositories": [ - { - "type": "vcs", - "url": "https://github.com/didoda/php-sdk" - }, - { - "type": "vcs", - "url": "https://github.com/didoda/web-tools" - } - ] + } } From 7b2388fdc0a25ffdd027b5df62a78771e39fa413 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Thu, 5 Sep 2024 15:00:19 +0200 Subject: [PATCH 03/24] test --- composer.json | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f3a47cde2..161fb62b3 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "php": ">=7.4", "bedita/i18n": "^4.4.3", "bedita/web-tools": "^3.10.1", + "bedita/web-tools": "dev-feat/new-bedita-client", "cakephp/authentication": "^2.9", "cakephp/cakephp": "~4.5.0", "cakephp/plugin-installer": "^1.3", @@ -75,6 +76,7 @@ "coverage": "vendor/bin/phpunit --colors=always --coverage-html coverage" }, "prefer-stable": true, + "minimum-stability": "dev", "config": { "sort-packages": true, "allow-plugins": { @@ -99,5 +101,15 @@ "merge-extra-deep": false, "merge-scripts": false } - } + }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/didoda/php-sdk" + }, + { + "type": "vcs", + "url": "https://github.com/didoda/web-tools" + } + ] } From 06104027da66ebf558e44da19f4f014e5c6589ef Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Thu, 5 Sep 2024 15:04:49 +0200 Subject: [PATCH 04/24] test --- composer.json | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/composer.json b/composer.json index 161fb62b3..f3a47cde2 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,6 @@ "php": ">=7.4", "bedita/i18n": "^4.4.3", "bedita/web-tools": "^3.10.1", - "bedita/web-tools": "dev-feat/new-bedita-client", "cakephp/authentication": "^2.9", "cakephp/cakephp": "~4.5.0", "cakephp/plugin-installer": "^1.3", @@ -76,7 +75,6 @@ "coverage": "vendor/bin/phpunit --colors=always --coverage-html coverage" }, "prefer-stable": true, - "minimum-stability": "dev", "config": { "sort-packages": true, "allow-plugins": { @@ -101,15 +99,5 @@ "merge-extra-deep": false, "merge-scripts": false } - }, - "repositories": [ - { - "type": "vcs", - "url": "https://github.com/didoda/php-sdk" - }, - { - "type": "vcs", - "url": "https://github.com/didoda/web-tools" - } - ] + } } From 5b4c2326fe785376365cdac67d7e96a116369589 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Thu, 5 Sep 2024 15:06:19 +0200 Subject: [PATCH 05/24] test --- .github/workflows/php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index e2c522e22..4a0fb3881 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -11,7 +11,7 @@ on: paths: - '**/*.php' - '.github/workflows/php.yml' - - 'composer.json' + - 'composer.json' # test - 'psalm.xml' jobs: From bd4f0be4b717b5d02354edee413fee31c3aea8d0 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Thu, 5 Sep 2024 15:07:42 +0200 Subject: [PATCH 06/24] test --- composer.json | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index f3a47cde2..abecb0cc2 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "require": { "php": ">=7.4", "bedita/i18n": "^4.4.3", - "bedita/web-tools": "^3.10.1", + "bedita/web-tools": "dev-feat/new-bedita-client", "cakephp/authentication": "^2.9", "cakephp/cakephp": "~4.5.0", "cakephp/plugin-installer": "^1.3", @@ -75,6 +75,7 @@ "coverage": "vendor/bin/phpunit --colors=always --coverage-html coverage" }, "prefer-stable": true, + "minimum-stability": "dev", "config": { "sort-packages": true, "allow-plugins": { @@ -99,5 +100,15 @@ "merge-extra-deep": false, "merge-scripts": false } - } + }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/didoda/php-sdk" + }, + { + "type": "vcs", + "url": "https://github.com/didoda/web-tools" + } + ] } From 636eef84bbdedb97e5a3334f761d02a6ca60a081 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Thu, 5 Sep 2024 15:12:07 +0200 Subject: [PATCH 07/24] test --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index abecb0cc2..a72521e2f 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "require": { "php": ">=7.4", "bedita/i18n": "^4.4.3", + "bedita/php-sdk": "dev-refactor/bedita-client", "bedita/web-tools": "dev-feat/new-bedita-client", "cakephp/authentication": "^2.9", "cakephp/cakephp": "~4.5.0", From 0089aab66c80bb296ca9151c4c8931a6d3890601 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Fri, 6 Sep 2024 11:17:05 +0200 Subject: [PATCH 08/24] test --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index a72521e2f..abecb0cc2 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,6 @@ "require": { "php": ">=7.4", "bedita/i18n": "^4.4.3", - "bedita/php-sdk": "dev-refactor/bedita-client", "bedita/web-tools": "dev-feat/new-bedita-client", "cakephp/authentication": "^2.9", "cakephp/cakephp": "~4.5.0", From d401e3e31097a8a3baf98bcdad4d8e0a1a7debcb Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Mon, 9 Sep 2024 14:49:55 +0200 Subject: [PATCH 09/24] test --- composer.json | 4 ---- 1 file changed, 4 deletions(-) diff --git a/composer.json b/composer.json index abecb0cc2..d13305811 100644 --- a/composer.json +++ b/composer.json @@ -102,10 +102,6 @@ } }, "repositories": [ - { - "type": "vcs", - "url": "https://github.com/didoda/php-sdk" - }, { "type": "vcs", "url": "https://github.com/didoda/web-tools" From 00a078fc29fd9ea870d1fb123fca2dc4bde9083f Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Mon, 9 Sep 2024 14:56:54 +0200 Subject: [PATCH 10/24] fix: web-tools 4.0.0 --- composer.json | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index d13305811..315259994 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "require": { "php": ">=7.4", "bedita/i18n": "^4.4.3", - "bedita/web-tools": "dev-feat/new-bedita-client", + "bedita/web-tools": "^4.0.0", "cakephp/authentication": "^2.9", "cakephp/cakephp": "~4.5.0", "cakephp/plugin-installer": "^1.3", @@ -100,11 +100,5 @@ "merge-extra-deep": false, "merge-scripts": false } - }, - "repositories": [ - { - "type": "vcs", - "url": "https://github.com/didoda/web-tools" - } - ] + } } From a5a68dc0d26b5874ac195dbefc8e9f02cc55874f Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Mon, 9 Sep 2024 15:11:09 +0200 Subject: [PATCH 11/24] fix: test --- tests/TestCase/Controller/AppControllerTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/TestCase/Controller/AppControllerTest.php b/tests/TestCase/Controller/AppControllerTest.php index 86ab55f2e..6e4d83b4d 100644 --- a/tests/TestCase/Controller/AppControllerTest.php +++ b/tests/TestCase/Controller/AppControllerTest.php @@ -195,11 +195,9 @@ public function testBeforeFilterCorrectTokens(): void /** @var \Authentication\Identity|null $user */ $user = $this->AppController->Authentication->getIdentity(); $expectedtokens = $user->get('tokens'); - - $event = $this->AppController->dispatchEvent('Controller.initialize'); - + $this->AppController->dispatchEvent('Controller.initialize'); $apiClient = $this->accessProperty($this->AppController, 'apiClient'); - $apiClientTokens = $this->accessProperty($apiClient, 'tokens'); + $apiClientTokens = $apiClient->getTokens(); static::assertEquals($expectedtokens, $apiClientTokens); } From 8152b2b14027e40ab662667da5546e8270c8efe1 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Mon, 9 Sep 2024 16:08:57 +0200 Subject: [PATCH 12/24] fix: bedita web-tools 4.0.1 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 315259994..e2aeab863 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "require": { "php": ">=7.4", "bedita/i18n": "^4.4.3", - "bedita/web-tools": "^4.0.0", + "bedita/web-tools": "^4.0.1", "cakephp/authentication": "^2.9", "cakephp/cakephp": "~4.5.0", "cakephp/plugin-installer": "^1.3", From 758fe9b01403db134c9eab70dd51d874b24fa558 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Mon, 9 Sep 2024 17:41:37 +0200 Subject: [PATCH 13/24] chore fix: remove comment --- .github/workflows/php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 4a0fb3881..e2c522e22 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -11,7 +11,7 @@ on: paths: - '**/*.php' - '.github/workflows/php.yml' - - 'composer.json' # test + - 'composer.json' - 'psalm.xml' jobs: From e9ad7a316bc19b3c52e3e38e8789c21626d0b794 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Mon, 16 Sep 2024 16:18:41 +0200 Subject: [PATCH 14/24] refactor: multi delete and restore --- composer.json | 2 +- src/Controller/ModulesController.php | 22 ++++---- src/Controller/TrashController.php | 81 ++++++++++++++-------------- src/Utility/PermissionsTrait.php | 4 +- 4 files changed, 54 insertions(+), 55 deletions(-) diff --git a/composer.json b/composer.json index e2aeab863..008548d20 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "require": { "php": ">=7.4", "bedita/i18n": "^4.4.3", - "bedita/web-tools": "^4.0.1", + "bedita/web-tools": "^4.0.2", "cakephp/authentication": "^2.9", "cakephp/cakephp": "~4.5.0", "cakephp/plugin-installer": "^1.3", diff --git a/src/Controller/ModulesController.php b/src/Controller/ModulesController.php index db7e342c5..831d06dd9 100644 --- a/src/Controller/ModulesController.php +++ b/src/Controller/ModulesController.php @@ -387,20 +387,20 @@ public function delete(): ?Response } elseif (!empty($this->getRequest()->getData('id'))) { $ids = [$this->getRequest()->getData('id')]; } - foreach ($ids as $id) { - try { - $this->apiClient->deleteObject($id, $this->objectType); + try { + $this->apiClient->deleteObjects($ids, $this->objectType); + foreach ($ids as $id) { $event = new Event('Controller.afterDelete', $this, ['id' => $id, 'type' => $this->objectType]); $this->getEventManager()->dispatch($event); - } catch (BEditaClientException $e) { - $this->log($e->getMessage(), LogLevel::ERROR); - $this->Flash->error($e->getMessage(), ['params' => $e]); - if (!empty($this->getRequest()->getData('id'))) { - return $this->redirect(['_name' => 'modules:view', 'object_type' => $this->objectType, 'id' => $this->getRequest()->getData('id')]); - } - - return $this->redirect(['_name' => 'modules:view', 'object_type' => $this->objectType, 'id' => $id]); } + } catch (BEditaClientException $e) { + $this->log($e->getMessage(), LogLevel::ERROR); + $this->Flash->error($e->getMessage(), ['params' => $e]); + if (!empty($this->getRequest()->getData('id'))) { + return $this->redirect(['_name' => 'modules:view', 'object_type' => $this->objectType, 'id' => $this->getRequest()->getData('id')]); + } + + return $this->redirect($this->referer()); } $this->Flash->success(__('Object(s) deleted')); diff --git a/src/Controller/TrashController.php b/src/Controller/TrashController.php index c7128ad9a..4c42f63d0 100644 --- a/src/Controller/TrashController.php +++ b/src/Controller/TrashController.php @@ -136,20 +136,12 @@ public function restore(): ?Response } else { $ids = [$this->getRequest()->getData('id')]; } - foreach ($ids as $id) { - try { - $this->apiClient->restoreObject($id, 'objects'); - } catch (BEditaClientException $e) { - // Error! Back to object view. - $this->log($e->getMessage(), LogLevel::ERROR); - $this->Flash->error($e->getMessage(), ['params' => $e]); - - if (!empty($this->getRequest()->getData('ids'))) { - return $this->redirect(['_name' => 'trash:list'] + $this->listQuery()); - } - - return $this->redirect(['_name' => 'trash:view', 'id' => $id]); - } + try { + $this->apiClient->restoreObjects($ids); + } catch (BEditaClientException $e) { + // Error! Back to object view. + $this->log($e->getMessage(), LogLevel::ERROR); + $this->Flash->error($e->getMessage(), ['params' => $e]); } return $this->redirect(['_name' => 'trash:list'] + $this->listQuery()); @@ -172,24 +164,15 @@ public function delete(): ?Response } else { $ids = [$this->getRequest()->getData('id')]; } - foreach ($ids as $id) { - try { - $this->deleteData($id); - } catch (BEditaClientException $e) { - // Error! Back to object view. - $this->log($e->getMessage(), LogLevel::ERROR); - $this->Flash->error($e->getMessage(), ['params' => $e]); - - if (!empty($this->getRequest()->getData('ids'))) { - return $this->redirect(['_name' => 'trash:list'] + $this->listQuery()); - } - - return $this->redirect(['_name' => 'trash:view', 'id' => $id]); - } + try { + $this->apiClient->removeObjects($ids); + $this->Flash->success(__('Object(s) deleted from trash')); + } catch (BEditaClientException $e) { + // Error! Back to object view. + $this->log($e->getMessage(), LogLevel::ERROR); + $this->Flash->error($e->getMessage(), ['params' => $e]); } - $this->Flash->success(__('Object(s) deleted from trash')); - return $this->redirect(['_name' => 'trash:list'] + $this->listQuery()); } @@ -224,17 +207,18 @@ public function emptyTrash(): ?Response $response = $this->apiClient->getObjects('trash', $query); $counter = 0; while (Hash::get($response, 'meta.pagination.count', 0) > 0) { - foreach ($response['data'] as $data) { - try { - $this->deleteData($data['id']); - $counter++; - } catch (BEditaClientException $e) { - // Error! Back to trash index. - $this->log($e->getMessage(), LogLevel::ERROR); - $this->Flash->error($e->getMessage(), ['params' => $e]); + $ids = Hash::extract($response, 'data.{n}.id'); + try { + $response = $this->apiClient->get('/streams', ['filter' => ['object_id' => $ids]]); + $this->apiClient->removeObjects($ids); + $streams = (array)Hash::get($response, 'data'); + $this->removeStreams($streams); + } catch (BEditaClientException $e) { + // Error! Back to trash index. + $this->log($e->getMessage(), LogLevel::ERROR); + $this->Flash->error($e->getMessage(), ['params' => $e]); - return $this->redirect(['_name' => 'trash:list'] + $this->listQuery()); - } + return $this->redirect(['_name' => 'trash:list'] + $this->listQuery()); } $response = $this->apiClient->getObjects('trash', $query); } @@ -263,4 +247,21 @@ public function deleteData(string $id): void } } } + + /** + * Remove streams + * + * @param array $streams The streams to remove + * @return void + */ + public function removeStreams(array $streams): void + { + // this for BE versions < 5.25.1, where streams are not deleted with media on delete + $uuids = (array)Hash::extract($streams, '{n}.id'); + $search = $this->apiClient->get('/streams', ['filter' => ['uuid' => $uuids]]); + $count = (int)Hash::get($search, 'meta.pagination.count', 0); + if ($count === 1) { + $this->apiClient->deleteObjects($uuids, 'streams'); + } + } } diff --git a/src/Utility/PermissionsTrait.php b/src/Utility/PermissionsTrait.php index 93af4d6be..6f845114a 100644 --- a/src/Utility/PermissionsTrait.php +++ b/src/Utility/PermissionsTrait.php @@ -75,9 +75,7 @@ public function addPermissions(string $objectId, array $roleIds): void */ public function removePermissions(array $objectPermissionIds): void { - foreach ($objectPermissionIds as $id) { - ApiClientProvider::getApiClient()->deleteObject($id, 'object_permissions'); - } + ApiClientProvider::getApiClient()->deleteObjects($objectPermissionIds, 'object_permissions'); } /** From 52380d70edee274a38102cd5d35f53ca6208f222 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Mon, 16 Sep 2024 16:38:52 +0200 Subject: [PATCH 15/24] fix: removeStreams --- src/Controller/TrashController.php | 6 +++--- tests/TestCase/Controller/TrashControllerTest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Controller/TrashController.php b/src/Controller/TrashController.php index 4c42f63d0..2d93e8a88 100644 --- a/src/Controller/TrashController.php +++ b/src/Controller/TrashController.php @@ -259,9 +259,9 @@ public function removeStreams(array $streams): void // this for BE versions < 5.25.1, where streams are not deleted with media on delete $uuids = (array)Hash::extract($streams, '{n}.id'); $search = $this->apiClient->get('/streams', ['filter' => ['uuid' => $uuids]]); - $count = (int)Hash::get($search, 'meta.pagination.count', 0); - if ($count === 1) { - $this->apiClient->deleteObjects($uuids, 'streams'); + $search = (array)Hash::get($search, 'data'); + foreach ($search as $stream) { + $this->apiClient->delete(sprintf('/streams/%s', $stream['id'])); } } } diff --git a/tests/TestCase/Controller/TrashControllerTest.php b/tests/TestCase/Controller/TrashControllerTest.php index ef29a5f2e..5fbcf3bc1 100644 --- a/tests/TestCase/Controller/TrashControllerTest.php +++ b/tests/TestCase/Controller/TrashControllerTest.php @@ -309,7 +309,7 @@ public function testDeleteMultiFailure(): void static::assertEquals('/trash', $response->getHeaderLine('Location')); $message = $this->Trash->getRequest()->getSession()->read('Flash.flash.0.message'); - static::assertEquals('[404] Not Found', $message); + static::assertEquals('Object(s) deleted from trash', $message); } /** From fc4bd753db797c4acfd0497bf99c5668511e7585 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Mon, 16 Sep 2024 17:02:34 +0200 Subject: [PATCH 16/24] fix: test for be4 --- tests/TestCase/Controller/TrashControllerTest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/TestCase/Controller/TrashControllerTest.php b/tests/TestCase/Controller/TrashControllerTest.php index 5fbcf3bc1..df01bc650 100644 --- a/tests/TestCase/Controller/TrashControllerTest.php +++ b/tests/TestCase/Controller/TrashControllerTest.php @@ -309,7 +309,13 @@ public function testDeleteMultiFailure(): void static::assertEquals('/trash', $response->getHeaderLine('Location')); $message = $this->Trash->getRequest()->getSession()->read('Flash.flash.0.message'); - static::assertEquals('Object(s) deleted from trash', $message); + $beditaApiVersion = (string)Hash::get((array)$this->client->get('/home'), 'meta.version'); + $apiMajor = substr($beditaApiVersion, 0, strpos($beditaApiVersion, '.')); + if ($apiMajor === '4') { + static::assertEquals('[404] Not Found', $message); + } else { + static::assertEquals('Object(s) deleted from trash', $message); + } } /** From 02f07583df4b4cb5e383d1a7f50984ca863ed337 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Mon, 16 Sep 2024 17:19:56 +0200 Subject: [PATCH 17/24] fix: remove streams --- src/Controller/TrashController.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Controller/TrashController.php b/src/Controller/TrashController.php index 2d93e8a88..685b89dec 100644 --- a/src/Controller/TrashController.php +++ b/src/Controller/TrashController.php @@ -165,7 +165,10 @@ public function delete(): ?Response $ids = [$this->getRequest()->getData('id')]; } try { + $response = $this->apiClient->get('/streams', ['filter' => ['object_id' => $ids]]); $this->apiClient->removeObjects($ids); + $streams = (array)Hash::get($response, 'data'); + $this->removeStreams($streams); $this->Flash->success(__('Object(s) deleted from trash')); } catch (BEditaClientException $e) { // Error! Back to object view. From c69aef73d16a8e878469222f107b68de45ac80a9 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Mon, 16 Sep 2024 17:23:21 +0200 Subject: [PATCH 18/24] test: better coverage --- .../Controller/TrashControllerTest.php | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/tests/TestCase/Controller/TrashControllerTest.php b/tests/TestCase/Controller/TrashControllerTest.php index df01bc650..1631c3a16 100644 --- a/tests/TestCase/Controller/TrashControllerTest.php +++ b/tests/TestCase/Controller/TrashControllerTest.php @@ -179,11 +179,10 @@ public function testRestoreMultiFailure(): void } /** - * Test `delete` and `deleteData` methods + * Test `delete` method * * @return void * @covers ::delete() - * @covers ::deleteData() */ public function testDelete(): void { @@ -206,12 +205,37 @@ public function testDelete(): void } /** - * Test `delete` and `deleteData` methods with media object + * Test `deleteData` method. For coverage and retrocompatibility only. * * @return void - * @covers ::delete() * @covers ::deleteData() */ + public function testDeleteData(): void + { + $id = $this->setupControllerAndData(); + $this->Trash->deleteData($id); + + try { + $this->client->getObject($id); + } catch (BEditaClientException $e) { + $expected = new BEditaClientException('Not Found', 404); + static::assertEquals($expected->getCode(), $e->getCode()); + } + + try { + $this->client->get(sprintf('/trash/%d', $id)); + } catch (BEditaClientException $e) { + $expected = new BEditaClientException('Not Found', 404); + static::assertEquals($expected->getCode(), $e->getCode()); + } + } + + /** + * Test `delete` method with media object + * + * @return void + * @covers ::delete() + */ public function testDeleteMediaWithStream(): void { // setup and auth From 5d7a19663a1da446c21a39ab43dbe9f69a7acdcf Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Mon, 16 Sep 2024 17:24:38 +0200 Subject: [PATCH 19/24] test: better coverage --- tests/TestCase/Controller/TrashControllerTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/TestCase/Controller/TrashControllerTest.php b/tests/TestCase/Controller/TrashControllerTest.php index 1631c3a16..8c355d721 100644 --- a/tests/TestCase/Controller/TrashControllerTest.php +++ b/tests/TestCase/Controller/TrashControllerTest.php @@ -183,6 +183,7 @@ public function testRestoreMultiFailure(): void * * @return void * @covers ::delete() + * @covers ::removeStreams() */ public function testDelete(): void { @@ -235,6 +236,7 @@ public function testDeleteData(): void * * @return void * @covers ::delete() + * @covers ::removeStreams() */ public function testDeleteMediaWithStream(): void { @@ -348,6 +350,7 @@ public function testDeleteMultiFailure(): void * @return void * @covers ::emptyTrash() * @covers ::listQuery() + * @covers ::removeStreams() */ public function testEmpty(): void { From 896e6fad272933bad583daa0230903689dffd90b Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Mon, 16 Sep 2024 17:26:53 +0200 Subject: [PATCH 20/24] chore fix: phpstan --- tests/TestCase/Controller/TrashControllerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TestCase/Controller/TrashControllerTest.php b/tests/TestCase/Controller/TrashControllerTest.php index 8c355d721..577d213e5 100644 --- a/tests/TestCase/Controller/TrashControllerTest.php +++ b/tests/TestCase/Controller/TrashControllerTest.php @@ -214,7 +214,7 @@ public function testDelete(): void public function testDeleteData(): void { $id = $this->setupControllerAndData(); - $this->Trash->deleteData($id); + $this->Trash->deleteData((string)$id); try { $this->client->getObject($id); From 54733d1aa64a33e831c29a318cf5a6c5ab4d7e48 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Tue, 17 Sep 2024 11:31:23 +0200 Subject: [PATCH 21/24] refactor: deleteMulti --- src/Controller/ModulesController.php | 15 ++-- src/Controller/TrashController.php | 75 ++++++++++--------- .../Controller/TrashControllerTest.php | 36 ++++++++- 3 files changed, 76 insertions(+), 50 deletions(-) diff --git a/src/Controller/ModulesController.php b/src/Controller/ModulesController.php index 831d06dd9..73d2ffcd3 100644 --- a/src/Controller/ModulesController.php +++ b/src/Controller/ModulesController.php @@ -379,19 +379,16 @@ public function clone($id): ?Response public function delete(): ?Response { $this->getRequest()->allowMethod(['post']); - $ids = []; - if (!empty($this->getRequest()->getData('ids'))) { - if (is_string($this->getRequest()->getData('ids'))) { - $ids = explode(',', (string)$this->getRequest()->getData('ids')); - } - } elseif (!empty($this->getRequest()->getData('id'))) { - $ids = [$this->getRequest()->getData('id')]; - } + $id = $this->getRequest()->getData('id'); + $ids = $this->getRequest()->getData('ids'); + $ids = is_string($ids) ? explode(',', $ids) : $ids; + $ids = empty($ids) ? [$id] : $ids; try { $this->apiClient->deleteObjects($ids, $this->objectType); + $eventManager = $this->getEventManager(); foreach ($ids as $id) { $event = new Event('Controller.afterDelete', $this, ['id' => $id, 'type' => $this->objectType]); - $this->getEventManager()->dispatch($event); + $eventManager->dispatch($event); } } catch (BEditaClientException $e) { $this->log($e->getMessage(), LogLevel::ERROR); diff --git a/src/Controller/TrashController.php b/src/Controller/TrashController.php index 685b89dec..c681e6d12 100644 --- a/src/Controller/TrashController.php +++ b/src/Controller/TrashController.php @@ -127,15 +127,10 @@ public function view($id): ?Response public function restore(): ?Response { $this->getRequest()->allowMethod(['post']); - $ids = []; - if (!empty($this->getRequest()->getData('ids'))) { - $ids = $this->getRequest()->getData('ids'); - if (is_string($ids)) { - $ids = explode(',', (string)$this->getRequest()->getData('ids')); - } - } else { - $ids = [$this->getRequest()->getData('id')]; - } + $id = $this->getRequest()->getData('id'); + $ids = $this->getRequest()->getData('ids'); + $ids = is_string($ids) ? explode(',', $ids) : $ids; + $ids = empty($ids) ? [$id] : $ids; try { $this->apiClient->restoreObjects($ids); } catch (BEditaClientException $e) { @@ -155,25 +150,12 @@ public function restore(): ?Response public function delete(): ?Response { $this->getRequest()->allowMethod(['post']); - $ids = []; - if (!empty($this->getRequest()->getData('ids'))) { - $ids = $this->getRequest()->getData('ids'); - if (is_string($ids)) { - $ids = explode(',', (string)$this->getRequest()->getData('ids')); - } - } else { - $ids = [$this->getRequest()->getData('id')]; - } - try { - $response = $this->apiClient->get('/streams', ['filter' => ['object_id' => $ids]]); - $this->apiClient->removeObjects($ids); - $streams = (array)Hash::get($response, 'data'); - $this->removeStreams($streams); + $id = $this->getRequest()->getData('id'); + $ids = $this->getRequest()->getData('ids'); + $ids = is_string($ids) ? explode(',', $ids) : $ids; + $ids = empty($ids) ? [$id] : $ids; + if ($this->deleteMulti($ids)) { $this->Flash->success(__('Object(s) deleted from trash')); - } catch (BEditaClientException $e) { - // Error! Back to object view. - $this->log($e->getMessage(), LogLevel::ERROR); - $this->Flash->error($e->getMessage(), ['params' => $e]); } return $this->redirect(['_name' => 'trash:list'] + $this->listQuery()); @@ -211,18 +193,10 @@ public function emptyTrash(): ?Response $counter = 0; while (Hash::get($response, 'meta.pagination.count', 0) > 0) { $ids = Hash::extract($response, 'data.{n}.id'); - try { - $response = $this->apiClient->get('/streams', ['filter' => ['object_id' => $ids]]); - $this->apiClient->removeObjects($ids); - $streams = (array)Hash::get($response, 'data'); - $this->removeStreams($streams); - } catch (BEditaClientException $e) { - // Error! Back to trash index. - $this->log($e->getMessage(), LogLevel::ERROR); - $this->Flash->error($e->getMessage(), ['params' => $e]); - + if (!$this->deleteMulti($ids)) { return $this->redirect(['_name' => 'trash:list'] + $this->listQuery()); } + $counter += count($ids); $response = $this->apiClient->getObjects('trash', $query); } $this->Flash->success(__(sprintf('%d objects deleted from trash', $counter))); @@ -251,6 +225,30 @@ public function deleteData(string $id): void } } + /** + * Delete multiple data and related streams, if any. + * + * @param array $ids Object IDs + * @return bool + */ + public function deleteMulti(array $ids): bool + { + try { + $response = $this->apiClient->get('/streams', ['filter' => ['object_id' => $ids]]); + $this->apiClient->removeObjects($ids); + $streams = (array)Hash::get($response, 'data'); + $this->removeStreams($streams); + } catch (BEditaClientException $e) { + // Error! Back to object view. + $this->log($e->getMessage(), LogLevel::ERROR); + $this->Flash->error($e->getMessage(), ['params' => $e]); + + return false; + } + + return true; + } + /** * Remove streams * @@ -261,6 +259,9 @@ public function removeStreams(array $streams): void { // this for BE versions < 5.25.1, where streams are not deleted with media on delete $uuids = (array)Hash::extract($streams, '{n}.id'); + if (empty($uuids)) { + return; + } $search = $this->apiClient->get('/streams', ['filter' => ['uuid' => $uuids]]); $search = (array)Hash::get($search, 'data'); foreach ($search as $stream) { diff --git a/tests/TestCase/Controller/TrashControllerTest.php b/tests/TestCase/Controller/TrashControllerTest.php index 577d213e5..b24723f9f 100644 --- a/tests/TestCase/Controller/TrashControllerTest.php +++ b/tests/TestCase/Controller/TrashControllerTest.php @@ -183,6 +183,7 @@ public function testRestoreMultiFailure(): void * * @return void * @covers ::delete() + * @covers ::deleteMulti() * @covers ::removeStreams() */ public function testDelete(): void @@ -231,6 +232,33 @@ public function testDeleteData(): void } } + /** + * Test `deleteMulti` method. + * + * @return void + * @covers ::deleteMulti() + */ + public function testDeleteMulti(): void + { + $id = $this->setupControllerAndData(true, false, true); + $actual = $this->Trash->deleteMulti([$id]); + static::assertTrue($actual); + + try { + $this->client->getObject($id); + } catch (BEditaClientException $e) { + $expected = new BEditaClientException('Not Found', 404); + static::assertEquals($expected->getCode(), $e->getCode()); + } + + try { + $this->client->get(sprintf('/trash/%d', $id)); + } catch (BEditaClientException $e) { + $expected = new BEditaClientException('Not Found', 404); + static::assertEquals($expected->getCode(), $e->getCode()); + } + } + /** * Test `delete` method with media object * @@ -304,16 +332,15 @@ public function testDeleteUnauthorized(): void } /** - * Test `restore` method with multiple items + * Test `delete` method passing ids in POST data * * @return void * @covers ::delete() */ - public function testDeleteMulti(): void + public function testDeleteByIds(): void { $id = $this->setupControllerAndData(true, false, true); $this->Trash->delete(); - $expected = new BEditaClientException('Not Found', 404); static::expectException(get_class($expected)); static::expectExceptionCode($expected->getCode()); @@ -326,7 +353,7 @@ public function testDeleteMulti(): void * @return void * @covers ::delete() */ - public function testDeleteMultiFailure(): void + public function testDeleteByIdsFailure(): void { $id = $this->setupControllerAndData(true, false, true); $this->client->remove($id); @@ -350,6 +377,7 @@ public function testDeleteMultiFailure(): void * @return void * @covers ::emptyTrash() * @covers ::listQuery() + * @covers ::deleteMulti() * @covers ::removeStreams() */ public function testEmpty(): void From e73106f29afaf24c82165077d974b1aa9b7ce7c7 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Tue, 17 Sep 2024 11:44:00 +0200 Subject: [PATCH 22/24] test: more coverage --- src/Controller/TrashController.php | 8 +------- tests/TestCase/Controller/TrashControllerTest.php | 13 +++++++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Controller/TrashController.php b/src/Controller/TrashController.php index c681e6d12..14cc3d5ae 100644 --- a/src/Controller/TrashController.php +++ b/src/Controller/TrashController.php @@ -216,13 +216,7 @@ public function deleteData(string $id): void $this->apiClient->remove($id); // this for BE versions < 5.25.1, where streams are not deleted with media on delete $streams = (array)Hash::get($response, 'data'); - foreach ($streams as $stream) { - $search = $this->apiClient->get('/streams', ['filter' => ['uuid' => $stream['id']]]); - $count = (int)Hash::get($search, 'meta.pagination.count', 0); - if ($count === 1) { - $this->apiClient->delete(sprintf('/streams/%s', $stream['id'])); - } - } + $this->removeStreams($streams); } /** diff --git a/tests/TestCase/Controller/TrashControllerTest.php b/tests/TestCase/Controller/TrashControllerTest.php index b24723f9f..ba7ebdcad 100644 --- a/tests/TestCase/Controller/TrashControllerTest.php +++ b/tests/TestCase/Controller/TrashControllerTest.php @@ -259,6 +259,19 @@ public function testDeleteMulti(): void } } + /** + * Test `deleteMulti` method with exception + * + * @return void + * @covers ::deleteMulti() + */ + public function testDeleteMultiException(): void + { + $this->setupControllerAndData(true, false, true); + $actual = $this->Trash->deleteMulti(['abc']); + static::assertFalse($actual); + } + /** * Test `delete` method with media object * From 6ab773afe90d935faa15150e9f66f1e1209a74ec Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Tue, 17 Sep 2024 11:47:40 +0200 Subject: [PATCH 23/24] fix: remove minimum-stability dev --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index 008548d20..131a08bff 100644 --- a/composer.json +++ b/composer.json @@ -75,7 +75,6 @@ "coverage": "vendor/bin/phpunit --colors=always --coverage-html coverage" }, "prefer-stable": true, - "minimum-stability": "dev", "config": { "sort-packages": true, "allow-plugins": { From 832dd0ed5b905296fdb7ef456b61d4db0a55debe Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Thu, 19 Sep 2024 10:06:24 +0200 Subject: [PATCH 24/24] fix: set module count when filter is empty --- src/Controller/Model/TagsController.php | 7 +++++-- src/Controller/ModulesController.php | 7 +++++-- src/Controller/TrashController.php | 7 +++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Controller/Model/TagsController.php b/src/Controller/Model/TagsController.php index 3ecb2b532..3fb077c13 100644 --- a/src/Controller/Model/TagsController.php +++ b/src/Controller/Model/TagsController.php @@ -54,12 +54,15 @@ public function initialize(): void public function index(): ?Response { $this->getRequest()->allowMethod(['get']); - $options = $this->getRequest()->getQueryParams(); + $params = $this->getRequest()->getQueryParams(); + $options = $params; $options['page_size'] = empty($options['page_size']) ? 20 : $options['page_size']; $options['sort'] = empty($options['sort']) ? 'name' : $options['sort']; $response = ApiClientProvider::getApiClient()->get('/model/tags', $options); $resources = Hash::combine((array)Hash::get($response, 'data'), '{n}.id', '{n}'); - CacheTools::setModuleCount((array)$response, 'tags'); + if (empty($params['q']) && empty($params['filter'])) { + CacheTools::setModuleCount((array)$response, 'tags'); + } $roots = $this->Categories->getAvailableRoots($resources); $tagsTree = $this->Categories->tree($resources); $this->set(compact('resources', 'roots', 'tagsTree')); diff --git a/src/Controller/ModulesController.php b/src/Controller/ModulesController.php index 73d2ffcd3..68f776f4f 100644 --- a/src/Controller/ModulesController.php +++ b/src/Controller/ModulesController.php @@ -103,8 +103,11 @@ public function index(): ?Response } try { - $response = $this->apiClient->getObjects($this->objectType, $this->Query->index()); - CacheTools::setModuleCount((array)$response, $this->Modules->getConfig('currentModuleName')); + $params = $this->Query->index(); + $response = $this->apiClient->getObjects($this->objectType, $params); + if (empty($params['q']) && empty($params['filter'])) { + CacheTools::setModuleCount((array)$response, $this->Modules->getConfig('currentModuleName')); + } } catch (BEditaClientException $e) { $this->log($e->getMessage(), LogLevel::ERROR); $this->Flash->error($e->getMessage(), ['params' => $e]); diff --git a/src/Controller/TrashController.php b/src/Controller/TrashController.php index 14cc3d5ae..75bf46321 100644 --- a/src/Controller/TrashController.php +++ b/src/Controller/TrashController.php @@ -63,8 +63,11 @@ public function index(): ?Response $this->getRequest()->allowMethod(['get']); try { - $response = $this->apiClient->getObjects('trash', $this->getRequest()->getQueryParams()); - CacheTools::setModuleCount($response, 'trash'); + $params = $this->getRequest()->getQueryParams(); + $response = $this->apiClient->getObjects('trash', $params); + if (empty($params['q']) && empty($params['filter'])) { + CacheTools::setModuleCount($response, 'trash'); + } } catch (BEditaClientException $e) { // Error! Back to dashboard. $this->log($e->getMessage(), LogLevel::ERROR);