Skip to content

Commit 9424d2e

Browse files
committed
Merge branch 'v2.x' into add-version-listnamesbyproject
2 parents 32fd099 + 324a946 commit 9424d2e

File tree

6 files changed

+420
-29
lines changed

6 files changed

+420
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- New method `Redmine\Api\Role::listNames()` for listing the ids and names of all roles.
1818
- New method `Redmine\Api\TimeEntryActivity::listNames()` for listing the ids and names of all time entry activities.
1919
- New method `Redmine\Api\Tracker::listNames()` for listing the ids and names of all trackers.
20+
- New method `Redmine\Api\User::listLogins()` for listing the ids and logins of all users.
2021
- New method `Redmine\Api\Version::listNamesByProject()` for listing the ids and names of all versions of a project.
2122

2223
### Deprecated
@@ -29,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2930
- `Redmine\Api\Role::listing()` is deprecated, use `\Redmine\Api\Role::listNames()` instead.
3031
- `Redmine\Api\TimeEntryActivity::listing()` is deprecated, use `\Redmine\Api\TimeEntryActivity::listNames()` instead.
3132
- `Redmine\Api\Tracker::listing()` is deprecated, use `\Redmine\Api\Tracker::listNames()` instead.
33+
- `Redmine\Api\User::listing()` is deprecated, use `\Redmine\Api\User::listLogins()` instead.
3234
- `Redmine\Api\Version::listing()` is deprecated, use `\Redmine\Api\Version::listNamesByProject()` instead.
3335

3436
## [v2.6.0](https://github.com/kbsali/php-redmine-api/compare/v2.5.0...v2.6.0) - 2024-03-25

src/Redmine/Api/User.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class User extends AbstractApi
2323
{
2424
private $users = [];
2525

26+
private $userLogins = null;
27+
2628
/**
2729
* List users.
2830
*
@@ -43,6 +45,43 @@ final public function list(array $params = []): array
4345
}
4446
}
4547

48+
/**
49+
* Returns an array of all users with id/login pairs.
50+
*
51+
* @return array<int,string> list of users (id => login)
52+
*/
53+
final public function listLogins(): array
54+
{
55+
if ($this->userLogins !== null) {
56+
return $this->userLogins;
57+
}
58+
59+
$this->userLogins = [];
60+
61+
$limit = 100;
62+
$offset = 0;
63+
64+
do {
65+
$list = $this->list([
66+
'limit' => $limit,
67+
'offset' => $offset,
68+
]);
69+
70+
$listCount = 0;
71+
$offset += $limit;
72+
73+
if (array_key_exists('users', $list)) {
74+
$listCount = count($list['users']);
75+
76+
foreach ($list['users'] as $user) {
77+
$this->userLogins[(int) $user['id']] = (string) $user['login'];
78+
}
79+
}
80+
} while ($listCount === $limit);
81+
82+
return $this->userLogins;
83+
}
84+
4685
/**
4786
* List users.
4887
*
@@ -79,13 +118,18 @@ public function all(array $params = [])
79118
/**
80119
* Returns an array of users with login/id pairs.
81120
*
121+
* @deprecated v2.7.0 Use listLogins() instead.
122+
* @see User::listLogins()
123+
*
82124
* @param bool $forceUpdate to force the update of the users var
83125
* @param array $params to allow offset/limit (and more) to be passed
84126
*
85127
* @return array list of users (id => username)
86128
*/
87129
public function listing($forceUpdate = false, array $params = [])
88130
{
131+
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listLogins()` instead.', E_USER_DEPRECATED);
132+
89133
if (empty($this->users) || $forceUpdate) {
90134
$this->users = $this->list($params);
91135
}

tests/Behat/Bootstrap/UserContextTrait.php

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@
99

1010
trait UserContextTrait
1111
{
12+
/**
13+
* @Given I create :count users
14+
*/
15+
public function iCreateUsers(int $count)
16+
{
17+
while ($count > 0) {
18+
$table = new TableNode([
19+
['property', 'value'],
20+
['login', 'testuser_' . $count],
21+
['firstname', 'first'],
22+
['lastname', 'last'],
23+
['mail', 'mail.' . $count . '@example.net'],
24+
]);
25+
26+
$this->iCreateAUserWithTheFollowingData($table);
27+
28+
$count--;
29+
}
30+
}
31+
1232
/**
1333
* @When I create a user with the following data
1434
*/
@@ -30,35 +50,63 @@ public function iCreateAUserWithTheFollowingData(TableNode $table)
3050
}
3151

3252
/**
33-
* @When I update the user with id :id and the following data
53+
* @When I show the user with id :userId
3454
*/
35-
public function iUpdateTheUserWithIdAndTheFollowingData($id, TableNode $table)
55+
public function iShowTheUserWithId(int $userId)
3656
{
37-
$data = [];
57+
/** @var User */
58+
$api = $this->getNativeCurlClient()->getApi('user');
3859

39-
foreach ($table as $row) {
40-
$data[$row['property']] = $row['value'];
41-
}
60+
$this->registerClientResponse(
61+
$api->show($userId),
62+
$api->getLastResponse(),
63+
);
64+
}
65+
66+
/**
67+
* @When I list all users
68+
*/
69+
public function iListAllUsers()
70+
{
71+
/** @var User */
72+
$api = $this->getNativeCurlClient()->getApi('user');
4273

74+
$this->registerClientResponse(
75+
$api->list(),
76+
$api->getLastResponse(),
77+
);
78+
}
79+
80+
/**
81+
* @When I list all user logins
82+
*/
83+
public function iListAllUserLogins()
84+
{
4385
/** @var User */
4486
$api = $this->getNativeCurlClient()->getApi('user');
4587

4688
$this->registerClientResponse(
47-
$api->update($id, $data),
89+
$api->listLogins(),
4890
$api->getLastResponse(),
4991
);
5092
}
5193

5294
/**
53-
* @When I show the user with id :userId
95+
* @When I update the user with id :id and the following data
5496
*/
55-
public function iShowTheUserWithId(int $userId)
97+
public function iUpdateTheUserWithIdAndTheFollowingData($id, TableNode $table)
5698
{
99+
$data = [];
100+
101+
foreach ($table as $row) {
102+
$data[$row['property']] = $row['value'];
103+
}
104+
57105
/** @var User */
58106
$api = $this->getNativeCurlClient()->getApi('user');
59107

60108
$this->registerClientResponse(
61-
$api->show($userId),
109+
$api->update($id, $data),
62110
$api->getLastResponse(),
63111
);
64112
}

tests/Behat/features/user.feature

Lines changed: 117 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Interacting with the REST API for users
44
As a user
55
I want to make sure the Redmine server replies with the correct response
66

7-
87
Scenario: Creating an user
98
Given I have a "NativeCurlClient" client
109
When I create a user with the following data
@@ -45,24 +44,6 @@ Feature: Interacting with the REST API for users
4544
| twofa_scheme | [] |
4645
| status | 1 |
4746

48-
Scenario: Updating an user
49-
Given I have a "NativeCurlClient" client
50-
And I create a user with the following data
51-
| property | value |
52-
| login | username |
53-
| firstname | first |
54-
| lastname | last |
55-
| mail | mail@example.com |
56-
When I update the user with id "5" and the following data
57-
| property | value |
58-
| firstname | new_first |
59-
| lastname | new_last |
60-
| mail | new_mail@example.com |
61-
Then the response has the status code "204"
62-
And the response has an empty content type
63-
And the response has the content ""
64-
And the returned data is exactly ""
65-
6647
Scenario: Showing a user
6748
Given I have a "NativeCurlClient" client
6849
When I show the user with id "1"
@@ -113,6 +94,123 @@ Feature: Interacting with the REST API for users
11394
And the response has the content ""
11495
And the returned data is false
11596

97+
Scenario: Listing of multiple users
98+
Given I have a "NativeCurlClient" client
99+
And I create a user with the following data
100+
| property | value |
101+
| login | username |
102+
| firstname | first |
103+
| lastname | last |
104+
| mail | mail@example.net |
105+
When I list all users
106+
Then the response has the status code "200"
107+
And the response has the content type "application/json"
108+
And the returned data has only the following properties
109+
"""
110+
users
111+
total_count
112+
offset
113+
limit
114+
"""
115+
And the returned data has proterties with the following data
116+
| property | value |
117+
| total_count | 2 |
118+
| offset | 0 |
119+
| limit | 25 |
120+
And the returned data "users" property is an array
121+
And the returned data "users" property contains "2" items
122+
And the returned data "users.0" property is an array
123+
And the returned data "users.0" property has only the following properties
124+
"""
125+
id
126+
login
127+
admin
128+
firstname
129+
lastname
130+
mail
131+
created_on
132+
updated_on
133+
last_login_on
134+
passwd_changed_on
135+
twofa_scheme
136+
"""
137+
And the returned data "users.0" property contains the following data
138+
| property | value |
139+
| id | 1 |
140+
| login | admin |
141+
| admin | true |
142+
| firstname | Redmine |
143+
| lastname | Admin |
144+
| mail | admin@example.net |
145+
| twofa_scheme | null |
146+
And the returned data "users.1" property is an array
147+
And the returned data "users.1" property has only the following properties
148+
"""
149+
id
150+
login
151+
admin
152+
firstname
153+
lastname
154+
mail
155+
created_on
156+
updated_on
157+
last_login_on
158+
passwd_changed_on
159+
twofa_scheme
160+
"""
161+
And the returned data "users.1" property contains the following data
162+
| property | value |
163+
| id | 5 |
164+
| login | username |
165+
| admin | false |
166+
| firstname | first |
167+
| lastname | last |
168+
| mail | mail@example.net |
169+
| twofa_scheme | null |
170+
171+
Scenario: Listing of multiple user logins
172+
Given I have a "NativeCurlClient" client
173+
And I create a user with the following data
174+
| property | value |
175+
| login | username |
176+
| firstname | first |
177+
| lastname | last |
178+
| mail | mail@example.net |
179+
When I list all user logins
180+
Then the response has the status code "200"
181+
And the response has the content type "application/json"
182+
And the returned data contains "2" items
183+
And the returned data has proterties with the following data
184+
| property | value |
185+
| 1 | admin |
186+
| 5 | username |
187+
188+
Scenario: Listing of multiple user logins
189+
Given I have a "NativeCurlClient" client
190+
And I create "108" users
191+
When I list all user logins
192+
Then the response has the status code "200"
193+
And the response has the content type "application/json"
194+
And the returned data contains "109" items
195+
196+
Scenario: Updating an user
197+
Given I have a "NativeCurlClient" client
198+
And I create a user with the following data
199+
| property | value |
200+
| login | username |
201+
| firstname | first |
202+
| lastname | last |
203+
| mail | mail@example.com |
204+
When I update the user with id "5" and the following data
205+
| property | value |
206+
| firstname | new_first |
207+
| lastname | new_last |
208+
| mail | new_mail@example.com |
209+
Then the response has the status code "204"
210+
And the response has an empty content type
211+
And the response has the content ""
212+
And the returned data is exactly ""
213+
116214
Scenario: Removing an user
117215
Given I have a "NativeCurlClient" client
118216
And I create a user with the following data

0 commit comments

Comments
 (0)