From 2ccbbd53a6ce216e20cdbe72a78bd4ed35ae2431 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Wed, 18 Aug 2021 17:30:02 +0100 Subject: [PATCH] [TESTS] Add code coverage annotations to entities --- src/Entity/Attachment.php | 16 +++++++++++++--- src/Entity/Link.php | 2 ++ src/Entity/LocalUser.php | 10 ++++++++-- src/Entity/ResetPasswordRequest.php | 4 ++++ 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Entity/Attachment.php b/src/Entity/Attachment.php index 272ee22d26..9b8ac11134 100644 --- a/src/Entity/Attachment.php +++ b/src/Entity/Attachment.php @@ -21,12 +21,12 @@ namespace App\Entity; -use App\Core\Router\Router; use App\Core\DB\DB; use App\Core\Entity; use App\Core\GSFile; use function App\Core\I18n\_m; use App\Core\Log; +use App\Core\Router\Router; use App\Util\Common; use App\Util\Exception\DuplicateFoundException; use App\Util\Exception\NotFoundException; @@ -222,8 +222,10 @@ class Attachment extends Entity if (!is_null($filepath = $this->getPath())) { if (file_exists($filepath)) { if (@unlink($filepath) === false) { + // @codeCoverageIgnoreStart Log::error("Failed deleting file for attachment with id={$this->getId()} at {$filepath}."); return false; + // @codeCoverageIgnoreEnd } else { $this->setFilename(null); $this->setSize(null); @@ -232,7 +234,9 @@ class Attachment extends Entity DB::flush(); } } else { + // @codeCoverageIgnoreStart Log::warning("File for attachment with id={$this->getId()} at {$filepath} was already deleted when I was going to handle it."); + // @codeCoverageIgnoreEnd } } return true; @@ -244,7 +248,9 @@ class Attachment extends Entity protected function delete(): bool { if ($this->getLives() > 0) { + // @codeCoverageIgnoreStart Log::warning("Deleting file {$this->getId()} with {$this->getLives()} lives. Why are you killing it so young?"); + // @codeCoverageIgnoreEnd } // Delete related files from storage $files = []; @@ -259,10 +265,14 @@ class Attachment extends Entity foreach ($files as $f) { if (file_exists($f)) { if (@unlink($f) === false) { + // @codeCoverageIgnoreStart Log::error("Failed deleting file for attachment with id={$this->getId()} at {$f}."); + // @codeCoverageIgnoreEnd } } else { + // @codeCoverageIgnoreStart Log::warning("File for attachment with id={$this->getId()} at {$f} was already deleted when I was going to handle it."); + // @codeCoverageIgnoreEnd } } DB::flush(); @@ -298,7 +308,7 @@ class Attachment extends Entity return $filename; } else { // Welp - return _m('Untitled Attachment.'); + return _m('Untitled attachment'); } } @@ -323,7 +333,7 @@ class Attachment extends Entity public function getThumbnailUrl() { - return Router::url('attachment_thumbnail', ['id' => $this->getId(), 'w' => Common::config('thumbnail', 'width'), 'h' => Common::config('thumbnail', 'height')]);; + return Router::url('attachment_thumbnail', ['id' => $this->getId(), 'w' => Common::config('thumbnail', 'width'), 'h' => Common::config('thumbnail', 'height')]); } public static function schemaDef(): array diff --git a/src/Entity/Link.php b/src/Entity/Link.php index 18b332f5be..84fdc106b1 100644 --- a/src/Entity/Link.php +++ b/src/Entity/Link.php @@ -148,8 +148,10 @@ class Link extends Entity // This must come before getInfo given that Symfony HTTPClient is lazy (thus forcing curl exec) try { $headers = $head->getHeaders(); + // @codeCoverageIgnoreStart } catch (ClientException $e) { throw new InvalidArgumentException(previous: $e); + // @codeCoverageIgnoreEnd } $url = $head->getInfo('url'); // The last effective url (after getHeaders, so it follows redirects) $url_hash = hash(self::URLHASH_ALGO, $url); diff --git a/src/Entity/LocalUser.php b/src/Entity/LocalUser.php index 8f33c6be30..6e8f4f27eb 100644 --- a/src/Entity/LocalUser.php +++ b/src/Entity/LocalUser.php @@ -317,7 +317,9 @@ class LocalUser extends Entity implements UserInterface case 1: return $users[0]; default: + // @codeCoverageIgnoreStart throw new DuplicateFoundException('Multiple values in table local_user match the requested criteria'); + // @codeCoverageIgnoreEnd } } @@ -334,7 +336,9 @@ class LocalUser extends Entity implements UserInterface self::algoNameToConstant(Common::config('security', 'algorithm')), Common::config('security', 'options')) ) { + // @codeCoverageIgnoreStart $this->changePassword(null, $password_plain_text, override: true); + // @codeCoverageIgnoreEnd } return true; } @@ -359,11 +363,13 @@ class LocalUser extends Entity implements UserInterface return password_hash($password, $algorithm, $options); } - private static function algoNameToConstant(string $algo) + /** + * Public for testing + */ + public static function algoNameToConstant(string $algo) { switch ($algo) { case 'bcrypt': - return PASSWORD_BCRYPT; case 'argon2i': case 'argon2d': case 'argon2id': diff --git a/src/Entity/ResetPasswordRequest.php b/src/Entity/ResetPasswordRequest.php index 90f64ab223..b2691d9a4c 100644 --- a/src/Entity/ResetPasswordRequest.php +++ b/src/Entity/ResetPasswordRequest.php @@ -85,6 +85,8 @@ class ResetPasswordRequest extends Entity implements ResetPasswordRequestInterfa // @codeCoverageIgnoreEnd // }}} Autocode + // {{{ Interface + // @codeCoverageIgnoreStart public function __construct(object $user, \DateTimeInterface $expiresAt, string $selector, string $hashedToken) { $this->user_id = $user->getId(); @@ -117,6 +119,8 @@ class ResetPasswordRequest extends Entity implements ResetPasswordRequestInterfa { return $this->token; } + // @codeCoverageIgnoreEnd + // }}} public static function schemaDef(): array {