Skip to content

Commit e59ca3f

Browse files
committed
AC-670: Create phpcs static check for LayoutTest
1 parent 01ff1bd commit e59ca3f

File tree

3 files changed

+22
-54
lines changed

3 files changed

+22
-54
lines changed

Magento2/Sniffs/Legacy/LayoutSniff.php

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types = 1);
67

78
namespace Magento2\Sniffs\Legacy;
89

@@ -20,7 +21,6 @@ class LayoutSniff implements Sniff
2021
private const ERROR_CODE_NOT_ALLOWED = 'NotAllowed';
2122
private const ERROR_CODE_OBSOLETE = 'Obsolete';
2223
private const ERROR_CODE_OBSOLETE_CLASS = 'ObsoleteClass';
23-
private const ERROR_CODE_ATTRIBUTE_NOT_VALID = 'AttributeNotValid';
2424
private const ERROR_CODE_METHOD_NOT_ALLOWED = 'MethodNotAllowed';
2525
private const ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_NOT_ALLOWED = 'CharacterNotAllowedInAttribute';
2626
private const ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_EXPECTED = 'CharacterExpectedInAttribute';
@@ -229,7 +229,6 @@ public function process(File $phpcsFile, $stackPtr)
229229
}
230230

231231
$this->testObsoleteReferences($layout, $phpcsFile);
232-
$this->testObsoleteAttributes($layout, $phpcsFile);
233232
$this->testHeadBlocks($layout, $phpcsFile);
234233
$this->testOutputAttribute($layout, $phpcsFile);
235234
$this->testHelperAttribute($layout, $phpcsFile);
@@ -240,21 +239,21 @@ public function process(File $phpcsFile, $stackPtr)
240239
/**
241240
* Check for obsolete block references
242241
*
243-
* @todo missing test
244242
* @param SimpleXMLElement $layout
245243
* @param File $phpcsFile
246244
*/
247245
private function testObsoleteReferences(SimpleXMLElement $layout, File $phpcsFile): void
248246
{
249247
foreach ($layout as $handle) {
250-
if (!isset($this->obsoleteReferences[$handle->getName()])) {
248+
$attributes = $handle->attributes();
249+
if (!isset($this->obsoleteReferences[(string)$attributes->handle])) {
251250
continue;
252251
}
253-
foreach ($handle->xpath('reference') as $reference) {
254-
if (strpos((string)$reference['name'], $this->obsoleteReferences[$handle->getName()]) !== false) {
252+
foreach ($handle->xpath('//reference | //referenceContainer | //referenceBlock') as $reference) {
253+
if (in_array((string)$reference['name'], $this->obsoleteReferences[(string)$attributes->handle]) !== false) {
255254
$phpcsFile->addError(
256255
'The block being referenced is removed.',
257-
dom_import_simplexml($reference)->getLineNo(),
256+
dom_import_simplexml($reference)->getLineNo()-1,
258257
self::ERROR_CODE_OBSOLETE
259258
);
260259
}
@@ -317,45 +316,12 @@ private function testOutputAttribute(SimpleXMLElement $layout, File $phpcsFile):
317316
if (!empty($elements)) {
318317
$phpcsFile->addError(
319318
'output="toHtml" is obsolete. Use output="1"',
320-
dom_import_simplexml($elements[0])->getLineNo(),
319+
dom_import_simplexml($elements[0])->getLineNo()-1,
321320
self::ERROR_CODE_OBSOLETE
322321
);
323322
};
324323
}
325324

326-
/**
327-
* Tests the attributes of the top-level Layout Node. Verifies there are no longer attributes of "parent" or "owner"
328-
*
329-
* @todo missing test
330-
* @param SimpleXMLElement $layout
331-
* @param File $phpcsFile
332-
*/
333-
private function testObsoleteAttributes(SimpleXMLElement $layout, File $phpcsFile): void
334-
{
335-
$type = $layout['type'];
336-
$parent = $layout['parent'];
337-
$owner = $layout['owner'];
338-
339-
if ((string)$type === 'page') {
340-
if ($parent) {
341-
$phpcsFile->addError(
342-
'Attribute "parent" is not valid',
343-
dom_import_simplexml($parent)->getLineNo(),
344-
self::ERROR_CODE_ATTRIBUTE_NOT_VALID
345-
);
346-
}
347-
}
348-
if ((string)$type === 'fragment') {
349-
if ($owner) {
350-
$phpcsFile->addError(
351-
'Attribute "owner" is not valid',
352-
dom_import_simplexml($owner)->getLineNo(),
353-
self::ERROR_CODE_ATTRIBUTE_NOT_VALID
354-
);
355-
}
356-
}
357-
}
358-
359325
/**
360326
* Returns attribute value by attribute name
361327
*
@@ -381,14 +347,14 @@ private function testHelperAttribute(SimpleXMLElement $layout, File $phpcsFile):
381347
if (strpos($this->getAttribute($action, 'helper'), '/') !== false) {
382348
$phpcsFile->addError(
383349
"'helper' attribute contains '/'",
384-
dom_import_simplexml($action)->getLineNo(),
350+
dom_import_simplexml($action)->getLineNo()-1,
385351
self::ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_NOT_ALLOWED
386352
);
387353
}
388354
if (strpos($this->getAttribute($action, 'helper'), '::') === false) {
389355
$phpcsFile->addError(
390356
"'helper' attribute does not contain '::'",
391-
dom_import_simplexml($action)->getLineNo(),
357+
dom_import_simplexml($action)->getLineNo()-1,
392358
self::ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_EXPECTED
393359
);
394360
}
@@ -408,7 +374,7 @@ private function testListText(SimpleXMLElement $layout, File $phpcsFile): void
408374
$phpcsFile->addError(
409375
'The class \Magento\Framework\View\Element\Text\ListText' .
410376
' is not supposed to be used in layout anymore.',
411-
dom_import_simplexml($elements[0])->getLineNo(),
377+
dom_import_simplexml($elements[0])->getLineNo()-1,
412378
self::ERROR_CODE_OBSOLETE_CLASS
413379
);
414380
};
@@ -430,7 +396,7 @@ private function testActionNodeMethods(SimpleXMLElement $layout, File $phpcsFile
430396
'Call of method "%s" via layout instruction <action> is not allowed.',
431397
$attributes['method']
432398
),
433-
dom_import_simplexml($node)->getLineNo(),
399+
dom_import_simplexml($node)->getLineNo()-1,
434400
self::ERROR_CODE_METHOD_NOT_ALLOWED
435401
);
436402
}

Magento2/Tests/Legacy/LayoutUnitTest.1.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
*/
77
-->
88
<page layout="3columns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
9-
<update handle="default_head_blocks"/>
9+
<update handle="adminhtml_user_edit"/>
1010
<body>
1111
<attribute name="id" value="html-body"/>
1212
<block name="require.js" class="Magento\Framework\View\Element\Template" template="Magento_Theme::page/js/require_js.phtml" />
13-
<referenceContainer name="after.body.start">
13+
<referenceContainer name="adminhtml.permission.user.edit.tabs">
1414
<block class="Magento\RequireJs\Block\Html\Head\Config" name="requirejs-config"/>
1515
<block class="Magento\Framework\View\Element\Js\Cookie" name="js_cookies" template="Magento_Theme::js/cookie.phtml">
1616
<arguments>
@@ -19,7 +19,7 @@
1919
</block>
2020
<block class="Magento\Theme\Block\Html\Notices" name="global_notices" template="Magento_Theme::html/notices.phtml"/>
2121
</referenceContainer>
22-
<referenceBlock name="top.links">
22+
<referenceBlock name="adminhtml.permission.user.edit">
2323
<block class="Magento\Theme\Block\Html\Head\Css">
2424
</block>
2525
<block class="Magento\Theme\Block\Html\Header" name="header" as="header" before="-">

Magento2/Tests/Legacy/LayoutUnitTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,23 @@ public function getErrorList($testFile = '')
1616
{
1717
if ($testFile === 'LayoutUnitTest.1.xml') {
1818
return [
19+
13 => 1,
20+
22 => 1,
1921
23 => 1,
20-
146 => 1,
21-
149 => 1,
22+
145 => 1,
23+
148 => 1,
2224
];
2325
}
2426
if ($testFile === 'LayoutUnitTest.2.xml') {
2527
return [
26-
12 => 1,
27-
29 => 1,
28+
11 => 1,
29+
28 => 1,
2830
];
2931
}
3032
if ($testFile === 'LayoutUnitTest.3.xml') {
3133
return [
32-
16 => 1,
33-
19 => 1,
34+
15 => 1,
35+
18 => 1,
3436
];
3537
}
3638
return [];

0 commit comments

Comments
 (0)