Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add server environment for URL generation in sys:cron:run, fixes #871
In #871 Ash Smith reported an inconsistency of sys:cron:run with the Magento cron shell script regarding URL generation. Fix is to set the $_SERVER superglobals likewise the shell script does. Note: There is a small chance this fix is not complete and it requires to reset the server configuration because $_SERVER change is too late. How this is down is exemplarly shown in the test case. Refs: - #871 - Command: sys:cron:run
- Loading branch information
Showing
5 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: mot | ||
* Date: 13.12.16 | ||
* Time: 00:08 | ||
*/ | ||
|
||
namespace N98\Magento\Command\System\Cron; | ||
|
||
use BadMethodCallException; | ||
|
||
/** | ||
* Class ServerEnvironment | ||
* | ||
* Set $_SERVER environment for URL generating while sys:cron:run | ||
* | ||
* @see https://github.com/netz98/n98-magerun/issues/871 | ||
* | ||
* @package N98\Magento\Command\System\Cron | ||
*/ | ||
class ServerEnvironment | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $backup; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $keys; | ||
|
||
public function __construct() | ||
{ | ||
$this->keys = array('SCRIPT_NAME', 'SCRIPT_FILENAME'); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function initalize() | ||
{ | ||
if (isset($this->backup)) { | ||
throw new BadMethodCallException('Environment already backed up, can\'t initialize any longer'); | ||
} | ||
|
||
if (!is_array($GLOBALS['argv'])) { | ||
throw new \UnexpectedValueException('Need argv to work'); | ||
} | ||
|
||
$basename = basename($GLOBALS['argv'][0]); | ||
|
||
foreach ($this->keys as $key) { | ||
$buffer = $_SERVER[$key]; | ||
$this->backup[$key] = $buffer; | ||
$_SERVER[$key] = str_replace($basename, 'index.php', $buffer); | ||
} | ||
} | ||
|
||
public function reset() | ||
{ | ||
if (false === isset($this->backup)) { | ||
throw new BadMethodCallException('Environment not yet backed up, initalize first, can\'t reset'); | ||
} | ||
|
||
foreach ($this->backup as $key => $value) { | ||
$_SERVER[$key] = $value; | ||
} | ||
|
||
$this->backup = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
tests/N98/Magento/Command/System/Cron/ServerEnvironmentTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace N98\Magento\Command\System\Cron; | ||
|
||
use N98\Magento\Command\TestCase; | ||
|
||
class ServerEnvironmentTest extends TestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
// Initialise Magento autoloader (if not yet) | ||
$application = $this->getApplication(); | ||
$this->assertInstanceOf('N98\Magento\Application', $application); | ||
} | ||
|
||
/** | ||
* @test that getBaseUrl contains the script-name (here: Phpunit runner) | ||
*/ | ||
public function regression() | ||
{ | ||
$store = \Mage::app()->getStore(null); | ||
$actual = $store->getBaseUrl(\Mage_Core_Model_Store::URL_TYPE_LINK); | ||
$this->assertInternalType('string', $actual); | ||
$this->assertRegExp('~/(ide-phpunit.php|phpunit)/$~', $actual); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function environmentFix() | ||
{ | ||
$store = \Mage::app()->getStore(null); | ||
$store->resetConfig(); | ||
|
||
$environment = new ServerEnvironment(); | ||
$environment->initalize(); | ||
|
||
$actual = $store->getBaseUrl(\Mage_Core_Model_Store::URL_TYPE_LINK); | ||
$this->assertInternalType('string', $actual); | ||
$this->assertStringEndsWith('/index.php/', $actual); | ||
|
||
$store->resetConfig(); | ||
|
||
$environment->reset(); | ||
|
||
$actual = \Mage::app()->getStore(null)->getBaseUrl(\Mage_Core_Model_Store::URL_TYPE_LINK); | ||
$this->assertInternalType('string', $actual); | ||
$this->assertRegExp('~/(ide-phpunit.php|phpunit)/$~', $actual); | ||
} | ||
} |