Fix account controller + coverage.

This commit is contained in:
James Cole
2017-12-31 10:40:27 +01:00
parent 8bcfa729b6
commit b73884160a
7 changed files with 254 additions and 28 deletions

View File

@@ -157,14 +157,14 @@ class AccountController extends Controller
* @throws FireflyException * @throws FireflyException
* @throws FireflyException * @throws FireflyException
*/ */
public function edit(Request $request, Account $account) public function edit(Request $request, Account $account, AccountRepositoryInterface $repository)
{ {
/** @var CurrencyRepositoryInterface $repository */ /** @var CurrencyRepositoryInterface $currencyRepos */
$repository = app(CurrencyRepositoryInterface::class); $currencyRepos = app(CurrencyRepositoryInterface::class);
$what = config('firefly.shortNamesByFullName')[$account->accountType->type]; $what = config('firefly.shortNamesByFullName')[$account->accountType->type];
$subTitle = trans('firefly.edit_' . $what . '_account', ['name' => $account->name]); $subTitle = trans('firefly.edit_' . $what . '_account', ['name' => $account->name]);
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $what); $subTitleIcon = config('firefly.subIconsByIdentifier.' . $what);
$allCurrencies = $repository->get(); $allCurrencies = $currencyRepos->get();
$currencySelectList = ExpandedForm::makeSelectList($allCurrencies); $currencySelectList = ExpandedForm::makeSelectList($allCurrencies);
$roles = []; $roles = [];
foreach (config('firefly.accountRoles') as $role) { foreach (config('firefly.accountRoles') as $role) {
@@ -184,7 +184,7 @@ class AccountController extends Controller
$openingBalanceAmount = '0' === $account->getOpeningBalanceAmount() ? '' : $openingBalanceAmount; $openingBalanceAmount = '0' === $account->getOpeningBalanceAmount() ? '' : $openingBalanceAmount;
$openingBalanceDate = $account->getOpeningBalanceDate(); $openingBalanceDate = $account->getOpeningBalanceDate();
$openingBalanceDate = 1900 === $openingBalanceDate->year ? null : $openingBalanceDate->format('Y-m-d'); $openingBalanceDate = 1900 === $openingBalanceDate->year ? null : $openingBalanceDate->format('Y-m-d');
$currency = $repository->find(intval($account->getMeta('currency_id'))); $currency = $currencyRepos->find(intval($account->getMeta('currency_id')));
$preFilled = [ $preFilled = [
'accountNumber' => $account->getMeta('accountNumber'), 'accountNumber' => $account->getMeta('accountNumber'),
@@ -196,10 +196,10 @@ class AccountController extends Controller
'openingBalance' => $openingBalanceAmount, 'openingBalance' => $openingBalanceAmount,
'virtualBalance' => $account->virtual_balance, 'virtualBalance' => $account->virtual_balance,
'currency_id' => $currency->id, 'currency_id' => $currency->id,
'notes' => '', 'notes' => '',
]; ];
/** @var Note $note */ /** @var Note $note */
$note = $account->notes()->first(); $note = $repository->getNote($account);
if (null !== $note) { if (null !== $note) {
$preFilled['notes'] = $note->text; $preFilled['notes'] = $note->text;
} }

View File

@@ -87,6 +87,16 @@ class AccountRepository implements AccountRepositoryInterface
return true; return true;
} }
/**
* @param Account $account
*
* @return Note|null
*/
public function getNote(Account $account): ?Note
{
return $account->notes()->first();
}
/** /**
* Returns the date of the very last transaction in this account. * Returns the date of the very last transaction in this account.
* *

View File

@@ -24,6 +24,7 @@ namespace FireflyIII\Repositories\Account;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@@ -33,6 +34,12 @@ use Illuminate\Support\Collection;
*/ */
interface AccountRepositoryInterface interface AccountRepositoryInterface
{ {
/**
* @param Account $account
*
* @return Note|null
*/
public function getNote(Account $account): ?Note;
/** /**
* Moved here from account CRUD. * Moved here from account CRUD.
* *

View File

@@ -79,7 +79,7 @@
<td> <td>
<div class="checkbox"> <div class="checkbox">
<label> <label>
<input name="ignore_category" type="checkbox" value="1"> <input name="ignore_category" type="checkbox" value="1" checked>
{{ 'no_bulk_category'|_ }} {{ 'no_bulk_category'|_ }}
</label> </label>
</div> </div>
@@ -97,7 +97,7 @@
<td> <td>
<div class="checkbox"> <div class="checkbox">
<label> <label>
<input name="ignore_budget" type="checkbox" value="1"> <input name="ignore_budget" type="checkbox" value="1" checked>
{{ 'no_bulk_budget'|_ }} {{ 'no_bulk_budget'|_ }}
</label> </label>
</div> </div>
@@ -111,7 +111,7 @@
<td> <td>
<div class="checkbox"> <div class="checkbox">
<label> <label>
<input name="ignore_tags" type="checkbox" value="1"> <input name="ignore_tags" type="checkbox" value="1" checked>
{{ 'no_bulk_tags'|_ }} {{ 'no_bulk_tags'|_ }}
</label> </label>
</div> </div>
@@ -121,24 +121,6 @@
</table> </table>
</div> </div>
</div> </div>
<!--
<table class="table table-striped table-condensed">
<tr>
<th class="col-lg-2 col-md-2 col-sm-2">{{ trans('list.category') }}</th>
<th class="col-lg-2 col-md-2 col-sm-2">{{ trans('list.tags') }}</th>
</tr>
<tr>
{# category #}
<td>
</td>
{# tags #}
<td>
</td>
</tr>
</table>
-->
</div> </div>
<div class="box-footer"> <div class="box-footer">
{% if journals.count > 0 %} {% if journals.count > 0 %}

View File

@@ -26,6 +26,7 @@ use Carbon\Carbon;
use FireflyIII\Helpers\Collector\JournalCollectorInterface; use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
use FireflyIII\Models\Note;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
@@ -114,12 +115,16 @@ class AccountControllerTest extends TestCase
*/ */
public function testEdit() public function testEdit()
{ {
$note = new Note();
$note->text = 'This is a test';
// mock stuff // mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(CurrencyRepositoryInterface::class); $repository = $this->mock(CurrencyRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('get')->andReturn(new Collection); $repository->shouldReceive('get')->andReturn(new Collection);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('find')->once()->andReturn(new TransactionCurrency()); $repository->shouldReceive('find')->once()->andReturn(new TransactionCurrency());
$accountRepos->shouldReceive('getNote')->andReturn($note)->once();
$this->be($this->user()); $this->be($this->user());
$account = $this->user()->accounts()->where('account_type_id', 3)->whereNull('deleted_at')->first(); $account = $this->user()->accounts()->where('account_type_id', 3)->whereNull('deleted_at')->first();
@@ -127,6 +132,7 @@ class AccountControllerTest extends TestCase
$response->assertStatus(200); $response->assertStatus(200);
// has bread crumb // has bread crumb
$response->assertSee('<ol class="breadcrumb">'); $response->assertSee('<ol class="breadcrumb">');
$response->assertSee($note->text);
} }
/** /**

View File

@@ -0,0 +1,217 @@
<?php
/**
* BulkControllerTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Feature\Controllers\Transaction;
use DB;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Support\Collection;
use Mockery;
use Tests\TestCase;
/**
* Class BulkControllerTest
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class BulkControllerTest extends TestCase
{
/**
* @covers \FireflyIII\Http\Controllers\Transaction\BulkController::edit
* @covers \FireflyIII\Http\Controllers\Transaction\BulkController::__construct
*/
public function testEdit()
{
// mock stuff:
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$budgetRepos->shouldReceive('getActiveBudgets')->andReturn(new Collection);
$transfers = TransactionJournal::where('transaction_type_id', 3)->where('user_id', $this->user()->id)->take(4)->get()->pluck('id')->toArray();
$this->be($this->user());
$response = $this->get(route('transactions.bulk.edit', $transfers));
$response->assertStatus(200);
$response->assertSee('Bulk edit a number of transactions');
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\BulkController::edit
*/
public function testEditMultiple()
{
// mock stuff:
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$budgetRepos->shouldReceive('getActiveBudgets')->andReturn(new Collection);
// default transactions
$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)
$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')])
);
// add reconcile transaction
$collection->push(
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')])
);
// 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.bulk.edit', join(',', $allIds));
$this->be($this->user());
$response = $this->get($route);
$response->assertStatus(200);
$response->assertSee('Bulk edit a number of transactions');
$response->assertSessionHas('info');
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
$response->assertSee('marked as reconciled');
$response->assertSee('multiple source accounts');
$response->assertSee('multiple destination accounts');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\BulkController::edit
*/
public function testEditMultipleNothingLeft()
{
// mock stuff:
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$budgetRepos->shouldReceive('getActiveBudgets')->andReturn(new Collection);
// default transactions
$collection = new Collection;
// 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)
$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')])
);
// add reconcile transaction
$collection->push(
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')])
);
// 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.bulk.edit', join(',', $allIds));
$this->be($this->user());
$response = $this->get($route);
$response->assertStatus(200);
$response->assertSee('Bulk edit a number of transactions');
$response->assertSessionHas('info');
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
$response->assertSessionHas('error', 'You have selected no valid transactions to edit.');
$response->assertSee('marked as reconciled');
$response->assertSee('multiple source accounts');
$response->assertSee('multiple destination accounts');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\BulkController::update
*/
public function testUpdate()
{
$tags = ['a', 'b', 'c'];
$collection = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->take(4)->get();
$allIds = $collection->pluck('id')->toArray();
$data = [
'category' => 'Some new category',
'budget_id' => 1,
'tags' => 'a,b,c',
'journals' => $allIds,
];
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('find')->times(4)->andReturn(new TransactionJournal);
$repository->shouldReceive('updateCategory')->times(4)->andReturn(new TransactionJournal())
->withArgs([Mockery::any(), $data['category']]);
$repository->shouldReceive('updateBudget')->times(4)->andReturn(new TransactionJournal())
->withArgs([Mockery::any(), $data['budget_id']]);
$repository->shouldReceive('updateTags')->times(4)->andReturn(true)
->withArgs([Mockery::any(), $tags]);
$route = route('transactions.bulk.update');
$this->be($this->user());
$response = $this->post($route, $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
}

View File

@@ -155,6 +155,9 @@ class MassControllerTest extends TestCase
$response->assertSee('Edit a number of transactions'); $response->assertSee('Edit a number of transactions');
// has bread crumb // has bread crumb
$response->assertSee('<ol class="breadcrumb">'); $response->assertSee('<ol class="breadcrumb">');
$response->assertSee('marked as reconciled');
$response->assertSee('multiple source accounts');
$response->assertSee('multiple destination accounts');
} }
/** /**
@@ -182,6 +185,7 @@ class MassControllerTest extends TestCase
$response = $this->get(route('transactions.mass.edit', join(',', $allIds))); $response = $this->get(route('transactions.mass.edit', join(',', $allIds)));
$response->assertStatus(200); $response->assertStatus(200);
$response->assertSee('Edit a number of transactions'); $response->assertSee('Edit a number of transactions');
$response->assertSessionHas('error','You have selected no valid transactions to edit.');
// has bread crumb // has bread crumb
$response->assertSee('<ol class="breadcrumb">'); $response->assertSee('<ol class="breadcrumb">');
} }