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

230 lines
10 KiB
PHP
Raw Normal View History

2017-02-12 18:40:39 +01:00
<?php
/**
* MassControllerTest.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
*/
declare(strict_types=1);
2017-02-12 18:40:39 +01:00
namespace Tests\Feature\Controllers\Transaction;
2017-03-04 15:29:20 +01:00
use FireflyIII\Models\AccountType;
2017-02-12 18:40:39 +01:00
use FireflyIII\Models\TransactionJournal;
2017-03-04 15:29:20 +01:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2017-03-09 08:19:05 +01:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2017-03-04 15:29:20 +01:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
2017-03-04 15:29:20 +01:00
use Illuminate\Support\Collection;
2018-03-24 06:08:50 +01:00
use Log;
use Mockery;
2017-02-12 18:40:39 +01:00
use Tests\TestCase;
2017-03-04 15:29:20 +01:00
/**
* Class MassControllerTest
*
2017-08-12 10:27:45 +02:00
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2017-03-04 15:29:20 +01:00
*/
2017-02-12 18:40:39 +01:00
class MassControllerTest 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-02-12 18:40:39 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\MassController
* @covers \FireflyIII\Http\Controllers\Transaction\MassController
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
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-07-27 04:46:21 +02:00
$journalRepos->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection)->once();
$journalRepos->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection)->once();
2018-02-28 15:50:00 +01:00
2017-02-12 18:40:39 +01:00
$withdrawals = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->take(2)->get()->pluck('id')->toArray();
$this->be($this->user());
$response = $this->get(route('transactions.mass.delete', $withdrawals));
$response->assertStatus(200);
$response->assertSee('Delete a number of transactions');
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\MassController
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
2017-03-04 15:29:20 +01:00
$deposits = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->take(2)->get();
$depositIds = $deposits->pluck('id')->toArray();
// mock deletion:
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-04-27 11:29:09 +02:00
$repository->shouldReceive('findNull')->andReturnValues([$deposits[0], $deposits[1]])->times(2);
2018-03-11 18:46:41 +01:00
$repository->shouldReceive('destroy')->times(2);
2017-03-04 15:29:20 +01:00
2017-03-18 20:53:44 +01:00
$this->session(['transactions.mass-delete.uri' => 'http://localhost']);
2017-03-04 15:29:20 +01:00
$data = [
'confirm_mass_delete' => $depositIds,
2017-02-12 18:40:39 +01:00
];
$this->be($this->user());
$response = $this->post(route('transactions.mass.destroy'), $data);
$response->assertSessionHas('success');
$response->assertStatus(302);
2017-03-04 15:29:20 +01:00
}
2017-02-12 18:40:39 +01:00
2017-03-04 15:29:20 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\MassController
2017-03-04 15:29:20 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testEdit(): void
2017-03-04 15:29:20 +01:00
{
2018-02-28 15:50:00 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
2018-02-28 15:50:00 +01:00
$transfers = TransactionJournal::where('transaction_type_id', 3)->where('user_id', $this->user()->id)->take(2)->get();
2018-02-28 15:50:00 +01:00
$transfersArray = $transfers->pluck('id')->toArray();
2018-03-24 06:08:50 +01:00
$source = $this->user()->accounts()->first();
2018-02-28 15:50:00 +01:00
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
// mock data for edit page:
2018-03-03 14:24:06 +01:00
$journalRepos->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$source]));
$journalRepos->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$source]));
2018-02-28 15:50:00 +01:00
$journalRepos->shouldReceive('getTransactionType')->andReturn('Transfer');
$journalRepos->shouldReceive('isJournalReconciled')->andReturn(false);
$journalRepos->shouldReceive('getFirstPosTransaction')->andReturn($transfers->first()->transactions()->first());
2017-03-04 15:29:20 +01:00
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getAccountsByType')->once()->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection);
2017-02-12 18:40:39 +01:00
2017-03-09 08:19:05 +01:00
// mock more stuff:
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection);
2018-02-28 15:50:00 +01:00
2017-03-04 15:29:20 +01:00
$this->be($this->user());
2018-02-28 15:50:00 +01:00
$response = $this->get(route('transactions.mass.edit', $transfersArray));
2017-03-04 15:29:20 +01:00
$response->assertStatus(200);
$response->assertSee('Edit a number of transactions');
// 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\MassController
2017-02-12 18:40:39 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testEditMultiple(): void
2017-02-12 18:40:39 +01:00
{
2018-02-28 15:50:00 +01:00
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
2018-02-28 15:50:00 +01:00
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
2017-03-04 15:29:20 +01:00
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getAccountsByType')->once()->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection);
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
$journalRepos->shouldReceive('getJournalSourceAccounts')
2018-04-27 11:29:09 +02:00
->andReturn(new Collection([1, 2, 3]), new Collection, new Collection, new Collection, new Collection([1]));
2018-02-28 15:50:00 +01:00
$journalRepos->shouldReceive('getJournalDestinationAccounts')
2018-04-27 11:29:09 +02:00
->andReturn(new Collection, new Collection([1, 2, 3]), new Collection, new Collection, new Collection([1]));
2018-02-28 15:50:00 +01:00
$journalRepos->shouldReceive('getTransactionType')
2018-04-27 11:29:09 +02:00
->andReturn('Withdrawal', 'Opening balance', 'Withdrawal', 'Withdrawal', 'Withdrawal');
2018-02-28 15:50:00 +01:00
$journalRepos->shouldReceive('isJournalReconciled')
2018-04-27 11:29:09 +02:00
->andReturn(true, false, false, false, false);
2018-02-28 15:50:00 +01:00
2017-03-04 15:29:20 +01:00
// default transactions
2018-04-27 11:29:09 +02:00
$collection = $this->user()->transactionJournals()->take(5)->get();
$allIds = $collection->pluck('id')->toArray();
2018-04-02 14:17:11 +02:00
$route = route('transactions.mass.edit', implode(',', $allIds));
2017-03-04 15:29:20 +01:00
$this->be($this->user());
2017-06-28 18:05:38 +02:00
$response = $this->get($route);
2017-03-04 15:29:20 +01:00
$response->assertStatus(200);
$response->assertSee('Edit a number of transactions');
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
2017-12-31 10:40:27 +01:00
$response->assertSee('marked as reconciled');
$response->assertSee('multiple source accounts');
$response->assertSee('multiple destination accounts');
2017-03-04 15:29:20 +01:00
}
2017-02-12 18:40:39 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\MassController
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
{
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)
->whereNull('deleted_at')
->first();
2018-02-28 15:50:00 +01:00
2017-03-04 15:29:20 +01:00
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('update')->once();
$repository->shouldReceive('findNull')->once()->andReturn($deposit);
2018-02-28 15:50:00 +01:00
$repository->shouldReceive('getTransactionType')->andReturn('Deposit');
2018-03-04 09:49:15 +01:00
$repository->shouldReceive('getNoteText')->andReturn('Some note');
2017-03-04 15:29:20 +01:00
2017-03-18 20:53:44 +01:00
$this->session(['transactions.mass-edit.uri' => 'http://localhost']);
2017-02-12 18:40:39 +01:00
$data = [
'journals' => [$deposit->id],
'description' => [$deposit->id => 'Updated salary thing'],
'amount' => [$deposit->id => 1600],
'amount_currency_id_amount_' . $deposit->id => 1,
'date' => [$deposit->id => '2014-07-24'],
2018-08-06 19:14:30 +02:00
'source_name' => [$deposit->id => 'Job'],
'destination_id' => [$deposit->id => 1],
2017-02-12 18:40:39 +01:00
'category' => [$deposit->id => 'Salary'],
];
$this->be($this->user());
$response = $this->post(route('transactions.mass.update', [$deposit->id]), $data);
$response->assertSessionHas('success');
$response->assertStatus(302);
}
2017-02-16 22:33:32 +01:00
}