From a1af092fdd863315d96bb4dea6098b0414efb432 Mon Sep 17 00:00:00 2001 From: Mateus Date: Wed, 30 Oct 2024 21:07:32 -0300 Subject: [PATCH] Apply several code improvements and tests --- Block/Adminhtml/LastPageRemember.php | 3 -- Observer/DashboardPageObserver.php | 49 ++++++++++++------- Observer/UserLoginObserver.php | 8 +-- ...havioursThroughAdminAuthenticationTest.php | 2 +- .../RememberScriptAdminhtmlBlockTest.php | 37 ++++++++++++++ .../adminhtml/layout/adminhtml_auth_login.xml | 4 +- view/adminhtml/layout/default.xml | 2 +- .../templates/last-page-remember.phtml | 2 +- .../login-session-storage-hidden-input.phtml | 2 +- .../adminhtml/templates/remove-remember.phtml | 2 +- view/adminhtml/web/js/fill-hiden-input.js | 2 +- 11 files changed, 80 insertions(+), 33 deletions(-) create mode 100644 Test/Integration/RememberScriptAdminhtmlBlockTest.php diff --git a/Block/Adminhtml/LastPageRemember.php b/Block/Adminhtml/LastPageRemember.php index dd0b644..8956ab4 100644 --- a/Block/Adminhtml/LastPageRemember.php +++ b/Block/Adminhtml/LastPageRemember.php @@ -42,7 +42,4 @@ protected function getRoutePath(): string $request = $this->getRequest(); return "{$request->getRouteName()}/{$request->getControllerName()}/{$request->getActionName()}"; } - - - } diff --git a/Observer/DashboardPageObserver.php b/Observer/DashboardPageObserver.php index 02a4f70..e503bb0 100644 --- a/Observer/DashboardPageObserver.php +++ b/Observer/DashboardPageObserver.php @@ -34,35 +34,48 @@ public function execute(Observer $observer): void { if(!$this->dataHelper->isActive()) return; + if(!$lastAccessedPageJsonAsArray = $this->willBeAbleToPerformRedirect()) return; $this->action = $observer->getEvent()->getData('controller_action'); - $storage = $this->om->get(StorageInterface::class); + //Try perform redirect to entity page(customer, product, etc) if possible + if($lastAccessedPageJsonAsArray['edit_details']['url_entity_param_value'] !== 0) { + $this->performRedirectToLastAccessedPage( + $lastAccessedPageJsonAsArray['route_path'], + $lastAccessedPageJsonAsArray['edit_details']['url_entity_param_name'], + $lastAccessedPageJsonAsArray['edit_details']['url_entity_param_value'] + ); + return; + } - $last = $storage->getLastPage(); + $this->performRedirectToLastAccessedPage($lastAccessedPageJsonAsArray['route_path']); + } - if(null === $last) return; + protected function willBeAbleToPerformRedirect(): mixed + { + $storage = $this->om->get(StorageInterface::class); + $lastAccessedPageJsonAsString = $storage->getLastPage(); - $last = json_decode($last, true); + if(null === $lastAccessedPageJsonAsString) return false; - if(!$storage->isFirstPageAfterLogin() || empty($last['route_path'])) return; + $lastAccessedPageJsonAsArray = \json_decode($lastAccessedPageJsonAsString, true); - if($last['edit_details']['url_entity_param_value'] !== 0) { - $this->redirect( - $last['route_path'], - $last['edit_details']['url_entity_param_name'], - $last['edit_details']['url_entity_param_value'] - ); - return; - } + if(!$storage->isFirstPageAfterLogin() || empty($lastAccessedPageJsonAsArray['route_path'])) return false; - $this->redirect($last['route_path']); + return $lastAccessedPageJsonAsArray; } - private function redirect(string $routePath, $urlEntityParamName = null, $urlEntityParamValue = null): void + + protected function performRedirectToLastAccessedPage(string $routePath, $urlEntityParamName = null, $urlEntityParamValue = null): void { $this->om->get(ActionFlag::class)->set('', ActionInterface::FLAG_NO_DISPATCH, true); - $this->action->getResponse() - ->setRedirect($this->om->get(UrlInterface::class)->getUrl( - $routePath, [$urlEntityParamName => $urlEntityParamValue])); + $this->action + ->getResponse() + ->setRedirect( + $this->om->get(UrlInterface::class) + ->getUrl( + $routePath, + [$urlEntityParamName => $urlEntityParamValue] + ) + ); } } diff --git a/Observer/UserLoginObserver.php b/Observer/UserLoginObserver.php index 32688d2..1d48c40 100644 --- a/Observer/UserLoginObserver.php +++ b/Observer/UserLoginObserver.php @@ -28,11 +28,11 @@ public function execute(Observer $observer): void { if(!$this->dataHelper->isActive()) return; - $last = $this->om->get(RequestInterface::class) - ->getParam('last-admin-page-accessed'); + $lastAccessedPageJsonAsString = $this->om->get(RequestInterface::class) + ->getParam('mbissonho-last-admin-page-accessed'); - if(!empty($last)) { - $this->om->get(StorageInterface::class)->setLastPage($last); + if(!empty($lastAccessedPageJsonAsString)) { + $this->om->get(StorageInterface::class)->setLastPage($lastAccessedPageJsonAsString); } } } diff --git a/Test/Integration/BehavioursThroughAdminAuthenticationTest.php b/Test/Integration/BehavioursThroughAdminAuthenticationTest.php index 9d63df1..e93a1e2 100644 --- a/Test/Integration/BehavioursThroughAdminAuthenticationTest.php +++ b/Test/Integration/BehavioursThroughAdminAuthenticationTest.php @@ -80,7 +80,7 @@ public function testLastAccessedPageInfoFromBrowserSessionStorageAppliedToBacken 'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD, ], 'form_key' => $formKey->getFormKey(), - 'last-admin-page-accessed' => "{\"foo\": \"bar\"}" + 'mbissonho-last-admin-page-accessed' => "{\"foo\": \"bar\"}" ] ); diff --git a/Test/Integration/RememberScriptAdminhtmlBlockTest.php b/Test/Integration/RememberScriptAdminhtmlBlockTest.php new file mode 100644 index 0000000..89046c2 --- /dev/null +++ b/Test/Integration/RememberScriptAdminhtmlBlockTest.php @@ -0,0 +1,37 @@ +dispatch('backend/customer/index/index/'); + + $body = $this->getResponse()->getBody(); + $this->assertStringContainsString(self::SCRIPT, $body); + } + + /** + * @magentoConfigFixture admin/mbissonho_remember_admin_last_page/active 0 + * @magentoAppIsolation enabled + */ + public function testHtmlDoesNotContainsRememberScriptOnceModuleIsEnabled() + { + $this->dispatch('backend/customer/index/index/'); + + $body = $this->getResponse()->getBody(); + $this->assertStringNotContainsString(self::SCRIPT, $body); + } +} diff --git a/view/adminhtml/layout/adminhtml_auth_login.xml b/view/adminhtml/layout/adminhtml_auth_login.xml index 0faad62..b63d0ac 100644 --- a/view/adminhtml/layout/adminhtml_auth_login.xml +++ b/view/adminhtml/layout/adminhtml_auth_login.xml @@ -3,11 +3,11 @@ xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> - - diff --git a/view/adminhtml/layout/default.xml b/view/adminhtml/layout/default.xml index 81de538..a9c95ee 100644 --- a/view/adminhtml/layout/default.xml +++ b/view/adminhtml/layout/default.xml @@ -4,7 +4,7 @@ diff --git a/view/adminhtml/templates/last-page-remember.phtml b/view/adminhtml/templates/last-page-remember.phtml index 1e2df55..c3bd1e6 100644 --- a/view/adminhtml/templates/last-page-remember.phtml +++ b/view/adminhtml/templates/last-page-remember.phtml @@ -8,7 +8,7 @@ $helper = $this->helper(Data::class); isActive()): ?> diff --git a/view/adminhtml/templates/login-session-storage-hidden-input.phtml b/view/adminhtml/templates/login-session-storage-hidden-input.phtml index d92fc2b..d27f284 100644 --- a/view/adminhtml/templates/login-session-storage-hidden-input.phtml +++ b/view/adminhtml/templates/login-session-storage-hidden-input.phtml @@ -6,7 +6,7 @@ use Mbissonho\RememberAdminLastPage\Helper\Data; ?> helper(Data::class)->isActive()): ?> - diff --git a/view/adminhtml/templates/remove-remember.phtml b/view/adminhtml/templates/remove-remember.phtml index 2726a9e..b4fe7e3 100644 --- a/view/adminhtml/templates/remove-remember.phtml +++ b/view/adminhtml/templates/remove-remember.phtml @@ -9,7 +9,7 @@ $helper = $this->helper(Data::class); isActive() && $helper->shouldRemove()): ?> diff --git a/view/adminhtml/web/js/fill-hiden-input.js b/view/adminhtml/web/js/fill-hiden-input.js index 9f6ec73..7df9aaa 100644 --- a/view/adminhtml/web/js/fill-hiden-input.js +++ b/view/adminhtml/web/js/fill-hiden-input.js @@ -9,7 +9,7 @@ define([ /** @inheritdoc */ _create: function () { - let last = sessionStorage.getItem('last-admin-page-accessed'); + let last = sessionStorage.getItem('mbissonho-last-admin-page-accessed'); if(last) { this.element.val(last);