Skip to content

Firefly III: a personal finances manager

Update tests

James Colec000fe4557cd
diff --git a/app/TransactionRules/Actions/AddTag.php b/app/TransactionRules/Actions/AddTag.php
index 3a12c8c694..60351f58b3 100644
--- a/app/TransactionRules/Actions/AddTag.php
+++ b/app/TransactionRules/Actions/AddTag.php
@@ -61,6 +61,6 @@ class AddTag implements ActionInterface
Log::debug(sprintf('RuleAction AddTag fired but tag %d ("%s") was already added to journal %d.', $tag->id, $tag->tag, $journal->id));
- return true;
+ return false;
}
}
diff --git a/app/TransactionRules/Actions/AppendNotes.php b/app/TransactionRules/Actions/AppendNotes.php
index 6f9af7fba4..95af366d84 100644
--- a/app/TransactionRules/Actions/AppendNotes.php
+++ b/app/TransactionRules/Actions/AppendNotes.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;
@@ -44,10 +45,16 @@ class AppendNotes implements ActionInterface
*/
public function act(TransactionJournal $journal): bool
{
- $notes = $journal->getMeta('notes');
+ $dbNote = $journal->notes()->first();
+ if (is_null($dbNote)) {
+ $dbNote = new Note;
+ $dbNote->noteable()->associate($journal);
+ }
+ $notes = $dbNote->text;
Log::debug(sprintf('RuleAction AppendNotes appended "%s" to "%s".', $this->action->action_value, $notes));
- $notes = $notes . $this->action->action_value;
- $journal->setMeta('notes', $notes);
+ $notes = $notes . $this->action->action_value;
+ $dbNote->text = $notes;
+ $dbNote->save();
$journal->save();
return true;
diff --git a/app/TransactionRules/Actions/PrependNotes.php b/app/TransactionRules/Actions/PrependNotes.php
index e0cede1236..0eb57fb53a 100644
--- a/app/TransactionRules/Actions/PrependNotes.php
+++ b/app/TransactionRules/Actions/PrependNotes.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;
@@ -44,10 +45,16 @@ class PrependNotes implements ActionInterface
*/
public function act(TransactionJournal $journal): bool
{
- $notes = $journal->getMeta('notes');
+ $dbNote = $journal->notes()->first();
+ if (is_null($dbNote)) {
+ $dbNote = new Note;
+ $dbNote->noteable()->associate($journal);
+ }
+ $notes = $dbNote->text;
Log::debug(sprintf('RuleAction PrependNotes prepended "%s" with "%s".', $notes, $this->action->action_value));
- $notes = $this->action->action_value . $notes;
- $journal->setMeta('notes', $notes);
+ $notes = $this->action->action_value . $notes;
+ $dbNote->text = $notes;
+ $dbNote->save();
$journal->save();
return true;
diff --git a/app/TransactionRules/Actions/SetNotes.php b/app/TransactionRules/Actions/SetNotes.php
index 2275c56271..2e0f0b0fa6 100644
--- a/app/TransactionRules/Actions/SetNotes.php
+++ b/app/TransactionRules/Actions/SetNotes.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;
@@ -44,9 +45,14 @@ class SetNotes implements ActionInterface
*/
public function act(TransactionJournal $journal): bool
{
- $oldNotes = $journal->getMeta('notes');
-
- $journal->setMeta('notes', $this->action->action_value);
+ $dbNote = $journal->notes()->first();
+ if (is_null($dbNote)) {
+ $dbNote = new Note;
+ $dbNote->noteable()->associate($journal);
+ }
+ $oldNotes = $dbNote->text;
+ $dbNote->text = $this->action->action_value;
+ $dbNote->save();
$journal->save();
Log::debug(sprintf('RuleAction SetNotes changed the notes of journal #%d from "%s" to "%s".', $journal->id, $oldNotes, $this->action->action_value));
diff --git a/tests/Feature/Controllers/TagControllerTest.php b/tests/Feature/Controllers/TagControllerTest.php
index 5f335c10bc..56aa6fea86 100644
--- a/tests/Feature/Controllers/TagControllerTest.php
+++ b/tests/Feature/Controllers/TagControllerTest.php
@@ -107,6 +107,7 @@ class TagControllerTest extends TestCase
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('count')->andReturn(0);
$repository->shouldReceive('tagCloud')->andReturn([]);
+ $repository->shouldReceive('oldestTag')->andReturn(null)->once();
$this->be($this->user());
$response = $this->get(route('tags.index'));
@@ -137,9 +138,7 @@ class TagControllerTest extends TestCase
$repository->shouldReceive('earnedInPeriod')->andReturn('1')->once();
$repository->shouldReceive('sumsOfTag')->andReturn($amounts)->once();
-
$collector->shouldReceive('removeFilter')->andReturnSelf()->once();
-
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf()->once();
$collector->shouldReceive('setLimit')->andReturnSelf()->once();
$collector->shouldReceive('setPage')->andReturnSelf()->once();
diff --git a/tests/Unit/TransactionRules/Actions/AddTagTest.php b/tests/Unit/TransactionRules/Actions/AddTagTest.php
new file mode 100644
index 0000000000..e8b9352879
--- /dev/null
+++ b/tests/Unit/TransactionRules/Actions/AddTagTest.php
@@ -0,0 +1,59 @@
+<?php
+/**
+ * AddTagTest.php
+ * Copyright (c) 2017 [email protected]
+ * This software may be modified and distributed under the terms of the
+ * Creative Commons Attribution-ShareAlike 4.0 International License.
+ *
+ * See the LICENSE file for details.
+ */
+
+declare(strict_types=1);
+
+namespace Tests\Unit\TransactionRules\Actions;
+
+
+use FireflyIII\Models\RuleAction;
+use FireflyIII\Models\Tag;
+use FireflyIII\Models\TransactionJournal;
+use FireflyIII\TransactionRules\Actions\AddTag;
+use Tests\TestCase;
+
+class AddTagTest extends TestCase
+{
+ /**
+ * @covers \FireflyIII\TransactionRules\Actions\AddTag::__construct
+ * @covers \FireflyIII\TransactionRules\Actions\AddTag::act()
+ */
+ public function testActExistingTag()
+ {
+ $this->assertDatabaseHas('tag_transaction_journal', ['tag_id' => 2, 'transaction_journal_id' => 1]);
+ $ruleAction = new RuleAction;
+ $ruleAction->action_value = 'housing';
+ $journal = TransactionJournal::find(1);
+ $action = new AddTag($ruleAction);
+ $result = $action->act($journal);
+ $this->assertFalse($result);
+ $this->assertDatabaseHas('tag_transaction_journal', ['tag_id' => 2, 'transaction_journal_id' => 1]);
+
+ }
+
+ /**
+ * @covers \FireflyIII\TransactionRules\Actions\AddTag::act()
+ */
+ public function testActNoTag()
+ {
+ $ruleAction = new RuleAction;
+ $ruleAction->action_value = 'TestTag-' . rand(1, 1000);
+ $journal = TransactionJournal::find(1);
+ $action = new AddTag($ruleAction);
+ $result = $action->act($journal);
+ $this->assertTrue($result);
+
+ // find newly created tag:
+ $tag = Tag::orderBy('id', 'DESC')->first();
+ $this->assertDatabaseHas('tag_transaction_journal', ['tag_id' => $tag->id, 'transaction_journal_id' => 1]);
+
+ }
+
+}
\ No newline at end of file
diff --git a/tests/Unit/TransactionRules/Actions/AppendDescriptionTest.php b/tests/Unit/TransactionRules/Actions/AppendDescriptionTest.php
new file mode 100644
index 0000000000..3d191319a4
--- /dev/null
+++ b/tests/Unit/TransactionRules/Actions/AppendDescriptionTest.php
@@ -0,0 +1,43 @@
+<?php
+/**
+ * AppendDescriptionTest.php
+ * Copyright (c) 2017 [email protected]
+ * This software may be modified and distributed under the terms of the
+ * Creative Commons Attribution-ShareAlike 4.0 International License.
+ *
+ * See the LICENSE file for details.
+ */
+
+declare(strict_types=1);
+
+namespace Tests\Unit\TransactionRules\Actions;
+
+
+use FireflyIII\Models\RuleAction;
+use FireflyIII\Models\TransactionJournal;
+use FireflyIII\TransactionRules\Actions\AppendDescription;
+use Tests\TestCase;
+
+class AppendDescriptionTest extends TestCase
+{
+ /**
+ * @covers \FireflyIII\TransactionRules\Actions\AppendDescription::__construct
+ * @covers \FireflyIII\TransactionRules\Actions\AppendDescription::act()
+ */
+ public function testActExistingTag()
+ {
+ $ruleAction = new RuleAction;
+ $ruleAction->action_value = 'APPEND';
+
+ $journal = TransactionJournal::find(1);
+ $oldDescription = $journal->description;
+ $action = new AppendDescription($ruleAction);
+ $result = $action->act($journal);
+ $this->assertTrue($result);
+
+ $journal = TransactionJournal::find(1);
+ $this->assertEquals($oldDescription . 'APPEND', $journal->description);
+
+ }
+
+}
\ No newline at end of file