diff --git a/Classes/Controller/FrontEndEditorController.php b/Classes/Controller/FrontEndEditorController.php index f71cbbc..e79dd55 100644 --- a/Classes/Controller/FrontEndEditorController.php +++ b/Classes/Controller/FrontEndEditorController.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace TTN\Tea\Controller; use Psr\Http\Message\ResponseInterface; +use TTN\Tea\Domain\Model\Product\Tea; use TTN\Tea\Domain\Repository\Product\TeaRepository; use TYPO3\CMS\Core\Context\Context; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; @@ -41,4 +42,32 @@ class FrontEndEditorController extends ActionController { return $this->context->getPropertyFromAspect('frontend.user', 'id'); } + + public function editAction(Tea $tea): ResponseInterface + { + $this->checkIfUserIsOwner($tea); + + $this->view->assign('tea', $tea); + + return $this->htmlResponse(); + } + + /** + * @throws \RuntimeException + */ + private function checkIfUserIsOwner(Tea $tea): void + { + if ($tea->getOwnerUid() !== $this->getUidOfLoggedInUser()) { + throw new \RuntimeException('You do not have the permissions to edit this tea.', 1687363749); + } + } + + public function updateAction(Tea $tea): ResponseInterface + { + $this->checkIfUserIsOwner($tea); + + $this->teaRepository->update($tea); + + return $this->redirect('index'); + } } diff --git a/Resources/Private/Language/de.locallang.xlf b/Resources/Private/Language/de.locallang.xlf index 9875a07..5a9716f 100644 --- a/Resources/Private/Language/de.locallang.xlf +++ b/Resources/Private/Language/de.locallang.xlf @@ -43,6 +43,30 @@ Title Titel + + Description + Beschreibung + + + Edit tea + Tee bearbeiten + + + Actions + Aktionen + + + Edit + Bearbeiten + + + Cancel + Abbrechen + + + Save + Speichern + diff --git a/Resources/Private/Language/locallang.xlf b/Resources/Private/Language/locallang.xlf index 1f2eb1a..02b5f47 100644 --- a/Resources/Private/Language/locallang.xlf +++ b/Resources/Private/Language/locallang.xlf @@ -21,6 +21,9 @@ Title + + Description + My teas @@ -33,6 +36,21 @@ Title + + Edit tea + + + Actions + + + Edit + + + Cancel + + + Save + diff --git a/Resources/Private/Partials/.gitkeep b/Resources/Private/Partials/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/Resources/Private/Partials/FrontEndEditor/Form.html b/Resources/Private/Partials/FrontEndEditor/Form.html new file mode 100644 index 0000000..8cb35cb --- /dev/null +++ b/Resources/Private/Partials/FrontEndEditor/Form.html @@ -0,0 +1,36 @@ + + + + +
+
+ +
+ + +
+
+ +
+ +
+ + +
+
+
+ +
+ + + + +
+ diff --git a/Resources/Private/Partials/FrontEndEditor/ValidationResult.html b/Resources/Private/Partials/FrontEndEditor/ValidationResult.html new file mode 100644 index 0000000..e924eca --- /dev/null +++ b/Resources/Private/Partials/FrontEndEditor/ValidationResult.html @@ -0,0 +1,11 @@ + + + + +
+ {error.message} +
+
+
+
+ diff --git a/Resources/Private/Templates/FrontEndEditor/Edit.html b/Resources/Private/Templates/FrontEndEditor/Edit.html new file mode 100644 index 0000000..0635cc2 --- /dev/null +++ b/Resources/Private/Templates/FrontEndEditor/Edit.html @@ -0,0 +1,14 @@ + + + + + +

+ +

+ + + + +
+ diff --git a/Resources/Private/Templates/FrontEndEditor/Index.html b/Resources/Private/Templates/FrontEndEditor/Index.html index a4bf144..9a8a7cd 100644 --- a/Resources/Private/Templates/FrontEndEditor/Index.html +++ b/Resources/Private/Templates/FrontEndEditor/Index.html @@ -17,6 +17,9 @@ + + + @@ -26,6 +29,11 @@ {tea.title} + + + + + diff --git a/Tests/Unit/Controller/FrontEndEditorControllerTest.php b/Tests/Unit/Controller/FrontEndEditorControllerTest.php index e580626..4c3f3a7 100644 --- a/Tests/Unit/Controller/FrontEndEditorControllerTest.php +++ b/Tests/Unit/Controller/FrontEndEditorControllerTest.php @@ -6,12 +6,15 @@ namespace TTN\Tea\Tests\Unit\Controller; use PHPUnit\Framework\MockObject\MockObject; use TTN\Tea\Controller\FrontEndEditorController; +use TTN\Tea\Domain\Model\Product\Tea; use TTN\Tea\Domain\Repository\Product\TeaRepository; use TYPO3\CMS\Core\Context\Context; use TYPO3\CMS\Core\Context\UserAspect; use TYPO3\CMS\Core\Http\HtmlResponse; +use TYPO3\CMS\Core\Http\RedirectResponse; use TYPO3\CMS\Core\Information\Typo3Version; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; +use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException; use TYPO3\CMS\Extbase\Persistence\QueryResultInterface; use TYPO3\CMS\Fluid\View\TemplateView; use TYPO3\TestingFramework\Core\AccessibleObjectInterface; @@ -118,4 +121,145 @@ final class FrontEndEditorControllerTest extends UnitTestCase self::assertInstanceOf(HtmlResponse::class, $result); } + + /** + * @test + */ + public function editActionWithOwnTeaAssignsProvidedTeaToView(): void + { + $userUid = 5; + $this->setUidOfLoggedInUser($userUid); + $tea = new Tea(); + $tea->setOwnerUid($userUid); + + $this->viewMock->expects(self::once())->method('assign')->with('tea', $tea); + + $this->subject->editAction($tea); + } + + /** + * @test + */ + public function editActionWithTeaFromOtherUserThrowsException(): 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->editAction($tea); + } + + /** + * @test + */ + public function editActionWithTeaWithoutOwnerThrowsException(): 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->editAction($tea); + } + + /** + * @test + */ + public function editActionForOwnTeaReturnsHtmlResponse(): void + { + $userUid = 5; + $this->setUidOfLoggedInUser($userUid); + $tea = new Tea(); + $tea->setOwnerUid($userUid); + + $result = $this->subject->editAction($tea); + + self::assertInstanceOf(HtmlResponse::class, $result); + } + + /** + * @test + */ + public function updateActionWithOwnTeaPersistsProvidedTea(): void + { + $userUid = 5; + $this->setUidOfLoggedInUser($userUid); + $tea = new Tea(); + $tea->setOwnerUid($userUid); + $this->mockRedirect('index'); + + $this->teaRepositoryMock->expects(self::once())->method('update')->with($tea); + + $this->subject->updateAction($tea); + } + + private function mockRedirect(string $actionName): void + { + if ((new Typo3Version())->getMajorVersion() <= 11) { + $this->subject->expects(self::once())->method('redirect') + ->with($actionName) + // @phpstan-ignore-next-line This class does not exist in V12 anymore, but this branch is V11-only. + ->willThrowException(new StopActionException('redirectToUri', 1476045828)); + // @phpstan-ignore-next-line This class does not exist in V12 anymore, but this branch is V11-only. + $this->expectException(StopActionException::class); + } else { + $redirectResponse = $this->createStub(RedirectResponse::class); + $this->subject->expects(self::once())->method('redirect')->with($actionName) + ->willReturn($redirectResponse); + } + } + + /** + * @test + */ + public function updateActionWithOwnTeaRedirectsToIndexAction(): void + { + $userUid = 5; + $this->setUidOfLoggedInUser($userUid); + $tea = new Tea(); + $tea->setOwnerUid($userUid); + + $this->mockRedirect('index'); + + $this->subject->updateAction($tea); + } + + /** + * @test + */ + public function updateActionWithTeaFromOtherUserThrowsException(): 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->updateAction($tea); + } + + /** + * @test + */ + public function updateActionWithTeaWithoutOwnerThrowsException(): 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->updateAction($tea); + } } diff --git a/ext_localconf.php b/ext_localconf.php index 9e54675..0c7cc19 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -42,12 +42,12 @@ ExtensionUtility::configurePlugin( 'TeaFrontEndEditor', // all actions [ - FrontEndEditorController::class => 'index', + FrontEndEditorController::class => 'index, edit, update', ], // 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', + FrontEndEditorController::class => 'index, edit, update', ] );