diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 9a9135a380..613158508b 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -286,9 +286,11 @@ class UsersController extends Controller $users = Company::scopeCompanyables($users); if ($request->filled('search')) { - $users = $users->SimpleNameSearch($request->get('search')) - ->orWhere('username', 'LIKE', '%'.$request->get('search').'%') - ->orWhere('employee_num', 'LIKE', '%'.$request->get('search').'%'); + $users = $users->where(function ($query) use ($request) { + $query->SimpleNameSearch($request->get('search')) + ->orWhere('username', 'LIKE', '%'.$request->get('search').'%') + ->orWhere('employee_num', 'LIKE', '%'.$request->get('search').'%'); + }); } $users = $users->orderBy('last_name', 'asc')->orderBy('first_name', 'asc'); diff --git a/app/Http/Controllers/Assets/AssetCheckoutController.php b/app/Http/Controllers/Assets/AssetCheckoutController.php index 38f7460986..e6326da6b1 100644 --- a/app/Http/Controllers/Assets/AssetCheckoutController.php +++ b/app/Http/Controllers/Assets/AssetCheckoutController.php @@ -27,7 +27,7 @@ class AssetCheckoutController extends Controller public function create($assetId) { // Check if the asset exists - if (is_null($asset = Asset::find(e($assetId)))) { + if (is_null($asset = Asset::with('company')->find(e($assetId)))) { return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist')); } diff --git a/database/factories/SettingFactory.php b/database/factories/SettingFactory.php index 73a9e049dc..cd88cdaf2f 100644 --- a/database/factories/SettingFactory.php +++ b/database/factories/SettingFactory.php @@ -52,4 +52,13 @@ class SettingFactory extends Factory 'email_domain' => 'test.com', ]; } + + public function withMultipleFullCompanySupport() + { + return $this->state(function () { + return [ + 'full_multiple_companies_support' => 1, + ]; + }); + } } diff --git a/resources/views/hardware/checkout.blade.php b/resources/views/hardware/checkout.blade.php index 31dff70eaa..98536c80f5 100755 --- a/resources/views/hardware/checkout.blade.php +++ b/resources/views/hardware/checkout.blade.php @@ -26,6 +26,16 @@
{{csrf_field()}} + @if ($asset->company && $asset->company->name) +
+ {{ Form::label('model', trans('general.company'), array('class' => 'col-md-3 control-label')) }} +
+

+ {{ $asset->company->name }} +

+
+
+ @endif
{{ Form::label('model', trans('admin/hardware/form.model'), array('class' => 'col-md-3 control-label')) }} @@ -33,7 +43,6 @@

@if (($asset->model) && ($asset->model->name)) {{ $asset->model->name }} - @else {{ trans('admin/hardware/general.model_invalid')}} @@ -168,4 +177,4 @@ -@stop \ No newline at end of file +@stop diff --git a/tests/Feature/Api/Users/UsersForSelectListTest.php b/tests/Feature/Api/Users/UsersForSelectListTest.php new file mode 100644 index 0000000000..558f362646 --- /dev/null +++ b/tests/Feature/Api/Users/UsersForSelectListTest.php @@ -0,0 +1,90 @@ +create(); + + User::factory()->count(3)->create(); + + 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()); + } + + public function testUsersScopedToCompanyWhenMultipleFullCompanySupportEnabled() + { + Setting::factory()->withMultipleFullCompanySupport()->create(); + + $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(); + + $sith = Company::factory() + ->has(User::factory()->state(['first_name' => 'Darth', 'last_name' => 'Vader', 'username' => 'dvader'])) + ->create(); + + Passport::actingAs($jedi->users->first()); + $response = $this->getJson(route('api.users.selectlist'))->assertOk(); + + $results = collect($response->json('results')); + + $this->assertEquals(3, $results->count()); + $this->assertTrue( + $results->pluck('text')->contains(fn($text) => str_contains($text, 'Luke')) + ); + $this->assertFalse( + $results->pluck('text')->contains(fn($text) => str_contains($text, 'Darth')) + ); + } + + public function testUsersScopedToCompanyDuringSearchWhenMultipleFullCompanySupportEnabled() + { + Setting::factory()->withMultipleFullCompanySupport()->create(); + + $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(); + + Company::factory() + ->has(User::factory()->state(['first_name' => 'Darth', 'last_name' => 'Vader', 'username' => 'dvader'])) + ->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(); + $this->assertEquals(0, collect($response->json('results'))->count()); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 2932d4a69d..5082341307 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,9 +2,17 @@ namespace Tests; +use App\Models\Setting; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { use CreatesApplication; + + protected function setUp(): void + { + parent::setUp(); + + $this->beforeApplicationDestroyed(fn() => Setting::$_cache = null); + } }