diff --git a/CHANGELOG.md b/CHANGELOG.md index 78f0838..bf6452b 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 a FE editor (#864) +- Add an FE editor (#864, #872, #874) - Add automerging of green Dependabot PRs (#756) ### Changed diff --git a/Classes/Controller/FrontEndEditorController.php b/Classes/Controller/FrontEndEditorController.php index e79dd55..1ef5d4c 100644 --- a/Classes/Controller/FrontEndEditorController.php +++ b/Classes/Controller/FrontEndEditorController.php @@ -8,6 +8,7 @@ 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\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; /** @@ -70,4 +71,22 @@ class FrontEndEditorController extends ActionController return $this->redirect('index'); } + + public function newAction(?Tea $tea = null): ResponseInterface + { + // Note: We are using `makeInstance` here instead of `new` to allow for XCLASSing. + $teaToAssign = $tea ?? GeneralUtility::makeInstance(Tea::class); + $this->view->assign('tea', $teaToAssign); + + return $this->htmlResponse(); + } + + public function createAction(Tea $tea): ResponseInterface + { + $tea->setOwnerUid($this->getUidOfLoggedInUser()); + + $this->teaRepository->add($tea); + + return $this->redirect('index'); + } } diff --git a/Resources/Private/Language/de.locallang.xlf b/Resources/Private/Language/de.locallang.xlf index 5a9716f..4eb8ad4 100644 --- a/Resources/Private/Language/de.locallang.xlf +++ b/Resources/Private/Language/de.locallang.xlf @@ -67,6 +67,14 @@ Save Speichern + + Create new tea + Neuen Tee anlegen + + + Create new tea + Neuen Tee anlegen + diff --git a/Resources/Private/Language/locallang.xlf b/Resources/Private/Language/locallang.xlf index 02b5f47..93a33dc 100644 --- a/Resources/Private/Language/locallang.xlf +++ b/Resources/Private/Language/locallang.xlf @@ -51,6 +51,12 @@ Save + + Create new tea + + + Create new tea + diff --git a/Resources/Private/Templates/FrontEndEditor/Index.html b/Resources/Private/Templates/FrontEndEditor/Index.html index 9a8a7cd..f492ab4 100644 --- a/Resources/Private/Templates/FrontEndEditor/Index.html +++ b/Resources/Private/Templates/FrontEndEditor/Index.html @@ -7,6 +7,12 @@ + + diff --git a/Resources/Private/Templates/FrontEndEditor/New.html b/Resources/Private/Templates/FrontEndEditor/New.html new file mode 100644 index 0000000..9264761 --- /dev/null +++ b/Resources/Private/Templates/FrontEndEditor/New.html @@ -0,0 +1,14 @@ + + + + + +

+ +

+ + + + +
+ diff --git a/Tests/Unit/Controller/FrontEndEditorControllerTest.php b/Tests/Unit/Controller/FrontEndEditorControllerTest.php index 4c3f3a7..9b4a1f4 100644 --- a/Tests/Unit/Controller/FrontEndEditorControllerTest.php +++ b/Tests/Unit/Controller/FrontEndEditorControllerTest.php @@ -13,6 +13,7 @@ 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\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException; use TYPO3\CMS\Extbase\Persistence\QueryResultInterface; @@ -67,6 +68,14 @@ final class FrontEndEditorControllerTest extends UnitTestCase $this->subject->method('htmlResponse')->willReturn($responseStub); } + protected function tearDown(): void + { + // empty FIFO queue + GeneralUtility::makeInstance(Tea::class); + + parent::tearDown(); + } + /** * @param int<0, max> $userUid */ @@ -193,7 +202,7 @@ final class FrontEndEditorControllerTest extends UnitTestCase $this->setUidOfLoggedInUser($userUid); $tea = new Tea(); $tea->setOwnerUid($userUid); - $this->mockRedirect('index'); + $this->stubRedirect('index'); $this->teaRepositoryMock->expects(self::once())->method('update')->with($tea); @@ -216,6 +225,20 @@ final class FrontEndEditorControllerTest extends UnitTestCase } } + private function stubRedirect(string $actionName): void + { + if ((new Typo3Version())->getMajorVersion() <= 11) { + $this->subject->method('redirect') + // @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->method('redirect')->willReturn($redirectResponse); + } + } + /** * @test */ @@ -262,4 +285,92 @@ final class FrontEndEditorControllerTest extends UnitTestCase $this->subject->updateAction($tea); } + + /** + * @test + */ + public function newActionWithTeaAssignsProvidedTeaToView(): void + { + $tea = new Tea(); + + $this->viewMock->expects(self::once())->method('assign')->with('tea', $tea); + + $this->subject->newAction($tea); + } + + /** + * @test + */ + public function newActionWithNullTeaAssignsProvidedNewTeaToView(): void + { + $tea = new Tea(); + GeneralUtility::addInstance(Tea::class, $tea); + + $this->viewMock->expects(self::once())->method('assign')->with('tea', $tea); + + $this->subject->newAction(null); + } + + /** + * @test + */ + public function newActionWithoutTeaAssignsProvidedNewTeaToView(): void + { + $tea = new Tea(); + GeneralUtility::addInstance(Tea::class, $tea); + + $this->viewMock->expects(self::once())->method('assign')->with('tea', $tea); + + $this->subject->newAction(); + } + + /** + * @test + */ + public function newActionReturnsHtmlResponse(): void + { + $result = $this->subject->newAction(); + + self::assertInstanceOf(HtmlResponse::class, $result); + } + + /** + * @test + */ + public function createActionSetsLoggedInUserAsOwnerOfProvidedTea(): void + { + $userUid = 5; + $this->setUidOfLoggedInUser($userUid); + $tea = new Tea(); + $this->stubRedirect('index'); + + $this->subject->createAction($tea); + + self::assertSame($userUid, $tea->getOwnerUid()); + } + + /** + * @test + */ + public function createActionPersistsProvidedTea(): void + { + $tea = new Tea(); + $this->stubRedirect('index'); + + $this->teaRepositoryMock->expects(self::once())->method('add')->with($tea); + + $this->subject->createAction($tea); + } + + /** + * @test + */ + public function createActionRedirectsToIndexAction(): void + { + $tea = new Tea(); + + $this->mockRedirect('index'); + + $this->subject->updateAction($tea); + } } diff --git a/ext_localconf.php b/ext_localconf.php index 0c7cc19..49faa72 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -42,12 +42,12 @@ ExtensionUtility::configurePlugin( 'TeaFrontEndEditor', // all actions [ - FrontEndEditorController::class => 'index, edit, update', + FrontEndEditorController::class => 'index, edit, update, create, new', ], // 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', + FrontEndEditorController::class => 'index, edit, update, create, new', ] );