Skip to content

Commit

Permalink
Ensure floor plan url uses https scheme [MA-154]
Browse files Browse the repository at this point in the history
  • Loading branch information
zachgarwood committed Jan 31, 2024
1 parent 49f8841 commit cfacf97
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/Models/Transformers/FloorTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ class FloorTransformer extends TransformerAbstract
{
public function transform(TwillModelContract $floor)
{
$floorPlan = $floor->fileObject('floor_plan');
return [
"map_floor$floor->level" => [
'label' => $floor->level,
'floor_plan' => $floor->file('floor_plan'),
'floor_plan' => $floorPlan ? secure_url('storage/uploads/' . $floorPlan?->uuid) : null,
'anchor_pixel_1' => Floor::ANCHOR_PIXELS[0],
'anchor_pixel_2' => Floor::ANCHOR_PIXELS[1],
'anchor_location_1' => FLOOR::ANCHOR_LOCATIONS[0],
Expand Down
16 changes: 16 additions & 0 deletions database/factories/FloorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Database\Factories;

use A17\Twill\Models\File;
use App\Models\Floor;
use Illuminate\Database\Eloquent\Factories\Factory;

Expand All @@ -18,4 +19,19 @@ public function definition(): array
},
];
}

public function withFloorPlan(): Factory
{
return $this->state(fn () => [])
->afterCreating(function (Floor $floor) {
$filename = 'test.pdf';
$file = File::create([
'filename' => $filename,
'uuid' => fake()->uuid() . '/' . $filename,
'size' => fake()->randomNumber(),
]);
$floor->files()->attach($file, ['locale' => 'en', 'role' => 'floor_plan']);
$floor->save();
});
}
}
12 changes: 12 additions & 0 deletions tests/Feature/FloorSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use App\Models\Floor;
use App\Repositories\Serializers\FloorSerializer;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class FloorSerializerTest extends TestCase
{
use RefreshDatabase;

public function test_serialize(): void
{
$floors = Floor::factory()->count(count(Floor::LEVELS))->make();
Expand All @@ -27,4 +30,13 @@ public function test_serialize(): void
$this->assertArrayHasKey('tile_images', $floor);
}
}

public function test_floor_plan_url(): void
{
$floor = Floor::factory()->withFloorPlan()->create();
$serializer = new FloorSerializer();
$serialized = $serializer->serialize([$floor]);
$floorPlanUrl = $serialized['map_floors']['map_floor' . $floor->level]['floor_plan'];
$this->assertStringStartsWith('https://', $floorPlanUrl, 'The floor plan url uses the https scheme');
}
}

0 comments on commit cfacf97

Please sign in to comment.