firefly-iii/tests/Feature/Controllers/Transaction/SingleControllerTest.php

888 lines
45 KiB
PHP
Raw Normal View History

2017-02-12 11:40:39 -06:00
<?php
/**
* SingleControllerTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* 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
2017-12-17 07:42:21 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-02-12 11:40:39 -06:00
*/
2017-06-28 11:05:38 -05:00
declare(strict_types=1);
2017-02-12 11:40:39 -06:00
namespace Tests\Feature\Controllers\Transaction;
2018-03-07 03:18:22 -06:00
use Amount;
2017-03-03 23:53:46 -06:00
use DB;
2018-02-28 08:50:00 -06:00
use Exception;
2017-03-03 23:53:46 -06:00
use FireflyIII\Events\StoredTransactionJournal;
use FireflyIII\Events\UpdatedTransactionJournal;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Models\AccountType;
2017-12-23 10:42:07 -06:00
use FireflyIII\Models\Note;
2017-03-03 23:53:46 -06:00
use FireflyIII\Models\Transaction;
2018-03-07 03:18:22 -06:00
use FireflyIII\Models\TransactionCurrency;
2017-02-12 11:40:39 -06:00
use FireflyIII\Models\TransactionJournal;
2017-03-03 23:53:46 -06:00
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2018-02-28 08:50:00 -06:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2017-02-12 11:40:39 -06:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2018-02-28 08:50:00 -06:00
use FireflyIII\Repositories\Journal\JournalTaskerInterface;
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
2017-03-03 23:53:46 -06:00
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
2017-06-28 11:05:38 -05:00
use Illuminate\Database\Query\JoinClause;
2017-03-03 23:53:46 -06:00
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
2017-03-17 10:34:57 -05:00
use Steam;
2017-02-12 11:40:39 -06:00
use Tests\TestCase;
/**
* Class SingleControllerTest
*
2017-08-12 03:27:45 -05:00
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2017-02-12 11:40:39 -06:00
*/
class SingleControllerTest extends TestCase
{
2017-03-03 23:53:46 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::cloneTransaction
*/
public function testCloneTransaction()
{
2018-02-28 08:50:00 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$account = $this->user()->accounts()->first();
$journalRepos->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$account]));
$journalRepos->shouldReceive('getJournalBudgetId')->andReturn(0);
$journalRepos->shouldReceive('getJournalCategoryName')->andReturn('');
$journalRepos->shouldReceive('getTags')->andReturn([]);
2017-12-23 10:42:07 -06:00
$note = new Note();
$note->id = 5;
$note->text = 'I see you...';
2018-02-28 08:50:00 -06:00
$journalRepos->shouldReceive('getNote')->andReturn($note)->once();
2017-12-23 10:42:07 -06:00
2017-03-03 23:53:46 -06:00
$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.clone', [$withdrawal->id]));
$response->assertStatus(302);
}
2017-02-12 11:40:39 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::create
2017-02-17 13:14:38 -06:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::__construct
2017-12-23 10:42:07 -06:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::groupedActiveAccountList
2017-02-12 11:40:39 -06:00
*/
public function testCreate()
{
2018-02-28 08:50:00 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-03-17 10:34:57 -05:00
Steam::shouldReceive('phpBytes')->andReturn(2048);
2017-03-03 23:53:46 -06:00
$budgetRepos->shouldReceive('getActiveBudgets')->andReturn(new Collection)->once();
$piggyRepos->shouldReceive('getPiggyBanksWithAmount')->andReturn(new Collection)->once();
2018-03-07 03:18:22 -06:00
2017-02-12 11:40:39 -06:00
$this->be($this->user());
$response = $this->get(route('transactions.create', ['withdrawal']));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
2018-03-03 07:24:06 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::create
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::__construct
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::groupedActiveAccountList
*/
public function testCreateDepositWithSource()
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
Steam::shouldReceive('phpBytes')->andReturn(2048);
$budgetRepos->shouldReceive('getActiveBudgets')->andReturn(new Collection)->once();
$piggyRepos->shouldReceive('getPiggyBanksWithAmount')->andReturn(new Collection)->once();
$this->be($this->user());
$response = $this->get(route('transactions.create', ['deposit']) . '?source=1');
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::create
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::__construct
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::groupedActiveAccountList
*/
public function testCreateWithSource()
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
Steam::shouldReceive('phpBytes')->andReturn(2048);
$budgetRepos->shouldReceive('getActiveBudgets')->andReturn(new Collection)->once();
$piggyRepos->shouldReceive('getPiggyBanksWithAmount')->andReturn(new Collection)->once();
$this->be($this->user());
$response = $this->get(route('transactions.create', ['withdrawal']) . '?source=1');
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
2017-02-12 11:40:39 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::delete
*/
public function testDelete()
{
2018-02-28 08:50:00 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 11:40:39 -06:00
$this->be($this->user());
2017-03-03 23:53:46 -06:00
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->whereNull('deleted_at')->where('user_id', $this->user()->id)->first();
$response = $this->get(route('transactions.delete', [$withdrawal->id]));
2017-02-12 11:40:39 -06:00
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::destroy
*/
public function testDestroy()
{
2018-02-28 08:50:00 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
$journalRepos->shouldReceive('destroy')->once();
2017-03-04 08:29:20 -06:00
2017-03-18 14:53:44 -05:00
$this->session(['transactions.delete.uri' => 'http://localhost']);
2017-03-04 08:29:20 -06:00
$this->be($this->user());
2017-03-03 23:53:46 -06:00
$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]));
2017-02-12 11:40:39 -06:00
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::edit
2017-09-03 09:06:37 -05:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::groupedAccountList
2017-12-23 15:02:54 -06:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::isSplitJournal
2017-02-12 11:40:39 -06:00
*/
public function testEdit()
{
2018-02-28 08:50:00 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$account = $this->user()->accounts()->first();
2017-03-03 23:53:46 -06:00
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection)->once();
2018-02-28 08:50:00 -06:00
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
2018-02-25 10:38:24 -06:00
$journalRepos->shouldReceive('countTransactions')->andReturn(2)->once();
$journalRepos->shouldReceive('getTransactionType')->andReturn('Withdrawal')->once();
$journalRepos->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$account]))->once();
$journalRepos->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$account]))->once();
$journalRepos->shouldReceive('getNoteText')->andReturn('Some Note')->once();
$journalRepos->shouldReceive('getFirstPosTransaction')->andReturn(new Transaction)->once();
$journalRepos->shouldReceive('getJournalDate')->withAnyArgs()->andReturn('2017-09-01');
$journalRepos->shouldReceive('getMetaField')->withAnyArgs()->andReturn('')->once();
$journalRepos->shouldReceive('getJournalCategoryName')->once()->andReturn('');
$journalRepos->shouldReceive('getJournalBudgetId')->once()->andReturn(0);
$journalRepos->shouldReceive('getTags')->once()->andReturn([]);
2017-12-23 10:42:07 -06:00
2018-03-07 03:18:22 -06:00
// mock new account list:
$currency = TransactionCurrency::first();
$accountRepos->shouldReceive('getAccountsByType')
->withArgs([[AccountType::ASSET, AccountType::DEFAULT]])->andReturn(new Collection([$account]))->once();
Amount::shouldReceive('getDefaultCurrency')->andReturn($currency)->times(6);
2017-03-03 23:53:46 -06:00
$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]));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::edit
2017-09-03 09:06:37 -05:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::groupedAccountList
2017-03-03 23:53:46 -06:00
*/
public function testEditCashDeposit()
{
2018-02-28 08:50:00 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
2017-03-03 23:53:46 -06:00
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection)->once();
2018-02-28 08:50:00 -06:00
$account = $this->user()->accounts()->first();
$cash = $this->user()->accounts()->where('account_type_id', 2)->first();
2018-02-25 10:38:24 -06:00
$journalRepos->shouldReceive('countTransactions')->andReturn(2)->once();
$journalRepos->shouldReceive('getTransactionType')->andReturn('Deposit')->once();
$journalRepos->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$cash]))->once();
$journalRepos->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$account]))->once();
$journalRepos->shouldReceive('getNoteText')->andReturn('Some Note')->once();
$journalRepos->shouldReceive('getFirstPosTransaction')->andReturn(new Transaction)->once();
$journalRepos->shouldReceive('getJournalDate')->withAnyArgs()->andReturn('2017-09-01');
$journalRepos->shouldReceive('getMetaField')->withAnyArgs()->andReturn('')->once();
$journalRepos->shouldReceive('getJournalCategoryName')->once()->andReturn('');
$journalRepos->shouldReceive('getJournalBudgetId')->once()->andReturn(0);
$journalRepos->shouldReceive('getTags')->once()->andReturn([]);
2017-03-03 23:53:46 -06:00
$this->be($this->user());
2018-02-25 10:38:24 -06:00
$deposit = Transaction::leftJoin('accounts', 'transactions.account_id', '=', 'accounts.id')
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('accounts.account_type_id', 2)
->where('transaction_journals.transaction_type_id', 2)
->whereNull('transaction_journals.deleted_at')
->where('transaction_journals.user_id', $this->user()->id)->first(['transactions.*']);
2018-03-07 03:18:22 -06:00
2018-02-25 10:38:24 -06:00
$response = $this->get(route('transactions.edit', [$deposit->transaction_journal_id]));
2017-03-03 23:53:46 -06:00
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
$response->assertSee(' name="source_account_name" type="text" value="">');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::edit
2017-09-03 09:06:37 -05:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::groupedAccountList
2017-03-03 23:53:46 -06:00
*/
public function testEditCashWithdrawal()
{
2018-02-28 08:50:00 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
2017-03-03 23:53:46 -06:00
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection)->once();
2018-02-28 08:50:00 -06:00
$account = $this->user()->accounts()->first();
$cash = $this->user()->accounts()->where('account_type_id', 2)->first();
2018-02-25 10:38:24 -06:00
$journalRepos->shouldReceive('countTransactions')->andReturn(2)->once();
$journalRepos->shouldReceive('getTransactionType')->andReturn('Withdrawal')->once();
$journalRepos->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$account]))->once();
$journalRepos->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$cash]))->once();
$journalRepos->shouldReceive('getNoteText')->andReturn('Some Note')->once();
$journalRepos->shouldReceive('getFirstPosTransaction')->andReturn(new Transaction)->once();
$journalRepos->shouldReceive('getJournalDate')->withAnyArgs()->andReturn('2017-09-01');
$journalRepos->shouldReceive('getMetaField')->withAnyArgs()->andReturn('')->once();
$journalRepos->shouldReceive('getJournalCategoryName')->once()->andReturn('');
$journalRepos->shouldReceive('getJournalBudgetId')->once()->andReturn(0);
$journalRepos->shouldReceive('getTags')->once()->andReturn([]);
2017-02-12 11:40:39 -06:00
$this->be($this->user());
2017-03-03 23:53:46 -06:00
$withdrawal = Transaction::leftJoin('accounts', 'transactions.account_id', '=', 'accounts.id')
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('accounts.account_type_id', 2)
->where('transaction_journals.transaction_type_id', 1)
->whereNull('transaction_journals.deleted_at')
2018-02-25 10:38:24 -06:00
->where('transaction_journals.user_id', $this->user()->id)->first(['transactions.*']);
2018-02-28 08:50:00 -06:00
$response = $this->get(route('transactions.edit', [$withdrawal->transaction_journal_id]));
2018-02-25 10:38:24 -06:00
2017-02-12 11:40:39 -06:00
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
2017-03-03 23:53:46 -06:00
$response->assertSee(' name="destination_account_name" type="text" value="">');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::edit
2017-09-03 09:06:37 -05:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::groupedAccountList
2017-03-03 23:53:46 -06:00
*/
2017-12-29 02:05:35 -06:00
public function testEditReconcile()
2017-03-03 23:53:46 -06:00
{
2018-02-28 08:50:00 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('getTransactionType')->andReturn('Reconciliation')->once();
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
2018-02-25 10:38:24 -06:00
2017-03-03 23:53:46 -06:00
$this->be($this->user());
2018-02-25 10:38:24 -06:00
$reconcile = TransactionJournal::where('transaction_type_id', 5)
->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')]);
$response = $this->get(route('transactions.edit', [$reconcile->id]));
2017-03-03 23:53:46 -06:00
$response->assertStatus(302);
2017-02-12 11:40:39 -06:00
}
2017-12-23 10:42:07 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::edit
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::groupedAccountList
*/
2017-12-29 02:05:35 -06:00
public function testEditRedirect()
2017-12-23 10:42:07 -06:00
{
2018-02-28 08:50:00 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
2018-02-25 10:38:24 -06:00
2017-12-23 10:42:07 -06:00
$this->be($this->user());
2017-12-29 02:05:35 -06:00
$withdrawal = TransactionJournal::where('transaction_type_id', 1)
2017-12-23 10:42:07 -06:00
->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')]);
2018-02-28 08:50:00 -06:00
$journalRepos->shouldReceive('getTransactionType')->andReturn('Withdrawal');
$journalRepos->shouldReceive('countTransactions')->andReturn(3);
2018-02-25 10:38:24 -06:00
$response = $this->get(route('transactions.edit', [$withdrawal->id]));
2017-12-23 10:42:07 -06:00
$response->assertStatus(302);
}
2018-03-03 07:24:06 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::edit
*/
public function testEditRedirectOpening()
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
$this->be($this->user());
$journalRepos->shouldReceive('getTransactionType')->andReturn('Opening balance');
$journalRepos->shouldReceive('countTransactions')->andReturn(3);
$response = $this->get(route('transactions.edit', [1]));
$response->assertStatus(302);
}
2017-06-28 11:05:38 -05:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::edit
2017-09-03 09:06:37 -05:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::groupedAccountList
2017-06-28 11:05:38 -05:00
*/
public function testEditTransferWithForeignAmount()
{
2018-02-28 08:50:00 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
2017-06-28 11:05:38 -05:00
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection)->once();
$this->be($this->user());
$withdrawal = TransactionJournal::where('transaction_type_id', 3)
->whereNull('transaction_journals.deleted_at')
->leftJoin(
'transactions', function (JoinClause $join) {
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('amount', '<', 0);
}
)
->where('user_id', $this->user()->id)
->whereNotNull('transactions.foreign_amount')
->first(['transaction_journals.*']);
2018-02-25 10:38:24 -06:00
2018-02-28 08:50:00 -06:00
$account = $this->user()->accounts()->first();
2018-02-25 10:38:24 -06:00
$journalRepos->shouldReceive('countTransactions')->andReturn(2)->once();
$journalRepos->shouldReceive('getTransactionType')->andReturn('Transfer')->once();
$journalRepos->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$account]))->once();
$journalRepos->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$account]))->once();
$journalRepos->shouldReceive('getNoteText')->andReturn('Some Note')->once();
$journalRepos->shouldReceive('getFirstPosTransaction')->andReturn(new Transaction)->once();
$journalRepos->shouldReceive('getJournalDate')->withAnyArgs()->andReturn('2017-09-01');
$journalRepos->shouldReceive('getMetaField')->withAnyArgs()->andReturn('')->once();
$journalRepos->shouldReceive('getJournalCategoryName')->once()->andReturn('');
$journalRepos->shouldReceive('getJournalBudgetId')->once()->andReturn(0);
$journalRepos->shouldReceive('getTags')->once()->andReturn([]);
$response = $this->get(route('transactions.edit', [$withdrawal->id]));
2017-06-28 11:05:38 -05:00
$response->assertStatus(200);
2018-03-03 07:24:06 -06:00
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::edit
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::groupedAccountList
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::isSplitJournal
*/
public function testEditWithForeign()
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$account = $this->user()->accounts()->first();
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection)->once();
$transaction = new Transaction;
$transaction->foreign_amount = '1';
$transaction->foreign_currency_id = 2;
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
$journalRepos->shouldReceive('countTransactions')->andReturn(2)->once();
$journalRepos->shouldReceive('getTransactionType')->andReturn('Withdrawal')->once();
$journalRepos->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$account]))->once();
$journalRepos->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$account]))->once();
$journalRepos->shouldReceive('getNoteText')->andReturn('Some Note')->once();
$journalRepos->shouldReceive('getFirstPosTransaction')->andReturn($transaction)->once();
$journalRepos->shouldReceive('getJournalDate')->withAnyArgs()->andReturn('2017-09-01');
$journalRepos->shouldReceive('getMetaField')->withAnyArgs()->andReturn('')->once();
$journalRepos->shouldReceive('getJournalCategoryName')->once()->andReturn('');
$journalRepos->shouldReceive('getJournalBudgetId')->once()->andReturn(0);
$journalRepos->shouldReceive('getTags')->once()->andReturn([]);
$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]));
$response->assertStatus(200);
2017-06-28 11:05:38 -05:00
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::edit
2017-09-03 09:06:37 -05:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::groupedAccountList
2017-06-28 11:05:38 -05:00
*/
public function testEditWithForeignAmount()
{
2018-02-28 08:50:00 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
2017-06-28 11:05:38 -05:00
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection)->once();
$this->be($this->user());
$withdrawal = TransactionJournal::where('transaction_type_id', 1)
->whereNull('transaction_journals.deleted_at')
->leftJoin(
'transactions', function (JoinClause $join) {
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('amount', '<', 0);
}
)
->where('user_id', $this->user()->id)
->whereNotNull('transactions.foreign_amount')
->first(['transaction_journals.*']);
2018-02-25 10:38:24 -06:00
2018-02-28 08:50:00 -06:00
$account = $this->user()->accounts()->first();
2018-02-25 10:38:24 -06:00
$journalRepos->shouldReceive('countTransactions')->andReturn(2)->once();
$journalRepos->shouldReceive('getTransactionType')->andReturn('Withdrawal')->once();
$journalRepos->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$account]))->once();
$journalRepos->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$account]))->once();
$journalRepos->shouldReceive('getNoteText')->andReturn('Some Note')->once();
$journalRepos->shouldReceive('getFirstPosTransaction')->andReturn(new Transaction)->once();
$journalRepos->shouldReceive('getJournalDate')->withAnyArgs()->andReturn('2017-09-01');
$journalRepos->shouldReceive('getMetaField')->withAnyArgs()->andReturn('')->once();
$journalRepos->shouldReceive('getJournalCategoryName')->once()->andReturn('');
$journalRepos->shouldReceive('getJournalBudgetId')->once()->andReturn(0);
$journalRepos->shouldReceive('getTags')->once()->andReturn([]);
$response = $this->get(route('transactions.edit', [$withdrawal->id]));
2017-06-28 11:05:38 -05:00
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
2017-02-12 11:40:39 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::store
2017-09-03 09:06:37 -05:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::groupedActiveAccountList
2018-03-03 10:16:47 -06:00
* @covers \FireflyIII\Http\Requests\JournalFormRequest
2017-02-12 11:40:39 -06:00
*/
2017-03-05 04:19:06 -06:00
public function testStoreError()
2017-02-12 11:40:39 -06:00
{
2018-02-28 08:50:00 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
2017-03-03 23:53:46 -06:00
// mock results:
$journal = new TransactionJournal();
$journal->description = 'New journal';
2018-02-28 08:50:00 -06:00
$journalRepos->shouldReceive('store')->andReturn($journal);
2017-03-18 14:53:44 -05:00
$this->session(['transactions.create.uri' => 'http://localhost']);
2017-02-12 11:40:39 -06:00
$this->be($this->user());
2017-03-03 23:53:46 -06:00
$data = [
2017-02-12 11:40:39 -06:00
'what' => 'withdrawal',
'amount' => '10',
'amount_currency_id_amount' => 1,
'source_account_id' => 1,
'destination_account_name' => 'Some destination',
'date' => '2016-01-01',
'description' => 'Test descr',
];
$response = $this->post(route('transactions.store', ['withdrawal']), $data);
$response->assertStatus(302);
2017-03-03 23:53:46 -06:00
$response->assertSessionHas('error');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::store
2017-11-15 04:33:07 -06:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::groupedActiveAccountList
2018-03-03 10:16:47 -06:00
* @covers \FireflyIII\Http\Requests\JournalFormRequest
2017-03-03 23:53:46 -06:00
*/
2017-03-05 04:19:06 -06:00
public function testStoreSuccess()
2017-03-03 23:53:46 -06:00
{
2018-02-28 08:50:00 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
2017-03-03 23:53:46 -06:00
// mock results:
$journal = new TransactionJournal();
2017-03-05 04:19:06 -06:00
$journal->id = 1000;
2017-03-03 23:53:46 -06:00
$journal->description = 'New journal';
2018-02-28 08:50:00 -06:00
$journalRepos->shouldReceive('store')->andReturn($journal);
2017-03-05 04:19:06 -06:00
$this->expectsEvents(StoredTransactionJournal::class);
$errors = new MessageBag;
$errors->add('attachments', 'Fake error');
$messages = new MessageBag;
$messages->add('attachments', 'Fake error');
// mock attachment helper, trigger an error AND and info thing.
2018-02-28 08:50:00 -06:00
$attRepos->shouldReceive('saveAttachmentsForModel');
$attRepos->shouldReceive('getErrors')->andReturn($errors);
$attRepos->shouldReceive('getMessages')->andReturn($messages);
2017-03-05 04:19:06 -06:00
2017-03-18 14:53:44 -05:00
$this->session(['transactions.create.uri' => 'http://localhost']);
2017-03-03 23:53:46 -06:00
$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' => 'Test descr',
];
$response = $this->post(route('transactions.store', ['withdrawal']), $data);
$response->assertStatus(302);
2017-03-05 04:19:06 -06:00
$response->assertSessionHas('success');
2017-03-03 23:53:46 -06:00
$response->assertSessionHas('error');
2017-03-05 04:19:06 -06:00
$response->assertSessionHas('info');
2017-02-12 11:40:39 -06:00
}
2018-03-03 10:16:47 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::store
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::groupedActiveAccountList
* @covers \FireflyIII\Http\Requests\JournalFormRequest
*/
public function testStoreSuccessDeposit()
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
// mock results:
$journal = new TransactionJournal();
$journal->id = 1000;
$journal->description = 'New deposit';
$journalRepos->shouldReceive('store')->andReturn($journal);
$this->expectsEvents(StoredTransactionJournal::class);
$errors = new MessageBag;
$errors->add('attachments', 'Fake error');
$messages = new MessageBag;
$messages->add('attachments', 'Fake error');
// mock attachment helper, trigger an error AND and info thing.
$attRepos->shouldReceive('saveAttachmentsForModel');
$attRepos->shouldReceive('getErrors')->andReturn($errors);
$attRepos->shouldReceive('getMessages')->andReturn($messages);
$this->session(['transactions.create.uri' => 'http://localhost']);
$this->be($this->user());
$data = [
'what' => 'deposit',
'amount' => '10',
'amount_currency_id_amount' => 1,
'destination_account_id' => 1,
'source_account_name' => 'Some source',
'date' => '2016-01-01',
'description' => 'Test descr',
];
$response = $this->post(route('transactions.store', ['deposit']), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
$response->assertSessionHas('error');
$response->assertSessionHas('info');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::store
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::groupedActiveAccountList
* @covers \FireflyIII\Http\Requests\JournalFormRequest
*/
public function testStoreSuccessTransfer()
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
// mock results:
$journal = new TransactionJournal();
$journal->id = 1000;
$journal->description = 'New transfer';
$journalRepos->shouldReceive('store')->andReturn($journal);
$this->expectsEvents(StoredTransactionJournal::class);
$errors = new MessageBag;
$errors->add('attachments', 'Fake error');
$messages = new MessageBag;
$messages->add('attachments', 'Fake error');
// mock attachment helper, trigger an error AND and info thing.
$attRepos->shouldReceive('saveAttachmentsForModel');
$attRepos->shouldReceive('getErrors')->andReturn($errors);
$attRepos->shouldReceive('getMessages')->andReturn($messages);
$this->session(['transactions.create.uri' => 'http://localhost']);
$this->be($this->user());
$data = [
'what' => 'transfer',
'amount' => '10',
'amount_currency_id_amount' => 1,
'destination_account_id' => 1,
'source_account_id' => 2,
'date' => '2016-01-01',
'description' => 'Test descr',
];
$response = $this->post(route('transactions.store', ['transfer']), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
$response->assertSessionHas('error');
$response->assertSessionHas('info');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::store
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::groupedActiveAccountList
* @covers \FireflyIII\Http\Requests\JournalFormRequest
*/
public function testStoreSuccessTransferForeign()
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
// mock results:
$journal = new TransactionJournal();
$journal->id = 1000;
$journal->description = 'New transfer';
$journalRepos->shouldReceive('store')->andReturn($journal);
$this->expectsEvents(StoredTransactionJournal::class);
$errors = new MessageBag;
$errors->add('attachments', 'Fake error');
$messages = new MessageBag;
$messages->add('attachments', 'Fake error');
// mock attachment helper, trigger an error AND and info thing.
$attRepos->shouldReceive('saveAttachmentsForModel');
$attRepos->shouldReceive('getErrors')->andReturn($errors);
$attRepos->shouldReceive('getMessages')->andReturn($messages);
$this->session(['transactions.create.uri' => 'http://localhost']);
$this->be($this->user());
$data = [
'what' => 'transfer',
'amount' => '10',
'amount_currency_id_amount' => 1,
'source_account_currency' => 1,
'destination_account_currency' => 2,
'destination_account_id' => 1,
'source_account_id' => 2,
'date' => '2016-01-01',
'description' => 'Test descr',
];
$response = $this->post(route('transactions.store', ['transfer']), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
$response->assertSessionHas('error');
$response->assertSessionHas('info');
}
2017-02-12 11:40:39 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController::update
2018-03-03 10:16:47 -06:00
* @covers \FireflyIII\Http\Requests\JournalFormRequest
2017-02-12 11:40:39 -06:00
*/
public function testUpdate()
{
2018-02-28 08:50:00 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$attRepos = $this->mock(AttachmentHelperInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$tasker = $this->mock(JournalTaskerInterface::class);
$linkRepos = $this->mock(LinkTypeRepositoryInterface::class);
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
$journalRepos->shouldReceive('getTransactionType')->andReturn('Withdrawal');
$linkRepos->shouldReceive('get')->andReturn(new Collection);
$linkRepos->shouldReceive('getLinks')->andReturn(new Collection);
$attRepos->shouldReceive('saveAttachmentsForModel');
$attRepos->shouldReceive('getErrors')->andReturn(new MessageBag);
$attRepos->shouldReceive('getMessages')->andReturn(new MessageBag);
$tasker->shouldReceive('getPiggyBankEvents')->andReturn(new Collection);
$tasker->shouldReceive('getTransactionsOverview')->andReturn([]);
2017-03-03 23:53:46 -06:00
2018-02-28 08:50:00 -06:00
// mock
try {
$this->expectsEvents(UpdatedTransactionJournal::class);
} catch (Exception $e) {
$this->assertTrue(false, 'expectsEvents failed!');
}
2017-03-03 23:53:46 -06:00
2018-02-28 08:50:00 -06:00
$journal = new TransactionJournal();
2017-03-05 04:19:06 -06:00
$type = TransactionType::find(1);
$journal->id = 1000;
$journal->description = 'New journal';
2017-03-03 23:53:46 -06:00
$journal->transactionType()->associate($type);
2018-02-28 08:50:00 -06:00
$journalRepos->shouldReceive('update')->andReturn($journal);
2017-03-03 23:53:46 -06:00
2017-03-18 14:53:44 -05:00
$this->session(['transactions.edit.uri' => 'http://localhost']);
2017-02-12 11:40:39 -06:00
$this->be($this->user());
$data = [
'id' => 123,
'what' => 'withdrawal',
'description' => 'Updated groceries',
'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->post(route('transactions.update', [123]), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
$response = $this->get(route('transactions.show', [123]));
$response->assertStatus(200);
$response->assertSee('Updated groceries');
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
2017-02-16 15:33:32 -06:00
}