Skip to content

Commit

Permalink
Bugfix: Fix for not respected alternative headers in maintenance mode
Browse files Browse the repository at this point in the history
If alternative headers were defined by di.xml it's not respected in
maintenance mode.

Example di.xml:

     <type name="Magento\Framework\HTTP\PhpEnvironment\RemoteAddress">
        <arguments>
            <argument name="alternativeHeaders" xsi:type="array">
                <item name="HTTP_X_FORWARED_FOR" xsi:type="string">HTTP_X_FORWARED_FOR</item>
            </argument>
        </arguments>
    </type>

Use class constant

Fixed line length
  • Loading branch information
cmuench committed Dec 18, 2016
1 parent 0da0b13 commit 5dc15b1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
15 changes: 11 additions & 4 deletions lib/internal/Magento/Framework/App/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

namespace Magento\Framework\App;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\AppInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Autoload\AutoloaderRegistry;
use Magento\Framework\Autoload\Populator;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Config\File\ConfigFilePool;
use Magento\Framework\Filesystem\DriverPool;
use Magento\Framework\Profiler;
use Magento\Framework\Config\File\ConfigFilePool;

/**
* A bootstrap of Magento application
Expand Down Expand Up @@ -284,7 +284,14 @@ protected function assertMaintenance()
$this->initObjectManager();
/** @var \Magento\Framework\App\MaintenanceMode $maintenance */
$this->maintenance = $this->objectManager->get(\Magento\Framework\App\MaintenanceMode::class);
$isOn = $this->maintenance->isOn(isset($this->server['REMOTE_ADDR']) ? $this->server['REMOTE_ADDR'] : '');

/** @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $phpRemoteAddressEnvironment */
$phpRemoteAddressEnvironment = $this->objectManager->get(
\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress::class
);
$remoteAddress = $phpRemoteAddressEnvironment->getRemoteAddress();
$isOn = $this->maintenance->isOn($remoteAddress ? $remoteAddress : '');

if ($isOn && !$isExpected) {
$this->errorCode = self::ERR_MAINTENANCE;
throw new \Exception('Unable to proceed: the maintenance mode is enabled. ');
Expand Down Expand Up @@ -332,7 +339,7 @@ private function getIsExpected($key, $default)
{
if (array_key_exists($key, $this->server)) {
if (isset($this->server[$key])) {
return (bool)(int)$this->server[$key];
return (bool) (int) $this->server[$key];
}
return null;
}
Expand Down
17 changes: 12 additions & 5 deletions lib/internal/Magento/Framework/App/Test/Unit/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

namespace Magento\Framework\App\Test\Unit;

use Magento\Framework\App\Filesystem\DirectoryList;
use \Magento\Framework\App\Bootstrap;
use \Magento\Framework\App\State;
use \Magento\Framework\App\MaintenanceMode;

use Magento\Framework\App\Filesystem\DirectoryList;
use \Magento\Framework\App\State;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand Down Expand Up @@ -64,6 +63,11 @@ class BootstrapTest extends \PHPUnit_Framework_TestCase
*/
protected $bootstrapMock;

/**
* @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress | \PHPUnit_Framework_MockObject_MockObject
*/
protected $remoteAddress;

protected function setUp()
{
$this->objectManagerFactory = $this->getMock(
Expand All @@ -82,6 +86,7 @@ protected function setUp()
false
);
$this->maintenanceMode = $this->getMock(\Magento\Framework\App\MaintenanceMode::class, ['isOn'], [], '', false);
$this->remoteAddress = $this->getMock('Magento\Framework\HTTP\PhpEnvironment\RemoteAddress', [], [], '', false);
$filesystem = $this->getMock(\Magento\Framework\Filesystem::class, [], [], '', false);

$this->logger = $this->getMock(\Psr\Log\LoggerInterface::class);
Expand All @@ -91,6 +96,7 @@ protected function setUp()
$mapObjectManager = [
[\Magento\Framework\App\Filesystem\DirectoryList::class, $this->dirs],
[\Magento\Framework\App\MaintenanceMode::class, $this->maintenanceMode],
[\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress::class, $this->remoteAddress],
[\Magento\Framework\Filesystem::class, $filesystem],
[\Magento\Framework\App\DeploymentConfig::class, $this->deploymentConfig],
['Psr\Log\LoggerInterface', $this->logger],
Expand Down Expand Up @@ -205,7 +211,7 @@ public function testIsDeveloperModeDataProvider()
[State::MODE_DEVELOPER, State::MODE_PRODUCTION, true],
[State::MODE_PRODUCTION, State::MODE_DEVELOPER, false],
[null, State::MODE_DEVELOPER, true],
[null, State::MODE_PRODUCTION, false]
[null, State::MODE_PRODUCTION, false],
];
}

Expand Down Expand Up @@ -260,6 +266,7 @@ public function testAssertMaintenance($isOn, $isExpected)
{
$bootstrap = self::createBootstrap([Bootstrap::PARAM_REQUIRE_MAINTENANCE => $isExpected]);
$this->maintenanceMode->expects($this->once())->method('isOn')->willReturn($isOn);
$this->remoteAddress->expects($this->once())->method('getRemoteAddress')->willReturn(false);
$this->application->expects($this->never())->method('launch');
$this->application->expects($this->once())->method('catchException')->willReturn(true);
$bootstrap->run($this->application);
Expand All @@ -273,7 +280,7 @@ public function assertMaintenanceDataProvider()
{
return [
[true, false],
[false, true]
[false, true],
];
}

Expand Down

0 comments on commit 5dc15b1

Please sign in to comment.