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

216 lines
9.4 KiB
PHP
Raw Normal View History

2017-02-12 11:40:39 -06:00
<?php
/**
* MassControllerTest.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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-02-12 11:40:39 -06:00
*/
declare(strict_types=1);
2017-02-12 11:40:39 -06:00
namespace Tests\Feature\Controllers\Transaction;
2017-03-04 08:29:20 -06:00
use DB;
use FireflyIII\Models\AccountType;
2017-02-12 11:40:39 -06:00
use FireflyIII\Models\TransactionJournal;
2017-03-04 08:29:20 -06:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2017-03-09 01:19:05 -06:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2017-03-04 08:29:20 -06:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Support\Collection;
2017-02-12 11:40:39 -06:00
use Tests\TestCase;
2017-03-04 08:29:20 -06:00
/**
* Class MassControllerTest
*
* @package Tests\Feature\Controllers\Transaction
2017-08-12 03:27:45 -05:00
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2017-03-04 08:29:20 -06:00
*/
2017-02-12 11:40:39 -06:00
class MassControllerTest extends TestCase
{
/**
* @covers \FireflyIII\Http\Controllers\Transaction\MassController::delete
2017-02-17 13:14:38 -06:00
* @covers \FireflyIII\Http\Controllers\Transaction\MassController::__construct
2017-02-12 11:40:39 -06:00
*/
public function testDelete()
{
$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">');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\MassController::destroy
*/
public function testDestroy()
{
2017-03-04 08:29:20 -06: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);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('find')->andReturnValues([$deposits[0], $deposits[1]])->times(2);
$repository->shouldReceive('delete')->times(2);
2017-03-18 14:53:44 -05:00
$this->session(['transactions.mass-delete.uri' => 'http://localhost']);
2017-03-04 08:29:20 -06:00
$data = [
'confirm_mass_delete' => $depositIds,
2017-02-12 11:40:39 -06:00
];
$this->be($this->user());
$response = $this->post(route('transactions.mass.destroy'), $data);
$response->assertSessionHas('success');
$response->assertStatus(302);
2017-03-04 08:29:20 -06:00
}
2017-02-12 11:40:39 -06:00
2017-03-04 08:29:20 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\MassController::edit
*/
public function testEdit()
{
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getAccountsByType')->once()->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection);
2017-02-12 11:40:39 -06:00
2017-03-09 01:19:05 -06:00
// mock more stuff:
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection);
2017-03-04 08:29:20 -06:00
$transfers = TransactionJournal::where('transaction_type_id', 3)->where('user_id', $this->user()->id)->take(2)->get()->pluck('id')->toArray();
2017-02-12 11:40:39 -06:00
2017-03-04 08:29:20 -06:00
$this->be($this->user());
$response = $this->get(route('transactions.mass.edit', $transfers));
$response->assertStatus(200);
$response->assertSee('Edit a number of transactions');
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
2017-02-12 11:40:39 -06:00
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\MassController::edit
*/
2017-03-05 04:19:06 -06:00
public function testEditMultiple()
2017-02-12 11:40:39 -06:00
{
2017-03-04 08:29:20 -06:00
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getAccountsByType')->once()->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection);
// default transactions
2017-03-05 04:19:06 -06:00
$collection = TransactionJournal::where('transaction_type_id', 3)->where('user_id', $this->user()->id)->take(2)->get();
// add deposit (with multiple sources)
$collection->push(
TransactionJournal::where('transaction_type_id', 2)
->whereNull('transaction_journals.deleted_at')
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->groupBy('transaction_journals.id')
->orderBy('ct', 'DESC')
->where('user_id', $this->user()->id)->first(['transaction_journals.id', DB::raw('count(transactions.`id`) as ct')])
);
// add withdrawal (with multiple destinations)
2017-03-04 08:29:20 -06:00
$collection->push(
TransactionJournal::where('transaction_type_id', 1)
->whereNull('transaction_journals.deleted_at')
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->groupBy('transaction_journals.id')
->orderBy('ct', 'DESC')
->where('user_id', $this->user()->id)->first(['transaction_journals.id', DB::raw('count(transactions.`id`) as ct')])
);
2017-06-28 11:05:38 -05:00
// add opening balance:
$collection->push(TransactionJournal::where('transaction_type_id', 4)->where('user_id', $this->user()->id)->first());
$allIds = $collection->pluck('id')->toArray();
$route = route('transactions.mass.edit', join(',', $allIds));
2017-03-04 08:29:20 -06:00
$this->be($this->user());
2017-06-28 11:05:38 -05:00
$response = $this->get($route);
2017-03-04 08:29:20 -06:00
$response->assertStatus(200);
$response->assertSee('Edit a number of transactions');
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\MassController::edit
*/
2017-03-05 04:19:06 -06:00
public function testEditMultipleNothingLeft()
2017-03-04 08:29:20 -06:00
{
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getAccountsByType')->once()->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection);
// default transactions
2017-03-05 04:19:06 -06:00
$collection = new Collection;
2017-03-04 08:29:20 -06:00
$collection->push(
TransactionJournal::where('transaction_type_id', 1)
->whereNull('transaction_journals.deleted_at')
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->groupBy('transaction_journals.id')
->orderBy('ct', 'DESC')
->where('user_id', $this->user()->id)->first(['transaction_journals.id', DB::raw('count(transactions.`id`) as ct')])
);
$allIds = $collection->pluck('id')->toArray();
2017-02-12 11:40:39 -06:00
$this->be($this->user());
2017-03-04 08:29:20 -06:00
$response = $this->get(route('transactions.mass.edit', join(',', $allIds)));
2017-02-12 11:40:39 -06:00
$response->assertStatus(200);
$response->assertSee('Edit a number of transactions');
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\MassController::update
*/
public function testUpdate()
{
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)
->whereNull('deleted_at')
->first();
2017-03-04 08:29:20 -06:00
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('update')->once();
2017-03-04 08:29:20 -06:00
$repository->shouldReceive('find')->once()->andReturn($deposit);
2017-03-18 14:53:44 -05:00
$this->session(['transactions.mass-edit.uri' => 'http://localhost']);
2017-02-12 11:40:39 -06: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'],
'source_account_name' => [$deposit->id => 'Job'],
'destination_account_id' => [$deposit->id => 1],
'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 15:33:32 -06:00
}