From baefd4f93b6ece0a16cb1e1fe31586aee5d5b6e9 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 22 Jan 2017 11:23:40 +0100 Subject: [PATCH] Two new rule triggers --- app/Rules/Triggers/BudgetIs.php | 95 ++++++++++++++++++++++ app/Rules/Triggers/CategoryIs.php | 95 ++++++++++++++++++++++ config/firefly.php | 2 + tests/acceptance/NaughtyListTest.php | 115 --------------------------- 4 files changed, 192 insertions(+), 115 deletions(-) create mode 100644 app/Rules/Triggers/BudgetIs.php create mode 100644 app/Rules/Triggers/CategoryIs.php delete mode 100644 tests/acceptance/NaughtyListTest.php diff --git a/app/Rules/Triggers/BudgetIs.php b/app/Rules/Triggers/BudgetIs.php new file mode 100644 index 0000000000..fa444f3ffc --- /dev/null +++ b/app/Rules/Triggers/BudgetIs.php @@ -0,0 +1,95 @@ +budgets()->first(); + if (!is_null($budget)) { + $name = strtolower($budget->name); + // match on journal: + if ($name === strtolower($this->triggerValue)) { + Log::debug(sprintf('RuleTrigger BudgetIs for journal #%d: "%s" is "%s", return true.', $journal->id, $name, $this->triggerValue)); + + return true; + } + } + + if (is_null($budget)) { + // perhaps transactions have this budget? + /** @var Transaction $transaction */ + foreach ($journal->transactions as $transaction) { + $budget = $transaction->budgets()->first(); + if (!is_null($budget)) { + $name = strtolower($budget->name); + if ($name === strtolower($this->triggerValue)) { + Log::debug( + sprintf( + 'RuleTrigger BudgetIs for journal #%d (transaction #%d): "%s" is "%s", return true.', + $journal->id, $transaction->id, $name, $this->triggerValue + ) + ); + + return true; + } + } + } + } + + Log::debug(sprintf('RuleTrigger BudgetIs for journal #%d: does not have budget "%s", return false.', $journal->id, $this->triggerValue)); + + return false; + } +} diff --git a/app/Rules/Triggers/CategoryIs.php b/app/Rules/Triggers/CategoryIs.php new file mode 100644 index 0000000000..28dcddb086 --- /dev/null +++ b/app/Rules/Triggers/CategoryIs.php @@ -0,0 +1,95 @@ +categories()->first(); + if (!is_null($category)) { + $name = strtolower($category->name); + // match on journal: + if ($name === strtolower($this->triggerValue)) { + Log::debug(sprintf('RuleTrigger CategoryIs for journal #%d: "%s" is "%s", return true.', $journal->id, $name, $this->triggerValue)); + + return true; + } + } + + if (is_null($category)) { + // perhaps transactions have this category? + /** @var Transaction $transaction */ + foreach ($journal->transactions as $transaction) { + $category = $transaction->categories()->first(); + if (!is_null($category)) { + $name = strtolower($category->name); + if ($name === strtolower($this->triggerValue)) { + Log::debug( + sprintf( + 'RuleTrigger CategoryIs for journal #%d (transaction #%d): "%s" is "%s", return true.', + $journal->id, $transaction->id, $name, $this->triggerValue + ) + ); + + return true; + } + } + } + } + + Log::debug(sprintf('RuleTrigger CategoryIs for journal #%d: does not have category "%s", return false.', $journal->id, $this->triggerValue)); + + return false; + } +} diff --git a/config/firefly.php b/config/firefly.php index 8c4f2a4085..a68e72bb74 100644 --- a/config/firefly.php +++ b/config/firefly.php @@ -174,6 +174,8 @@ return [ 'description_ends' => 'FireflyIII\Rules\Triggers\DescriptionEnds', 'description_contains' => 'FireflyIII\Rules\Triggers\DescriptionContains', 'description_is' => 'FireflyIII\Rules\Triggers\DescriptionIs', + 'category_is' => 'FireflyIII\Rules\Triggers\CategoryIs', + 'budget_is' => 'FireflyIII\Rules\Triggers\BudgetIs', ], 'rule-actions' => [ 'set_category' => 'FireflyIII\Rules\Actions\SetCategory', diff --git a/tests/acceptance/NaughtyListTest.php b/tests/acceptance/NaughtyListTest.php deleted file mode 100644 index 60a80ff11d..0000000000 --- a/tests/acceptance/NaughtyListTest.php +++ /dev/null @@ -1,115 +0,0 @@ -where('user_id', $this->user()->id) - ->whereNull('deleted_at') - ->first(); - $this->session(['transactions.mass-edit.url' => 'http://localhost']); - - $data = [ - 'journals' => [$deposit->id], - 'description' => [$deposit->id => $description], - 'amount' => [$deposit->id => 1600], - 'amount_currency_id_amount_' . $deposit->id => 1, - 'date' => [$deposit->id => '2014-07-24'], - 'source_account_name' => [$deposit->id => 'Job'], - 'destination_account_id' => [$deposit->id => 1], - 'category' => [$deposit->id => 'Salary'], - ]; - - $this->be($this->user()); - $response = $this->call('post', route('transactions.mass.update', [$deposit->id]), $data); - $this->assertNotEquals($response->getStatusCode(), 500); - - // visit them should show updated content - $this->call('get', route('transactions.show', [$deposit->id])); - $this->assertResponseOk(); - $this->see($description); - } - - /** - * @covers \FireflyIII\Http\Controllers\Transaction\SingleController::store - * @dataProvider naughtyStringProvider - * - * @param string $description - */ - public function testSingleStore(string $description) - { - $this->session(['transactions.create.url' => 'http://localhost']); - $this->be($this->user()); - - $data = [ - 'what' => 'withdrawal', - 'amount' => '10', - 'amount_currency_id_amount' => 1, - 'source_account_id' => 1, - 'destination_account_name' => 'Some destination', - 'date' => '2016-01-01', - 'description' => $description, - ]; - $response = $this->call('post', route('transactions.store', ['withdrawal']), $data); - $this->assertNotEquals($response->getStatusCode(), 500); - - } - - /** - * @covers \FireflyIII\Http\Controllers\Transaction\SingleController::update - * @dataProvider naughtyStringProvider - * - * @param string $description - */ - public function testSingleUpdate(string $description) - { - $this->session(['transactions.edit.url' => 'http://localhost']); - $this->be($this->user()); - $data = [ - 'id' => 123, - 'what' => 'withdrawal', - 'description' => $description, - 'source_account_id' => 1, - 'destination_account_name' => 'PLUS', - 'amount' => '123', - 'amount_currency_id_amount' => 1, - 'budget_id' => 1, - 'category' => 'Daily groceries', - 'tags' => '', - 'date' => '2016-01-01', - ]; - - $response = $this->call('post', route('transactions.update', [123]), $data); - $this->assertNotEquals($response->getStatusCode(), 500); - } - -} \ No newline at end of file