From cda38af84b323a6f10326c4c110486719ed451e9 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Fri, 23 Jun 2023 11:55:19 +0200 Subject: [PATCH] [FEATURE] Add a delete functionality for the Tea FE editor (#876) The delete action is triggered using a form with a submit button, causing a POST request to be sent instead of a GET request. This is because GET requests should not modify (or delete) data, but only read it and be idempotent. Also, the request then is guaranteed to not get cached. From a usability perspective, a button instead of a link also is semantically more correct: A link is expected to bring you to some place, whereas a button is expected to trigger some action. Closes #871 --- CHANGELOG.md | 2 +- .../Controller/FrontEndEditorController.php | 13 ++++ Resources/Private/Language/de.locallang.xlf | 4 ++ Resources/Private/Language/locallang.xlf | 3 + .../Templates/FrontEndEditor/Index.html | 8 ++- .../FrontEndEditorControllerTest.php | 63 +++++++++++++++++++ ext_localconf.php | 4 +- 7 files changed, 93 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf6452b..945c886 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ## x.y.z ### Added -- Add an FE editor (#864, #872, #874) +- Add an FE editor (#864, #872, #874, #876) - Add automerging of green Dependabot PRs (#756) ### Changed diff --git a/Classes/Controller/FrontEndEditorController.php b/Classes/Controller/FrontEndEditorController.php index 1ef5d4c..4ae6387 100644 --- a/Classes/Controller/FrontEndEditorController.php +++ b/Classes/Controller/FrontEndEditorController.php @@ -9,6 +9,7 @@ use TTN\Tea\Domain\Model\Product\Tea; use TTN\Tea\Domain\Repository\Product\TeaRepository; use TYPO3\CMS\Core\Context\Context; use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Annotation as Extbase; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; /** @@ -89,4 +90,16 @@ class FrontEndEditorController extends ActionController return $this->redirect('index'); } + + /** + * @Extbase\IgnoreValidation("tea") + */ + public function deleteAction(Tea $tea): ResponseInterface + { + $this->checkIfUserIsOwner($tea); + + $this->teaRepository->remove($tea); + + return $this->redirect('index'); + } } diff --git a/Resources/Private/Language/de.locallang.xlf b/Resources/Private/Language/de.locallang.xlf index 4eb8ad4..7357618 100644 --- a/Resources/Private/Language/de.locallang.xlf +++ b/Resources/Private/Language/de.locallang.xlf @@ -71,6 +71,10 @@ Create new tea Neuen Tee anlegen + + Delete + Löschen + Create new tea Neuen Tee anlegen diff --git a/Resources/Private/Language/locallang.xlf b/Resources/Private/Language/locallang.xlf index 93a33dc..7f219ef 100644 --- a/Resources/Private/Language/locallang.xlf +++ b/Resources/Private/Language/locallang.xlf @@ -54,6 +54,9 @@ Create new tea + + Delete + Create new tea diff --git a/Resources/Private/Templates/FrontEndEditor/Index.html b/Resources/Private/Templates/FrontEndEditor/Index.html index f492ab4..fd89ad4 100644 --- a/Resources/Private/Templates/FrontEndEditor/Index.html +++ b/Resources/Private/Templates/FrontEndEditor/Index.html @@ -23,7 +23,7 @@ - + @@ -40,6 +40,12 @@ + + + + + diff --git a/Tests/Unit/Controller/FrontEndEditorControllerTest.php b/Tests/Unit/Controller/FrontEndEditorControllerTest.php index 9b4a1f4..bd54303 100644 --- a/Tests/Unit/Controller/FrontEndEditorControllerTest.php +++ b/Tests/Unit/Controller/FrontEndEditorControllerTest.php @@ -373,4 +373,67 @@ final class FrontEndEditorControllerTest extends UnitTestCase $this->subject->updateAction($tea); } + + /** + * @test + */ + public function deleteActionWithOwnTeaRemovesProvidedTea(): void + { + $userUid = 5; + $this->setUidOfLoggedInUser($userUid); + $tea = new Tea(); + $tea->setOwnerUid($userUid); + $this->stubRedirect('index'); + + $this->teaRepositoryMock->expects(self::once())->method('remove')->with($tea); + + $this->subject->deleteAction($tea); + } + + /** + * @test + */ + public function deleteActionWithOwnTeaRedirectsToIndexAction(): void + { + $userUid = 5; + $this->setUidOfLoggedInUser($userUid); + $tea = new Tea(); + $tea->setOwnerUid($userUid); + + $this->mockRedirect('index'); + + $this->subject->deleteAction($tea); + } + + /** + * @test + */ + public function deleteActionWithTeaFromOtherUserThrowsException(): void + { + $this->setUidOfLoggedInUser(1); + $tea = new Tea(); + $tea->setOwnerUid(2); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('You do not have the permissions to edit this tea.'); + $this->expectExceptionCode(1687363749); + + $this->subject->deleteAction($tea); + } + + /** + * @test + */ + public function deleteActionWithTeaWithoutOwnerThrowsException(): void + { + $this->setUidOfLoggedInUser(1); + $tea = new Tea(); + $tea->setOwnerUid(0); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('You do not have the permissions to edit this tea.'); + $this->expectExceptionCode(1687363749); + + $this->subject->deleteAction($tea); + } } diff --git a/ext_localconf.php b/ext_localconf.php index 49faa72..76fb8c7 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -42,12 +42,12 @@ ExtensionUtility::configurePlugin( 'TeaFrontEndEditor', // all actions [ - FrontEndEditorController::class => 'index, edit, update, create, new', + FrontEndEditorController::class => 'index, edit, update, create, new, delete', ], // non-cacheable actions [ // All actions need to be non-cacheable because they either contain dynamic data, // or because they are specific to the logged-in FE user (while FE content is cached by FE groups). - FrontEndEditorController::class => 'index, edit, update, create, new', + FrontEndEditorController::class => 'index, edit, update, create, new, delete', ] );