mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-07 14:41:20 +00:00
Improve test coverage.
This commit is contained in:
@@ -41,6 +41,7 @@ class BelongsUser implements Rule
|
||||
* Create a new rule instance.
|
||||
*
|
||||
* @return void
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
@@ -51,6 +52,7 @@ class BelongsUser implements Rule
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @return string
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function message(): string
|
||||
{
|
||||
|
||||
@@ -36,6 +36,7 @@ class IsAssetAccountId implements Rule
|
||||
* Get the validation error message. This is not translated because only the API uses it.
|
||||
*
|
||||
* @return string
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function message(): string
|
||||
{
|
||||
|
||||
@@ -33,7 +33,7 @@ class IsBoolean implements Rule
|
||||
{
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @return string
|
||||
*/
|
||||
public function message(): string
|
||||
@@ -51,25 +51,16 @@ class IsBoolean implements Rule
|
||||
*/
|
||||
public function passes($attribute, $value): bool
|
||||
{
|
||||
if (\is_bool($value)) {
|
||||
if (is_bool($value)) {
|
||||
return true;
|
||||
}
|
||||
if (\is_int($value) && 0 === $value) {
|
||||
if (is_int($value) && 0 === $value) {
|
||||
return true;
|
||||
}
|
||||
if (\is_int($value) && 1 === $value) {
|
||||
if (is_int($value) && 1 === $value) {
|
||||
return true;
|
||||
}
|
||||
if (\is_string($value) && '1' === $value) {
|
||||
return true;
|
||||
}
|
||||
if (\is_string($value) && '0' === $value) {
|
||||
return true;
|
||||
}
|
||||
if (\is_string($value) && 'true' === $value) {
|
||||
return true;
|
||||
}
|
||||
if (\is_string($value) && 'false' === $value) {
|
||||
if (is_string($value) && in_array($value, ['0', '1', 'true', 'false', 'on', 'off', 'yes', 'no', 'y', 'n'])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ namespace FireflyIII\Rules;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Carbon\Exceptions\InvalidDateException;
|
||||
use Exception;
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Log;
|
||||
|
||||
@@ -39,6 +40,7 @@ class IsDateOrTime implements Rule
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @return string|array
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
@@ -56,11 +58,14 @@ class IsDateOrTime implements Rule
|
||||
public function passes($attribute, $value): bool
|
||||
{
|
||||
$value = (string)$value;
|
||||
if ('' === $value) {
|
||||
return false;
|
||||
}
|
||||
if (10 === strlen($value)) {
|
||||
// probably a date format.
|
||||
try {
|
||||
Carbon::createFromFormat('Y-m-d', $value);
|
||||
} catch (InvalidDateException $e) {
|
||||
} catch (InvalidDateException|Exception $e) {
|
||||
Log::error(sprintf('"%s" is not a valid date: %s', $value, $e->getMessage()));
|
||||
|
||||
return false;
|
||||
@@ -71,7 +76,7 @@ class IsDateOrTime implements Rule
|
||||
// is an atom string, I hope?
|
||||
try {
|
||||
Carbon::parse($value);
|
||||
} catch (InvalidDateException $e) {
|
||||
} catch (InvalidDateException|Exception $e) {
|
||||
Log::error(sprintf('"%s" is not a valid date or time: %s', $value, $e->getMessage()));
|
||||
|
||||
return false;
|
||||
|
||||
@@ -46,16 +46,19 @@ class IsValidAttachmentModel implements Rule
|
||||
/**
|
||||
* IsValidAttachmentModel constructor.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param string $model
|
||||
*/
|
||||
public function __construct(string $model)
|
||||
{
|
||||
$model = $this->normalizeModel($model);
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @return string
|
||||
*/
|
||||
public function message(): string
|
||||
@@ -78,9 +81,9 @@ class IsValidAttachmentModel implements Rule
|
||||
if (!auth()->check()) {
|
||||
return false;
|
||||
}
|
||||
$model = false === strpos('FireflyIII', $this->model) ? 'FireflyIII\\Models\\' . $this->model : $this->model;
|
||||
|
||||
if (Bill::class === $model) {
|
||||
|
||||
if (Bill::class === $this->model) {
|
||||
/** @var BillRepositoryInterface $repository */
|
||||
$repository = app(BillRepositoryInterface::class);
|
||||
/** @var User $user */
|
||||
@@ -91,7 +94,7 @@ class IsValidAttachmentModel implements Rule
|
||||
return null !== $bill;
|
||||
}
|
||||
|
||||
if (ImportJob::class === $model) {
|
||||
if (ImportJob::class === $this->model) {
|
||||
/** @var ImportJobRepositoryInterface $repository */
|
||||
$repository = app(ImportJobRepositoryInterface::class);
|
||||
/** @var User $user */
|
||||
@@ -102,7 +105,7 @@ class IsValidAttachmentModel implements Rule
|
||||
return null !== $importJob;
|
||||
}
|
||||
|
||||
if (Transaction::class === $model) {
|
||||
if (Transaction::class === $this->model) {
|
||||
/** @var JournalRepositoryInterface $repository */
|
||||
$repository = app(JournalRepositoryInterface::class);
|
||||
/** @var User $user */
|
||||
@@ -113,7 +116,7 @@ class IsValidAttachmentModel implements Rule
|
||||
return null !== $transaction;
|
||||
}
|
||||
|
||||
if (TransactionJournal::class === $model) {
|
||||
if (TransactionJournal::class === $this->model) {
|
||||
$repository = app(JournalRepositoryInterface::class);
|
||||
$user = auth()->user();
|
||||
$repository->setUser($user);
|
||||
@@ -121,8 +124,23 @@ class IsValidAttachmentModel implements Rule
|
||||
|
||||
return null !== $result;
|
||||
}
|
||||
Log::error(sprintf('No model was recognized from string "%s"', $model));
|
||||
Log::error(sprintf('No model was recognized from string "%s"', $this->model));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $model
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function normalizeModel(string $model): string
|
||||
{
|
||||
$search = ['FireflyIII\Models\\'];
|
||||
$replace = '';
|
||||
$model = str_replace($search, $replace, $model);
|
||||
|
||||
$model = sprintf('FireflyIII\Models\%s', $model);
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ namespace FireflyIII\Rules;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
@@ -43,6 +42,8 @@ class UniqueIban implements Rule
|
||||
/**
|
||||
* Create a new rule instance.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param Account|null $account
|
||||
* @param string|null $expectedType
|
||||
*/
|
||||
@@ -55,6 +56,8 @@ class UniqueIban implements Rule
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function message(): string
|
||||
@@ -78,13 +81,13 @@ class UniqueIban implements Rule
|
||||
return true; // @codeCoverageIgnore
|
||||
}
|
||||
if (null === $this->expectedType) {
|
||||
return true;
|
||||
return true; // @codeCoverageIgnore
|
||||
}
|
||||
$maxCounts = $this->getMaxOccurrences();
|
||||
|
||||
foreach ($maxCounts as $type => $max) {
|
||||
$count = $this->countHits($type, $value);
|
||||
|
||||
Log::debug(sprintf('Count for "%s" and IBAN "%s" is %d', $type, $value, $count));
|
||||
if ($count > $max) {
|
||||
Log::debug(
|
||||
sprintf(
|
||||
@@ -108,24 +111,18 @@ class UniqueIban implements Rule
|
||||
*/
|
||||
private function countHits(string $type, string $iban): int
|
||||
{
|
||||
$count = 0;
|
||||
/** @noinspection NullPointerExceptionInspection */
|
||||
$query = auth()->user()
|
||||
->accounts()
|
||||
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
|
||||
->where('account_types.type', $type);
|
||||
$query
|
||||
= auth()->user()
|
||||
->accounts()
|
||||
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
|
||||
->where('accounts.iban', $iban)
|
||||
->where('account_types.type', $type);
|
||||
|
||||
if (null !== $this->account) {
|
||||
$query->where('accounts.id', '!=', $this->account->id);
|
||||
}
|
||||
/** @var Collection $result */
|
||||
$result = $query->get(['accounts.*']);
|
||||
foreach ($result as $account) {
|
||||
if ($account->iban === $iban) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
return $count;
|
||||
return $query->count();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -31,6 +31,7 @@ use Log;
|
||||
|
||||
/**
|
||||
* Class ValidJournals
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class ValidJournals implements Rule
|
||||
{
|
||||
|
||||
@@ -27,6 +27,7 @@ use Illuminate\Contracts\Validation\Rule;
|
||||
|
||||
/**
|
||||
* Class ValidRecurrenceRepetitionType
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class ValidRecurrenceRepetitionType implements Rule
|
||||
{
|
||||
|
||||
@@ -31,6 +31,7 @@ use Log;
|
||||
|
||||
/**
|
||||
* Class ValidRecurrenceRepetitionValue
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class ValidRecurrenceRepetitionValue implements Rule
|
||||
{
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ValidTransactions.php
|
||||
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace FireflyIII\Rules;
|
||||
|
||||
use FireflyIII\Models\Transaction;
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class ValidTransactions
|
||||
*/
|
||||
class ValidTransactions implements Rule
|
||||
{
|
||||
/**
|
||||
* Create a new rule instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function message(): string
|
||||
{
|
||||
return (string)trans('validation.invalid_selection');
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function passes($attribute, $value): bool
|
||||
{
|
||||
Log::debug('In ValidTransactions::passes');
|
||||
if (!is_array($value)) {
|
||||
return true;
|
||||
}
|
||||
$userId = auth()->user()->id;
|
||||
foreach ($value as $transactionId) {
|
||||
$count = Transaction::where('transactions.id', $transactionId)
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
||||
->where('accounts.user_id', $userId)->count();
|
||||
if (0 === $count) {
|
||||
Log::debug(sprintf('Count for transaction #%d and user #%d is zero! Return FALSE', $transactionId, $userId));
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Log::debug('Return true!');
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ use Illuminate\Contracts\Validation\Rule;
|
||||
/**
|
||||
*
|
||||
* Class ZeroOrMore
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class ZeroOrMore implements Rule
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user