Skip to content

Commit

Permalink
Merge pull request #419 from Art4/add-user-listlogins
Browse files Browse the repository at this point in the history
Add `User::listLogins()` method as replacement for `User::listing()`
  • Loading branch information
Art4 authored Jul 8, 2024
2 parents e31143f + 5da7204 commit 324a946
Show file tree
Hide file tree
Showing 6 changed files with 420 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New method `Redmine\Api\Role::listNames()` for listing the ids and names of all roles.
- New method `Redmine\Api\TimeEntryActivity::listNames()` for listing the ids and names of all time entry activities.
- New method `Redmine\Api\Tracker::listNames()` for listing the ids and names of all trackers.
- New method `Redmine\Api\User::listLogins()` for listing the ids and logins of all users.

### Deprecated

Expand All @@ -28,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Redmine\Api\Role::listing()` is deprecated, use `\Redmine\Api\Role::listNames()` instead.
- `Redmine\Api\TimeEntryActivity::listing()` is deprecated, use `\Redmine\Api\TimeEntryActivity::listNames()` instead.
- `Redmine\Api\Tracker::listing()` is deprecated, use `\Redmine\Api\Tracker::listNames()` instead.
- `Redmine\Api\User::listing()` is deprecated, use `\Redmine\Api\User::listLogins()` instead.

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

Expand Down
44 changes: 44 additions & 0 deletions src/Redmine/Api/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class User extends AbstractApi
{
private $users = [];

private $userLogins = null;

/**
* List users.
*
Expand All @@ -43,6 +45,43 @@ final public function list(array $params = []): array
}
}

/**
* Returns an array of all users with id/login pairs.
*
* @return array<int,string> list of users (id => login)
*/
final public function listLogins(): array
{
if ($this->userLogins !== null) {
return $this->userLogins;
}

$this->userLogins = [];

$limit = 100;
$offset = 0;

do {
$list = $this->list([
'limit' => $limit,
'offset' => $offset,
]);

$listCount = 0;
$offset += $limit;

if (array_key_exists('users', $list)) {
$listCount = count($list['users']);

foreach ($list['users'] as $user) {
$this->userLogins[(int) $user['id']] = (string) $user['login'];
}
}
} while ($listCount === $limit);

return $this->userLogins;
}

/**
* List users.
*
Expand Down Expand Up @@ -79,13 +118,18 @@ public function all(array $params = [])
/**
* Returns an array of users with login/id pairs.
*
* @deprecated v2.7.0 Use listLogins() instead.
* @see User::listLogins()
*
* @param bool $forceUpdate to force the update of the users var
* @param array $params to allow offset/limit (and more) to be passed
*
* @return array list of users (id => username)
*/
public function listing($forceUpdate = false, array $params = [])
{
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listLogins()` instead.', E_USER_DEPRECATED);

if (empty($this->users) || $forceUpdate) {
$this->users = $this->list($params);
}
Expand Down
68 changes: 58 additions & 10 deletions tests/Behat/Bootstrap/UserContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@

trait UserContextTrait
{
/**
* @Given I create :count users
*/
public function iCreateUsers(int $count)
{
while ($count > 0) {
$table = new TableNode([
['property', 'value'],
['login', 'testuser_' . $count],
['firstname', 'first'],
['lastname', 'last'],
['mail', 'mail.' . $count . '@example.net'],
]);

$this->iCreateAUserWithTheFollowingData($table);

$count--;
}
}

/**
* @When I create a user with the following data
*/
Expand All @@ -30,35 +50,63 @@ public function iCreateAUserWithTheFollowingData(TableNode $table)
}

/**
* @When I update the user with id :id and the following data
* @When I show the user with id :userId
*/
public function iUpdateTheUserWithIdAndTheFollowingData($id, TableNode $table)
public function iShowTheUserWithId(int $userId)
{
$data = [];
/** @var User */
$api = $this->getNativeCurlClient()->getApi('user');

foreach ($table as $row) {
$data[$row['property']] = $row['value'];
}
$this->registerClientResponse(
$api->show($userId),
$api->getLastResponse(),
);
}

/**
* @When I list all users
*/
public function iListAllUsers()
{
/** @var User */
$api = $this->getNativeCurlClient()->getApi('user');

$this->registerClientResponse(
$api->list(),
$api->getLastResponse(),
);
}

/**
* @When I list all user logins
*/
public function iListAllUserLogins()
{
/** @var User */
$api = $this->getNativeCurlClient()->getApi('user');

$this->registerClientResponse(
$api->update($id, $data),
$api->listLogins(),
$api->getLastResponse(),
);
}

/**
* @When I show the user with id :userId
* @When I update the user with id :id and the following data
*/
public function iShowTheUserWithId(int $userId)
public function iUpdateTheUserWithIdAndTheFollowingData($id, TableNode $table)
{
$data = [];

foreach ($table as $row) {
$data[$row['property']] = $row['value'];
}

/** @var User */
$api = $this->getNativeCurlClient()->getApi('user');

$this->registerClientResponse(
$api->show($userId),
$api->update($id, $data),
$api->getLastResponse(),
);
}
Expand Down
136 changes: 117 additions & 19 deletions tests/Behat/features/user.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Feature: Interacting with the REST API for users
As a user
I want to make sure the Redmine server replies with the correct response


Scenario: Creating an user
Given I have a "NativeCurlClient" client
When I create a user with the following data
Expand Down Expand Up @@ -45,24 +44,6 @@ Feature: Interacting with the REST API for users
| twofa_scheme | [] |
| status | 1 |

Scenario: Updating an user
Given I have a "NativeCurlClient" client
And I create a user with the following data
| property | value |
| login | username |
| firstname | first |
| lastname | last |
| mail | mail@example.com |
When I update the user with id "5" and the following data
| property | value |
| firstname | new_first |
| lastname | new_last |
| mail | new_mail@example.com |
Then the response has the status code "204"
And the response has an empty content type
And the response has the content ""
And the returned data is exactly ""

Scenario: Showing a user
Given I have a "NativeCurlClient" client
When I show the user with id "1"
Expand Down Expand Up @@ -113,6 +94,123 @@ Feature: Interacting with the REST API for users
And the response has the content ""
And the returned data is false

Scenario: Listing of multiple users
Given I have a "NativeCurlClient" client
And I create a user with the following data
| property | value |
| login | username |
| firstname | first |
| lastname | last |
| mail | mail@example.net |
When I list all users
Then the response has the status code "200"
And the response has the content type "application/json"
And the returned data has only the following properties
"""
users
total_count
offset
limit
"""
And the returned data has proterties with the following data
| property | value |
| total_count | 2 |
| offset | 0 |
| limit | 25 |
And the returned data "users" property is an array
And the returned data "users" property contains "2" items
And the returned data "users.0" property is an array
And the returned data "users.0" property has only the following properties
"""
id
login
admin
firstname
lastname
mail
created_on
updated_on
last_login_on
passwd_changed_on
twofa_scheme
"""
And the returned data "users.0" property contains the following data
| property | value |
| id | 1 |
| login | admin |
| admin | true |
| firstname | Redmine |
| lastname | Admin |
| mail | admin@example.net |
| twofa_scheme | null |
And the returned data "users.1" property is an array
And the returned data "users.1" property has only the following properties
"""
id
login
admin
firstname
lastname
mail
created_on
updated_on
last_login_on
passwd_changed_on
twofa_scheme
"""
And the returned data "users.1" property contains the following data
| property | value |
| id | 5 |
| login | username |
| admin | false |
| firstname | first |
| lastname | last |
| mail | mail@example.net |
| twofa_scheme | null |

Scenario: Listing of multiple user logins
Given I have a "NativeCurlClient" client
And I create a user with the following data
| property | value |
| login | username |
| firstname | first |
| lastname | last |
| mail | mail@example.net |
When I list all user logins
Then the response has the status code "200"
And the response has the content type "application/json"
And the returned data contains "2" items
And the returned data has proterties with the following data
| property | value |
| 1 | admin |
| 5 | username |

Scenario: Listing of multiple user logins
Given I have a "NativeCurlClient" client
And I create "108" users
When I list all user logins
Then the response has the status code "200"
And the response has the content type "application/json"
And the returned data contains "109" items

Scenario: Updating an user
Given I have a "NativeCurlClient" client
And I create a user with the following data
| property | value |
| login | username |
| firstname | first |
| lastname | last |
| mail | mail@example.com |
When I update the user with id "5" and the following data
| property | value |
| firstname | new_first |
| lastname | new_last |
| mail | new_mail@example.com |
Then the response has the status code "204"
And the response has an empty content type
And the response has the content ""
And the returned data is exactly ""

Scenario: Removing an user
Given I have a "NativeCurlClient" client
And I create a user with the following data
Expand Down
Loading

0 comments on commit 324a946

Please sign in to comment.