2023-02-02 13:47:00 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Api\Users;
|
|
|
|
|
2023-02-02 17:41:32 -08:00
|
|
|
use App\Models\Company;
|
2023-02-02 13:47:00 -08:00
|
|
|
use App\Models\Setting;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
use Illuminate\Testing\Fluent\AssertableJson;
|
|
|
|
use Laravel\Passport\Passport;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class UsersForSelectListTest extends TestCase
|
|
|
|
{
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
|
|
public function testUsersAreReturned()
|
|
|
|
{
|
|
|
|
Setting::factory()->create();
|
|
|
|
|
|
|
|
User::factory()->count(3)->create();
|
|
|
|
|
2023-02-02 18:25:54 -08:00
|
|
|
Passport::actingAs(User::factory()->firstAdmin()->create());
|
|
|
|
$this->getJson(route('api.users.selectlist'))
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonStructure([
|
|
|
|
'results',
|
|
|
|
'pagination',
|
|
|
|
'total_count',
|
|
|
|
'page',
|
|
|
|
'page_count',
|
|
|
|
])
|
|
|
|
->assertJson(fn(AssertableJson $json) => $json->has('results', 4)->etc());
|
2023-02-02 13:47:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUsersScopedToCompanyWhenMultipleFullCompanySupportEnabled()
|
|
|
|
{
|
2023-02-02 17:41:32 -08:00
|
|
|
Setting::factory()->withMultipleFullCompanySupport()->create();
|
2023-02-02 13:47:00 -08:00
|
|
|
|
2023-02-02 18:25:54 -08:00
|
|
|
$jedi = Company::factory()->has(User::factory()->count(3)->sequence(
|
|
|
|
['first_name' => 'Luke', 'last_name' => 'Skywalker', 'username' => 'lskywalker'],
|
|
|
|
['first_name' => 'Obi-Wan', 'last_name' => 'Kenobi', 'username' => 'okenobi'],
|
|
|
|
['first_name' => 'Anakin', 'last_name' => 'Skywalker', 'username' => 'askywalker'],
|
|
|
|
))->create();
|
2023-02-02 17:41:32 -08:00
|
|
|
|
2023-02-02 18:25:54 -08:00
|
|
|
$sith = Company::factory()
|
|
|
|
->has(User::factory()->state(['first_name' => 'Darth', 'last_name' => 'Vader', 'username' => 'dvader']))
|
2023-02-02 17:41:32 -08:00
|
|
|
->create();
|
|
|
|
|
|
|
|
Passport::actingAs($jedi->users->first());
|
2023-02-02 17:57:24 -08:00
|
|
|
$response = $this->getJson(route('api.users.selectlist'))->assertOk();
|
2023-02-02 17:41:32 -08:00
|
|
|
|
|
|
|
$results = collect($response->json('results'));
|
|
|
|
|
|
|
|
$this->assertEquals($jedi->users->count(), $results->count());
|
|
|
|
$this->assertTrue(
|
|
|
|
$results->pluck('text')->contains(fn($text) => str_contains($text, $jedi->users->first()->first_name))
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$results->pluck('text')->contains(fn($text) => str_contains($text, $sith->users->first()->first_name))
|
|
|
|
);
|
2023-02-02 13:47:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUsersScopedToCompanyDuringSearchWhenMultipleFullCompanySupportEnabled()
|
|
|
|
{
|
2023-02-02 17:57:24 -08:00
|
|
|
Setting::factory()->withMultipleFullCompanySupport()->create();
|
|
|
|
|
2023-02-02 18:25:54 -08:00
|
|
|
$jedi = Company::factory()->has(User::factory()->count(3)->sequence(
|
|
|
|
['first_name' => 'Luke', 'last_name' => 'Skywalker', 'username' => 'lskywalker'],
|
|
|
|
['first_name' => 'Obi-Wan', 'last_name' => 'Kenobi', 'username' => 'okenobi'],
|
|
|
|
['first_name' => 'Anakin', 'last_name' => 'Skywalker', 'username' => 'askywalker'],
|
|
|
|
))->create();
|
2023-02-02 17:57:24 -08:00
|
|
|
|
2023-02-02 18:25:54 -08:00
|
|
|
Company::factory()
|
|
|
|
->has(User::factory()->state(['first_name' => 'Darth', 'last_name' => 'Vader', 'username' => 'dvader']))
|
2023-02-02 17:57:24 -08:00
|
|
|
->create();
|
|
|
|
|
|
|
|
Passport::actingAs($jedi->users->first());
|
|
|
|
$response = $this->getJson(route('api.users.selectlist', ['search' => 'a']))->assertOk();
|
|
|
|
|
|
|
|
$results = collect($response->json('results'));
|
|
|
|
|
|
|
|
$this->assertEquals(3, $results->count());
|
|
|
|
$this->assertTrue($results->pluck('text')->contains(fn($text) => str_contains($text, 'Luke')));
|
|
|
|
$this->assertTrue($results->pluck('text')->contains(fn($text) => str_contains($text, 'Anakin')));
|
|
|
|
|
|
|
|
$response = $this->getJson(route('api.users.selectlist', ['search' => 'v']))->assertOk();
|
2023-02-02 18:25:54 -08:00
|
|
|
$this->assertEquals(0, collect($response->json('results'))->count());
|
2023-02-02 13:47:00 -08:00
|
|
|
}
|
|
|
|
}
|