2017-10-04 15:27:20 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* SetDestinationAccountTest.php
|
2020-02-16 13:59:55 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2017-10-04 15:27:20 +02:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 08:40:00 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:45:03 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-10-04 15:27:20 +02:00
|
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Tests\Unit\TransactionRules\Actions;
|
|
|
|
|
|
|
|
use FireflyIII\Models\AccountType;
|
|
|
|
use FireflyIII\Models\RuleAction;
|
2020-08-23 16:12:16 +02:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2017-10-04 15:27:20 +02:00
|
|
|
use FireflyIII\TransactionRules\Actions\SetDestinationAccount;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try split journal
|
|
|
|
*
|
|
|
|
* Class SetDestinationAccountTest
|
|
|
|
*/
|
|
|
|
class SetDestinationAccountTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Give deposit existing asset account.
|
|
|
|
*
|
2018-03-04 07:56:30 +01:00
|
|
|
* @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount
|
2017-10-04 15:27:20 +02:00
|
|
|
*/
|
2018-05-11 19:58:10 +02:00
|
|
|
public function testActDepositExisting(): void
|
2017-10-04 15:27:20 +02:00
|
|
|
{
|
2020-08-23 16:12:16 +02:00
|
|
|
// get random deposit:
|
|
|
|
$deposit = $this->getRandomDeposit();
|
|
|
|
$destinationTr = $deposit->transactions()->where('amount', '>', 0)->first();
|
|
|
|
$destination = $destinationTr->account;
|
|
|
|
$oldDestination = $destinationTr->account_id;
|
|
|
|
|
|
|
|
// grab unused asset account:
|
|
|
|
$user = $deposit->user;
|
|
|
|
$accountType = AccountType::whereType(AccountType::ASSET)->first();
|
|
|
|
$newDestination = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $destination->id)->first();
|
|
|
|
$this->assertNotEquals($destination->id, $newDestination->id);
|
|
|
|
|
|
|
|
// array with info:
|
|
|
|
$array = [
|
|
|
|
'transaction_journal_id' => $deposit->id,
|
|
|
|
'user_id' => $this->user()->id,
|
|
|
|
'transaction_type_type' => TransactionType::DEPOSIT,
|
|
|
|
'source_account_type' => AccountType::REVENUE,
|
|
|
|
];
|
2018-02-28 20:18:47 +01:00
|
|
|
|
2017-10-04 15:27:20 +02:00
|
|
|
// fire the action:
|
|
|
|
$ruleAction = new RuleAction;
|
2020-08-23 16:12:16 +02:00
|
|
|
$ruleAction->action_value = $newDestination->name;
|
2017-10-04 15:27:20 +02:00
|
|
|
$action = new SetDestinationAccount($ruleAction);
|
2020-08-23 16:12:16 +02:00
|
|
|
$result = $action->actOnArray($array);
|
2017-10-04 15:27:20 +02:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
// test journal for new account
|
2020-08-23 16:12:16 +02:00
|
|
|
$destinationTr->refresh();
|
|
|
|
$updatedDestination = $destinationTr->account;
|
|
|
|
$this->assertEquals($newDestination->id, $updatedDestination->id);
|
|
|
|
$this->assertEquals($destinationTr->account_id, $newDestination->id);
|
|
|
|
|
|
|
|
$destinationTr->account_id = $oldDestination;
|
|
|
|
$destinationTr->save();
|
2017-10-04 15:27:20 +02:00
|
|
|
}
|
|
|
|
|
2018-03-04 07:56:30 +01:00
|
|
|
/**
|
|
|
|
* Give deposit not existing asset account.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount
|
|
|
|
*/
|
2018-05-11 19:58:10 +02:00
|
|
|
public function testActDepositNotExisting(): void
|
2018-03-04 07:56:30 +01:00
|
|
|
{
|
2020-08-23 16:12:16 +02:00
|
|
|
// get random deposit:
|
|
|
|
$deposit = $this->getRandomDeposit();
|
|
|
|
$destinationTr = $deposit->transactions()->where('amount', '>', 0)->first();
|
|
|
|
$oldDestination = $destinationTr->account;
|
2018-03-04 07:56:30 +01:00
|
|
|
|
2020-08-23 16:12:16 +02:00
|
|
|
// array with info:
|
|
|
|
$array = [
|
|
|
|
'transaction_journal_id' => $deposit->id,
|
|
|
|
'user_id' => $this->user()->id,
|
|
|
|
'transaction_type_type' => TransactionType::DEPOSIT,
|
|
|
|
'source_account_type' => AccountType::REVENUE,
|
|
|
|
];
|
2018-03-04 07:56:30 +01:00
|
|
|
|
|
|
|
// fire the action:
|
|
|
|
$ruleAction = new RuleAction;
|
2020-08-23 16:12:16 +02:00
|
|
|
$ruleAction->action_value = sprintf('Not existing asset account #%d', $this->randomInt());
|
2018-03-04 07:56:30 +01:00
|
|
|
$action = new SetDestinationAccount($ruleAction);
|
2020-08-23 16:12:16 +02:00
|
|
|
$result = $action->actOnArray($array);
|
2018-03-04 07:56:30 +01:00
|
|
|
$this->assertFalse($result);
|
2020-08-23 16:12:16 +02:00
|
|
|
|
|
|
|
// test journal for new account
|
|
|
|
$destinationTr->refresh();
|
|
|
|
$this->assertEquals($destinationTr->account_id, $oldDestination->id);
|
2018-03-04 07:56:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Give withdrawal not existing expense account.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount
|
|
|
|
*/
|
2018-05-11 19:58:10 +02:00
|
|
|
public function testActWithDrawalNotExisting(): void
|
2018-03-04 07:56:30 +01:00
|
|
|
{
|
2020-08-23 16:12:16 +02:00
|
|
|
// get random withdrawal:
|
|
|
|
$withdrawal = $this->getRandomWithdrawal();
|
|
|
|
$destinationTr = $withdrawal->transactions()->where('amount', '>', 0)->first();
|
|
|
|
$oldDestination = $destinationTr->account;
|
|
|
|
// array with info:
|
|
|
|
$array = [
|
|
|
|
'transaction_journal_id' => $withdrawal->id,
|
|
|
|
'user_id' => $this->user()->id,
|
|
|
|
'transaction_type_type' => TransactionType::WITHDRAWAL,
|
|
|
|
'source_account_type' => AccountType::ASSET,
|
|
|
|
];
|
2018-03-04 07:56:30 +01:00
|
|
|
|
|
|
|
// fire the action:
|
|
|
|
$ruleAction = new RuleAction;
|
2020-08-23 16:12:16 +02:00
|
|
|
$ruleAction->action_value = sprintf('Not existing expense account #%d', $this->randomInt());
|
2018-03-04 07:56:30 +01:00
|
|
|
$action = new SetDestinationAccount($ruleAction);
|
2020-08-23 16:12:16 +02:00
|
|
|
$result = $action->actOnArray($array);
|
2018-03-04 07:56:30 +01:00
|
|
|
$this->assertTrue($result);
|
2020-08-23 16:12:16 +02:00
|
|
|
|
|
|
|
// test journal for new account
|
|
|
|
$destinationTr->refresh();
|
|
|
|
$newDestination = $destinationTr->account;
|
|
|
|
$this->assertEquals($newDestination->name, $ruleAction->action_value);
|
|
|
|
|
|
|
|
$destinationTr->account_id = $oldDestination;
|
|
|
|
$destinationTr->save();
|
2018-03-04 07:56:30 +01:00
|
|
|
}
|
|
|
|
|
2017-10-04 15:27:20 +02:00
|
|
|
/**
|
|
|
|
* Give withdrawal existing expense account.
|
|
|
|
*
|
2018-03-04 07:56:30 +01:00
|
|
|
* @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount
|
2017-10-04 15:27:20 +02:00
|
|
|
*/
|
2018-05-11 19:58:10 +02:00
|
|
|
public function testActWithdrawalExisting(): void
|
2017-10-04 15:27:20 +02:00
|
|
|
{
|
2020-08-23 16:12:16 +02:00
|
|
|
// get random withdrawal:
|
|
|
|
$withdrawal = $this->getRandomWithdrawal();
|
|
|
|
$destinationTr = $withdrawal->transactions()->where('amount', '>', 0)->first();
|
|
|
|
$oldDestination = $destinationTr->account;
|
|
|
|
$newDestination = $this->user()->accounts()->where('id', '!=', $oldDestination->id)->first();
|
|
|
|
|
|
|
|
// array with info:
|
|
|
|
$array = [
|
|
|
|
'transaction_journal_id' => $withdrawal->id,
|
|
|
|
'user_id' => $this->user()->id,
|
|
|
|
'transaction_type_type' => TransactionType::WITHDRAWAL,
|
|
|
|
'source_account_type' => AccountType::ASSET,
|
|
|
|
];
|
2018-02-28 20:18:47 +01:00
|
|
|
|
2017-10-04 15:27:20 +02:00
|
|
|
// fire the action:
|
|
|
|
$ruleAction = new RuleAction;
|
2020-08-23 16:12:16 +02:00
|
|
|
$ruleAction->action_value = $newDestination->name;
|
2017-10-04 15:27:20 +02:00
|
|
|
$action = new SetDestinationAccount($ruleAction);
|
2020-08-23 16:12:16 +02:00
|
|
|
$result = $action->actOnArray($array);
|
2017-10-04 15:27:20 +02:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
// test journal for new account
|
2020-08-23 16:12:16 +02:00
|
|
|
$destinationTr->refresh();
|
2017-10-04 15:27:20 +02:00
|
|
|
$newDestination = $destinationTr->account;
|
2020-08-23 16:12:16 +02:00
|
|
|
$this->assertEquals($newDestination->name, $ruleAction->action_value);
|
2017-10-04 15:27:20 +02:00
|
|
|
}
|
|
|
|
|
2017-11-08 09:05:10 +01:00
|
|
|
}
|