Skip to content

Commit 071e731

Browse files
author
Bohdan Korablov
committed
MAGETWO-43793: [github] broken references when viewing admin after initial install #1795
1 parent 9b1f7f5 commit 071e731

File tree

4 files changed

+222
-18
lines changed

4 files changed

+222
-18
lines changed

lib/internal/Magento/Framework/View/Layout/Data/Structure.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Framework\View\Layout\Data;
77

88
use Magento\Framework\Data\Structure as DataStructure;
9+
use Magento\Framework\App\State;
910

1011
/**
1112
* An associative data structure, that features "nested set" parent-child relations
@@ -24,17 +25,25 @@ class Structure extends DataStructure
2425
*/
2526
protected $logger;
2627

28+
/**
29+
* @var \Magento\Framework\App\State
30+
*/
31+
protected $state;
32+
2733
/**
2834
* Constructor
2935
*
3036
* @param \Psr\Log\LoggerInterface $logger
37+
* @param \Magento\Framework\App\State $state
3138
* @param array $elements
3239
*/
3340
public function __construct(
3441
\Psr\Log\LoggerInterface $logger,
42+
\Magento\Framework\App\State $state,
3543
array $elements = null
3644
) {
3745
$this->logger = $logger;
46+
$this->state = $state;
3847
parent::__construct($elements);
3948
}
4049

@@ -109,10 +118,12 @@ public function reorderChildElement($parentName, $childName, $offsetOrSibling, $
109118
if ($childName !== $sibling) {
110119
$siblingParentName = $this->getParentId($sibling);
111120
if ($parentName !== $siblingParentName) {
112-
$this->logger->critical(
113-
"Broken reference: the '{$childName}' tries to reorder itself towards '{$sibling}', but " .
114-
"their parents are different: '{$parentName}' and '{$siblingParentName}' respectively."
115-
);
121+
if ($this->state->getMode() == State::MODE_DEVELOPER) {
122+
$this->logger->critical(
123+
"Broken reference: the '{$childName}' tries to reorder itself towards '{$sibling}', but " .
124+
"their parents are different: '{$parentName}' and '{$siblingParentName}' respectively."
125+
);
126+
}
116127
return;
117128
}
118129
$this->reorderToSibling($parentName, $childName, $sibling, $after ? 1 : -1);

lib/internal/Magento/Framework/View/Layout/ScheduledStructure/Helper.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Framework\View\Layout\ScheduledStructure;
77

88
use Magento\Framework\View\Layout;
9+
use Magento\Framework\App\State;
910

1011
class Helper
1112
{
@@ -31,13 +32,21 @@ class Helper
3132
*/
3233
protected $logger;
3334

35+
/**
36+
* @var \Magento\Framework\App\State
37+
*/
38+
protected $state;
39+
3440
/**
3541
* @param \Psr\Log\LoggerInterface $logger
42+
* @param \Magento\Framework\App\State $state
3643
*/
3744
public function __construct(
38-
\Psr\Log\LoggerInterface $logger
45+
\Psr\Log\LoggerInterface $logger,
46+
\Magento\Framework\App\State $state
3947
) {
4048
$this->logger = $logger;
49+
$this->state = $state;
4150
}
4251

4352
/**
@@ -190,10 +199,13 @@ public function scheduleElement(
190199
}
191200
} else {
192201
$scheduledStructure->setElementToBrokenParentList($key);
193-
$this->logger->critical(
194-
"Broken reference: the '{$name}' element cannot be added as child to '{$parentName}', " .
195-
'because the latter doesn\'t exist'
196-
);
202+
203+
if ($this->state->getMode() == State::MODE_DEVELOPER) {
204+
$this->logger->critical(
205+
"Broken reference: the '{$name}' element cannot be added as child to '{$parentName}', " .
206+
'because the latter doesn\'t exist'
207+
);
208+
}
197209
}
198210
}
199211

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
8+
use Magento\Framework\App\State;
9+
10+
class StructureTest extends PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
14+
*/
15+
protected $loggerMock;
16+
17+
/**
18+
* @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
19+
*/
20+
protected $stateMock;
21+
22+
/**
23+
* @var ObjectManagerHelper
24+
*/
25+
protected $objectManagerHelper;
26+
27+
/**
28+
* @var \Magento\Framework\View\Layout\Data\Structure
29+
*/
30+
protected $dataStructure;
31+
32+
/**
33+
* @return void
34+
*/
35+
protected function setUp()
36+
{
37+
$this->loggerMock = $this->getMock('Psr\Log\LoggerInterface');
38+
$this->stateMock = $this->getMock('Magento\Framework\App\State', [], [], '', false);
39+
40+
$this->objectManagerHelper = new ObjectManagerHelper($this);
41+
$this->dataStructure = $this->objectManagerHelper->getObject(
42+
'Magento\Framework\View\Layout\Data\Structure',
43+
[
44+
'logger' => $this->loggerMock,
45+
'state' => $this->stateMock
46+
]
47+
);
48+
}
49+
50+
/**
51+
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $loggerExpects
52+
* @param string $stateMode
53+
* @return void
54+
* @dataProvider reorderChildElementLogDataProvider
55+
*/
56+
public function testReorderChildElementLog($loggerExpects, $stateMode)
57+
{
58+
$parentName = 'parent';
59+
$childName = 'child';
60+
$offsetOrSibling = '-';
61+
62+
$this->stateMock->expects($this->once())
63+
->method('getMode')
64+
->willReturn($stateMode);
65+
66+
$this->loggerMock->expects($loggerExpects)
67+
->method('critical')
68+
->with("Broken reference: the '{$childName}' tries to reorder itself towards '', but " .
69+
"their parents are different: '{$parentName}' and '' respectively.");
70+
71+
$this->dataStructure->reorderChildElement($parentName, $childName, $offsetOrSibling);
72+
}
73+
74+
/**
75+
* @return array
76+
*/
77+
public function reorderChildElementLogDataProvider()
78+
{
79+
return [
80+
[
81+
'loggerExpects' => $this->once(),
82+
'stateMode' => State::MODE_DEVELOPER
83+
],
84+
[
85+
'loggerExpects' => $this->never(),
86+
'stateMode' => State::MODE_DEFAULT
87+
],
88+
[
89+
'loggerExpects' => $this->never(),
90+
'stateMode' => State::MODE_PRODUCTION
91+
]
92+
];
93+
}
94+
}

lib/internal/Magento/Framework/View/Test/Unit/Layout/ScheduledStructure/HelperTest.php

Lines changed: 96 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Framework\View\Test\Unit\Layout\ScheduledStructure;
88

99
use Magento\Framework\View\Layout;
10+
use Magento\Framework\App\State;
1011

1112
/**
1213
* Class HelperTest
@@ -22,7 +23,17 @@ class HelperTest extends \PHPUnit_Framework_TestCase
2223
/**
2324
* @var \Magento\Framework\View\Layout\Data\Structure|\PHPUnit_Framework_MockObject_MockObject
2425
*/
25-
protected $dataStructure;
26+
protected $dataStructureMock;
27+
28+
/**
29+
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
protected $loggerMock;
32+
33+
/**
34+
* @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
protected $stateMock;
2637

2738
/**
2839
* @var \Magento\Framework\View\Layout\ScheduledStructure\Helper
@@ -37,13 +48,20 @@ public function setUp()
3748
$this->scheduledStructureMock = $this->getMockBuilder('Magento\Framework\View\Layout\ScheduledStructure')
3849
->disableOriginalConstructor()
3950
->getMock();
40-
41-
$this->dataStructure = $this->getMockBuilder('Magento\Framework\View\Layout\Data\Structure')
51+
$this->dataStructureMock = $this->getMockBuilder('Magento\Framework\View\Layout\Data\Structure')
4252
->disableOriginalConstructor()
4353
->getMock();
54+
$this->loggerMock = $this->getMock('Psr\Log\LoggerInterface');
55+
$this->stateMock = $this->getMock('Magento\Framework\App\State', [], [], '', false);
4456

4557
$helperObjectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
46-
$this->helper = $helperObjectManager->getObject('Magento\Framework\View\Layout\ScheduledStructure\Helper');
58+
$this->helper = $helperObjectManager->getObject(
59+
'Magento\Framework\View\Layout\ScheduledStructure\Helper',
60+
[
61+
'logger' => $this->loggerMock,
62+
'state' => $this->stateMock
63+
]
64+
);
4765
}
4866

4967
/**
@@ -122,7 +140,76 @@ public function testScheduleNonExistentElement()
122140
$this->scheduledStructureMock->expects($this->once())->method('unsetPathElement')->with($key);
123141
$this->scheduledStructureMock->expects($this->once())->method('unsetStructureElement')->with($key);
124142

125-
$this->helper->scheduleElement($this->scheduledStructureMock, $this->dataStructure, $key);
143+
$this->helper->scheduleElement($this->scheduledStructureMock, $this->dataStructureMock, $key);
144+
}
145+
146+
/**
147+
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $loggerExpects
148+
* @param string $stateMode
149+
* @return void
150+
* @dataProvider scheduleElementLogDataProvider
151+
*/
152+
public function testScheduleElementLog($loggerExpects, $stateMode)
153+
{
154+
$key = 'key';
155+
$parentName = 'parent';
156+
$alias = 'alias';
157+
$block = 'block';
158+
$siblingName = null;
159+
$isAfter = false;
160+
161+
$this->scheduledStructureMock->expects($this->once())
162+
->method('getStructureElement')
163+
->willReturn(
164+
[
165+
Layout\ScheduledStructure\Helper::SCHEDULED_STRUCTURE_INDEX_TYPE => $block,
166+
Layout\ScheduledStructure\Helper::SCHEDULED_STRUCTURE_INDEX_ALIAS => $alias,
167+
Layout\ScheduledStructure\Helper::SCHEDULED_STRUCTURE_INDEX_PARENT_NAME => $parentName,
168+
Layout\ScheduledStructure\Helper::SCHEDULED_STRUCTURE_INDEX_SIBLING_NAME => $siblingName,
169+
Layout\ScheduledStructure\Helper::SCHEDULED_STRUCTURE_INDEX_IS_AFTER => $isAfter
170+
]
171+
);
172+
$this->scheduledStructureMock->expects($this->once())
173+
->method('hasStructureElement')
174+
->with($parentName)
175+
->willReturn(false);
176+
177+
$this->dataStructureMock->expects($this->once())
178+
->method('hasElement')
179+
->with($parentName)
180+
->willReturn(false);
181+
182+
$this->stateMock->expects($this->once())
183+
->method('getMode')
184+
->willReturn($stateMode);
185+
186+
$this->loggerMock->expects($loggerExpects)
187+
->method('critical')
188+
->with("Broken reference: the '{$key}' element cannot be added as child to '{$parentName}', " .
189+
'because the latter doesn\'t exist');
190+
191+
$this->helper->scheduleElement($this->scheduledStructureMock, $this->dataStructureMock, $key);
192+
}
193+
194+
/**
195+
* @return array
196+
*/
197+
public function scheduleElementLogDataProvider()
198+
{
199+
return [
200+
[
201+
'loggerExpects' => $this->once(),
202+
'stateMode' => State::MODE_DEVELOPER
203+
],
204+
[
205+
'loggerExpects' => $this->never(),
206+
'stateMode' => State::MODE_DEFAULT
207+
],
208+
[
209+
'loggerExpects' => $this->never(),
210+
'stateMode' => State::MODE_PRODUCTION
211+
]
212+
];
126213
}
127214

128215
/**
@@ -171,9 +258,9 @@ public function testScheduleElement($hasParent, $setAsChild, $toRemoveList, $sib
171258
$this->scheduledStructureMock->expects($this->any())->method('hasStructureElement')->willReturn(true);
172259
$this->scheduledStructureMock->expects($this->once())->method('setElement')->with($key, [$block, $data]);
173260

174-
$this->dataStructure->expects($this->once())->method('createElement')->with($key, ['type' => $block]);
175-
$this->dataStructure->expects($this->once())->method('hasElement')->with($parentName)->willReturn($hasParent);
176-
$this->dataStructure->expects($this->exactly($setAsChild))
261+
$this->dataStructureMock->expects($this->once())->method('createElement')->with($key, ['type' => $block]);
262+
$this->dataStructureMock->expects($this->once())->method('hasElement')->with($parentName)->willReturn($hasParent);
263+
$this->dataStructureMock->expects($this->exactly($setAsChild))
177264
->method('setAsChild')
178265
->with($key, $parentName, $alias)
179266
->willReturn(true);
@@ -185,7 +272,7 @@ public function testScheduleElement($hasParent, $setAsChild, $toRemoveList, $sib
185272
->method('setElementToSortList')
186273
->with($parentName, $key, $siblingName, $isAfter);
187274

188-
$this->helper->scheduleElement($this->scheduledStructureMock, $this->dataStructure, $key);
275+
$this->helper->scheduleElement($this->scheduledStructureMock, $this->dataStructureMock, $key);
189276
}
190277

191278
/**

0 commit comments

Comments
 (0)