Skip to content

Commit

Permalink
Replace User::getIdByUsername() with User::listLogins()
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Jul 9, 2024
1 parent 057a289 commit 5913938
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
12 changes: 10 additions & 2 deletions src/Redmine/Api/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,22 @@ private function cleanParams(array $params = [])
if (isset($params['assigned_to'])) {
$userApi = $this->getUserApi();

$params['assigned_to_id'] = $userApi->getIdByUsername($params['assigned_to']);
$params['assigned_to_id'] = array_search(
$params['assigned_to'],
$userApi->listLogins(),
true,
);
unset($params['assigned_to']);
}

if (isset($params['author'])) {
$userApi = $this->getUserApi();

$params['author_id'] = $userApi->getIdByUsername($params['author']);
$params['author_id'] = array_search(
$params['author'],
$userApi->listLogins(),
true,
);
unset($params['author']);
}

Expand Down
14 changes: 7 additions & 7 deletions tests/Unit/Api/Issue/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,12 @@ public function testCreateWithHttpClientRetrievesUserId()
$this,
[
'GET',
'/users.json',
'/users.json?limit=100&offset=0',
'application/json',
'',
200,
'application/json',
'{"users":[{"login":"Author Name","id":5},{"login":"Assigned to User Name","id":6}]}',
'{"users":[{"login":"user_5","id":5},{"login":"user_6","id":6}]}',
],
[
'POST',
Expand All @@ -432,7 +432,7 @@ public function testCreateWithHttpClientRetrievesUserId()
$api = new Issue($client);

// Perform the tests
$xmlElement = $api->create(['assigned_to' => 'Assigned to User Name', 'author' => 'Author Name']);
$xmlElement = $api->create(['assigned_to' => 'user_6', 'author' => 'user_5']);

$this->assertInstanceOf(SimpleXMLElement::class, $xmlElement);
$this->assertXmlStringEqualsXmlString(
Expand Down Expand Up @@ -486,12 +486,12 @@ public function testCreateWithClientCleansParameters()
],
[
'GET',
'/users.json',
'/users.json?limit=100&offset=0',
'application/json',
'',
200,
'application/json',
'{"users":[{"login":"Author Name","id":5},{"login":"Assigned to User Name","id":6}]}',
'{"users":[{"login":"user_5","id":5},{"login":"user_6","id":6}]}',
],
[
'POST',
Expand Down Expand Up @@ -519,8 +519,8 @@ public function testCreateWithClientCleansParameters()
'category' => 'Category Name',
'status' => 'Status Name',
'tracker' => 'Tracker Name',
'assigned_to' => 'Assigned to User Name',
'author' => 'Author Name',
'assigned_to' => 'user_6',
'author' => 'user_5',
];

// Create the object under test
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/Issue/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function testUpdateCleansParameters()
],
[
'GET',
'/users.json',
'/users.json?limit=100&offset=0',
'application/json',
'',
200,
Expand Down
23 changes: 14 additions & 9 deletions tests/Unit/Api/IssueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Redmine\Api\IssueStatus;
use Redmine\Api\Project;
use Redmine\Api\Tracker;
use Redmine\Api\User;
use Redmine\Client\Client;
use Redmine\Http\HttpClient;
use Redmine\Http\Response;
Expand Down Expand Up @@ -152,16 +153,11 @@ public function testCreateWithClientCleansParameters()
'category' => 'Category 5 Name',
'status' => 'Status 6 Name',
'tracker' => 'Tracker 2 Name',
'assigned_to' => 'Assigned to User Name',
'author' => 'Author Name',
'assigned_to' => 'user_3',
'author' => 'user_4',
];

// Create the used mock objects
$getIdByUsernameApi = $this->createMock('Redmine\Api\User');
$getIdByUsernameApi->expects($this->exactly(2))
->method('getIdByUsername')
->willReturn('cleanedValue');

$httpClient = AssertingHttpClient::create(
$this,
[
Expand Down Expand Up @@ -200,6 +196,15 @@ public function testCreateWithClientCleansParameters()
'application/json',
'{"trackers":[{"id":2,"name":"Tracker 2 Name"}]}',
],
[
'GET',
'/users.json?limit=100&offset=0',
'application/json',
'',
200,
'application/json',
'{"users":[{"id":3,"login":"user_3"},{"id":4,"login":"user_4"}]}',
],
);

$client = $this->createMock(Client::class);
Expand All @@ -211,7 +216,7 @@ public function testCreateWithClientCleansParameters()
['issue_category', new IssueCategory($httpClient)],
['issue_status', new IssueStatus($httpClient)],
['tracker', new Tracker($httpClient)],
['user', $getIdByUsernameApi],
['user', new User($httpClient)],
],
)
;
Expand All @@ -222,7 +227,7 @@ public function testCreateWithClientCleansParameters()
'/issues.xml',
<<< XML
<?xml version="1.0"?>
<issue><project_id>1</project_id><category_id>5</category_id><status_id>6</status_id><tracker_id>2</tracker_id><assigned_to_id>cleanedValue</assigned_to_id><author_id>cleanedValue</author_id></issue>
<issue><project_id>1</project_id><category_id>5</category_id><status_id>6</status_id><tracker_id>2</tracker_id><assigned_to_id>3</assigned_to_id><author_id>4</author_id></issue>
XML,
)
Expand Down

0 comments on commit 5913938

Please sign in to comment.