1
0
mirror of https://github.com/flarum/core.git synced 2025-07-25 10:41:24 +02:00

Search Filter Split, Use Same Controller (#2454)

This commit is contained in:
Alexander Skvortsov
2021-02-24 11:17:40 -05:00
committed by GitHub
parent 1c578a83e4
commit 023871ef86
42 changed files with 1663 additions and 535 deletions

View File

@@ -11,11 +11,26 @@ namespace Flarum\Tests\integration\api\users;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
use Illuminate\Support\Arr;
class ListTest extends TestCase
{
use RetrievesAuthorizedUsers;
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();
$this->prepareDatabase([
'users' => [
$this->normalUser(),
],
]);
}
/**
* @test
*/
@@ -59,4 +74,200 @@ class ListTest extends TestCase
$this->assertEquals(200, $response->getStatusCode());
}
/**
* @test
*/
public function shows_full_results_without_search_or_filter()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])
);
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['1', '2'], Arr::pluck($data, 'id'));
}
/**
* @test
*/
public function group_filter_works()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['group' => '1'],
])
);
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['1'], Arr::pluck($data, 'id'));
}
/**
* @test
*/
public function group_filter_works_negated()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['-group' => '1'],
])
);
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['2'], Arr::pluck($data, 'id'));
}
/**
* @test
*/
public function email_filter_works()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['email' => 'admin@machine.local'],
])
);
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['1'], Arr::pluck($data, 'id'));
}
/**
* @test
*/
public function email_filter_works_negated()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['-email' => 'admin@machine.local'],
])
);
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['2'], Arr::pluck($data, 'id'));
}
/**
* @test
*/
public function email_filter_only_works_for_admin()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 2,
])->withQueryParams([
'filter' => ['email' => 'admin@machine.local'],
])
);
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['1', '2'], Arr::pluck($data, 'id'));
}
/**
* @test
*/
public function group_gambit_works()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['q' => 'group:1'],
])
);
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['1'], Arr::pluck($data, 'id'));
}
/**
* @test
*/
public function group_gambit_works_negated()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['q' => '-group:1'],
])
);
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['2'], Arr::pluck($data, 'id'));
}
/**
* @test
*/
public function email_gambit_works()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['q' => 'email:admin@machine.local'],
])
);
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['1'], Arr::pluck($data, 'id'));
}
/**
* @test
*/
public function email_gambit_works_negated()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['q' => '-email:admin@machine.local'],
])
);
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['2'], Arr::pluck($data, 'id'));
}
/**
* @test
*/
public function email_gambit_only_works_for_admin()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 2,
])->withQueryParams([
'filter' => ['q' => 'email:admin@machine.local'],
])
);
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals([], Arr::pluck($data, 'id'));
}
}