You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/UserProfileTest.php

142 lines
6.5 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\User;
use App\Services\UserProfileService;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$expectInvalid = static function (array $profile, string $message) use ($assert): void {
try {
UserProfileService::validate($profile);
$assert(false, $message);
} catch (InvalidArgumentException $exception) {
// Expected.
}
};
$valid = [
'last_name' => '陈',
'first_name' => '小 明',
'phone' => '+86 138-0013-8000',
'country' => "\u{00A0}中国\u{00A0}",
'province' => '广东省',
'city' => '深圳市',
'address' => '南山区 科技园 1 号',
'postal_code' => '518000',
];
$normalized = UserProfileService::validate($valid);
$assert($normalized['country'] === '中国'
&& $normalized['address'] === '南山区 科技园 1 号',
'Profile whitespace was not normalized consistently.');
foreach ([
['field' => 'last_name', 'value' => '', 'message' => 'An empty last name was accepted.'],
['field' => 'first_name', 'value' => 'Test123', 'message' => 'A numeric given name was accepted.'],
['field' => 'phone', 'value' => '+12', 'message' => 'A short phone number was accepted.'],
['field' => 'phone', 'value' => '138/0013/8000', 'message' => 'An invalid phone number was accepted.'],
['field' => 'address', 'value' => '<script>alert(1)</script>', 'message' => 'Markup was accepted in an address.'],
['field' => 'postal_code', 'value' => 'A@123', 'message' => 'An invalid postal code was accepted.'],
] as $case) {
$invalid = $valid;
$invalid[$case['field']] = $case['value'];
$expectInvalid($invalid, $case['message']);
}
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->getConnection('default')->getSchemaBuilder();
$schema->create('user', static function (Blueprint $table): void {
$table->increments('id');
$table->string('user_name', 128);
$table->string('email', 254);
foreach (UserProfileService::FIELDS as $field) {
$table->string($field, $field === 'address' ? 255 : 100)->default('');
}
});
Capsule::table('user')->insert([
'id' => 1,
'user_name' => 'member',
'email' => 'member@example.com',
]);
$profileWithExtraField = $valid;
$profileWithExtraField['email'] = 'attacker@example.com';
$saved = UserProfileService::update(User::findOrFail(1), $profileWithExtraField);
$reloaded = User::findOrFail(1);
$assert($saved === $normalized
&& $reloaded->email === 'member@example.com'
&& $reloaded->last_name === '陈'
&& $reloaded->address === '南山区 科技园 1 号',
'Validated profile data was not saved to the intended user columns only.');
$root = dirname(__DIR__);
$sources = [
'routes' => file_get_contents($root . '/config/routes.php'),
'auth' => file_get_contents($root . '/app/Controllers/AuthController.php'),
'account' => file_get_contents($root . '/app/Controllers/AccountSecurityController.php'),
'register' => file_get_contents($root . '/resources/views/venix/auth/register.tpl'),
'drawer' => file_get_contents($root . '/resources/views/venix/user/edit.tpl'),
'runtime' => file_get_contents($root . '/public/theme/venix/js/user-home.js'),
'schema' => file_get_contents($root . '/sql/ss.sql'),
'migration' => file_get_contents($root . '/sql/user_profile.sql'),
];
foreach ($sources as $name => $source) {
$assert(is_string($source), 'Unable to read profile source: ' . $name);
}
$assert(str_contains($sources['routes'], "'/profile'")
&& str_contains($sources['routes'], 'AccountSecurityController:updateProfile')
&& str_contains($sources['account'], 'AdminCsrf::verify')
&& str_contains($sources['account'], 'UserProfileService::update'),
'The authenticated CSRF-protected profile API is not fully wired.');
$assert(!str_contains($sources['auth'], 'UserProfileService')
&& !str_contains($sources['register'], 'registrationProfileTitle')
&& str_contains($sources['register'], "email: fieldValue('email')")
&& str_contains($sources['register'], "name: fieldValue('name')"),
'Registration must keep its original fields and must not collect account profile data.');
$assert(str_contains($sources['drawer'], 'id="vuProfileForm"')
&& str_contains($sources['runtime'], 'post("/user/profile", profile)')
&& preg_match('/<details\b[^>]*\bopen\b/i', $sources['drawer']) !== 1,
'The account information form is not wired correctly or a drawer card is open by default.');
$assert(preg_match('/<form\b[^>]*id="vuProfileForm"[^>]*>(.*?)<\/form>/s', $sources['drawer'], $profileForm) === 1
&& preg_match_all('/<div class="vu-compact-grid">(.*?)<\/div>/s', $profileForm[1], $profileRows) === 4,
'The account information form must render as four two-column rows.');
foreach ($profileRows[1] as $profileRow) {
$assert(substr_count($profileRow, '<input ') === 2,
'Every account information row must contain exactly two inputs.');
}
$assert(preg_match('/CREATE TABLE `user` \((.*?)\) ENGINE=/s', $sources['schema'], $userTable) === 1,
'The fresh user table definition is missing.');
$columnDefinitions = [
'last_name' => "varchar(80) NOT NULL DEFAULT ''",
'first_name' => "varchar(80) NOT NULL DEFAULT ''",
'phone' => "varchar(32) NOT NULL DEFAULT ''",
'country' => "varchar(100) NOT NULL DEFAULT ''",
'province' => "varchar(100) NOT NULL DEFAULT ''",
'city' => "varchar(100) NOT NULL DEFAULT ''",
'address' => "varchar(255) NOT NULL DEFAULT ''",
'postal_code' => "varchar(32) NOT NULL DEFAULT ''",
];
foreach (UserProfileService::FIELDS as $field) {
$assert(str_contains($userTable[1], '`' . $field . '` ' . $columnDefinitions[$field])
&& str_contains($sources['migration'], '`' . $field . '`'),
'The user table schema is missing the profile column: ' . $field);
$assert(!str_contains($sources['register'], 'name="' . $field . '"')
&& !str_contains($sources['register'], $field . ": fieldValue('" . $field . "')")
&& str_contains($sources['drawer'], 'name="' . $field . '"'),
'Profile data must be editable in account settings without appearing during registration: ' . $field);
}
echo "User account profile and registration separation checks passed.\n";