diff --git a/app/Http/Controllers/Chart/AccountController.php b/app/Http/Controllers/Chart/AccountController.php index ae14e368f0..bd862d3012 100644 --- a/app/Http/Controllers/Chart/AccountController.php +++ b/app/Http/Controllers/Chart/AccountController.php @@ -48,14 +48,10 @@ class AccountController extends Controller $chartProperties->addProperty($end); $chartProperties->addProperty('all'); $chartProperties->addProperty('accounts'); - $md5 = $chartProperties->md5(); - - - if (Cache::has($md5)) { - Log::debug('Successfully returned cached chart [' . $md5 . '].'); - - return Response::json(Cache::get($md5)); + if ($chartProperties->has()) { + return Response::json($chartProperties->get()); } + $md5 = $chartProperties->getMd5(); $chart->addColumn(trans('firefly.dayOfMonth'), 'date'); @@ -123,14 +119,10 @@ class AccountController extends Controller $chartProperties->addProperty($end); $chartProperties->addProperty('frontpage'); $chartProperties->addProperty('accounts'); - $md5 = $chartProperties->md5(); - - - if (Cache::has($md5)) { - Log::debug('Successfully returned cached chart [' . $md5 . '].'); - - return Response::json(Cache::get($md5)); + if ($chartProperties->has()) { + return Response::json($chartProperties->get()); } + $md5 = $chartProperties->getMd5(); $index = 1; @@ -188,14 +180,10 @@ class AccountController extends Controller $chartProperties->addProperty('frontpage'); $chartProperties->addProperty('single'); $chartProperties->addProperty($account->id); - $md5 = $chartProperties->md5(); - - - if (Cache::has($md5)) { - Log::debug('Successfully returned cached chart [' . $md5 . '].'); - - return Response::json(Cache::get($md5)); + if ($chartProperties->has()) { + return Response::json($chartProperties->get()); } + $md5 = $chartProperties->getMd5(); diff --git a/app/Http/Controllers/Chart/BillController.php b/app/Http/Controllers/Chart/BillController.php index d79c879350..7950e294ed 100644 --- a/app/Http/Controllers/Chart/BillController.php +++ b/app/Http/Controllers/Chart/BillController.php @@ -44,13 +44,10 @@ class BillController extends Controller $chartProperties->addProperty('single'); $chartProperties->addProperty('bill'); $chartProperties->addProperty($bill->id); - $md5 = $chartProperties->md5(); - - if (Cache::has($md5)) { - Log::debug('Successfully returned cached chart [' . $md5 . '].'); - - return Response::json(Cache::get($md5)); + if ($chartProperties->has()) { + return Response::json($chartProperties->get()); } + $md5 = $chartProperties->getMd5(); // get first transaction or today for start: $results = $repository->getJournals($bill); @@ -92,13 +89,10 @@ class BillController extends Controller $chartProperties->addProperty($end); $chartProperties->addProperty('bills'); $chartProperties->addProperty('frontpage'); - $md5 = $chartProperties->md5(); - - if (Cache::has($md5)) { - Log::debug('Successfully returned cached chart [' . $md5 . '].'); - - return Response::json(Cache::get($md5)); + if ($chartProperties->has()) { + return Response::json($chartProperties->get()); } + $md5 = $chartProperties->getMd5(); $bills = $repository->getActiveBills(); $paid = new Collection; // journals. diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index 9627a53c07..731f87b4de 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -50,13 +50,10 @@ class BudgetController extends Controller $chartProperties->addProperty($last); $chartProperties->addProperty('budget'); $chartProperties->addProperty('budget'); - $md5 = $chartProperties->md5(); - - if (Cache::has($md5)) { - Log::debug('Successfully returned cached chart [' . $md5 . '].'); - - return Response::json(Cache::get($md5)); + if ($chartProperties->has()) { + return Response::json($chartProperties->get()); } + $md5 = $chartProperties->getMd5(); while ($first < $last) { @@ -100,13 +97,10 @@ class BudgetController extends Controller $chartProperties->addProperty('limit'); $chartProperties->addProperty($budget->id); $chartProperties->addProperty($repetition->id); - $md5 = $chartProperties->md5(); - - if (Cache::has($md5)) { - Log::debug('Successfully returned cached chart [' . $md5 . '].'); - - return Response::json(Cache::get($md5)); + if ($chartProperties->has()) { + return Response::json($chartProperties->get()); } + $md5 = $chartProperties->getMd5(); $chart->addColumn(trans('firefly.day'), 'date'); $chart->addColumn(trans('firefly.left'), 'number'); @@ -158,13 +152,10 @@ class BudgetController extends Controller $chartProperties->addProperty($end); $chartProperties->addProperty('budget'); $chartProperties->addProperty('all'); - $md5 = $chartProperties->md5(); - - if (Cache::has($md5)) { - Log::debug('Successfully returned cached chart [' . $md5 . '].'); - - return Response::json(Cache::get($md5)); + if ($chartProperties->has()) { + return Response::json($chartProperties->get()); } + $md5 = $chartProperties->getMd5(); /** @var Budget $budget */ @@ -232,14 +223,10 @@ class BudgetController extends Controller $chartProperties->addProperty($end); $chartProperties->addProperty('budget'); $chartProperties->addProperty('year'); - $md5 = $chartProperties->md5(); - - - if (Cache::has($md5)) { - Log::debug('Successfully returned cached chart [' . $md5 . '].'); - - return Response::json(Cache::get($md5)); + if ($chartProperties->has()) { + return Response::json($chartProperties->get()); } + $md5 = $chartProperties->getMd5(); // add columns: $chart->addColumn(trans('firefly.month'), 'date'); diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index b86eb143f3..5d0a57b031 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -85,14 +85,10 @@ class CategoryController extends Controller $chartProperties->addProperty($end); $chartProperties->addProperty('category'); $chartProperties->addProperty('frontpage'); - $md5 = $chartProperties->md5(); - - - if (Cache::has($md5)) { - Log::debug('Successfully returned cached chart [' . $md5 . '].'); - - return Response::json(Cache::get($md5)); + if ($chartProperties->has()) { + return Response::json($chartProperties->get()); } + $md5 = $chartProperties->getMd5(); $set = $repository->getCategoriesAndExpensesCorrected($start, $end); diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 7ffa3d9b4e..17472183d5 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -41,16 +41,6 @@ class HomeController extends Controller { Session::clear(); - // encrypt transaction journal description - $set = TransactionJournal::where('encrypted', 0)->take(100)->get(); - /** @var TransactionJournal $entry */ - foreach ($set as $entry) { - $description = $entry->description; - $entry->description = $description; - $entry->save(); - } - unset($set, $entry, $description); - return Redirect::route('index'); } diff --git a/app/Http/Controllers/JsonController.php b/app/Http/Controllers/JsonController.php index 1908ed7dcf..9d0295411a 100644 --- a/app/Http/Controllers/JsonController.php +++ b/app/Http/Controllers/JsonController.php @@ -13,7 +13,6 @@ use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\Tag\TagRepositoryInterface; use FireflyIII\Support\CacheProperties; use Illuminate\Support\Collection; -use Log; use Response; use Session; use Steam; @@ -44,12 +43,10 @@ class JsonController extends Controller $prop->addProperty($start); $prop->addProperty($end); $prop->addProperty('box-bills-paid'); - $md5 = $prop->md5(); - if (Cache::has($md5)) { - Log::debug('Successfully returned cached box bills-paid [' . $md5 . ']'); - - return Response::json(Cache::get($md5)); + if ($prop->has()) { + return Response::json($prop->get()); } + $md5 = $prop->getMd5(); $amount = 0; @@ -100,12 +97,10 @@ class JsonController extends Controller $prop->addProperty($start); $prop->addProperty($end); $prop->addProperty('box-bills-unpaid'); - $md5 = $prop->md5(); - if (Cache::has($md5)) { - Log::debug('Successfully returned cached box bills-unpaid [' . $md5 . ']'); - - return Response::json(Cache::get($md5)); + if ($prop->has()) { + return Response::json($prop->get()); } + $md5 = $prop->getMd5(); $bills = $repository->getActiveBills(); $unpaid = new Collection; // bills @@ -162,13 +157,10 @@ class JsonController extends Controller $prop->addProperty($start); $prop->addProperty($end); $prop->addProperty('box-in'); - $md5 = $prop->md5(); - if (Cache::has($md5)) { - Log::debug('Successfully returned cached box in [' . $md5 . ']'); - - return Response::json(Cache::get($md5)); + if ($prop->has()) { + return Response::json($prop->get()); } - + $md5 = $prop->getMd5(); $amount = $reportQuery->incomeInPeriodCorrected($start, $end, true)->sum('amount'); @@ -194,12 +186,10 @@ class JsonController extends Controller $prop->addProperty($start); $prop->addProperty($end); $prop->addProperty('box-out'); - $md5 = $prop->md5(); - if (Cache::has($md5)) { - Log::debug('Successfully returned cached box out [' . $md5 . ']'); - - return Response::json(Cache::get($md5)); + if ($prop->has()) { + return Response::json($prop->get()); } + $md5 = $prop->getMd5(); $amount = $reportQuery->expenseInPeriodCorrected($start, $end, true)->sum('amount'); diff --git a/app/Http/Middleware/Reminders.php b/app/Http/Middleware/Reminders.php index eb161a4123..5f663e84c4 100644 --- a/app/Http/Middleware/Reminders.php +++ b/app/Http/Middleware/Reminders.php @@ -57,14 +57,13 @@ class Reminders $properties = new CacheProperties; $properties->addProperty('reminders'); - $md5 = $properties->md5(); - - if (Cache::has($md5)) { - $reminders = Cache::get($md5); + if ($properties->has()) { + $reminders = $properties->get(); View::share('reminders', $reminders); return $next($request); } + $md5 = $properties->getMd5(); $piggyBanks = $this->auth->user()->piggyBanks()->where('remind_me', 1)->get(); diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php index b4fc946b3e..60f76d1cbe 100644 --- a/app/Models/TransactionJournal.php +++ b/app/Models/TransactionJournal.php @@ -139,10 +139,10 @@ class TransactionJournal extends Model $prop = new CacheProperties(); $prop->addProperty($this->id); $prop->addProperty('amount'); - $md5 = $prop->md5(); - if (Cache::has($md5)) { - return Cache::get($md5); + if ($prop->has()) { + return $prop->get(); } + $md5 = $prop->getMd5(); $amount = '0'; diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index 02559a82d9..352a89b756 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -114,10 +114,10 @@ class AccountRepository implements AccountRepositoryInterface $cache = new CacheProperties(); $cache->addProperty($preference->data); $cache->addProperty('frontPageaccounts'); - $md5 = $cache->md5(); - if (Cache::has($md5)) { - return Cache::get($md5); + if ($cache->has()) { + return $cache->get(); } + $md5 = $cache->getMd5(); if ($preference->data == []) { @@ -148,10 +148,10 @@ class AccountRepository implements AccountRepositoryInterface $prop->addProperty($account->id); $prop->addProperty($start); $prop->addProperty($end); - $md5 = $prop->md5(); - if(Cache::has($md5)) { - return Cache::get($md5); + if ($prop->has()) { + return $prop->get(); } + $md5 = $prop->getMd5(); $set = Auth::user() ->transactionjournals() @@ -167,6 +167,7 @@ class AccountRepository implements AccountRepositoryInterface ->take(10) ->get(['transaction_journals.*', 'transaction_currencies.symbol', 'transaction_types.type']); Cache::forever($md5, $set); + return $set; } @@ -234,10 +235,10 @@ class AccountRepository implements AccountRepositoryInterface $cache = new CacheProperties; $cache->addProperty($ids); $cache->addProperty('piggyAccounts'); - $md5 = $cache->md5(); - if (Cache::has($md5)) { - return Cache::get($md5); + if ($cache->has()) { + return $cache->get(); } + $md5 = $cache->getMd5(); $ids = array_unique($ids); if (count($ids) > 0) { diff --git a/app/Support/CacheProperties.php b/app/Support/CacheProperties.php index 699d7e9a26..f7f7018b44 100644 --- a/app/Support/CacheProperties.php +++ b/app/Support/CacheProperties.php @@ -4,6 +4,7 @@ namespace FireflyIII\Support; use Auth; +use Cache; use Carbon\Carbon; use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Support\Collection; @@ -17,6 +18,8 @@ use Preferences as Prefs; class CacheProperties { + /** @var string */ + protected $md5 = ''; /** @var Collection */ protected $properties; @@ -38,39 +41,62 @@ class CacheProperties $this->properties->push($property); } + /** + * @return mixed + */ + public function get() + { + return Cache::get($this->md5); + } /** * @return string */ - public function md5() + public function getMd5() + { + return $this->md5; + } + + /** + * @return bool + */ + public function has() + { + $this->md5(); + + return Cache::has($this->md5); + } + + /** + * @return void + */ + private function md5() { - $string = ''; - //Log::debug('--- building string ---'); foreach ($this->properties as $property) { if ($property instanceof Collection || $property instanceof EloquentCollection) { - $string .= print_r($property->toArray(), true); + $this->md5 .= print_r($property->toArray(), true); continue; } if ($property instanceof Carbon) { - $string .= $property->toRfc3339String(); + $this->md5 .= $property->toRfc3339String(); continue; } if (is_array($property)) { - $string .= print_r($property, true); + $this->md5 .= print_r($property, true); continue; } if (is_object($property)) { - $string .= $property->__toString(); + $this->md5 .= $property->__toString(); } if (is_array($property)) { - $string .= print_r($property, true); + $this->md5 .= print_r($property, true); } - $string .= (string)$property; + $this->md5 .= (string)$property; } - return md5($string); + $this->md5 = md5($this->md5); } } \ No newline at end of file diff --git a/app/Support/Steam.php b/app/Support/Steam.php index 6e61e3cff5..5e3804864b 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -30,10 +30,10 @@ class Steam $properties->addProperty('balance'); $properties->addProperty($date); $properties->addProperty($ignoreVirtualBalance); - $md5 = $properties->md5(); - if (Cache::has($md5)) { - return Cache::get($md5); + if ($properties->has()) { + return $properties->get(); } + $md5 = $properties->getMd5(); // find the first known transaction on this account: diff --git a/app/Support/Twig/Journal.php b/app/Support/Twig/Journal.php index ba03775a5d..7e53787fde 100644 --- a/app/Support/Twig/Journal.php +++ b/app/Support/Twig/Journal.php @@ -33,10 +33,10 @@ class Journal extends Twig_Extension $prop = new CacheProperties(); $prop->addProperty($journal->id); $prop->addProperty('typeIcon'); - $md5 = $prop->md5(); - if (Cache::has($md5)) { - return Cache::get($md5); + if ($prop->has()) { + return $prop->get(); } + $md5 = $prop->getMd5(); $type = $journal->transactionType->type; diff --git a/tests/controllers/AccountControllerTest.php b/tests/controllers/AccountControllerTest.php index 0ff00f50ba..d1b548e774 100644 --- a/tests/controllers/AccountControllerTest.php +++ b/tests/controllers/AccountControllerTest.php @@ -1,9 +1,6 @@ once()->andReturn($currency); Amount::shouldReceive('getAllCurrencies')->once()->andReturn([$currency]); Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); $this->call('GET', '/accounts/create/asset'); $this->assertResponseOk(); diff --git a/tests/controllers/BudgetControllerTest.php b/tests/controllers/BudgetControllerTest.php index 01f8b7c9d4..84cf4eda92 100644 --- a/tests/controllers/BudgetControllerTest.php +++ b/tests/controllers/BudgetControllerTest.php @@ -194,6 +194,9 @@ class BudgetControllerTest extends TestCase $date = Carbon::now()->startOfMonth()->format('FY'); Preferences::shouldReceive('set')->once()->withArgs(['budgetIncomeTotal' . $date, 1001]); Preferences::shouldReceive('mark')->once()->andReturn(true); + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); // language preference: $language = FactoryMuffin::create('FireflyIII\Models\Preference'); @@ -386,6 +389,9 @@ class BudgetControllerTest extends TestCase Amount::shouldReceive('format')->andReturn('xx'); Amount::shouldReceive('getCurrencyCode')->andReturn('X'); Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); // language preference: $language = FactoryMuffin::create('FireflyIII\Models\Preference'); diff --git a/tests/controllers/HomeControllerTest.php b/tests/controllers/HomeControllerTest.php index 71444d6fef..4ec8a289d1 100644 --- a/tests/controllers/HomeControllerTest.php +++ b/tests/controllers/HomeControllerTest.php @@ -108,6 +108,10 @@ class HomeControllerTest extends TestCase $language->save(); Preferences::shouldReceive('get')->withAnyArgs()->andReturn($language); + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + Amount::shouldReceive('getCurrencyCode')->andReturn('EUR'); Amount::shouldReceive('format')->andReturn('xxx'); diff --git a/tests/controllers/PreferencesControllerTest.php b/tests/controllers/PreferencesControllerTest.php index af3b9d1ab9..93ec1fa3ec 100644 --- a/tests/controllers/PreferencesControllerTest.php +++ b/tests/controllers/PreferencesControllerTest.php @@ -62,6 +62,10 @@ class PreferencesControllerTest extends TestCase Amount::shouldReceive('getAllCurrencies')->andReturn(new Collection); Amount::shouldReceive('getDefaultCurrency')->andReturn($currency); + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + // language preference: $language = FactoryMuffin::create('FireflyIII\Models\Preference'); $language->data = 'en'; @@ -105,6 +109,10 @@ class PreferencesControllerTest extends TestCase $language->save(); Preferences::shouldReceive('get')->withAnyArgs()->andReturn($language); + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + $this->call('POST', '/preferences', $data); $this->assertResponseStatus(302); diff --git a/tests/controllers/charts/ChartBillControllerTest.php b/tests/controllers/charts/ChartBillControllerTest.php index 884c4627ff..b7f3ec3f9d 100644 --- a/tests/controllers/charts/ChartBillControllerTest.php +++ b/tests/controllers/charts/ChartBillControllerTest.php @@ -60,6 +60,14 @@ class ChartBillControllerTest extends TestCase $repository->shouldReceive('createFakeBill')->andReturn($bills->first()); Steam::shouldReceive('balance')->andReturn(-10, 0); + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + + $language = FactoryMuffin::create('FireflyIII\Models\Preference'); + $language->data = 'en'; + Preferences::shouldReceive('get')->withArgs(['language', 'en'])->andReturn($language); + $this->call('GET', '/chart/bill/frontpage'); $this->assertResponseOk(); diff --git a/tests/helpers/ReportHelperTest.php b/tests/helpers/ReportHelperTest.php index 3598c959eb..7adfaddf77 100644 --- a/tests/helpers/ReportHelperTest.php +++ b/tests/helpers/ReportHelperTest.php @@ -111,6 +111,9 @@ class ReportHelperTest extends TestCase */ public function testGetBillReport() { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + // factory! $set = new Collection; $journals = new Collection; diff --git a/tests/models/TransactionJournalModelTest.php b/tests/models/TransactionJournalModelTest.php index d05f285529..26c98d80be 100644 --- a/tests/models/TransactionJournalModelTest.php +++ b/tests/models/TransactionJournalModelTest.php @@ -62,6 +62,8 @@ class TransactionJournalModelTest extends TestCase */ public function testGetAmountAttributeAdvancePayment() { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); // make types: $withdrawalType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); $depositType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); @@ -120,6 +122,9 @@ class TransactionJournalModelTest extends TestCase */ public function testGetAmountAttributeBalancingAct() { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + // make types: $withdrawalType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); FactoryMuffin::create('FireflyIII\Models\TransactionType'); @@ -177,6 +182,8 @@ class TransactionJournalModelTest extends TestCase */ public function testGetAmountAttributeNoTags() { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); $journal->transactions[0]->amount = 123.45; @@ -194,6 +201,9 @@ class TransactionJournalModelTest extends TestCase */ public function testGetAmountAttributeTag() { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + // has a normal tag, but nothing special. // make tag $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); diff --git a/tests/repositories/BillRepositoryTest.php b/tests/repositories/BillRepositoryTest.php index 5c1de9fe92..2e22d58e7a 100644 --- a/tests/repositories/BillRepositoryTest.php +++ b/tests/repositories/BillRepositoryTest.php @@ -38,6 +38,9 @@ class BillRepositoryTest extends TestCase public function testBillPaymentsInRange() { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); $start = Carbon::now()->startOfMonth(); $end = Carbon::now()->endOfMonth(); diff --git a/tests/repositories/JournalRepositoryTest.php b/tests/repositories/JournalRepositoryTest.php index 375b06e70d..e0bc5443b5 100644 --- a/tests/repositories/JournalRepositoryTest.php +++ b/tests/repositories/JournalRepositoryTest.php @@ -446,6 +446,9 @@ class JournalRepositoryTest extends TestCase */ public function testUpdate() { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + for ($i = 0; $i < 4; $i++) { FactoryMuffin::create('FireflyIII\Models\AccountType'); } @@ -504,6 +507,9 @@ class JournalRepositoryTest extends TestCase */ public function testUpdateNoTags() { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + for ($i = 0; $i < 4; $i++) { FactoryMuffin::create('FireflyIII\Models\AccountType'); }