Files
projectsend/tests/Feature/Clients/ClientPortalCustomFieldsTest.php
ignacionelson 6e47d76ba6 ProjectSend 2.0.0
Client file sharing, rebuilt from the ground up: a private area per
client, resumable uploads, folders, groups and categories, sharing with
expiry dates and download limits, comments, file versions, an activity
log, a REST API, and sixteen languages.

This repository begins here. ProjectSend 2 was developed privately, and
that development history is not published — the previous generation
remains available, with its own history, at projectsend/legacy.

Free software under the GNU General Public License v2, or (at your
option) any later version.
2026-08-14 01:38:12 -03:00

129 lines
5.3 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\User;
use App\Modules\Clients\Models\ClientCustomField;
use App\Modules\Clients\Models\ClientCustomFieldValue;
use App\Modules\Platform\Settings\Setting;
use App\Modules\Platform\Settings\Settings;
use Inertia\Testing\AssertableInertia;
beforeEach(function () {
// Setup complete.
User::factory()->create();
});
test('a field visible only on registration renders and saves there, and is absent from the account page', function () {
app(Settings::class)->set(Setting::ClientsCanRegister, true);
app(Settings::class)->set(Setting::ClientsAutoApprove, true);
$field = ClientCustomField::query()->create([
'name' => 'referral', 'label' => 'Referral source', 'type' => 'text',
'client_editability' => 'editable', 'client_contexts' => ['registration'],
]);
$this->get('/register')->assertInertia(
fn (AssertableInertia $page) => $page->has('custom_fields', 1)->where('custom_fields.0.id', $field->id),
);
$this->post('/register', [
'name' => 'New Client', 'email' => 'new@example.com', 'password' => 'super-secret-password',
'password_confirmation' => 'super-secret-password',
'custom_field_values' => [$field->id => 'Friend'],
])->assertRedirect(route('login'));
$client = User::query()->where('email', 'new@example.com')->sole();
expect(ClientCustomFieldValue::query()->where('client_custom_field_id', $field->id)->where('user_id', $client->id)->value('value'))
->toBe('Friend');
$this->actingAs($client)->get('/settings/profile')->assertInertia(
fn (AssertableInertia $page) => $page->has('custom_fields', 0),
);
});
test('a field visible on the account page appears for a client but not for staff', function () {
$admin = User::query()->sole();
$client = User::factory()->client()->create();
$field = ClientCustomField::query()->create([
'name' => 'newsletter', 'label' => 'Newsletter opt-in', 'type' => 'checkbox',
'client_editability' => 'editable', 'client_contexts' => ['account_edit'],
]);
$this->actingAs($client)->get('/settings/profile')->assertInertia(
fn (AssertableInertia $page) => $page->has('custom_fields', 1)->where('custom_fields.0.id', $field->id),
);
$this->actingAs($admin)->get('/settings/profile')->assertInertia(
fn (AssertableInertia $page) => $page->has('custom_fields', 0),
);
$this->actingAs($client)->patch('/settings/profile', [
'name' => $client->name, 'email' => $client->email,
'custom_field_values' => [$field->id => '1'],
])->assertSessionDoesntHaveErrors();
expect(ClientCustomFieldValue::query()->where('client_custom_field_id', $field->id)->where('user_id', $client->id)->value('value'))
->toBe('1');
});
test('an editable_once field locks after the client sets it once', function () {
$client = User::factory()->client()->create();
$field = ClientCustomField::query()->create([
'name' => 'terms', 'label' => 'Accept terms', 'type' => 'checkbox', 'required' => true,
'client_editability' => 'editable_once', 'client_contexts' => ['account_edit'],
]);
$this->actingAs($client)->get('/settings/profile')->assertInertia(
fn (AssertableInertia $page) => $page->where('custom_fields.0.locked', false),
);
$this->actingAs($client)->patch('/settings/profile', [
'name' => $client->name, 'email' => $client->email,
'custom_field_values' => [$field->id => '1'],
])->assertSessionDoesntHaveErrors();
expect(ClientCustomFieldValue::query()->where('client_custom_field_id', $field->id)->where('user_id', $client->id)->value('value'))
->toBe('1');
$this->actingAs($client)->get('/settings/profile')->assertInertia(
fn (AssertableInertia $page) => $page
->where('custom_fields.0.locked', true)
->where("custom_field_values.{$field->id}", '1'),
);
// A subsequent attempt to change a locked field is silently ignored.
$this->actingAs($client)->patch('/settings/profile', [
'name' => $client->name, 'email' => $client->email,
'custom_field_values' => [$field->id => '0'],
])->assertSessionDoesntHaveErrors();
expect(ClientCustomFieldValue::query()->where('client_custom_field_id', $field->id)->where('user_id', $client->id)->value('value'))
->toBe('1');
});
test('a required checkbox in a client context must actually be checked', function () {
$client = User::factory()->client()->create();
$field = ClientCustomField::query()->create([
'name' => 'terms', 'label' => 'Accept terms', 'type' => 'checkbox', 'required' => true,
'client_editability' => 'editable', 'client_contexts' => ['account_edit'],
]);
$this->actingAs($client)->patch('/settings/profile', [
'name' => $client->name, 'email' => $client->email,
'custom_field_values' => [$field->id => '0'],
])->assertSessionHasErrors("custom_field_values.{$field->id}");
$this->actingAs($client)->patch('/settings/profile', [
'name' => $client->name, 'email' => $client->email,
])->assertSessionHasErrors("custom_field_values.{$field->id}");
$this->actingAs($client)->patch('/settings/profile', [
'name' => $client->name, 'email' => $client->email,
'custom_field_values' => [$field->id => '1'],
])->assertSessionDoesntHaveErrors();
});