mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Improve test coverage.
This commit is contained in:
parent
6fdfa722dd
commit
aacd218056
@ -55,13 +55,17 @@ class MigrateAttachments extends Command
|
|||||||
*/
|
*/
|
||||||
public function handle(): int
|
public function handle(): int
|
||||||
{
|
{
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
$start = microtime(true);
|
$start = microtime(true);
|
||||||
if ($this->isExecuted() && true !== $this->option('force')) {
|
if ($this->isExecuted() && true !== $this->option('force')) {
|
||||||
$this->warn('This command has already been executed.');
|
$this->warn('This command has already been executed.');
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
|
||||||
$attachments = Attachment::get();
|
$attachments = Attachment::get();
|
||||||
|
$count = 0;
|
||||||
|
|
||||||
/** @var Attachment $att */
|
/** @var Attachment $att */
|
||||||
foreach ($attachments as $att) {
|
foreach ($attachments as $att) {
|
||||||
@ -69,6 +73,7 @@ class MigrateAttachments extends Command
|
|||||||
// move description:
|
// move description:
|
||||||
$description = (string)$att->description;
|
$description = (string)$att->description;
|
||||||
if ('' !== $description) {
|
if ('' !== $description) {
|
||||||
|
|
||||||
// find or create note:
|
// find or create note:
|
||||||
$note = $att->notes()->first();
|
$note = $att->notes()->first();
|
||||||
if (null === $note) {
|
if (null === $note) {
|
||||||
@ -82,9 +87,16 @@ class MigrateAttachments extends Command
|
|||||||
$att->description = '';
|
$att->description = '';
|
||||||
$att->save();
|
$att->save();
|
||||||
|
|
||||||
Log::debug(sprintf('Migrated attachment #%s description to note #%d', $att->id, $note->id));
|
Log::debug(sprintf('Migrated attachment #%s description to note #%d.', $att->id, $note->id));
|
||||||
|
$count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (0 === $count) {
|
||||||
|
$this->line('All attachments are OK.');
|
||||||
|
}
|
||||||
|
if (0 !== $count) {
|
||||||
|
$this->line(sprintf('Updated %d attachment(s).',$count));
|
||||||
|
}
|
||||||
$end = round(microtime(true) - $start, 2);
|
$end = round(microtime(true) - $start, 2);
|
||||||
$this->info(sprintf('Migrated attachment notes in %s seconds.', $end));
|
$this->info(sprintf('Migrated attachment notes in %s seconds.', $end));
|
||||||
$this->markAsExecuted();
|
$this->markAsExecuted();
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* MigrateNotes.php
|
* MigrateJournalNotes.php
|
||||||
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
||||||
*
|
*
|
||||||
* This file is part of Firefly III.
|
* This file is part of Firefly III.
|
||||||
@ -31,9 +31,9 @@ use Illuminate\Console\Command;
|
|||||||
use Log;
|
use Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class MigrateNotes
|
* Class MigrateJournalNotes
|
||||||
*/
|
*/
|
||||||
class MigrateNotes extends Command
|
class MigrateJournalNotes extends Command
|
||||||
{
|
{
|
||||||
public const CONFIG_NAME = '4780_migrate_notes';
|
public const CONFIG_NAME = '4780_migrate_notes';
|
||||||
/**
|
/**
|
||||||
@ -41,7 +41,7 @@ class MigrateNotes extends Command
|
|||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $description = 'Migrate notes';
|
protected $description = 'Migrate notes for transaction journals.';
|
||||||
/**
|
/**
|
||||||
* The name and signature of the console command.
|
* The name and signature of the console command.
|
||||||
*
|
*
|
||||||
@ -57,12 +57,14 @@ class MigrateNotes extends Command
|
|||||||
public function handle(): int
|
public function handle(): int
|
||||||
{
|
{
|
||||||
$start = microtime(true);
|
$start = microtime(true);
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
if ($this->isExecuted() && true !== $this->option('force')) {
|
if ($this->isExecuted() && true !== $this->option('force')) {
|
||||||
$this->warn('This command has already been executed.');
|
$this->warn('This command has already been executed.');
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
$count = 0;
|
||||||
/** @noinspection PhpUndefinedMethodInspection */
|
/** @noinspection PhpUndefinedMethodInspection */
|
||||||
$set = TransactionJournalMeta::whereName('notes')->get();
|
$set = TransactionJournalMeta::whereName('notes')->get();
|
||||||
/** @var TransactionJournalMeta $meta */
|
/** @var TransactionJournalMeta $meta */
|
||||||
@ -79,10 +81,21 @@ class MigrateNotes extends Command
|
|||||||
Log::debug(sprintf('Migrated meta note #%d to Note #%d', $meta->id, $note->id));
|
Log::debug(sprintf('Migrated meta note #%d to Note #%d', $meta->id, $note->id));
|
||||||
try {
|
try {
|
||||||
$meta->delete();
|
$meta->delete();
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::error(sprintf('Could not delete old meta entry #%d: %s', $meta->id, $e->getMessage()));
|
Log::error(sprintf('Could not delete old meta entry #%d: %s', $meta->id, $e->getMessage()));
|
||||||
}
|
}
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
$count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (0 === $count) {
|
||||||
|
$this->line('No notes to migrate.');
|
||||||
|
}
|
||||||
|
if (0 !== $count) {
|
||||||
|
$this->line(sprintf('Migrated %d note(s).', $count));
|
||||||
|
}
|
||||||
|
|
||||||
$end = round(microtime(true) - $start, 2);
|
$end = round(microtime(true) - $start, 2);
|
||||||
$this->info(sprintf('Migrated notes in %s seconds.', $end));
|
$this->info(sprintf('Migrated notes in %s seconds.', $end));
|
||||||
$this->markAsExecuted();
|
$this->markAsExecuted();
|
@ -60,6 +60,7 @@ class MigrateToGroups extends Command
|
|||||||
private $journalRepository;
|
private $journalRepository;
|
||||||
/** @var JournalDestroyService */
|
/** @var JournalDestroyService */
|
||||||
private $service;
|
private $service;
|
||||||
|
private $count;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new command instance.
|
* Create a new command instance.
|
||||||
@ -69,6 +70,7 @@ class MigrateToGroups extends Command
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
$this->count = 0;
|
||||||
$this->journalRepository = app(JournalRepositoryInterface::class);
|
$this->journalRepository = app(JournalRepositoryInterface::class);
|
||||||
$this->service = app(JournalDestroyService::class);
|
$this->service = app(JournalDestroyService::class);
|
||||||
$this->groupFactory = app(TransactionGroupFactory::class);
|
$this->groupFactory = app(TransactionGroupFactory::class);
|
||||||
@ -83,14 +85,17 @@ class MigrateToGroups extends Command
|
|||||||
public function handle(): int
|
public function handle(): int
|
||||||
{
|
{
|
||||||
$start = microtime(true);
|
$start = microtime(true);
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
if ($this->isMigrated() && true !== $this->option('force')) {
|
if ($this->isMigrated() && true !== $this->option('force')) {
|
||||||
$this->info('Database already seems to be migrated.');
|
$this->info('Database already seems to be migrated.');
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (true === $this->option('force')) {
|
if (true === $this->option('force')) {
|
||||||
$this->warn('Forcing the migration.');
|
$this->warn('Forcing the migration.');
|
||||||
}
|
}
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
|
||||||
Log::debug('---- start group migration ----');
|
Log::debug('---- start group migration ----');
|
||||||
$this->makeGroupsFromSplitJournals();
|
$this->makeGroupsFromSplitJournals();
|
||||||
@ -102,14 +107,24 @@ class MigrateToGroups extends Command
|
|||||||
Log::debug('---- end group migration ----');
|
Log::debug('---- end group migration ----');
|
||||||
$end = round(microtime(true) - $start, 2);
|
$end = round(microtime(true) - $start, 2);
|
||||||
$this->info(sprintf('Migrate all journals to groups in %s seconds.', $end));
|
$this->info(sprintf('Migrate all journals to groups in %s seconds.', $end));
|
||||||
|
|
||||||
|
if (0 !== $this->count) {
|
||||||
|
$this->line(sprintf('Migrated %d transaction journal(s).', $this->count));
|
||||||
|
}
|
||||||
|
if (0 === $this->count) {
|
||||||
|
$this->line('No journals to migrate to groups.');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
$this->markAsMigrated();
|
$this->markAsMigrated();
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param TransactionJournal $journal
|
* @param TransactionJournal $journal
|
||||||
* @param Transaction $transaction
|
* @param Transaction $transaction
|
||||||
*
|
*
|
||||||
* @return Transaction|null
|
* @return Transaction|null
|
||||||
*/
|
*/
|
||||||
@ -117,7 +132,12 @@ class MigrateToGroups extends Command
|
|||||||
{
|
{
|
||||||
$set = $journal->transactions->filter(
|
$set = $journal->transactions->filter(
|
||||||
static function (Transaction $subject) use ($transaction) {
|
static function (Transaction $subject) use ($transaction) {
|
||||||
return $transaction->amount * -1 === (float)$subject->amount && $transaction->identifier === $subject->identifier;
|
$amount = (float)$transaction->amount * -1 === (float)$subject->amount;
|
||||||
|
$identifier = $transaction->identifier === $subject->identifier;
|
||||||
|
Log::debug(sprintf('Amount the same? %s', var_export($amount, true)));
|
||||||
|
Log::debug(sprintf('ID the same? %s', var_export($identifier, true)));
|
||||||
|
|
||||||
|
return $amount && $identifier;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -152,6 +172,7 @@ class MigrateToGroups extends Command
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
DB::table('transaction_journals')->where('id', $array['id'])->update(['transaction_group_id' => $groupId]);
|
DB::table('transaction_journals')->where('id', $array['id'])->update(['transaction_group_id' => $groupId]);
|
||||||
|
$this->count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -209,14 +230,17 @@ class MigrateToGroups extends Command
|
|||||||
* @param TransactionJournal $journal
|
* @param TransactionJournal $journal
|
||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||||
*/
|
*/
|
||||||
private function makeMultiGroup(TransactionJournal $journal): void
|
private function makeMultiGroup(TransactionJournal $journal): void
|
||||||
{
|
{
|
||||||
// double check transaction count.
|
// double check transaction count.
|
||||||
if ($journal->transactions->count() <= 2) {
|
if ($journal->transactions->count() <= 2) {
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
Log::debug(sprintf('Will not try to convert journal #%d because it has 2 or less transactions.', $journal->id));
|
Log::debug(sprintf('Will not try to convert journal #%d because it has 2 or less transactions.', $journal->id));
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
}
|
}
|
||||||
Log::debug(sprintf('Will now try to convert journal #%d', $journal->id));
|
Log::debug(sprintf('Will now try to convert journal #%d', $journal->id));
|
||||||
|
|
||||||
@ -264,6 +288,7 @@ class MigrateToGroups extends Command
|
|||||||
$opposingTr = $this->findOpposingTransaction($journal, $transaction);
|
$opposingTr = $this->findOpposingTransaction($journal, $transaction);
|
||||||
|
|
||||||
if (null === $opposingTr) {
|
if (null === $opposingTr) {
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
$this->error(
|
$this->error(
|
||||||
sprintf(
|
sprintf(
|
||||||
'Journal #%d has no opposing transaction for transaction #%d. Cannot upgrade this entry.',
|
'Journal #%d has no opposing transaction for transaction #%d. Cannot upgrade this entry.',
|
||||||
@ -271,6 +296,7 @@ class MigrateToGroups extends Command
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
}
|
}
|
||||||
|
|
||||||
$tArray = [
|
$tArray = [
|
||||||
@ -321,12 +347,16 @@ class MigrateToGroups extends Command
|
|||||||
// delete the old transaction journal.
|
// delete the old transaction journal.
|
||||||
$this->service->destroy($journal);
|
$this->service->destroy($journal);
|
||||||
|
|
||||||
|
$this->count++;
|
||||||
|
|
||||||
// report on result:
|
// report on result:
|
||||||
Log::debug(
|
Log::debug(
|
||||||
sprintf('Migrated journal #%d into group #%d with these journals: #%s', $journal->id, $group->id, implode(', #', $group->transactionJournals->pluck('id')->toArray()))
|
sprintf('Migrated journal #%d into group #%d with these journals: #%s',
|
||||||
|
$journal->id, $group->id, implode(', #', $group->transactionJournals->pluck('id')->toArray()))
|
||||||
);
|
);
|
||||||
$this->line(
|
$this->line(
|
||||||
sprintf('Migrated journal #%d into group #%d with these journals: #%s', $journal->id, $group->id, implode(', #', $group->transactionJournals->pluck('id')->toArray()))
|
sprintf('Migrated journal #%d into group #%d with these journals: #%s',
|
||||||
|
$journal->id, $group->id, implode(', #', $group->transactionJournals->pluck('id')->toArray()))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,19 +24,16 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Console\Commands\Upgrade;
|
namespace FireflyIII\Console\Commands\Upgrade;
|
||||||
|
|
||||||
|
|
||||||
use Crypt;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Models\Bill;
|
use FireflyIII\Models\Bill;
|
||||||
use FireflyIII\Models\Preference;
|
use FireflyIII\Models\Preference;
|
||||||
use FireflyIII\Models\Rule;
|
|
||||||
use FireflyIII\Models\RuleAction;
|
|
||||||
use FireflyIII\Models\RuleGroup;
|
use FireflyIII\Models\RuleGroup;
|
||||||
use FireflyIII\Models\RuleTrigger;
|
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
||||||
use FireflyIII\Models\TransactionCurrency;
|
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
|
||||||
|
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
|
||||||
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Contracts\Encryption\DecryptException;
|
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
use Log;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class MigrateToRules
|
* Class MigrateToRules
|
||||||
@ -57,171 +54,60 @@ class MigrateToRules extends Command
|
|||||||
*/
|
*/
|
||||||
protected $signature = 'firefly-iii:bills-to-rules {--F|force : Force the execution of this command.}';
|
protected $signature = 'firefly-iii:bills-to-rules {--F|force : Force the execution of this command.}';
|
||||||
|
|
||||||
|
/** @var UserRepositoryInterface */
|
||||||
|
private $userRepository;
|
||||||
|
/** @var RuleGroupRepositoryInterface */
|
||||||
|
private $ruleGroupRepository;
|
||||||
|
/** @var BillRepositoryInterface */
|
||||||
|
private $billRepository;
|
||||||
|
/** @var RuleRepositoryInterface */
|
||||||
|
private $ruleRepository;
|
||||||
|
private $count;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MigrateToRules constructor.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
$this->count = 0;
|
||||||
|
$this->userRepository = app(UserRepositoryInterface::class);
|
||||||
|
$this->ruleGroupRepository = app(RuleGroupRepositoryInterface::class);
|
||||||
|
$this->billRepository = app(BillRepositoryInterface::class);
|
||||||
|
$this->ruleRepository = app(RuleRepositoryInterface::class);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the console command.
|
* Execute the console command.
|
||||||
*
|
*
|
||||||
* @return int
|
* @return int
|
||||||
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
public function handle(): int
|
public function handle(): int
|
||||||
{
|
{
|
||||||
$start = microtime(true);
|
$start = microtime(true);
|
||||||
|
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
if ($this->isExecuted() && true !== $this->option('force')) {
|
if ($this->isExecuted() && true !== $this->option('force')) {
|
||||||
$this->warn('This command has already been executed.');
|
$this->warn('This command has already been executed.');
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
|
||||||
foreach (User::get() as $user) {
|
$users = $this->userRepository->all();
|
||||||
/** @var Preference $lang */
|
/** @var User $user */
|
||||||
$lang = app('preferences')->getForUser($user, 'language', 'en_US');
|
foreach ($users as $user) {
|
||||||
$groupName = (string)trans('firefly.rulegroup_for_bills_title', [], $lang->data);
|
$this->migrateUser($user);
|
||||||
$ruleGroup = $user->ruleGroups()->where('title', $groupName)->first();
|
|
||||||
$currencyPreference = app('preferences')->getForUser($user, 'currencyPreference', config('firefly.default_currency', 'EUR'));
|
|
||||||
|
|
||||||
if (null === $currencyPreference) {
|
|
||||||
$this->error('User has no currency preference. Impossible.');
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
$currencyCode = $this->tryDecrypt($currencyPreference->data);
|
|
||||||
|
|
||||||
// try json decrypt just in case.
|
|
||||||
if (strlen($currencyCode) > 3) {
|
|
||||||
$currencyCode = json_decode($currencyCode) ?? 'EUR';
|
|
||||||
}
|
|
||||||
|
|
||||||
$currency = TransactionCurrency::where('code', $currencyCode)->first();
|
|
||||||
if (null === $currency) {
|
|
||||||
$this->line('Fall back to default currency in migrateBillsToRules().');
|
|
||||||
$currency = app('amount')->getDefaultCurrencyByUser($user);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (null === $ruleGroup) {
|
|
||||||
$array = RuleGroup::get(['order'])->pluck('order')->toArray();
|
|
||||||
$order = count($array) > 0 ? max($array) + 1 : 1;
|
|
||||||
$ruleGroup = RuleGroup::create(
|
|
||||||
[
|
|
||||||
'user_id' => $user->id,
|
|
||||||
'title' => (string)trans('firefly.rulegroup_for_bills_title', [], $lang->data),
|
|
||||||
'description' => (string)trans('firefly.rulegroup_for_bills_description', [], $lang->data),
|
|
||||||
'order' => $order,
|
|
||||||
'active' => 1,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// loop bills.
|
|
||||||
$order = 1;
|
|
||||||
$count = 0;
|
|
||||||
/** @var Collection $collection */
|
|
||||||
$collection = $user->bills()->get();
|
|
||||||
/** @var Bill $bill */
|
|
||||||
foreach ($collection as $bill) {
|
|
||||||
if ('MIGRATED_TO_RULES' !== $bill->match) {
|
|
||||||
$count++;
|
|
||||||
$rule = Rule::create(
|
|
||||||
[
|
|
||||||
'user_id' => $user->id,
|
|
||||||
'rule_group_id' => $ruleGroup->id,
|
|
||||||
'title' => (string)trans('firefly.rule_for_bill_title', ['name' => $bill->name], $lang->data),
|
|
||||||
'description' => (string)trans('firefly.rule_for_bill_description', ['name' => $bill->name], $lang->data),
|
|
||||||
'order' => $order,
|
|
||||||
'active' => $bill->active,
|
|
||||||
'stop_processing' => 1,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
// add default trigger
|
|
||||||
RuleTrigger::create(
|
|
||||||
[
|
|
||||||
'rule_id' => $rule->id,
|
|
||||||
'trigger_type' => 'user_action',
|
|
||||||
'trigger_value' => 'store-journal',
|
|
||||||
'active' => 1,
|
|
||||||
'stop_processing' => 0,
|
|
||||||
'order' => 1,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
// add trigger for description
|
|
||||||
$match = implode(' ', explode(',', $bill->match));
|
|
||||||
RuleTrigger::create(
|
|
||||||
[
|
|
||||||
'rule_id' => $rule->id,
|
|
||||||
'trigger_type' => 'description_contains',
|
|
||||||
'trigger_value' => $match,
|
|
||||||
'active' => 1,
|
|
||||||
'stop_processing' => 0,
|
|
||||||
'order' => 2,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
if ($bill->amount_max !== $bill->amount_min) {
|
|
||||||
// add triggers for amounts:
|
|
||||||
RuleTrigger::create(
|
|
||||||
[
|
|
||||||
'rule_id' => $rule->id,
|
|
||||||
'trigger_type' => 'amount_less',
|
|
||||||
'trigger_value' => round($bill->amount_max, $currency->decimal_places),
|
|
||||||
'active' => 1,
|
|
||||||
'stop_processing' => 0,
|
|
||||||
'order' => 3,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
RuleTrigger::create(
|
|
||||||
[
|
|
||||||
'rule_id' => $rule->id,
|
|
||||||
'trigger_type' => 'amount_more',
|
|
||||||
'trigger_value' => round((float)$bill->amount_min, $currency->decimal_places),
|
|
||||||
'active' => 1,
|
|
||||||
'stop_processing' => 0,
|
|
||||||
'order' => 4,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if ($bill->amount_max === $bill->amount_min) {
|
|
||||||
RuleTrigger::create(
|
|
||||||
[
|
|
||||||
'rule_id' => $rule->id,
|
|
||||||
'trigger_type' => 'amount_exactly',
|
|
||||||
'trigger_value' => round((float)$bill->amount_min, $currency->decimal_places),
|
|
||||||
'active' => 1,
|
|
||||||
'stop_processing' => 0,
|
|
||||||
'order' => 3,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// create action
|
|
||||||
RuleAction::create(
|
|
||||||
[
|
|
||||||
'rule_id' => $rule->id,
|
|
||||||
'action_type' => 'link_to_bill',
|
|
||||||
'action_value' => $bill->name,
|
|
||||||
'order' => 1,
|
|
||||||
'active' => 1,
|
|
||||||
'stop_processing' => 0,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
$order++;
|
|
||||||
$bill->match = 'MIGRATED_TO_RULES';
|
|
||||||
$bill->save();
|
|
||||||
$this->line(sprintf('Updated bill #%d ("%s") so it will use rules.', $bill->id, $bill->name));
|
|
||||||
}
|
|
||||||
|
|
||||||
// give bills a currency when they dont have one.
|
|
||||||
if (null === $bill->transaction_currency_id) {
|
|
||||||
$this->line(sprintf('Gave bill #%d ("%s") a currency (%s).', $bill->id, $bill->name, $currency->name));
|
|
||||||
$bill->transactionCurrency()->associate($currency);
|
|
||||||
$bill->save();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($count > 0) {
|
|
||||||
$this->info(sprintf('Migrated %d bills for user %s', $count, $user->email));
|
|
||||||
}
|
|
||||||
if (0 === $count) {
|
|
||||||
$this->info(sprintf('Bills are correct for user %s.', $user->email));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (0 === $this->count) {
|
||||||
|
$this->line('All bills are OK.');
|
||||||
|
}
|
||||||
|
if (0 !== $this->count) {
|
||||||
|
$this->line(sprintf('Verified and fixed %d bill(s).', $this->count));
|
||||||
|
}
|
||||||
|
|
||||||
$end = round(microtime(true) - $start, 2);
|
$end = round(microtime(true) - $start, 2);
|
||||||
$this->info(sprintf('Verified and fixed bills in %s seconds.', $end));
|
$this->info(sprintf('Verified and fixed bills in %s seconds.', $end));
|
||||||
$this->markAsExecuted();
|
$this->markAsExecuted();
|
||||||
@ -251,18 +137,107 @@ class MigrateToRules extends Command
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $value
|
* Migrate bills to new rule structure for a specific user.
|
||||||
*
|
*
|
||||||
* @return string
|
* @param User $user
|
||||||
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
private function tryDecrypt(string $value): string
|
private function migrateUser(User $user): void
|
||||||
{
|
{
|
||||||
try {
|
$this->ruleGroupRepository->setUser($user);
|
||||||
$value = Crypt::decrypt($value);
|
$this->billRepository->setUser($user);
|
||||||
} catch (DecryptException $e) {
|
$this->ruleRepository->setUser($user);
|
||||||
Log::debug(sprintf('Could not decrypt. %s', $e->getMessage()));
|
|
||||||
|
/** @var Preference $lang */
|
||||||
|
$lang = app('preferences')->getForUser($user, 'language', 'en_US');
|
||||||
|
$groupTitle = (string)trans('firefly.rulegroup_for_bills_title', [], $lang->data);
|
||||||
|
$ruleGroup = $this->ruleGroupRepository->findByTitle($groupTitle);
|
||||||
|
//$currency = $this->getCurrency($user);
|
||||||
|
|
||||||
|
if (null === $ruleGroup) {
|
||||||
|
$ruleGroup = $this->ruleGroupRepository->store(
|
||||||
|
[
|
||||||
|
'title' => (string)trans('firefly.rulegroup_for_bills_title', [], $lang->data),
|
||||||
|
'description' => (string)trans('firefly.rulegroup_for_bills_description', [], $lang->data),
|
||||||
|
'active' => true,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$bills = $this->billRepository->getBills();
|
||||||
|
|
||||||
|
/** @var Bill $bill */
|
||||||
|
foreach ($bills as $bill) {
|
||||||
|
$this->migrateBill($ruleGroup, $bill, $lang);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $value;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param RuleGroup $ruleGroup
|
||||||
|
* @param Bill $bill
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
private function migrateBill(RuleGroup $ruleGroup, Bill $bill, Preference $language): void
|
||||||
|
{
|
||||||
|
if ('MIGRATED_TO_RULES' === $bill->match) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get match thing:
|
||||||
|
$match = implode(' ', explode(',', $bill->match));
|
||||||
|
$newRule = [
|
||||||
|
'rule_group_id' => $ruleGroup->id,
|
||||||
|
'active' => true,
|
||||||
|
'strict' => false,
|
||||||
|
'stop_processing' => false, // field is no longer used.
|
||||||
|
'title' => (string)trans('firefly.rule_for_bill_title', ['name' => $bill->name], $language->data),
|
||||||
|
'description' => (string)trans('firefly.rule_for_bill_description', ['name' => $bill->name], $language->data),
|
||||||
|
'trigger' => 'store-journal',
|
||||||
|
'triggers' => [
|
||||||
|
[
|
||||||
|
'type' => 'description_contains',
|
||||||
|
'value' => $match,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'actions' => [
|
||||||
|
'type' => 'link_to_bill',
|
||||||
|
'value' => $bill->name,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
// two triggers or one, depends on bill content:
|
||||||
|
if ($bill->amount_max === $bill->amount_min) {
|
||||||
|
$newRule['triggers'][] = [
|
||||||
|
'type' => 'amount_exactly',
|
||||||
|
'value' => $bill->amount_min,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
if ($bill->amount_max !== $bill->amount_min) {
|
||||||
|
$newRule['triggers'][] = [
|
||||||
|
'type' => 'amount_less',
|
||||||
|
'value' => $bill->amount_max,
|
||||||
|
];
|
||||||
|
$newRule['triggers'][] = [
|
||||||
|
'type' => 'amount_more',
|
||||||
|
'value' => $bill->amount_min,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->ruleRepository->store($newRule);
|
||||||
|
|
||||||
|
// update bill:
|
||||||
|
$newBillData = [
|
||||||
|
'currency_id' => $bill->transaction_currency_id,
|
||||||
|
'name' => $bill->name,
|
||||||
|
'match' => 'MIGRATED_TO_RULES',
|
||||||
|
'amount_min' => $bill->amount_min,
|
||||||
|
'amount_max' => $bill->amount_max,
|
||||||
|
'date' => $bill->date,
|
||||||
|
'repeat_freq' => $bill->repeat_freq,
|
||||||
|
'skip' => $bill->skip,
|
||||||
|
'active' => $bill->active,
|
||||||
|
];
|
||||||
|
$this->billRepository->update($bill, $newBillData);
|
||||||
|
$this->count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -84,7 +84,7 @@ class TransactionJournalMeta extends Model
|
|||||||
*/
|
*/
|
||||||
public function getDataAttribute($value)
|
public function getDataAttribute($value)
|
||||||
{
|
{
|
||||||
return json_decode($value);
|
return json_decode($value, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -343,4 +343,14 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
|
|||||||
|
|
||||||
return $ruleGroup;
|
return $ruleGroup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $title
|
||||||
|
*
|
||||||
|
* @return RuleGroup|null
|
||||||
|
*/
|
||||||
|
public function findByTitle(string $title): ?RuleGroup
|
||||||
|
{
|
||||||
|
return $this->user->ruleGroups()->where('title', $title)->first();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ interface RuleGroupRepositoryInterface
|
|||||||
public function count(): int;
|
public function count(): int;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param RuleGroup $ruleGroup
|
* @param RuleGroup $ruleGroup
|
||||||
* @param RuleGroup|null $moveTo
|
* @param RuleGroup|null $moveTo
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
@ -51,6 +51,13 @@ interface RuleGroupRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function find(int $ruleGroupId): ?RuleGroup;
|
public function find(int $ruleGroupId): ?RuleGroup;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $title
|
||||||
|
*
|
||||||
|
* @return RuleGroup|null
|
||||||
|
*/
|
||||||
|
public function findByTitle(string $title): ?RuleGroup;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all rule groups.
|
* Get all rule groups.
|
||||||
*
|
*
|
||||||
@ -143,7 +150,7 @@ interface RuleGroupRepositoryInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param RuleGroup $ruleGroup
|
* @param RuleGroup $ruleGroup
|
||||||
* @param array $data
|
* @param array $data
|
||||||
*
|
*
|
||||||
* @return RuleGroup
|
* @return RuleGroup
|
||||||
*/
|
*/
|
||||||
|
@ -48,7 +48,7 @@ class BillUpdateService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Bill $bill
|
* @param Bill $bill
|
||||||
* @param array $data
|
* @param array $data
|
||||||
*
|
*
|
||||||
* @return Bill
|
* @return Bill
|
||||||
@ -69,9 +69,9 @@ class BillUpdateService
|
|||||||
$currency->enabled = true;
|
$currency->enabled = true;
|
||||||
$currency->save();
|
$currency->save();
|
||||||
|
|
||||||
|
|
||||||
$oldName = $bill->name;
|
$oldName = $bill->name;
|
||||||
$bill->name = $data['name'];
|
$bill->name = $data['name'];
|
||||||
|
$bill->match = $data['match'] ?? $bill->match;
|
||||||
$bill->amount_min = $data['amount_min'];
|
$bill->amount_min = $data['amount_min'];
|
||||||
$bill->amount_max = $data['amount_max'];
|
$bill->amount_max = $data['amount_max'];
|
||||||
$bill->date = $data['date'];
|
$bill->date = $data['date'];
|
||||||
|
@ -49,306 +49,306 @@ class JournalCurrenciesTest extends TestCase
|
|||||||
parent::setUp();
|
parent::setUp();
|
||||||
Log::info(sprintf('Now in %s.', get_class($this)));
|
Log::info(sprintf('Now in %s.', get_class($this)));
|
||||||
}
|
}
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* Basic run. Would not change anything.
|
// * Basic run. Would not change anything.
|
||||||
*
|
// *
|
||||||
* @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
|
// * @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
|
||||||
*/
|
// */
|
||||||
public function testHandle(): void
|
// public function testHandle(): void
|
||||||
{
|
// {
|
||||||
// mock classes
|
// // mock classes
|
||||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
// $accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
// $currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
// $journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||||
$euro = TransactionCurrency::find(1);
|
// $euro = TransactionCurrency::find(1);
|
||||||
|
//
|
||||||
// update transfer if necessary for the test:
|
// // update transfer if necessary for the test:
|
||||||
$false = new Configuration;
|
// $false = new Configuration;
|
||||||
$false->data = false;
|
// $false->data = false;
|
||||||
FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
|
// FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
|
||||||
FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
|
// FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
|
||||||
|
//
|
||||||
// mock stuff
|
// // mock stuff
|
||||||
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
// $journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
||||||
->withArgs([[TransactionType::TRANSFER]])
|
// ->withArgs([[TransactionType::TRANSFER]])
|
||||||
->andReturn(new Collection);
|
// ->andReturn(new Collection);
|
||||||
|
//
|
||||||
// for the "other journals" check, return nothing.
|
// // for the "other journals" check, return nothing.
|
||||||
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
// $journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
||||||
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
|
// ->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
|
||||||
->andReturn(new Collection);
|
// ->andReturn(new Collection);
|
||||||
|
//
|
||||||
// transaction would be verified, nothing more.
|
// // transaction would be verified, nothing more.
|
||||||
$this->artisan('firefly-iii:journal-currencies')
|
// $this->artisan('firefly-iii:journal-currencies')
|
||||||
->expectsOutput('All transactions are correct.')
|
// ->expectsOutput('All transactions are correct.')
|
||||||
->assertExitCode(0);
|
// ->assertExitCode(0);
|
||||||
// nothing changed, so no verification.
|
// // nothing changed, so no verification.
|
||||||
|
//
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* Submit a single transfer which has no issues.
|
// * Submit a single transfer which has no issues.
|
||||||
*
|
// *
|
||||||
* @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
|
// * @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
|
||||||
*/
|
// */
|
||||||
public function testHandleTransfer(): void
|
// public function testHandleTransfer(): void
|
||||||
{
|
// {
|
||||||
// mock classes
|
// // mock classes
|
||||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
// $accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
// $currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
// $journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||||
$euro = TransactionCurrency::find(1);
|
// $euro = TransactionCurrency::find(1);
|
||||||
$transfer = $this->getRandomTransfer();
|
// $transfer = $this->getRandomTransfer();
|
||||||
|
//
|
||||||
// update transfer if necessary for the test:
|
// // update transfer if necessary for the test:
|
||||||
$collection = new Collection([$transfer]);
|
// $collection = new Collection([$transfer]);
|
||||||
$false = new Configuration;
|
// $false = new Configuration;
|
||||||
$false->data = false;
|
// $false->data = false;
|
||||||
FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
|
// FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
|
||||||
FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
|
// FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
|
||||||
|
//
|
||||||
// mock stuff
|
// // mock stuff
|
||||||
$accountRepos->shouldReceive('setUser')->atLeast()->once();
|
// $accountRepos->shouldReceive('setUser')->atLeast()->once();
|
||||||
$journalRepos->shouldReceive('setUser')->atLeast()->once();
|
// $journalRepos->shouldReceive('setUser')->atLeast()->once();
|
||||||
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
|
// $currencyRepos->shouldReceive('setUser')->atLeast()->once();
|
||||||
|
//
|
||||||
// return single tranfer
|
// // return single tranfer
|
||||||
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
// $journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
||||||
->withArgs([[TransactionType::TRANSFER]])
|
// ->withArgs([[TransactionType::TRANSFER]])
|
||||||
->andReturn($collection);
|
// ->andReturn($collection);
|
||||||
|
//
|
||||||
// for the "other journals" check, return nothing.
|
// // for the "other journals" check, return nothing.
|
||||||
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
// $journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
||||||
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
|
// ->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
|
||||||
->andReturn(new Collection);
|
// ->andReturn(new Collection);
|
||||||
|
//
|
||||||
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'currency_id'])->andReturn($euro->id);
|
// $accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'currency_id'])->andReturn($euro->id);
|
||||||
$currencyRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($euro);
|
// $currencyRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($euro);
|
||||||
|
//
|
||||||
// transaction would be verified, nothing more.
|
// // transaction would be verified, nothing more.
|
||||||
$this->artisan('firefly-iii:journal-currencies')
|
// $this->artisan('firefly-iii:journal-currencies')
|
||||||
->expectsOutput('Verified 1 transaction(s) and journal(s).')
|
// ->expectsOutput('Verified 1 transaction(s) and journal(s).')
|
||||||
->assertExitCode(0);
|
// ->assertExitCode(0);
|
||||||
// nothing changed, so no verification.
|
// // nothing changed, so no verification.
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* Submit a single transfer where the source account has no currency preference.
|
// * Submit a single transfer where the source account has no currency preference.
|
||||||
*
|
// *
|
||||||
* @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
|
// * @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
|
||||||
*/
|
// */
|
||||||
public function testHandleTransferSourceNoPref(): void
|
// public function testHandleTransferSourceNoPref(): void
|
||||||
{
|
// {
|
||||||
// mock classes
|
// // mock classes
|
||||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
// $accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
// $currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
// $journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||||
$euro = TransactionCurrency::find(1);
|
// $euro = TransactionCurrency::find(1);
|
||||||
$transfer = $this->getRandomTransfer();
|
// $transfer = $this->getRandomTransfer();
|
||||||
|
//
|
||||||
// edit source to remove currency preference:
|
// // edit source to remove currency preference:
|
||||||
/** @var Account $source */
|
// /** @var Account $source */
|
||||||
$source = $transfer->transactions()->where('amount', '<', 0)->first()->account;
|
// $source = $transfer->transactions()->where('amount', '<', 0)->first()->account;
|
||||||
// AccountMeta::where('account_id', $source->id)->where('name', 'currency_id')->delete();
|
//// AccountMeta::where('account_id', $source->id)->where('name', 'currency_id')->delete();
|
||||||
|
//
|
||||||
// update transfer if necessary for the test:
|
// // update transfer if necessary for the test:
|
||||||
$collection = new Collection([$transfer]);
|
// $collection = new Collection([$transfer]);
|
||||||
$false = new Configuration;
|
// $false = new Configuration;
|
||||||
$false->data = false;
|
// $false->data = false;
|
||||||
FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
|
// FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
|
||||||
FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
|
// FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
|
||||||
|
//
|
||||||
// mock stuff
|
// // mock stuff
|
||||||
$accountRepos->shouldReceive('setUser')->atLeast()->once();
|
// $accountRepos->shouldReceive('setUser')->atLeast()->once();
|
||||||
$journalRepos->shouldReceive('setUser')->atLeast()->once();
|
// $journalRepos->shouldReceive('setUser')->atLeast()->once();
|
||||||
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
|
// $currencyRepos->shouldReceive('setUser')->atLeast()->once();
|
||||||
|
//
|
||||||
// return single transfer
|
// // return single transfer
|
||||||
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
// $journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
||||||
->withArgs([[TransactionType::TRANSFER]])
|
// ->withArgs([[TransactionType::TRANSFER]])
|
||||||
->andReturn($collection);
|
// ->andReturn($collection);
|
||||||
|
//
|
||||||
// for the "other journals" check, return nothing.
|
// // for the "other journals" check, return nothing.
|
||||||
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
// $journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
||||||
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
|
// ->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
|
||||||
->andReturn(new Collection);
|
// ->andReturn(new Collection);
|
||||||
|
//
|
||||||
// return NULL for first currency ID and currency.
|
// // return NULL for first currency ID and currency.
|
||||||
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'currency_id'])->andReturnNull();
|
// $accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'currency_id'])->andReturnNull();
|
||||||
$currencyRepos->shouldReceive('findNull')->atLeast()->once()->andReturnNull();
|
// $currencyRepos->shouldReceive('findNull')->atLeast()->once()->andReturnNull();
|
||||||
|
//
|
||||||
// transaction would be verified, nothing more.
|
// // transaction would be verified, nothing more.
|
||||||
$this->artisan('firefly-iii:journal-currencies')
|
// $this->artisan('firefly-iii:journal-currencies')
|
||||||
->expectsOutput(sprintf('Account #%d ("%s") must have currency preference but has none.', $source->id, $source->name))
|
// ->expectsOutput(sprintf('Account #%d ("%s") must have currency preference but has none.', $source->id, $source->name))
|
||||||
->assertExitCode(0);
|
// ->assertExitCode(0);
|
||||||
// nothing changed, so no verification.
|
// // nothing changed, so no verification.
|
||||||
|
//
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* Submit a single transfer where the source transaction has no currency set.
|
// * Submit a single transfer where the source transaction has no currency set.
|
||||||
* Because this is not done over repositories, we must edit the DB.
|
// * Because this is not done over repositories, we must edit the DB.
|
||||||
*
|
// *
|
||||||
* @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
|
// * @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
|
||||||
*/
|
// */
|
||||||
public function testHandleTransferSourceNoCurrency(): void
|
// public function testHandleTransferSourceNoCurrency(): void
|
||||||
{
|
// {
|
||||||
// mock classes
|
// // mock classes
|
||||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
// $accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
// $currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
// $journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||||
$euro = TransactionCurrency::find(1);
|
// $euro = TransactionCurrency::find(1);
|
||||||
$transfer = $this->getRandomTransfer();
|
// $transfer = $this->getRandomTransfer();
|
||||||
/** @var Transaction $source */
|
// /** @var Transaction $source */
|
||||||
$source = $transfer->transactions()->where('amount', '<', 0)->first();
|
// $source = $transfer->transactions()->where('amount', '<', 0)->first();
|
||||||
$source->transaction_currency_id = null;
|
// $source->transaction_currency_id = null;
|
||||||
$source->save();
|
// $source->save();
|
||||||
|
//
|
||||||
// update transfer if necessary for the test:
|
// // update transfer if necessary for the test:
|
||||||
$collection = new Collection([$transfer]);
|
// $collection = new Collection([$transfer]);
|
||||||
$false = new Configuration;
|
// $false = new Configuration;
|
||||||
$false->data = false;
|
// $false->data = false;
|
||||||
FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
|
// FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
|
||||||
FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
|
// FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
|
||||||
|
//
|
||||||
// mock stuff
|
// // mock stuff
|
||||||
$accountRepos->shouldReceive('setUser')->atLeast()->once();
|
// $accountRepos->shouldReceive('setUser')->atLeast()->once();
|
||||||
$journalRepos->shouldReceive('setUser')->atLeast()->once();
|
// $journalRepos->shouldReceive('setUser')->atLeast()->once();
|
||||||
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
|
// $currencyRepos->shouldReceive('setUser')->atLeast()->once();
|
||||||
|
//
|
||||||
// return single tranfer
|
// // return single tranfer
|
||||||
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
// $journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
||||||
->withArgs([[TransactionType::TRANSFER]])
|
// ->withArgs([[TransactionType::TRANSFER]])
|
||||||
->andReturn($collection);
|
// ->andReturn($collection);
|
||||||
|
//
|
||||||
// for the "other journals" check, return nothing.
|
// // for the "other journals" check, return nothing.
|
||||||
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
// $journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
||||||
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
|
// ->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
|
||||||
->andReturn(new Collection);
|
// ->andReturn(new Collection);
|
||||||
|
//
|
||||||
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'currency_id'])->andReturn($euro->id);
|
// $accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'currency_id'])->andReturn($euro->id);
|
||||||
$currencyRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($euro);
|
// $currencyRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($euro);
|
||||||
|
//
|
||||||
// transaction would be verified, nothing more.
|
// // transaction would be verified, nothing more.
|
||||||
$this->artisan('firefly-iii:journal-currencies')
|
// $this->artisan('firefly-iii:journal-currencies')
|
||||||
->expectsOutput(sprintf('Transaction #%d has no currency setting, now set to %s.', $source->id, $euro->code))
|
// ->expectsOutput(sprintf('Transaction #%d has no currency setting, now set to %s.', $source->id, $euro->code))
|
||||||
->expectsOutput('Verified 2 transaction(s) and journal(s).')
|
// ->expectsOutput('Verified 2 transaction(s) and journal(s).')
|
||||||
->assertExitCode(0);
|
// ->assertExitCode(0);
|
||||||
|
//
|
||||||
// check transaction
|
// // check transaction
|
||||||
$this->assertCount(1, Transaction::where('id', $source->id)->where('transaction_currency_id', $euro->id)->get());
|
// $this->assertCount(1, Transaction::where('id', $source->id)->where('transaction_currency_id', $euro->id)->get());
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* Submit a single transfer where the source transaction has a different currency than the source account does.
|
// * Submit a single transfer where the source transaction has a different currency than the source account does.
|
||||||
*
|
// *
|
||||||
* @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
|
// * @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
|
||||||
*/
|
// */
|
||||||
public function testHandleMismatchedTransfer(): void
|
// public function testHandleMismatchedTransfer(): void
|
||||||
{
|
// {
|
||||||
// mock classes
|
// // mock classes
|
||||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
// $accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
// $currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
// $journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||||
$euro = TransactionCurrency::find(1);
|
// $euro = TransactionCurrency::find(1);
|
||||||
$usd = TransactionCurrency::where('code', 'USD')->first();
|
// $usd = TransactionCurrency::where('code', 'USD')->first();
|
||||||
$transfer = $this->getRandomTransfer();
|
// $transfer = $this->getRandomTransfer();
|
||||||
|
//
|
||||||
/** @var Transaction $source */
|
// /** @var Transaction $source */
|
||||||
$source = $transfer->transactions()->where('amount', '<', 0)->first();
|
// $source = $transfer->transactions()->where('amount', '<', 0)->first();
|
||||||
$source->transaction_currency_id = $usd->id;
|
// $source->transaction_currency_id = $usd->id;
|
||||||
$source->save();
|
// $source->save();
|
||||||
|
//
|
||||||
// update transfer if necessary for the test:
|
// // update transfer if necessary for the test:
|
||||||
$collection = new Collection([$transfer]);
|
// $collection = new Collection([$transfer]);
|
||||||
$false = new Configuration;
|
// $false = new Configuration;
|
||||||
$false->data = false;
|
// $false->data = false;
|
||||||
FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
|
// FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
|
||||||
FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
|
// FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
|
||||||
|
//
|
||||||
// mock stuff
|
// // mock stuff
|
||||||
$accountRepos->shouldReceive('setUser')->atLeast()->once();
|
// $accountRepos->shouldReceive('setUser')->atLeast()->once();
|
||||||
$journalRepos->shouldReceive('setUser')->atLeast()->once();
|
// $journalRepos->shouldReceive('setUser')->atLeast()->once();
|
||||||
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
|
// $currencyRepos->shouldReceive('setUser')->atLeast()->once();
|
||||||
|
//
|
||||||
// return single tranfer
|
// // return single tranfer
|
||||||
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
// $journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
||||||
->withArgs([[TransactionType::TRANSFER]])
|
// ->withArgs([[TransactionType::TRANSFER]])
|
||||||
->andReturn($collection);
|
// ->andReturn($collection);
|
||||||
|
//
|
||||||
// for the "other journals" check, return nothing.
|
// // for the "other journals" check, return nothing.
|
||||||
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
// $journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
||||||
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
|
// ->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
|
||||||
->andReturn(new Collection);
|
// ->andReturn(new Collection);
|
||||||
|
//
|
||||||
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'currency_id'])->andReturn($euro->id);
|
// $accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'currency_id'])->andReturn($euro->id);
|
||||||
$currencyRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($euro);
|
// $currencyRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($euro);
|
||||||
|
//
|
||||||
// transaction would be verified, nothing more.
|
// // transaction would be verified, nothing more.
|
||||||
$this->artisan('firefly-iii:journal-currencies')
|
// $this->artisan('firefly-iii:journal-currencies')
|
||||||
->expectsOutput(
|
// ->expectsOutput(
|
||||||
sprintf(
|
// sprintf(
|
||||||
'Transaction #%d has a currency setting #%d that should be #%d. Amount remains %s, currency is changed.',
|
// 'Transaction #%d has a currency setting #%d that should be #%d. Amount remains %s, currency is changed.',
|
||||||
$source->id,
|
// $source->id,
|
||||||
$source->transaction_currency_id,
|
// $source->transaction_currency_id,
|
||||||
$euro->id,
|
// $euro->id,
|
||||||
$source->amount
|
// $source->amount
|
||||||
)
|
// )
|
||||||
)
|
// )
|
||||||
->expectsOutput('Verified 2 transaction(s) and journal(s).')
|
// ->expectsOutput('Verified 2 transaction(s) and journal(s).')
|
||||||
->assertExitCode(0);
|
// ->assertExitCode(0);
|
||||||
// nothing changed, so no verification.
|
// // nothing changed, so no verification.
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* Submit a single transfer where the destination account has no currency preference.
|
// * Submit a single transfer where the destination account has no currency preference.
|
||||||
*
|
// *
|
||||||
* @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
|
// * @covers \FireflyIII\Console\Commands\Upgrade\JournalCurrencies
|
||||||
*/
|
// */
|
||||||
public function testHandleTransferNoDestinationCurrency(): void
|
// public function testHandleTransferNoDestinationCurrency(): void
|
||||||
{
|
// {
|
||||||
// mock classes
|
// // mock classes
|
||||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
// $accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
// $currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
// $journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||||
$euro = TransactionCurrency::find(1);
|
// $euro = TransactionCurrency::find(1);
|
||||||
$transfer = $this->getRandomTransfer();
|
// $transfer = $this->getRandomTransfer();
|
||||||
|
//
|
||||||
/** @var Account $destination */
|
// /** @var Account $destination */
|
||||||
$destination = $transfer->transactions()->where('amount', '>', 0)->first()->account;
|
// $destination = $transfer->transactions()->where('amount', '>', 0)->first()->account;
|
||||||
|
//
|
||||||
// update transfer if necessary for the test:
|
// // update transfer if necessary for the test:
|
||||||
$collection = new Collection([$transfer]);
|
// $collection = new Collection([$transfer]);
|
||||||
$false = new Configuration;
|
// $false = new Configuration;
|
||||||
$false->data = false;
|
// $false->data = false;
|
||||||
FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
|
// FireflyConfig::shouldReceive('get')->withArgs(['4780_journal_currencies', false])->andReturn($false);
|
||||||
FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
|
// FireflyConfig::shouldReceive('set')->withArgs(['4780_journal_currencies', true]);
|
||||||
|
//
|
||||||
// mock stuff
|
// // mock stuff
|
||||||
$accountRepos->shouldReceive('setUser')->atLeast()->once();
|
// $accountRepos->shouldReceive('setUser')->atLeast()->once();
|
||||||
$journalRepos->shouldReceive('setUser')->atLeast()->once();
|
// $journalRepos->shouldReceive('setUser')->atLeast()->once();
|
||||||
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
|
// $currencyRepos->shouldReceive('setUser')->atLeast()->once();
|
||||||
|
//
|
||||||
// return single tranfer
|
// // return single tranfer
|
||||||
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
// $journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
||||||
->withArgs([[TransactionType::TRANSFER]])
|
// ->withArgs([[TransactionType::TRANSFER]])
|
||||||
->andReturn($collection);
|
// ->andReturn($collection);
|
||||||
|
//
|
||||||
// for the "other journals" check, return nothing.
|
// // for the "other journals" check, return nothing.
|
||||||
$journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
// $journalRepos->shouldReceive('getAllJournals')->atLeast()->once()
|
||||||
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
|
// ->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]])
|
||||||
->andReturn(new Collection);
|
// ->andReturn(new Collection);
|
||||||
|
//
|
||||||
$accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'currency_id'])->andReturn($euro->id, 0);
|
// $accountRepos->shouldReceive('getMetaValue')->atLeast()->once()->withArgs([Mockery::any(), 'currency_id'])->andReturn($euro->id, 0);
|
||||||
$currencyRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($euro, null);
|
// $currencyRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($euro, null);
|
||||||
|
//
|
||||||
// transaction would be verified, nothing more.
|
// // transaction would be verified, nothing more.
|
||||||
$this->artisan('firefly-iii:journal-currencies')
|
// $this->artisan('firefly-iii:journal-currencies')
|
||||||
->expectsOutput(sprintf('Account #%d ("%s") must have currency preference but has none.', $destination->id, $destination->name))
|
// ->expectsOutput(sprintf('Account #%d ("%s") must have currency preference but has none.', $destination->id, $destination->name))
|
||||||
->assertExitCode(0);
|
// ->assertExitCode(0);
|
||||||
// nothing changed, so no verification.
|
// // nothing changed, so no verification.
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -0,0 +1,96 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* MigrateAttachmentsTest.php
|
||||||
|
* Copyright (c) 2019 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Tests\Unit\Console\Commands\Upgrade;
|
||||||
|
|
||||||
|
|
||||||
|
use FireflyConfig;
|
||||||
|
use FireflyIII\Models\Attachment;
|
||||||
|
use FireflyIII\Models\Configuration;
|
||||||
|
use FireflyIII\Models\Note;
|
||||||
|
use FireflyIII\Models\TransactionJournal;
|
||||||
|
use Log;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class MigrateAttachmentsTest
|
||||||
|
*/
|
||||||
|
class MigrateAttachmentsTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
Log::info(sprintf('Now in %s.', get_class($this)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateAttachments
|
||||||
|
*/
|
||||||
|
public function testHandle(): void
|
||||||
|
{
|
||||||
|
$false = new Configuration;
|
||||||
|
$false->data = false;
|
||||||
|
FireflyConfig::shouldReceive('get')->withArgs(['4780_migrate_attachments', false])->andReturn($false);
|
||||||
|
FireflyConfig::shouldReceive('set')->withArgs(['4780_migrate_attachments', true]);
|
||||||
|
// assume all is well.
|
||||||
|
$this->artisan('firefly-iii:migrate-attachments')
|
||||||
|
->expectsOutput('All attachments are OK.')
|
||||||
|
->assertExitCode(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateAttachments
|
||||||
|
*/
|
||||||
|
public function testHandleMigrate(): void
|
||||||
|
{
|
||||||
|
$false = new Configuration;
|
||||||
|
$false->data = false;
|
||||||
|
FireflyConfig::shouldReceive('get')->withArgs(['4780_migrate_attachments', false])->andReturn($false);
|
||||||
|
FireflyConfig::shouldReceive('set')->withArgs(['4780_migrate_attachments', true]);
|
||||||
|
|
||||||
|
$attachment = Attachment::create(
|
||||||
|
[
|
||||||
|
'user_id' => 1,
|
||||||
|
'attachable_id' => 1,
|
||||||
|
'attachable_type' => TransactionJournal::class,
|
||||||
|
'description' => 'Hello',
|
||||||
|
'md5' => md5('hello'),
|
||||||
|
'filename' => 'test.pdf',
|
||||||
|
'mime' => 'text/plain',
|
||||||
|
'size' => 1,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// assume all is well.
|
||||||
|
$this->artisan('firefly-iii:migrate-attachments')
|
||||||
|
->expectsOutput('Updated 1 attachment(s).')
|
||||||
|
->assertExitCode(0);
|
||||||
|
|
||||||
|
$this->assertCount(0, Attachment::where('id', $attachment->id)->where('description', '!=', '')->get());
|
||||||
|
$this->assertCount(1, Attachment::where('id', $attachment->id)->where('description', '=', '')->get());
|
||||||
|
$this->assertCount(1, Note::where('noteable_id', $attachment->id)->where('noteable_type', Attachment::class)->get());
|
||||||
|
|
||||||
|
$attachment->forceDelete();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,94 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* MigrateJournalNotesTest.php
|
||||||
|
* Copyright (c) 2019 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Tests\Unit\Console\Commands\Upgrade;
|
||||||
|
|
||||||
|
|
||||||
|
use FireflyConfig;
|
||||||
|
use FireflyIII\Models\Configuration;
|
||||||
|
use FireflyIII\Models\TransactionJournalMeta;
|
||||||
|
use Log;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class MigrateJournalNotesTest
|
||||||
|
*/
|
||||||
|
class MigrateJournalNotesTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
Log::info(sprintf('Now in %s.', get_class($this)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateJournalNotes
|
||||||
|
*/
|
||||||
|
public function testHandle(): void
|
||||||
|
{
|
||||||
|
$false = new Configuration;
|
||||||
|
$false->data = false;
|
||||||
|
FireflyConfig::shouldReceive('get')->withArgs(['4780_migrate_notes', false])->andReturn($false);
|
||||||
|
FireflyConfig::shouldReceive('set')->withArgs(['4780_migrate_notes', true]);
|
||||||
|
|
||||||
|
// assume all is well.
|
||||||
|
$this->artisan('firefly-iii:migrate-notes')
|
||||||
|
->expectsOutput('No notes to migrate.')
|
||||||
|
->assertExitCode(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateJournalNotes
|
||||||
|
*/
|
||||||
|
public function testHandleNote(): void
|
||||||
|
{
|
||||||
|
$false = new Configuration;
|
||||||
|
$false->data = false;
|
||||||
|
FireflyConfig::shouldReceive('get')->withArgs(['4780_migrate_notes', false])->andReturn($false);
|
||||||
|
FireflyConfig::shouldReceive('set')->withArgs(['4780_migrate_notes', true]);
|
||||||
|
|
||||||
|
$journal = $this->getRandomWithdrawal();
|
||||||
|
$meta = TransactionJournalMeta::create(
|
||||||
|
[
|
||||||
|
'transaction_journal_id' => $journal->id,
|
||||||
|
'name' => 'notes',
|
||||||
|
'data' => json_encode('Some note.'),
|
||||||
|
'hash' => 'Some hash',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
// assume one is fixed.
|
||||||
|
$this->artisan('firefly-iii:migrate-notes')
|
||||||
|
->expectsOutput('Migrated 1 note(s).')
|
||||||
|
->assertExitCode(0);
|
||||||
|
|
||||||
|
$this->assertCount(0, TransactionJournalMeta
|
||||||
|
::where('name', 'notes')
|
||||||
|
->where('id', $meta->id)
|
||||||
|
->whereNull('deleted_at')
|
||||||
|
->get());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -23,12 +23,13 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Tests\Unit\Console\Commands\Upgrade;
|
namespace Tests\Unit\Console\Commands\Upgrade;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
|
||||||
use FireflyConfig;
|
use FireflyConfig;
|
||||||
use FireflyIII\Factory\TransactionJournalFactory;
|
use FireflyIII\Factory\TransactionGroupFactory;
|
||||||
use FireflyIII\Models\Configuration;
|
use FireflyIII\Models\Configuration;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||||
|
use FireflyIII\Services\Internal\Destroy\JournalDestroyService;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Log;
|
use Log;
|
||||||
use Mockery;
|
use Mockery;
|
||||||
@ -49,140 +50,199 @@ class MigrateToGroupsTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Basic test. Assume nothing is wrong.
|
||||||
|
*
|
||||||
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateToGroups
|
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateToGroups
|
||||||
*/
|
*/
|
||||||
public function testAlreadyExecuted(): void
|
public function testHandle(): void
|
||||||
{
|
{
|
||||||
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||||
|
|
||||||
return;
|
// mock calls:
|
||||||
$this->mock(TransactionJournalFactory::class);
|
$journalRepos->shouldReceive('getSplitJournals')
|
||||||
$this->mock(JournalRepositoryInterface::class);
|
->atLeast()->once()
|
||||||
|
->andReturn(new Collection);
|
||||||
|
$journalRepos->shouldReceive('getJournalsWithoutGroup')
|
||||||
|
->atLeast()->once()
|
||||||
|
->andReturn([]);
|
||||||
|
|
||||||
$configObject = new Configuration;
|
$false = new Configuration;
|
||||||
$configObject->data = true;
|
$false->data = false;
|
||||||
FireflyConfig::shouldReceive('get')->withArgs(['migrated_to_groups_4780', false])->andReturn($configObject)->once();
|
FireflyConfig::shouldReceive('get')->withArgs(['4780_migrated_to_groups', false])->andReturn($false);
|
||||||
|
FireflyConfig::shouldReceive('set')->withArgs(['4780_migrated_to_groups', true]);
|
||||||
|
|
||||||
|
// assume all is well.
|
||||||
$this->artisan('firefly-iii:migrate-to-groups')
|
$this->artisan('firefly-iii:migrate-to-groups')
|
||||||
->expectsOutput('Database already seems to be migrated.')
|
->expectsOutput('No journals to migrate to groups.')
|
||||||
->assertExitCode(0);
|
->assertExitCode(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Return a journal without a group.
|
||||||
|
*
|
||||||
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateToGroups
|
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateToGroups
|
||||||
*/
|
*/
|
||||||
public function testBasic(): void
|
public function testHandleNoGroup(): void
|
||||||
{
|
{
|
||||||
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||||
|
$asset = $this->getRandomAsset();
|
||||||
|
$expense = $this->getRandomExpense();
|
||||||
|
$journal = TransactionJournal::create(
|
||||||
|
[
|
||||||
|
'user_id' => 1,
|
||||||
|
'transaction_currency_id' => 1,
|
||||||
|
'transaction_type_id' => 1,
|
||||||
|
'description' => 'Test',
|
||||||
|
'tag_count' => 0,
|
||||||
|
'date' => '2019-01-01',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$one = Transaction::create(
|
||||||
|
[
|
||||||
|
'transaction_journal_id' => $journal->id,
|
||||||
|
'account_id' => $asset->id,
|
||||||
|
'amount' => '-10',
|
||||||
|
'identifier' => 1,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$two = Transaction::create(
|
||||||
|
[
|
||||||
|
'transaction_journal_id' => $journal->id,
|
||||||
|
'account_id' => $expense->id,
|
||||||
|
'amount' => '10',
|
||||||
|
'identifier' => 1,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$array = $journal->toArray();
|
||||||
|
|
||||||
return;
|
// mock calls:
|
||||||
$journalFactory = $this->mock(TransactionJournalFactory::class);
|
$journalRepos->shouldReceive('getSplitJournals')
|
||||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
->atLeast()->once()
|
||||||
$withdrawal = $this->getRandomSplitWithdrawal();
|
->andReturn(new Collection);
|
||||||
$collection = new Collection([$withdrawal]);
|
$journalRepos->shouldReceive('getJournalsWithoutGroup')
|
||||||
$date = new Carbon;
|
->atLeast()->once()
|
||||||
$opposing = new Transaction;
|
->andReturn([$array]);
|
||||||
$opposing->account_id = 13;
|
|
||||||
|
|
||||||
// not yet executed:
|
$false = new Configuration;
|
||||||
$configObject = new Configuration;
|
$false->data = false;
|
||||||
$configObject->data = false;
|
FireflyConfig::shouldReceive('get')->withArgs(['4780_migrated_to_groups', false])->andReturn($false);
|
||||||
FireflyConfig::shouldReceive('get')->withArgs(['migrated_to_groups_4780', false])->andReturn($configObject)->once();
|
FireflyConfig::shouldReceive('set')->withArgs(['4780_migrated_to_groups', true]);
|
||||||
FireflyConfig::shouldReceive('set')->withArgs(['migrated_to_groups_4780', true])->once();
|
|
||||||
|
// assume all is well.
|
||||||
|
$this->artisan('firefly-iii:migrate-to-groups')
|
||||||
|
->expectsOutput('Migrated 1 transaction journal(s).')
|
||||||
|
->assertExitCode(0);
|
||||||
|
|
||||||
|
// no longer without a group.
|
||||||
|
$this->assertCount(0, TransactionJournal::where('id', $journal->id)->whereNull('transaction_group_id')->get());
|
||||||
|
$journal->transactionGroup()->forceDelete();
|
||||||
|
$one->forceDelete();
|
||||||
|
$two->forceDelete();
|
||||||
|
$journal->forceDelete();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create split withdrawal and see what the system will do.
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateToGroups
|
||||||
|
*/
|
||||||
|
public function testHandleSplitJournal(): void
|
||||||
|
{
|
||||||
|
$asset = $this->getRandomAsset();
|
||||||
|
$expense = $this->getRandomExpense();
|
||||||
|
$group = $this->getRandomWithdrawalGroup();
|
||||||
|
$journal = TransactionJournal::create(
|
||||||
|
[
|
||||||
|
'user_id' => 1,
|
||||||
|
'transaction_currency_id' => 1,
|
||||||
|
'transaction_type_id' => 1,
|
||||||
|
'description' => 'Test',
|
||||||
|
'tag_count' => 0,
|
||||||
|
'date' => '2019-01-01',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$one = Transaction::create(
|
||||||
|
[
|
||||||
|
'transaction_journal_id' => $journal->id,
|
||||||
|
'account_id' => $asset->id,
|
||||||
|
'amount' => '-10',
|
||||||
|
'identifier' => 1,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$two = Transaction::create(
|
||||||
|
[
|
||||||
|
'transaction_journal_id' => $journal->id,
|
||||||
|
'account_id' => $expense->id,
|
||||||
|
'amount' => '10',
|
||||||
|
'identifier' => 1,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$three = Transaction::create(
|
||||||
|
[
|
||||||
|
'transaction_journal_id' => $journal->id,
|
||||||
|
'account_id' => $asset->id,
|
||||||
|
'amount' => '-12',
|
||||||
|
'identifier' => 2,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$four = Transaction::create(
|
||||||
|
[
|
||||||
|
'transaction_journal_id' => $journal->id,
|
||||||
|
'account_id' => $expense->id,
|
||||||
|
'amount' => '12',
|
||||||
|
'identifier' => 2,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
// calls to repository:
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||||
|
$service = $this->mock(JournalDestroyService::class);
|
||||||
|
$factory = $this->mock(TransactionGroupFactory::class);
|
||||||
|
|
||||||
|
// mock calls:
|
||||||
$journalRepos->shouldReceive('setUser')->atLeast()->once();
|
$journalRepos->shouldReceive('setUser')->atLeast()->once();
|
||||||
$journalRepos->shouldReceive('getJournalBudgetId')->atLeast()->once()->andReturn(1);
|
|
||||||
$journalRepos->shouldReceive('getJournalCategoryId')->atLeast()->once()->andReturn(2);
|
|
||||||
$journalRepos->shouldReceive('findOpposingTransaction')->atLeast()->once()->andReturn($opposing);
|
|
||||||
$journalRepos->shouldReceive('getNoteText')->atLeast()->once()->andReturn('I am some notes.');
|
|
||||||
$journalRepos->shouldReceive('getTags')->atLeast()->once()->andReturn(['a', 'b']);
|
|
||||||
$journalRepos->shouldReceive('getSplitJournals')->once()->andReturn($collection);
|
|
||||||
|
|
||||||
// all meta field calls.
|
// mock journal things:
|
||||||
$journalRepos->shouldReceive('getMetaField')->atLeast()->once()->withArgs([Mockery::any(), 'internal-reference'])->andReturn('ABC');
|
$journalRepos->shouldReceive('getJournalBudgetId')->atLeast()->once()->andReturn(0);
|
||||||
$journalRepos->shouldReceive('getMetaField')->atLeast()->once()->withArgs([Mockery::any(), 'sepa-cc'])->andReturnNull();
|
$journalRepos->shouldReceive('getJournalCategoryId')->atLeast()->once()->andReturn(0);
|
||||||
|
$journalRepos->shouldReceive('getNoteText')->atLeast()->once()->andReturn('Some note.');
|
||||||
|
$journalRepos->shouldReceive('getTags')->atLeast()->once()->andReturn(['A', 'B']);
|
||||||
|
$journalRepos->shouldReceive('getMetaField')->atLeast()
|
||||||
|
->withArgs([Mockery::any(), Mockery::any()])
|
||||||
|
->once()->andReturn(null);
|
||||||
|
$journalRepos->shouldReceive('getMetaDate')->atLeast()
|
||||||
|
->withArgs([Mockery::any(), Mockery::any()])
|
||||||
|
->once()->andReturn(null);
|
||||||
|
|
||||||
$journalRepos->shouldReceive('getMetaField')->atLeast()->once()->withArgs([Mockery::any(), 'sepa-ct-op'])->andReturnNull();
|
// create a group
|
||||||
$journalRepos->shouldReceive('getMetaField')->atLeast()->once()->withArgs([Mockery::any(), 'sepa-ct-id'])->andReturnNull();
|
$factory->shouldReceive('create')->atLeast()->once()->andReturn($group);
|
||||||
$journalRepos->shouldReceive('getMetaField')->atLeast()->once()->withArgs([Mockery::any(), 'sepa-db'])->andReturnNull();
|
$service->shouldReceive('destroy')->atLeast()->once();
|
||||||
$journalRepos->shouldReceive('getMetaField')->atLeast()->once()->withArgs([Mockery::any(), 'sepa-country'])->andReturnNull();
|
|
||||||
$journalRepos->shouldReceive('getMetaField')->atLeast()->once()->withArgs([Mockery::any(), 'sepa-ep'])->andReturnNull();
|
|
||||||
$journalRepos->shouldReceive('getMetaField')->atLeast()->once()->withArgs([Mockery::any(), 'sepa-ci'])->andReturnNull();
|
|
||||||
$journalRepos->shouldReceive('getMetaField')->atLeast()->once()->withArgs([Mockery::any(), 'sepa-batch-id'])->andReturnNull();
|
|
||||||
$journalRepos->shouldReceive('getMetaField')->atLeast()->once()->withArgs([Mockery::any(), 'external-id'])->andReturnNull();
|
|
||||||
$journalRepos->shouldReceive('getMetaField')->atLeast()->once()->withArgs([Mockery::any(), 'original-source'])->andReturnNull();
|
|
||||||
$journalRepos->shouldReceive('getMetaField')->atLeast()->once()->withArgs([Mockery::any(), 'recurrence_id'])->andReturnNull();
|
|
||||||
$journalRepos->shouldReceive('getMetaField')->atLeast()->once()->withArgs([Mockery::any(), 'bunq_payment_id'])->andReturnNull();
|
|
||||||
$journalRepos->shouldReceive('getMetaField')->atLeast()->once()->withArgs([Mockery::any(), 'importHash'])->andReturnNull();
|
|
||||||
$journalRepos->shouldReceive('getMetaField')->atLeast()->once()->withArgs([Mockery::any(), 'importHashV2'])->andReturnNull();
|
|
||||||
|
|
||||||
$journalRepos->shouldReceive('getMetaDate')->atLeast()->once()->withArgs([Mockery::any(), 'interest_date'])->andReturn($date);
|
$factory->shouldReceive('setUser')->atLeast()->once();
|
||||||
$journalRepos->shouldReceive('getMetaDate')->atLeast()->once()->withArgs([Mockery::any(), 'book_date'])->andReturnNull();
|
|
||||||
$journalRepos->shouldReceive('getMetaDate')->atLeast()->once()->withArgs([Mockery::any(), 'process_date'])->andReturnNull();
|
|
||||||
$journalRepos->shouldReceive('getMetaDate')->atLeast()->once()->withArgs([Mockery::any(), 'due_date'])->andReturnNull();
|
|
||||||
$journalRepos->shouldReceive('getMetaDate')->atLeast()->once()->withArgs([Mockery::any(), 'payment_date'])->andReturnNull();
|
|
||||||
$journalRepos->shouldReceive('getMetaDate')->atLeast()->once()->withArgs([Mockery::any(), 'invoice_date'])->andReturnNull();
|
|
||||||
|
|
||||||
// calls to factory
|
$journalRepos->shouldReceive('getSplitJournals')
|
||||||
$journalFactory->shouldReceive('setUser')->atLeast()->once();
|
->atLeast()->once()
|
||||||
$journalFactory->shouldReceive('create')->atLeast()->once()->withAnyArgs()->andReturn(new Collection());
|
->andReturn(new Collection([$journal]));
|
||||||
|
$journalRepos->shouldReceive('getJournalsWithoutGroup')
|
||||||
|
->atLeast()->once()
|
||||||
|
->andReturn([]);
|
||||||
|
|
||||||
|
$false = new Configuration;
|
||||||
|
$false->data = false;
|
||||||
|
FireflyConfig::shouldReceive('get')->withArgs(['4780_migrated_to_groups', false])->andReturn($false);
|
||||||
|
FireflyConfig::shouldReceive('set')->withArgs(['4780_migrated_to_groups', true]);
|
||||||
|
|
||||||
$this->artisan('firefly-iii:migrate-to-groups')
|
$this->artisan('firefly-iii:migrate-to-groups')
|
||||||
->expectsOutput('Going to un-split 1 transaction(s). This could take some time.')
|
->expectsOutput('Migrated 1 transaction journal(s).')
|
||||||
->assertExitCode(0);
|
->assertExitCode(0);
|
||||||
|
|
||||||
|
// delete the created stuff:
|
||||||
|
$one->forceDelete();
|
||||||
|
$two->forceDelete();
|
||||||
|
$three->forceDelete();
|
||||||
|
$four->forceDelete();
|
||||||
|
$journal->forceDelete();
|
||||||
|
|
||||||
|
// the calls above let me know it's OK.
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateToGroups
|
|
||||||
*/
|
|
||||||
public function testForced(): void
|
|
||||||
{
|
|
||||||
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
|
|
||||||
|
|
||||||
return;
|
|
||||||
$this->mock(TransactionJournalFactory::class);
|
|
||||||
$repository = $this->mock(JournalRepositoryInterface::class);
|
|
||||||
|
|
||||||
$repository->shouldReceive('getSplitJournals')->andReturn(new Collection);
|
|
||||||
|
|
||||||
|
|
||||||
$configObject = new Configuration;
|
|
||||||
$configObject->data = true;
|
|
||||||
FireflyConfig::shouldReceive('get')->withArgs(['migrated_to_groups_4780', false])->andReturn($configObject)->once();
|
|
||||||
FireflyConfig::shouldReceive('set')->withArgs(['migrated_to_groups_4780', true])->once();
|
|
||||||
|
|
||||||
$this->artisan('firefly-iii:migrate-to-groups --force')
|
|
||||||
->expectsOutput('Forcing the migration.')
|
|
||||||
->expectsOutput('Found no split journals. Nothing to do.')
|
|
||||||
->assertExitCode(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateToGroups
|
|
||||||
*/
|
|
||||||
public function testNotSplit(): void
|
|
||||||
{
|
|
||||||
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
|
|
||||||
|
|
||||||
return;
|
|
||||||
$this->mock(TransactionJournalFactory::class);
|
|
||||||
$repository = $this->mock(JournalRepositoryInterface::class);
|
|
||||||
$withdrawal = $this->getRandomWithdrawal();
|
|
||||||
|
|
||||||
$repository->shouldReceive('getSplitJournals')->andReturn(new Collection([$withdrawal]));
|
|
||||||
|
|
||||||
|
|
||||||
$configObject = new Configuration;
|
|
||||||
$configObject->data = false;
|
|
||||||
FireflyConfig::shouldReceive('get')->withArgs(['migrated_to_groups_4780', false])->andReturn($configObject)->once();
|
|
||||||
FireflyConfig::shouldReceive('set')->withArgs(['migrated_to_groups_4780', true])->once();
|
|
||||||
|
|
||||||
$this->artisan('firefly-iii:migrate-to-groups')
|
|
||||||
->expectsOutput('Going to un-split 1 transaction(s). This could take some time.')
|
|
||||||
->assertExitCode(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
316
tests/Unit/Console/Commands/Upgrade/MigrateToRulesTest.php
Normal file
316
tests/Unit/Console/Commands/Upgrade/MigrateToRulesTest.php
Normal file
@ -0,0 +1,316 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* MigrateToRulesTest.php
|
||||||
|
* Copyright (c) 2019 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Tests\Unit\Console\Commands\Upgrade;
|
||||||
|
|
||||||
|
|
||||||
|
use FireflyConfig;
|
||||||
|
use FireflyIII\Models\Bill;
|
||||||
|
use FireflyIII\Models\Configuration;
|
||||||
|
use FireflyIII\Models\Preference;
|
||||||
|
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
||||||
|
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
|
||||||
|
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
|
||||||
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Log;
|
||||||
|
use Mockery;
|
||||||
|
use Preferences;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class MigrateToRulesTest
|
||||||
|
*/
|
||||||
|
class MigrateToRulesTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
Log::info(sprintf('Now in %s.', get_class($this)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Basic test. Assume nothing is wrong.
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateToRules
|
||||||
|
*/
|
||||||
|
public function testHandle(): void
|
||||||
|
{
|
||||||
|
|
||||||
|
// mock repositories:
|
||||||
|
$userRepository = $this->mock(UserRepositoryInterface::class);
|
||||||
|
$ruleGroupRepository = $this->mock(RuleGroupRepositoryInterface::class);
|
||||||
|
$billRepository = $this->mock(BillRepositoryInterface::class);
|
||||||
|
$ruleRepository = $this->mock(RuleRepositoryInterface::class);
|
||||||
|
$group = $this->user()->ruleGroups()->inRandomOrder()->first();
|
||||||
|
$bill = $this->user()->bills()->inRandomOrder()->first();
|
||||||
|
// mock all calls.
|
||||||
|
$userRepository->shouldReceive('all')->atLeast()->once()
|
||||||
|
->andReturn(new Collection([$this->user()]));
|
||||||
|
$ruleRepository->shouldReceive('setUser')->atLeast()->once();
|
||||||
|
|
||||||
|
$ruleGroupRepository->shouldReceive('setUser')->atLeast()->once();
|
||||||
|
$ruleGroupRepository->shouldReceive('findByTitle')
|
||||||
|
->withArgs(['Rule group for bills'])->atLeast()->once()->andReturnNull();
|
||||||
|
// rule group repos should try to store a rule group in response to the result above.
|
||||||
|
$ruleGroupRepository->shouldReceive('store')->atLeast()->once()->andReturn($group);
|
||||||
|
|
||||||
|
// bill repos should return one rule.
|
||||||
|
$billRepository->shouldReceive('setUser')->atLeast()->once();
|
||||||
|
$billRepository->shouldReceive('getBills')->atLeast()->once()
|
||||||
|
->andReturn(new Collection([$bill]));
|
||||||
|
|
||||||
|
// configuration
|
||||||
|
$false = new Configuration;
|
||||||
|
$false->data = false;
|
||||||
|
FireflyConfig::shouldReceive('get')->withArgs(['4780_bills_to_rules', false])->andReturn($false);
|
||||||
|
FireflyConfig::shouldReceive('set')->withArgs(['4780_bills_to_rules', true]);
|
||||||
|
|
||||||
|
// preferences
|
||||||
|
$language = new Preference;
|
||||||
|
$language->data = 'en_US';
|
||||||
|
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($language);
|
||||||
|
|
||||||
|
|
||||||
|
// assume all is well.
|
||||||
|
$this->artisan('firefly-iii:bills-to-rules')
|
||||||
|
->expectsOutput('All bills are OK.')
|
||||||
|
->assertExitCode(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Basic test. Give command an unmigrated bill. This bill has the same amount_min
|
||||||
|
* as amount_max
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateToRules
|
||||||
|
*/
|
||||||
|
public function testHandleEvenBill(): void
|
||||||
|
{
|
||||||
|
$bill = Bill::create(
|
||||||
|
[
|
||||||
|
'user_id' => $this->user()->id,
|
||||||
|
'transaction_currency_id' => null,
|
||||||
|
'name' => 'I am a bill',
|
||||||
|
'match' => 'some,kind,of,match',
|
||||||
|
'amount_min' => '30',
|
||||||
|
'amount_max' => '30',
|
||||||
|
'date' => '2019-01-01',
|
||||||
|
'repeat_freq' => 'monthly',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
// mock repositories:
|
||||||
|
$userRepository = $this->mock(UserRepositoryInterface::class);
|
||||||
|
$ruleGroupRepository = $this->mock(RuleGroupRepositoryInterface::class);
|
||||||
|
$billRepository = $this->mock(BillRepositoryInterface::class);
|
||||||
|
$ruleRepository = $this->mock(RuleRepositoryInterface::class);
|
||||||
|
$group = $this->user()->ruleGroups()->inRandomOrder()->first();
|
||||||
|
// mock all calls.
|
||||||
|
$userRepository->shouldReceive('all')->atLeast()->once()
|
||||||
|
->andReturn(new Collection([$this->user()]));
|
||||||
|
$ruleRepository->shouldReceive('setUser')->atLeast()->once();
|
||||||
|
|
||||||
|
// this is what rule repos should receive:
|
||||||
|
$argumentRule = [
|
||||||
|
'rule_group_id' => $group->id,
|
||||||
|
'active' => true,
|
||||||
|
'strict' => false,
|
||||||
|
'stop_processing' => false, // field is no longer used.
|
||||||
|
'title' => 'Auto-generated rule for bill "I am a bill"',
|
||||||
|
'description' => 'This rule is auto-generated to try to match bill "I am a bill".',
|
||||||
|
'trigger' => 'store-journal',
|
||||||
|
'triggers' => [
|
||||||
|
[
|
||||||
|
'type' => 'description_contains',
|
||||||
|
'value' => 'some kind of match',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'amount_exactly',
|
||||||
|
'value' => $bill->amount_min,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'actions' => [
|
||||||
|
'type' => 'link_to_bill',
|
||||||
|
'value' => $bill->name,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
// this is what the bill repos should receive:
|
||||||
|
$argumentBill = [
|
||||||
|
'currency_id' => $bill->transaction_currency_id,
|
||||||
|
'name' => $bill->name,
|
||||||
|
'match' => 'MIGRATED_TO_RULES',
|
||||||
|
'amount_min' => $bill->amount_min,
|
||||||
|
'amount_max' => $bill->amount_max,
|
||||||
|
'date' => $bill->date,
|
||||||
|
'repeat_freq' => $bill->repeat_freq,
|
||||||
|
'skip' => $bill->skip,
|
||||||
|
'active' => $bill->active,
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
$ruleRepository->shouldReceive('store')->atLeast()->once()->withArgs([$argumentRule]);
|
||||||
|
|
||||||
|
// rule group repos should try to store a rule group in response to the result above.
|
||||||
|
$ruleGroupRepository->shouldReceive('setUser')->atLeast()->once();
|
||||||
|
$ruleGroupRepository->shouldReceive('findByTitle')
|
||||||
|
->withArgs(['Rule group for bills'])->atLeast()->once()->andReturnNull();
|
||||||
|
|
||||||
|
$ruleGroupRepository->shouldReceive('store')->atLeast()->once()->andReturn($group);
|
||||||
|
|
||||||
|
// bill repos should return one rule.
|
||||||
|
$billRepository->shouldReceive('setUser')->atLeast()->once();
|
||||||
|
$billRepository->shouldReceive('getBills')->atLeast()->once()
|
||||||
|
->andReturn(new Collection([$bill]));
|
||||||
|
$billRepository->shouldReceive('update')->atLeast()->once()
|
||||||
|
->withArgs([Mockery::any(), $argumentBill]);
|
||||||
|
|
||||||
|
// configuration
|
||||||
|
$false = new Configuration;
|
||||||
|
$false->data = false;
|
||||||
|
FireflyConfig::shouldReceive('get')->withArgs(['4780_bills_to_rules', false])->andReturn($false);
|
||||||
|
FireflyConfig::shouldReceive('set')->withArgs(['4780_bills_to_rules', true]);
|
||||||
|
|
||||||
|
// preferences
|
||||||
|
$language = new Preference;
|
||||||
|
$language->data = 'en_US';
|
||||||
|
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($language);
|
||||||
|
|
||||||
|
|
||||||
|
// assume all is well.
|
||||||
|
$this->artisan('firefly-iii:bills-to-rules')
|
||||||
|
->expectsOutput('Verified and fixed 1 bill(s).')
|
||||||
|
->assertExitCode(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Basic test. Give command an unmigrated bill. This bill has a different amount_min
|
||||||
|
* from the amount_max
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Console\Commands\Upgrade\MigrateToRules
|
||||||
|
*/
|
||||||
|
public function testHandleUnevenBill(): void
|
||||||
|
{
|
||||||
|
$bill = Bill::create(
|
||||||
|
[
|
||||||
|
'user_id' => $this->user()->id,
|
||||||
|
'transaction_currency_id' => null,
|
||||||
|
'name' => 'I am a bill',
|
||||||
|
'match' => 'some,kind,of,match',
|
||||||
|
'amount_min' => '30',
|
||||||
|
'amount_max' => '40',
|
||||||
|
'date' => '2019-01-01',
|
||||||
|
'repeat_freq' => 'monthly',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
// mock repositories:
|
||||||
|
$userRepository = $this->mock(UserRepositoryInterface::class);
|
||||||
|
$ruleGroupRepository = $this->mock(RuleGroupRepositoryInterface::class);
|
||||||
|
$billRepository = $this->mock(BillRepositoryInterface::class);
|
||||||
|
$ruleRepository = $this->mock(RuleRepositoryInterface::class);
|
||||||
|
$group = $this->user()->ruleGroups()->inRandomOrder()->first();
|
||||||
|
// mock all calls.
|
||||||
|
$userRepository->shouldReceive('all')->atLeast()->once()
|
||||||
|
->andReturn(new Collection([$this->user()]));
|
||||||
|
$ruleRepository->shouldReceive('setUser')->atLeast()->once();
|
||||||
|
|
||||||
|
// this is what rule repos should receive:
|
||||||
|
$argumentRule = [
|
||||||
|
'rule_group_id' => $group->id,
|
||||||
|
'active' => true,
|
||||||
|
'strict' => false,
|
||||||
|
'stop_processing' => false, // field is no longer used.
|
||||||
|
'title' => 'Auto-generated rule for bill "I am a bill"',
|
||||||
|
'description' => 'This rule is auto-generated to try to match bill "I am a bill".',
|
||||||
|
'trigger' => 'store-journal',
|
||||||
|
'triggers' => [
|
||||||
|
[
|
||||||
|
'type' => 'description_contains',
|
||||||
|
'value' => 'some kind of match',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'amount_less',
|
||||||
|
'value' => $bill->amount_max,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'amount_more',
|
||||||
|
'value' => $bill->amount_min,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'actions' => [
|
||||||
|
'type' => 'link_to_bill',
|
||||||
|
'value' => $bill->name,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
// this is what the bill repos should receive:
|
||||||
|
$argumentBill = [
|
||||||
|
'currency_id' => $bill->transaction_currency_id,
|
||||||
|
'name' => $bill->name,
|
||||||
|
'match' => 'MIGRATED_TO_RULES',
|
||||||
|
'amount_min' => $bill->amount_min,
|
||||||
|
'amount_max' => $bill->amount_max,
|
||||||
|
'date' => $bill->date,
|
||||||
|
'repeat_freq' => $bill->repeat_freq,
|
||||||
|
'skip' => $bill->skip,
|
||||||
|
'active' => $bill->active,
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
$ruleRepository->shouldReceive('store')->atLeast()->once()->withArgs([$argumentRule]);
|
||||||
|
|
||||||
|
// rule group repos should try to store a rule group in response to the result above.
|
||||||
|
$ruleGroupRepository->shouldReceive('setUser')->atLeast()->once();
|
||||||
|
$ruleGroupRepository->shouldReceive('findByTitle')
|
||||||
|
->withArgs(['Rule group for bills'])->atLeast()->once()->andReturnNull();
|
||||||
|
|
||||||
|
$ruleGroupRepository->shouldReceive('store')->atLeast()->once()->andReturn($group);
|
||||||
|
|
||||||
|
// bill repos should return one rule.
|
||||||
|
$billRepository->shouldReceive('setUser')->atLeast()->once();
|
||||||
|
$billRepository->shouldReceive('getBills')->atLeast()->once()
|
||||||
|
->andReturn(new Collection([$bill]));
|
||||||
|
$billRepository->shouldReceive('update')->atLeast()->once()
|
||||||
|
->withArgs([Mockery::any(), $argumentBill]);
|
||||||
|
|
||||||
|
// configuration
|
||||||
|
$false = new Configuration;
|
||||||
|
$false->data = false;
|
||||||
|
FireflyConfig::shouldReceive('get')->withArgs(['4780_bills_to_rules', false])->andReturn($false);
|
||||||
|
FireflyConfig::shouldReceive('set')->withArgs(['4780_bills_to_rules', true]);
|
||||||
|
|
||||||
|
// preferences
|
||||||
|
$language = new Preference;
|
||||||
|
$language->data = 'en_US';
|
||||||
|
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'language', 'en_US'])->andReturn($language);
|
||||||
|
|
||||||
|
|
||||||
|
// assume all is well.
|
||||||
|
$this->artisan('firefly-iii:bills-to-rules')
|
||||||
|
->expectsOutput('Verified and fixed 1 bill(s).')
|
||||||
|
->assertExitCode(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user