mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-01 18:10:06 +00:00
Covered the reminder controller.
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
<?php namespace FireflyIII\Http\Controllers;
|
<?php namespace FireflyIII\Http\Controllers;
|
||||||
|
|
||||||
use Auth;
|
use Auth;
|
||||||
use Carbon\Carbon;
|
|
||||||
use FireflyIII\Helpers\Reminders\ReminderHelperInterface;
|
use FireflyIII\Helpers\Reminders\ReminderHelperInterface;
|
||||||
use FireflyIII\Models\Reminder;
|
use FireflyIII\Models\Reminder;
|
||||||
|
use FireflyIII\Repositories\Reminder\ReminderRepositoryInterface;
|
||||||
use Redirect;
|
use Redirect;
|
||||||
use Session;
|
use Session;
|
||||||
use URL;
|
use URL;
|
||||||
@@ -50,53 +50,14 @@ class ReminderController extends Controller
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function index(ReminderHelperInterface $helper)
|
public function index(ReminderRepositoryInterface $repository)
|
||||||
{
|
{
|
||||||
|
|
||||||
$reminders = Auth::user()->reminders()->get();
|
|
||||||
|
|
||||||
$reminders->each(
|
$active = $repository->getActiveReminders();
|
||||||
function (Reminder $reminder) use ($helper) {
|
$expired = $repository->getExpiredReminders();
|
||||||
$reminder->description = $helper->getReminderText($reminder);
|
$inactive = $repository->getInactiveReminders();
|
||||||
}
|
$dismissed = $repository->getDismissedReminders();
|
||||||
);
|
|
||||||
|
|
||||||
$today = new Carbon;
|
|
||||||
// active reminders:
|
|
||||||
$active = $reminders->filter(
|
|
||||||
function (Reminder $reminder) use ($today) {
|
|
||||||
if ($reminder->notnow === false && $reminder->active === true && $reminder->startdate <= $today && $reminder->enddate >= $today) {
|
|
||||||
return $reminder;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// expired reminders:
|
|
||||||
$expired = $reminders->filter(
|
|
||||||
function (Reminder $reminder) use ($today) {
|
|
||||||
if ($reminder->notnow === false && $reminder->active === true && $reminder->startdate > $today || $reminder->enddate < $today) {
|
|
||||||
return $reminder;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// inactive reminders
|
|
||||||
$inactive = $reminders->filter(
|
|
||||||
function (Reminder $reminder) {
|
|
||||||
if ($reminder->active === false) {
|
|
||||||
return $reminder;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// dismissed reminders
|
|
||||||
$dismissed = $reminders->filter(
|
|
||||||
function (Reminder $reminder) {
|
|
||||||
if ($reminder->notnow === true) {
|
|
||||||
return $reminder;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$title = 'Reminders';
|
$title = 'Reminders';
|
||||||
$mainTitleIcon = 'fa-clock-o';
|
$mainTitleIcon = 'fa-clock-o';
|
||||||
|
@@ -130,9 +130,25 @@ Route::bind(
|
|||||||
Route::bind(
|
Route::bind(
|
||||||
'category', function ($value, $route) {
|
'category', function ($value, $route) {
|
||||||
if (Auth::check()) {
|
if (Auth::check()) {
|
||||||
return Category::where('id', $value)->where('user_id', Auth::user()->id)->first();
|
$object = Category::where('id', $value)->where('user_id', Auth::user()->id)->first();
|
||||||
if ($object) {
|
if ($object) {
|
||||||
$object = $object;
|
return $object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new NotFoundHttpException;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Route::bind(
|
||||||
|
'reminder', function ($value, $route) {
|
||||||
|
if (Auth::check()) {
|
||||||
|
/** @var \FireflyIII\Models\Reminder $object */
|
||||||
|
$object = Reminder::find($value);
|
||||||
|
if($object) {
|
||||||
|
if($object->remindersable->account->user_id == Auth::user()->id) {
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,9 +159,9 @@ Route::bind(
|
|||||||
Route::bind(
|
Route::bind(
|
||||||
'tag', function ($value, $route) {
|
'tag', function ($value, $route) {
|
||||||
if (Auth::check()) {
|
if (Auth::check()) {
|
||||||
return Tag::where('id', $value)->where('user_id', Auth::user()->id)->first();
|
$object = Tag::where('id', $value)->where('user_id', Auth::user()->id)->first();
|
||||||
if ($object) {
|
if ($object) {
|
||||||
$object = $object;
|
return $object;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -84,6 +84,7 @@ class FireflyServiceProvider extends ServiceProvider
|
|||||||
$this->app->bind('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface', 'FireflyIII\Repositories\PiggyBank\PiggyBankRepository');
|
$this->app->bind('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface', 'FireflyIII\Repositories\PiggyBank\PiggyBankRepository');
|
||||||
$this->app->bind('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface', 'FireflyIII\Repositories\Currency\CurrencyRepository');
|
$this->app->bind('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface', 'FireflyIII\Repositories\Currency\CurrencyRepository');
|
||||||
$this->app->bind('FireflyIII\Repositories\Tag\TagRepositoryInterface', 'FireflyIII\Repositories\Tag\TagRepository');
|
$this->app->bind('FireflyIII\Repositories\Tag\TagRepositoryInterface', 'FireflyIII\Repositories\Tag\TagRepository');
|
||||||
|
$this->app->bind('FireflyIII\Repositories\Reminder\ReminderRepositoryInterface', 'FireflyIII\Repositories\Reminder\ReminderRepository');
|
||||||
$this->app->bind('FireflyIII\Support\Search\SearchInterface', 'FireflyIII\Support\Search\Search');
|
$this->app->bind('FireflyIII\Support\Search\SearchInterface', 'FireflyIII\Support\Search\Search');
|
||||||
|
|
||||||
|
|
||||||
|
116
app/Repositories/Reminder/ReminderRepository.php
Normal file
116
app/Repositories/Reminder/ReminderRepository.php
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FireflyIII\Repositories\Reminder;
|
||||||
|
|
||||||
|
use App;
|
||||||
|
use Auth;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use FireflyIII\Models\Reminder;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ReminderRepository
|
||||||
|
*
|
||||||
|
* @package FireflyIII\Repositories\Reminder
|
||||||
|
*/
|
||||||
|
class ReminderRepository implements ReminderRepositoryInterface
|
||||||
|
{
|
||||||
|
/** @var \FireflyIII\Helpers\Reminders\ReminderHelperInterface */
|
||||||
|
protected $helper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
/** @var \FireflyIII\Helpers\Reminders\ReminderHelperInterface helper */
|
||||||
|
$this->helper = App::make('FireflyIII\Helpers\Reminders\ReminderHelperInterface');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function getActiveReminders()
|
||||||
|
{
|
||||||
|
$today = new Carbon;
|
||||||
|
// active reminders:
|
||||||
|
$active = Auth::user()->reminders()
|
||||||
|
->where('notnow', 0)
|
||||||
|
->where('active', 1)
|
||||||
|
->where('startdate', '<=', $today->format('Y-m-d 00:00:00'))
|
||||||
|
->where('enddate', '>=', $today->format('Y-m-d 00:00:00'))
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$active->each(
|
||||||
|
function (Reminder $reminder) {
|
||||||
|
$reminder->description = $this->helper->getReminderText($reminder);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return $active;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function getDismissedReminders()
|
||||||
|
{
|
||||||
|
$dismissed = Auth::user()->reminders()
|
||||||
|
->where('notnow', 1)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$dismissed->each(
|
||||||
|
function (Reminder $reminder) {
|
||||||
|
$reminder->description = $this->helper->getReminderText($reminder);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return $dismissed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function getExpiredReminders()
|
||||||
|
{
|
||||||
|
|
||||||
|
$expired = Auth::user()->reminders()
|
||||||
|
->where('notnow', 0)
|
||||||
|
->where('active', 1)
|
||||||
|
->where(
|
||||||
|
function (Builder $q) {
|
||||||
|
$today = new Carbon;
|
||||||
|
$q->where('startdate', '>', $today->format('Y-m-d 00:00:00'));
|
||||||
|
$q->orWhere('enddate', '<', $today->format('Y-m-d 00:00:00'));
|
||||||
|
}
|
||||||
|
)->get();
|
||||||
|
|
||||||
|
$expired->each(
|
||||||
|
function (Reminder $reminder) {
|
||||||
|
$reminder->description = $this->helper->getReminderText($reminder);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return $expired;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function getInactiveReminders()
|
||||||
|
{
|
||||||
|
$inactive = Auth::user()->reminders()
|
||||||
|
->where('active', 0)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$inactive->each(
|
||||||
|
function (Reminder $reminder) {
|
||||||
|
$reminder->description = $this->helper->getReminderText($reminder);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return $inactive;
|
||||||
|
}
|
||||||
|
}
|
34
app/Repositories/Reminder/ReminderRepositoryInterface.php
Normal file
34
app/Repositories/Reminder/ReminderRepositoryInterface.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FireflyIII\Repositories\Reminder;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface ReminderRepositoryInterface
|
||||||
|
*
|
||||||
|
* @package FireflyIII\Repositories\Reminder
|
||||||
|
*/
|
||||||
|
interface ReminderRepositoryInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function getActiveReminders();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function getDismissedReminders();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function getExpiredReminders();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function getInactiveReminders();
|
||||||
|
|
||||||
|
}
|
100
tests/controllers/ReminderControllerTest.php
Normal file
100
tests/controllers/ReminderControllerTest.php
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use League\FactoryMuffin\Facade as FactoryMuffin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ProfileControllerTest
|
||||||
|
*/
|
||||||
|
class ReminderControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Sets up the fixture, for example, opens a network connection.
|
||||||
|
* This method is called before a test is executed.
|
||||||
|
*/
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called before the first test of this test class is run.
|
||||||
|
*
|
||||||
|
* @since Method available since Release 3.4.0
|
||||||
|
*/
|
||||||
|
public static function setUpBeforeClass()
|
||||||
|
{
|
||||||
|
parent::setUpBeforeClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tears down the fixture, for example, closes a network connection.
|
||||||
|
* This method is called after a test is executed.
|
||||||
|
*/
|
||||||
|
public function tearDown()
|
||||||
|
{
|
||||||
|
parent::tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAct()
|
||||||
|
{
|
||||||
|
$reminder = FactoryMuffin::create('FireflyIII\Models\Reminder');
|
||||||
|
$this->be($reminder->remindersable->account->user);
|
||||||
|
|
||||||
|
$this->call('GET', '/reminder/act/' . $reminder->id);
|
||||||
|
$this->assertResponseStatus(302);
|
||||||
|
$this->assertRedirectedToRoute('transactions.create', ['transfer']);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDismiss()
|
||||||
|
{
|
||||||
|
$reminder = FactoryMuffin::create('FireflyIII\Models\Reminder');
|
||||||
|
$this->be($reminder->remindersable->account->user);
|
||||||
|
|
||||||
|
$this->call('GET', '/reminder/dismiss/' . $reminder->id);
|
||||||
|
$this->assertResponseStatus(302);
|
||||||
|
$this->assertRedirectedTo('/');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIndex()
|
||||||
|
{
|
||||||
|
$user = FactoryMuffin::create('FireflyIII\User');
|
||||||
|
$this->be($user);
|
||||||
|
$reminder = FactoryMuffin::create('FireflyIII\Models\Reminder');
|
||||||
|
$collection = new Collection([$reminder]);
|
||||||
|
|
||||||
|
$repository = $this->mock('FireflyIII\Repositories\Reminder\ReminderRepositoryInterface\ReminderRepositoryInterface');
|
||||||
|
|
||||||
|
$repository->shouldReceive('getActiveReminders')->andReturn($collection);
|
||||||
|
$repository->shouldReceive('getExpiredReminders')->andReturn($collection);
|
||||||
|
$repository->shouldReceive('getInactiveReminders')->andReturn($collection);
|
||||||
|
$repository->shouldReceive('getDismissedReminders')->andReturn($collection);
|
||||||
|
|
||||||
|
$this->call('GET', '/reminders');
|
||||||
|
$this->assertResponseOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testShow()
|
||||||
|
{
|
||||||
|
$reminder = FactoryMuffin::create('FireflyIII\Models\Reminder');
|
||||||
|
$reminder->notnow = false;
|
||||||
|
$reminder->save();
|
||||||
|
$this->be($reminder->remindersable->account->user);
|
||||||
|
|
||||||
|
$this->call('GET', '/reminder/' . $reminder->id);
|
||||||
|
$this->assertResponseOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testShowDismissed()
|
||||||
|
{
|
||||||
|
$reminder = FactoryMuffin::create('FireflyIII\Models\Reminder');
|
||||||
|
$reminder->notnow = true;
|
||||||
|
$reminder->save();
|
||||||
|
$this->be($reminder->remindersable->account->user);
|
||||||
|
|
||||||
|
$this->call('GET', '/reminder/' . $reminder->id);
|
||||||
|
$this->assertResponseOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -91,6 +91,29 @@ FactoryMuffin::define(
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
FactoryMuffin::define(
|
||||||
|
'FireflyIII\Models\Reminder',
|
||||||
|
[
|
||||||
|
'user_id' => 'factory|FireflyIII\User',
|
||||||
|
'startdate' => 'date',
|
||||||
|
'enddate' => 'date',
|
||||||
|
'active' => 'boolean',
|
||||||
|
'notnow' => 'boolean',
|
||||||
|
'remindersable_id' => 'factory|FireflyIII\Models\Piggybank',
|
||||||
|
'remindersable_type' => 'FireflyIII\Models\Piggybank',
|
||||||
|
'metadata' => function () {
|
||||||
|
return [
|
||||||
|
'perReminder' => 100,
|
||||||
|
'rangesCount' => 0,
|
||||||
|
'ranges' => [],
|
||||||
|
'leftToSave' => 100,
|
||||||
|
];
|
||||||
|
},
|
||||||
|
'encrypted' => 1,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
FactoryMuffin::define(
|
FactoryMuffin::define(
|
||||||
'FireflyIII\Models\Category',
|
'FireflyIII\Models\Category',
|
||||||
[
|
[
|
||||||
|
Reference in New Issue
Block a user