Skip to content

Commit 5913938

Browse files
committed
Replace User::getIdByUsername() with User::listLogins()
1 parent 057a289 commit 5913938

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

src/Redmine/Api/Issue.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,22 @@ private function cleanParams(array $params = [])
393393
if (isset($params['assigned_to'])) {
394394
$userApi = $this->getUserApi();
395395

396-
$params['assigned_to_id'] = $userApi->getIdByUsername($params['assigned_to']);
396+
$params['assigned_to_id'] = array_search(
397+
$params['assigned_to'],
398+
$userApi->listLogins(),
399+
true,
400+
);
397401
unset($params['assigned_to']);
398402
}
399403

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

403-
$params['author_id'] = $userApi->getIdByUsername($params['author']);
407+
$params['author_id'] = array_search(
408+
$params['author'],
409+
$userApi->listLogins(),
410+
true,
411+
);
404412
unset($params['author']);
405413
}
406414

tests/Unit/Api/Issue/CreateTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,12 @@ public function testCreateWithHttpClientRetrievesUserId()
410410
$this,
411411
[
412412
'GET',
413-
'/users.json',
413+
'/users.json?limit=100&offset=0',
414414
'application/json',
415415
'',
416416
200,
417417
'application/json',
418-
'{"users":[{"login":"Author Name","id":5},{"login":"Assigned to User Name","id":6}]}',
418+
'{"users":[{"login":"user_5","id":5},{"login":"user_6","id":6}]}',
419419
],
420420
[
421421
'POST',
@@ -432,7 +432,7 @@ public function testCreateWithHttpClientRetrievesUserId()
432432
$api = new Issue($client);
433433

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

437437
$this->assertInstanceOf(SimpleXMLElement::class, $xmlElement);
438438
$this->assertXmlStringEqualsXmlString(
@@ -486,12 +486,12 @@ public function testCreateWithClientCleansParameters()
486486
],
487487
[
488488
'GET',
489-
'/users.json',
489+
'/users.json?limit=100&offset=0',
490490
'application/json',
491491
'',
492492
200,
493493
'application/json',
494-
'{"users":[{"login":"Author Name","id":5},{"login":"Assigned to User Name","id":6}]}',
494+
'{"users":[{"login":"user_5","id":5},{"login":"user_6","id":6}]}',
495495
],
496496
[
497497
'POST',
@@ -519,8 +519,8 @@ public function testCreateWithClientCleansParameters()
519519
'category' => 'Category Name',
520520
'status' => 'Status Name',
521521
'tracker' => 'Tracker Name',
522-
'assigned_to' => 'Assigned to User Name',
523-
'author' => 'Author Name',
522+
'assigned_to' => 'user_6',
523+
'author' => 'user_5',
524524
];
525525

526526
// Create the object under test

tests/Unit/Api/Issue/UpdateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function testUpdateCleansParameters()
155155
],
156156
[
157157
'GET',
158-
'/users.json',
158+
'/users.json?limit=100&offset=0',
159159
'application/json',
160160
'',
161161
200,

tests/Unit/Api/IssueTest.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Redmine\Api\IssueStatus;
1111
use Redmine\Api\Project;
1212
use Redmine\Api\Tracker;
13+
use Redmine\Api\User;
1314
use Redmine\Client\Client;
1415
use Redmine\Http\HttpClient;
1516
use Redmine\Http\Response;
@@ -152,16 +153,11 @@ public function testCreateWithClientCleansParameters()
152153
'category' => 'Category 5 Name',
153154
'status' => 'Status 6 Name',
154155
'tracker' => 'Tracker 2 Name',
155-
'assigned_to' => 'Assigned to User Name',
156-
'author' => 'Author Name',
156+
'assigned_to' => 'user_3',
157+
'author' => 'user_4',
157158
];
158159

159160
// Create the used mock objects
160-
$getIdByUsernameApi = $this->createMock('Redmine\Api\User');
161-
$getIdByUsernameApi->expects($this->exactly(2))
162-
->method('getIdByUsername')
163-
->willReturn('cleanedValue');
164-
165161
$httpClient = AssertingHttpClient::create(
166162
$this,
167163
[
@@ -200,6 +196,15 @@ public function testCreateWithClientCleansParameters()
200196
'application/json',
201197
'{"trackers":[{"id":2,"name":"Tracker 2 Name"}]}',
202198
],
199+
[
200+
'GET',
201+
'/users.json?limit=100&offset=0',
202+
'application/json',
203+
'',
204+
200,
205+
'application/json',
206+
'{"users":[{"id":3,"login":"user_3"},{"id":4,"login":"user_4"}]}',
207+
],
203208
);
204209

205210
$client = $this->createMock(Client::class);
@@ -211,7 +216,7 @@ public function testCreateWithClientCleansParameters()
211216
['issue_category', new IssueCategory($httpClient)],
212217
['issue_status', new IssueStatus($httpClient)],
213218
['tracker', new Tracker($httpClient)],
214-
['user', $getIdByUsernameApi],
219+
['user', new User($httpClient)],
215220
],
216221
)
217222
;
@@ -222,7 +227,7 @@ public function testCreateWithClientCleansParameters()
222227
'/issues.xml',
223228
<<< XML
224229
<?xml version="1.0"?>
225-
<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>
230+
<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>
226231
227232
XML,
228233
)

0 commit comments

Comments
 (0)