2021-12-14 13:47:22 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\User;
|
|
|
|
|
|
|
|
use BookStack\Auth\User;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class UserSearchTest extends TestCase
|
|
|
|
{
|
|
|
|
public function test_select_search_matches_by_name()
|
|
|
|
{
|
2023-01-21 06:08:34 -05:00
|
|
|
$viewer = $this->users->viewer();
|
|
|
|
$admin = $this->users->admin();
|
2021-12-14 13:47:22 -05:00
|
|
|
$resp = $this->actingAs($admin)->get('/search/users/select?search=' . urlencode($viewer->name));
|
|
|
|
|
|
|
|
$resp->assertOk();
|
|
|
|
$resp->assertSee($viewer->name);
|
|
|
|
$resp->assertDontSee($admin->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_select_search_shows_first_by_name_without_search()
|
|
|
|
{
|
|
|
|
/** @var User $firstUser */
|
|
|
|
$firstUser = User::query()->orderBy('name', 'desc')->first();
|
|
|
|
$resp = $this->asAdmin()->get('/search/users/select');
|
|
|
|
|
|
|
|
$resp->assertOk();
|
|
|
|
$resp->assertSee($firstUser->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_select_search_does_not_match_by_email()
|
|
|
|
{
|
2023-01-21 06:08:34 -05:00
|
|
|
$viewer = $this->users->viewer();
|
|
|
|
$editor = $this->users->editor();
|
2021-12-14 13:47:22 -05:00
|
|
|
$resp = $this->actingAs($editor)->get('/search/users/select?search=' . urlencode($viewer->email));
|
|
|
|
|
|
|
|
$resp->assertDontSee($viewer->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_select_requires_right_permission()
|
|
|
|
{
|
|
|
|
$permissions = ['users-manage', 'restrictions-manage-own', 'restrictions-manage-all'];
|
2023-01-21 06:08:34 -05:00
|
|
|
$user = $this->users->viewer();
|
2021-12-14 13:47:22 -05:00
|
|
|
|
|
|
|
foreach ($permissions as $permission) {
|
|
|
|
$resp = $this->actingAs($user)->get('/search/users/select?search=a');
|
|
|
|
$this->assertPermissionError($resp);
|
|
|
|
|
2023-01-21 06:08:34 -05:00
|
|
|
$this->permissions->grantUserRolePermissions($user, [$permission]);
|
2021-12-14 13:47:22 -05:00
|
|
|
$resp = $this->actingAs($user)->get('/search/users/select?search=a');
|
|
|
|
$resp->assertOk();
|
|
|
|
$user->roles()->delete();
|
|
|
|
$user->clearPermissionCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_select_requires_logged_in_user()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => true]);
|
|
|
|
$defaultUser = User::getDefault();
|
2023-01-21 06:08:34 -05:00
|
|
|
$this->permissions->grantUserRolePermissions($defaultUser, ['users-manage']);
|
2021-12-14 13:47:22 -05:00
|
|
|
|
|
|
|
$resp = $this->get('/search/users/select?search=a');
|
|
|
|
$this->assertPermissionError($resp);
|
|
|
|
}
|
2021-12-15 08:49:20 -05:00
|
|
|
}
|