Skip to content

Commit

Permalink
Merge pull request #1410 from magento-frontend/PR_09_08
Browse files Browse the repository at this point in the history
Fixed issues:
- MAGETWO-70571 There is no possibility to activate DEBUG logging in production mode
- MAGETWO-70806 The Recently Viewed widget UI issue
  • Loading branch information
slavvka authored Aug 16, 2017
2 parents a4c37be + c067714 commit 6ba5991
Show file tree
Hide file tree
Showing 9 changed files with 427 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
'listing' => [
'displayMode' => 'grid'
],
'column' => [
'image' => [
'imageCode' => 'recently_viewed_products_images_names_widget'
]
'image' => [
'imageCode' => 'recently_viewed_products_images_names_widget'
]
]
);
58 changes: 58 additions & 0 deletions app/code/Magento/Deploy/App/Mode/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Deploy\App\Mode;

/**
* This class is responsible for providing configuration while switching application mode
*/
class ConfigProvider
{
/**
* Configuration for combinations of $currentMode and $targetMode
* For example:
* [
* 'developer' => [
* 'production' => [
* {{setting_path}} => {{setting_value}}
* ]
* ]
* ]
*
* @var array
*/
private $config;

/**
* @param array $config
*/
public function __construct(array $config = [])
{
$this->config = $config;
}

/**
* Provide configuration while switching from $currentMode to $targetMode
* This method used in \Magento\Deploy\Model\Mode::setStoreMode
*
* For example: while switching from developer mode to production mode
* need to turn off 'dev/debug/debug_logging' setting in this case method
* will return array
* [
* {{setting_path}} => {{setting_value}}
* ]
*
* @param string $currentMode
* @param string $targetMode
* @return array
*/
public function getConfigs($currentMode, $targetMode)
{
if (isset($this->config[$currentMode][$targetMode])) {
return $this->config[$currentMode][$targetMode];
}
return [];
}
}
68 changes: 67 additions & 1 deletion app/code/Magento/Deploy/Model/Mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Magento\Deploy\Model;

use Magento\Deploy\App\Mode\ConfigProvider;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\DeploymentConfig\Reader;
use Magento\Framework\App\DeploymentConfig\Writer;
use Magento\Framework\App\Filesystem\DirectoryList;
Expand All @@ -14,7 +16,10 @@
use Magento\Framework\Config\File\ConfigFilePool;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Config\Console\Command\ConfigSet\ProcessorFacadeFactory;
use Magento\Config\Console\Command\EmulatedAdminhtmlAreaProcessor;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\App\ObjectManager;

/**
* A class to manage Magento modes
Expand Down Expand Up @@ -44,33 +49,70 @@ class Mode
*/
private $reader;

/**
* @var MaintenanceMode
*/
private $maintenanceMode;

/**
* @var Filesystem
*/
private $filesystem;

/**
* @var ConfigProvider
*/
private $configProvider;

/**
* The factory for processor facade.
*
* @var ProcessorFacadeFactory
*/
private $processorFacadeFactory;

/**
* Emulator adminhtml area for CLI command.
*
* @var EmulatedAdminhtmlAreaProcessor
*/
private $emulatedAreaProcessor;

/**
* @param InputInterface $input
* @param OutputInterface $output
* @param Writer $writer
* @param Reader $reader
* @param MaintenanceMode $maintenanceMode
* @param Filesystem $filesystem
* @param ConfigProvider $configProvider
* @param ProcessorFacadeFactory $processorFacadeFactory
* @param EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor
*/
public function __construct(
InputInterface $input,
OutputInterface $output,
Writer $writer,
Reader $reader,
MaintenanceMode $maintenanceMode,
Filesystem $filesystem
Filesystem $filesystem,
ConfigProvider $configProvider = null,
ProcessorFacadeFactory $processorFacadeFactory = null,
EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor = null
) {
$this->input = $input;
$this->output = $output;
$this->writer = $writer;
$this->reader = $reader;
$this->maintenanceMode = $maintenanceMode;
$this->filesystem = $filesystem;

$this->configProvider =
$configProvider ?: ObjectManager::getInstance()->get(ConfigProvider::class);
$this->processorFacadeFactory =
$processorFacadeFactory ?: ObjectManager::getInstance()->get(ProcessorFacadeFactory::class);
$this->emulatedAreaProcessor =
$emulatedAreaProcessor ?: ObjectManager::getInstance()->get(EmulatedAdminhtmlAreaProcessor::class);
}

/**
Expand Down Expand Up @@ -145,6 +187,7 @@ public function getMode()
*/
protected function setStoreMode($mode)
{
$this->saveAppConfigs($mode);
$data = [
ConfigFilePool::APP_ENV => [
State::PARAM_MODE => $mode
Expand All @@ -153,6 +196,29 @@ protected function setStoreMode($mode)
$this->writer->saveConfig($data);
}

/**
* Save application configs while switching mode
*
* @param string $mode
* @return void
*/
private function saveAppConfigs($mode)
{
$configs = $this->configProvider->getConfigs($this->getMode(), $mode);
foreach ($configs as $path => $value) {
$this->emulatedAreaProcessor->process(function () use ($path, $value) {
$this->processorFacadeFactory->create()->process(
$path,
$value,
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
null,
true
);
});
$this->output->writeln('Config "' . $path . ' = ' . $value . '" has been saved.');
}
}

/**
* Enable maintenance mode
*
Expand Down
27 changes: 27 additions & 0 deletions app/code/Magento/Deploy/Test/Unit/App/Mode/ConfigProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Deploy\Test\Unit\App\Mode;

use Magento\Deploy\App\Mode\ConfigProvider;

class ConfigProviderTest extends \PHPUnit\Framework\TestCase
{
public function testGetConfigs()
{
$expectedValue = [
'{{setting_path}}' => '{{setting_value}}'
];
$configProvider = new ConfigProvider(
[
'developer' => [
'production' => $expectedValue
]
]
);
$this->assertEquals($expectedValue, $configProvider->getConfigs('developer', 'production'));
$this->assertEquals([], $configProvider->getConfigs('undefined', 'production'));
}
}
90 changes: 89 additions & 1 deletion app/code/Magento/Deploy/Test/Unit/Model/ModeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
*/
namespace Magento\Deploy\Test\Unit\Model;

use Magento\Config\Console\Command\ConfigSet\ProcessorFacadeFactory;
use Magento\Config\Console\Command\ConfigSet\ProcessorFacade;
use Magento\Config\Console\Command\EmulatedAdminhtmlAreaProcessor;
use Magento\Deploy\App\Mode\ConfigProvider;
use Magento\Deploy\Model\Filesystem;
use Magento\Deploy\Model\Mode;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\DeploymentConfig\Reader;
use Magento\Framework\App\DeploymentConfig\Writer;
use Magento\Framework\App\MaintenanceMode;
Expand All @@ -19,6 +24,7 @@

/**
* @inheritdoc
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ModeTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -57,6 +63,26 @@ class ModeTest extends \PHPUnit\Framework\TestCase
*/
private $filesystemMock;

/**
* @var ConfigProvider|Mock
*/
private $configProvider;

/**
* @var ProcessorFacadeFactory|Mock
*/
private $processorFacadeFactory;

/**
* @var ProcessorFacade|Mock
*/
private $processorFacade;

/**
* @var EmulatedAdminhtmlAreaProcessor|Mock
*/
private $emulatedAreaProcessor;

protected function setUp()
{
$this->inputMock = $this->getMockBuilder(InputInterface::class)
Expand All @@ -75,14 +101,30 @@ protected function setUp()
$this->filesystemMock = $this->getMockBuilder(Filesystem::class)
->disableOriginalConstructor()
->getMock();
$this->configProvider = $this->getMockBuilder(ConfigProvider::class)
->disableOriginalConstructor()
->getMock();
$this->processorFacadeFactory = $this->getMockBuilder(ProcessorFacadeFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMockForAbstractClass();
$this->processorFacade = $this->getMockBuilder(ProcessorFacade::class)
->disableOriginalConstructor()
->getMock();
$this->emulatedAreaProcessor = $this->getMockBuilder(EmulatedAdminhtmlAreaProcessor::class)
->disableOriginalConstructor()
->getMock();

$this->model = new Mode(
$this->inputMock,
$this->outputMock,
$this->writerMock,
$this->readerMock,
$this->maintenanceMock,
$this->filesystemMock
$this->filesystemMock,
$this->configProvider,
$this->processorFacadeFactory,
$this->emulatedAreaProcessor
);
}

Expand Down Expand Up @@ -112,6 +154,9 @@ public function testEnableProductionMode()
State::PARAM_MODE => State::MODE_DEVELOPER,
],
];
$this->configProvider->expects($this->any())
->method('getConfigs')
->willReturn([]);
$this->writerMock->expects($this->once())
->method("saveConfig")
->willReturnCallback(function ($data) use (&$dataStorage) {
Expand Down Expand Up @@ -145,6 +190,12 @@ public function testEnableDeveloperModeOnFail()
State::PARAM_MODE => State::MODE_DEVELOPER,
],
];
$this->readerMock->expects($this->any())
->method('load')
->willReturn([State::PARAM_MODE => State::MODE_DEVELOPER]);
$this->configProvider->expects($this->any())
->method('getConfigs')
->willReturn([]);
$this->writerMock->expects($this->exactly(2))
->method("saveConfig")
->withConsecutive(
Expand All @@ -165,4 +216,41 @@ public function testEnableDeveloperModeOnFail()
$this->model->enableProductionMode();
$this->assertEquals(State::MODE_PRODUCTION, $mode);
}

public function testEnableProductionModeMinimal()
{
$this->readerMock->expects($this->once())
->method('load')
->willReturn([State::PARAM_MODE => State::MODE_DEVELOPER]);
$this->configProvider->expects($this->once())
->method('getConfigs')
->with('developer', 'production')
->willReturn([
'dev/debug/debug_logging' => 0
]);
$this->emulatedAreaProcessor->expects($this->once())
->method('process')
->willReturnCallback(function (\Closure $closure) {
return $closure->call($this->model);
});

$this->processorFacadeFactory->expects($this->once())
->method('create')
->willReturn($this->processorFacade);
$this->processorFacade
->expects($this->once())
->method('process')
->with(
'dev/debug/debug_logging',
0,
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
null,
true
);
$this->outputMock->expects($this->once())
->method('writeln')
->with('Config "dev/debug/debug_logging = 0" has been saved.');

$this->model->enableProductionModeMinimal();
}
}
11 changes: 11 additions & 0 deletions app/code/Magento/Deploy/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,15 @@
</argument>
</arguments>
</type>
<type name="Magento\Deploy\App\Mode\ConfigProvider">
<arguments>
<argument name="config" xsi:type="array">
<item name="developer" xsi:type="array">
<item name="production" xsi:type="array">
<item name="dev/debug/debug_logging" xsi:type="string">0</item>
</item>
</item>
</argument>
</arguments>
</type>
</config>
1 change: 0 additions & 1 deletion app/code/Magento/Developer/Model/Logger/Handler/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public function isHandling(array $record)
if ($this->deploymentConfig->isAvailable()) {
return
parent::isHandling($record)
&& $this->state->getMode() !== State::MODE_PRODUCTION
&& $this->scopeConfig->getValue('dev/debug/debug_logging', ScopeInterface::SCOPE_STORE);
}

Expand Down
Loading

0 comments on commit 6ba5991

Please sign in to comment.