Skip to content

Include support for banner/ajax/load controller (enterprise specific) #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/Plugin/BannerAjaxLoadControllerPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);

namespace IntegerNet\SessionUnblocker\Plugin;

use Magento\Framework\Session\Generic as GenericSession;
use Magento\Framework\Session\SessionManagerInterface;

/**
* We are writing this plugin to make sure sessions are all loaded
* Magento\Banner\Controller\Ajax\Load
*
* Right after initiating all the needed Sessions we close the session,
* since we're only reading from the session when requesting banners.
*/
class BannerAjaxLoadControllerPlugin
{
/**
* @param GenericSession $genericSession
* @param SessionManagerInterface[] $additionalSessions
*
* Disabling 3 PHPCS rules because:
* 1 - We are well aware that we normally shouldn't call Sessions without
* proxy, but in this case, we actually want the sessions to be
* initiated directly.
* 2 - Also, we don't actually use the Sessions
* 3 - Lastly, we normally should not execute operations in a constructor
*
* phpcs:disable MEQP2.Classes.MutableObjects.MutableObjects
* phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.Found
* phpcs:disable MEQP2.Classes.ConstructorOperations.CustomOperationsFound
*/
public function __construct(
GenericSession $genericSession,
array $additionalSessions = []
) {
/**
* This is earliest moment where we can close the session,
* after we initialised all sessions we think will be needed
*
* Should there ever be an additional Session-type that's needed,
* nothing breaks, but the new session-type will open a new session
* and therefore block other requests
*/
$genericSession->writeClose();
}

//phpcs:ignore MEQP2.Classes.PublicNonInterfaceMethods.PublicMethodFound
public function beforeExecute()
{
}
}
14 changes: 13 additions & 1 deletion src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@
</argument>
</arguments>
</type>
</config>

<type name="Magento\Banner\Controller\Ajax\Load">
<plugin name="integernet_session_unblocker" type="IntegerNet\SessionUnblocker\Plugin\BannerAjaxLoadControllerPlugin" />
</type>

<type name="IntegerNet\SessionUnblocker\Plugin\BannerAjaxLoadControllerPlugin">
<arguments>
<argument name="additionalSessions" xsi:type="array">
<item name="messageSession" xsi:type="object">Magento\Framework\Message\Session</item>
</argument>
</arguments>
</type>
</config>
11 changes: 7 additions & 4 deletions tests/Integration/AbstractSessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

use IntegerNet\SessionUnblocker\Plugin\SessionStoragePlugin;
use IntegerNet\SessionUnblocker\MethodLog;
use IntegerNet\SessionUnblocker\Test\Util\SectionLoadActionSpy;
use IntegerNet\SessionUnblocker\Test\Util\BannerAjaxLoadActionSpy;
use IntegerNet\SessionUnblocker\Test\Util\CustomerSectionLoadActionSpy;
use IntegerNet\SessionUnblocker\Test\Util\SessionSpy;
use Magento\Customer\Controller\Section\Load as LoadAction;
use Magento\Banner\Controller\Ajax\Load as BannerAjaxLoadAction;
use Magento\Customer\Controller\Section\Load as CustomerSectionLoadAction;
use Magento\Framework\Session\Generic as GenericSession;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
Expand Down Expand Up @@ -78,8 +80,9 @@ private function setUpSpies(): void
$this->objectManager->configure(
[
'preferences' => [
LoadAction::class => SectionLoadActionSpy::class,
GenericSession::class => SessionSpy::class,
BannerAjaxLoadAction::class => BannerAjaxLoadActionSpy::class,
CustomerSectionLoadAction::class => CustomerSectionLoadActionSpy::class,
GenericSession::class => SessionSpy::class,
],
SessionStoragePlugin::class => [
'arguments' => ['doLogMethods' => true]
Expand Down
20 changes: 20 additions & 0 deletions tests/Integration/BannerAjaxLoadControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace IntegerNet\SessionUnblocker\Test\Integration;

class BannerAjaxLoadControllerTest extends AbstractSessionTest
{
public function testBannerAjaxLoad()
{
$this->when_dispatched('banner/ajax/load');
$this->then_sessions_have_been_started_and_closed_before_action();
}

public function testBannerAjaxLoadNoWrites()
{
$this->given_session_already_exists();
$this->when_dispatched('banner/ajax/load');
$this->then_sessions_have_not_been_modified_after_write();
}
}
17 changes: 17 additions & 0 deletions tests/Util/BannerAjaxLoadActionSpy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);

namespace IntegerNet\SessionUnblocker\Test\Util;

use IntegerNet\SessionUnblocker\MethodLog;
use Magento\Banner\Controller\Ajax\Load;

class BannerAjaxLoadActionSpy extends Load
{
public function execute()
{
MethodLog::instance()->logControllerAction(parent::class, __FUNCTION__);
return parent::execute();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use IntegerNet\SessionUnblocker\MethodLog;
use Magento\Customer\Controller\Section\Load;

class SectionLoadActionSpy extends Load
class CustomerSectionLoadActionSpy extends Load
{
public function execute()
{
Expand Down