Skip to content

Commit

Permalink
Account for building closures when determining next open day [WEB-2682]
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhiltri committed Dec 20, 2023
1 parent 1e5d715 commit 37150e4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
11 changes: 11 additions & 0 deletions app/Models/BuildingClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class BuildingClosure extends AbstractModel
{
use Transformable;
use HasFactory;

protected $presenter = 'App\Presenters\BuildingClosurePresenter';
protected $presenterAdmin = 'App\Presenters\BuildingClosurePresenter';
Expand All @@ -32,6 +34,15 @@ class BuildingClosure extends AbstractModel

public $dates = ['date_start', 'date_end'];

/**
* The model's default values for attributes.
*
* @var array
*/
protected $attributes = [
'published' => true,
];

public function scopeToday($query, $type = 0)
{
$today = Carbon::today();
Expand Down
5 changes: 3 additions & 2 deletions app/Presenters/HoursPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,10 @@ private function getWhenFields($when)

private function isMuseumClosedToday($when)
{
$isClosed = $this->getWhenFields($when)['is_closed'];
$isHoursClosed = $this->getWhenFields($when)['is_closed'];
$isBuildingClosureClosed = $this->entity->buildingClosures()->where('date_start', '>=', $when)->where('date_end', '<=', $when)->get();

return $isClosed ?? false;
return $isHoursClosed || $isBuildingClosureClosed->isNotEmpty();
}

private function isBeforePublicOpen($when)
Expand Down
30 changes: 30 additions & 0 deletions database/factories/BuildingClosureFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Database\Factories;

use App\Models\BuildingClosure;
use Illuminate\Database\Eloquent\Factories\Factory;

class BuildingClosureFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = BuildingClosure::class;

/**
* Define the model's default state.
*/
public function definition(): array
{
return [
'published' => true,
'type' => 0,
'date_start' => today(),
'date_end' => today(),
'closure_copy' => 'The museum is closed',
];
}
}
18 changes: 17 additions & 1 deletion tests/Unit/HourTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Unit;

use App\Models\Hour;
use App\Models\BuildingClosure;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Tests\CreatesApplication;
Expand All @@ -18,7 +19,7 @@ protected function setUp(): void
{
parent::setUp();

$this->hour = Hour::factory()->make([
$this->hour = Hour::factory()->create([
'monday_is_closed' => false,
'monday_member_open' => 'PT10H00M',
'monday_member_close' => 'PT11H00M',
Expand Down Expand Up @@ -48,6 +49,13 @@ protected function setUp(): void
'sunday_public_close' => 'PT17H00M',
]);

$this->hour->buildingClosures()->save(BuildingClosure::factory()->create([
'date_start' => Carbon::now()->next('Thursday'),
'date_end' => Carbon::now()->next('Thursday'),
'closure_copy' => 'The museum is closed on Thursday',
'type' => 0,
]));

$this->hourAllClosed = Hour::factory()->make([
'monday_is_closed' => true,
'tuesday_is_closed' => true,
Expand Down Expand Up @@ -197,6 +205,14 @@ public function it_displays_a_closed_day_before_an_open_day()
$this->assertEquals(null, $this->getHoursHeader());
}

/** @test */
public function it_displays_when_thursday_has_an_emergency_closure(): void
{
$this->travelTo(Carbon::now('America/Chicago')->next('Thursday')->subDays(2));
$this->assertEquals('Closed today, next open Friday.', $this->getStatusHeader());
$this->assertEquals(null, $this->getHoursHeader());
}

/** @test */
public function it_displays_when_closed_all_days()
{
Expand Down

0 comments on commit 37150e4

Please sign in to comment.