Code cleanup and tests.

This commit is contained in:
James Cole 2017-07-23 08:32:51 +02:00
parent 8bb7d5de3f
commit f85ab74e2b
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
15 changed files with 72 additions and 90 deletions

View File

@ -27,11 +27,10 @@ use Route;
*/ */
class Help implements HelpInterface class Help implements HelpInterface
{ {
const CACHEKEY = 'help_%s_%s';
/** @var string */ /** @var string */
protected $userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'; protected $userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36';
const CACHEKEY = 'help_%s_%s';
/** /**
* @param string $route * @param string $route
* @param string $language * @param string $language

View File

@ -138,14 +138,14 @@ class HomeController extends Controller
} }
return view( return view(
'index', compact('count', 'title', 'subTitle', 'mainTitleIcon', 'transactions', 'showDepositsFrontpage', 'billCount') 'index', compact('count', 'title', 'subTitle', 'mainTitleIcon', 'transactions', 'showDepositsFrontpage', 'billCount')
); );
} }
public function routes() public function routes()
{ {
$set = RouteFacade::getRoutes(); $set = RouteFacade::getRoutes();
$ignore = ['chart.','javascript.','json.','report-data.','popup.','debugbar.']; $ignore = ['chart.', 'javascript.', 'json.', 'report-data.', 'popup.', 'debugbar.'];
/** @var Route $route */ /** @var Route $route */
foreach ($set as $route) { foreach ($set as $route) {
$name = $route->getName(); $name = $route->getName();
@ -157,7 +157,7 @@ class HomeController extends Controller
} }
} }
if (!$found) { if (!$found) {
echo 'touch '.$route->getName() . '.md;'; echo 'touch ' . $route->getName() . '.md;';
} }
} }

View File

@ -89,7 +89,7 @@ class PiggyBankController extends Controller
/** @var Carbon $date */ /** @var Carbon $date */
$date = session('end', Carbon::now()->endOfMonth()); $date = session('end', Carbon::now()->endOfMonth());
$leftOnAccount = $piggyBank->leftOnAccount($date); $leftOnAccount = $piggyBank->leftOnAccount($date);
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount?? '0'; $savedSoFar = $piggyBank->currentRelevantRep()->currentamount ?? '0';
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar); $leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = min($leftOnAccount, $leftToSave); $maxAmount = min($leftOnAccount, $leftToSave);

View File

@ -213,11 +213,11 @@ class MassController extends Controller
if ($journal) { if ($journal) {
// get optional fields: // get optional fields:
$what = strtolower($journal->transactionTypeStr()); $what = strtolower($journal->transactionTypeStr());
$sourceAccountId = $request->get('source_account_id')[$journal->id] ?? 0; $sourceAccountId = $request->get('source_account_id')[$journal->id] ?? 0;
$sourceAccountName = $request->get('source_account_name')[$journal->id] ?? ''; $sourceAccountName = $request->get('source_account_name')[$journal->id] ?? '';
$destAccountId = $request->get('destination_account_id')[$journal->id] ?? 0; $destAccountId = $request->get('destination_account_id')[$journal->id] ?? 0;
$destAccountName = $request->get('destination_account_name')[$journal->id] ?? ''; $destAccountName = $request->get('destination_account_name')[$journal->id] ?? '';
$budgetId = $request->get('budget_id')[$journal->id] ?? 0; $budgetId = $request->get('budget_id')[$journal->id] ?? 0;
$category = $request->get('category')[$journal->id]; $category = $request->get('category')[$journal->id];
$tags = $journal->tags->pluck('tag')->toArray(); $tags = $journal->tags->pluck('tag')->toArray();
$amount = round($request->get('amount')[$journal->id], 12); $amount = round($request->get('amount')[$journal->id], 12);

View File

@ -10,6 +10,7 @@
*/ */
declare(strict_types=1); declare(strict_types=1);
use Carbon\Carbon; use Carbon\Carbon;
use DaveJamesMiller\Breadcrumbs\Generator as BreadCrumbGenerator; use DaveJamesMiller\Breadcrumbs\Generator as BreadCrumbGenerator;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;

View File

@ -180,8 +180,8 @@ class ImportBill
Log::debug('Finding a mapped bill based on', $array); Log::debug('Finding a mapped bill based on', $array);
$search = intval($array['mapped']); $search = intval($array['mapped']);
$bill = $this->repository->find($search); $bill = $this->repository->find($search);
if (is_null($bill->id)) { if (is_null($bill->id)) {
Log::error(sprintf('There is no bill with id #%d. Invalid mapping will be ignored!', $search)); Log::error(sprintf('There is no bill with id #%d. Invalid mapping will be ignored!', $search));

View File

@ -180,7 +180,7 @@ class ImportBudget
Log::debug('Finding a mapped budget based on', $array); Log::debug('Finding a mapped budget based on', $array);
$search = intval($array['mapped']); $search = intval($array['mapped']);
$budget = $this->repository->find($search); $budget = $this->repository->find($search);
if (is_null($budget->id)) { if (is_null($budget->id)) {

View File

@ -174,7 +174,7 @@ class ImportCategory
Log::debug('Finding a mapped category based on', $array); Log::debug('Finding a mapped category based on', $array);
$search = intval($array['mapped']); $search = intval($array['mapped']);
$category = $this->repository->find($search); $category = $this->repository->find($search);
if (is_null($category->id)) { if (is_null($category->id)) {

View File

@ -65,19 +65,6 @@ class ImportJournal
/** @var User */ /** @var User */
private $user; private $user;
/**
* @return string
*/
public function getDescription(): string
{
if ($this->description === '') {
return '(no description)';
}
return $this->description;
}
/** /**
* ImportEntry constructor. * ImportEntry constructor.
*/ */
@ -157,6 +144,18 @@ class ImportJournal
return $date; return $date;
} }
/**
* @return string
*/
public function getDescription(): string
{
if ($this->description === '') {
return '(no description)';
}
return $this->description;
}
/** /**
* @param string $hash * @param string $hash
*/ */

View File

@ -474,31 +474,32 @@ class ImportStorage
private function verifyDoubleTransfer(TransactionType $transactionType, ImportJournal $importJournal): bool private function verifyDoubleTransfer(TransactionType $transactionType, ImportJournal $importJournal): bool
{ {
if ($transactionType->type === TransactionType::TRANSFER) { if ($transactionType->type === TransactionType::TRANSFER) {
$amount = Steam::positive($importJournal->getAmount()); $amount = Steam::positive($importJournal->getAmount());
$date = $importJournal->getDate($this->dateFormat); $date = $importJournal->getDate($this->dateFormat);
$set = TransactionJournal::leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id') $set = TransactionJournal::leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->leftJoin( ->leftJoin(
'transactions AS source', function (JoinClause $join) { 'transactions AS source', function (JoinClause $join) {
$join->on('transaction_journals.id', '=', 'source.transaction_journal_id')->where('source.amount', '<', 0); $join->on('transaction_journals.id', '=', 'source.transaction_journal_id')->where('source.amount', '<', 0);
} }
) )
->leftJoin( ->leftJoin(
'transactions AS destination', function (JoinClause $join) { 'transactions AS destination', function (JoinClause $join) {
$join->on('transaction_journals.id', '=', 'destination.transaction_journal_id')->where( $join->on('transaction_journals.id', '=', 'destination.transaction_journal_id')->where(
'destination.amount', '>', 0 'destination.amount', '>', 0
); );
} }
) )
->leftJoin('accounts as source_accounts', 'source.account_id', '=', 'source_accounts.id') ->leftJoin('accounts as source_accounts', 'source.account_id', '=', 'source_accounts.id')
->leftJoin('accounts as destination_accounts', 'destination.account_id', '=', 'destination_accounts.id') ->leftJoin('accounts as destination_accounts', 'destination.account_id', '=', 'destination_accounts.id')
->where('transaction_journals.user_id', $this->job->user_id) ->where('transaction_journals.user_id', $this->job->user_id)
->where('transaction_types.type', TransactionType::TRANSFER) ->where('transaction_types.type', TransactionType::TRANSFER)
->where('transaction_journals.date', $date->format('Y-m-d')) ->where('transaction_journals.date', $date->format('Y-m-d'))
->where('destination.amount', $amount) ->where('destination.amount', $amount)
->get( ->get(
['transaction_journals.id', 'transaction_journals.encrypted', 'transaction_journals.description', ['transaction_journals.id', 'transaction_journals.encrypted', 'transaction_journals.description',
'source_accounts.name as source_name', 'destination_accounts.name as destination_name'] 'source_accounts.name as source_name', 'destination_accounts.name as destination_name']
); );
return $this->filterTransferSet($set, $importJournal); return $this->filterTransferSet($set, $importJournal);
} }

View File

@ -311,7 +311,7 @@ class Amount
$coloured = false; $coloured = false;
$format = '<span class="text-info">%s</span>'; $format = '<span class="text-info">%s</span>';
} }
if($transaction->transaction_type_type === TransactionType::OPENING_BALANCE) { if ($transaction->transaction_type_type === TransactionType::OPENING_BALANCE) {
$amount = strval($transaction->transaction_amount); $amount = strval($transaction->transaction_amount);
} }

View File

@ -135,7 +135,7 @@ class Roles implements ConfigurationInterface
$count = $config['column-count']; $count = $config['column-count'];
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$role = $config['column-roles'][$i] ?? '_ignore'; $role = $config['column-roles'][$i] ?? '_ignore';
$mapping = $config['column-do-mapping'][$i] ?? false; $mapping = $config['column-do-mapping'][$i] ?? false;
if ($role === '_ignore' && $mapping === true) { if ($role === '_ignore' && $mapping === true) {
$mapping = false; $mapping = false;
@ -160,7 +160,7 @@ class Roles implements ConfigurationInterface
$count = $config['column-count']; $count = $config['column-count'];
$toBeMapped = 0; $toBeMapped = 0;
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$mapping = $config['column-do-mapping'][$i] ?? false; $mapping = $config['column-do-mapping'][$i] ?? false;
if ($mapping === true) { if ($mapping === true) {
$toBeMapped++; $toBeMapped++;
} }

View File

@ -249,21 +249,6 @@ class Steam
return $value; return $value;
} }
/**
* @param $value
*
* @return mixed
*/
public function tryDecrypt($value)
{
try {
$value = Crypt::decrypt($value);
} catch (DecryptException $e) {
// do not care.
}
return $value;
}
/** /**
* @param array $accounts * @param array $accounts
@ -361,4 +346,20 @@ class Steam
return $amount; return $amount;
} }
/**
* @param $value
*
* @return mixed
*/
public function tryDecrypt($value)
{
try {
$value = Crypt::decrypt($value);
} catch (DecryptException $e) {
// do not care.
}
return $value;
}
} }

View File

@ -41,10 +41,6 @@ class TwoFactorControllerTest extends TestCase
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn($secretPreference)->once(); Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn($secretPreference)->once();
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn($secretPreference)->once(); Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn($secretPreference)->once();
$falsePref = new Preference;
$falsePref->data = false;
Preferences::shouldReceive('get')->withArgs(['shown_demo_two-factor.index', false])->andReturn($falsePref)->once();
$response = $this->get(route('two-factor.index')); $response = $this->get(route('two-factor.index'));
$response->assertStatus(200); $response->assertStatus(200);
} }
@ -61,7 +57,6 @@ class TwoFactorControllerTest extends TestCase
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->andReturn($falsePreference)->twice(); Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->andReturn($falsePreference)->twice();
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn(null)->once(); Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn(null)->once();
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn(null)->once(); Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn(null)->once();
Preferences::shouldReceive('get')->withArgs(['shown_demo_two-factor.index', false])->andReturn($falsePreference)->once();
$response = $this->get(route('two-factor.index')); $response = $this->get(route('two-factor.index'));
$response->assertStatus(302); $response->assertStatus(302);
@ -84,10 +79,6 @@ class TwoFactorControllerTest extends TestCase
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn($secretPreference)->once(); Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn($secretPreference)->once();
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn($secretPreference)->once(); Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn($secretPreference)->once();
$falsePref = new Preference;
$falsePref->data = false;
Preferences::shouldReceive('get')->withArgs(['shown_demo_two-factor.index', false])->andReturn($falsePref)->once();
$response = $this->get(route('two-factor.index')); $response = $this->get(route('two-factor.index'));
$response->assertStatus(500); $response->assertStatus(500);
} }
@ -107,10 +98,6 @@ class TwoFactorControllerTest extends TestCase
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn($secretPreference); Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn($secretPreference);
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn($secretPreference); Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn($secretPreference);
$falsePref = new Preference;
$falsePref->data = false;
Preferences::shouldReceive('get')->withArgs(['shown_demo_two-factor.lost', false])->andReturn($falsePref)->once();
$response = $this->get(route('two-factor.lost')); $response = $this->get(route('two-factor.lost'));
$response->assertStatus(200); $response->assertStatus(200);
} }

View File

@ -56,12 +56,9 @@ class HelpControllerTest extends TestCase
$help->shouldReceive('getFromGithub')->withArgs(['index', 'nl_NL'])->andReturn('')->once(); $help->shouldReceive('getFromGithub')->withArgs(['index', 'nl_NL'])->andReturn('')->once();
// is US in cache? // is US in cache?
$help->shouldReceive('inCache')->withArgs(['index', 'en_US'])->andReturn(true)->twice(); $help->shouldReceive('inCache')->withArgs(['index', 'en_US'])->andReturn(true)->once();
$help->shouldReceive('getFromCache')->withArgs(['index', 'en_US'])->andReturn('US from cache.')->once(); $help->shouldReceive('getFromCache')->withArgs(['index', 'en_US'])->andReturn('US from cache.')->once();
// put US in cache:
$help->shouldReceive('putInCache')->once();
$this->be($this->user()); $this->be($this->user());
$response = $this->get(route('help.show', ['index'])); $response = $this->get(route('help.show', ['index']));
$response->assertStatus(200); $response->assertStatus(200);
@ -87,12 +84,9 @@ class HelpControllerTest extends TestCase
$help->shouldReceive('getFromGithub')->withArgs(['index', 'nl_NL'])->andReturn('')->once(); $help->shouldReceive('getFromGithub')->withArgs(['index', 'nl_NL'])->andReturn('')->once();
// is US in cache? // is US in cache?
$help->shouldReceive('inCache')->withArgs(['index', 'en_US'])->andReturn(false)->twice(); $help->shouldReceive('inCache')->withArgs(['index', 'en_US'])->andReturn(false)->once();
$help->shouldReceive('getFromGithub')->withArgs(['index', 'en_US'])->andReturn('')->once(); $help->shouldReceive('getFromGithub')->withArgs(['index', 'en_US'])->andReturn('')->once();
// put US in cache:
$help->shouldReceive('putInCache')->once();
$this->be($this->user()); $this->be($this->user());
$response = $this->get(route('help.show', ['index'])); $response = $this->get(route('help.show', ['index']));
$response->assertStatus(200); $response->assertStatus(200);