From b6e6aabacd068652c3c049ba245cbd69e854b132 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 8 Oct 2020 15:12:24 +0200 Subject: [PATCH] fix tests and replace file_exists --- Admin/Installer.php | 4 +- Models/UploadFile.php | 6 +-- Theme/Backend/media-single.tpl.php | 2 +- tests/Admin/AdminTest.php | 16 +++++++ tests/Admin/invalidJson.json | 3 ++ .../Api/ApiControllerCollectionTrait.php | 4 +- .../Api/ApiControllerMediaTrait.php | 4 +- tests/Models/CollectionTest.php | 36 ++++++++++++++ tests/Models/MediaMapperTest.php | 12 +++++ tests/Models/MediaTest.php | 48 +++++++++++++++++++ 10 files changed, 124 insertions(+), 11 deletions(-) create mode 100644 tests/Admin/invalidJson.json diff --git a/Admin/Installer.php b/Admin/Installer.php index c7c1c1f..565689f 100755 --- a/Admin/Installer.php +++ b/Admin/Installer.php @@ -49,17 +49,15 @@ final class Installer extends InstallerAbstract try { $dbPool->get()->con->query('select 1 from `media`'); } catch (\Exception $e) { - return; + return; // @codeCoverageIgnore } $mediaFile = \file_get_contents($data['path'] ?? ''); - if ($mediaFile === false) { throw new PathException($data['path'] ?? ''); } $mediaData = \json_decode($mediaFile, true); - if ($mediaData === false) { throw new \Exception(); } diff --git a/Models/UploadFile.php b/Models/UploadFile.php index 631445d..dafb9b6 100755 --- a/Models/UploadFile.php +++ b/Models/UploadFile.php @@ -158,7 +158,7 @@ class UploadFile $result[$key]['filename'] = $this->fileName; } - if (empty($this->fileName) || \file_exists($path . '/' . $this->fileName)) { + if (empty($this->fileName) || \is_file($path . '/' . $this->fileName)) { try { $this->fileName = $this->createFileName($path, $f['tmp_name'], $extension); $result[$key]['filename'] = $this->fileName; @@ -271,7 +271,7 @@ class UploadFile $fileName = $sha; $rnd = \mt_rand(); ++$limit; - } while (\file_exists($path . '/' . $fileName) && $limit < self::PATH_GENERATION_LIMIT); + } while (\is_file($path . '/' . $fileName) && $limit < self::PATH_GENERATION_LIMIT); if ($limit >= self::PATH_GENERATION_LIMIT) { throw new \Exception('No file path could be found. Potential attack!'); @@ -328,7 +328,7 @@ class UploadFile { do { $rndPath = \str_pad(\dechex(\mt_rand(0, 65535)), 4, '0', \STR_PAD_LEFT); - } while (\file_exists($this->outputDir . '/' . $rndPath)); + } while (\is_dir($this->outputDir . '/' . $rndPath)); return $this->outputDir . '/' . $rndPath; } diff --git a/Theme/Backend/media-single.tpl.php b/Theme/Backend/media-single.tpl.php index f4958cb..1db8b51 100755 --- a/Theme/Backend/media-single.tpl.php +++ b/Theme/Backend/media-single.tpl.php @@ -131,7 +131,7 @@ echo $this->getData('nav')->render(); isTextFile($media, $path)) : ?> - isAbsolute() ? '' : __DIR__ . '/../../../../') . $media->getPath())) : ?> + isAbsolute() ? '' : __DIR__ . '/../../../../') . $media->getPath())) : ?>