Skip to content

Commit

Permalink
Apply several code improvements and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbissonho committed Oct 31, 2024
1 parent 282bba7 commit a1af092
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 33 deletions.
3 changes: 0 additions & 3 deletions Block/Adminhtml/LastPageRemember.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,4 @@ protected function getRoutePath(): string
$request = $this->getRequest();
return "{$request->getRouteName()}/{$request->getControllerName()}/{$request->getActionName()}";
}



}
49 changes: 31 additions & 18 deletions Observer/DashboardPageObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)
);
}
}
8 changes: 4 additions & 4 deletions Observer/UserLoginObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}"
]
);

Expand Down
37 changes: 37 additions & 0 deletions Test/Integration/RememberScriptAdminhtmlBlockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Mbissonho\RememberAdminLastPage\Test\Integration;

use Magento\TestFramework\TestCase\AbstractBackendController;

/**
* @magentoDbIsolation enabled
*/
class RememberScriptAdminhtmlBlockTest extends AbstractBackendController
{
const SCRIPT = "sessionStorage.setItem('mbissonho-last-admin-page-accessed', '{\"route_path\":\"customer\/index\/index\",\"edit_details\":{\"url_entity_param_name\":\"entity_id\",\"url_entity_param_value\":0}}')";

/**
* @magentoConfigFixture admin/mbissonho_remember_admin_last_page/active 1
* @magentoAppIsolation enabled
*/
public function testHtmlContainsRememberScriptOnceModuleIsEnabled()
{
$this->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);
}
}
4 changes: 2 additions & 2 deletions view/adminhtml/layout/adminhtml_auth_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block name="login-session-storage-hidden-input"
<block name="mbissonho-login-session-storage-hidden-input"
template="Mbissonho_RememberAdminLastPage::login-session-storage-hidden-input.phtml"
cacheable="false"
/>
<block name="remove-remember"
<block name="mbissonho-remove-remember"
template="Mbissonho_RememberAdminLastPage::remove-remember.phtml"
cacheable="false"
/>
Expand Down
2 changes: 1 addition & 1 deletion view/adminhtml/layout/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<body>
<referenceContainer name="before.body.end">
<block class="Mbissonho\RememberAdminLastPage\Block\Adminhtml\LastPageRemember"
name="last-page-remember"
name="mbissonho-last-page-remember"
template="Mbissonho_RememberAdminLastPage::last-page-remember.phtml"/>
</referenceContainer>
</body>
Expand Down
2 changes: 1 addition & 1 deletion view/adminhtml/templates/last-page-remember.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ $helper = $this->helper(Data::class);
<?php if($helper->isActive()): ?>
<script>
require([], function () {
sessionStorage.setItem('last-admin-page-accessed', '<?= json_encode($block->getPathData()) ?>')
sessionStorage.setItem('mbissonho-last-admin-page-accessed', '<?= json_encode($block->getPathData()) ?>')
});
</script>
<?php endif; ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use Mbissonho\RememberAdminLastPage\Helper\Data;

?>
<?php if($this->helper(Data::class)->isActive()): ?>
<input type="hidden" name="last-admin-page-accessed"
<input type="hidden" name="mbissonho-last-admin-page-accessed"
value=""
data-mage-init='{"Mbissonho_RememberAdminLastPage/js/fill-hiden-input": {}}' />
<?php endif; ?>
2 changes: 1 addition & 1 deletion view/adminhtml/templates/remove-remember.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ $helper = $this->helper(Data::class);
<?php if($helper->isActive() && $helper->shouldRemove()): ?>
<script>
require([], function () {
sessionStorage.removeItem('last-admin-page-accessed');
sessionStorage.removeItem('mbissonho-last-admin-page-accessed');
});
</script>
<?php endif; ?>
2 changes: 1 addition & 1 deletion view/adminhtml/web/js/fill-hiden-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a1af092

Please sign in to comment.