Skip to content

Commit 010156f

Browse files
authored
Merge pull request #623 from magento-troll/troll_pr
MAGETWO-60201: Test UnsecureFunctionsUsageTest fails in case of changes in files with unsecure code MAGETWO-60818: [FT] Incorrect array index 'frontend_type' in the CreateBundleProductEntityTest
2 parents b4c3bfd + 7b92679 commit 010156f

File tree

7 files changed

+167
-12
lines changed

7 files changed

+167
-12
lines changed

dev/tests/functional/tests/app/Magento/Bundle/Test/Block/Catalog/Product/View.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ function () use ($browser, $selector) {
9595
);
9696
$this->_rootElement->find($this->customizeButton)->click();
9797
$this->waitForElementVisible($this->addToCart);
98+
$this->waitForElementVisible($this->visibleOptions, Locator::SELECTOR_XPATH);
9899
}
99100

100101
/**

dev/tests/functional/tests/app/Magento/Bundle/Test/Block/Catalog/Product/View/Type/Option/Hidden.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
<mapping strict="1">
99
<fields>
1010
<qty>
11-
<selector>//input[contains(@class,"qty")]</selector>
12-
<strategy>xpath</strategy>
11+
<selector>input.qty</selector>
1312
<class>Magento\Bundle\Test\Block\Catalog\Product\View\Type\Option\Element\Qty</class>
1413
</qty>
1514
</fields>

dev/tests/functional/tests/app/Magento/Bundle/Test/Constraint/AssertBundleProductForm.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ protected function prepareFixtureData(array $data, array $sortFields = [])
5252
protected function prepareBundleOptions(array $bundleSelections)
5353
{
5454
foreach ($bundleSelections as &$item) {
55+
unset($item['frontend_type']);
5556
foreach ($item['assigned_products'] as &$selection) {
5657
$selection['data']['getProductName'] = $selection['search_data']['name'];
5758
$selection = $selection['data'];

dev/tests/functional/tests/app/Magento/Bundle/Test/Repository/BundleProduct/CheckoutData.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@
353353
<item name="3" xsi:type="array">
354354
<item name="title" xsi:type="string">Multiple Select Option</item>
355355
<item name="type" xsi:type="string">Multiple</item>
356+
<item name="frontend_type" xsi:type="string">Multiple</item>
356357
<item name="value" xsi:type="array">
357358
<item name="name" xsi:type="string">product_100_dollar</item>
358359
</item>
@@ -367,6 +368,7 @@
367368
<item name="0" xsi:type="array">
368369
<item name="title" xsi:type="string">Drop-down Option</item>
369370
<item name="type" xsi:type="string">Drop-down</item>
371+
<item name="frontend_type" xsi:type="string">Drop-down</item>
370372
<item name="value" xsi:type="array">
371373
<item name="name" xsi:type="string">Test simple product</item>
372374
</item>

dev/tests/static/framework/Magento/TestFramework/Utility/ChangedFiles.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © 2016 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\TestFramework\Utility;
78

89
use Magento\Framework\App\Utility\Files;
@@ -15,6 +16,11 @@
1516
*/
1617
class ChangedFiles
1718
{
19+
/**
20+
* File path with changed files content.
21+
*/
22+
const CHANGED_FILES_CONTENT_FILE = '/dev/tests/static/testsuite/Magento/Test/_files/changed_%s_files_content.json';
23+
1824
/**
1925
* Returns array of PHP-files, that use or declare Magento application classes and Magento libs
2026
*
@@ -45,4 +51,36 @@ function (&$file) {
4551

4652
return $phpFiles;
4753
}
54+
55+
/**
56+
* Get changed content.
57+
*
58+
* @param string $fileName
59+
* @return string
60+
*/
61+
public static function getChangedContent($fileName)
62+
{
63+
$data = [];
64+
$extension = self::getFileExtension($fileName);
65+
$fileName = ltrim(str_replace(BP, '', $fileName), DIRECTORY_SEPARATOR);
66+
$changedFilesContentFile = BP . sprintf(self::CHANGED_FILES_CONTENT_FILE, $extension);
67+
if (file_exists($changedFilesContentFile)) {
68+
$changedContent = file_get_contents($changedFilesContentFile);
69+
$data = json_decode($changedContent, true);
70+
}
71+
72+
return isset($data[$fileName]) ? $data[$fileName] : '';
73+
}
74+
75+
/**
76+
* Get file extension.
77+
*
78+
* @param string $fileName
79+
* @return string
80+
*/
81+
public static function getFileExtension($fileName)
82+
{
83+
$fileInfo = pathinfo($fileName);
84+
return isset($fileInfo['extension']) ? $fileInfo['extension'] : 'unknown';
85+
}
4886
}

dev/tests/static/framework/Magento/TestFramework/Utility/FunctionDetector.php

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,65 @@ public function detect($filePath, $functions)
3535
{
3636
$result = [];
3737
$regexp = $this->composeRegexp($functions);
38-
if ($regexp) {
39-
$file = file($filePath);
40-
array_unshift($file, '');
41-
$lines = preg_grep(
42-
$regexp,
43-
$file
44-
);
45-
foreach ($lines as $lineNumber => $line) {
46-
if (preg_match_all($regexp, $line, $matches)) {
47-
$result[$lineNumber] = $matches[1];
38+
39+
if (!$regexp) {
40+
return $result;
41+
}
42+
43+
$fileContent = \Magento\TestFramework\Utility\ChangedFiles::getChangedContent($filePath);
44+
$file = file($filePath);
45+
46+
return $fileContent
47+
? $this->grepChangedContent($file, $regexp, $functions, $fileContent)
48+
: $this->grepFile($file, $regexp);
49+
}
50+
51+
/**
52+
* Grep only changed content.
53+
*
54+
* @param array $file
55+
* @param string $regexp
56+
* @param string[] $functions
57+
* @param string $fileContent
58+
* @return array
59+
*/
60+
public function grepChangedContent(array $file, $regexp, $functions, $fileContent)
61+
{
62+
$result = [];
63+
$matches = preg_grep($regexp, explode("\n", $fileContent));
64+
if (!empty($matches)) {
65+
foreach ($matches as $line) {
66+
$actualFunctions = [];
67+
foreach ($functions as $function) {
68+
if (false !== strpos($line, $function)) {
69+
$actualFunctions[] = $function;
70+
}
4871
}
72+
$result[array_search($line . "\n", $file) + 1] = $actualFunctions;
4973
}
5074
}
75+
76+
return $result;
77+
}
78+
79+
/**
80+
* Grep File.
81+
*
82+
* @param array $file
83+
* @param string $regexp
84+
* @return array
85+
*/
86+
public function grepFile(array $file, $regexp)
87+
{
88+
$result = [];
89+
array_unshift($file, '');
90+
$lines = preg_grep($regexp, $file);
91+
foreach ($lines as $lineNumber => $line) {
92+
if (preg_match_all($regexp, $line, $matches)) {
93+
$result[$lineNumber] = $matches[1];
94+
}
95+
}
96+
5197
return $result;
5298
}
5399

dev/tests/static/get_github_changes.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,36 @@
3434

3535
$fileExtensions = explode(',', isset($options['file-extensions']) ? $options['file-extensions'] : 'php');
3636

37+
include_once __DIR__ . '/framework/autoload.php';
38+
3739
$mainline = 'mainline_' . (string)rand(0, 9999);
3840
$repo = getRepo($options, $mainline);
3941
$branches = $repo->getBranches('--remotes');
4042
generateBranchesList($options['output-file'], $branches, $options['branch']);
4143
$changes = retrieveChangesAcrossForks($mainline, $repo, $options['branch']);
4244
$changedFiles = getChangedFiles($changes, $fileExtensions);
4345
generateChangedFilesList($options['output-file'], $changedFiles);
46+
saveChangedFileContent($repo);
4447
cleanup($repo, $mainline);
4548

49+
/**
50+
* Save changed file content.
51+
*
52+
* @param GitRepo $repo
53+
* @return void
54+
*/
55+
function saveChangedFileContent(GitRepo $repo)
56+
{
57+
$changedFilesContentFileName = BP . Magento\TestFramework\Utility\ChangedFiles::CHANGED_FILES_CONTENT_FILE;
58+
foreach ($repo->getChangedContentFiles() as $key => $changedContentFile) {
59+
$filePath = sprintf($changedFilesContentFileName, $key);
60+
$oldContent = file_exists($filePath) ? file_get_contents($filePath) : '{}';
61+
$oldData = json_decode($oldContent, true);
62+
$data = array_merge($oldData, $changedContentFile);
63+
file_put_contents($filePath, json_encode($data));
64+
}
65+
}
66+
4667
/**
4768
* Generates a file containing changed files
4869
*
@@ -170,6 +191,18 @@ class GitRepo
170191
*/
171192
private $remoteList = [];
172193

194+
/**
195+
* Array of changed content files.
196+
*
197+
* Example:
198+
* 'extension' =>
199+
* 'path_to_file/filename' => 'Content that was edited',
200+
* 'path_to_file/filename2' => 'Content that was edited',
201+
*
202+
* @var array
203+
*/
204+
private $changedContentFiles = [];
205+
173206
/**
174207
* @param string $workTree absolute path to git project
175208
*/
@@ -285,6 +318,9 @@ protected function filterChangedFiles(array $changes, $remoteAlias, $remoteBranc
285318
'diff HEAD %s/%s -- %s', $remoteAlias, $remoteBranch, $this->workTree . '/' . $fileName)
286319
);
287320
if ($result) {
321+
if (!(isset($this->changedContentFiles[$fileName]))) {
322+
$this->setChangedContentFile($result, $fileName);
323+
}
288324
$filteredChanges[] = $fileName;
289325
}
290326
}
@@ -295,6 +331,38 @@ protected function filterChangedFiles(array $changes, $remoteAlias, $remoteBranc
295331
return $filteredChanges;
296332
}
297333

334+
/**
335+
* Set changed content for file.
336+
*
337+
* @param array $content
338+
* @param string $fileName
339+
* @return void
340+
*/
341+
private function setChangedContentFile(array $content, $fileName)
342+
{
343+
$changedContent = '';
344+
$extension = Magento\TestFramework\Utility\ChangedFiles::getFileExtension($fileName);
345+
346+
foreach ($content as $item) {
347+
if (strpos($item, '---') !== 0 && strpos($item, '-') === 0 && $line = ltrim($item, '-')) {
348+
$changedContent .= $line . "\n";
349+
}
350+
}
351+
if ($changedContent !== '') {
352+
$this->changedContentFiles[$extension][$fileName] = $changedContent;
353+
}
354+
}
355+
356+
/**
357+
* Get changed content files collection.
358+
*
359+
* @return array
360+
*/
361+
public function getChangedContentFiles()
362+
{
363+
return $this->changedContentFiles;
364+
}
365+
298366
/**
299367
* Makes call ro git cli
300368
*

0 commit comments

Comments
 (0)