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
3 changes: 2 additions & 1 deletion app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public function register(Request $request) {
DB::transaction(function () use (&$user, $request): void {
$user = (new UserCreateJob(
$request->input('email'),
$request->input('password')
$request->input('password'),
$request->input('accepted_policies')
))->handle();
(UserVerificationCreateTokenAndSendJob::newForAccountCreation($user))->handle();
});
Expand Down
35 changes: 34 additions & 1 deletion app/Jobs/UserCreateJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace App\Jobs;

use App\User;
use App\UserTermsOfUseAcceptance;
use App\Policy;
use App\PolicyAcceptance;
use App\TermsOfUseVersion

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missng a ;

Suggested change
use App\TermsOfUseVersion
use App\TermsOfUseVersion;

use Illuminate\Support\Facades\Hash;

class UserCreateJob extends Job {
Expand All @@ -12,11 +16,12 @@ class UserCreateJob extends Job {

private $verified;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->acceptedPolicyIds assigned in constructor with no property declaration

Suggested change
private $verified;
private $verified;
private $acceptedPolicyIds;


public function __construct($email, $password, $verified = false) {
public function __construct($email, $password, $acceptedPolicyIds, $verified = false) {
// TODO maybe pass in an unsaved eloquent model?
// // but that would make CLI job creation hard
$this->email = $email;
$this->password = $password;
$this->acceptedPolicyIds = $acceptedPolicyIds;
$this->verified = false;
}

Expand All @@ -30,6 +35,34 @@ public function handle() {
'verified' => $this->verified,
]);

// accept latest Terms of Use Policy automatically
$latestToU = TermsOfUseVersion::latestActiveVersion();

@dati18 dati18 Jul 10, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

references to classes that have been deleted since T430532
You can remove it entirely

if ($latestToU) {
UserTermsOfUseAcceptance::create([
'user_id' => $user->id,
'tou_version' => $latestToU->version,
'tou_accepted_at' => now(),
]);
} else {
Log::warning("No active Terms of Use version found when creating user {$user->email} (ID {$user->id}).");
}

if (is_array($this->acceptedPolicyIds)) {
foreach($this->acceptedPolicyIds as $acceptedPolicyId) {
$policy = Policy::find($acceptedPolicyId);

if ($policy) {
PolicyAcceptance::create([
'user_id' => $user->id,
'policy_id' => $policy->id,
'accepted_at' => now(),
]);
} else {
Log::warning("Policy ID '{$acceptedPolicyId}' not found when creating user {$user->email} (ID {$user->id}).");
}
}
}

return $user;

}
Expand Down
70 changes: 70 additions & 0 deletions tests/Jobs/UserAcceptedPoliciesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Tests\Jobs;

use App\Policy;
use App\Jobs\CreateFirstTermsOfUseVersionJob;
use App\Jobs\UserCreateJob;
use App\TermsOfUseVersion;
use App\PolicyAcceptance;
use App\UserTermsOfUseAcceptance;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use Carbon\CarbonImmutable;

class UserAcceptedPoliciesTest extends TestCase {
use RefreshDatabase;

public function testUserRegistrationAcceptsNone(): void {
$email = 'test+' . uniqid('', true) . '@example.com';
$user = (new UserCreateJob($email, 'thisisapassword123', null, true))->handle();

$this->assertEquals(
PolicyAcceptance::all()->count(),
0
);
}

public function testUserRegistrationAcceptsOne(): void {
$termsOfUse = new Policy;
$termsOfUse->policy_type = 'terms-of-use';
$termsOfUse->active_from = CarbonImmutable::now();
$termsOfUse->content_vue_file = 'termsOfUse.vue';
$termsOfUse->save();

$email = 'test+' . uniqid('', true) . '@example.com';
$user = (new UserCreateJob($email, 'thisisapassword123', [$termsOfUse->id], true))->handle();

$this->assertDatabaseHas('policy_acceptances', [
'user_id' => $user->id,
'policy_id' => $termsOfUse->id,
]);
}

public function testUserRegistrationAcceptsTwo(): void {
$termsOfUse = new Policy;
$termsOfUse->policy_type = 'terms-of-use';
$termsOfUse->active_from = CarbonImmutable::now();
$termsOfUse->content_vue_file = 'termsOfUse.vue';
$termsOfUse->save();

$hostingPolicy = new Policy;
$hostingPolicy->policy_type = 'hosting-policy';
$hostingPolicy->active_from = CarbonImmutable::now();
$hostingPolicy->content_vue_file = 'hostingPolicy.vue';
$hostingPolicy->save();

$email = 'test+' . uniqid('', true) . '@example.com';
$user = (new UserCreateJob($email, 'thisisapassword123', [$termsOfUse->id, $hostingPolicy->id], true))->handle();

$this->assertDatabaseHas('policy_acceptances', [
'user_id' => $user->id,
'policy_id' => $termsOfUse->id,
]);

$this->assertDatabaseHas('policy_acceptances', [
'user_id' => $user->id,
'policy_id' => $hostingPolicy->id,
]);
}
}
32 changes: 32 additions & 0 deletions tests/Jobs/UserTermsOfUseAcceptanceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Tests\Jobs;

use App\Jobs\CreateFirstTermsOfUseVersionJob;
use App\Jobs\UserCreateJob;
use App\TermsOfUseVersion;
use App\UserTermsOfUseAcceptance;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class UserTermsOfUseAcceptanceTest extends TestCase {
use RefreshDatabase;

public function testUserCreationCreatesTouAcceptance(): void {
(new CreateFirstTermsOfUseVersionJob())->handle();
$email = 'test+' . uniqid('', true) . '@example.com';
$user = (new UserCreateJob($email, 'thisisapassword123', null, true))->handle();

$this->assertDatabaseHas('tou_acceptances', [
'user_id' => $user->id,
'tou_version' => TermsOfUseVersion::latestActiveVersion()->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->assertNotNull($acceptance->tou_accepted_at);
}
}
Loading