-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathSaveUserAbbrLengthTest.php
More file actions
139 lines (119 loc) · 4.54 KB
/
Copy pathSaveUserAbbrLengthTest.php
File metadata and controls
139 lines (119 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/*
* Copyright (c) 2025-2026 Netresearch DTT GmbH
* SPDX-License-Identifier: AGPL-3.0-only
*/
declare(strict_types=1);
namespace Tests\Controller;
use Tests\AbstractWebTestCase;
/**
* Regression tests for https://github.com/netresearch/timetracker/issues/35.
*
* The user abbreviation column is char(3), so 1 to 3 characters must be
* accepted; only empty and longer-than-3 values are rejected.
*
* @internal
*
* @coversNothing
*/
final class SaveUserAbbrLengthTest extends AbstractWebTestCase
{
public function testSaveUserAcceptsOneLetterAbbr(): void
{
$this->assertUserSaved('abbruser1', 'Q');
}
public function testSaveUserAcceptsTwoLetterAbbr(): void
{
$this->assertUserSaved('abbruser2', 'QW');
}
public function testSaveUserAcceptsThreeLetterAbbr(): void
{
$this->assertUserSaved('abbruser3', 'QWZ');
}
public function testSaveUserRejectsTooLongAbbr(): void
{
$this->assertUserRejected('abbruser4', 'QWXZ');
}
public function testSaveUserRejectsEmptyAbbr(): void
{
$this->assertUserRejected('abbruser5', '');
}
public function testGrandfathersAnUnchangedDuplicateAbbr(): void
{
// A legacy duplicate: give user 2 the same abbreviation as user 1. Re-
// saving user 1 with its UNCHANGED abbr (e.g. just deactivating) must
// succeed despite the collision — only a new/changed abbr is checked.
$connection = $this->connection;
self::assertNotNull($connection);
$row = $connection->fetchAssociative('SELECT username, abbr, type FROM users WHERE id = 1');
self::assertIsArray($row);
$connection->executeStatement('UPDATE users SET abbr = ? WHERE id = 2', [$row['abbr']]);
$this->logInSession('unittest');
$this->client->request(\Symfony\Component\HttpFoundation\Request::METHOD_POST, '/user/save', [
'id' => 1,
'username' => $row['username'],
'abbr' => $row['abbr'],
'teams' => ['1'],
'locale' => 'de',
'type' => $row['type'],
'active' => '0',
], [], ['HTTP_ACCEPT' => 'application/json']);
$this->assertStatusCode(200);
}
public function testStillRejectsChangingToACollidingAbbr(): void
{
// Changing user 1's abbreviation to one another user already holds must
// still be rejected (the grandfathering only covers an unchanged abbr).
$connection = $this->connection;
self::assertNotNull($connection);
$connection->executeStatement("UPDATE users SET abbr = 'AA' WHERE id = 1");
$connection->executeStatement("UPDATE users SET abbr = 'ZZ' WHERE id = 3");
$row = $connection->fetchAssociative('SELECT username, type FROM users WHERE id = 1');
self::assertIsArray($row);
$this->logInSession('unittest');
$this->client->request(\Symfony\Component\HttpFoundation\Request::METHOD_POST, '/user/save', [
'id' => 1,
'username' => $row['username'],
'abbr' => 'ZZ',
'teams' => ['1'],
'locale' => 'de',
'type' => $row['type'],
], [], ['HTTP_ACCEPT' => 'application/json']);
$this->assertStatusCode(422);
}
/**
* @return array<mixed>
*/
private function saveUser(string $username, string $abbr): array
{
$this->logInSession('unittest');
$this->client->request(\Symfony\Component\HttpFoundation\Request::METHOD_POST, '/user/save', [
'username' => $username,
'abbr' => $abbr,
'teams' => ['1'],
'locale' => 'de',
'type' => 'DEV',
], [], ['HTTP_ACCEPT' => 'application/json']);
$content = $this->client->getResponse()->getContent();
self::assertIsString($content);
$data = json_decode($content, true);
self::assertIsArray($data);
return $data;
}
private function assertUserSaved(string $username, string $abbr): void
{
$data = $this->saveUser($username, $abbr);
$this->assertStatusCode(200);
// Response payload is [id, username, abbr, type]
self::assertSame($username, $data[1]);
self::assertSame($abbr, $data[2]);
}
private function assertUserRejected(string $username, string $abbr): void
{
$data = $this->saveUser($username, $abbr);
$this->assertStatusCode(422);
self::assertArrayHasKey('message', $data);
self::assertIsString($data['message']);
self::assertNotSame('', $data['message']);
}
}