Skip to content

Commit

Permalink
Session fix (CP #37) (#38)
Browse files Browse the repository at this point in the history
* Session fix (cherry pick from #37)
* adjust p6 config, remove runtime config modifier in tests
  • Loading branch information
solverat authored Aug 22, 2019
1 parent 7c9b7d2 commit 675116e
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 2 deletions.
1 change: 1 addition & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Just click the "update" button or execute the migration command to finish the bu

#### Update from Version 3.0.0 to Version 3.1.0
- **[NEW FEATURE]**: Allow [pimcore redirect modification](https://github.com/dachcom-digital/pimcore-i18n/issues/33).
- **[BUGFIX]**: Disable context switch event if [pimcore full page cache](https://github.com/dachcom-digital/pimcore-i18n/issues/18) is enabled

#### Update from Version 2.x to Version 3.0.0
- **[NEW FEATURE]**: Pimcore 6.0.0 ready
Expand Down
3 changes: 3 additions & 0 deletions docs/70_ContextSwitch.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ If you have a complex e-commerce environment for example, you may want to check
- ContextSwitch only works in **same domain levels**. Since there is no way for simple cross-domain session ids, the zone switch will be sort of useless most of the time.
- The ContextSwitchEvent **ignores** ajax request. If your requesting data via ajax in a different language / country, no event will be triggered!

## Limitation
This event is disabled if the pimcore full page cache feature is enable to prevent session interventions.

***

## Implementation
Expand Down
14 changes: 13 additions & 1 deletion src/I18nBundle/EventListener/ContextSwitchDetectorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,33 @@ class ContextSwitchDetectorListener implements EventSubscriberInterface
*/
protected $requestValidatorHelper;

/**
* @var array
*/
protected $pimcoreConfig;

/**
* @param EventDispatcherInterface $eventDispatcher
* @param DocumentResolver $documentResolver
* @param ZoneManager $zoneManager
* @param DocumentHelper $documentHelper
* @param RequestValidatorHelper $requestValidatorHelper
* @param array $pimcoreConfig
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
DocumentResolver $documentResolver,
ZoneManager $zoneManager,
DocumentHelper $documentHelper,
RequestValidatorHelper $requestValidatorHelper
RequestValidatorHelper $requestValidatorHelper,
$pimcoreConfig
) {
$this->eventDispatcher = $eventDispatcher;
$this->documentResolver = $documentResolver;
$this->zoneManager = $zoneManager;
$this->documentHelper = $documentHelper;
$this->requestValidatorHelper = $requestValidatorHelper;
$this->pimcoreConfig = $pimcoreConfig;
}

/**
Expand All @@ -111,6 +119,10 @@ public static function getSubscribedEvents()
*/
public function onKernelRequest(GetResponseEvent $event)
{
if ($this->pimcoreConfig['cache']['enabled'] === true) {
return;
}

if ($event->isMasterRequest() === false) {
return;
}
Expand Down
2 changes: 2 additions & 0 deletions src/I18nBundle/Resources/config/services/event.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ services:
I18nBundle\EventListener\ContextSwitchDetectorListener:
tags:
- { name: kernel.event_subscriber }
arguments:
$pimcoreConfig: '%pimcore.config%'

# event: checks if hardlink page has a front-page map
I18nBundle\EventListener\FrontPageMapperListener:
Expand Down
9 changes: 9 additions & 0 deletions tests/_etc/config/bundle/symfony/config_full_page_cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
i18n:
mode: language
locale_adapter: system
default_locale: 'en'
translations: ~

pimcore:
cache:
enabled: true
45 changes: 44 additions & 1 deletion tests/_support/Helper/Browser/PhpBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use Codeception\Module;
use Codeception\Lib;
use Codeception\Util\Uri;
use Codeception\Exception\ModuleException;
use DachcomBundle\Test\Helper\PimcoreCore;
use DachcomBundle\Test\Helper\PimcoreUser;
use Pimcore\Model\User;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
use Symfony\Component\HttpKernel\Profiler\Profile;
Expand Down Expand Up @@ -228,6 +228,49 @@ public function dontSeeCanonicalLinkInResponse()
$this->assertNull($link);
}

/**
* Actor Function to see pimcore output cached disabled header
*
* @param $disabledReasonMessage
*/
public function seePimcoreOutputCacheDisabledHeader($disabledReasonMessage)
{
$disabledReason = $this->pimcoreCore->client->getInternalResponse()->getHeader('X-Pimcore-Output-Cache-Disable-Reason');

$this->assertEquals($disabledReasonMessage, $disabledReason);
}

/**
* Actor Function to not to see pimcore output cached disabled header
*/
public function dontSeePimcoreOutputCacheDisabledHeader()
{
$disabledReason = $this->pimcoreCore->client->getInternalResponse()->getHeader('X-Pimcore-Output-Cache-Disable-Reason');

$this->assertNull($disabledReason);
}

/**
* Actor Function to not to see pimcore output cached disabled header
*/
public function seePimcoreOutputCacheDate()
{
$cacheDateHeader = $this->pimcoreCore->client->getInternalResponse()->getHeader('x-pimcore-cache-date');

$this->assertNotNull($cacheDateHeader);
}

/**
* Actor Function to assert empty i18n session bag
*/
public function seeEmptyI18nSessionBag()
{
/** @var NamespacedAttributeBag $sessionBag */
$sessionBag = $this->pimcoreCore->client->getRequest()->getSession()->getBag('i18n_session');

$this->assertCount(0, $sessionBag->all());
}

/**
* Actor Function to check if last _fragment request has given properties in request attributes.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/functional.full_page_cache.suite.dist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
actor: FunctionalTester
modules:
enabled:
- \DachcomBundle\Test\Helper\PimcoreCore:
connect_db: true
rebootable_client: true
configuration_file: 'config_full_page_cache.yml'
- \DachcomBundle\Test\Helper\PimcoreBundleCore:
run_installer: true
- \DachcomBundle\Test\Helper\Browser\PhpBrowser:
depends: \DachcomBundle\Test\Helper\PimcoreCore
- \DachcomBundle\Test\Helper\PimcoreBackend
- \DachcomBundle\Test\Helper\PimcoreUser
25 changes: 25 additions & 0 deletions tests/functional.full_page_cache/FullPageCacheCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace DachcomBundle\Test\FunctionalFullPageCache;

use DachcomBundle\Test\FunctionalTester;

class FullPageCacheCest
{
/**
* @param FunctionalTester $I
*/
public function testFullPageCacheEnabled(FunctionalTester $I)
{
$site1 = $I->haveASite('test-domain1.test');
$I->haveAPageDocumentForSite($site1, 'en', 'en');
$I->haveAPageDocumentForSite($site1, 'de', 'de');

$I->amOnPageWithLocale('http://test-domain1.test/de', 'de');

$I->dontSeePimcoreOutputCacheDisabledHeader();
$I->seePimcoreOutputCacheDate();
$I->seeEmptyI18nSessionBag();
}

}
2 changes: 2 additions & 0 deletions tests/functional.full_page_cache/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
// Here you can initialize variables that will be available to your tests

0 comments on commit 675116e

Please sign in to comment.