Skip to content

Add User::listLogins() method as replacement for User::listing() #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | [email protected] |
When I update the user with id "5" and the following data
| property | value |
| firstname | new_first |
| lastname | new_last |
| mail | [email protected] |
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 | [email protected] |
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 | [email protected] |
| 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 | [email protected] |
| 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 | [email protected] |
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 | [email protected] |
When I update the user with id "5" and the following data
| property | value |
| firstname | new_first |
| lastname | new_last |
| mail | [email protected] |
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
Loading