diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php
index 627fbf4d0c..a2926001fb 100644
--- a/app/Http/Controllers/HomeController.php
+++ b/app/Http/Controllers/HomeController.php
@@ -3,6 +3,7 @@
use Auth;
use Cache;
use Carbon\Carbon;
+use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Input;
use Preferences;
use Redirect;
@@ -51,9 +52,10 @@ class HomeController extends Controller
/**
* @return \Illuminate\View\View
*/
- public function index()
+ public function index(AccountRepositoryInterface $repository)
{
- $count = Auth::user()->accounts()->accountTypeIn(['Asset account', 'Default account'])->count();
+
+ $count = $repository->countAssetAccounts();
$title = 'Firefly';
$subTitle = 'What\'s playing?';
$mainTitleIcon = 'fa-fire';
@@ -61,26 +63,10 @@ class HomeController extends Controller
$frontPage = Preferences::get('frontPageAccounts', []);
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
-
-
- if ($frontPage->data == []) {
- $accounts = Auth::user()->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
- } else {
- $accounts = Auth::user()->accounts()->whereIn('id', $frontPage->data)->get(['accounts.*']);
- }
+ $accounts = $repository->getFrontpageAccounts($frontPage);
foreach ($accounts as $account) {
- $set = Auth::user()
- ->transactionjournals()
- ->with(['transactions', 'transactioncurrency', 'transactiontype'])
- ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
- ->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')->where('accounts.id', $account->id)
- ->before($end)
- ->after($start)
- ->orderBy('transaction_journals.date', 'DESC')
- ->orderBy('transaction_journals.id', 'DESC')
- ->take(10)
- ->get(['transaction_journals.*']);
+ $set = $repository->getFrontpageTransactions($account, $start, $end);
if (count($set) > 0) {
$transactions[] = [$set, $account];
}
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php
index 7cb8e23608..506f0b9ddd 100644
--- a/app/Repositories/Account/AccountRepository.php
+++ b/app/Repositories/Account/AccountRepository.php
@@ -10,10 +10,12 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\PiggyBank;
+use FireflyIII\Models\Preference;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use Illuminate\Pagination\LengthAwarePaginator;
+use Illuminate\Support\Collection;
use Log;
use Session;
@@ -25,6 +27,14 @@ use Session;
class AccountRepository implements AccountRepositoryInterface
{
+ /**
+ * @return int
+ */
+ public function countAssetAccounts()
+ {
+ return Auth::user()->accounts()->accountTypeIn(['Asset account', 'Default account'])->count();
+ }
+
/**
* @param Account $account
*
@@ -37,6 +47,44 @@ class AccountRepository implements AccountRepositoryInterface
return true;
}
+ /**
+ * @param Preference $preference
+ *
+ * @return Collection
+ */
+ public function getFrontpageAccounts(Preference $preference)
+ {
+ if ($preference->data == []) {
+ $accounts = Auth::user()->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
+ } else {
+ $accounts = Auth::user()->accounts()->whereIn('id', $preference->data)->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
+ }
+
+ return $accounts;
+ }
+
+ /**
+ * @param Account $account
+ * @param Carbon $start
+ * @param Carbon $end
+ *
+ * @return mixed
+ */
+ public function getFrontpageTransactions(Account $account, Carbon $start, Carbon $end)
+ {
+ return Auth::user()
+ ->transactionjournals()
+ ->with(['transactions', 'transactioncurrency', 'transactiontype'])
+ ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
+ ->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')->where('accounts.id', $account->id)
+ ->before($end)
+ ->after($start)
+ ->orderBy('transaction_journals.date', 'DESC')
+ ->orderBy('transaction_journals.id', 'DESC')
+ ->take(10)
+ ->get(['transaction_journals.*']);
+ }
+
/**
* @param Account $account
* @param int $page
diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php
index 644f67744d..020ae41b69 100644
--- a/app/Repositories/Account/AccountRepositoryInterface.php
+++ b/app/Repositories/Account/AccountRepositoryInterface.php
@@ -4,7 +4,9 @@ namespace FireflyIII\Repositories\Account;
use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionJournal;
-
+use FireflyIII\Models\Preference;
+use Illuminate\Support\Collection;
+use Carbon\Carbon;
/**
* Interface AccountRepositoryInterface
*
@@ -19,6 +21,27 @@ interface AccountRepositoryInterface
*/
public function destroy(Account $account);
+ /**
+ * @return int
+ */
+ public function countAssetAccounts();
+
+ /**
+ * @param Preference $preference
+ *
+ * @return Collection
+ */
+ public function getFrontpageAccounts(Preference $preference);
+
+ /**
+ * @param Account $account
+ * @param Carbon $start
+ * @param Carbon $end
+ *
+ * @return mixed
+ */
+ public function getFrontpageTransactions(Account $account, Carbon $start, Carbon $end);
+
/**
* @param Account $account
* @param string $range
diff --git a/app/Support/Preferences.php b/app/Support/Preferences.php
index 98cab769c9..185fea9512 100644
--- a/app/Support/Preferences.php
+++ b/app/Support/Preferences.php
@@ -45,12 +45,12 @@ class Preferences
if (is_null($pref)) {
$pref = new Preference;
$pref->name = $name;
- $pref->user()->associate(Auth::user());
-
}
$pref->data = $value;
- $pref->save();
-
+ if (!is_null(Auth::user()->id)) {
+ $pref->user()->associate(Auth::user());
+ $pref->save();
+ }
return $pref;
diff --git a/phpunit.xml b/phpunit.xml
index a440863019..7fd14d7b0c 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -11,7 +11,7 @@
syntaxCheck="false">
- ./tests/
+ ./tests
@@ -21,9 +21,11 @@
+
diff --git a/tests/TestCase.php b/tests/TestCase.php
index 76bcb54c16..971e451ea7 100644
--- a/tests/TestCase.php
+++ b/tests/TestCase.php
@@ -13,7 +13,7 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
*/
public function createApplication()
{
- $app = require __DIR__ . '/../../bootstrap/app.php';
+ $app = require __DIR__ . '/../bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
diff --git a/tests/controllers/HomeControllerTest.php b/tests/controllers/HomeControllerTest.php
new file mode 100644
index 0000000000..cb03ff6fbd
--- /dev/null
+++ b/tests/controllers/HomeControllerTest.php
@@ -0,0 +1,68 @@
+markTestIncomplete('This test has not been implemented yet.');
+ }
+
+ /**
+ * @covers FireflyIII\Http\Controllers\HomeController::flush
+ */
+ public function testFlush()
+ {
+ // Remove the following lines when you implement this test.
+ $this->markTestIncomplete('This test has not been implemented yet.');
+ }
+
+ /**
+ * @covers FireflyIII\Http\Controllers\HomeController::index
+ */
+ public function testIndexNoLogin()
+ {
+ $response = $this->call('GET', '/');
+ $this->assertRedirectedTo('auth/login');
+
+ }
+
+ /**
+ * @covers FireflyIII\Http\Controllers\HomeController::index
+ */
+ public function testIndexLoggedIn()
+ {
+ $this->be(new FireflyIII\User);
+ $response = $this->call('GET', '/');
+ $this->assertResponseOk();
+
+ }
+
+}