Skip to content

Commit 57ad442

Browse files
authored
MQE-943: Inconsistency in *Page.xml files declaration for "module" attribute. (magento#135)
* MQE-943: Inconsistency in *Page.xml files declaration for "module" attribute. - Moved Module Extractor method - Extended Page Filesystem and Dom - Added warning for incorrectly named page modules * MQE-943: Inconsistency in *Page.xml files declaration for "module" attribute. - Moved duplicated code to a single location * MQE-943: Inconsistency in *Page.xml files declaration for "module" attribute. - Added Tests for Extractor Class - Updated samplepage.xml * MQE-943: Inconsistency in *Page.xml files declaration for "module" attribute. - Added correct references for filename and exceptionCollector to the merge signature - Fixed issue with Module Resolver on Windows
1 parent 343009b commit 57ad442

File tree

9 files changed

+238
-39
lines changed

9 files changed

+238
-39
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace tests\unit\Magento\FunctionalTestFramework\Test\Util;
8+
9+
use Magento\FunctionalTestingFramework\Util\ModulePathExtractor;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class ModulePathExtractorTest extends TestCase
13+
{
14+
/**
15+
* Validate correct module is returned for dev/tests path
16+
* @throws \Exception
17+
*/
18+
public function testGetMagentoModule()
19+
{
20+
$modulePathExtractor = new ModulePathExtractor();
21+
$this->assertEquals(
22+
'[Analytics]',
23+
$modulePathExtractor->extractModuleName(
24+
"dev/tests/acceptance/tests/functional/Magento/FunctionalTest/[Analytics]/Test/SomeText.xml"
25+
)
26+
);
27+
}
28+
29+
/**
30+
* Validate correct module is returned for extension path
31+
* @throws \Exception
32+
*/
33+
public function testGetExtensionModule()
34+
{
35+
$modulePathExtractor = new ModulePathExtractor();
36+
$this->assertEquals(
37+
'[Analytics]',
38+
$modulePathExtractor->extractModuleName(
39+
"app/code/Magento/[Analytics]/Test/Mftf/Test/SomeText.xml"
40+
)
41+
);
42+
}
43+
44+
/**
45+
* Validate Magento is returned for dev/tests/acceptance
46+
* @throws \Exception
47+
*/
48+
public function testMagentoModulePath()
49+
{
50+
$modulePathExtractor = new ModulePathExtractor();
51+
$this->assertEquals(
52+
'Magento',
53+
$modulePathExtractor->getExtensionPath(
54+
"dev/tests/acceptance/tests/functional/Magento/FunctionalTest/[Analytics]/Test/SomeText.xml"
55+
)
56+
);
57+
}
58+
59+
/**
60+
* Validate correct extension path is returned
61+
* @throws \Exception
62+
*/
63+
public function testExtensionModulePath()
64+
{
65+
$modulePathExtractor = new ModulePathExtractor();
66+
$this->assertEquals(
67+
'TestExtension',
68+
$modulePathExtractor->getExtensionPath(
69+
"app/code/TestExtension/[Analytics]/Test/Mftf/Test/SomeText.xml"
70+
)
71+
);
72+
}
73+
}

dev/tests/verification/TestModule/Page/SamplePage.xml

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@
88

99
<pages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1010
xsi:noNamespaceSchemaLocation="../../../../../src/Magento/FunctionalTestingFramework/Page/etc/PageObject.xsd">
11-
<page name="SamplePage" url="/{{var1}}/{{var2}}.html" area="storefront" module="SampleTests" parameterized="true">
11+
<page name="SamplePage" url="/{{var1}}/{{var2}}.html" area="storefront" module="TestModule_Magento" parameterized="true">
1212
<section name="SampleSection"/>
1313
</page>
14-
<page name="NoParamPage" url="/page.html" area="storefront" module="SampleTests">
14+
<page name="NoParamPage" url="/page.html" area="storefront" module="TestModule_Magento">
1515
<section name="SampleSection"/>
1616
</page>
17-
<page name="OneParamPage" url="/{{var1}}/page.html" area="storefront" module="SampleTests" parameterized="true">
17+
<page name="OneParamPage" url="/{{var1}}/page.html" area="storefront" module="TestModule_Magento" parameterized="true">
1818
<section name="SampleSection"/>
1919
</page>
20-
<page name="TwoParamPage" url="/{{var1}}/{{var2}}.html" area="storefront" module="SampleTests" parameterized="true">
20+
<page name="TwoParamPage" url="/{{var1}}/{{var2}}.html" area="storefront" module="TestModule_Magento" parameterized="true">
2121
<section name="SampleSection"/>
2222
</page>
23-
<page name="AdminPage" url="/backend" area="admin" module="SampleTests">
23+
<page name="AdminPage" url="/backend" area="admin" module="TestModule_Magento">
2424
<section name="SampleSection"/>
2525
</page>
26-
<page name="AdminOneParamPage" url="/{{var1}}/page.html" area="admin" module="SampleTests" parameterized="true">
26+
<page name="AdminOneParamPage" url="/{{var1}}/page.html" area="admin" module="TestModule_Magento" parameterized="true">
2727
<section name="SampleSection"/>
2828
</page>
29-
<page name="ExternalPage" url="http://myFullUrl.com/" area="external" module="SampleTests">
29+
<page name="ExternalPage" url="http://myFullUrl.com/" area="external" module="TestModule_Magento">
3030
<section name="SampleSection"/>
3131
</page>
3232
</pages>

etc/di.xml

+4-3
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,12 @@
7878
<argument name="schemaPath" xsi:type="string">Magento/FunctionalTestingFramework/Page/etc/SectionObject.xsd</argument>
7979
</arguments>
8080
</virtualType>
81-
<virtualType name="Magento\FunctionalTestingFramework\Config\Reader\Page" type="Magento\FunctionalTestingFramework\Config\Reader\Filesystem">
81+
<virtualType name="Magento\FunctionalTestingFramework\Config\Reader\Page" type="Magento\FunctionalTestingFramework\Config\Reader\MftfFilesystem">
8282
<arguments>
8383
<argument name="fileResolver" xsi:type="object">Magento\FunctionalTestingFramework\Config\FileResolver\Module</argument>
8484
<argument name="converter" xsi:type="object">Magento\FunctionalTestingFramework\Config\Converter</argument>
8585
<argument name="schemaLocator" xsi:type="object">Magento\FunctionalTestingFramework\Config\SchemaLocator\Page</argument>
86+
<argument name="domDocumentClass" xsi:type="string">Magento\FunctionalTestingFramework\Page\Config\Dom</argument>
8687
<argument name="idAttributes" xsi:type="array">
8788
<item name="/pages/page" xsi:type="string">name</item>
8889
<item name="/pages/page/section" xsi:type="string">name</item>
@@ -207,7 +208,7 @@
207208
<argument name="schemaPath" xsi:type="string">Magento/FunctionalTestingFramework/Test/etc/mergedTestSchema.xsd</argument>
208209
</arguments>
209210
</virtualType>
210-
<virtualType name="Magento\FunctionalTestingFramework\Config\Reader\TestData" type="Magento\FunctionalTestingFramework\Test\Config\Reader\Filesystem">
211+
<virtualType name="Magento\FunctionalTestingFramework\Config\Reader\TestData" type="Magento\FunctionalTestingFramework\Config\Reader\MftfFilesystem">
211212
<arguments>
212213
<argument name="fileResolver" xsi:type="object">Magento\FunctionalTestingFramework\Config\FileResolver\Module</argument>
213214
<argument name="converter" xsi:type="object">Magento\FunctionalTestingFramework\Config\TestDataConverter</argument>
@@ -286,7 +287,7 @@
286287
</arguments>
287288
</virtualType>
288289

289-
<virtualType name="Magento\FunctionalTestingFramework\Config\Reader\ActionGroupData" type="Magento\FunctionalTestingFramework\Test\Config\Reader\Filesystem">
290+
<virtualType name="Magento\FunctionalTestingFramework\Config\Reader\ActionGroupData" type="Magento\FunctionalTestingFramework\Config\Reader\MftfFilesystem">
290291
<arguments>
291292
<argument name="fileResolver" xsi:type="object">Magento\FunctionalTestingFramework\Config\FileResolver\Module</argument>
292293
<argument name="converter" xsi:type="object">Magento\FunctionalTestingFramework\Config\ActionGroupDataConverter</argument>

src/Magento/FunctionalTestingFramework/Config/Dom.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,13 @@ public function __construct(
9494
* Merge $xml into DOM document
9595
*
9696
* @param string $xml
97+
* @param string $filename
98+
* @param ExceptionCollector $exceptionCollector
9799
* @return void
98100
*/
99-
public function merge($xml)
101+
public function merge($xml, $filename = null, $exceptionCollector = null)
100102
{
101-
$dom = $this->initDom($xml);
103+
$dom = $this->initDom($xml, $filename, $exceptionCollector);
102104
$this->mergeNode($dom->documentElement, '');
103105
}
104106

src/Magento/FunctionalTestingFramework/Test/Config/Reader/Filesystem.php renamed to src/Magento/FunctionalTestingFramework/Config/Reader/MftfFilesystem.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
namespace Magento\FunctionalTestingFramework\Test\Config\Reader;
7+
namespace Magento\FunctionalTestingFramework\Config\Reader;
88

99
use Magento\FunctionalTestingFramework\Exceptions\Collector\ExceptionCollector;
1010
use Magento\FunctionalTestingFramework\Util\Iterator\File;
1111

12-
class Filesystem extends \Magento\FunctionalTestingFramework\Config\Reader\Filesystem
12+
class MftfFilesystem extends \Magento\FunctionalTestingFramework\Config\Reader\Filesystem
1313
{
1414
/**
1515
* Method to redirect file name passing into Dom class
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\Page\Config;
8+
9+
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
10+
use Magento\FunctionalTestingFramework\Exceptions\Collector\ExceptionCollector;
11+
use Magento\FunctionalTestingFramework\Config\Dom\NodeMergingConfig;
12+
use Magento\FunctionalTestingFramework\Config\Dom\NodePathMatcher;
13+
use Magento\FunctionalTestingFramework\Util\ModulePathExtractor;
14+
15+
class Dom extends \Magento\FunctionalTestingFramework\Config\Dom
16+
{
17+
/**
18+
* Module Path extractor
19+
*
20+
* @var ModulePathExtractor
21+
*/
22+
private $modulePathExtractor;
23+
24+
/**
25+
* TestDom constructor.
26+
* @param string $xml
27+
* @param string $filename
28+
* @param ExceptionCollector $exceptionCollector
29+
* @param array $idAttributes
30+
* @param string $typeAttributeName
31+
* @param string $schemaFile
32+
* @param string $errorFormat
33+
*/
34+
public function __construct(
35+
$xml,
36+
$filename,
37+
$exceptionCollector,
38+
array $idAttributes = [],
39+
$typeAttributeName = null,
40+
$schemaFile = null,
41+
$errorFormat = self::ERROR_FORMAT_DEFAULT
42+
) {
43+
$this->schemaFile = $schemaFile;
44+
$this->nodeMergingConfig = new NodeMergingConfig(new NodePathMatcher(), $idAttributes);
45+
$this->typeAttributeName = $typeAttributeName;
46+
$this->errorFormat = $errorFormat;
47+
$this->modulePathExtractor = new ModulePathExtractor();
48+
$this->dom = $this->initDom($xml, $filename, $exceptionCollector);
49+
$this->rootNamespace = $this->dom->lookupNamespaceUri($this->dom->namespaceURI);
50+
}
51+
52+
/**
53+
* Takes a dom element from xml and appends the filename based on location
54+
*
55+
* @param string $xml
56+
* @param string|null $filename
57+
* @param ExceptionCollector $exceptionCollector
58+
* @return \DOMDocument
59+
*/
60+
public function initDom($xml, $filename = null, $exceptionCollector = null)
61+
{
62+
$dom = parent::initDom($xml);
63+
64+
$pageNodes = $dom->getElementsByTagName('page');
65+
$currentModule =
66+
$this->modulePathExtractor->extractModuleName($filename) .
67+
'_' .
68+
$this->modulePathExtractor->getExtensionPath($filename);
69+
foreach ($pageNodes as $pageNode) {
70+
$pageModule = $pageNode->getAttribute("module");
71+
$pageName = $pageNode->getAttribute("name");
72+
if ($pageModule !== $currentModule) {
73+
if (MftfApplicationConfig::getConfig()->verboseEnabled()) {
74+
print(
75+
"Page Module does not match path Module. " .
76+
"(Page, Module): ($pageName, $pageModule) - Path Module: $currentModule" .
77+
PHP_EOL
78+
);
79+
}
80+
}
81+
}
82+
return $dom;
83+
}
84+
}

src/Magento/FunctionalTestingFramework/Test/Util/TestObjectExtractor.php

+10-22
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
1111
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
1212
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
13+
use Magento\FunctionalTestingFramework\Util\ModulePathExtractor;
1314
use Magento\FunctionalTestingFramework\Util\Validation\NameValidationUtil;
1415

1516
/**
@@ -48,6 +49,13 @@ class TestObjectExtractor extends BaseObjectExtractor
4849
*/
4950
private $testHookObjectExtractor;
5051

52+
/**
53+
* Module Path extractor
54+
*
55+
* @var ModulePathExtractor
56+
*/
57+
private $modulePathExtractor;
58+
5159
/**
5260
* TestObjectExtractor constructor.
5361
*/
@@ -56,6 +64,7 @@ public function __construct()
5664
$this->actionObjectExtractor = new ActionObjectExtractor();
5765
$this->annotationExtractor = new AnnotationExtractor();
5866
$this->testHookObjectExtractor = new TestHookObjectExtractor();
67+
$this->modulePathExtractor = new ModulePathExtractor();
5968
}
6069

6170
/**
@@ -76,7 +85,7 @@ public function extractTestData($testData)
7685
$filename = $testData['filename'] ?? null;
7786
$fileNames = explode(",", $filename);
7887
$baseFileName = $fileNames[0];
79-
$module = $this->extractModuleName($baseFileName);
88+
$module = $this->modulePathExtractor->extractModuleName($baseFileName);
8089
$testReference = $testData['extends'] ?? null;
8190
$testActions = $this->stripDescriptorTags(
8291
$testData,
@@ -136,25 +145,4 @@ public function extractTestData($testData)
136145
throw new XmlException($exception->getMessage() . ' in Test ' . $filename);
137146
}
138147
}
139-
140-
/**
141-
* Extracts module name from the path given
142-
* @param string $path
143-
* @return string
144-
*/
145-
private function extractModuleName($path)
146-
{
147-
if (empty($path)) {
148-
return "NO MODULE DETECTED";
149-
}
150-
$paths = explode(DIRECTORY_SEPARATOR, $path);
151-
if (count($paths) < 3) {
152-
return "NO MODULE DETECTED";
153-
} elseif ($paths[count($paths)-3] == "Mftf") {
154-
// app/code/Magento/[Analytics]/Test/Mftf/Test/SomeText.xml
155-
return $paths[count($paths)-5];
156-
}
157-
// dev/tests/acceptance/tests/functional/Magento/FunctionalTest/[Analytics]/Test/SomeText.xml
158-
return $paths[count($paths)-3];
159-
}
160148
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\Util;
8+
9+
/**
10+
* Class ModulePathExtractor, resolve module reference based on path
11+
*/
12+
class ModulePathExtractor
13+
{
14+
const MAGENTO = 'Magento';
15+
16+
/**
17+
* Extracts module name from the path given
18+
* @param string $path
19+
* @return string
20+
*/
21+
public function extractModuleName($path)
22+
{
23+
if (empty($path)) {
24+
return "NO MODULE DETECTED";
25+
}
26+
$paths = explode(DIRECTORY_SEPARATOR, $path);
27+
if (count($paths) < 3) {
28+
return "NO MODULE DETECTED";
29+
} elseif ($paths[count($paths)-3] == "Mftf") {
30+
// app/code/Magento/[Analytics]/Test/Mftf/Test/SomeText.xml
31+
return $paths[count($paths)-5];
32+
}
33+
// dev/tests/acceptance/tests/functional/Magento/FunctionalTest/[Analytics]/Test/SomeText.xml
34+
return $paths[count($paths)-3];
35+
}
36+
37+
/**
38+
* Extracts the extension form the path, Magento for dev/tests/acceptance, [name] before module otherwise
39+
* @param string $path
40+
* @return string
41+
*/
42+
public function getExtensionPath($path)
43+
{
44+
$paths = explode(DIRECTORY_SEPARATOR, $path);
45+
if ($paths[count($paths)-3] == "Mftf") {
46+
// app/code/[Magento]/Analytics/Test/Mftf/Test/SomeText.xml
47+
return $paths[count($paths)-6];
48+
}
49+
return self::MAGENTO;
50+
}
51+
}

src/Magento/FunctionalTestingFramework/Util/ModuleResolver.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ private function aggregateTestModulePaths()
238238

239239
$codePathsToPattern = [
240240
$modulePath => '',
241-
$appCodePath => '/Test/Mftf',
242-
$vendorCodePath => '/Test/Mftf'
241+
$appCodePath => DIRECTORY_SEPARATOR . 'Test' . DIRECTORY_SEPARATOR . 'Mftf',
242+
$vendorCodePath => DIRECTORY_SEPARATOR . 'Test' . DIRECTORY_SEPARATOR . 'Mftf'
243243
];
244244

245245
foreach ($codePathsToPattern as $codePath => $pattern) {
@@ -284,7 +284,7 @@ private function globRelevantPaths($testPath, $pattern)
284284
*/
285285
private static function globRelevantWrapper($testPath, $pattern)
286286
{
287-
return glob($testPath . '*/*' . $pattern);
287+
return glob($testPath . '*' . DIRECTORY_SEPARATOR . '*' . $pattern);
288288
}
289289

290290
/**

0 commit comments

Comments
 (0)