Skip to content

Firefly III: a personal finances manager

Improved various tests.

James Colec206a95d55c6
diff --git a/app/Http/Controllers/Transaction/MassController.php b/app/Http/Controllers/Transaction/MassController.php
index c95544296f..0457d4d677 100644
--- a/app/Http/Controllers/Transaction/MassController.php
+++ b/app/Http/Controllers/Transaction/MassController.php
@@ -144,7 +144,7 @@ class MassController extends Controller
$filtered->push($journal);
}
- if (count($messages)) {
+ if (count($messages) > 0) {
Session::flash('info', $messages);
}
diff --git a/app/Rules/Factory/TriggerFactory.php b/app/Rules/Factory/TriggerFactory.php
index 9f1f76498e..fe0a23e355 100644
--- a/app/Rules/Factory/TriggerFactory.php
+++ b/app/Rules/Factory/TriggerFactory.php
@@ -45,8 +45,9 @@ class TriggerFactory
$triggerType = $trigger->trigger_type;
/** @var AbstractTrigger $class */
- $class = self::getTriggerClass($triggerType);
- $obj = $class::makeFromTriggerValue($trigger->trigger_value);
+ $class = self::getTriggerClass($triggerType);
+ $obj = $class::makeFromTriggerValue($trigger->trigger_value);
+ $obj->stopProcessing = $trigger->stop_processing;
Log::debug(sprintf('self::getTriggerClass("%s") = "%s"', $triggerType, $class));
Log::debug(sprintf('%s::makeFromTriggerValue(%s) = object of class "%s"', $class, $trigger->trigger_value, get_class($obj)));
diff --git a/app/Rules/Processor.php b/app/Rules/Processor.php
index 9b0daf0ae9..1bca777f62 100644
--- a/app/Rules/Processor.php
+++ b/app/Rules/Processor.php
@@ -69,6 +69,7 @@ final class Processor
$triggerSet = $rule->ruleTriggers()->orderBy('order', 'ASC')->get();
/** @var RuleTrigger $trigger */
foreach ($triggerSet as $trigger) {
+ Log::debug(sprintf('Push trigger %d', $trigger->id));
$self->triggers->push(TriggerFactory::getTrigger($trigger));
}
$self->actions = $rule->ruleActions()->orderBy('order', 'ASC')->get();
diff --git a/app/Support/Models/TransactionJournalTrait.php b/app/Support/Models/TransactionJournalTrait.php
index 7ae16ce215..98c74a416c 100644
--- a/app/Support/Models/TransactionJournalTrait.php
+++ b/app/Support/Models/TransactionJournalTrait.php
@@ -158,6 +158,7 @@ trait TransactionJournalTrait
foreach ($transactions as $t) {
$list->push($t->account);
}
+ $list = $list->unique('id');
$cache->store($list);
return $list;
@@ -232,6 +233,7 @@ trait TransactionJournalTrait
foreach ($transactions as $t) {
$list->push($t->account);
}
+ $list = $list->unique('id');
$cache->store($list);
return $list;
diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php
index 92cc9d698f..2235b32e67 100644
--- a/database/factories/ModelFactory.php
+++ b/database/factories/ModelFactory.php
@@ -36,9 +36,18 @@ $factory->define(
$factory->define(
FireflyIII\Models\Transaction::class, function (Faker\Generator $faker) {
return [
- 'transaction_amount' => strval($faker->randomFloat(2, -100, 100)),
- 'opposing_account_id' => $faker->numberBetween(1, 10),
- 'opposing_account_name' => $faker->words(3),
+ 'transaction_amount' => strval($faker->randomFloat(2, -100, 100)),
+ 'destination_amount' => strval($faker->randomFloat(2, -100, 100)),
+ 'opposing_account_id' => $faker->numberBetween(1, 10),
+ 'source_account_id' => $faker->numberBetween(1, 10),
+ 'opposing_account_name' => $faker->words(3, true),
+ 'description' => $faker->words(3, true),
+ 'source_account_name' => $faker->words(3, true),
+ 'destination_account_id' => $faker->numberBetween(1, 10),
+ 'destination_account_name' => $faker->words(3, true),
+ 'amount' => strval($faker->randomFloat(2, -100, 100)),
+ 'budget_id' => 0,
+ 'category' => $faker->words(3, true),
];
}
);
\ No newline at end of file
diff --git a/tests/Feature/Controllers/Transaction/ConvertControllerTest.php b/tests/Feature/Controllers/Transaction/ConvertControllerTest.php
index d0e5e2ab4c..dad0c6550e 100644
--- a/tests/Feature/Controllers/Transaction/ConvertControllerTest.php
+++ b/tests/Feature/Controllers/Transaction/ConvertControllerTest.php
@@ -13,10 +13,12 @@ namespace Tests\Feature\Controllers\Transaction;
use DB;
+use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
+use FireflyIII\Support\Twig\Transaction;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
use Tests\TestCase;
@@ -174,6 +176,9 @@ class ConvertControllerTest extends TestCase
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
+ $accountRepos = $this->mock(AccountRepositoryInterface::class);
+ $accountRepos->shouldReceive('find')->andReturn(new Account);
+
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
$data = ['source_account_asset' => 1];
$this->be($this->user());
@@ -194,6 +199,9 @@ class ConvertControllerTest extends TestCase
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
+ $accountRepos = $this->mock(AccountRepositoryInterface::class);
+ $accountRepos->shouldReceive('store')->andReturn(new Account);
+
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
$data = ['destination_account_expense' => 'New expense name.',];
$this->be($this->user());
@@ -288,6 +296,9 @@ class ConvertControllerTest extends TestCase
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
+ $accountRepos = $this->mock(AccountRepositoryInterface::class);
+ $accountRepos->shouldReceive('store')->andReturn(new Account);
+
$transfer = TransactionJournal::where('transaction_type_id', 3)->where('user_id', $this->user()->id)->first();
$data = ['source_account_revenue' => 'New rev'];
$this->be($this->user());
@@ -308,6 +319,9 @@ class ConvertControllerTest extends TestCase
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
+ $accountRepos = $this->mock(AccountRepositoryInterface::class);
+ $accountRepos->shouldReceive('store')->andReturn(new Account);
+
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first();
$data = ['source_account_revenue' => 'New revenue name.',];
$this->be($this->user());
@@ -328,6 +342,9 @@ class ConvertControllerTest extends TestCase
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
+ $accountRepos = $this->mock(AccountRepositoryInterface::class);
+ $accountRepos->shouldReceive('find')->andReturn(new Account);
+
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first();
$data = [
'destination_account_asset' => 2,
diff --git a/tests/Feature/Controllers/Transaction/MassControllerTest.php b/tests/Feature/Controllers/Transaction/MassControllerTest.php
index 09b1d6347e..607fa84792 100644
--- a/tests/Feature/Controllers/Transaction/MassControllerTest.php
+++ b/tests/Feature/Controllers/Transaction/MassControllerTest.php
@@ -12,9 +12,20 @@ declare(strict_types = 1);
namespace Tests\Feature\Controllers\Transaction;
+use DB;
+use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionJournal;
+use FireflyIII\Repositories\Account\AccountRepositoryInterface;
+use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
+use Illuminate\Support\Collection;
+use Log;
use Tests\TestCase;
+/**
+ * Class MassControllerTest
+ *
+ * @package Tests\Feature\Controllers\Transaction
+ */
class MassControllerTest extends TestCase
{
/**
@@ -37,31 +48,113 @@ class MassControllerTest extends TestCase
*/
public function testDestroy()
{
+ $deposits = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->take(2)->get();
+ $depositIds = $deposits->pluck('id')->toArray();
+
+ // mock deletion:
+ $repository = $this->mock(JournalRepositoryInterface::class);
+ $repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
+ $repository->shouldReceive('find')->andReturnValues([$deposits[0], $deposits[1]])->times(2);
+ $repository->shouldReceive('delete')->times(2);
+
$this->session(['transactions.mass-delete.url' => 'http://localhost']);
- $deposits = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->take(2)->get()->pluck('id')->toArray();
- $data = [
- 'confirm_mass_delete' => $deposits,
+
+ $data = [
+ 'confirm_mass_delete' => $depositIds,
];
$this->be($this->user());
$response = $this->post(route('transactions.mass.destroy'), $data);
$response->assertSessionHas('success');
$response->assertStatus(302);
+ }
- // visit them should give 404.
- $response = $this->get(route('transactions.show', [$deposits[0]]));
- $response->assertStatus(404);
+ /**
+ * @covers \FireflyIII\Http\Controllers\Transaction\MassController::edit
+ */
+ public function testEdit()
+ {
+ // mock stuff:
+ $repository = $this->mock(AccountRepositoryInterface::class);
+ $repository->shouldReceive('getAccountsByType')->once()->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection);
+ $transfers = TransactionJournal::where('transaction_type_id', 3)->where('user_id', $this->user()->id)->take(2)->get()->pluck('id')->toArray();
+ $this->be($this->user());
+ $response = $this->get(route('transactions.mass.edit', $transfers));
+ $response->assertStatus(200);
+ $response->assertSee('Edit a number of transactions');
+ // has bread crumb
+ $response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\MassController::edit
*/
- public function testEdit()
+ public function testEditMultipleNothingLeft()
{
- $transfers = TransactionJournal::where('transaction_type_id', 3)->where('user_id', $this->user()->id)->take(2)->get()->pluck('id')->toArray();
+ Log::debug('Edit multiple.');
+ // mock stuff:
+ $repository = $this->mock(AccountRepositoryInterface::class);
+ $repository->shouldReceive('getAccountsByType')->once()->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection);
+
+ // default transactions
+ $collection = new Collection;
+ $collection->push(
+ TransactionJournal::where('transaction_type_id', 1)
+ ->whereNull('transaction_journals.deleted_at')
+ ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
+ ->groupBy('transaction_journals.id')
+ ->orderBy('ct', 'DESC')
+ ->where('user_id', $this->user()->id)->first(['transaction_journals.id', DB::raw('count(transactions.`id`) as ct')])
+ );
+ $allIds = $collection->pluck('id')->toArray();
+ Log::debug('Ids', $allIds);
+
+ $this->be($this->user());
+ $response = $this->get(route('transactions.mass.edit', join(',', $allIds)));
+ $response->assertStatus(200);
+ $response->assertSee('Edit a number of transactions');
+ // has bread crumb
+ $response->assertSee('<ol class="breadcrumb">');
+ }
+
+ /**
+ * @covers \FireflyIII\Http\Controllers\Transaction\MassController::edit
+ */
+ public function testEditMultiple()
+ {
+ Log::debug('Edit multiple.');
+ // mock stuff:
+ $repository = $this->mock(AccountRepositoryInterface::class);
+ $repository->shouldReceive('getAccountsByType')->once()->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection);
+
+ // default transactions
+ $collection = TransactionJournal::where('transaction_type_id', 3)->where('user_id', $this->user()->id)->take(2)->get();
+
+ // add deposit (with multiple sources)
+ $collection->push(
+ TransactionJournal::where('transaction_type_id', 2)
+ ->whereNull('transaction_journals.deleted_at')
+ ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
+ ->groupBy('transaction_journals.id')
+ ->orderBy('ct', 'DESC')
+ ->where('user_id', $this->user()->id)->first(['transaction_journals.id', DB::raw('count(transactions.`id`) as ct')])
+ );
+
+ // add withdrawal (with multiple destinations)
+ $collection->push(
+ TransactionJournal::where('transaction_type_id', 1)
+ ->whereNull('transaction_journals.deleted_at')
+ ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
+ ->groupBy('transaction_journals.id')
+ ->orderBy('ct', 'DESC')
+ ->where('user_id', $this->user()->id)->first(['transaction_journals.id', DB::raw('count(transactions.`id`) as ct')])
+ );
+ $allIds = $collection->pluck('id')->toArray();
+ Log::debug('Ids', $allIds);
+
$this->be($this->user());
- $response = $this->get(route('transactions.mass.delete', $transfers));
+ $response = $this->get(route('transactions.mass.edit', join(',', $allIds)));
$response->assertStatus(200);
$response->assertSee('Edit a number of transactions');
// has bread crumb
@@ -76,6 +169,13 @@ class MassControllerTest extends TestCase
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)
->whereNull('deleted_at')
->first();
+ // mock stuff
+ $repository = $this->mock(JournalRepositoryInterface::class);
+ $repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
+ $repository->shouldReceive('update')->once();
+ $repository->shouldReceive('find')->once()->andReturn($deposit);
+
+
$this->session(['transactions.mass-edit.url' => 'http://localhost']);
$data = [
@@ -93,12 +193,5 @@ class MassControllerTest extends TestCase
$response = $this->post(route('transactions.mass.update', [$deposit->id]), $data);
$response->assertSessionHas('success');
$response->assertStatus(302);
-
- // visit them should show updated content
- $response = $this->get(route('transactions.show', [$deposit->id]));
- $response->assertStatus(200);
- $response->assertSee('Updated salary thing');
}
-
-
}
diff --git a/tests/Feature/Controllers/Transaction/SingleControllerTest.php b/tests/Feature/Controllers/Transaction/SingleControllerTest.php
index 4aa6c17a06..c95169840b 100644
--- a/tests/Feature/Controllers/Transaction/SingleControllerTest.php
+++ b/tests/Feature/Controllers/Transaction/SingleControllerTest.php
@@ -86,12 +86,13 @@ class SingleControllerTest extends TestCase
*/
public function testDestroy()
{
- $this->session(['transactions.delete.url' => 'http://localhost']);
- $this->be($this->user());
-
+ // mock
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('delete')->once();
+
+ $this->session(['transactions.delete.url' => 'http://localhost']);
+ $this->be($this->user());
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->whereNull('deleted_at')->where('user_id', $this->user()->id)->first();
$response = $this->post(route('transactions.destroy', [$withdrawal->id]));
$response->assertStatus(302);
@@ -109,7 +110,6 @@ class SingleControllerTest extends TestCase
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection)->once();
-
$this->be($this->user());
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->whereNull('deleted_at')->where('user_id', $this->user()->id)->first();
$response = $this->get(route('transactions.edit', [$withdrawal->id]));
diff --git a/tests/Feature/Controllers/Transaction/SplitControllerTest.php b/tests/Feature/Controllers/Transaction/SplitControllerTest.php
index d5fae24bcc..0cd3559ee4 100644
--- a/tests/Feature/Controllers/Transaction/SplitControllerTest.php
+++ b/tests/Feature/Controllers/Transaction/SplitControllerTest.php
@@ -12,12 +12,17 @@ declare(strict_types = 1);
namespace Tests\Feature\Controllers\Transaction;
+use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Models\AccountType;
+use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
+use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
+use FireflyIII\Repositories\Journal\JournalTaskerInterface;
use Illuminate\Support\Collection;
+use Illuminate\Support\MessageBag;
use Tests\TestCase;
/**
@@ -39,11 +44,14 @@ class SplitControllerTest extends TestCase
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$accountRepository = $this->mock(AccountRepositoryInterface::class);
$budgetRepository = $this->mock(BudgetRepositoryInterface::class);
+ $transactions = factory(Transaction::class, 3)->make();
+ $tasker = $this->mock(JournalTaskerInterface::class);
$currencyRepository->shouldReceive('get')->once()->andReturn(new Collection);
$accountRepository->shouldReceive('getAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])
->andReturn(new Collection)->once();
$budgetRepository->shouldReceive('getActiveBudgets')->andReturn(new Collection);
+ $tasker->shouldReceive('getTransactionsOverview')->andReturn($transactions->toArray());
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
@@ -92,6 +100,15 @@ class SplitControllerTest extends TestCase
],
],
];
+
+ // mock stuff
+ $repository = $this->mock(JournalRepositoryInterface::class);
+ $repository->shouldReceive('updateSplitJournal')->andReturn($deposit);
+ $repository->shouldReceive('first')->times(2)->andReturn(new TransactionJournal);
+ $attachmentRepos = $this->mock(AttachmentHelperInterface::class);
+ $attachmentRepos->shouldReceive('saveAttachmentsForModel');
+ $attachmentRepos->shouldReceive('getMessages')->andReturn(new MessageBag);
+
$this->be($this->user());
$response = $this->post(route('transactions.split.update', [$deposit->id]), $data);
$response->assertStatus(302);