Skip to content

Commit

Permalink
Add tests on method listing content of directory before plugging to u…
Browse files Browse the repository at this point in the history
…pgrade process
  • Loading branch information
Quetzacoalt91 committed Apr 26, 2024
1 parent 9616997 commit 18267e0
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 52 deletions.
17 changes: 1 addition & 16 deletions classes/UpgradeTools/FileFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

namespace PrestaShop\Module\AutoUpgrade\UpgradeTools;

use DirectoryIterator;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;

class FileFilter
Expand Down Expand Up @@ -145,27 +144,13 @@ public function getFilesToIgnoreOnUpgrade()
'/app/config/parameters.yml',
'/install',
'/install-dev',
'/modules',
'/override',
'/override/classes',
'/override/controllers',
'/override/modules',
];

// Fetch all existing native modules
$nativeModules = $this->getNativeModules();

if (is_dir($this->rootDir . '/modules')) {
$dir = new DirectoryIterator($this->rootDir . '/modules');
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDir() || $fileinfo->isDot()) {
continue;
}
if (in_array($fileinfo->getFilename(), $nativeModules)) {
$this->excludeAbsoluteFilesFromUpgrade[] = '/modules/' . $fileinfo->getFilename();
}
}
}

// this will exclude autoupgrade dir from admin, and autoupgrade from modules
// If set to false, we need to preserve the default themes
if (!$this->configuration->shouldUpdateDefaultTheme()) {
Expand Down
71 changes: 39 additions & 32 deletions classes/UpgradeTools/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@

namespace PrestaShop\Module\AutoUpgrade\UpgradeTools;

use FilesystemIterator;
use PrestaShop\Module\AutoUpgrade\Tools14;
use RecursiveCallbackFilterIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

class FilesystemAdapter
{
Expand Down Expand Up @@ -81,37 +85,40 @@ public static function deleteDirectory($dirname, $delete_self = true)
return Tools14::deleteDirectory($dirname, $delete_self);
}

public function listFilesInDir($dir, $way = 'backup', $list_directories = false)
/**
* @param string $dir
* @param 'upgrade'|'restore'|'backup' $way
* @param bool $listDirectories
*/
public function listFilesInDir($dir, $way, $listDirectories = false)
{
$list = [];
$dir = rtrim($dir, '/') . DIRECTORY_SEPARATOR;
$allFiles = false;
if (is_dir($dir) && is_readable($dir)) {
$allFiles = scandir($dir);
}
if (!is_array($allFiles)) {
return $list;
}
foreach ($allFiles as $file) {
$fullPath = $dir . $file;
// skip broken symbolic links
if (is_link($fullPath) && !is_readable($fullPath)) {
continue;
}
if ($this->isFileSkipped($file, $fullPath, $way)) {
continue;
}
if (is_dir($fullPath)) {
$list = array_merge($list, $this->listFilesInDir($fullPath, $way, $list_directories));
if ($list_directories) {
$list[] = $fullPath;
}
} else {
$list[] = $fullPath;
$files = [];
$directory = new RecursiveDirectoryIterator(
$dir,
FilesystemIterator::SKIP_DOTS | FilesystemIterator::KEY_AS_FILENAME | FilesystemIterator::CURRENT_AS_PATHNAME | FilesystemIterator::UNIX_PATHS
);
$filter = new RecursiveCallbackFilterIterator($directory, function ($current, $key, $iterator) use ($way, $dir) {
if ($this->isFileSkipped(
$key,
$current,
$way,
$dir
)) {
return false;
}

return true;
});
$iterator = new \RecursiveIteratorIterator(
$filter,
$listDirectories ? RecursiveIteratorIterator::SELF_FIRST : RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($iterator as $info) {
$files[] = $info;
}

return $list;
return $files;
}

/**
Expand Down Expand Up @@ -195,12 +202,12 @@ public function listSampleFiles($dir, $fileext = '.jpg')
}

/**
* bool _skipFile : check whether a file is in backup or restore skip list.
*
* @param string $file : current file or directory name eg:'.svn' , 'settings.inc.php'
* @param string $fullpath : current file or directory fullpath eg:'/home/web/www/prestashop/app/config/parameters.php'
* @param string $way : 'backup' , 'upgrade'
* @param string $temporaryWorkspace : If needed, another folder than the shop root can be used (used for releases)
* @param 'upgrade'|'restore'|'backup' $way
* @param string|null $temporaryWorkspace : If needed, another folder than the shop root can be used (used for releases)
*
* @return bool
*/
public function isFileSkipped($file, $fullpath, $way = 'backup', $temporaryWorkspace = null)
{
Expand All @@ -226,7 +233,7 @@ public function isFileSkipped($file, $fullpath, $way = 'backup', $temporaryWorks

foreach ($ignoreList as $path) {
$path = str_replace(DIRECTORY_SEPARATOR . 'admin', DIRECTORY_SEPARATOR . $this->adminSubDir, $path);
if ($fullpath === $rootpath . $path) {
if (strpos($fullpath, $rootpath . $path) === 0 && /* endsWith */ substr($fullpath, -strlen($rootpath . $path)) === $rootpath . $path) {
return true;
}
}
Expand Down
70 changes: 70 additions & 0 deletions tests/fixtures/listOfFiles-ReleaseExample-upgrade-with-theme.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
[
"\/.php-cs-fixer.dist.php",
"\/INSTALL.txt",
"\/LICENSES",
"\/Makefile",
"\/admin-dev",
"\/admin-dev\/.htaccess",
"\/admin-dev\/init.php",
"\/admin-dev\/robots.txt",
"\/admin-dev\/themes",
"\/admin-dev\/themes\/default",
"\/admin-dev\/themes\/default\/.stylelintignore",
"\/admin-dev\/themes\/default\/.stylelintrc",
"\/admin-dev\/themes\/default\/index.php",
"\/admin-dev\/themes\/default\/package-lock.json",
"\/admin-dev\/themes\/default\/postcss.config.js",
"\/admin-dev\/themes\/default\/scss",
"\/admin-dev\/themes\/default\/scss\/admin-theme.scss",
"\/admin-dev\/themes\/default\/scss\/font.scss",
"\/admin-dev\/themes\/default\/scss\/index.php",
"\/admin-dev\/themes\/default\/scss\/modules",
"\/admin-dev\/themes\/default\/scss\/modules\/_colors.scss",
"\/admin-dev\/themes\/default\/scss\/modules\/_mixins.scss",
"\/admin-dev\/themes\/default\/scss\/modules\/_variables.scss",
"\/admin-dev\/themes\/default\/scss\/modules\/index.php",
"\/admin-dev\/themes\/default\/scss\/rtl.scss",
"\/admin-dev\/themes\/default\/template",
"\/admin-dev\/themes\/default\/template\/content-legacy.tpl",
"\/admin-dev\/themes\/default\/template\/controllers",
"\/admin-dev\/themes\/default\/template\/controllers\/access",
"\/admin-dev\/themes\/default\/template\/controllers\/access\/helpers",
"\/admin-dev\/themes\/default\/template\/controllers\/access\/helpers\/form",
"\/admin-dev\/themes\/default\/template\/controllers\/access\/helpers\/form\/form.tpl",
"\/admin-dev\/themes\/default\/template\/controllers\/attributes",
"\/admin-dev\/themes\/default\/template\/controllers\/attributes\/helpers",
"\/admin-dev\/themes\/default\/template\/controllers\/attributes\/helpers\/form",
"\/admin-dev\/themes\/default\/template\/controllers\/attributes\/helpers\/form\/form.tpl",
"\/admin-dev\/themes\/default\/template\/controllers\/attributes\/helpers\/form\/index.php",
"\/admin-dev\/themes\/default\/template\/controllers\/attributes\/helpers\/index.php",
"\/admin-dev\/themes\/default\/template\/controllers\/attributes\/index.php",
"\/admin-dev\/themes\/default\/template\/controllers\/index.php",
"\/admin-dev\/themes\/default\/template\/controllers\/modules",
"\/admin-dev\/themes\/default\/template\/controllers\/modules\/configuration_bar.tpl",
"\/admin-dev\/themes\/default\/template\/controllers\/modules\/configure.tpl",
"\/admin-dev\/themes\/default\/template\/controllers\/modules\/content-legacy.tpl",
"\/admin-dev\/themes\/default\/template\/controllers\/modules\/favorites.tpl",
"\/admin-dev\/themes\/default\/template\/controllers\/modules\/filters.tpl",
"\/admin-dev\/themes\/default\/template\/toolbar.tpl",
"\/admin-dev\/themes\/index.php",
"\/autoload.php",
"\/composer.lock",
"\/error500.html",
"\/index.php",
"\/init.php",
"\/phpstan.neon.dist",
"\/src",
"\/src\/Controller.php",
"\/themes",
"\/themes\/classic",
"\/themes\/classic\/modules",
"\/themes\/classic\/modules\/module",
"\/themes\/classic\/modules\/module\/views",
"\/themes\/classic\/modules\/module\/views\/templates",
"\/themes\/classic\/modules\/module\/views\/templates\/module.tpl",
"\/themes\/classic\/templates",
"\/themes\/classic\/templates\/catalog",
"\/themes\/classic\/templates\/catalog\/best-sales.tpl",
"\/themes\/classic\/templates\/catalog\/listing",
"\/themes\/classic\/templates\/catalog\/listing\/tpl.tpl"
]
59 changes: 59 additions & 0 deletions tests/fixtures/listOfFiles-ReleaseExample-upgrade.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[
"\/.php-cs-fixer.dist.php",
"\/INSTALL.txt",
"\/LICENSES",
"\/Makefile",
"\/admin-dev",
"\/admin-dev\/.htaccess",
"\/admin-dev\/init.php",
"\/admin-dev\/robots.txt",
"\/admin-dev\/themes",
"\/admin-dev\/themes\/default",
"\/admin-dev\/themes\/default\/.stylelintignore",
"\/admin-dev\/themes\/default\/.stylelintrc",
"\/admin-dev\/themes\/default\/index.php",
"\/admin-dev\/themes\/default\/package-lock.json",
"\/admin-dev\/themes\/default\/postcss.config.js",
"\/admin-dev\/themes\/default\/scss",
"\/admin-dev\/themes\/default\/scss\/admin-theme.scss",
"\/admin-dev\/themes\/default\/scss\/font.scss",
"\/admin-dev\/themes\/default\/scss\/index.php",
"\/admin-dev\/themes\/default\/scss\/modules",
"\/admin-dev\/themes\/default\/scss\/modules\/_colors.scss",
"\/admin-dev\/themes\/default\/scss\/modules\/_mixins.scss",
"\/admin-dev\/themes\/default\/scss\/modules\/_variables.scss",
"\/admin-dev\/themes\/default\/scss\/modules\/index.php",
"\/admin-dev\/themes\/default\/scss\/rtl.scss",
"\/admin-dev\/themes\/default\/template",
"\/admin-dev\/themes\/default\/template\/content-legacy.tpl",
"\/admin-dev\/themes\/default\/template\/controllers",
"\/admin-dev\/themes\/default\/template\/controllers\/access",
"\/admin-dev\/themes\/default\/template\/controllers\/access\/helpers",
"\/admin-dev\/themes\/default\/template\/controllers\/access\/helpers\/form",
"\/admin-dev\/themes\/default\/template\/controllers\/access\/helpers\/form\/form.tpl",
"\/admin-dev\/themes\/default\/template\/controllers\/attributes",
"\/admin-dev\/themes\/default\/template\/controllers\/attributes\/helpers",
"\/admin-dev\/themes\/default\/template\/controllers\/attributes\/helpers\/form",
"\/admin-dev\/themes\/default\/template\/controllers\/attributes\/helpers\/form\/form.tpl",
"\/admin-dev\/themes\/default\/template\/controllers\/attributes\/helpers\/form\/index.php",
"\/admin-dev\/themes\/default\/template\/controllers\/attributes\/helpers\/index.php",
"\/admin-dev\/themes\/default\/template\/controllers\/attributes\/index.php",
"\/admin-dev\/themes\/default\/template\/controllers\/index.php",
"\/admin-dev\/themes\/default\/template\/controllers\/modules",
"\/admin-dev\/themes\/default\/template\/controllers\/modules\/configuration_bar.tpl",
"\/admin-dev\/themes\/default\/template\/controllers\/modules\/configure.tpl",
"\/admin-dev\/themes\/default\/template\/controllers\/modules\/content-legacy.tpl",
"\/admin-dev\/themes\/default\/template\/controllers\/modules\/favorites.tpl",
"\/admin-dev\/themes\/default\/template\/controllers\/modules\/filters.tpl",
"\/admin-dev\/themes\/default\/template\/toolbar.tpl",
"\/admin-dev\/themes\/index.php",
"\/autoload.php",
"\/composer.lock",
"\/error500.html",
"\/index.php",
"\/init.php",
"\/phpstan.neon.dist",
"\/src",
"\/src\/Controller.php",
"\/themes"
]
56 changes: 52 additions & 4 deletions tests/unit/UpgradeContainer/FilesystemAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,44 @@ class FilesystemAdapterTest extends TestCase
protected function setUp()
{
parent::setUp();
$this->container = new UpgradeContainer('/html', '/html/admin');
// We expect in these tests to NOT update the theme
$this->container = new UpgradeContainer('/html', '/html/admin'); // We expect in these tests to NOT update the theme
$this->container->getUpgradeConfiguration()->set('PS_AUTOUP_UPDATE_DEFAULT_THEME', false);
$this->filesystemAdapter = $this->container->getFilesystemAdapter();
}

public function testListFilesInDirForUpgrade()
{
$pathToRelease = __DIR__ . '/../../releases/release-example';
$expected = $this->loadFixtureAndAddPrefixToFilePaths(
__DIR__ . '/../../fixtures/listOfFiles-ReleaseExample-upgrade.json',
__DIR__ . '/../../releases/release-example'
);

$actual = $this->filesystemAdapter->listFilesInDir(
$pathToRelease, 'upgrade', true
);
// TODO: Should try using assertEqualsCanonicalizing after upgrade of PHPUnit
$this->assertEquals([], array_diff($expected, $actual), "There are more files in the expected array than in the actual list: \n" . implode("\n", array_diff($expected, $actual)));
$this->assertEquals([], array_diff($actual, $expected), "There are more files in the actual array than in the expected list: \n" . implode("\n", array_diff($actual, $expected)));
}

public function testListFilesInDirForUpgradeWithTheme()
{
$this->container->getUpgradeConfiguration()->set('PS_AUTOUP_UPDATE_DEFAULT_THEME', true);
$pathToRelease = __DIR__ . '/../../releases/release-example';
$expected = $this->loadFixtureAndAddPrefixToFilePaths(
__DIR__ . '/../../fixtures/listOfFiles-ReleaseExample-upgrade-with-theme.json',
__DIR__ . '/../../releases/release-example'
);

$actual = $this->filesystemAdapter->listFilesInDir(
$pathToRelease, 'upgrade', true
);
// TODO: Should try using assertEqualsCanonicalizing after upgrade of PHPUnit
$this->assertEquals([], array_diff($expected, $actual), "There are more files in the expected array than in the actual list: \n" . implode("\n", array_diff($expected, $actual)));
$this->assertEquals([], array_diff($actual, $expected), "There are more files in the actual array than in the expected list: \n" . implode("\n", array_diff($actual, $expected)));
}

/**
* @dataProvider ignoredFilesProvider
*/
Expand All @@ -49,7 +81,8 @@ public function testFileIsIgnored($file, $fullpath, $process)
$this->filesystemAdapter->isFileSkipped(
$file,
$this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . $fullpath,
$process));
$process,
$this->container->getProperty(UpgradeContainer::PS_ROOT_PATH)));
}

/**
Expand Down Expand Up @@ -78,7 +111,8 @@ public function testFileIsNotIgnored($file, $fullpath, $process)
$this->filesystemAdapter->isFileSkipped(
$file,
$this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . $fullpath,
$process));
$process,
$this->container->getProperty(UpgradeContainer::PS_ROOT_PATH)));
}

public function ignoredFilesProvider()
Expand All @@ -94,6 +128,10 @@ public function ignoredFilesProvider()
['autoupgrade', '/modules/autoupgrade', 'restore'],
['autoupgrade', '/modules/autoupgrade', 'backup'],

['classes', '/override/classes', 'upgrade'],
['controllers', '/override/controllers', 'upgrade'],
['modules', '/override/modules', 'upgrade'],

['parameters.yml', '/app/config/parameters.yml', 'upgrade'],

['classic', '/themes/classic', 'upgrade'],
Expand All @@ -104,6 +142,7 @@ public function notIgnoredFilesProvider()
{
return [
['parameters.yml', '/app/config/parameters.yml', 'backup'],
['modules', '/some/unrelated/folder/named/modules', 'upgrade'],

['doge.txt', '/doge.txt', 'upgrade'],
['parameters.yml', '/parameters.yml', 'upgrade'],
Expand Down Expand Up @@ -154,4 +193,13 @@ protected function fillFolderWithPsAssets($folder)
mkdir($folder . DIRECTORY_SEPARATOR . 'controllers');
touch($folder . DIRECTORY_SEPARATOR . 'index.php');
}

private function loadFixtureAndAddPrefixToFilePaths($fixturePath, $prefixToAdd)
{
$fileContents = json_decode(file_get_contents($fixturePath), true);

return array_map(function ($path) use ($prefixToAdd) {
return $prefixToAdd . $path;
}, $fileContents);
}
}

0 comments on commit 18267e0

Please sign in to comment.