77 lines
2.6 KiB
PHP
77 lines
2.6 KiB
PHP
<?php
|
||
|
||
namespace App\Tests\Admin;
|
||
|
||
use App\Tests\ApiTestCase;
|
||
|
||
/**
|
||
* GET /api/v1/admin/users?search=… — the search box must accept a numeric user id
|
||
* on top of name / mobile / email.
|
||
*/
|
||
class AdminUsersSearchTest extends ApiTestCase
|
||
{
|
||
private function idsOf(array $body): array
|
||
{
|
||
return array_map(static fn(array $u) => $u['id'], $body['data']);
|
||
}
|
||
|
||
public function testSearchByUserIdReturnsThatUserOnly(): void
|
||
{
|
||
$target = $this->createUser(['ROLE_USER']);
|
||
$other = $this->createUser(['ROLE_USER']);
|
||
$admin = $this->createUser(['ROLE_ADMIN']);
|
||
|
||
$body = $this->authJson('GET', '/api/v1/admin/users?search=' . $target->getId(), $admin);
|
||
|
||
self::assertSame(200, $this->responseCode());
|
||
self::assertTrue($body['success']);
|
||
self::assertContains($target->getId(), $this->idsOf($body));
|
||
self::assertNotContains($other->getId(), $this->idsOf($body));
|
||
}
|
||
|
||
public function testSearchByPersianDigitsMatchesUserId(): void
|
||
{
|
||
$target = $this->createUser(['ROLE_USER']);
|
||
$admin = $this->createUser(['ROLE_ADMIN']);
|
||
|
||
$persianId = strtr((string) $target->getId(), [
|
||
'0' => '۰', '1' => '۱', '2' => '۲', '3' => '۳', '4' => '۴',
|
||
'5' => '۵', '6' => '۶', '7' => '۷', '8' => '۸', '9' => '۹',
|
||
]);
|
||
|
||
$body = $this->authJson('GET', '/api/v1/admin/users?search=' . urlencode($persianId), $admin);
|
||
|
||
self::assertSame(200, $this->responseCode());
|
||
self::assertContains($target->getId(), $this->idsOf($body));
|
||
}
|
||
|
||
public function testSearchByMobileStillWorks(): void
|
||
{
|
||
$target = $this->createUser(['ROLE_USER']);
|
||
$admin = $this->createUser(['ROLE_ADMIN']);
|
||
|
||
$body = $this->authJson('GET', '/api/v1/admin/users?search=' . $target->getMobileNumber(), $admin);
|
||
|
||
self::assertSame(200, $this->responseCode());
|
||
self::assertContains($target->getId(), $this->idsOf($body));
|
||
}
|
||
|
||
public function testSearchByUnknownIdReturnsEmptyList(): void
|
||
{
|
||
$admin = $this->createUser(['ROLE_ADMIN']);
|
||
|
||
$body = $this->authJson('GET', '/api/v1/admin/users?search=999999999', $admin);
|
||
|
||
self::assertSame(200, $this->responseCode());
|
||
self::assertSame([], $body['data']);
|
||
self::assertSame(0, $body['meta']['totalRecords']);
|
||
}
|
||
|
||
public function testNonAdminForbidden(): void
|
||
{
|
||
$user = $this->createUser(['ROLE_USER']);
|
||
$this->authJson('GET', '/api/v1/admin/users?search=1', $user);
|
||
self::assertSame(403, $this->responseCode());
|
||
}
|
||
}
|