Skip to content

Commit

Permalink
Add server environment for URL generation in sys:cron:run, fixes #871
Browse files Browse the repository at this point in the history
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
ktomk committed Dec 16, 2016
1 parent b997b5b commit d29da5d
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@ RECENT CHANGES

1.97.28
-------
* Fix: URL generation in sys:cron:run (report by Ash Smith, fix by Tom Klingenberg, #871)
* Fix: Indexer dies on error (report by Henry Hirsch, fix by Tom Klingenberg, #701)
* Fix: Incompatibilities with PHP 7.1 (report by Don Bosco van Hoi, fix by Tom Klingenberg, #881)
* Fix: Warning db:import sprintf too few arguments (report by Peter Jaap Blaakmeer, fix by Tom Klingenberg, #884)
Expand Down
3 changes: 3 additions & 0 deletions src/N98/Magento/Command/System/Cron/RunCommand.php
Expand Up @@ -147,6 +147,9 @@ private function executeConfigModel($callback, $jobCode)
throw new RuntimeException('Failed to create new Mage_Cron_Model_Schedule model');
}

$environment = new ServerEnvironment();
$environment->initalize();

try {
$timestamp = strftime('%Y-%m-%d %H:%M:%S', time());
$schedule
Expand Down
73 changes: 73 additions & 0 deletions src/N98/Magento/Command/System/Cron/ServerEnvironment.php
@@ -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;
}
}
19 changes: 19 additions & 0 deletions tests/N98/Magento/Command/System/Cron/RunCommandTest.php
Expand Up @@ -22,4 +22,23 @@ public function testExecute()

$this->assertRegExp('/Run Mage_Log_Model_Cron::logClean done/', $commandTester->getDisplay());
}

/**
* @test
*/
public function urlBuildingWhileCron()
{
$application = $this->getApplication();
$application->add(new RunCommand());
$command = $this->getApplication()->find('sys:cron:run');

$commandTester = new CommandTester($command);
$commandTester->execute(
array(
'command' => $command->getName(),
'job' => 'log_clean', )
);

$this->assertRegExp('/Run Mage_Log_Model_Cron::logClean done/', $commandTester->getDisplay());
}
}
52 changes: 52 additions & 0 deletions tests/N98/Magento/Command/System/Cron/ServerEnvironmentTest.php
@@ -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);
}
}

0 comments on commit d29da5d

Please sign in to comment.