diff --git a/app/TransactionRules/Actions/ClearNotes.php b/app/TransactionRules/Actions/ClearNotes.php index e4825d78ba..38cc19998a 100644 --- a/app/TransactionRules/Actions/ClearNotes.php +++ b/app/TransactionRules/Actions/ClearNotes.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; +use FireflyIII\Models\Note; use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionJournal; use Log; @@ -45,8 +46,11 @@ class ClearNotes implements ActionInterface public function act(TransactionJournal $journal): bool { Log::debug(sprintf('RuleAction ClearNotes removed all notes.')); - $journal->deleteMeta('notes'); - $journal->save(); + $notes = $journal->notes()->get(); + /** @var Note $note */ + foreach ($notes as $note) { + $note->delete(); + } return true; } diff --git a/app/TransactionRules/Actions/SetDescription.php b/app/TransactionRules/Actions/SetDescription.php index 17c5eb1a13..25488f6b36 100644 --- a/app/TransactionRules/Actions/SetDescription.php +++ b/app/TransactionRules/Actions/SetDescription.php @@ -45,8 +45,7 @@ class SetDescription implements ActionInterface */ public function act(TransactionJournal $journal): bool { - $oldDescription = $journal->description; - + $oldDescription = $journal->description; $journal->description = $this->action->action_value; $journal->save(); diff --git a/app/TransactionRules/Actions/SetDestinationAccount.php b/app/TransactionRules/Actions/SetDestinationAccount.php index 9646006810..292407aa6e 100644 --- a/app/TransactionRules/Actions/SetDestinationAccount.php +++ b/app/TransactionRules/Actions/SetDestinationAccount.php @@ -66,7 +66,7 @@ class SetDestinationAccount implements ActionInterface if ($count > 2) { Log::error(sprintf('Cannot change destination account of journal #%d because it is a split journal.', $journal->id)); - return true; + return false; } // journal type: @@ -81,7 +81,7 @@ class SetDestinationAccount implements ActionInterface ) ); - return true; + return false; } // if this is a withdrawal, the new destination account must be a expense account and may be created: diff --git a/app/TransactionRules/Actions/SetSourceAccount.php b/app/TransactionRules/Actions/SetSourceAccount.php index 4b27c044d2..08fa29e315 100644 --- a/app/TransactionRules/Actions/SetSourceAccount.php +++ b/app/TransactionRules/Actions/SetSourceAccount.php @@ -66,7 +66,7 @@ class SetSourceAccount implements ActionInterface if ($count > 2) { Log::error(sprintf('Cannot change source account of journal #%d because it is a split journal.', $journal->id)); - return true; + return false; } // journal type: @@ -80,7 +80,7 @@ class SetSourceAccount implements ActionInterface ) ); - return true; + return false; } // if this is a deposit, the new source account must be a revenue account and may be created: diff --git a/tests/Unit/TransactionRules/Actions/AppendNotesTest.php b/tests/Unit/TransactionRules/Actions/AppendNotesTest.php new file mode 100644 index 0000000000..093e9f32d8 --- /dev/null +++ b/tests/Unit/TransactionRules/Actions/AppendNotesTest.php @@ -0,0 +1,84 @@ +notes()->first(); + $start = 'Default note text'; + $toAppend = 'This is appended'; + if (is_null($note)) { + $note = new Note(); + $note->noteable()->associate($journal); + } + $note->text = $start; + $note->save(); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $toAppend; + $action = new AppendNotes($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + $newNote = $journal->notes()->first(); + $this->assertEquals($start . $toAppend, $newNote->text); + + } + + /** + * @covers \FireflyIII\TransactionRules\Actions\AppendNotes::__construct() + * @covers \FireflyIII\TransactionRules\Actions\AppendNotes::act() + */ + public function testActNewNote() + { + // give journal some notes. + $journal = TransactionJournal::find(4); + $note = $journal->notes()->first(); + if (!is_null($note)) { + $note->forceDelete(); + } + $toAppend = 'This is appended'; + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $toAppend; + $action = new AppendNotes($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + $newNote = $journal->notes()->first(); + $this->assertEquals($toAppend, $newNote->text); + + } +} \ No newline at end of file diff --git a/tests/Unit/TransactionRules/Actions/ClearBudgetTest.php b/tests/Unit/TransactionRules/Actions/ClearBudgetTest.php new file mode 100644 index 0000000000..1b00597916 --- /dev/null +++ b/tests/Unit/TransactionRules/Actions/ClearBudgetTest.php @@ -0,0 +1,51 @@ +user->budgets()->first(); + $journal->budgets()->save($budget); + $this->assertGreaterThan(0, $journal->budgets()->count()); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = null; + $action = new ClearBudget($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + // assert result + $this->assertEquals(0, $journal->budgets()->count()); + + } +} \ No newline at end of file diff --git a/tests/Unit/TransactionRules/Actions/ClearCategoryTest.php b/tests/Unit/TransactionRules/Actions/ClearCategoryTest.php new file mode 100644 index 0000000000..294e3b984f --- /dev/null +++ b/tests/Unit/TransactionRules/Actions/ClearCategoryTest.php @@ -0,0 +1,49 @@ +user->categories()->first(); + $journal->budgets()->save($category); + $this->assertGreaterThan(0, $journal->categories()->count()); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = null; + $action = new ClearCategory($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + // assert result + $this->assertEquals(0, $journal->categories()->count()); + + } +} \ No newline at end of file diff --git a/tests/Unit/TransactionRules/Actions/ClearNotesTest.php b/tests/Unit/TransactionRules/Actions/ClearNotesTest.php new file mode 100644 index 0000000000..41c6d8bc22 --- /dev/null +++ b/tests/Unit/TransactionRules/Actions/ClearNotesTest.php @@ -0,0 +1,57 @@ +notes()->first(); + if (is_null($note)) { + $note = new Note; + $note->noteable()->associate($journal); + } + $note->text = 'Hello test note'; + $note->save(); + $this->assertEquals(1, $journal->notes()->count()); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = null; + $action = new ClearNotes($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + // assert result + $this->assertEquals(0, $journal->notes()->count()); + + } +} \ No newline at end of file diff --git a/tests/Unit/TransactionRules/Actions/PrependDescriptionTest.php b/tests/Unit/TransactionRules/Actions/PrependDescriptionTest.php new file mode 100644 index 0000000000..d6063ef3fa --- /dev/null +++ b/tests/Unit/TransactionRules/Actions/PrependDescriptionTest.php @@ -0,0 +1,53 @@ +description = $description; + $journal->save(); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $prepend; + $action = new PrependDescription($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + $journal = TransactionJournal::find(7); + + // assert result + $this->assertEquals($prepend.$description, $journal->description); + + } +} \ No newline at end of file diff --git a/tests/Unit/TransactionRules/Actions/PrependNotesTest.php b/tests/Unit/TransactionRules/Actions/PrependNotesTest.php new file mode 100644 index 0000000000..0d42bb23ee --- /dev/null +++ b/tests/Unit/TransactionRules/Actions/PrependNotesTest.php @@ -0,0 +1,84 @@ +notes()->first(); + $start = 'Default note text'; + $toPrepend = 'This is prepended'; + if (is_null($note)) { + $note = new Note(); + $note->noteable()->associate($journal); + } + $note->text = $start; + $note->save(); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $toPrepend; + $action = new PrependNotes($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + $newNote = $journal->notes()->first(); + $this->assertEquals($toPrepend . $start, $newNote->text); + + } + + /** + * @covers \FireflyIII\TransactionRules\Actions\PrependNotes::__construct() + * @covers \FireflyIII\TransactionRules\Actions\PrependNotes::act() + */ + public function testActNewNote() + { + // give journal some notes. + $journal = TransactionJournal::find(4); + $note = $journal->notes()->first(); + if (!is_null($note)) { + $note->forceDelete(); + } + $toPrepend = 'This is appended'; + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $toPrepend; + $action = new PrependNotes($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + $newNote = $journal->notes()->first(); + $this->assertEquals($toPrepend, $newNote->text); + + } +} \ No newline at end of file diff --git a/tests/Unit/TransactionRules/Actions/RemoveAllTagsTest.php b/tests/Unit/TransactionRules/Actions/RemoveAllTagsTest.php new file mode 100644 index 0000000000..2e74697ba7 --- /dev/null +++ b/tests/Unit/TransactionRules/Actions/RemoveAllTagsTest.php @@ -0,0 +1,52 @@ +user->tags()->get(); + foreach ($tags as $tag) { + $journal->tags()->save($tag); + $journal->save(); + } + $this->assertEquals($tags->count(), $journal->tags()->count()); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = null; + $action = new RemoveAllTags($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + $this->assertEquals(0, $journal->tags()->count()); + } +} \ No newline at end of file diff --git a/tests/Unit/TransactionRules/Actions/RemoveTagTest.php b/tests/Unit/TransactionRules/Actions/RemoveTagTest.php new file mode 100644 index 0000000000..78434a7d72 --- /dev/null +++ b/tests/Unit/TransactionRules/Actions/RemoveTagTest.php @@ -0,0 +1,79 @@ +user->tags()->get(); + foreach ($tags as $tag) { + $journal->tags()->save($tag); + $journal->save(); + } + $firstTag = $tags->first(); + $this->assertEquals($tags->count(), $journal->tags()->count()); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $firstTag->tag; + $action = new RemoveTag($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + foreach ($journal->tags()->get() as $tag) { + $this->assertNotEquals($firstTag->id, $tag->id); + } + $this->assertEquals(($tags->count() - 1), $journal->tags()->count()); + } + + /** + * @covers \FireflyIII\TransactionRules\Actions\RemoveTag::__construct() + * @covers \FireflyIII\TransactionRules\Actions\RemoveTag::act() + */ + public function testActNoTag() + { + // get journal, link al tags: + $journal = TransactionJournal::find(11); + $tags = $journal->user->tags()->get(); + foreach ($tags as $tag) { + $journal->tags()->save($tag); + $journal->save(); + } + $this->assertEquals($tags->count(), $journal->tags()->count()); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = rand(1, 1234) . 'nosuchtag'; + $action = new RemoveTag($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + $this->assertEquals($tags->count(), $journal->tags()->count()); + } +} \ No newline at end of file diff --git a/tests/Unit/TransactionRules/Actions/SetBudgetTest.php b/tests/Unit/TransactionRules/Actions/SetBudgetTest.php new file mode 100644 index 0000000000..d00b55cf99 --- /dev/null +++ b/tests/Unit/TransactionRules/Actions/SetBudgetTest.php @@ -0,0 +1,49 @@ +user->budgets()->first(); + $journal->budgets()->detach(); + $this->assertEquals(0, $journal->budgets()->count()); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $budget->name; + $action = new SetBudget($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + $this->assertEquals(1, $journal->budgets()->count()); + $this->assertEquals($budget->name, $journal->budgets()->first()->name); + } +} \ No newline at end of file diff --git a/tests/Unit/TransactionRules/Actions/SetCategoryTest.php b/tests/Unit/TransactionRules/Actions/SetCategoryTest.php new file mode 100644 index 0000000000..09fef3f2f4 --- /dev/null +++ b/tests/Unit/TransactionRules/Actions/SetCategoryTest.php @@ -0,0 +1,48 @@ +user->categories()->first(); + $journal->categories()->detach(); + $this->assertEquals(0, $journal->categories()->count()); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $category->name; + $action = new SetBudget($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + $this->assertEquals(1, $journal->categories()->count()); + $this->assertEquals($category->name, $journal->categories()->first()->name); + } +} \ No newline at end of file diff --git a/tests/Unit/TransactionRules/Actions/SetDescriptionTest.php b/tests/Unit/TransactionRules/Actions/SetDescriptionTest.php new file mode 100644 index 0000000000..92c7037173 --- /dev/null +++ b/tests/Unit/TransactionRules/Actions/SetDescriptionTest.php @@ -0,0 +1,52 @@ +description = $description; + $journal->save(); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $newDescription; + $action = new SetDescription($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + $journal = TransactionJournal::find(14); + + // assert result + $this->assertEquals($newDescription, $journal->description); + + } +} \ No newline at end of file diff --git a/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php b/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php new file mode 100644 index 0000000000..91dc27d940 --- /dev/null +++ b/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php @@ -0,0 +1,175 @@ +first(); + $journal = TransactionJournal::where('transaction_type_id', $type->id)->first(); + $destinationTr = $journal->transactions()->where('amount', '>', 0)->first(); + $destination = $destinationTr->account; + $user = $journal->user; + $accountType = AccountType::whereType(AccountType::ASSET)->first(); + $account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $destination->id)->first(); + $this->assertNotEquals($destination->id, $account->id); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $account->name; + $action = new SetDestinationAccount($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + // test journal for new account + $journal = TransactionJournal::find($journal->id); + $destinationTr = $journal->transactions()->where('amount', '>', 0)->first(); + $newDestination = $destinationTr->account; + $this->assertNotEquals($destination->id, $newDestination->id); + $this->assertEquals($newDestination->id, $account->id); + } + + /** + * Give deposit new asset account (will fail) + * + * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::__construct() + * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::act() + * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::findAssetAccount() + */ + public function testActDepositNew() + { + $type = TransactionType::whereType(TransactionType::DEPOSIT)->first(); + $journal = TransactionJournal::where('transaction_type_id', $type->id)->first(); + $destinationTr = $journal->transactions()->where('amount', '>', 0)->first(); + $destination = $destinationTr->account; + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = 'Some new asset ' . rand(1, 1000); + $action = new SetDestinationAccount($ruleAction); + $result = $action->act($journal); + $this->assertFalse($result); + + // test journal for still having old account + $journal = TransactionJournal::find($journal->id); + $destinationTr = $journal->transactions()->where('amount', '>', 0)->first(); + $newDestination = $destinationTr->account; + $this->assertEquals($destination->id, $newDestination->id); + } + + /** + * Give withdrawal existing expense account. + * + * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::__construct() + * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::act() + * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::findExpenseAccount + */ + public function testActWithdrawalExisting() + { + $type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first(); + $journal = TransactionJournal::where('transaction_type_id', $type->id)->first(); + $destinationTr = $journal->transactions()->where('amount', '>', 0)->first(); + $destination = $destinationTr->account; + $user = $journal->user; + $accountType = AccountType::whereType(AccountType::EXPENSE)->first(); + $account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $destination->id)->first(); + $this->assertNotEquals($destination->id, $account->id); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $account->name; + $action = new SetDestinationAccount($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + // test journal for new account + $journal = TransactionJournal::find($journal->id); + $destinationTr = $journal->transactions()->where('amount', '>', 0)->first(); + $newDestination = $destinationTr->account; + $this->assertNotEquals($destination->id, $newDestination->id); + $this->assertEquals($newDestination->id, $account->id); + } + + /** + * Give withdrawal new expense account. + * + * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::__construct() + * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::act() + * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::findExpenseAccount + */ + public function testActWithdrawalNew() + { + $type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first(); + $journal = TransactionJournal::where('transaction_type_id', $type->id)->first(); + $destinationTr = $journal->transactions()->where('amount', '>', 0)->first(); + $destination = $destinationTr->account; + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = 'Some new expense ' . rand(1, 1000); + $action = new SetDestinationAccount($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + // test journal for new account + $journal = TransactionJournal::find($journal->id); + $destinationTr = $journal->transactions()->where('amount', '>', 0)->first(); + $newDestination = $destinationTr->account; + $this->assertNotEquals($destination->id, $newDestination->id); + } + + /** + * Test this on a split journal. + * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::__construct() + * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::act() + */ + public function testSplitJournal() + { + $transaction = Transaction::orderBy('count', 'DESC')->groupBy('transaction_journal_id') + ->get(['transaction_journal_id', DB::raw('COUNT(transaction_journal_id) as count')]) + ->first(); + $journal = TransactionJournal::find($transaction->transaction_journal_id); + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = 'Some new asset ' . rand(1, 1000); + $action = new SetDestinationAccount($ruleAction); + $result = $action->act($journal); + $this->assertFalse($result); + } + +} \ No newline at end of file diff --git a/tests/Unit/TransactionRules/Actions/SetNotesTest.php b/tests/Unit/TransactionRules/Actions/SetNotesTest.php new file mode 100644 index 0000000000..96e4d8edf5 --- /dev/null +++ b/tests/Unit/TransactionRules/Actions/SetNotesTest.php @@ -0,0 +1,81 @@ +notes()->first(); + if (is_null($note)) { + $note = new Note; + $note->noteable()->associate($journal); + } + $note->text = 'Hello test note'; + $note->save(); + $this->assertEquals(1, $journal->notes()->count()); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = 'These are new notes ' . rand(1, 1234); + $action = new SetNotes($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + // assert result + $this->assertEquals(1, $journal->notes()->count()); + $this->assertEquals($note->id, $journal->notes()->first()->id); + + } + + /** + * @covers \FireflyIII\TransactionRules\Actions\SetNotes::__construct() + * @covers \FireflyIII\TransactionRules\Actions\SetNotes::act() + */ + public function testActNoNotes() + { + // give journal a note: + $journal = TransactionJournal::find(16); + $journal->notes()->forceDelete(); + $this->assertEquals(0, $journal->notes()->count()); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = 'These are new notes ' . rand(1, 1234); + $action = new SetNotes($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + // assert result + $this->assertEquals(1, $journal->notes()->count()); + } +} \ No newline at end of file diff --git a/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php b/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php new file mode 100644 index 0000000000..d4ec0912be --- /dev/null +++ b/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php @@ -0,0 +1,173 @@ +first(); + $journal = TransactionJournal::where('transaction_type_id', $type->id)->first(); + $sourceTr = $journal->transactions()->where('amount', '<', 0)->first(); + $source = $sourceTr->account; + $user = $journal->user; + $accountType = AccountType::whereType(AccountType::REVENUE)->first(); + $account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $source->id)->first(); + $this->assertNotEquals($source->id, $account->id); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $account->name; + $action = new SetSourceAccount($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + // test journal for new account + $journal = TransactionJournal::find($journal->id); + $sourceTr = $journal->transactions()->where('amount', '<', 0)->first(); + $newSource = $sourceTr->account; + $this->assertNotEquals($source->id, $newSource->id); + $this->assertEquals($newSource->id, $account->id); + } + + /** + * Give deposit new revenue account. + * + * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::__construct() + * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::act() + * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::findRevenueAccount + */ + public function testActDepositNewUpdated() + { + $type = TransactionType::whereType(TransactionType::DEPOSIT)->first(); + $journal = TransactionJournal::where('transaction_type_id', $type->id)->first(); + $sourceTr = $journal->transactions()->where('amount', '<', 0)->first(); + $source = $sourceTr->account; + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = 'Some new revenue ' . rand(1, 1000); + $action = new SetSourceAccount($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + // test journal for new account + $journal = TransactionJournal::find($journal->id); + $sourceTr = $journal->transactions()->where('amount', '<', 0)->first(); + $newSource = $sourceTr->account; + $this->assertNotEquals($source->id, $newSource->id); + } + + /** + * Give withdrawal existing asset account. + * + * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::__construct() + * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::act() + * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::findAssetAccount() + */ + public function testActWithdrawalExistingUpdated() + { + $type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first(); + $journal = TransactionJournal::where('transaction_type_id', $type->id)->first(); + $sourceTr = $journal->transactions()->where('amount', '<', 0)->first(); + $source = $sourceTr->account; + $user = $journal->user; + $accountType = AccountType::whereType(AccountType::ASSET)->first(); + $account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $source->id)->first(); + $this->assertNotEquals($source->id, $account->id); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $account->name; + $action = new SetSourceAccount($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + + // test journal for new account + $journal = TransactionJournal::find($journal->id); + $sourceTr = $journal->transactions()->where('amount', '<', 0)->first(); + $newSource = $sourceTr->account; + $this->assertNotEquals($source->id, $newSource->id); + $this->assertEquals($newSource->id, $account->id); + } + + /** + * Give withdrawal new asset account (will fail) + * + * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::__construct() + * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::act() + * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::findAssetAccount() + */ + public function testActWithdrawalNew() + { + $type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first(); + $journal = TransactionJournal::where('transaction_type_id', $type->id)->first(); + $sourceTr = $journal->transactions()->where('amount', '<', 0)->first(); + $source = $sourceTr->account; + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = 'Some new asset ' . rand(1, 1000); + $action = new SetSourceAccount($ruleAction); + $result = $action->act($journal); + $this->assertFalse($result); + + // test journal for still having old account + $journal = TransactionJournal::find($journal->id); + $sourceTr = $journal->transactions()->where('amount', '<', 0)->first(); + $newSource = $sourceTr->account; + $this->assertEquals($source->id, $newSource->id); + } + + /** + * Test this on a split journal. + * + * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::__construct() + * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::act() + */ + public function testSplitJournal() + { + $transaction = Transaction::orderBy('count', 'DESC')->groupBy('transaction_journal_id') + ->get(['transaction_journal_id', DB::raw('COUNT(transaction_journal_id) as count')]) + ->first(); + $journal = TransactionJournal::find($transaction->transaction_journal_id); + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = 'Some new asset ' . rand(1, 1000); + $action = new SetSourceAccount($ruleAction); + $result = $action->act($journal); + $this->assertFalse($result); + } + +} \ No newline at end of file