[]]);
$this->tenant = Tenant::create(['name' => 'CABM', 'entra_tenant_id' => '11111111-1111-1111-1111-111111111111']);
$this->agent = DirectoryUser::create([
'tenant_id' => $this->tenant->id, 'entra_oid' => '22222222-2222-2222-2222-222222222222',
'given_name' => 'Jean', 'mobile_phone' => '06 00 00 00 00', 'office_location' => 'Bâtiment A',
'mail' => 'j.martin@beziers-mediterranee.fr', 'account_enabled' => true,
]);
EditableField::create(['tenant_id' => $this->tenant->id, 'field' => 'mobile', 'label' => 'Mobile', 'enabled' => true, 'validation_rule' => 'phone']);
EditableField::create(['tenant_id' => $this->tenant->id, 'field' => 'bureau', 'label' => 'Bureau', 'enabled' => true, 'validation_rule' => 'text']);
EditableField::create(['tenant_id' => $this->tenant->id, 'field' => 'fonction', 'label' => 'Fonction', 'enabled' => false]);
Template::create(['tenant_id' => $this->tenant->id, 'name' => 'D', 'is_default' => true,
'draft_html_full' => '{{prenom}}{{#if mobile}} M:{{mobile}}{{/if}}{{#if bureau}} B:{{bureau}}{{/if}}'])->publish();
$this->app->instance(EntraTokenValidator::class, new class extends EntraTokenValidator {
public function validate(string $jwt): array
{
if ($jwt !== 'ok') {
throw new RuntimeException('bad');
}
return ['tid' => '11111111-1111-1111-1111-111111111111', 'oid' => '22222222-2222-2222-2222-222222222222'];
}
});
}
public function test_api_liste_seulement_les_champs_ouverts(): void
{
$this->withToken('ok')->getJson('/api/v1/profile')->assertOk()
->assertJsonCount(2, 'fields')
->assertJsonPath('fields.0.key', 'mobile')
->assertJsonPath('fields.0.directory_value', '06 00 00 00 00')
->assertJsonPath('fields.0.overridden', false)
->assertJsonPath('reply_variant_default', 'short');
}
public function test_api_modifie_masque_puis_retablit_et_trace(): void
{
$this->withToken('ok')->putJson('/api/v1/profile', ['fields' => ['mobile' => '07 11 22 33 44'], 'hidden' => ['bureau'], 'reply_variant' => 'none'])
->assertOk()->assertJsonPath('fields.0.override', '07 11 22 33 44')->assertJsonPath('fields.1.override', '')->assertJsonPath('reply_variant', 'none');
$this->withToken('ok')->getJson('/api/v1/signature')->assertJson(['html' => 'Jean M:07 11 22 33 44']);
$this->withToken('ok')->getJson('/api/v1/signature?context=reply')->assertJson(['html' => '', 'variant' => 'none']);
$this->assertTrue(AuditLog::where('action', 'profile.update')->where('actor', 'j.martin@beziers-mediterranee.fr')->exists());
$this->withToken('ok')->putJson('/api/v1/profile', ['fields' => ['mobile' => null, 'bureau' => null], 'reply_variant' => null])->assertOk();
$this->assertSame(0, UserOverride::count());
$this->withToken('ok')->getJson('/api/v1/signature')->assertJson(['html' => 'Jean M:06 00 00 00 00 B:Bâtiment A']);
}
public function test_api_refuse_champ_ferme_et_numero_invalide(): void
{
$this->withToken('ok')->putJson('/api/v1/profile', ['fields' => ['fonction' => 'Directeur']])
->assertStatus(422)->assertJsonValidationErrors('fields.fonction');
$this->withToken('ok')->putJson('/api/v1/profile', ['fields' => ['mobile' => '06']])
->assertStatus(422)->assertJsonValidationErrors('fields.mobile');
$this->withToken('ok')->putJson('/api/v1/profile', ['fields' => []], )->assertOk();
$this->assertSame(0, UserOverride::count());
}
public function test_portail_enregistre_les_trois_modes(): void
{
$user = User::create(['name' => 'Jean', 'email' => 'j.martin@beziers-mediterranee.fr', 'tenant_id' => $this->tenant->id, 'entra_oid' => '22222222-2222-2222-2222-222222222222']);
$this->actingAs($user)->get('/me')->assertOk()->assertSee('Mes informations')->assertSee('06 00 00 00 00');
$this->actingAs($user)->put('/me', [
'fields' => ['mobile' => ['mode' => 'custom', 'value' => '07 99 99 99 99'], 'bureau' => ['mode' => 'hidden']],
'reply_variant' => 'full',
])->assertRedirect()->assertSessionHasNoErrors();
$this->assertSame('07 99 99 99 99', UserOverride::where('field', 'mobile')->value('value'));
$this->assertSame('', UserOverride::where('field', 'bureau')->value('value'));
$this->assertSame('full', $this->agent->fresh()->preference->reply_variant);
}
public function test_admin_ouvre_et_ferme_les_champs(): void
{
$admin = User::create(['name' => 'A', 'email' => 'a@x.fr', 'tenant_id' => $this->tenant->id, 'entra_oid' => '33333333-3333-3333-3333-333333333333', 'role' => 'admin']);
$this->actingAs($admin)->get('/admin/champs')->assertOk()->assertSee('telephone')->assertDontSee('email', false);
$this->actingAs($admin)->put('/admin/champs', ['fields' => [
'mobile' => ['enabled' => '0', 'label' => 'Mobile', 'type' => 'phone'],
'fonction' => ['enabled' => '1', 'label' => 'Ma fonction', 'type' => 'text'],
]])->assertRedirect();
$this->assertFalse(EditableField::where('field', 'mobile')->value('enabled'));
$this->assertSame('Ma fonction', EditableField::where('field', 'fonction')->value('label'));
$this->withToken('ok')->getJson('/api/v1/profile')->assertJsonMissing(['key' => 'mobile'])->assertJsonFragment(['key' => 'fonction']);
}
}