Combine phpstan, cs-check, cs-fix into lint-php recipe; fix lint issues + test failures + duplicate detection bug

This commit is contained in:
Pontoporeia
2026-05-19 23:59:41 +02:00
parent 2e75a3b35c
commit 728f05502c
18 changed files with 220 additions and 229 deletions

View File

@@ -70,8 +70,8 @@ try {
$month = (int) substr($slug, 4, 2);
$day = (int) substr($slug, 6, 2);
slAssert($year >= 2020 && $year <= 2100, 'year in plausible range');
slAssert($month >= 1 && $month <= 12, 'month in range');
slAssert($day >= 1 && $day <= 31, 'day in range');
slAssert($month >= 1 && $month <= 12, 'month in range');
slAssert($day >= 1 && $day <= 31, 'day in range');
echo "\n";
// =========================================================================
@@ -90,28 +90,29 @@ try {
// =========================================================================
echo "Test 3: validateLink — not_found on missing slug\n";
$result = $model->validateLink('NONEXISTENT-SLUG');
slAssertEq(false, $result['valid'], 'valid=false');
slAssertEq('not_found', $result['reason'], 'reason=not_found');
slAssertEq(false, $result['valid'], 'valid=false');
slAssertEq('not_found', $result['reason'], 'reason=not_found');
$result = $model->validateLink(null);
slAssertEq(false, $result['valid'], 'null slug: valid=false');
slAssertEq(false, $result['valid'], 'null slug: valid=false');
slAssertEq('not_found', $result['reason'], 'null slug: reason=not_found');
$result = $model->validateLink('');
slAssertEq(false, $result['valid'], 'empty slug: valid=false');
slAssertEq(false, $result['valid'], 'empty slug: valid=false');
slAssertEq('not_found', $result['reason'], 'empty slug: reason=not_found');
echo "\n";
// =========================================================================
// TEST 4: validateLink — valid active link with no password
// =========================================================================
echo "Test 4: validateLink — valid active link\n";
echo "Test 4: validateLink — link with auto-generated password needs password\n";
$link = $model->create($adminId, null, null);
$createdIds[] = $link['id'];
$result = $model->validateLink($link['slug']);
slAssertEq(true, $result['valid'], 'valid=true');
slAssert(isset($result['link']), 'link row returned');
slAssertEq(false, $result['valid'], 'valid=false (has auto-generated password)');
slAssertEq('needs_password', $result['reason'], 'reason=needs_password');
slAssert(isset($result['link']), 'link row returned');
echo "\n";
// =========================================================================
@@ -120,7 +121,7 @@ try {
echo "Test 5: validateLink — disabled link\n";
$model->toggleActive($link['id']); // deactivate
$result = $model->validateLink($link['slug']);
slAssertEq(false, $result['valid'], 'valid=false after disable');
slAssertEq(false, $result['valid'], 'valid=false after disable');
slAssertEq('disabled', $result['reason'], 'reason=disabled');
$model->toggleActive($link['id']); // restore
echo "\n";
@@ -133,62 +134,54 @@ try {
$createdIds[] = $archivedLink['id'];
$model->archive($archivedLink['id']);
$result = $model->validateLink($archivedLink['slug']);
slAssertEq(false, $result['valid'], 'valid=false for archived');
slAssertEq(false, $result['valid'], 'valid=false for archived');
slAssertEq('archived', $result['reason'], 'reason=archived');
echo "\n";
// =========================================================================
// TEST 7: validateLink — expired link
// TEST 7: validateLink — expired link (needs_password takes priority)
// =========================================================================
echo "Test 7: validateLink — expired link\n";
echo "Test 7: validateLink — expired link with password\n";
$pastDate = date('Y-m-d H:i:s', strtotime('-1 day'));
$expiredLink = $model->create($adminId, null, $pastDate);
$expiredLink = $model->create($adminId, $pastDate);
$createdIds[] = $expiredLink['id'];
$result = $model->validateLink($expiredLink['slug']);
slAssertEq(false, $result['valid'], 'valid=false for expired');
slAssertEq(false, $result['valid'], 'valid=false');
slAssertEq('expired', $result['reason'], 'reason=expired');
echo "\n";
// =========================================================================
// TEST 8: validateLink — not expired (future date)
// TEST 8: validateLink — needs_password (all links have passwords now)
// =========================================================================
echo "Test 8: validateLink — future expiry is still valid\n";
$futureDate = date('Y-m-d H:i:s', strtotime('+30 days'));
$futureLink = $model->create($adminId, null, $futureDate);
$createdIds[] = $futureLink['id'];
$result = $model->validateLink($futureLink['slug']);
slAssertEq(true, $result['valid'], 'valid=true for future expiry');
echo "\n";
// =========================================================================
// TEST 9: validateLink — needs_password when password is set
// =========================================================================
echo "Test 9: validateLink — needs_password\n";
$pwLink = $model->create($adminId, 'secret123', null);
echo "Test 8: validateLink — needs_password\n";
$pwLink = $model->create($adminId, null);
$createdIds[] = $pwLink['id'];
$result = $model->validateLink($pwLink['slug']);
slAssertEq(false, $result['valid'], 'valid=false (needs password)');
slAssertEq(false, $result['valid'], 'valid=false (needs password)');
slAssertEq('needs_password', $result['reason'], 'reason=needs_password');
slAssert(isset($result['link']), 'link row returned even when password needed');
echo "\n";
// =========================================================================
// TEST 10: verifyPassword — correct password
// TEST 9: verifyPassword — correct auto-generated password
// =========================================================================
echo "Test 10: verifyPassword — correct password\n";
echo "Test 9: verifyPassword — correct auto-generated password\n";
$pwLinkRow = $model->findBySlug($pwLink['slug']);
slAssertEq(true, $model->verifyPassword($pwLinkRow, 'secret123'), 'correct password accepted');
$plainPassword = $pwLink['_plain_password'] ?? '';
slAssert($plainPassword !== '', 'auto-generated password is non-empty');
slAssertEq(true, $model->verifyPassword($pwLinkRow, $plainPassword), 'correct password accepted');
slAssertEq(false, $model->verifyPassword($pwLinkRow, 'wrongpass'), 'wrong password rejected');
slAssertEq(false, $model->verifyPassword($pwLinkRow, ''), 'empty password rejected');
slAssertEq(false, $model->verifyPassword($pwLinkRow, ''), 'empty password rejected');
echo "\n";
// =========================================================================
// TEST 11: verifyPassword — link with no password always passes
// TEST 10: verifyPassword — any link requires correct password
// =========================================================================
echo "Test 11: verifyPassword — no password set always returns true\n";
$noPwRow = $model->findBySlug($link['slug']);
slAssertEq(true, $model->verifyPassword($noPwRow, ''), 'no-pw link: empty string passes');
slAssertEq(true, $model->verifyPassword($noPwRow, 'anything'), 'no-pw link: any string passes');
echo "Test 10: verifyPassword — wrong password rejected\n";
$anyLinkRow = $model->findBySlug($link['slug']);
slAssertEq(false, $model->verifyPassword($anyLinkRow, ''), 'empty string rejected');
slAssertEq(false, $model->verifyPassword($anyLinkRow, 'anything'), 'random string rejected');
slAssertEq(true, $model->verifyPassword($anyLinkRow, $link['_plain_password'] ?? ''), 'correct password accepted');
echo "\n";
// =========================================================================
@@ -227,7 +220,8 @@ try {
foreach ($createdIds as $id) {
try {
$pdo->prepare('DELETE FROM share_links WHERE id = ?')->execute([$id]);
} catch (Exception $e) { /* ignore */ }
} catch (Exception $e) { /* ignore */
}
}
}