Skip to content

Commit 7f28ac7

Browse files
deer-wmdedati18
andauthored
fix PolicyResource: null date should not default to valid date (#1215)
fix for the discovery in wmde/wbaas-deploy#2925 https://phabricator.wikimedia.org/T432714 --------- Co-authored-by: Dat WMDE <dat.nguyen@wikimedia.de>
1 parent fbc21c0 commit 7f28ac7

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

app/Http/Resources/PolicyResource.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public function toArray(Request $request): array {
1919
'metadata' => [
2020
'policy_id' => $this->id,
2121
'type' => $this->policy_type,
22-
'active_from' => Carbon::parse($this->active_from)->format('Y-m-d'),
22+
'active_from' => $this->active_from === null
23+
? null
24+
: Carbon::parse($this->active_from)->format('Y-m-d'),
2325
'content_vue_file' => $this->content_vue_file,
2426
],
2527
];
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Tests\Resources;
4+
5+
use App\Http\Resources\PolicyResource;
6+
use App\Policy;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Tests\TestCase;
9+
10+
class PolicyResourceTest extends TestCase {
11+
use RefreshDatabase;
12+
13+
public function testActiveFrom(): void {
14+
$policy = Policy::create([
15+
'policy_type' => 'terms-of-use',
16+
'content_vue_file' => 'terms-of-use/example.vue',
17+
'active_from' => '2022-02-02',
18+
]);
19+
20+
$resource = new PolicyResource($policy);
21+
$data = $resource->resolve();
22+
23+
$this->assertSame(
24+
'2022-02-02',
25+
data_get($data, 'metadata.active_from'),
26+
);
27+
}
28+
29+
public function testActiveFromNull(): void {
30+
$policy = Policy::create([
31+
'policy_type' => 'terms-of-use',
32+
'content_vue_file' => 'terms-of-use/example.vue',
33+
]);
34+
35+
$resource = new PolicyResource($policy);
36+
$data = $resource->resolve();
37+
38+
$this->assertSame(
39+
null,
40+
data_get($data, 'metadata.active_from'),
41+
);
42+
}
43+
44+
public function testActiveFromNullExplicit(): void {
45+
$policy = Policy::create([
46+
'policy_type' => 'terms-of-use',
47+
'content_vue_file' => 'terms-of-use/example.vue',
48+
'active_from' => null,
49+
]);
50+
51+
$resource = new PolicyResource($policy);
52+
$data = $resource->resolve();
53+
54+
$this->assertSame(
55+
null,
56+
data_get($data, 'metadata.active_from'),
57+
);
58+
}
59+
}

tests/Routes/Policies/PolicyControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Http\Controllers;
3+
namespace Tests\Routes;
44

55
use App\Policy;
66
use Carbon\CarbonImmutable;

0 commit comments

Comments
 (0)