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

1060 lines
52 KiB
PHP
Raw Normal View History

2017-02-12 18:40:39 +01:00
<?php
/**
* SingleControllerTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02: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 14:42:21 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-02-12 18:40:39 +01:00
*/
2017-06-28 18:05:38 +02:00
declare(strict_types=1);
2017-02-12 18:40:39 +01:00
namespace Tests\Feature\Controllers\Transaction;
2018-03-07 10:18:22 +01:00
use Amount;
2017-03-04 06:53:46 +01:00
use DB;
2018-02-28 15:50:00 +01:00
use Exception;
2017-03-04 06:53:46 +01:00
use FireflyIII\Events\StoredTransactionJournal;
use FireflyIII\Events\UpdatedTransactionJournal;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
2018-03-07 10:18:22 +01:00
use FireflyIII\Models\TransactionCurrency;
2017-02-12 18:40:39 +01:00
use FireflyIII\Models\TransactionJournal;
2017-03-04 06:53:46 +01:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2018-02-28 15:50:00 +01:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2017-02-12 18:40:39 +01:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2018-02-28 15:50:00 +01:00
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
2017-03-04 06:53:46 +01:00
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
2017-06-28 18:05:38 +02:00
use Illuminate\Database\Query\JoinClause;
2017-03-04 06:53:46 +01:00
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
2018-03-24 06:08:50 +01:00
use Log;
use Mockery;
2017-03-17 16:34:57 +01:00
use Steam;
2017-02-12 18:40:39 +01:00
use Tests\TestCase;
/**
* Class SingleControllerTest
*
2017-08-12 10:27:45 +02:00
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2017-02-12 18:40:39 +01:00
*/
class SingleControllerTest extends TestCase
{
2018-03-24 06:08:50 +01:00
/**
*
*/
2018-09-02 20:27:26 +02:00
public function setUp(): void
2018-03-24 06:08:50 +01:00
{
parent::setUp();
2018-09-03 18:52:46 +02:00
Log::info(sprintf('Now in %s.', \get_class($this)));
2018-03-24 06:08:50 +01:00
}
2017-03-04 06:53:46 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2017-03-04 06:53:46 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testCloneTransaction(): void
2017-03-04 06:53:46 +01:00
{
2018-02-28 15:50:00 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
$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([]);
$journalRepos->shouldReceive('getMetaField')->andReturnNull();
2018-02-28 15:50:00 +01:00
2018-07-23 21:49:15 +02:00
$journalRepos->shouldReceive('getNoteText')->andReturn('I see you...')->once();
2018-02-28 15:50:00 +01:00
2017-12-23 17:42:07 +01:00
2017-03-04 06:53:46 +01: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 18:40:39 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2017-02-12 18:40:39 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testCreate(): void
2017-02-12 18:40:39 +01:00
{
2018-02-28 15:50:00 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
2017-03-17 16:34:57 +01:00
Steam::shouldReceive('phpBytes')->andReturn(2048);
2017-03-04 06:53:46 +01:00
$budgetRepos->shouldReceive('getActiveBudgets')->andReturn(new Collection)->once();
$piggyRepos->shouldReceive('getPiggyBanksWithAmount')->andReturn(new Collection)->once();
2018-03-07 10:18:22 +01:00
2017-02-12 18:40:39 +01: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 14:24:06 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2018-03-03 14:24:06 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testCreateDepositWithSource(): void
2018-03-03 14:24:06 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-03-03 14:24:06 +01:00
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">');
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2018-03-03 14:24:06 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testCreateWithSource(): void
2018-03-03 14:24:06 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-03-03 14:24:06 +01:00
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 18:40:39 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2017-02-12 18:40:39 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testDelete(): void
2017-02-12 18:40:39 +01:00
{
2018-02-28 15:50:00 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
2017-02-12 18:40:39 +01:00
$this->be($this->user());
2017-03-04 06:53:46 +01: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 18:40:39 +01:00
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2017-02-12 18:40:39 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testDestroy(): void
2017-02-12 18:40:39 +01:00
{
2018-02-28 15:50:00 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
2018-02-28 15:50:00 +01:00
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
$journalRepos->shouldReceive('destroy')->once();
2018-04-27 11:29:09 +02:00
$journalRepos->shouldReceive('getTransactionType')->once()->andReturn('Withdrawal');
2017-03-04 15:29:20 +01:00
2017-03-18 20:53:44 +01:00
$this->session(['transactions.delete.uri' => 'http://localhost']);
2017-03-04 15:29:20 +01:00
$this->be($this->user());
2017-03-04 06:53:46 +01: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 18:40:39 +01:00
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2017-02-12 18:40:39 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testEdit(): void
2017-02-12 18:40:39 +01:00
{
2018-02-28 15:50:00 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
2018-08-09 19:44:36 +02:00
$journalRepos->shouldReceive('setUser')->once();
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
$account = $this->user()->accounts()->first();
2017-03-04 06:53:46 +01:00
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection)->once();
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-02-25 17:38:24 +01: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 17:42:07 +01:00
2018-03-07 10:18:22 +01:00
// mock new account list:
$currency = TransactionCurrency::first();
$accountRepos->shouldReceive('getAccountsByType')
2018-09-02 20:13:25 +02:00
->withArgs(
[[AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,]]
)->andReturn(new Collection([$account]))->once();
2018-03-07 10:18:22 +01:00
Amount::shouldReceive('getDefaultCurrency')->andReturn($currency)->times(6);
2017-03-04 06:53:46 +01: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">');
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2017-03-04 06:53:46 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testEditCashDeposit(): void
2017-03-04 06:53:46 +01:00
{
2018-02-28 15:50:00 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-08-09 19:44:36 +02:00
$journalRepos->shouldReceive('setUser')->once();
2018-02-28 15:50:00 +01:00
2017-03-04 06:53:46 +01:00
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection)->once();
2018-02-28 15:50:00 +01:00
$account = $this->user()->accounts()->first();
$cash = $this->user()->accounts()->where('account_type_id', 2)->first();
2018-02-25 17:38:24 +01: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-04 06:53:46 +01:00
$this->be($this->user());
2018-02-25 17:38:24 +01: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 10:18:22 +01:00
2018-02-25 17:38:24 +01:00
$response = $this->get(route('transactions.edit', [$deposit->transaction_journal_id]));
2017-03-04 06:53:46 +01:00
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
2018-06-30 05:21:21 +02:00
$response->assertSee(' name="source_name" type="text" value="">');
2017-03-04 06:53:46 +01:00
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2017-03-04 06:53:46 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testEditCashWithdrawal(): void
2017-03-04 06:53:46 +01:00
{
2018-02-28 15:50:00 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-08-09 19:44:36 +02:00
$journalRepos->shouldReceive('setUser')->once();
2018-02-28 15:50:00 +01:00
2017-03-04 06:53:46 +01:00
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection)->once();
2018-02-28 15:50:00 +01:00
$account = $this->user()->accounts()->first();
$cash = $this->user()->accounts()->where('account_type_id', 2)->first();
2018-02-25 17:38:24 +01: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 18:40:39 +01:00
$this->be($this->user());
2017-03-04 06:53:46 +01: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 17:38:24 +01:00
->where('transaction_journals.user_id', $this->user()->id)->first(['transactions.*']);
2018-02-28 15:50:00 +01:00
$response = $this->get(route('transactions.edit', [$withdrawal->transaction_journal_id]));
2018-02-25 17:38:24 +01:00
2017-02-12 18:40:39 +01:00
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
2018-07-01 09:27:22 +02:00
$response->assertSee(' name="destination_name" type="text" value="">');
2017-03-04 06:53:46 +01:00
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2017-03-04 06:53:46 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testEditReconcile(): void
2017-03-04 06:53:46 +01:00
{
2018-02-28 15:50:00 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
2018-02-28 15:50:00 +01:00
$journalRepos->shouldReceive('getTransactionType')->andReturn('Reconciliation')->once();
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-02-25 17:38:24 +01:00
2017-03-04 06:53:46 +01:00
$this->be($this->user());
2018-02-25 17:38:24 +01: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-04 06:53:46 +01:00
$response->assertStatus(302);
2017-02-12 18:40:39 +01:00
}
2017-12-23 17:42:07 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testEditRedirect(): void
2017-12-23 17:42:07 +01:00
{
2018-02-28 15:50:00 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-08-09 19:44:36 +02:00
$journalRepos->shouldReceive('setUser')->once();
2018-02-25 17:38:24 +01:00
2017-12-23 17:42:07 +01:00
$this->be($this->user());
2017-12-29 09:05:35 +01:00
$withdrawal = TransactionJournal::where('transaction_type_id', 1)
2017-12-23 17:42:07 +01: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 15:50:00 +01:00
$journalRepos->shouldReceive('getTransactionType')->andReturn('Withdrawal');
$journalRepos->shouldReceive('countTransactions')->andReturn(3);
2018-02-25 17:38:24 +01:00
$response = $this->get(route('transactions.edit', [$withdrawal->id]));
2017-12-23 17:42:07 +01:00
$response->assertStatus(302);
}
2018-03-03 14:24:06 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2018-03-03 14:24:06 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testEditRedirectOpening(): void
2018-03-03 14:24:06 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-03-03 14:24:06 +01:00
$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 18:05:38 +02:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2017-06-28 18:05:38 +02:00
*/
2018-05-11 19:58:10 +02:00
public function testEditTransferWithForeignAmount(): void
2017-06-28 18:05:38 +02:00
{
2018-02-28 15:50:00 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-08-09 19:44:36 +02:00
$journalRepos->shouldReceive('setUser')->once();
2018-02-28 15:50:00 +01:00
2017-06-28 18:05:38 +02: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 17:38:24 +01:00
2018-02-28 15:50:00 +01:00
$account = $this->user()->accounts()->first();
2018-02-25 17:38:24 +01: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 18:05:38 +02:00
$response->assertStatus(200);
2018-03-03 14:24:06 +01:00
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2018-03-03 14:24:06 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testEditWithForeign(): void
2018-03-03 14:24:06 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-08-09 19:44:36 +02:00
$journalRepos->shouldReceive('setUser')->once();
2018-03-03 14:24:06 +01:00
$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('firstNull')->andReturn(new TransactionJournal);
2018-03-03 14:24:06 +01: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($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 18:05:38 +02:00
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2017-06-28 18:05:38 +02:00
*/
2018-05-11 19:58:10 +02:00
public function testEditWithForeignAmount(): void
2017-06-28 18:05:38 +02:00
{
2018-02-28 15:50:00 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-08-09 19:44:36 +02:00
$journalRepos->shouldReceive('setUser')->once();
2018-02-28 15:50:00 +01:00
2017-06-28 18:05:38 +02: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 17:38:24 +01:00
2018-02-28 15:50:00 +01:00
$account = $this->user()->accounts()->first();
2018-02-25 17:38:24 +01: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 18:05:38 +02:00
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
2018-09-15 13:44:36 +02:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
* @covers \FireflyIII\Http\Requests\JournalFormRequest
*/
public function testStoreDepositInvalidNative(): void
{
$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);
$userRepos = $this->mock(UserRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
// mock results:
$journal = new TransactionJournal();
$journal->id = 1000;
$journal->description = 'New deposit';
// mock attachment helper, trigger an error AND and info thing.
$this->session(['transactions.create.uri' => 'http://localhost']);
$this->be($this->user());
$data = [
'what' => 'deposit',
'amount' => '10',
'amount_currency_id_amount' => 1,
'destination_id' => 1,
'destination_account_currency' => 2,
'native_amount' => '',
'source_name' => 'Some source',
'date' => '2016-01-01',
'description' => 'Test descr',
];
$response = $this->post(route('transactions.store', ['deposit']), $data);
$response->assertStatus(302);
}
2017-02-12 18:40:39 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\JournalFormRequest
2017-02-12 18:40:39 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testStoreError(): void
2017-02-12 18:40:39 +01:00
{
2018-02-28 15:50:00 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
2017-03-04 06:53:46 +01:00
// mock results:
$journal = new TransactionJournal();
$journal->description = 'New journal';
2018-02-28 15:50:00 +01:00
$journalRepos->shouldReceive('store')->andReturn($journal);
2017-03-18 20:53:44 +01:00
$this->session(['transactions.create.uri' => 'http://localhost']);
2017-02-12 18:40:39 +01:00
$this->be($this->user());
2017-03-04 06:53:46 +01:00
$data = [
2017-02-12 18:40:39 +01:00
'what' => 'withdrawal',
'amount' => '10',
'amount_currency_id_amount' => 1,
2018-07-01 09:27:22 +02:00
'source_id' => 1,
'destination_name' => 'Some destination',
2017-02-12 18:40:39 +01:00
'date' => '2016-01-01',
'description' => 'Test descr',
];
$response = $this->post(route('transactions.store', ['withdrawal']), $data);
$response->assertStatus(302);
2017-03-04 06:53:46 +01:00
$response->assertSessionHas('error');
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\JournalFormRequest
2017-03-04 06:53:46 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testStoreSuccess(): void
2017-03-04 06:53:46 +01:00
{
2018-02-28 15:50:00 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
2017-03-04 06:53:46 +01:00
// mock results:
$journal = new TransactionJournal();
2017-03-05 11:19:06 +01:00
$journal->id = 1000;
2017-03-04 06:53:46 +01:00
$journal->description = 'New journal';
2018-02-28 15:50:00 +01:00
$journalRepos->shouldReceive('store')->andReturn($journal);
2018-05-11 19:58:10 +02:00
try {
$this->expectsEvents(StoredTransactionJournal::class);
} catch (Exception $e) {
$this->assertTrue(false, $e->getMessage());
}
2017-03-05 11:19:06 +01:00
$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 15:50:00 +01:00
$attRepos->shouldReceive('saveAttachmentsForModel');
$attRepos->shouldReceive('getErrors')->andReturn($errors);
$attRepos->shouldReceive('getMessages')->andReturn($messages);
2017-03-05 11:19:06 +01:00
2017-03-18 20:53:44 +01:00
$this->session(['transactions.create.uri' => 'http://localhost']);
2017-03-04 06:53:46 +01:00
$this->be($this->user());
$data = [
'what' => 'withdrawal',
'amount' => '10',
'amount_currency_id_amount' => 1,
2018-07-01 09:27:22 +02:00
'source_id' => 1,
'destination_name' => 'Some destination',
2017-03-04 06:53:46 +01:00
'date' => '2016-01-01',
'description' => 'Test descr',
];
$response = $this->post(route('transactions.store', ['withdrawal']), $data);
$response->assertStatus(302);
2017-03-05 11:19:06 +01:00
$response->assertSessionHas('success');
2017-03-04 06:53:46 +01:00
$response->assertSessionHas('error');
2017-03-05 11:19:06 +01:00
$response->assertSessionHas('info');
2017-02-12 18:40:39 +01:00
}
2018-03-03 17:16:47 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\JournalFormRequest
*/
2018-05-11 19:58:10 +02:00
public function testStoreSuccessDeposit(): void
2018-03-03 17:16:47 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-03-03 17:16:47 +01:00
// mock results:
$journal = new TransactionJournal();
$journal->id = 1000;
$journal->description = 'New deposit';
$journalRepos->shouldReceive('store')->andReturn($journal);
2018-05-11 19:58:10 +02:00
try {
$this->expectsEvents(StoredTransactionJournal::class);
} catch (Exception $e) {
$this->assertTrue(false, $e->getMessage());
}
2018-03-03 17:16:47 +01:00
$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,
2018-07-01 09:27:22 +02:00
'destination_id' => 1,
'source_name' => 'Some source',
2018-03-03 17:16:47 +01:00
'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');
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\JournalFormRequest
*/
2018-05-11 19:58:10 +02:00
public function testStoreSuccessTransfer(): void
2018-03-03 17:16:47 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-03-03 17:16:47 +01:00
// mock results:
$journal = new TransactionJournal();
$journal->id = 1000;
$journal->description = 'New transfer';
$journalRepos->shouldReceive('store')->andReturn($journal);
2018-05-11 19:58:10 +02:00
try {
$this->expectsEvents(StoredTransactionJournal::class);
} catch (Exception $e) {
$this->assertTrue(false, $e->getMessage());
}
2018-03-03 17:16:47 +01:00
$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,
2018-07-01 09:27:22 +02:00
'destination_id' => 1,
'source_id' => 2,
2018-03-03 17:16:47 +01:00
'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');
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\JournalFormRequest
*/
2018-05-11 19:58:10 +02:00
public function testStoreSuccessTransferForeign(): void
2018-03-03 17:16:47 +01: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);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-03-03 17:16:47 +01:00
// mock results:
$journal = new TransactionJournal();
$journal->id = 1000;
$journal->description = 'New transfer';
$journalRepos->shouldReceive('store')->andReturn($journal);
2018-05-11 19:58:10 +02:00
try {
$this->expectsEvents(StoredTransactionJournal::class);
} catch (Exception $e) {
$this->assertTrue(false, $e->getMessage());
}
2018-03-03 17:16:47 +01:00
$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',
2018-07-03 05:52:27 +02:00
'source_amount' => '10',
'destination_amount' => '10',
2018-03-03 17:16:47 +01:00
'amount_currency_id_amount' => 1,
'source_account_currency' => 1,
'destination_account_currency' => 2,
2018-07-01 09:27:22 +02:00
'destination_id' => 1,
'source_id' => 2,
2018-03-03 17:16:47 +01:00
'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');
}
2018-09-15 13:44:36 +02:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
* @covers \FireflyIII\Http\Requests\JournalFormRequest
*/
public function testStoreTransferInvalidSource(): void
{
$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);
$userRepos = $this->mock(UserRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
$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,
'source_amount' => '',
'destination_id' => 1,
'source_id' => 2,
'date' => '2016-01-01',
'description' => 'Test descr',
];
$response = $this->post(route('transactions.store', ['transfer']), $data);
$response->assertStatus(302);
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
* @covers \FireflyIII\Http\Requests\JournalFormRequest
*/
public function testStoreWithdrawalInvalidNative(): void
{
$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);
$userRepos = $this->mock(UserRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
$this->session(['transactions.create.uri' => 'http://localhost']);
$this->be($this->user());
$data = [
'what' => 'withdrawal',
'amount' => '10',
'source_id' => 1,
'amount_currency_id_amount' => 2,
'source_account_currency' => 3,
'native_amount' => '',
'destination_name' => 'Some destination',
'date' => '2016-01-01',
'description' => 'Test descr',
];
$response = $this->post(route('transactions.store', ['withdrawal']), $data);
$response->assertStatus(302);
}
2017-02-12 18:40:39 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\SingleController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\JournalFormRequest
2017-02-12 18:40:39 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testUpdate(): void
2017-02-12 18:40:39 +01:00
{
2018-02-28 15:50:00 +01: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);
$linkRepos = $this->mock(LinkTypeRepositoryInterface::class);
2018-09-15 13:44:36 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
2018-02-28 15:50:00 +01:00
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
$journalRepos->shouldReceive('getTransactionType')->andReturn('Withdrawal');
2018-03-11 18:46:41 +01:00
$journalRepos->shouldReceive('getPiggyBankEvents')->andReturn(new Collection);
$journalRepos->shouldReceive('getMetaField')->andReturn('');
2018-02-28 15:50:00 +01:00
2018-07-27 04:46:21 +02:00
$journalRepos->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection);
$journalRepos->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection);
2018-02-28 15:50:00 +01:00
$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);
2017-03-04 06:53:46 +01:00
2018-02-28 15:50:00 +01:00
// mock
try {
$this->expectsEvents(UpdatedTransactionJournal::class);
} catch (Exception $e) {
2018-05-11 19:58:10 +02:00
$this->assertTrue(false, $e->getMessage());
2018-02-28 15:50:00 +01:00
}
2017-03-04 06:53:46 +01:00
2018-07-01 09:27:22 +02:00
// find withdrawal:
2018-09-02 20:13:25 +02:00
$withdrawal = $this->getRandomWithdrawal();
2018-07-01 09:27:22 +02:00
$journalRepos->shouldReceive('update')->andReturn($withdrawal);
2017-03-04 06:53:46 +01:00
2017-03-18 20:53:44 +01:00
$this->session(['transactions.edit.uri' => 'http://localhost']);
2017-02-12 18:40:39 +01:00
$this->be($this->user());
$data = [
'id' => 123,
'what' => 'withdrawal',
'description' => 'Updated groceries',
2018-07-01 09:27:22 +02:00
'source_id' => 1,
'destination_name' => 'PLUS',
2017-02-12 18:40:39 +01:00
'amount' => '123',
'amount_currency_id_amount' => 1,
'budget_id' => 1,
'category' => 'Daily groceries',
'tags' => '',
'date' => '2016-01-01',
];
2018-07-01 09:27:22 +02:00
$response = $this->post(route('transactions.update', [$withdrawal->id]), $data);
2017-02-12 18:40:39 +01:00
$response->assertStatus(302);
$response->assertSessionHas('success');
2018-07-01 09:27:22 +02:00
$response = $this->get(route('transactions.show', [$withdrawal->id]));
2017-02-12 18:40:39 +01:00
$response->assertStatus(200);
2018-07-01 09:27:22 +02:00
$response->assertSee($withdrawal->description);
2017-02-12 18:40:39 +01:00
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
2017-02-16 22:33:32 +01:00
}