From 5846431b3421ced51009ae6a3e53eb5ed6f623e7 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 12 Jul 2018 21:32:58 +0200 Subject: [PATCH] Various code coverage changes and code updates. --- .../Account/ReconcileController.php | 113 +--------- app/Http/Controllers/Admin/UserController.php | 10 +- .../Controllers/Json/ReconcileController.php | 207 ++++++++++++++++++ app/Http/Middleware/IsDemoUser.php | 2 + routes/web.php | 10 +- .../Account/ReconcileControllerTest.php | 69 ------ .../Controllers/Admin/UserControllerTest.php | 14 +- .../Controllers/BillControllerTest.php | 11 +- .../Import/IndexControllerTest.php | 3 + .../Json/ReconcileControllerTest.php | 122 +++++++++++ .../Popup/ReportControllerTest.php | 4 +- .../Controllers/ProfileControllerTest.php | 28 +-- .../Feature/Controllers/TagControllerTest.php | 4 +- .../Transaction/MassControllerTest.php | 2 +- .../Transaction/SingleControllerTest.php | 2 + .../Controllers/TransactionControllerTest.php | 2 +- .../Handlers/Events/UserEventHandlerTest.php | 4 + .../Events/VersionCheckEventHandlerTest.php | 7 +- tests/Unit/Middleware/SandstormTest.php | 4 +- .../Triggers/ToAccountStartsTest.php | 4 +- 20 files changed, 398 insertions(+), 224 deletions(-) create mode 100644 app/Http/Controllers/Json/ReconcileController.php create mode 100644 tests/Feature/Controllers/Json/ReconcileControllerTest.php diff --git a/app/Http/Controllers/Account/ReconcileController.php b/app/Http/Controllers/Account/ReconcileController.php index 0cc4997679..d87905a05d 100644 --- a/app/Http/Controllers/Account/ReconcileController.php +++ b/app/Http/Controllers/Account/ReconcileController.php @@ -25,7 +25,6 @@ namespace FireflyIII\Http\Controllers\Account; use Carbon\Carbon; use FireflyIII\Exceptions\FireflyException; -use FireflyIII\Helpers\Collector\JournalCollectorInterface; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Requests\ReconciliationStoreRequest; use FireflyIII\Http\Requests\ReconciliationUpdateRequest; @@ -38,14 +37,13 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Services\Internal\Update\CurrencyUpdateService; -use Illuminate\Http\JsonResponse; -use Illuminate\Http\Request; -use Illuminate\Support\Collection; use Log; use Preferences; /** * Class ReconcileController. + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class ReconcileController extends Controller { @@ -113,63 +111,6 @@ class ReconcileController extends Controller )->with('data', $preFilled); } - /** @noinspection MoreThanThreeArgumentsInspection */ - /** - * @param Request $request - * @param Account $account - * @param Carbon $start - * @param Carbon $end - * - * @return JsonResponse - * - * @throws FireflyException - * @throws \Throwable - */ - public function overview(Request $request, Account $account, Carbon $start, Carbon $end): JsonResponse - { - if (AccountType::ASSET !== $account->accountType->type) { - throw new FireflyException(sprintf('Account %s is not an asset account.', $account->name)); - } - $startBalance = $request->get('startBalance'); - $endBalance = $request->get('endBalance'); - $transactionIds = $request->get('transactions') ?? []; - $clearedIds = $request->get('cleared') ?? []; - $amount = '0'; - $clearedAmount = '0'; - $route = route('accounts.reconcile.submit', [$account->id, $start->format('Ymd'), $end->format('Ymd')]); - // get sum of transaction amounts: - $transactions = $this->repository->getTransactionsById($transactionIds); - $cleared = $this->repository->getTransactionsById($clearedIds); - $countCleared = 0; - - /** @var Transaction $transaction */ - foreach ($transactions as $transaction) { - $amount = bcadd($amount, $transaction->amount); - } - - /** @var Transaction $transaction */ - foreach ($cleared as $transaction) { - if ($transaction->transactionJournal->date <= $end) { - $clearedAmount = bcadd($clearedAmount, $transaction->amount); - ++$countCleared; - } - } - $difference = bcadd(bcadd(bcsub($startBalance, $endBalance), $clearedAmount), $amount); - $diffCompare = bccomp($difference, '0'); - $return = [ - 'post_uri' => $route, - 'html' => view( - 'accounts.reconcile.overview', compact( - 'account', 'start', 'diffCompare', 'difference', 'end', 'clearedIds', 'transactionIds', 'clearedAmount', - 'startBalance', 'endBalance', 'amount', - 'route', 'countCleared' - ) - )->render(), - ]; - - return response()->json($return); - } - /** * @param Account $account * @param Carbon|null $start @@ -243,10 +184,10 @@ class ReconcileController extends Controller // get main transaction: $transaction = $this->repository->getAssetTransaction($journal); - if(null === $transaction) { + if (null === $transaction) { throw new FireflyException('The transaction data is incomplete. This is probably a bug. Apologies.'); } - $account = $transaction->account; + $account = $transaction->account; return view('accounts.reconcile.show', compact('journal', 'subTitle', 'transaction', 'account')); } @@ -338,52 +279,6 @@ class ReconcileController extends Controller return redirect(route('accounts.show', [$account->id])); } - /** - * @param Account $account - * @param Carbon $start - * @param Carbon $end - * - * @return mixed - * - * @throws FireflyException - * @throws \Throwable - */ - public function transactions(Account $account, Carbon $start, Carbon $end) - { - if (AccountType::INITIAL_BALANCE === $account->accountType->type) { - return $this->redirectToOriginalAccount($account); - } - - $startDate = clone $start; - $startDate->subDays(1); - - $currencyId = (int)$this->accountRepos->getMetaValue($account, 'currency_id'); - $currency = $this->currencyRepos->findNull($currencyId); - if (0 === $currency) { - $currency = app('amount')->getDefaultCurrency(); // @codeCoverageIgnore - } - - $startBalance = round(app('steam')->balance($account, $startDate), $currency->decimal_places); - $endBalance = round(app('steam')->balance($account, $end), $currency->decimal_places); - - // get the transactions - $selectionStart = clone $start; - $selectionStart->subDays(3); - $selectionEnd = clone $end; - $selectionEnd->addDays(3); - - // grab transactions: - /** @var JournalCollectorInterface $collector */ - $collector = app(JournalCollectorInterface::class); - $collector->setAccounts(new Collection([$account])) - ->setRange($selectionStart, $selectionEnd)->withBudgetInformation()->withOpposingAccount()->withCategoryInformation(); - $transactions = $collector->getJournals(); - $html = view( - 'accounts.reconcile.transactions', compact('account', 'transactions', 'currency', 'start', 'end', 'selectionStart', 'selectionEnd') - )->render(); - - return response()->json(['html' => $html, 'startBalance' => $startBalance, 'endBalance' => $endBalance]); - } /** * @param ReconciliationUpdateRequest $request diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 4510b3d8fb..4a5c9ae3cf 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -147,15 +147,7 @@ class UserController extends Controller $subTitleIcon = 'fa-user'; $information = $repository->getUserData($user); - return view( - 'admin.users.show', - compact( - 'title', - 'mainTitleIcon', - 'subTitle', - 'subTitleIcon', - 'information', - 'user' + return view('admin.users.show', compact('title', 'mainTitleIcon', 'subTitle', 'subTitleIcon', 'information', 'user' ) ); } diff --git a/app/Http/Controllers/Json/ReconcileController.php b/app/Http/Controllers/Json/ReconcileController.php new file mode 100644 index 0000000000..cfea152bea --- /dev/null +++ b/app/Http/Controllers/Json/ReconcileController.php @@ -0,0 +1,207 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Http\Controllers\Json; + + +use Carbon\Carbon; +use FireflyIII\Exceptions\FireflyException; +use FireflyIII\Helpers\Collector\JournalCollectorInterface; +use FireflyIII\Http\Controllers\Controller; +use FireflyIII\Models\Account; +use FireflyIII\Models\AccountType; +use FireflyIII\Models\Transaction; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; +use FireflyIII\Repositories\Journal\JournalRepositoryInterface; +use FireflyIII\Services\Internal\Update\CurrencyUpdateService; +use Illuminate\Http\JsonResponse; +use Illuminate\Http\Request; +use Illuminate\Support\Collection; + +/** + * + * Class ReconcileController + */ +class ReconcileController extends Controller +{ + + /** @var CurrencyUpdateService */ + private $accountRepos; + /** @var AccountRepositoryInterface */ + private $currencyRepos; + /** @var JournalRepositoryInterface */ + private $repository; + + /** + * + */ + public function __construct() + { + parent::__construct(); + + // translations: + $this->middleware( + function ($request, $next) { + app('view')->share('mainTitleIcon', 'fa-credit-card'); + app('view')->share('title', trans('firefly.accounts')); + $this->repository = app(JournalRepositoryInterface::class); + $this->accountRepos = app(AccountRepositoryInterface::class); + $this->currencyRepos = app(CurrencyRepositoryInterface::class); + + return $next($request); + } + ); + } + + /** @noinspection MoreThanThreeArgumentsInspection */ + /** + * @param Request $request + * @param Account $account + * @param Carbon $start + * @param Carbon $end + * + * @return JsonResponse + * + * @throws FireflyException + * @throws \Throwable + */ + public function overview(Request $request, Account $account, Carbon $start, Carbon $end): JsonResponse + { + if (AccountType::ASSET !== $account->accountType->type) { + throw new FireflyException(sprintf('Account %s is not an asset account.', $account->name)); + } + $startBalance = $request->get('startBalance'); + $endBalance = $request->get('endBalance'); + $transactionIds = $request->get('transactions') ?? []; + $clearedIds = $request->get('cleared') ?? []; + $amount = '0'; + $clearedAmount = '0'; + $route = route('accounts.reconcile.submit', [$account->id, $start->format('Ymd'), $end->format('Ymd')]); + // get sum of transaction amounts: + $transactions = $this->repository->getTransactionsById($transactionIds); + $cleared = $this->repository->getTransactionsById($clearedIds); + $countCleared = 0; + + /** @var Transaction $transaction */ + foreach ($transactions as $transaction) { + $amount = bcadd($amount, $transaction->amount); + } + + /** @var Transaction $transaction */ + foreach ($cleared as $transaction) { + if ($transaction->transactionJournal->date <= $end) { + $clearedAmount = bcadd($clearedAmount, $transaction->amount); + ++$countCleared; + } + } + $difference = bcadd(bcadd(bcsub($startBalance, $endBalance), $clearedAmount), $amount); + $diffCompare = bccomp($difference, '0'); + $return = [ + 'post_uri' => $route, + 'html' => view( + 'accounts.reconcile.overview', compact( + 'account', 'start', 'diffCompare', 'difference', 'end', 'clearedIds', 'transactionIds', 'clearedAmount', + 'startBalance', 'endBalance', 'amount', + 'route', 'countCleared' + ) + )->render(), + ]; + + return response()->json($return); + } + + + /** + * @param Account $account + * @param Carbon $start + * @param Carbon $end + * + * @return mixed + * + * @throws FireflyException + * @throws \Throwable + */ + public function transactions(Account $account, Carbon $start, Carbon $end) + { + if (AccountType::INITIAL_BALANCE === $account->accountType->type) { + return $this->redirectToOriginalAccount($account); + } + + $startDate = clone $start; + $startDate->subDays(1); + + $currencyId = (int)$this->accountRepos->getMetaValue($account, 'currency_id'); + $currency = $this->currencyRepos->findNull($currencyId); + if (0 === $currency) { + $currency = app('amount')->getDefaultCurrency(); // @codeCoverageIgnore + } + + $startBalance = round(app('steam')->balance($account, $startDate), $currency->decimal_places); + $endBalance = round(app('steam')->balance($account, $end), $currency->decimal_places); + + // get the transactions + $selectionStart = clone $start; + $selectionStart->subDays(3); + $selectionEnd = clone $end; + $selectionEnd->addDays(3); + + // grab transactions: + /** @var JournalCollectorInterface $collector */ + $collector = app(JournalCollectorInterface::class); + $collector->setAccounts(new Collection([$account])) + ->setRange($selectionStart, $selectionEnd)->withBudgetInformation()->withOpposingAccount()->withCategoryInformation(); + $transactions = $collector->getJournals(); + $html = view( + 'accounts.reconcile.transactions', compact('account', 'transactions', 'currency', 'start', 'end', 'selectionStart', 'selectionEnd') + )->render(); + + return response()->json(['html' => $html, 'startBalance' => $startBalance, 'endBalance' => $endBalance]); + } + + /** + * @param Account $account + * + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector + * + * @throws FireflyException + */ + private function redirectToOriginalAccount(Account $account) + { + /** @var Transaction $transaction */ + $transaction = $account->transactions()->first(); + if (null === $transaction) { + throw new FireflyException(sprintf('Expected a transaction. Account #%d has none. BEEP, error.', $account->id)); // @codeCoverageIgnore + } + + $journal = $transaction->transactionJournal; + /** @var Transaction $opposingTransaction */ + $opposingTransaction = $journal->transactions()->where('transactions.id', '!=', $transaction->id)->first(); + + if (null === $opposingTransaction) { + throw new FireflyException('Expected an opposing transaction. This account has none. BEEP, error.'); // @codeCoverageIgnore + } + + return redirect(route('accounts.show', [$opposingTransaction->account_id])); + } +} \ No newline at end of file diff --git a/app/Http/Middleware/IsDemoUser.php b/app/Http/Middleware/IsDemoUser.php index 4b77966803..adf759708e 100644 --- a/app/Http/Middleware/IsDemoUser.php +++ b/app/Http/Middleware/IsDemoUser.php @@ -26,6 +26,7 @@ use Closure; use FireflyIII\Repositories\User\UserRepositoryInterface; use FireflyIII\User; use Illuminate\Http\Request; +use Log; /** * Class IsDemoUser. @@ -51,6 +52,7 @@ class IsDemoUser /** @var UserRepositoryInterface $repository */ $repository = app(UserRepositoryInterface::class); if ($repository->hasRole($user, 'demo')) { + Log::info('User is a demo user.'); $request->session()->flash('info', (string)trans('firefly.not_available_demo_user')); $current = $request->url(); $previous = $request->session()->previousUrl(); diff --git a/routes/web.php b/routes/web.php index 84dd57281a..3232892b16 100755 --- a/routes/web.php +++ b/routes/web.php @@ -117,12 +117,14 @@ Route::group( // reconcile routes: Route::get('reconcile/{account}/index/{start_date?}/{end_date?}', ['uses' => 'Account\ReconcileController@reconcile', 'as' => 'reconcile']); - Route::get( - 'reconcile/{account}/transactions/{start_date?}/{end_date?}', ['uses' => 'Account\ReconcileController@transactions', 'as' => 'reconcile.transactions'] - ); - Route::get('reconcile/{account}/overview/{start_date?}/{end_date?}', ['uses' => 'Account\ReconcileController@overview', 'as' => 'reconcile.overview']); + + Route::post('reconcile/{account}/submit/{start_date?}/{end_date?}', ['uses' => 'Account\ReconcileController@submit', 'as' => 'reconcile.submit']); + // reconcile JSON routes + Route::get('reconcile/{account}/overview/{start_date?}/{end_date?}', ['uses' => 'Json\ReconcileController@overview', 'as' => 'reconcile.overview']); + Route::get('reconcile/{account}/transactions/{start_date?}/{end_date?}', ['uses' => 'Json\ReconcileController@transactions', 'as' => 'reconcile.transactions']); + // show reconciliation Route::get('reconcile/show/{tj}', ['uses' => 'Account\ReconcileController@show', 'as' => 'reconcile.show']); Route::get('reconcile/edit/{tj}', ['uses' => 'Account\ReconcileController@edit', 'as' => 'reconcile.edit']); diff --git a/tests/Feature/Controllers/Account/ReconcileControllerTest.php b/tests/Feature/Controllers/Account/ReconcileControllerTest.php index a3a33e47ab..e4476b9754 100644 --- a/tests/Feature/Controllers/Account/ReconcileControllerTest.php +++ b/tests/Feature/Controllers/Account/ReconcileControllerTest.php @@ -87,48 +87,6 @@ class ReconcileControllerTest extends TestCase $response->assertRedirect(route('transactions.edit', [$journal->id])); } - /** - * Test overview of reconciliation. - * - * @covers \FireflyIII\Http\Controllers\Account\ReconcileController - */ - public function testOverview(): void - { - $transactions = $this->user()->transactions()->inRandomOrder()->take(3)->get(); - $repository = $this->mock(JournalRepositoryInterface::class); - $repository->shouldReceive('firstNull')->andReturn(new TransactionJournal); - $repository->shouldReceive('getTransactionsById')->andReturn($transactions)->twice(); - - $parameters = [ - 'startBalance' => '0', - 'endBalance' => '10', - 'transactions' => [1, 2, 3], - 'cleared' => [4, 5, 6], - ]; - $this->be($this->user()); - $response = $this->get(route('accounts.reconcile.overview', [1, '20170101', '20170131']) . '?' . http_build_query($parameters)); - $response->assertStatus(200); - } - - /** - * Test overview when it's not an asset. - * - * @covers \FireflyIII\Http\Controllers\Account\ReconcileController - * @expectedExceptionMessage is not an asset account - */ - public function testOverviewNotAsset(): void - { - $account = $this->user()->accounts()->where('account_type_id', '!=', 3)->first(); - $parameters = [ - 'startBalance' => '0', - 'endBalance' => '10', - 'transactions' => [1, 2, 3], - 'cleared' => [4, 5, 6], - ]; - $this->be($this->user()); - $response = $this->get(route('accounts.reconcile.overview', [$account->id, '20170101', '20170131']) . '?' . http_build_query($parameters)); - $response->assertStatus(500); - } /** * Test showing the reconciliation. @@ -275,33 +233,6 @@ class ReconcileControllerTest extends TestCase $response->assertSessionHas('success'); } - /** - * List transactions for reconciliation view. - * - * @covers \FireflyIII\Http\Controllers\Account\ReconcileController - */ - public function testTransactions(): void - { - $repository = $this->mock(CurrencyRepositoryInterface::class); - $repository->shouldReceive('findNull')->once()->andReturn(TransactionCurrency::find(1)); - - $this->be($this->user()); - $response = $this->get(route('accounts.reconcile.transactions', [1, '20170101', '20170131'])); - $response->assertStatus(200); - } - - /** - * @covers \FireflyIII\Http\Controllers\Account\ReconcileController - */ - public function testTransactionsInitialBalance(): void - { - $transaction = Transaction::leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id') - ->where('accounts.user_id', $this->user()->id)->where('accounts.account_type_id', 6)->first(['account_id']); - $this->be($this->user()); - $response = $this->get(route('accounts.reconcile.transactions', [$transaction->account_id, '20170101', '20170131'])); - $response->assertStatus(302); - } - /** * @covers \FireflyIII\Http\Controllers\Account\ReconcileController * @covers \FireflyIII\Http\Requests\ReconciliationUpdateRequest diff --git a/tests/Feature/Controllers/Admin/UserControllerTest.php b/tests/Feature/Controllers/Admin/UserControllerTest.php index 3835627969..e5a7c24048 100644 --- a/tests/Feature/Controllers/Admin/UserControllerTest.php +++ b/tests/Feature/Controllers/Admin/UserControllerTest.php @@ -25,6 +25,7 @@ namespace Tests\Feature\Controllers\Admin; use FireflyIII\Repositories\User\UserRepositoryInterface; use Illuminate\Support\Collection; use Log; +use Mockery; use Tests\TestCase; /** @@ -47,7 +48,8 @@ class UserControllerTest extends TestCase public function testDelete(): void { $repository = $this->mock(UserRepositoryInterface::class); - $repository->shouldReceive('hasRole')->once()->andReturn(true); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true); $this->be($this->user()); $response = $this->get(route('admin.users.delete', [1])); $response->assertStatus(200); @@ -62,7 +64,8 @@ class UserControllerTest extends TestCase { $repository = $this->mock(UserRepositoryInterface::class); $repository->shouldReceive('destroy')->once(); - $repository->shouldReceive('hasRole')->once()->andReturn(true); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true); $this->be($this->user()); $response = $this->post(route('admin.users.destroy', ['2'])); @@ -76,6 +79,8 @@ class UserControllerTest extends TestCase public function testEdit(): void { $repository = $this->mock(UserRepositoryInterface::class); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true); $this->be($this->user()); $response = $this->get(route('admin.users.edit', [1])); $response->assertStatus(200); @@ -89,6 +94,8 @@ class UserControllerTest extends TestCase public function testIndex(): void { $repository = $this->mock(UserRepositoryInterface::class); + //$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->times(2)->andReturn(true); $user = $this->user(); $repository->shouldReceive('all')->andReturn(new Collection([$user])); @@ -105,6 +112,7 @@ class UserControllerTest extends TestCase public function testShow(): void { $repository = $this->mock(UserRepositoryInterface::class); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true); $repository->shouldReceive('getUserData')->andReturn( [ 'export_jobs_success' => 0, @@ -129,6 +137,8 @@ class UserControllerTest extends TestCase $repository->shouldReceive('changePassword')->once(); $repository->shouldReceive('changeStatus')->once(); $repository->shouldReceive('updateEmail')->once(); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true); $data = [ 'id' => 1, 'email' => 'test@example.com', diff --git a/tests/Feature/Controllers/BillControllerTest.php b/tests/Feature/Controllers/BillControllerTest.php index 5892a60dfc..67a2468328 100644 --- a/tests/Feature/Controllers/BillControllerTest.php +++ b/tests/Feature/Controllers/BillControllerTest.php @@ -243,19 +243,21 @@ class BillControllerTest extends TestCase } /** - * @covers \FireflyIII\Http\Controllers\BillController::store + * @covers \FireflyIII\Http\Controllers\BillController * @covers \FireflyIII\Http\Requests\BillFormRequest * @covers \FireflyIII\Http\Requests\Request */ public function testStore(): void { + $this->be($this->user()); + $bill = $this->user()->bills()->first(); // mock stuff $attachHelper = $this->mock(AttachmentHelperInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); $repository = $this->mock(BillRepositoryInterface::class); $ruleGroupRepos =$this->mock(RuleGroupRepositoryInterface::class); $journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal); - $repository->shouldReceive('store')->andReturn(new Bill); + $repository->shouldReceive('store')->andReturn($bill); $attachHelper->shouldReceive('saveAttachmentsForModel'); $attachHelper->shouldReceive('getMessages')->andReturn(new MessageBag); $ruleGroupRepos->shouldReceive('count')->andReturn(1); @@ -271,7 +273,7 @@ class BillControllerTest extends TestCase 'repeat_freq' => 'monthly', ]; $this->session(['bills.create.uri' => 'http://localhost']); - $this->be($this->user()); + $response = $this->post(route('bills.store'), $data); $response->assertStatus(302); $response->assertSessionHas('success'); @@ -289,8 +291,9 @@ class BillControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class); $repository = $this->mock(BillRepositoryInterface::class); $ruleGroupRepos =$this->mock(RuleGroupRepositoryInterface::class); + $bill = $this->user()->bills()->first(); $journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal); - $repository->shouldReceive('store')->andReturn(new Bill); + $repository->shouldReceive('store')->andReturn($bill); $attachHelper->shouldReceive('saveAttachmentsForModel'); $attachHelper->shouldReceive('getMessages')->andReturn(new MessageBag); $ruleGroupRepos->shouldReceive('count')->andReturn(1); diff --git a/tests/Feature/Controllers/Import/IndexControllerTest.php b/tests/Feature/Controllers/Import/IndexControllerTest.php index 10b8e6a942..e14c97e910 100644 --- a/tests/Feature/Controllers/Import/IndexControllerTest.php +++ b/tests/Feature/Controllers/Import/IndexControllerTest.php @@ -103,6 +103,7 @@ class IndexControllerTest extends TestCase $importJob = new ImportJob; $importJob->provider = 'fake'; $importJob->key = 'fake_job_1'; + $importJob->user_id = 1; // mock calls $userRepository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(true)->once(); @@ -135,6 +136,7 @@ class IndexControllerTest extends TestCase $importJob = new ImportJob; $importJob->provider = 'fake'; $importJob->key = 'fake_job_2'; + $importJob->user_id = 1; // mock call: $userRepository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(true)->once(); @@ -168,6 +170,7 @@ class IndexControllerTest extends TestCase $importJob = new ImportJob; $importJob->provider = 'file'; $importJob->key = 'file_job_1'; + $importJob->user_id =1; // mock calls $fakePrerequisites->shouldReceive('setUser')->once(); diff --git a/tests/Feature/Controllers/Json/ReconcileControllerTest.php b/tests/Feature/Controllers/Json/ReconcileControllerTest.php new file mode 100644 index 0000000000..25dd06716b --- /dev/null +++ b/tests/Feature/Controllers/Json/ReconcileControllerTest.php @@ -0,0 +1,122 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Feature\Controllers\Json; + + +use FireflyIII\Models\Transaction; +use FireflyIII\Models\TransactionCurrency; +use FireflyIII\Models\TransactionJournal; +use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; +use FireflyIII\Repositories\Journal\JournalRepositoryInterface; +use Log; +use Tests\TestCase; + +/** + * + * Class ReconcileControllerTest + */ +class ReconcileControllerTest extends TestCase +{ + /** + * + */ + public function setUp() + { + parent::setUp(); + Log::debug(sprintf('Now in %s.', \get_class($this))); + } + + /** + * Test overview of reconciliation. + * + * @covers \FireflyIII\Http\Controllers\Json\ReconcileController + */ + public function testOverview(): void + { + $transactions = $this->user()->transactions()->inRandomOrder()->take(3)->get(); + $repository = $this->mock(JournalRepositoryInterface::class); + $repository->shouldReceive('firstNull')->andReturn(new TransactionJournal); + $repository->shouldReceive('getTransactionsById')->andReturn($transactions)->twice(); + + $parameters = [ + 'startBalance' => '0', + 'endBalance' => '10', + 'transactions' => [1, 2, 3], + 'cleared' => [4, 5, 6], + ]; + $this->be($this->user()); + $response = $this->get(route('accounts.reconcile.overview', [1, '20170101', '20170131']) . '?' . http_build_query($parameters)); + $response->assertStatus(200); + } + + /** + * Test overview when it's not an asset. + * + * @covers \FireflyIII\Http\Controllers\Json\ReconcileController + * @expectedExceptionMessage is not an asset account + */ + public function testOverviewNotAsset(): void + { + $account = $this->user()->accounts()->where('account_type_id', '!=', 3)->first(); + $parameters = [ + 'startBalance' => '0', + 'endBalance' => '10', + 'transactions' => [1, 2, 3], + 'cleared' => [4, 5, 6], + ]; + $this->be($this->user()); + $response = $this->get(route('accounts.reconcile.overview', [$account->id, '20170101', '20170131']) . '?' . http_build_query($parameters)); + $response->assertStatus(500); + } + + + /** + * List transactions for reconciliation view. + * + * @covers \FireflyIII\Http\Controllers\Json\ReconcileController + */ + public function testTransactions(): void + { + $repository = $this->mock(CurrencyRepositoryInterface::class); + $repository->shouldReceive('findNull')->once()->andReturn(TransactionCurrency::find(1)); + + $this->be($this->user()); + $response = $this->get(route('accounts.reconcile.transactions', [1, '20170101', '20170131'])); + $response->assertStatus(200); + } + + /** + * @covers \FireflyIII\Http\Controllers\Json\ReconcileController + */ + public function testTransactionsInitialBalance(): void + { + $transaction = Transaction::leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id') + ->where('accounts.user_id', $this->user()->id)->where('accounts.account_type_id', 6)->first(['account_id']); + $this->be($this->user()); + $response = $this->get(route('accounts.reconcile.transactions', [$transaction->account_id, '20170101', '20170131'])); + $response->assertStatus(302); + } + + +} \ No newline at end of file diff --git a/tests/Feature/Controllers/Popup/ReportControllerTest.php b/tests/Feature/Controllers/Popup/ReportControllerTest.php index ab2366932d..3915aae03f 100644 --- a/tests/Feature/Controllers/Popup/ReportControllerTest.php +++ b/tests/Feature/Controllers/Popup/ReportControllerTest.php @@ -114,10 +114,10 @@ class ReportControllerTest extends TestCase $popupHelper = $this->mock(PopupReportInterface::class); $account = factory(Account::class)->make(); - $popupHelper->shouldReceive('balanceForNoBudget')->once()->andReturn(new Collection); + $popupHelper->shouldReceive('balanceForNoBudget')->andReturn(new Collection); $budgetRepos->shouldReceive('findNull')->andReturn(new Budget)->once()->withArgs([0]); $accountRepos->shouldReceive('findNull')->andReturn($account)->once()->withArgs([1]); - + $popupHelper->shouldReceive('balanceForBudget')->once()->andReturn(new Collection); $this->be($this->user()); $arguments = [ diff --git a/tests/Feature/Controllers/ProfileControllerTest.php b/tests/Feature/Controllers/ProfileControllerTest.php index d1d433b4aa..6f157eb889 100644 --- a/tests/Feature/Controllers/ProfileControllerTest.php +++ b/tests/Feature/Controllers/ProfileControllerTest.php @@ -158,27 +158,13 @@ class ProfileControllerTest extends TestCase $response->assertRedirect(route('profile.index')); } - /** - * @covers \FireflyIII\Http\Controllers\ProfileController - */ - public function testEnable2FADemo(): void - { - $repository = $this->mock(UserRepositoryInterface::class); - $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(true); - - $this->be($this->user()); - $response = $this->post(route('profile.enable2FA')); - $response->assertStatus(302); - $response->assertRedirect(route('profile.index')); - } - /** * @covers \FireflyIII\Http\Controllers\ProfileController */ public function testEnable2FANoSecret(): void { $repository = $this->mock(UserRepositoryInterface::class); - $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->twice()->andReturn(false); // ask about language: $langPreference = new Preference; @@ -220,7 +206,7 @@ class ProfileControllerTest extends TestCase public function testEnable2FASecret(): void { $repository = $this->mock(UserRepositoryInterface::class); - $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->twice()->andReturn(false); // ask about language: $langPreference = new Preference; @@ -291,6 +277,7 @@ class ProfileControllerTest extends TestCase $repository = $this->mock(UserRepositoryInterface::class); $repository->shouldReceive('findByEmail')->once()->andReturn(null); $repository->shouldReceive('changeEmail')->once()->andReturn(true); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false); $this->be($this->user()); $response = $this->post(route('profile.change-email.post'), $data); @@ -310,6 +297,7 @@ class ProfileControllerTest extends TestCase ]; $repository = $this->mock(UserRepositoryInterface::class); $repository->shouldReceive('findByEmail')->once()->andReturn(new User); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false); $this->be($this->user()); $response = $this->post(route('profile.change-email.post'), $data); @@ -324,6 +312,7 @@ class ProfileControllerTest extends TestCase public function testPostChangeEmailSame(): void { $repository = $this->mock(UserRepositoryInterface::class); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false); $data = [ 'email' => $this->user()->email, ]; @@ -345,6 +334,7 @@ class ProfileControllerTest extends TestCase $journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal); $repository = $this->mock(UserRepositoryInterface::class); $repository->shouldReceive('changePassword'); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false); $data = [ 'current_password' => 'james', @@ -368,6 +358,7 @@ class ProfileControllerTest extends TestCase $journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal); $repository = $this->mock(UserRepositoryInterface::class); $repository->shouldReceive('changePassword'); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false); $data = [ 'current_password' => 'james3', @@ -391,6 +382,7 @@ class ProfileControllerTest extends TestCase $journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal); $repository = $this->mock(UserRepositoryInterface::class); $repository->shouldReceive('changePassword'); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false); $data = [ 'current_password' => 'james', @@ -440,6 +432,7 @@ class ProfileControllerTest extends TestCase $journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal); $repository = $this->mock(UserRepositoryInterface::class); $repository->shouldReceive('destroy')->once(); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false); $data = [ 'password' => 'james', ]; @@ -458,6 +451,7 @@ class ProfileControllerTest extends TestCase $repository = $this->mock(UserRepositoryInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); $journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false); $data = [ 'password' => 'james2', ]; @@ -473,6 +467,8 @@ class ProfileControllerTest extends TestCase */ public function testRegenerate(): void { + $repository = $this->mock(UserRepositoryInterface::class); + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false); $token = ''; $currentToken = Preference::where('user_id', $this->user()->id)->where('name', 'access_token')->first(); if (null !== $currentToken) { diff --git a/tests/Feature/Controllers/TagControllerTest.php b/tests/Feature/Controllers/TagControllerTest.php index 7152e7fd48..2454b4bae9 100644 --- a/tests/Feature/Controllers/TagControllerTest.php +++ b/tests/Feature/Controllers/TagControllerTest.php @@ -264,7 +264,7 @@ class TagControllerTest extends TestCase $repository = $this->mock(TagRepositoryInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); $journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal); - $repository->shouldReceive('find')->andReturn(new Tag); + $repository->shouldReceive('findNull')->andReturn(null); $repository->shouldReceive('store')->andReturn(new Tag); $this->session(['tags.create.uri' => 'http://localhost']); @@ -299,7 +299,7 @@ class TagControllerTest extends TestCase ]; $repository->shouldReceive('update'); - $repository->shouldReceive('find')->andReturn(Tag::first()); + $repository->shouldReceive('findNull')->andReturn(Tag::first()); $this->be($this->user()); $response = $this->post(route('tags.update', [1]), $data); diff --git a/tests/Feature/Controllers/Transaction/MassControllerTest.php b/tests/Feature/Controllers/Transaction/MassControllerTest.php index ce6d3b0f01..202be2a26e 100644 --- a/tests/Feature/Controllers/Transaction/MassControllerTest.php +++ b/tests/Feature/Controllers/Transaction/MassControllerTest.php @@ -188,7 +188,7 @@ class MassControllerTest extends TestCase $repository = $this->mock(JournalRepositoryInterface::class); $repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal); $repository->shouldReceive('update')->once(); - $repository->shouldReceive('find')->once()->andReturn($deposit); + $repository->shouldReceive('findNull')->once()->andReturn($deposit); $repository->shouldReceive('getTransactionType')->andReturn('Deposit'); $repository->shouldReceive('getNoteText')->andReturn('Some note'); diff --git a/tests/Feature/Controllers/Transaction/SingleControllerTest.php b/tests/Feature/Controllers/Transaction/SingleControllerTest.php index 486c4e0492..836ae75cf3 100644 --- a/tests/Feature/Controllers/Transaction/SingleControllerTest.php +++ b/tests/Feature/Controllers/Transaction/SingleControllerTest.php @@ -84,6 +84,8 @@ class SingleControllerTest extends TestCase $journalRepos->shouldReceive('getJournalBudgetId')->andReturn(0); $journalRepos->shouldReceive('getJournalCategoryName')->andReturn(''); $journalRepos->shouldReceive('getTags')->andReturn([]); + $journalRepos->shouldReceive('getMetaField')->andReturnNull(); + $note = new Note(); $note->id = 5; diff --git a/tests/Feature/Controllers/TransactionControllerTest.php b/tests/Feature/Controllers/TransactionControllerTest.php index db10f10d27..a9dedac278 100644 --- a/tests/Feature/Controllers/TransactionControllerTest.php +++ b/tests/Feature/Controllers/TransactionControllerTest.php @@ -323,7 +323,7 @@ class TransactionControllerTest extends TestCase $journal->date = new Carbon('2016-01-01'); $repository = $this->mock(JournalRepositoryInterface::class); $repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal); - $repository->shouldReceive('find')->once()->andReturn($journal); + $repository->shouldReceive('findNull')->once()->andReturn($journal); $repository->shouldReceive('setOrder')->once()->andReturn(true); $data = [ diff --git a/tests/Unit/Handlers/Events/UserEventHandlerTest.php b/tests/Unit/Handlers/Events/UserEventHandlerTest.php index a74ac77800..d3cfe0a348 100644 --- a/tests/Unit/Handlers/Events/UserEventHandlerTest.php +++ b/tests/Unit/Handlers/Events/UserEventHandlerTest.php @@ -92,6 +92,8 @@ class UserEventHandlerTest extends TestCase $listener = new UserEventHandler(); // mock stuff + + $repository->shouldReceive('hasRole')->once()->andReturn(false); $repository->shouldReceive('count')->once()->andReturn(1); $repository->shouldReceive('getRole')->once()->andReturn(null); $repository->shouldReceive('attachRole')->once()->withArgs([Mockery::any(), 'owner']); @@ -112,6 +114,7 @@ class UserEventHandlerTest extends TestCase $listener = new UserEventHandler(); // mock stuff + $repository->shouldReceive('hasRole')->once()->andReturn(false); $repository->shouldReceive('count')->once()->andReturn(1); $repository->shouldReceive('getRole')->once()->andReturn(new Role); $repository->shouldReceive('attachRole')->once()->withArgs([Mockery::any(), 'owner']); @@ -131,6 +134,7 @@ class UserEventHandlerTest extends TestCase $listener = new UserEventHandler(); // mock stuff + $repository->shouldReceive('hasRole')->once()->andReturn(true); $repository->shouldReceive('count')->once()->andReturn(1); $listener->checkSingleUserIsAdmin($event); diff --git a/tests/Unit/Handlers/Events/VersionCheckEventHandlerTest.php b/tests/Unit/Handlers/Events/VersionCheckEventHandlerTest.php index 41416cfe7b..1f4399ba6f 100644 --- a/tests/Unit/Handlers/Events/VersionCheckEventHandlerTest.php +++ b/tests/Unit/Handlers/Events/VersionCheckEventHandlerTest.php @@ -59,10 +59,11 @@ class VersionCheckEventHandlerTest extends TestCase // report on config variables: FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check', -1])->once()->andReturn($updateConfig); FireflyConfig::shouldReceive('get')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig); + FireflyConfig::shouldReceive('set')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig); // request thing: - $request->shouldReceive('call')->once(); - $request->shouldReceive('getReleases')->once()->andThrow(new FireflyException('Errrr')); + $request->shouldReceive('call')->once()->andThrow(new FireflyException('Errrr')); + $request->shouldReceive('getReleases')->once(); $handler = new VersionCheckEventHandler; @@ -140,6 +141,7 @@ class VersionCheckEventHandlerTest extends TestCase // report on config variables: FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check', -1])->once()->andReturn($updateConfig); FireflyConfig::shouldReceive('get')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig); + FireflyConfig::shouldReceive('set')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig); $handler = new VersionCheckEventHandler; $handler->checkForUpdates($event); @@ -164,6 +166,7 @@ class VersionCheckEventHandlerTest extends TestCase // report on config variables: FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check', -1])->once()->andReturn($updateConfig); FireflyConfig::shouldReceive('get')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig); + FireflyConfig::shouldReceive('set')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig); $handler = new VersionCheckEventHandler; $handler->checkForUpdates($event); diff --git a/tests/Unit/Middleware/SandstormTest.php b/tests/Unit/Middleware/SandstormTest.php index e7afb03da5..47ff386cd5 100644 --- a/tests/Unit/Middleware/SandstormTest.php +++ b/tests/Unit/Middleware/SandstormTest.php @@ -127,8 +127,8 @@ class SandstormTest extends TestCase $repository = $this->mock(UserRepositoryInterface::class); $repository->shouldReceive('count')->twice()->andReturn(0); $repository->shouldReceive('store')->once()->andReturn($this->user()); - $repository->shouldReceive('attachRole')->twice()->andReturn(true); - $repository->shouldReceive('getRole')->once()->andReturn(new Role); + $repository->shouldReceive('attachRole')->once()->andReturn(true); + //$repository->shouldReceive('getRole')->once()->andReturn(new Role); $repository->shouldReceive('hasRole')->andReturn(false); $response = $this->get('/_test/sandstorm', ['X-Sandstorm-User-Id' => 'abcd']); diff --git a/tests/Unit/TransactionRules/Triggers/ToAccountStartsTest.php b/tests/Unit/TransactionRules/Triggers/ToAccountStartsTest.php index 8f5ed5a155..dfa3f641bb 100644 --- a/tests/Unit/TransactionRules/Triggers/ToAccountStartsTest.php +++ b/tests/Unit/TransactionRules/Triggers/ToAccountStartsTest.php @@ -72,10 +72,12 @@ class ToAccountStartsTest extends TestCase $count = $journal->transactions()->where('amount', '>', 0)->count(); $transaction = $journal->transactions()->where('amount', '>', 0)->first(); $account = null === $transaction ? null : $transaction->account; + Log::debug(sprintf('Journal with id #%d', $journal->id)); Log::debug(sprintf('Count of transactions is %d', $count)); Log::debug(sprintf('Account is null: %s', var_export(null === $account, true))); - } while ($loopCount < 30 && $count !== 1 && null !== $account); + + } while ($loopCount < 30 && $count < 1 && null !== $account); Log::debug(sprintf('Loop has ended. loopCount is %d', $loopCount)); $trigger = ToAccountStarts::makeFromStrings('bla-bla-bla' . $account->name, false);