Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/roles #30

Merged
merged 11 commits into from Nov 26, 2020
7 changes: 7 additions & 0 deletions config/calendar.php
@@ -0,0 +1,7 @@
<?php

return [
'birthdays' => [
'roles' => ['*'],
],
];
12 changes: 12 additions & 0 deletions src/AppServiceProvider.php
Expand Up @@ -18,13 +18,16 @@ public function boot()
->publishProvider()
->publishFactories()
->publishMail()
->publishConfig()
->commands(SendReminders::class);
}

private function load()
{
$this->loadRoutesFrom(__DIR__.'/../routes/api.php');

$this->mergeConfigFrom(__DIR__.'/../config/calendar.php', 'enso.calendar');

$this->loadMigrationsFrom(__DIR__.'/../database/migrations');

$this->loadViewsFrom(__DIR__.'/../resources/views', 'laravel-enso/calendar');
Expand Down Expand Up @@ -68,4 +71,13 @@ private function publishMail()

return $this;
}

private function publishConfig()
{
$this->publishes([
__DIR__.'/../config' => config_path('enso'),
], ['enso-config', 'calendar-config']);

return $this;
}
}
4 changes: 4 additions & 0 deletions src/Calendars/BirthdayCalendar.php
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;
use LaravelEnso\Calendar\Contracts\CustomCalendar;
use LaravelEnso\Calendar\Enums\Colors;
use LaravelEnso\People\Models\Person;
Expand Down Expand Up @@ -42,8 +43,11 @@ public function events(Carbon $startDate, Carbon $endDate): Collection
{
$this->startDate = $startDate;
$this->endDate = $endDate;
$roles = Config::get('enso.calendar.birthdays.roles');

return Person::query()
->unless($roles === ['*'], fn ($query) => $query
->whereHas('user', fn ($query) => $query->whereIn('role_id', $roles)))
->when(! $this->withinSameYear(), $this->differentYearQuery())
->when(
$this->withinSameYear() && $this->withinSameMonth(),
Expand Down
51 changes: 51 additions & 0 deletions tests/features/CalendarTest.php
@@ -0,0 +1,51 @@
<?php

use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use LaravelEnso\Calendar\Calendars\BirthdayCalendar;
use LaravelEnso\Calendar\Enums\Colors;
use LaravelEnso\Calendar\Models\Calendar;
use LaravelEnso\Core\Models\User;
use LaravelEnso\People\Models\Person;
use Tests\TestCase;

class CalendarTest extends TestCase
{
use RefreshDatabase;

protected Calendar $calendar;
protected Carbon $date;

protected function setUp(): void
{
parent::setUp();

$this->seed();

$this->actingAs(User::first());

$this->calendar = Calendar::factory()->create();
}

/** @test */
public function create_calendar()
{
$result = $this->post(route('core.calendar.store'), [
'name' => 'test',
'color' => Colors::Red,
'private' => false,
]);

$this->assertTrue(Calendar::whereName('test')->exists());
}

/** @test */
public function can_limit_birthday_calendar_roles()
{
Config::set('enso.calendar.birthdays.roles', []);

$this->assertEmpty((new BirthdayCalendar())
->events(Person::first()->birthday, Person::first()->birthday->addDay()));
}
}
2 changes: 0 additions & 2 deletions tests/features/CreateTest.php
@@ -1,7 +1,5 @@
<?php

namespace LaravelEnso\Calendar\tests\features;

use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use LaravelEnso\Calendar\Enums\Frequencies;
Expand Down
2 changes: 0 additions & 2 deletions tests/features/EventTest.php
@@ -1,7 +1,5 @@
<?php

namespace LaravelEnso\Calendar\tests\features;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Collection;
use LaravelEnso\Calendar\Enums\Frequencies;
Expand Down
2 changes: 0 additions & 2 deletions tests/features/SequenceTest.php
@@ -1,7 +1,5 @@
<?php

namespace LaravelEnso\Calendar\tests\features;

use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use LaravelEnso\Calendar\Enums\Frequencies;
Expand Down