From 7297ff32806fa37dc81eccbe681307dd74e6cc17 Mon Sep 17 00:00:00 2001 From: Tatyana Koleva Date: Mon, 15 Jul 2024 13:37:11 +0300 Subject: [PATCH 1/7] OXDEV-8462 Add admin controller example --- menu.xml | 10 ++++ metadata.php | 3 +- src/Core/Module.php | 2 + .../Admin/GreetingAdminController.php | 29 +++++++++++ .../Admin/GreetingAdminControllerTest.php | 52 +++++++++++++++++++ views/admin_twig/de/module_options.php | 7 +++ views/admin_twig/en/module_options.php | 7 +++ views/twig/admin/user_greetings.html.twig | 18 +++++++ 8 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 menu.xml create mode 100644 src/Greeting/Controller/Admin/GreetingAdminController.php create mode 100644 tests/Integration/Greeting/Controller/Admin/GreetingAdminControllerTest.php create mode 100644 views/twig/admin/user_greetings.html.twig diff --git a/menu.xml b/menu.xml new file mode 100644 index 0000000..be2a624 --- /dev/null +++ b/menu.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/metadata.php b/metadata.php index 3fda7f7..0ede64c 100644 --- a/metadata.php +++ b/metadata.php @@ -28,7 +28,8 @@ \OxidEsales\Eshop\Application\Model\User::class => \OxidEsales\ModuleTemplate\Extension\Model\User::class, ], 'controllers' => [ - 'oemtgreeting' => \OxidEsales\ModuleTemplate\Greeting\Controller\GreetingController::class + 'oemtgreeting' => \OxidEsales\ModuleTemplate\Greeting\Controller\GreetingController::class, + 'oemt_admin_greeting' => \OxidEsales\ModuleTemplate\Greeting\Controller\Admin\GreetingAdminController::class, ], 'events' => [ 'onActivate' => '\OxidEsales\ModuleTemplate\Core\ModuleEvents::onActivate', diff --git a/src/Core/Module.php b/src/Core/Module.php index fd95178..64cf660 100644 --- a/src/Core/Module.php +++ b/src/Core/Module.php @@ -18,4 +18,6 @@ final class Module public const OEMT_COUNTER_TEMPLATE_VARNAME = 'oemt_greeting_counter'; public const DEFAULT_PERSONAL_GREETING_LANGUAGE_CONST = 'OEMODULETEMPLATE_GREETING_GENERIC'; + + public const OEMT_ADMIN_GREETING_TEMPLATE_VARNAME = 'greeting_message'; } diff --git a/src/Greeting/Controller/Admin/GreetingAdminController.php b/src/Greeting/Controller/Admin/GreetingAdminController.php new file mode 100644 index 0000000..adce9bc --- /dev/null +++ b/src/Greeting/Controller/Admin/GreetingAdminController.php @@ -0,0 +1,29 @@ +load($this->getEditObjectId())) { + $this->addTplParam(ModuleCore::OEMT_ADMIN_GREETING_TEMPLATE_VARNAME, $oUser->getPersonalGreeting()); + } + + return parent::render(); + } +} diff --git a/tests/Integration/Greeting/Controller/Admin/GreetingAdminControllerTest.php b/tests/Integration/Greeting/Controller/Admin/GreetingAdminControllerTest.php new file mode 100644 index 0000000..fb7e1bf --- /dev/null +++ b/tests/Integration/Greeting/Controller/Admin/GreetingAdminControllerTest.php @@ -0,0 +1,52 @@ +createTestUser(); + + $controller = oxNew(GreetingAdminController::class); + $controller->setEditObjectId(self::TEST_USER_ID); + + $this->assertSame('@oe_moduletemplate/admin/user_greetings', $controller->render()); + + $viewData = $controller->getViewData(); + + $this->assertSame(self::TEST_GREETING, $viewData[ModuleCore::OEMT_ADMIN_GREETING_TEMPLATE_VARNAME]); + } + + private function createTestUser(): void + { + $user = oxNew(EshopModelUser::class); + $user->assign( + [ + 'oxid' => self::TEST_USER_ID, + 'oemtgreeting' => self::TEST_GREETING, + ] + ); + $user->save(); + } +} diff --git a/views/admin_twig/de/module_options.php b/views/admin_twig/de/module_options.php index e9e60fe..68524f3 100644 --- a/views/admin_twig/de/module_options.php +++ b/views/admin_twig/de/module_options.php @@ -9,6 +9,13 @@ $aLang = [ 'charset' => 'UTF-8', + 'tbcluser_greetings' => 'Greetings', + + 'OEMODULETEMPLATE_GREETING_TITLE' => 'Admin controller example', + 'OEMODULETEMPLATE_GREETING_MESSAGE_TEXT' => 'Greeting message: ', + 'OEMODULETEMPLATE_NO_GREETING_TEXT' => 'No greeting message added!', + + # Module settings 'SHOP_MODULE_GROUP_oemoduletemplate_main' => 'Einstellungen', 'SHOP_MODULE_oemoduletemplate_GreetingMode' => 'Begrüßungsmodus', 'SHOP_MODULE_oemoduletemplate_GreetingMode_generic' => 'höflich', diff --git a/views/admin_twig/en/module_options.php b/views/admin_twig/en/module_options.php index 80de61c..79c881e 100644 --- a/views/admin_twig/en/module_options.php +++ b/views/admin_twig/en/module_options.php @@ -9,6 +9,13 @@ $aLang = [ 'charset' => 'UTF-8', + 'tbcluser_greetings' => 'Greetings', + + 'OEMODULETEMPLATE_GREETING_TITLE' => 'Admin controller example', + 'OEMODULETEMPLATE_GREETING_MESSAGE_TEXT' => 'Greeting message: ', + 'OEMODULETEMPLATE_NO_GREETING_TEXT' => 'No greeting message added!', + + # Module settings 'SHOP_MODULE_GROUP_oemoduletemplate_main' => 'Settings', 'SHOP_MODULE_oemoduletemplate_GreetingMode' => 'Greeting mode', 'SHOP_MODULE_oemoduletemplate_GreetingMode_generic' => 'generic', diff --git a/views/twig/admin/user_greetings.html.twig b/views/twig/admin/user_greetings.html.twig new file mode 100644 index 0000000..15ce786 --- /dev/null +++ b/views/twig/admin/user_greetings.html.twig @@ -0,0 +1,18 @@ +{% include "headitem.html.twig" with {title: "GENERAL_ADMIN_TITLE"|translate} %} + +
+ {{ oViewConf.getHiddenSid()|raw }} + + +
+ +

{{ translate({ ident: "OEMODULETEMPLATE_GREETING_TITLE" }) }}

+ +{% if greeting_message %} +
{{ translate({ ident: "OEMODULETEMPLATE_GREETING_MESSAGE_TEXT" }) }} {{ greeting_message }}
+{% else %} +
{{ translate({ ident: "OEMODULETEMPLATE_NO_GREETING_TEXT" }) }}
+{% endif %} + +{% include "bottomnaviitem.html.twig" %} +{% include "bottomitem.html.twig" %} From 2e29e3a43066b0d8811e8b1c1e13276806ddac8d Mon Sep 17 00:00:00 2001 From: Tatyana Koleva Date: Mon, 15 Jul 2024 14:29:28 +0300 Subject: [PATCH 2/7] OXDEV-8462 Add example of extending admin template --- tests/Codeception/Acceptance.suite.yml | 5 ++ .../Acceptance/Admin/GreetingAdminCest.php | 66 +++++++++++++++++++ tests/Codeception/Acceptance/GreetingCest.php | 2 +- tests/Codeception/Acceptance/ModuleCest.php | 2 +- .../Acceptance/UpdateGreetingCest.php | 2 +- .../Codeception/Support/AcceptanceTester.php | 17 +++++ tests/Codeception/Support/Data/fixtures.php | 5 ++ tests/Codeception/Support/Data/fixtures.sql | 3 +- views/admin_twig/de/module_options.php | 2 + views/admin_twig/en/module_options.php | 2 + .../themes/admin_twig/user_main.html.twig | 16 +++++ 11 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 tests/Codeception/Acceptance/Admin/GreetingAdminCest.php create mode 100644 views/twig/extensions/themes/admin_twig/user_main.html.twig diff --git a/tests/Codeception/Acceptance.suite.yml b/tests/Codeception/Acceptance.suite.yml index 4f81b98..8e58792 100644 --- a/tests/Codeception/Acceptance.suite.yml +++ b/tests/Codeception/Acceptance.suite.yml @@ -35,6 +35,11 @@ modules: depends: - WebDriver - Db + - \OxidEsales\Codeception\Module\OxideshopAdmin: + screen_shot_url: '%SCREEN_SHOT_URL%' + depends: + - WebDriver + - \OxidEsales\Codeception\Module\Oxideshop - \OxidEsales\Codeception\Module\Database: depends: Db - \OxidEsales\Codeception\Module\SelectTheme: diff --git a/tests/Codeception/Acceptance/Admin/GreetingAdminCest.php b/tests/Codeception/Acceptance/Admin/GreetingAdminCest.php new file mode 100644 index 0000000..6ae657a --- /dev/null +++ b/tests/Codeception/Acceptance/Admin/GreetingAdminCest.php @@ -0,0 +1,66 @@ +setGreetingModeGeneric(); + $this->setUserPersonalGreeting($I, 'Hello there!'); + } + + public function _after(AcceptanceTester $I): void + { + //clean up after each test + $I->setGreetingModeGeneric(); + } + + /** @param AcceptanceTester $I */ + public function seeGreetingOptionsForUser(AcceptanceTester $I): void + { + $I->openAdmin(); + $adminPage = $I->loginAdmin(); + + $userList = $adminPage->openUsers(); + $userList->find("where[oxuser][oxusername]", $I->getDemoUserName()); + + $I->selectEditFrame(); + $I->see(Translator::translate('OEMODULETEMPLATE_ALLOW_GREETING')); + + $I->selectListFrame(); + $I->click(Translator::translate('tbcluser_greetings')); + + $I->selectEditFrame(); + $I->see(Translator::translate('OEMODULETEMPLATE_GREETING_TITLE')); + $I->see('Hello there!'); + } + + private function setUserPersonalGreeting(AcceptanceTester $I, string $value = ''): void + { + $I->updateInDatabase( + 'oxuser', + [ + 'oemtgreeting' => $value, + ], + [ + 'oxusername' => $I->getDemoUserName(), + ] + ); + } +} diff --git a/tests/Codeception/Acceptance/GreetingCest.php b/tests/Codeception/Acceptance/GreetingCest.php index 8089231..0696853 100644 --- a/tests/Codeception/Acceptance/GreetingCest.php +++ b/tests/Codeception/Acceptance/GreetingCest.php @@ -7,7 +7,7 @@ declare(strict_types=1); -namespace OxidEsales\ModuleTemplate\Tests\Codeception\Helper; +namespace OxidEsales\ModuleTemplate\Tests\Codeception\Acceptance; use OxidEsales\Codeception\Module\Translation\Translator; use OxidEsales\ModuleTemplate\Tests\Codeception\Support\AcceptanceTester; diff --git a/tests/Codeception/Acceptance/ModuleCest.php b/tests/Codeception/Acceptance/ModuleCest.php index 9242193..88df3b5 100644 --- a/tests/Codeception/Acceptance/ModuleCest.php +++ b/tests/Codeception/Acceptance/ModuleCest.php @@ -7,7 +7,7 @@ declare(strict_types=1); -namespace OxidEsales\ModuleTemplate\Tests\Codeception\Helper; +namespace OxidEsales\ModuleTemplate\Tests\Codeception\Acceptance; use OxidEsales\Codeception\Module\Translation\Translator; use OxidEsales\ModuleTemplate\Core\Module; diff --git a/tests/Codeception/Acceptance/UpdateGreetingCest.php b/tests/Codeception/Acceptance/UpdateGreetingCest.php index 748c1eb..2af51e1 100644 --- a/tests/Codeception/Acceptance/UpdateGreetingCest.php +++ b/tests/Codeception/Acceptance/UpdateGreetingCest.php @@ -7,7 +7,7 @@ declare(strict_types=1); -namespace OxidEsales\ModuleTemplate\Tests\Codeception\Helper; +namespace OxidEsales\ModuleTemplate\Tests\Codeception\Acceptance; use OxidEsales\Codeception\Module\Translation\Translator; use OxidEsales\ModuleTemplate\Core\Module as ModuleCore; diff --git a/tests/Codeception/Support/AcceptanceTester.php b/tests/Codeception/Support/AcceptanceTester.php index eb1acbd..6a18554 100644 --- a/tests/Codeception/Support/AcceptanceTester.php +++ b/tests/Codeception/Support/AcceptanceTester.php @@ -10,6 +10,8 @@ namespace OxidEsales\ModuleTemplate\Tests\Codeception\Support; use Codeception\Util\Fixtures; +use OxidEsales\Codeception\Admin\AdminLoginPage; +use OxidEsales\Codeception\Admin\AdminPanel; use OxidEsales\Codeception\Page\Home; use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; use OxidEsales\Facts\Facts; @@ -91,6 +93,21 @@ public function getShopUrl(): string return $facts->getShopUrl(); } + public function openAdmin(): AdminLoginPage + { + $I = $this; + $adminLogin = new AdminLoginPage($I); + $I->amOnPage($adminLogin->URL); + return $adminLogin; + } + + public function loginAdmin(): AdminPanel + { + $adminPage = $this->openAdmin(); + $admin = Fixtures::get('adminUser'); + return $adminPage->login($admin['email'], $admin['password']); + } + protected function getServiceFromContainer(string $serviceName) { return ContainerFactory::getInstance() diff --git a/tests/Codeception/Support/Data/fixtures.php b/tests/Codeception/Support/Data/fixtures.php index f0a0ec2..8203c99 100644 --- a/tests/Codeception/Support/Data/fixtures.php +++ b/tests/Codeception/Support/Data/fixtures.php @@ -13,4 +13,9 @@ 'email' => 'user@oxid-esales.com', 'password' => 'useruser', ], + 'adminUser' => [ + 'OXID' => 'oxadminuser', + 'email' => 'admin@oxid-esales.com', + 'password' => 'useruser', + ], ]; diff --git a/tests/Codeception/Support/Data/fixtures.sql b/tests/Codeception/Support/Data/fixtures.sql index b46ea9b..f0c7152 100644 --- a/tests/Codeception/Support/Data/fixtures.sql +++ b/tests/Codeception/Support/Data/fixtures.sql @@ -1,3 +1,4 @@ #Add default user REPLACE INTO `oxuser` (`OXID`, `OXACTIVE`, `OXRIGHTS`, `OXSHOPID`, `OXUSERNAME`, `OXPASSWORD`, `OXPASSSALT`, `OXCREATE`, `OXREGISTER`, `OXTIMESTAMP`, `OXBIRTHDATE`) VALUES -('oxdefaultuser',1,'user',1,'user@oxid-esales.com','$2y$10$ljaDXMPHOyC7ELlnC5ErK.3ET4B0oAN3WVr/Tk.RKlUfiuBcQEVVC','', '2003-01-01 00:00:00', '2003-01-01 00:00:00', '2003-01-01 00:00:00', '1985-01-01'); +('oxdefaultuser',1,'user',1,'user@oxid-esales.com','$2y$10$ljaDXMPHOyC7ELlnC5ErK.3ET4B0oAN3WVr/Tk.RKlUfiuBcQEVVC','', '2003-01-01 00:00:00', '2003-01-01 00:00:00', '2003-01-01 00:00:00', '1985-01-01'), +('oxadminuser',1,'malladmin',1,'admin@oxid-esales.com','$2y$10$ljaDXMPHOyC7ELlnC5ErK.3ET4B0oAN3WVr/Tk.RKlUfiuBcQEVVC','', '2003-01-01 00:00:00', '2003-01-01 00:00:00', '2003-01-01 00:00:00', '1985-01-01'); diff --git a/views/admin_twig/de/module_options.php b/views/admin_twig/de/module_options.php index 68524f3..0f6c739 100644 --- a/views/admin_twig/de/module_options.php +++ b/views/admin_twig/de/module_options.php @@ -14,6 +14,8 @@ 'OEMODULETEMPLATE_GREETING_TITLE' => 'Admin controller example', 'OEMODULETEMPLATE_GREETING_MESSAGE_TEXT' => 'Greeting message: ', 'OEMODULETEMPLATE_NO_GREETING_TEXT' => 'No greeting message added!', + 'OEMODULETEMPLATE_ALLOW_GREETING' => 'Allow user to set greeting', + 'OEMODULETEMPLATE_HELP_ALLOW_GREETING' => 'Example of extending admin template', # Module settings 'SHOP_MODULE_GROUP_oemoduletemplate_main' => 'Einstellungen', diff --git a/views/admin_twig/en/module_options.php b/views/admin_twig/en/module_options.php index 79c881e..3d3091a 100644 --- a/views/admin_twig/en/module_options.php +++ b/views/admin_twig/en/module_options.php @@ -14,6 +14,8 @@ 'OEMODULETEMPLATE_GREETING_TITLE' => 'Admin controller example', 'OEMODULETEMPLATE_GREETING_MESSAGE_TEXT' => 'Greeting message: ', 'OEMODULETEMPLATE_NO_GREETING_TEXT' => 'No greeting message added!', + 'OEMODULETEMPLATE_ALLOW_GREETING' => 'Allow user to set greeting', + 'OEMODULETEMPLATE_HELP_ALLOW_GREETING' => 'Example of extending admin template', # Module settings 'SHOP_MODULE_GROUP_oemoduletemplate_main' => 'Settings', diff --git a/views/twig/extensions/themes/admin_twig/user_main.html.twig b/views/twig/extensions/themes/admin_twig/user_main.html.twig new file mode 100644 index 0000000..eb57f25 --- /dev/null +++ b/views/twig/extensions/themes/admin_twig/user_main.html.twig @@ -0,0 +1,16 @@ +{% extends 'user_main.html.twig' %} + +{% block admin_user_main_form %} + + + + {{ translate({ ident: "OEMODULETEMPLATE_ALLOW_GREETING" }) }} + + + + {% include "inputhelp.html.twig" with {'sHelpId': help_id("OEMODULETEMPLATE_HELP_ALLOW_GREETING"), 'sHelpText': help_text("OEMODULETEMPLATE_HELP_ALLOW_GREETING")} %} + + + + {{ parent() }} +{% endblock %} From f3c344ef1b2a459c5b8f9b4047b31eb1cfddf771 Mon Sep 17 00:00:00 2001 From: Tatyana Koleva Date: Mon, 15 Jul 2024 16:44:54 +0300 Subject: [PATCH 3/7] Add oxnew factory example --- .../Infrastructure/CoreRequestFactory.php | 23 +++++++++++++++ .../CoreRequestFactoryInterface.php | 18 ++++++++++++ .../Service/GreetingMessageService.php | 7 +++-- src/Greeting/services.yaml | 6 ++-- .../Service/GreetingMessageServiceTest.php | 8 ++--- .../Infrastructure/CoreRequestFactoryTest.php | 29 +++++++++++++++++++ .../Service/GreetingMessageServiceTest.php | 8 ++--- 7 files changed, 86 insertions(+), 13 deletions(-) create mode 100644 src/Greeting/Infrastructure/CoreRequestFactory.php create mode 100644 src/Greeting/Infrastructure/CoreRequestFactoryInterface.php create mode 100644 tests/Unit/Greeting/Infrastructure/CoreRequestFactoryTest.php diff --git a/src/Greeting/Infrastructure/CoreRequestFactory.php b/src/Greeting/Infrastructure/CoreRequestFactory.php new file mode 100644 index 0000000..eaa8a6f --- /dev/null +++ b/src/Greeting/Infrastructure/CoreRequestFactory.php @@ -0,0 +1,23 @@ +shopRequest->getRequestParameter(ModuleCore::OEMT_GREETING_TEMPLATE_VARNAME); + $coreRequestService = $this->coreRequestFactory->create(); + $input = (string)$coreRequestService->getRequestParameter(ModuleCore::OEMT_GREETING_TEMPLATE_VARNAME); //in real life add some input validation return (string)substr($input, 0, 253); diff --git a/src/Greeting/services.yaml b/src/Greeting/services.yaml index ebd21e6..25ed4a2 100644 --- a/src/Greeting/services.yaml +++ b/src/Greeting/services.yaml @@ -3,7 +3,6 @@ services: public: false autowire: true bind: - OxidEsales\Eshop\Core\Request: '@=service("OxidEsales\\ModuleTemplate\\Core\\Registry").getRequest()' OxidEsales\Eshop\Core\Language: '@=service("OxidEsales\\ModuleTemplate\\Core\\Registry").getLang()' OxidEsales\ModuleTemplate\Greeting\Repository\GreetingRepositoryInterface: @@ -12,4 +11,7 @@ services: OxidEsales\ModuleTemplate\Greeting\Service\GreetingMessageServiceInterface: class: OxidEsales\ModuleTemplate\Greeting\Service\GreetingMessageService - public: true \ No newline at end of file + public: true + + OxidEsales\ModuleTemplate\Greeting\Infrastructure\CoreRequestFactoryInterface: + class: OxidEsales\ModuleTemplate\Greeting\Infrastructure\CoreRequestFactory \ No newline at end of file diff --git a/tests/Integration/Greeting/Service/GreetingMessageServiceTest.php b/tests/Integration/Greeting/Service/GreetingMessageServiceTest.php index 8202911..d27837a 100644 --- a/tests/Integration/Greeting/Service/GreetingMessageServiceTest.php +++ b/tests/Integration/Greeting/Service/GreetingMessageServiceTest.php @@ -10,9 +10,9 @@ namespace OxidEsales\ModuleTemplate\Tests\Integration\Greeting\Service; use OxidEsales\Eshop\Core\Language as CoreLanguage; -use OxidEsales\Eshop\Core\Request as CoreRequest; use OxidEsales\ModuleTemplate\Extension\Model\User; use OxidEsales\ModuleTemplate\Greeting\Service\GreetingMessageService; +use OxidEsales\ModuleTemplate\Greeting\Infrastructure\CoreRequestFactoryInterface; use OxidEsales\ModuleTemplate\Settings\Service\ModuleSettingsServiceInterface; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; @@ -28,7 +28,7 @@ public function testGenericGreetingWithUserForPersonalMode(): void { $sut = new GreetingMessageService( moduleSettings: $moduleSettingsStub = $this->createMock(ModuleSettingsServiceInterface::class), - shopRequest: $this->createStub(CoreRequest::class), + coreRequestFactory: $this->createStub(CoreRequestFactoryInterface::class), shopLanguage: $langStub = $this->createStub(CoreLanguage::class), ); @@ -50,11 +50,11 @@ public function testGenericGreetingWithUserForPersonalMode(): void private function getSut( ModuleSettingsServiceInterface $moduleSettings = null, - CoreRequest $shopRequest = null, + CoreRequestFactoryInterface $coreRequestFactory = null, ): GreetingMessageService { return new GreetingMessageService( moduleSettings: $moduleSettings ?? $this->createStub(ModuleSettingsServiceInterface::class), - shopRequest: $shopRequest ?? $this->createStub(CoreRequest::class), + coreRequestFactory: $coreRequestFactory ?? $this->createStub(CoreRequestFactoryInterface::class), shopLanguage: $shopLanguage ?? $this->createStub(CoreLanguage::class), ); } diff --git a/tests/Unit/Greeting/Infrastructure/CoreRequestFactoryTest.php b/tests/Unit/Greeting/Infrastructure/CoreRequestFactoryTest.php new file mode 100644 index 0000000..1adf431 --- /dev/null +++ b/tests/Unit/Greeting/Infrastructure/CoreRequestFactoryTest.php @@ -0,0 +1,29 @@ +getMockBuilder(CoreRequestFactory::class) + ->onlyMethods(['create']) + ->getMock(); + + $this->assertInstanceOf(Request::class, $coreRequestFactoryMock->create()); + } +} diff --git a/tests/Unit/Greeting/Service/GreetingMessageServiceTest.php b/tests/Unit/Greeting/Service/GreetingMessageServiceTest.php index 2192280..53b9ab2 100644 --- a/tests/Unit/Greeting/Service/GreetingMessageServiceTest.php +++ b/tests/Unit/Greeting/Service/GreetingMessageServiceTest.php @@ -10,8 +10,8 @@ namespace OxidEsales\ModuleTemplate\Tests\Unit\Greeting\Service; use OxidEsales\Eshop\Core\Language as CoreLanguage; -use OxidEsales\Eshop\Core\Request as CoreRequest; use OxidEsales\ModuleTemplate\Core\Module as ModuleCore; +use OxidEsales\ModuleTemplate\Greeting\Infrastructure\CoreRequestFactoryInterface; use OxidEsales\ModuleTemplate\Greeting\Service\GreetingMessageService; use OxidEsales\ModuleTemplate\Settings\Service\ModuleSettingsServiceInterface; use PHPUnit\Framework\Attributes\CoversClass; @@ -24,7 +24,7 @@ public function testGenericGreetingNoUserForGenericMode(): void { $service = new GreetingMessageService( moduleSettings: $moduleSettingsStub = $this->createMock(ModuleSettingsServiceInterface::class), - shopRequest: $this->createStub(CoreRequest::class), + coreRequestFactory: $this->createStub(CoreRequestFactoryInterface::class), shopLanguage: $langStub = $this->createStub(CoreLanguage::class), ); @@ -43,7 +43,7 @@ public function testGenericGreetingWithUserForGenericMode(): void { $service = new GreetingMessageService( moduleSettings: $moduleSettingsStub = $this->createMock(ModuleSettingsServiceInterface::class), - shopRequest: $this->createStub(CoreRequest::class), + coreRequestFactory: $this->createStub(CoreRequestFactoryInterface::class), shopLanguage: $langStub = $this->createStub(CoreLanguage::class), ); @@ -62,7 +62,7 @@ public function testGenericGreetingNoUserForPersonalMode(): void { $service = new GreetingMessageService( moduleSettings: $moduleSettingsStub = $this->createMock(ModuleSettingsServiceInterface::class), - shopRequest: $this->createStub(CoreRequest::class), + coreRequestFactory: $this->createStub(CoreRequestFactoryInterface::class), shopLanguage: $this->createStub(CoreLanguage::class), ); From 0b97903f2e4aa9750e2d24650d54cf50828fd57f Mon Sep 17 00:00:00 2001 From: Tatyana Koleva Date: Mon, 15 Jul 2024 17:02:04 +0300 Subject: [PATCH 4/7] OXDEV-8462 Add changes in readme and changelog --- CHANGELOG.md | 8 ++++++++ README.md | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ee93e4..5d0e1b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [undecided] - unreleased + +### Added +- Admin controller +- Template for admin controller +- Example of extending of current admin template +- ``oxNew`` object factory example + ## [v3.0.0] - 2024-06-27 This is the stable release for v3.0.0. No changes have been made since v3.0.0-rc.1. diff --git a/README.md b/README.md index a906325..451aed9 100644 --- a/README.md +++ b/README.md @@ -214,7 +214,8 @@ Story: * extending a shop model (`OxidEsales\ModuleTemplate\Extension\Model\User`) / (`OxidEsales\ModuleTemplate\Extension\Model\Basket`) * extending a shop controller (`OxidEsales\ModuleTemplate\Extension\Controller\StartController`) * extending a shop database table (`oxuser`) -* extending a shop template block (`start_welcome_text`) +* extending a shop template block (`start_newest_articles`) +* extending a shop admin template block (`admin_user_main_form`) **HINT**: only extend the shop core if there is no other way like listen and handle shop events, decorate/replace some DI service. Your module might be one of many in the class chain and you should @@ -227,10 +228,12 @@ If you need to extend the shop class chain by overwriting, try to stick to the p #### Sometimes we need to bring our own * own module controller (`oemtgreeting` with own template and own translations) +* own module admin controller (`oemt_admin_greeting` with own template and own translations) * module setting (`oemoduletemplate_GreetingMode`) * event subscriber (`OxidEsales\ModuleTemplate\Tracker\Subscriber\BeforeModelUpdate`) * model with a database (`OxidEsales\ModuleTemplate\Tracker\Model\GreetingTracker`) * DI service examples +* ``oxNew`` object factory example (`OxidEsales\ModuleTemplate\Greeting\Infrastructure\CoreRequestFactory`) #### Whatever you do, ensure it is covered with tests * unit/integration test From 97fb89b76fbda71ddd7bc5195e5e85bfea4aefdb Mon Sep 17 00:00:00 2001 From: Nikola Ivanovski Date: Tue, 16 Jul 2024 15:43:59 +0300 Subject: [PATCH 5/7] OXDEV-8462 Translate admin settings --- views/admin_twig/de/module_options.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/views/admin_twig/de/module_options.php b/views/admin_twig/de/module_options.php index 0f6c739..f1d490f 100644 --- a/views/admin_twig/de/module_options.php +++ b/views/admin_twig/de/module_options.php @@ -11,11 +11,11 @@ 'charset' => 'UTF-8', 'tbcluser_greetings' => 'Greetings', - 'OEMODULETEMPLATE_GREETING_TITLE' => 'Admin controller example', - 'OEMODULETEMPLATE_GREETING_MESSAGE_TEXT' => 'Greeting message: ', - 'OEMODULETEMPLATE_NO_GREETING_TEXT' => 'No greeting message added!', - 'OEMODULETEMPLATE_ALLOW_GREETING' => 'Allow user to set greeting', - 'OEMODULETEMPLATE_HELP_ALLOW_GREETING' => 'Example of extending admin template', + 'OEMODULETEMPLATE_GREETING_TITLE' => 'Beispiel Admin Controller', + 'OEMODULETEMPLATE_GREETING_MESSAGE_TEXT' => 'Begrüßungsnachricht: ', + 'OEMODULETEMPLATE_NO_GREETING_TEXT' => 'Es wurde keine Begrüßungsnachricht hinzugefügt!', + 'OEMODULETEMPLATE_ALLOW_GREETING' => 'Benutzer erlauben die Begrüßungsnachricht zu setzen', + 'OEMODULETEMPLATE_HELP_ALLOW_GREETING' => 'Beispiel für die Erweiterung des Admin-Templates', # Module settings 'SHOP_MODULE_GROUP_oemoduletemplate_main' => 'Einstellungen', From a049efccf41121eee14ff88633a38e83ec1679a6 Mon Sep 17 00:00:00 2001 From: Tatyana Koleva Date: Thu, 25 Jul 2024 16:22:35 +0300 Subject: [PATCH 6/7] OXDEV-8462 Fix admin templates and description --- README.md | 2 +- views/admin_twig/de/module_options.php | 2 +- views/admin_twig/en/module_options.php | 2 +- views/twig/admin/user_greetings.html.twig | 2 +- views/twig/extensions/themes/admin_twig/user_main.html.twig | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 451aed9..69265b4 100644 --- a/README.md +++ b/README.md @@ -215,7 +215,7 @@ Story: * extending a shop controller (`OxidEsales\ModuleTemplate\Extension\Controller\StartController`) * extending a shop database table (`oxuser`) * extending a shop template block (`start_newest_articles`) -* extending a shop admin template block (`admin_user_main_form`) +* extending a shop admin template block (`admin_user_main_form` - only an extension of a block, without functionality) **HINT**: only extend the shop core if there is no other way like listen and handle shop events, decorate/replace some DI service. Your module might be one of many in the class chain and you should diff --git a/views/admin_twig/de/module_options.php b/views/admin_twig/de/module_options.php index f1d490f..2befaa8 100644 --- a/views/admin_twig/de/module_options.php +++ b/views/admin_twig/de/module_options.php @@ -15,7 +15,7 @@ 'OEMODULETEMPLATE_GREETING_MESSAGE_TEXT' => 'Begrüßungsnachricht: ', 'OEMODULETEMPLATE_NO_GREETING_TEXT' => 'Es wurde keine Begrüßungsnachricht hinzugefügt!', 'OEMODULETEMPLATE_ALLOW_GREETING' => 'Benutzer erlauben die Begrüßungsnachricht zu setzen', - 'OEMODULETEMPLATE_HELP_ALLOW_GREETING' => 'Beispiel für die Erweiterung des Admin-Templates', + 'OEMODULETEMPLATE_HELP_ALLOW_GREETING' => 'Dies ist ein Beispiel, wie ein Admintemplate erweitert werden kann. Momentan ist keine Funktionalität hinter dieser Checkbox hinterlegt, es wird keine Einstellung in der Datenbank gespeichert.', # Module settings 'SHOP_MODULE_GROUP_oemoduletemplate_main' => 'Einstellungen', diff --git a/views/admin_twig/en/module_options.php b/views/admin_twig/en/module_options.php index 3d3091a..839a250 100644 --- a/views/admin_twig/en/module_options.php +++ b/views/admin_twig/en/module_options.php @@ -15,7 +15,7 @@ 'OEMODULETEMPLATE_GREETING_MESSAGE_TEXT' => 'Greeting message: ', 'OEMODULETEMPLATE_NO_GREETING_TEXT' => 'No greeting message added!', 'OEMODULETEMPLATE_ALLOW_GREETING' => 'Allow user to set greeting', - 'OEMODULETEMPLATE_HELP_ALLOW_GREETING' => 'Example of extending admin template', + 'OEMODULETEMPLATE_HELP_ALLOW_GREETING' => 'This is an example of extending admin template. There is no functionality implemented behind this checkbox and does not save anything to the database', # Module settings 'SHOP_MODULE_GROUP_oemoduletemplate_main' => 'Settings', diff --git a/views/twig/admin/user_greetings.html.twig b/views/twig/admin/user_greetings.html.twig index 15ce786..26b8789 100644 --- a/views/twig/admin/user_greetings.html.twig +++ b/views/twig/admin/user_greetings.html.twig @@ -1,6 +1,6 @@ {% include "headitem.html.twig" with {title: "GENERAL_ADMIN_TITLE"|translate} %} -
+ {{ oViewConf.getHiddenSid()|raw }} diff --git a/views/twig/extensions/themes/admin_twig/user_main.html.twig b/views/twig/extensions/themes/admin_twig/user_main.html.twig index eb57f25..77b240d 100644 --- a/views/twig/extensions/themes/admin_twig/user_main.html.twig +++ b/views/twig/extensions/themes/admin_twig/user_main.html.twig @@ -7,7 +7,7 @@ {{ translate({ ident: "OEMODULETEMPLATE_ALLOW_GREETING" }) }} - + {% include "inputhelp.html.twig" with {'sHelpId': help_id("OEMODULETEMPLATE_HELP_ALLOW_GREETING"), 'sHelpText': help_text("OEMODULETEMPLATE_HELP_ALLOW_GREETING")} %} From e6079e43ed35b8c640a159927e934978051710ad Mon Sep 17 00:00:00 2001 From: Tatyana Koleva Date: Thu, 1 Aug 2024 18:25:05 +0300 Subject: [PATCH 7/7] OXDEV-8462 Change oxNew factory example --- README.md | 2 +- .../Admin/GreetingAdminController.php | 11 ++++-- ...equestFactory.php => UserModelFactory.php} | 8 ++-- ...face.php => UserModelFactoryInterface.php} | 8 ++-- .../Service/GreetingMessageService.php | 7 ++-- src/Greeting/Service/UserService.php | 38 +++++++++++++++++++ src/Greeting/Service/UserServiceInterface.php | 17 +++++++++ src/Greeting/services.yaml | 9 ++++- .../Service/GreetingMessageServiceTest.php | 8 ++-- .../Infrastructure/CoreRequestFactoryTest.php | 29 -------------- .../Infrastructure/UserModelFactoryTest.php | 29 ++++++++++++++ .../Service/GreetingMessageServiceTest.php | 8 ++-- 12 files changed, 118 insertions(+), 56 deletions(-) rename src/Greeting/Infrastructure/{CoreRequestFactory.php => UserModelFactory.php} (58%) rename src/Greeting/Infrastructure/{CoreRequestFactoryInterface.php => UserModelFactoryInterface.php} (58%) create mode 100644 src/Greeting/Service/UserService.php create mode 100644 src/Greeting/Service/UserServiceInterface.php delete mode 100644 tests/Unit/Greeting/Infrastructure/CoreRequestFactoryTest.php create mode 100644 tests/Unit/Greeting/Infrastructure/UserModelFactoryTest.php diff --git a/README.md b/README.md index 69265b4..02cc03e 100644 --- a/README.md +++ b/README.md @@ -233,7 +233,7 @@ If you need to extend the shop class chain by overwriting, try to stick to the p * event subscriber (`OxidEsales\ModuleTemplate\Tracker\Subscriber\BeforeModelUpdate`) * model with a database (`OxidEsales\ModuleTemplate\Tracker\Model\GreetingTracker`) * DI service examples -* ``oxNew`` object factory example (`OxidEsales\ModuleTemplate\Greeting\Infrastructure\CoreRequestFactory`) +* ``oxNew`` object factory example (`OxidEsales\ModuleTemplate\Greeting\Infrastructure\UserModelFactory`) #### Whatever you do, ensure it is covered with tests * unit/integration test diff --git a/src/Greeting/Controller/Admin/GreetingAdminController.php b/src/Greeting/Controller/Admin/GreetingAdminController.php index adce9bc..ba03e10 100644 --- a/src/Greeting/Controller/Admin/GreetingAdminController.php +++ b/src/Greeting/Controller/Admin/GreetingAdminController.php @@ -1,7 +1,7 @@ load($this->getEditObjectId())) { + $userService = $this->getService(UserServiceInterface::class); + if ($this->getEditObjectId()) { + /** @var TemplateModelUser $oUser */ + $oUser = $userService->getUserById($this->getEditObjectId()); $this->addTplParam(ModuleCore::OEMT_ADMIN_GREETING_TEMPLATE_VARNAME, $oUser->getPersonalGreeting()); } diff --git a/src/Greeting/Infrastructure/CoreRequestFactory.php b/src/Greeting/Infrastructure/UserModelFactory.php similarity index 58% rename from src/Greeting/Infrastructure/CoreRequestFactory.php rename to src/Greeting/Infrastructure/UserModelFactory.php index eaa8a6f..ee02c1c 100644 --- a/src/Greeting/Infrastructure/CoreRequestFactory.php +++ b/src/Greeting/Infrastructure/UserModelFactory.php @@ -9,15 +9,15 @@ namespace OxidEsales\ModuleTemplate\Greeting\Infrastructure; -use OxidEsales\Eshop\Core\Request; +use OxidEsales\Eshop\Application\Model\User; -class CoreRequestFactory implements CoreRequestFactoryInterface +class UserModelFactory implements UserModelFactoryInterface { /** * @inheritDoc */ - public function create(): Request + public function create(): User { - return oxNew(Request::class); + return oxNew(User::class); } } diff --git a/src/Greeting/Infrastructure/CoreRequestFactoryInterface.php b/src/Greeting/Infrastructure/UserModelFactoryInterface.php similarity index 58% rename from src/Greeting/Infrastructure/CoreRequestFactoryInterface.php rename to src/Greeting/Infrastructure/UserModelFactoryInterface.php index 3a3587b..6bd0ed2 100644 --- a/src/Greeting/Infrastructure/CoreRequestFactoryInterface.php +++ b/src/Greeting/Infrastructure/UserModelFactoryInterface.php @@ -7,12 +7,12 @@ namespace OxidEsales\ModuleTemplate\Greeting\Infrastructure; -use OxidEsales\Eshop\Core\Request; +use OxidEsales\Eshop\Application\Model\User; -interface CoreRequestFactoryInterface +interface UserModelFactoryInterface { /** - * @return Request + * @return User */ - public function create(): Request; + public function create(): User; } diff --git a/src/Greeting/Service/GreetingMessageService.php b/src/Greeting/Service/GreetingMessageService.php index 1e2cbd0..e3e604c 100644 --- a/src/Greeting/Service/GreetingMessageService.php +++ b/src/Greeting/Service/GreetingMessageService.php @@ -11,16 +11,16 @@ use OxidEsales\Eshop\Application\Model\User as EshopModelUser; use OxidEsales\Eshop\Core\Language as EshopLanguage; +use OxidEsales\Eshop\Core\Request as EshopRequest; use OxidEsales\ModuleTemplate\Core\Module as ModuleCore; use OxidEsales\ModuleTemplate\Extension\Model\User as TemplateModelUser; -use OxidEsales\ModuleTemplate\Greeting\Infrastructure\CoreRequestFactoryInterface; use OxidEsales\ModuleTemplate\Settings\Service\ModuleSettingsServiceInterface; class GreetingMessageService implements GreetingMessageServiceInterface { public function __construct( private ModuleSettingsServiceInterface $moduleSettings, - private CoreRequestFactoryInterface $coreRequestFactory, + private EshopRequest $shopRequest, private EshopLanguage $shopLanguage, ) { } @@ -62,8 +62,7 @@ public function saveGreeting(EshopModelUser $user): bool */ private function getRequestOemtGreeting(): string { - $coreRequestService = $this->coreRequestFactory->create(); - $input = (string)$coreRequestService->getRequestParameter(ModuleCore::OEMT_GREETING_TEMPLATE_VARNAME); + $input = (string)$this->shopRequest->getRequestParameter(ModuleCore::OEMT_GREETING_TEMPLATE_VARNAME); //in real life add some input validation return (string)substr($input, 0, 253); diff --git a/src/Greeting/Service/UserService.php b/src/Greeting/Service/UserService.php new file mode 100644 index 0000000..38fdd1e --- /dev/null +++ b/src/Greeting/Service/UserService.php @@ -0,0 +1,38 @@ +userModelFactory = $userModelFactory; + } + + public function getUserById(string $userId): EshopModelUser + { + $userModel = $this->userModelFactory->create(); + $userModel->load($userId); + + return $userModel; + } +} diff --git a/src/Greeting/Service/UserServiceInterface.php b/src/Greeting/Service/UserServiceInterface.php new file mode 100644 index 0000000..df3d645 --- /dev/null +++ b/src/Greeting/Service/UserServiceInterface.php @@ -0,0 +1,17 @@ +createMock(ModuleSettingsServiceInterface::class), - coreRequestFactory: $this->createStub(CoreRequestFactoryInterface::class), + shopRequest: $this->createStub(CoreRequest::class), shopLanguage: $langStub = $this->createStub(CoreLanguage::class), ); @@ -50,11 +50,11 @@ public function testGenericGreetingWithUserForPersonalMode(): void private function getSut( ModuleSettingsServiceInterface $moduleSettings = null, - CoreRequestFactoryInterface $coreRequestFactory = null, + CoreRequest $shopRequest = null, ): GreetingMessageService { return new GreetingMessageService( moduleSettings: $moduleSettings ?? $this->createStub(ModuleSettingsServiceInterface::class), - coreRequestFactory: $coreRequestFactory ?? $this->createStub(CoreRequestFactoryInterface::class), + shopRequest: $this->createStub(CoreRequest::class), shopLanguage: $shopLanguage ?? $this->createStub(CoreLanguage::class), ); } diff --git a/tests/Unit/Greeting/Infrastructure/CoreRequestFactoryTest.php b/tests/Unit/Greeting/Infrastructure/CoreRequestFactoryTest.php deleted file mode 100644 index 1adf431..0000000 --- a/tests/Unit/Greeting/Infrastructure/CoreRequestFactoryTest.php +++ /dev/null @@ -1,29 +0,0 @@ -getMockBuilder(CoreRequestFactory::class) - ->onlyMethods(['create']) - ->getMock(); - - $this->assertInstanceOf(Request::class, $coreRequestFactoryMock->create()); - } -} diff --git a/tests/Unit/Greeting/Infrastructure/UserModelFactoryTest.php b/tests/Unit/Greeting/Infrastructure/UserModelFactoryTest.php new file mode 100644 index 0000000..2e4bd71 --- /dev/null +++ b/tests/Unit/Greeting/Infrastructure/UserModelFactoryTest.php @@ -0,0 +1,29 @@ +getMockBuilder(UserModelFactory::class) + ->onlyMethods(['create']) + ->getMock(); + + $this->assertInstanceOf(User::class, $coreRequestFactoryMock->create()); + } +} diff --git a/tests/Unit/Greeting/Service/GreetingMessageServiceTest.php b/tests/Unit/Greeting/Service/GreetingMessageServiceTest.php index 53b9ab2..2192280 100644 --- a/tests/Unit/Greeting/Service/GreetingMessageServiceTest.php +++ b/tests/Unit/Greeting/Service/GreetingMessageServiceTest.php @@ -10,8 +10,8 @@ namespace OxidEsales\ModuleTemplate\Tests\Unit\Greeting\Service; use OxidEsales\Eshop\Core\Language as CoreLanguage; +use OxidEsales\Eshop\Core\Request as CoreRequest; use OxidEsales\ModuleTemplate\Core\Module as ModuleCore; -use OxidEsales\ModuleTemplate\Greeting\Infrastructure\CoreRequestFactoryInterface; use OxidEsales\ModuleTemplate\Greeting\Service\GreetingMessageService; use OxidEsales\ModuleTemplate\Settings\Service\ModuleSettingsServiceInterface; use PHPUnit\Framework\Attributes\CoversClass; @@ -24,7 +24,7 @@ public function testGenericGreetingNoUserForGenericMode(): void { $service = new GreetingMessageService( moduleSettings: $moduleSettingsStub = $this->createMock(ModuleSettingsServiceInterface::class), - coreRequestFactory: $this->createStub(CoreRequestFactoryInterface::class), + shopRequest: $this->createStub(CoreRequest::class), shopLanguage: $langStub = $this->createStub(CoreLanguage::class), ); @@ -43,7 +43,7 @@ public function testGenericGreetingWithUserForGenericMode(): void { $service = new GreetingMessageService( moduleSettings: $moduleSettingsStub = $this->createMock(ModuleSettingsServiceInterface::class), - coreRequestFactory: $this->createStub(CoreRequestFactoryInterface::class), + shopRequest: $this->createStub(CoreRequest::class), shopLanguage: $langStub = $this->createStub(CoreLanguage::class), ); @@ -62,7 +62,7 @@ public function testGenericGreetingNoUserForPersonalMode(): void { $service = new GreetingMessageService( moduleSettings: $moduleSettingsStub = $this->createMock(ModuleSettingsServiceInterface::class), - coreRequestFactory: $this->createStub(CoreRequestFactoryInterface::class), + shopRequest: $this->createStub(CoreRequest::class), shopLanguage: $this->createStub(CoreLanguage::class), );