Skip to content

Commit

Permalink
Merge branch '2.4-develop' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Charlie authored Jul 24, 2020
2 parents e495a51 + 8f42112 commit 274103d
Show file tree
Hide file tree
Showing 75 changed files with 2,042 additions and 550 deletions.
21 changes: 18 additions & 3 deletions app/code/Magento/Analytics/Model/ReportWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Analytics\Model;

use Magento\Analytics\ReportXml\DB\ReportValidator;
use Magento\Framework\Filesystem\Directory\WriteInterface;

/**
* Writes reports in files in csv format
* @inheritdoc
*/
class ReportWriter implements ReportWriterInterface
{
Expand Down Expand Up @@ -54,7 +55,7 @@ public function __construct(
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function write(WriteInterface $directory, $path)
{
Expand All @@ -81,7 +82,7 @@ public function write(WriteInterface $directory, $path)
$headers = array_keys($row);
$stream->writeCsv($headers);
}
$stream->writeCsv($row);
$stream->writeCsv($this->prepareRow($row));
}
$stream->unlock();
$stream->close();
Expand All @@ -98,4 +99,18 @@ public function write(WriteInterface $directory, $path)

return true;
}

/**
* Replace wrong symbols in row
*
* @param array $row
* @return array
*/
private function prepareRow(array $row): array
{
$row = preg_replace('/(?<!\\\\)"/', '\\"', $row);
$row = preg_replace('/[\\\\]+/', '\\', $row);

return $row;
}
}
36 changes: 21 additions & 15 deletions app/code/Magento/Analytics/Test/Unit/Model/ReportWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
use Magento\Analytics\Model\ReportWriter;
use Magento\Analytics\ReportXml\DB\ReportValidator;
use Magento\Analytics\ReportXml\ReportProvider;
use Magento\Framework\Filesystem\Directory\WriteInterface;
use Magento\Framework\Filesystem\Directory\WriteInterface as DirectoryWriteInterface;
use Magento\Framework\Filesystem\File\WriteInterface as FileWriteInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -48,7 +49,7 @@ class ReportWriterTest extends TestCase
private $objectManagerHelper;

/**
* @var WriteInterface|MockObject
* @var DirectoryWriteInterface|MockObject
*/
private $directoryMock;

Expand Down Expand Up @@ -82,7 +83,7 @@ protected function setUp(): void
$this->reportValidatorMock = $this->createMock(ReportValidator::class);
$this->providerFactoryMock = $this->createMock(ProviderFactory::class);
$this->reportProviderMock = $this->createMock(ReportProvider::class);
$this->directoryMock = $this->getMockBuilder(WriteInterface::class)
$this->directoryMock = $this->getMockBuilder(DirectoryWriteInterface::class)
->getMockForAbstractClass();
$this->objectManagerHelper = new ObjectManagerHelper($this);

Expand All @@ -98,16 +99,15 @@ protected function setUp(): void

/**
* @param array $configData
* @param array $fileData
* @param array $expectedFileData
* @return void
*
* @dataProvider configDataProvider
*/
public function testWrite(array $configData)
public function testWrite(array $configData, array $fileData, array $expectedFileData): void
{
$errors = [];
$fileData = [
['number' => 1, 'type' => 'Shoes Usual']
];
$this->configInterfaceMock
->expects($this->once())
->method('get')
Expand All @@ -126,7 +126,7 @@ public function testWrite(array $configData)
->with($parameterName ?: null)
->willReturn($fileData);
$errorStreamMock = $this->getMockBuilder(
\Magento\Framework\Filesystem\File\WriteInterface::class
FileWriteInterface::class
)->getMockForAbstractClass();
$errorStreamMock
->expects($this->once())
Expand All @@ -136,8 +136,8 @@ public function testWrite(array $configData)
->expects($this->exactly(2))
->method('writeCsv')
->withConsecutive(
[array_keys($fileData[0])],
[$fileData[0]]
[array_keys($expectedFileData[0])],
[$expectedFileData[0]]
);
$errorStreamMock->expects($this->once())->method('unlock');
$errorStreamMock->expects($this->once())->method('close');
Expand All @@ -164,12 +164,12 @@ public function testWrite(array $configData)
*
* @dataProvider configDataProvider
*/
public function testWriteErrorFile($configData)
public function testWriteErrorFile(array $configData): void
{
$errors = ['orders', 'SQL Error: test'];
$this->configInterfaceMock->expects($this->once())->method('get')->willReturn([$configData]);
$errorStreamMock = $this->getMockBuilder(
\Magento\Framework\Filesystem\File\WriteInterface::class
FileWriteInterface::class
)->getMockForAbstractClass();
$errorStreamMock->expects($this->once())->method('lock');
$errorStreamMock->expects($this->once())->method('writeCsv')->with($errors);
Expand All @@ -184,7 +184,7 @@ public function testWriteErrorFile($configData)
/**
* @return void
*/
public function testWriteEmptyReports()
public function testWriteEmptyReports(): void
{
$this->configInterfaceMock->expects($this->once())->method('get')->willReturn([]);
$this->reportValidatorMock->expects($this->never())->method('validate');
Expand All @@ -195,11 +195,11 @@ public function testWriteEmptyReports()
/**
* @return array
*/
public function configDataProvider()
public function configDataProvider(): array
{
return [
'reportProvider' => [
[
'configData' => [
'providers' => [
[
'name' => $this->providerName,
Expand All @@ -209,6 +209,12 @@ public function configDataProvider()
],
]
]
],
'fileData' => [
['number' => 1, 'type' => 'Shoes\"" Usual\\\\"']
],
'expectedFileData' => [
['number' => 1, 'type' => 'Shoes\"\" Usual\\"']
]
],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Controller\Adminhtml\Product\Attribute;

use Magento\Backend\App\Action\Context;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute as AttributeAction;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Eav\Model\Entity\Attribute\Set;
use Magento\Eav\Model\Validator\Attribute\Code as AttributeCodeValidator;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Cache\FrontendInterface;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Escaper;
use Magento\Framework\Registry;
use Magento\Framework\Serialize\Serializer\FormData;
use Magento\Framework\View\LayoutFactory;
use Magento\Framework\View\Result\PageFactory;

/**
* Product attribute validate controller.
Expand All @@ -25,12 +35,12 @@ class Validate extends AttributeAction implements HttpGetActionInterface, HttpPo
const DEFAULT_MESSAGE_KEY = 'message';

/**
* @var \Magento\Framework\Controller\Result\JsonFactory
* @var JsonFactory
*/
protected $resultJsonFactory;

/**
* @var \Magento\Framework\View\LayoutFactory
* @var LayoutFactory
*/
protected $layoutFactory;

Expand All @@ -57,25 +67,25 @@ class Validate extends AttributeAction implements HttpGetActionInterface, HttpPo
/**
* Constructor
*
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Cache\FrontendInterface $attributeLabelCache
* @param \Magento\Framework\Registry $coreRegistry
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
* @param Context $context
* @param FrontendInterface $attributeLabelCache
* @param Registry $coreRegistry
* @param PageFactory $resultPageFactory
* @param JsonFactory $resultJsonFactory
* @param LayoutFactory $layoutFactory
* @param array $multipleAttributeList
* @param FormData|null $formDataSerializer
* @param AttributeCodeValidator|null $attributeCodeValidator
* @param Escaper $escaper
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Cache\FrontendInterface $attributeLabelCache,
\Magento\Framework\Registry $coreRegistry,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
\Magento\Framework\View\LayoutFactory $layoutFactory,
Context $context,
FrontendInterface $attributeLabelCache,
Registry $coreRegistry,
PageFactory $resultPageFactory,
JsonFactory $resultJsonFactory,
LayoutFactory $layoutFactory,
array $multipleAttributeList = [],
FormData $formDataSerializer = null,
AttributeCodeValidator $attributeCodeValidator = null,
Expand All @@ -96,7 +106,7 @@ public function __construct(
/**
* @inheritdoc
*
* @return \Magento\Framework\Controller\ResultInterface
* @return ResultInterface
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
Expand All @@ -118,14 +128,22 @@ public function execute()

$attributeCode = $this->getRequest()->getParam('attribute_code');
$frontendLabel = $this->getRequest()->getParam('frontend_label');
$attributeCode = $attributeCode ?: $this->generateCode($frontendLabel[0]);
$attributeId = $this->getRequest()->getParam('attribute_id');
$attribute = $this->_objectManager->create(
\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
)->loadByCode(
$this->_entityTypeId,
$attributeCode
);

if ($attributeId) {
$attribute = $this->_objectManager->create(
Attribute::class
)->load($attributeId);
$attributeCode = $attribute->getAttributeCode();
} else {
$attributeCode = $attributeCode ?: $this->generateCode($frontendLabel[0]);
$attribute = $this->_objectManager->create(
Attribute::class
)->loadByCode(
$this->_entityTypeId,
$attributeCode
);
}

if ($attribute->getId() && !$attributeId || $attributeCode === 'product_type' || $attributeCode === 'type_id') {
$message = strlen($this->getRequest()->getParam('attribute_code'))
Expand All @@ -145,8 +163,8 @@ public function execute()

if ($this->getRequest()->has('new_attribute_set_name')) {
$setName = $this->getRequest()->getParam('new_attribute_set_name');
/** @var $attributeSet \Magento\Eav\Model\Entity\Attribute\Set */
$attributeSet = $this->_objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class);
/** @var $attributeSet Set */
$attributeSet = $this->_objectManager->create(Set::class);
$attributeSet->setEntityTypeId($this->_entityTypeId)->load($setName, 'attribute_set_name');
if ($attributeSet->getId()) {
$setName = $this->escaper->escapeHtml($setName);
Expand Down Expand Up @@ -252,7 +270,7 @@ private function checkUniqueOption(DataObject $response, array $options = null)
private function checkEmptyOption(DataObject $response, array $optionsForCheck = null)
{
foreach ($optionsForCheck as $optionValues) {
if (isset($optionValues[0]) && trim($optionValues[0]) == '') {
if (isset($optionValues[0]) && trim((string)$optionValues[0]) == '') {
$this->setMessageToResponse($response, [__("The value of Admin scope can't be empty.")]);
$response->setError(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public function beforeSave($object)
}
$object->setData($attributeCode, implode(',', $data) ?: null);
}
if ($attributeCode == 'default_sort_by') {
$data = $object->getData($attributeCode);
$attributeValue = (is_array($data) ? reset($data) : (!empty($data))) ? $data : null;
$object->setData($attributeCode, $attributeValue);
}
if (!$object->hasData($attributeCode)) {
$object->setData($attributeCode, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
namespace Magento\Catalog\Model\Plugin;

use Magento\Catalog\Model\Category\DataProvider;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Sets the default value for Category Design Layout if provided
Expand All @@ -21,11 +24,28 @@ class SetPageLayoutDefaultValue
private $defaultValue;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @param ScopeConfigInterface $scopeConfig
* @param StoreManagerInterface $storeManager
* @param string $defaultValue
*/
public function __construct(string $defaultValue = "")
{
public function __construct(
ScopeConfigInterface $scopeConfig,
StoreManagerInterface $storeManager,
string $defaultValue = ""
) {
$this->defaultValue = $defaultValue;
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
}

/**
Expand All @@ -42,7 +62,15 @@ public function afterGetDefaultMetaData(DataProvider $subject, array $result): a
$currentCategory = $subject->getCurrentCategory();

if ($currentCategory && !$currentCategory->getId() && array_key_exists('page_layout', $result)) {
$result['page_layout']['default'] = $this->defaultValue ?: null;
$defaultAdminValue = $this->scopeConfig->getValue(
'web/default_layouts/default_category_layout',
ScopeInterface::SCOPE_STORE,
$this->storeManager->getStore()->getId()
);

$defaultValue = $defaultAdminValue ?: $this->defaultValue;

$result['page_layout']['default'] = $defaultValue ?: null;
}

return $result;
Expand Down
Loading

0 comments on commit 274103d

Please sign in to comment.