From ea16e9ab83db293d961ab440f0b338a60ef69711 Mon Sep 17 00:00:00 2001
From: Vitaliy Goncharenko <vgoncharenko@magento.com>
Date: Thu, 1 Dec 2016 20:08:27 +0200
Subject: [PATCH] MAGETWO-60201: Test UnsecureFunctionsUsageTest fails in case
 of changes in files with unsecure code

- fixed static build
---
 .../TestFramework/Utility/ChangedFiles.php    |  8 ++-
 .../Utility/FunctionDetector.php              | 66 +++++++++++++++----
 dev/tests/static/get_github_changes.php       |  6 +-
 3 files changed, 65 insertions(+), 15 deletions(-)

diff --git a/dev/tests/static/framework/Magento/TestFramework/Utility/ChangedFiles.php b/dev/tests/static/framework/Magento/TestFramework/Utility/ChangedFiles.php
index 42b34560ae4..a25b16c2277 100644
--- a/dev/tests/static/framework/Magento/TestFramework/Utility/ChangedFiles.php
+++ b/dev/tests/static/framework/Magento/TestFramework/Utility/ChangedFiles.php
@@ -60,10 +60,14 @@ class ChangedFiles
      */
     public static function getChangedContent($fileName)
     {
+        $data = [];
         $extension = self::getFileExtension($fileName);
         $fileName = ltrim(str_replace(BP, '', $fileName), DIRECTORY_SEPARATOR);
-        $changedContent = file_get_contents(BP . sprintf(self::CHANGED_FILES_CONTENT_FILE, $extension));
-        $data = json_decode($changedContent, true);
+        $changedFilesContentFile = BP . sprintf(self::CHANGED_FILES_CONTENT_FILE, $extension);
+        if (file_exists($changedFilesContentFile)) {
+            $changedContent = file_get_contents($changedFilesContentFile);
+            $data = json_decode($changedContent, true);
+        }
 
         return isset($data[$fileName]) ? $data[$fileName] : '';
     }
diff --git a/dev/tests/static/framework/Magento/TestFramework/Utility/FunctionDetector.php b/dev/tests/static/framework/Magento/TestFramework/Utility/FunctionDetector.php
index 308b34855a3..69162d3dfba 100644
--- a/dev/tests/static/framework/Magento/TestFramework/Utility/FunctionDetector.php
+++ b/dev/tests/static/framework/Magento/TestFramework/Utility/FunctionDetector.php
@@ -36,22 +36,64 @@ class FunctionDetector
         $result = [];
         $regexp = $this->composeRegexp($functions);
 
-        if ($regexp) {
-            $fileContent = \Magento\TestFramework\Utility\ChangedFiles::getChangedContent($filePath);
-            $matches = preg_grep($regexp, explode("\n", $fileContent));
-            $file = file($filePath);
-            if (!empty($matches)) {
-                foreach ($matches as $line) {
-                    $actualFunctions = [];
-                    foreach ($functions as $function) {
-                        if (false !== strpos($line, $function)) {
-                            $actualFunctions[] = $function;
-                        }
+        if (!$regexp) {
+            return $result;
+        }
+
+        $fileContent = \Magento\TestFramework\Utility\ChangedFiles::getChangedContent($filePath);
+        $file = file($filePath);
+
+        return $fileContent
+            ? $this->grepChangedContent($file, $regexp, $functions, $fileContent)
+            : $this->grepFile($file, $regexp);
+    }
+
+    /**
+     * Grep only changed content.
+     *
+     * @param array $file
+     * @param string $regexp
+     * @param string[] $functions
+     * @param string $fileContent
+     * @return array
+     */
+    public function grepChangedContent(array $file, $regexp, $functions, $fileContent)
+    {
+        $result = [];
+        $matches = preg_grep($regexp, explode("\n", $fileContent));
+        if (!empty($matches)) {
+            foreach ($matches as $line) {
+                $actualFunctions = [];
+                foreach ($functions as $function) {
+                    if (false !== strpos($line, $function)) {
+                        $actualFunctions[] = $function;
                     }
-                    $result[array_search($line . "\n", $file) + 1] = $actualFunctions;
                 }
+                $result[array_search($line . "\n", $file) + 1] = $actualFunctions;
             }
         }
+
+        return $result;
+    }
+
+    /**
+     * Grep File.
+     *
+     * @param array $file
+     * @param string $regexp
+     * @return array
+     */
+    public function grepFile(array $file, $regexp)
+    {
+        $result = [];
+        array_unshift($file, '');
+        $lines = preg_grep($regexp, $file);
+        foreach ($lines as $lineNumber => $line) {
+            if (preg_match_all($regexp, $line, $matches)) {
+                $result[$lineNumber] = $matches[1];
+            }
+        }
+
         return $result;
     }
 
diff --git a/dev/tests/static/get_github_changes.php b/dev/tests/static/get_github_changes.php
index 7e57a13552d..16a4f7d8e09 100644
--- a/dev/tests/static/get_github_changes.php
+++ b/dev/tests/static/get_github_changes.php
@@ -56,7 +56,11 @@ function saveChangedFileContent(GitRepo $repo)
 {
     $changedFilesContentFileName = BP . Magento\TestFramework\Utility\ChangedFiles::CHANGED_FILES_CONTENT_FILE;
     foreach ($repo->getChangedContentFiles() as $key => $changedContentFile) {
-        file_put_contents(sprintf($changedFilesContentFileName, $key), json_encode($changedContentFile), FILE_APPEND);
+        $filePath = sprintf($changedFilesContentFileName, $key);
+        $oldContent = file_exists($filePath) ? file_get_contents($filePath) : '{}';
+        $oldData = json_decode($oldContent, true);
+        $data = array_merge($oldData, $changedContentFile);
+        file_put_contents($filePath, json_encode($data));
     }
 }
 
-- 
GitLab