From d2ebfb75c0ab186ca5d168b6a7fe3d234574af1c Mon Sep 17 00:00:00 2001 From: Perside Rosalie Date: Fri, 10 Jul 2026 18:05:28 +0200 Subject: [PATCH 1/5] Create endpoint to get polices that should be accepted Bug: T429591 --- app/Http/Controllers/PoliciesController.php | 24 +++++++++++++++++++ app/Http/Resources/PoliciesCollection.php | 19 +++++++++++++++ routes/api.php | 1 + .../Controllers/PoliciesControllerTest.php | 14 +++++++++++ 4 files changed, 58 insertions(+) create mode 100644 app/Http/Controllers/PoliciesController.php create mode 100644 app/Http/Resources/PoliciesCollection.php create mode 100644 tests/Http/Controllers/PoliciesControllerTest.php diff --git a/app/Http/Controllers/PoliciesController.php b/app/Http/Controllers/PoliciesController.php new file mode 100644 index 00000000..960a28ab --- /dev/null +++ b/app/Http/Controllers/PoliciesController.php @@ -0,0 +1,24 @@ +selectRaw('MAX(id) as id') + ->groupBy('policy_type') + ->pluck('id'); + + $currentPolicies = Policy::whereIn('id', $latestPolicyIds)->get(); + return new PoliciesCollection($currentPolicies); + } +} diff --git a/app/Http/Resources/PoliciesCollection.php b/app/Http/Resources/PoliciesCollection.php new file mode 100644 index 00000000..e183cef2 --- /dev/null +++ b/app/Http/Resources/PoliciesCollection.php @@ -0,0 +1,19 @@ + + */ + public function toArray(Request $request): array { + return [ + 'items'=> $this->collection + ]; + } +} diff --git a/routes/api.php b/routes/api.php index f014ffbc..e88422c6 100644 --- a/routes/api.php +++ b/routes/api.php @@ -21,6 +21,7 @@ $router->post('user/resetPassword', ['uses' => 'Auth\ResetPasswordController@reset']); $router->post('contact/sendMessage', ['uses' => 'ContactController@sendMessage']); $router->post('complaint/sendMessage', ['uses' => 'ComplaintController@sendMessage']); + $router->get('policies/current', ['uses' => 'PoliciesController@getCurrentPolicies']); $router->post('auth/login', ['uses' => 'Auth\LoginController@postLogin'])->name('login'); // Authed diff --git a/tests/Http/Controllers/PoliciesControllerTest.php b/tests/Http/Controllers/PoliciesControllerTest.php new file mode 100644 index 00000000..aa7d5aa6 --- /dev/null +++ b/tests/Http/Controllers/PoliciesControllerTest.php @@ -0,0 +1,14 @@ +create(); + } +} From 14506fd06d1787df3f9fc7459664d32d0a265134 Mon Sep 17 00:00:00 2001 From: Perside Rosalie Date: Mon, 13 Jul 2026 12:27:34 +0200 Subject: [PATCH 2/5] Added unit tests --- app/Policy.php | 4 ++- database/factories/PolicyFactory.php | 26 +++++++++++++++++++ .../Controllers/PoliciesControllerTest.php | 17 +++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 database/factories/PolicyFactory.php diff --git a/app/Policy.php b/app/Policy.php index 44933192..95b9f499 100644 --- a/app/Policy.php +++ b/app/Policy.php @@ -5,6 +5,7 @@ use Carbon\CarbonImmutable; use Eloquent; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; /** @@ -24,10 +25,11 @@ * @method static Builder|Policy whereId($value) * @method static Builder|Policy wherePolicyType($value) * @method static Builder|Policy whereUpdatedAt($value) - * + * @method static \Database\Factories\PolicyFactory factory(...$parameters) * @mixin Eloquent */ class Policy extends Model { + use HasFactory; // define which attributes are mass assignable protected $fillable = [ 'policy_type', diff --git a/database/factories/PolicyFactory.php b/database/factories/PolicyFactory.php new file mode 100644 index 00000000..de713bd3 --- /dev/null +++ b/database/factories/PolicyFactory.php @@ -0,0 +1,26 @@ + + */ +class PolicyFactory extends Factory { + + protected $model = Policy::class; + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array { + return [ + 'policy_type' => $this->faker->randomElement(['terms-of-use','hosting-policy']), + 'active_from' => now(), + 'content_vue_file' => fake()->slug() . '.vue', + ]; + } +} diff --git a/tests/Http/Controllers/PoliciesControllerTest.php b/tests/Http/Controllers/PoliciesControllerTest.php index aa7d5aa6..0c1148ae 100644 --- a/tests/Http/Controllers/PoliciesControllerTest.php +++ b/tests/Http/Controllers/PoliciesControllerTest.php @@ -2,6 +2,7 @@ namespace Tests\Http\Controllers; +use App\Policy; use Illuminate\Foundation\Testing\DatabaseTransactions; use Tests\TestCase; @@ -9,6 +10,20 @@ class PoliciesControllerTest extends TestCase { use DatabaseTransactions; public function testGetCurrentPolicies(): void { - //$policy = Policy::factory()->create(); + // Future policy + Policy::factory()->create([ + 'active_from' => now()->addDay(), + ]); + // Active policy + Policy::factory()->create(); + // Active policy + Policy::factory()->create([ + 'active_from' => now()->subMonth(), + ]); + + $response = $this->getJson('/policies/current'); + + $response->assertOk(); + $response->assertJsonCount(2, 'data.items'); } } From ee33c8c0775913763c5c16d10776cb796dcc2cad Mon Sep 17 00:00:00 2001 From: Perside Rosalie Date: Mon, 13 Jul 2026 12:41:14 +0200 Subject: [PATCH 3/5] Fix tests --- app/Http/Controllers/PoliciesController.php | 2 +- tests/Http/Controllers/PoliciesControllerTest.php | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/PoliciesController.php b/app/Http/Controllers/PoliciesController.php index 960a28ab..782520e8 100644 --- a/app/Http/Controllers/PoliciesController.php +++ b/app/Http/Controllers/PoliciesController.php @@ -13,7 +13,7 @@ public function getCurrentPolicies() { $now = CarbonImmutable::now(); // This works based on the assumption that the latest policy has the highest id given that id is AUTO_INCREMENT - $latestPolicyIds = Policy::where('active_from', '<=', $now) + $latestPolicyIds = Policy::where('active_from', '<', $now) ->selectRaw('MAX(id) as id') ->groupBy('policy_type') ->pluck('id'); diff --git a/tests/Http/Controllers/PoliciesControllerTest.php b/tests/Http/Controllers/PoliciesControllerTest.php index 0c1148ae..61f4a66c 100644 --- a/tests/Http/Controllers/PoliciesControllerTest.php +++ b/tests/Http/Controllers/PoliciesControllerTest.php @@ -15,8 +15,6 @@ public function testGetCurrentPolicies(): void { 'active_from' => now()->addDay(), ]); // Active policy - Policy::factory()->create(); - // Active policy Policy::factory()->create([ 'active_from' => now()->subMonth(), ]); @@ -24,6 +22,6 @@ public function testGetCurrentPolicies(): void { $response = $this->getJson('/policies/current'); $response->assertOk(); - $response->assertJsonCount(2, 'data.items'); + $response->assertJsonCount(1, 'data.items'); } } From a25966124102060ca5f2b0af6290a13ed3dac4d9 Mon Sep 17 00:00:00 2001 From: Perside Rosalie Date: Mon, 13 Jul 2026 12:54:12 +0200 Subject: [PATCH 4/5] Fix linting --- app/Http/Controllers/PoliciesController.php | 3 +-- app/Http/Resources/PoliciesCollection.php | 2 +- app/Policy.php | 2 ++ database/factories/PolicyFactory.php | 7 ++++--- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/PoliciesController.php b/app/Http/Controllers/PoliciesController.php index 782520e8..5c860445 100644 --- a/app/Http/Controllers/PoliciesController.php +++ b/app/Http/Controllers/PoliciesController.php @@ -5,10 +5,8 @@ use App\Http\Resources\PoliciesCollection; use App\Policy; use Carbon\CarbonImmutable; -use Illuminate\Http\Request; class PoliciesController extends Controller { - public function getCurrentPolicies() { $now = CarbonImmutable::now(); @@ -19,6 +17,7 @@ public function getCurrentPolicies() { ->pluck('id'); $currentPolicies = Policy::whereIn('id', $latestPolicyIds)->get(); + return new PoliciesCollection($currentPolicies); } } diff --git a/app/Http/Resources/PoliciesCollection.php b/app/Http/Resources/PoliciesCollection.php index e183cef2..d233312b 100644 --- a/app/Http/Resources/PoliciesCollection.php +++ b/app/Http/Resources/PoliciesCollection.php @@ -13,7 +13,7 @@ class PoliciesCollection extends ResourceCollection { */ public function toArray(Request $request): array { return [ - 'items'=> $this->collection + 'items' => $this->collection, ]; } } diff --git a/app/Policy.php b/app/Policy.php index 95b9f499..d9eb662a 100644 --- a/app/Policy.php +++ b/app/Policy.php @@ -26,10 +26,12 @@ * @method static Builder|Policy wherePolicyType($value) * @method static Builder|Policy whereUpdatedAt($value) * @method static \Database\Factories\PolicyFactory factory(...$parameters) + * * @mixin Eloquent */ class Policy extends Model { use HasFactory; + // define which attributes are mass assignable protected $fillable = [ 'policy_type', diff --git a/database/factories/PolicyFactory.php b/database/factories/PolicyFactory.php index de713bd3..b53aa93b 100644 --- a/database/factories/PolicyFactory.php +++ b/database/factories/PolicyFactory.php @@ -4,13 +4,14 @@ use App\Policy; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Database\Eloquent\Model; /** - * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Model> + * @extends Factory */ class PolicyFactory extends Factory { - protected $model = Policy::class; + /** * Define the model's default state. * @@ -18,7 +19,7 @@ class PolicyFactory extends Factory { */ public function definition(): array { return [ - 'policy_type' => $this->faker->randomElement(['terms-of-use','hosting-policy']), + 'policy_type' => $this->faker->randomElement(['terms-of-use', 'hosting-policy']), 'active_from' => now(), 'content_vue_file' => fake()->slug() . '.vue', ]; From ecc89530291bb7514e77b81ba130a938fbc54fbe Mon Sep 17 00:00:00 2001 From: Perside Rosalie Date: Tue, 14 Jul 2026 17:37:32 +0200 Subject: [PATCH 5/5] Improve tests --- app/Http/Controllers/PoliciesController.php | 2 +- .../Controllers/PoliciesControllerTest.php | 28 ++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/PoliciesController.php b/app/Http/Controllers/PoliciesController.php index 5c860445..1a3dfd65 100644 --- a/app/Http/Controllers/PoliciesController.php +++ b/app/Http/Controllers/PoliciesController.php @@ -7,7 +7,7 @@ use Carbon\CarbonImmutable; class PoliciesController extends Controller { - public function getCurrentPolicies() { + public function getCurrentPolicies(): PoliciesCollection { $now = CarbonImmutable::now(); // This works based on the assumption that the latest policy has the highest id given that id is AUTO_INCREMENT diff --git a/tests/Http/Controllers/PoliciesControllerTest.php b/tests/Http/Controllers/PoliciesControllerTest.php index 61f4a66c..fbfdbb8c 100644 --- a/tests/Http/Controllers/PoliciesControllerTest.php +++ b/tests/Http/Controllers/PoliciesControllerTest.php @@ -10,18 +10,38 @@ class PoliciesControllerTest extends TestCase { use DatabaseTransactions; public function testGetCurrentPolicies(): void { + $currentTime = now(); // Future policy Policy::factory()->create([ - 'active_from' => now()->addDay(), + 'policy_type' => 'terms-of-use', + 'active_from' => $currentTime->addDay(), ]); - // Active policy + // Old policy Policy::factory()->create([ - 'active_from' => now()->subMonth(), + 'policy_type' => 'hosting-policy', + 'active_from' => $currentTime->subMonth(), + ]); + // Active policies + $latestToUPolicy = Policy::factory()->create([ + 'policy_type' => 'terms-of-use', + 'active_from' => $currentTime->subMonth(), + ]); + $latestHostingPolicy = Policy::factory()->create([ + 'policy_type' => 'hosting-policy', + 'active_from' => $currentTime->subWeek(), ]); $response = $this->getJson('/policies/current'); $response->assertOk(); - $response->assertJsonCount(1, 'data.items'); + $response->assertJsonCount(2, 'data.items'); + $response->assertJsonFragment([ + 'id' => $latestToUPolicy->id, + 'active_from' => $latestToUPolicy->active_from, + ]); + $response->assertJsonFragment([ + 'id' => $latestHostingPolicy->id, + 'active_from' => $latestHostingPolicy->active_from, + ]); } }