Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Jobs/UserCreateJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function handle() {
'verified' => $this->verified,
]);

$latest = TermsOfUseVersion::latestActiveVersion();
$latest = TermsOfUseVersion::getActiveVersion();
if ($latest) {
UserTermsOfUseAcceptance::create([
'user_id' => $user->id,
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/UserTouAcceptanceJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function handle(): void {
try {
UserTermsOfUseAcceptance::create([
'user_id' => $user->id,
'tou_version' => TermsOfUseVersion::latestActiveVersion()->version,
'tou_version' => TermsOfUseVersion::getActiveVersion()->version,
'tou_accepted_at' => $user->created_at,
]);
} catch (Throwable $exception) {
Expand Down
23 changes: 21 additions & 2 deletions app/TermsOfUseVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,26 @@ class TermsOfUseVersion extends Model {
'active' => 'boolean',
];

public static function latestActiveVersion(): ?self {
return self::query()->where('active', true)->latest()->first();
/**
* Get the active ToU version.
*/
public static function getActiveVersion(): ?self {
return self::query()->where('active', true)->first();
}

/**
* Ensure only one ToU version remains active after any save operation.
*/
protected static function booted(): void {
static::saving(function (self $model): void {
if (!$model->active) {
return;
}

self::query()
->where('version', '!=', $model->version)
->where('active', true)
->update(['active' => false]);
});
}
}
4 changes: 2 additions & 2 deletions tests/Jobs/UserTermsOfUseAcceptanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public function testUserCreationCreatesTouAcceptance(): void {

$this->assertDatabaseHas('tou_acceptances', [
'user_id' => $user->id,
'tou_version' => TermsOfUseVersion::latestActiveVersion()->version,
'tou_version' => TermsOfUseVersion::getActiveVersion()->version,
]);

$rows = UserTermsOfUseAcceptance::where('user_id', $user->id)->get();
$this->assertCount(1, $rows);
$acceptance = $rows->first();

$this->assertSame(TermsOfUseVersion::latestActiveVersion()->version, $acceptance->tou_version);
$this->assertSame(TermsOfUseVersion::getActiveVersion()->version, $acceptance->tou_version);
$this->assertNotNull($acceptance->tou_accepted_at);
}
}
2 changes: 1 addition & 1 deletion tests/Jobs/UserTouAcceptanceJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testTouAcceptanceJob(): void {
(new CreateFirstTermsOfUseVersionJob)->handle();
(new UserTouAcceptanceJob)->handle();

$latest = TermsOfUseVersion::latestActiveVersion()->version;
$latest = TermsOfUseVersion::getActiveVersion()->version;

$this->assertDatabaseHas('tou_acceptances', ['user_id' => $u1->id, 'tou_version' => $latest]);
$this->assertDatabaseHas('tou_acceptances', ['user_id' => $u2->id, 'tou_version' => $latest]);
Expand Down
40 changes: 40 additions & 0 deletions tests/Models/TermsOfUseVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Tests\Models;

use App\TermsOfUseVersion;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class TermsOfUseVersionTest extends TestCase {
use RefreshDatabase;

public function testSavingActiveVersionDeactivatesOtherVersions(): void {
$first = TermsOfUseVersion::create([
'version' => '2024-01-01',
'active' => true,
]);

$second = TermsOfUseVersion::create([
'version' => '2025-01-01',
'active' => true,
]);

$this->assertFalse($first->fresh()->active);
$this->assertTrue($second->fresh()->active);
}

public function testSavingInactiveVersionDoesNotAffectExistingActiveVersion(): void {
$active = TermsOfUseVersion::create([
'version' => '2024-03-01',
'active' => true,
]);

TermsOfUseVersion::create([
'version' => '2024-04-01',
'active' => false,
]);

$this->assertTrue($active->fresh()->active);
}
}
Loading