Files
firefly-iii/tests/Feature/Controllers/Account/ReconcileControllerTest.php

317 lines
12 KiB
PHP
Raw Normal View History

2017-12-17 18:23:10 +01:00
<?php
/**
* ReconcileControllerTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Feature\Controllers\Account;
2018-02-28 15:50:00 +01:00
use FireflyIII\Models\Account;
2017-12-17 18:23:10 +01:00
use FireflyIII\Models\Transaction;
2018-02-28 15:50:00 +01:00
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Support\Collection;
2018-02-28 20:18:47 +01:00
use Log;
2018-03-28 19:38:20 +02:00
use Mockery;
2018-08-06 19:14:30 +02:00
use Tests\TestCase;
2017-12-17 18:23:10 +01:00
/**
* Class ConfigurationControllerTest
*/
class ReconcileControllerTest extends TestCase
{
2018-02-28 20:18:47 +01:00
/**
*
*/
2018-07-14 11:45:05 +02:00
public function setUp(): void
2018-02-28 20:18:47 +01:00
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
2018-02-28 20:18:47 +01:00
}
2017-12-17 18:23:10 +01:00
/**
2018-07-01 09:27:22 +02:00
* Test editing a reconciliation.
*
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
2017-12-17 18:23:10 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testEdit(): void
2017-12-17 18:23:10 +01:00
{
2018-02-28 15:50:00 +01:00
$repository = $this->mock(JournalRepositoryInterface::class);
$journal = $this->user()->transactionJournals()->where('transaction_type_id', 5)->first();
$transaction = $journal->transactions()->where('amount', '>', 0)->first();
$repository->shouldReceive('firstNull')->andReturn($journal);
2018-02-28 15:50:00 +01:00
$repository->shouldReceive('getFirstPosTransaction')->andReturn($transaction);
$repository->shouldReceive('getJournalDate')->andReturn('2018-01-01');
$repository->shouldReceive('getJournalCategoryName')->andReturn('');
$repository->shouldReceive('getJournalBudgetid')->andReturn(0);
2017-12-17 18:23:10 +01:00
$this->be($this->user());
$response = $this->get(route('accounts.reconcile.edit', [$journal->id]));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
2018-07-01 09:27:22 +02:00
* Test the redirect if journal is not a reconciliation.
*
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
2017-12-17 18:23:10 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testEditRedirect(): void
2017-12-17 18:23:10 +01:00
{
$journal = $this->user()->transactionJournals()->where('transaction_type_id', '!=', 5)->first();
$this->be($this->user());
$response = $this->get(route('accounts.reconcile.edit', [$journal->id]));
$response->assertStatus(302);
$response->assertRedirect(route('transactions.edit', [$journal->id]));
}
/**
2018-07-01 09:27:22 +02:00
* Test showing the reconciliation.
*
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
2017-12-17 18:23:10 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testReconcile(): void
2017-12-17 18:23:10 +01:00
{
2018-02-28 15:50:00 +01:00
$repository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('findNull')->once()->andReturn(TransactionCurrency::find(1));
2017-12-17 18:23:10 +01:00
$this->be($this->user());
$response = $this->get(route('accounts.reconcile', [1, '20170101', '20170131']));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
2018-07-01 09:27:22 +02:00
* Test showing the reconciliation (its a initial balance).
*
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
2017-12-17 18:23:10 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testReconcileInitialBalance(): void
2017-12-17 18:23:10 +01:00
{
$transaction = Transaction::leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
->where('accounts.user_id', $this->user()->id)->where('accounts.account_type_id', 6)->first(['account_id']);
$this->be($this->user());
$response = $this->get(route('accounts.reconcile', [$transaction->account_id, '20170101', '20170131']));
$response->assertStatus(302);
}
/**
2018-07-01 09:27:22 +02:00
* Test reconcile view (without date info).
*
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
2017-12-17 18:23:10 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testReconcileNoDates(): void
2017-12-17 18:23:10 +01:00
{
2018-02-28 15:50:00 +01:00
$repository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('findNull')->once()->andReturn(TransactionCurrency::find(1));
2017-12-17 18:23:10 +01:00
$this->be($this->user());
$response = $this->get(route('accounts.reconcile', [1]));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
2018-07-01 09:27:22 +02:00
* Test reconcile view (without end date).
*
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
2017-12-17 18:23:10 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testReconcileNoEndDate(): void
2017-12-17 18:23:10 +01:00
{
2018-02-28 15:50:00 +01:00
$repository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('findNull')->once()->andReturn(TransactionCurrency::find(1));
2017-12-17 18:23:10 +01:00
$this->be($this->user());
$response = $this->get(route('accounts.reconcile', [1, '20170101']));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
2018-07-01 09:27:22 +02:00
* Test reconcile view when account is not an asset.
*
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
2017-12-17 18:23:10 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testReconcileNotAsset(): void
2017-12-17 18:23:10 +01:00
{
$account = $this->user()->accounts()->where('account_type_id', '!=', 6)->where('account_type_id', '!=', 3)->first();
$this->be($this->user());
$response = $this->get(route('accounts.reconcile', [$account->id, '20170101', '20170131']));
$response->assertStatus(302);
}
/**
2018-07-01 09:27:22 +02:00
* Test show for actual reconciliation.
*
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
2017-12-17 18:23:10 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testShow(): void
2017-12-17 18:23:10 +01:00
{
2018-02-28 15:50:00 +01:00
$journal = $this->user()->transactionJournals()->where('transaction_type_id', 5)->first();
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
$repository->shouldReceive('getAssetTransaction')->once()->andReturn($journal->transactions()->first());
2017-12-17 18:23:10 +01:00
$this->be($this->user());
$response = $this->get(route('accounts.reconcile.show', [$journal->id]));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
2018-08-30 20:58:07 +02:00
/**
* Test show for actual reconciliation.
*
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
*/
public function testShowError(): void
{
$journal = $this->user()->transactionJournals()->where('transaction_type_id', 5)->first();
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('firstNull')->andReturn(new TransactionJournal);
$repository->shouldReceive('getAssetTransaction')->once()->andReturnNull();
$this->be($this->user());
$response = $this->get(route('accounts.reconcile.show', [$journal->id]));
$response->assertStatus(500);
// has bread crumb
$response->assertSee('The transaction data is incomplete. This is probably a bug. Apologies.');
}
2017-12-17 18:23:10 +01:00
/**
2018-07-01 09:27:22 +02:00
* Test show for actual reconciliation, but its not a reconciliation.
*
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
2017-12-17 18:23:10 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testShowSomethingElse(): void
2017-12-17 18:23:10 +01:00
{
$journal = $this->user()->transactionJournals()->where('transaction_type_id', '!=', 5)->first();
$this->be($this->user());
$response = $this->get(route('accounts.reconcile.show', [$journal->id]));
$response->assertStatus(302);
$response->assertRedirect(route('transactions.show', [$journal->id]));
}
/**
2018-07-01 09:27:22 +02:00
* Submit reconciliation.
*
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\ReconciliationStoreRequest
2017-12-17 18:23:10 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testSubmit(): void
2017-12-17 18:23:10 +01:00
{
2018-02-28 15:50:00 +01:00
$repository = $this->mock(AccountRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
$journalRepos->shouldReceive('reconcileById')->andReturn(true);
$journalRepos->shouldReceive('store')->andReturn(new TransactionJournal);
$repository->shouldReceive('getReconciliation')->andReturn(new Account);
$repository->shouldReceive('findNull')->andReturn(new Account);
2018-08-06 19:14:30 +02:00
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
2018-03-28 19:38:20 +02:00
2017-12-17 18:23:10 +01:00
$data = [
'transactions' => [1, 2, 3],
'reconcile' => 'create',
'difference' => '5',
2018-02-24 09:18:01 +01:00
'start' => '20170101',
2017-12-17 18:23:10 +01:00
'end' => '20170131',
];
$this->be($this->user());
$response = $this->post(route('accounts.reconcile.submit', [1, '20170101', '20170131']), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\ReconciliationUpdateRequest
2017-12-17 18:23:10 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testUpdate(): void
2017-12-17 18:23:10 +01:00
{
2018-02-28 15:50:00 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
$journalRepos->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([new Account]));
$journalRepos->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([new Account]));
$journalRepos->shouldReceive('getNoteText')->andReturn('');
$journalRepos->shouldReceive('update')->once();
2017-12-17 18:23:10 +01:00
$journal = $this->user()->transactionJournals()->where('transaction_type_id', 5)->first();
$data = [
'amount' => '5',
];
$this->be($this->user());
$response = $this->post(route('accounts.reconcile.update', [$journal->id]), $data);
$response->assertStatus(302);
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\ReconciliationUpdateRequest
2017-12-17 18:23:10 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testUpdateNotReconcile(): void
2017-12-17 18:23:10 +01:00
{
$journal = $this->user()->transactionJournals()->where('transaction_type_id', '!=', 5)->first();
$data = [
'amount' => '5',
];
$this->be($this->user());
$response = $this->post(route('accounts.reconcile.update', [$journal->id]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('transactions.show', [$journal->id]));
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\ReconciliationUpdateRequest
2017-12-17 18:23:10 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testUpdateZero(): void
2017-12-17 18:23:10 +01:00
{
$journal = $this->user()->transactionJournals()->where('transaction_type_id', 5)->first();
$data = [
'amount' => '0',
];
$this->be($this->user());
$response = $this->post(route('accounts.reconcile.update', [$journal->id]), $data);
$response->assertStatus(302);
$response->assertSessionHas('error');
}
2018-02-28 15:50:00 +01:00
2017-12-22 18:32:43 +01:00
}