Skip to content

Commit e381231

Browse files
author
Olexandr Lysenko
committed
Merge remote-tracking branch 'origin/MAGETWO-58386' into bugfixes
2 parents d7b9a02 + f3d785c commit e381231

File tree

11 files changed

+209
-138
lines changed

11 files changed

+209
-138
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Eav\Model;
7+
8+
9+
use Magento\Framework\Api\AttributeInterface;
10+
use Magento\Framework\Api\CustomAttributesDataInterface;
11+
use Magento\Eav\Api\AttributeRepositoryInterface;
12+
use Magento\Framework\Api\SearchCriteriaBuilder;
13+
use Magento\Framework\EntityManager\MapperInterface;
14+
use Magento\Framework\EntityManager\MetadataPool;
15+
16+
/**
17+
* Class CustomAttributesMapper
18+
*/
19+
class CustomAttributesMapper implements MapperInterface
20+
{
21+
/**
22+
* @var AttributeRepositoryInterface
23+
*/
24+
private $attributeRepository;
25+
26+
/**
27+
* @var MetadataPool
28+
*/
29+
private $metadataPool;
30+
31+
/**
32+
* @var SearchCriteriaBuilder
33+
*/
34+
private $searchCriteriaBuilder;
35+
36+
/**
37+
* @var array
38+
*/
39+
private $attributes;
40+
41+
/**
42+
* @param AttributeRepositoryInterface $attributeRepository
43+
* @param MetadataPool $metadataPool
44+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
45+
*/
46+
public function __construct(
47+
AttributeRepositoryInterface $attributeRepository,
48+
MetadataPool $metadataPool,
49+
SearchCriteriaBuilder $searchCriteriaBuilder
50+
) {
51+
$this->attributeRepository = $attributeRepository;
52+
$this->metadataPool = $metadataPool;
53+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
public function entityToDatabase($entityType, $data)
60+
{
61+
if (!$this->metadataPool->hasConfiguration($entityType)
62+
|| !$this->metadataPool->getMetadata($entityType)->getEavEntityType()
63+
) {
64+
return $data;
65+
}
66+
if (isset($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])) {
67+
foreach ($this->getNonStaticAttributes($entityType) as $attribute) {
68+
foreach ($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES] as $key => $customAttribute) {
69+
if ($customAttribute[AttributeInterface::ATTRIBUTE_CODE] == $attribute->getAttributeCode()) {
70+
unset($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES][$key]);
71+
$data[$attribute->getAttributeCode()] = $customAttribute[AttributeInterface::VALUE];
72+
}
73+
}
74+
}
75+
}
76+
return $data;
77+
}
78+
79+
/**
80+
* {@inheritdoc}
81+
*/
82+
public function databaseToEntity($entityType, $data)
83+
{
84+
$metadata = $this->metadataPool->getMetadata($entityType);
85+
if (!$metadata->getEavEntityType()) {
86+
return $data;
87+
}
88+
foreach ($this->getNonStaticAttributes($entityType) as $attribute) {
89+
if (isset($data[$attribute->getAttributeCode()])) {
90+
$data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES][] = [
91+
AttributeInterface::ATTRIBUTE_CODE => $attribute->getAttributeCode(),
92+
AttributeInterface::VALUE => $data[$attribute->getAttributeCode()]
93+
];
94+
}
95+
}
96+
return $data;
97+
}
98+
99+
/**
100+
* Get custom attributes
101+
*
102+
* @param string $entityType
103+
* @return \Magento\Eav\Api\Data\AttributeInterface[]
104+
* @throws \Exception
105+
*/
106+
private function getNonStaticAttributes($entityType)
107+
{
108+
if (!isset($this->attributes[$entityType])) {
109+
$metadata = $this->metadataPool->getMetadata($entityType);
110+
$searchResult = $this->attributeRepository->getList(
111+
$metadata->getEavEntityType(),
112+
$this->searchCriteriaBuilder->addFilter('attribute_set_id', null, 'neq')->create()
113+
);
114+
$attributes = [];
115+
foreach ($searchResult->getItems() as $attribute) {
116+
if (!$attribute->isStatic()) {
117+
$attributes[] = $attribute;
118+
}
119+
}
120+
$this->attributes[$entityType] = $attributes;
121+
}
122+
return $this->attributes[$entityType];
123+
}
124+
}
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
* Copyright © 2016 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
namespace Magento\Framework\EntityManager\Test\Unit;
7-
8-
use Magento\Framework\App\ResourceConnection;
6+
namespace Magento\Eav\Test\Unit\Model;
97

8+
/**
9+
* Class CustomAttributesMapperTest
10+
*/
1011
class CustomAttributesMapperTest extends \PHPUnit_Framework_TestCase
1112
{
1213
/**
@@ -55,7 +56,7 @@ public function testEntityToDatabase()
5556
->willReturn(true);
5657
$metadataPool->expects($this->any())
5758
->method('getMetadata')
58-
->with($this->equalTo(\Magento\Customer\Api\Data\AddressInterface::class))
59+
->with($this->equalTo(\Magento\Framework\Api\CustomAttributesDataInterface::class))
5960
->will($this->returnValue($metadata));
6061
$metadataPool->expects($this->once())
6162
->method('hasConfiguration')
@@ -75,16 +76,16 @@ public function testEntityToDatabase()
7576
->method('create')
7677
->will($this->returnValue($searchCriteria));
7778

78-
/** @var \Magento\Framework\EntityManager\CustomAttributesMapper $customAttributesMapper */
79+
/** @var \Magento\Eav\Model\CustomAttributesMapper $customAttributesMapper */
7980
$customAttributesMapper = $this->objectManager
80-
->getObject(\Magento\Framework\EntityManager\CustomAttributesMapper::class, [
81+
->getObject(\Magento\Eav\Model\CustomAttributesMapper::class, [
8182
'attributeRepository' => $attributeRepository,
8283
'metadataPool' => $metadataPool,
8384
'searchCriteriaBuilder' => $searchCriteriaBuilder
8485
]);
8586

8687
$actual = $customAttributesMapper->entityToDatabase(
87-
\Magento\Customer\Api\Data\AddressInterface::class,
88+
\Magento\Framework\Api\CustomAttributesDataInterface::class,
8889
[
8990
\Magento\Framework\Api\CustomAttributesDataInterface::CUSTOM_ATTRIBUTES => [
9091
'test' => [
@@ -151,7 +152,7 @@ public function testDatabaseToEntity()
151152
->getMock();
152153
$metadataPool->expects($this->any())
153154
->method('getMetadata')
154-
->with($this->equalTo(\Magento\Customer\Api\Data\AddressInterface::class))
155+
->with($this->equalTo(\Magento\Framework\Api\CustomAttributesDataInterface::class))
155156
->will($this->returnValue($metadata));
156157

157158
$searchCriteriaBuilder = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaBuilder::class)
@@ -168,15 +169,15 @@ public function testDatabaseToEntity()
168169
->method('create')
169170
->will($this->returnValue($searchCriteria));
170171

171-
/** @var \Magento\Framework\EntityManager\CustomAttributesMapper $customAttributesMapper */
172+
/** @var \Magento\Eav\Model\CustomAttributesMapper $customAttributesMapper */
172173
$customAttributesMapper = $this->objectManager
173-
->getObject(\Magento\Framework\EntityManager\CustomAttributesMapper::class, [
174+
->getObject(\Magento\Eav\Model\CustomAttributesMapper::class, [
174175
'attributeRepository' => $attributeRepository,
175176
'metadataPool' => $metadataPool,
176177
'searchCriteriaBuilder' => $searchCriteriaBuilder
177178
]);
178179
$actual = $customAttributesMapper->databaseToEntity(
179-
\Magento\Customer\Api\Data\AddressInterface::class,
180+
\Magento\Framework\Api\CustomAttributesDataInterface::class,
180181
[
181182
'test' => 'test',
182183
'test4' => 'test4',
@@ -200,10 +201,10 @@ public function testDatabaseToEntity()
200201
private function getAttributes()
201202
{
202203
/* Attribute with the code we want to copy */
203-
$attribute = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
204+
$attribute = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
204205
->disableOriginalConstructor()
205206
->setMethods(['isStatic', 'getAttributeCode'])
206-
->getMock();
207+
->getMockForAbstractClass();
207208
$attribute->expects($this->any())
208209
->method('isStatic')
209210
->will($this->returnValue(false));
@@ -212,10 +213,10 @@ private function getAttributes()
212213
->will($this->returnValue('test'));
213214

214215
/* Attribute with the code we don't want to copy */
215-
$attribute1 = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
216+
$attribute1 = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
216217
->disableOriginalConstructor()
217218
->setMethods(['isStatic', 'getAttributeCode'])
218-
->getMock();
219+
->getMockForAbstractClass();
219220
$attribute1->expects($this->any())
220221
->method('isStatic')
221222
->will($this->returnValue(false));
@@ -224,10 +225,10 @@ private function getAttributes()
224225
->will($this->returnValue('test1'));
225226

226227
/* Static attribute but with the code which exists in custom attributes */
227-
$attribute2 = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
228+
$attribute2 = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
228229
->disableOriginalConstructor()
229230
->setMethods(['isStatic', 'getAttributeCode'])
230-
->getMock();
231+
->getMockForAbstractClass();
231232
$attribute2->expects($this->any())
232233
->method('isStatic')
233234
->will($this->returnValue(true));

app/code/Magento/Eav/etc/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,10 @@
152152
<argument name="collectionProcessor" xsi:type="object">Magento\Eav\Model\Api\SearchCriteria\AttributeGroupCollectionProcessor</argument>
153153
</arguments>
154154
</type>
155+
<type name="Magento\Framework\EntityManager\CustomAttributesMapper">
156+
<arguments>
157+
<argument name="mapper" xsi:type="object">Magento\Eav\Model\CustomAttributesMapper</argument>
158+
</arguments>
159+
</type>
155160
</config>
156161

dev/tests/static/testsuite/Magento/Test/Integrity/Library/DependencyTest.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,33 @@ class DependencyTest extends \PHPUnit_Framework_TestCase
2727
protected $errors = [];
2828

2929
/**
30-
* Forbidden base namespaces
30+
* Allowed sub namespaces
3131
*
3232
* @return array
3333
*/
34-
protected function getForbiddenNamespaces()
34+
protected function getAllowedNamespaces()
3535
{
36-
return ['Magento'];
36+
return [
37+
'Framework',
38+
'SomeModule',
39+
'ModuleName',
40+
'Setup\Console\CommandList',
41+
'Setup\Console\CompilerPreparation',
42+
'Setup\Model\ObjectManagerProvider',
43+
'Setup\Mvc\Bootstrap\InitParamListener',
44+
'Store\Model\ScopeInterface',
45+
'Store\Model\StoreManagerInterface',
46+
'Directory\Model\CurrencyFactory',
47+
'PageCache\Model\Cache\Type',
48+
'Backup\Model\ResourceModel\Db',
49+
'Backend\Block\Widget\Button',
50+
'Ui\Component\Container',
51+
'SalesRule\Model\Rule',
52+
'SalesRule\Api\Data\RuleInterface',
53+
'SalesRule\Model\Rule\Interceptor',
54+
'SalesRule\Model\Rule\Proxy',
55+
'Theme\Model\View\Design'
56+
];
3757
}
3858

3959
public function testCheckDependencies()
@@ -53,15 +73,15 @@ function ($file) {
5373
(new Injectable())->getDependencies($fileReflection),
5474
$tokens->getDependencies()
5575
);
56-
57-
$pattern = '#^(\\\\|)' . implode('|', $this->getForbiddenNamespaces()) . '\\\\#';
76+
$allowedNamespaces = str_replace('\\','\\\\', implode('|', $this->getAllowedNamespaces()));
77+
$pattern = '#Magento\\\\(?!' . $allowedNamespaces . ').*#';
5878
foreach ($dependencies as $dependency) {
59-
$dependencyPaths = explode('/', $dependency);
79+
$dependencyPaths = explode('\\', $dependency);
6080
$dependencyPaths = array_slice($dependencyPaths, 2);
61-
$dependency = implode('\\', $dependencyPaths);
81+
$dependencyPath = implode('\\', $dependencyPaths);
6282
$libraryPaths = $componentRegistrar->getPaths(ComponentRegistrar::LIBRARY);
6383
foreach ($libraryPaths as $libraryPath) {
64-
$filePath = str_replace('\\', '/', $libraryPath . '/' . $dependency . '.php');
84+
$filePath = str_replace('\\', '/', $libraryPath . '/' . $dependencyPath . '.php');
6585
if (preg_match($pattern, $dependency) && !file_exists($filePath)) {
6686
$this->errors[$fileReflection->getFileName()][] = $dependency;
6787
}

lib/internal/Magento/Framework/App/Router/Base.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
*/
88
namespace Magento\Framework\App\Router;
99

10-
use Magento\Framework\App\RequestInterface;
11-
use Magento\Store\Model\ScopeInterface;
12-
1310
/**
1411
* @SuppressWarnings(PHPMD.TooManyFields)
1512
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)

lib/internal/Magento/Framework/App/Test/Unit/Action/AbstractActionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function setUp()
3636
->setMethods(['setRefererOrBaseUrl'])
3737
->disableOriginalConstructor()
3838
->getMock();
39-
$this->redirectFactory = $this->getMockBuilder(\Magento\Backend\Model\View\Result\RedirectFactory::class)
39+
$this->redirectFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\RedirectFactory::class)
4040
->setMethods(['create'])
4141
->disableOriginalConstructor()
4242
->getMock();

lib/internal/Magento/Framework/App/Test/Unit/Router/BaseTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function testMatch()
8080
$moduleFrontName = 'module front name';
8181
$actionPath = 'action path';
8282
$actionName = 'action name';
83-
$actionClassName = \Magento\Cms\Controller\Index\Index::class;
83+
$actionClassName = \Magento\Framework\App\Action\Action::class;
8484
$moduleName = 'module name';
8585
$moduleList = [$moduleName];
8686

@@ -109,7 +109,7 @@ public function testMatchUseParams()
109109
$moduleFrontName = 'module front name';
110110
$actionPath = 'action path';
111111
$actionName = 'action name';
112-
$actionClassName = \Magento\Cms\Controller\Index\Index::class;
112+
$actionClassName = \Magento\Framework\App\Action\Action::class;
113113
$moduleName = 'module name';
114114
$moduleList = [$moduleName];
115115
$paramList = $moduleFrontName . '/' . $actionPath . '/' . $actionName . '/key/val/key2/val2/';
@@ -137,7 +137,7 @@ public function testMatchUseDefaultPath()
137137
$moduleFrontName = 'module front name';
138138
$actionPath = 'action path';
139139
$actionName = 'action name';
140-
$actionClassName = \Magento\Cms\Controller\Index\Index::class;
140+
$actionClassName = \Magento\Framework\App\Action\Action::class;
141141
$moduleName = 'module name';
142142
$moduleList = [$moduleName];
143143

@@ -169,7 +169,7 @@ public function testMatchEmptyModuleList()
169169
$moduleFrontName = 'module front name';
170170
$actionPath = 'action path';
171171
$actionName = 'action name';
172-
$actionClassName = \Magento\Cms\Controller\Index\Index::class;
172+
$actionClassName = \Magento\Framework\App\Action\Action::class;
173173
$emptyModuleList = [];
174174

175175
// Stubs
@@ -192,7 +192,7 @@ public function testMatchEmptyActionInstance()
192192
$moduleFrontName = 'module front name';
193193
$actionPath = 'action path';
194194
$actionName = 'action name';
195-
$actionClassName = \Magento\Cms\Controller\Index\Index::class;
195+
$actionClassName = \Magento\Framework\App\Action\Action::class;
196196
$moduleName = 'module name';
197197
$moduleList = [$moduleName];
198198

0 commit comments

Comments
 (0)