Skip to content

Commit de62359

Browse files
authored
Merge pull request #15533 from marcusmoore/testing/fmcs-accessories
Added tests for accessory api controller
2 parents 12bda8f + 3dc64cc commit de62359

File tree

12 files changed

+542
-58
lines changed

12 files changed

+542
-58
lines changed

app/Http/Controllers/Api/AccessoriesController.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ public function index(Request $request)
137137
*/
138138
public function store(StoreAccessoryRequest $request)
139139
{
140-
$this->authorize('create', Accessory::class);
141140
$accessory = new Accessory;
142141
$accessory->fill($request->all());
143142
$accessory = $request->handleImages($accessory);
@@ -197,9 +196,6 @@ public function checkedout($id, Request $request)
197196
$this->authorize('view', Accessory::class);
198197

199198
$accessory = Accessory::with('lastCheckout')->findOrFail($id);
200-
if (! Company::isCurrentUserHasAccess($accessory)) {
201-
return ['total' => 0, 'rows' => []];
202-
}
203199

204200
$offset = request('offset', 0);
205201
$limit = request('limit', 50);
@@ -325,22 +321,14 @@ public function checkin(Request $request, $accessoryUserId = null)
325321
$accessory = Accessory::find($accessory_checkout->accessory_id);
326322
$this->authorize('checkin', $accessory);
327323

328-
$logaction = $accessory->logCheckin(User::find($accessory_checkout->assigned_to), $request->input('note'));
324+
$accessory->logCheckin(User::find($accessory_checkout->assigned_to), $request->input('note'));
329325

330326
// Was the accessory updated?
331327
if ($accessory_checkout->delete()) {
332328
if (! is_null($accessory_checkout->assigned_to)) {
333329
$user = User::find($accessory_checkout->assigned_to);
334330
}
335331

336-
$data['log_id'] = $logaction->id;
337-
$data['first_name'] = $user->first_name;
338-
$data['last_name'] = $user->last_name;
339-
$data['item_name'] = $accessory->name;
340-
$data['checkin_date'] = $logaction->created_at;
341-
$data['item_tag'] = '';
342-
$data['note'] = $logaction->note;
343-
344332
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/accessories/message.checkin.success')));
345333
}
346334

app/Models/Loggable.php

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ private function determineLogItemType($log)
117117
*/
118118
public function logCheckin($target, $note, $action_date = null, $originalValues = [])
119119
{
120-
$settings = Setting::getSettings();
121120
$log = new Actionlog;
122121

123122
if($target != null){
@@ -171,39 +170,6 @@ public function logCheckin($target, $note, $action_date = null, $originalValues
171170

172171
$log->logaction('checkin from');
173172

174-
// $params = [
175-
// 'target' => $target,
176-
// 'item' => $log->item,
177-
// 'admin' => $log->user,
178-
// 'note' => $note,
179-
// 'target_type' => $log->target_type,
180-
// 'settings' => $settings,
181-
// ];
182-
//
183-
//
184-
// $checkinClass = null;
185-
//
186-
// if (method_exists($target, 'notify')) {
187-
// try {
188-
// $target->notify(new static::$checkinClass($params));
189-
// } catch (\Exception $e) {
190-
// Log::debug($e);
191-
// }
192-
//
193-
// }
194-
//
195-
// // Send to the admin, if settings dictate
196-
// $recipient = new \App\Models\Recipients\AdminRecipient();
197-
//
198-
// if (($settings->admin_cc_email!='') && (static::$checkinClass!='')) {
199-
// try {
200-
// $recipient->notify(new static::$checkinClass($params));
201-
// } catch (\Exception $e) {
202-
// Log::debug($e);
203-
// }
204-
//
205-
// }
206-
207173
return $log;
208174
}
209175

database/factories/AccessoryFactory.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Database\Factories;
44

55
use App\Models\Accessory;
6-
use App\Models\AccessoryCheckout;
76
use App\Models\Category;
87
use App\Models\Location;
98
use App\Models\Manufacturer;
@@ -156,4 +155,19 @@ public function checkedOutToUser(User $user = null)
156155
]);
157156
});
158157
}
158+
159+
public function checkedOutToUsers(array $users)
160+
{
161+
return $this->afterCreating(function (Accessory $accessory) use ($users) {
162+
foreach ($users as $user) {
163+
$accessory->checkouts()->create([
164+
'accessory_id' => $accessory->id,
165+
'created_at' => Carbon::now(),
166+
'user_id' => 1,
167+
'assigned_to' => $user->id,
168+
'assigned_type' => User::class,
169+
]);
170+
}
171+
});
172+
}
159173
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Tests\Feature\Accessories\Api;
4+
5+
use App\Models\Accessory;
6+
use App\Models\Company;
7+
use App\Models\User;
8+
use Tests\Concerns\TestsFullMultipleCompaniesSupport;
9+
use Tests\TestCase;
10+
11+
class AccessoriesForSelectListTest extends TestCase implements TestsFullMultipleCompaniesSupport
12+
{
13+
public function testAdheresToFullMultipleCompaniesSupportScoping()
14+
{
15+
[$companyA, $companyB] = Company::factory()->count(2)->create();
16+
17+
$accessoryA = Accessory::factory()->for($companyA)->create();
18+
$accessoryB = Accessory::factory()->for($companyB)->create();
19+
20+
$superuser = User::factory()->superuser()->create();
21+
$userInCompanyA = $companyA->users()->save(User::factory()->viewAccessories()->make());
22+
$userInCompanyB = $companyB->users()->save(User::factory()->viewAccessories()->make());
23+
24+
$this->settings->enableMultipleFullCompanySupport();
25+
26+
$this->actingAsForApi($userInCompanyA)
27+
->getJson(route('api.accessories.selectlist'))
28+
->assertOk()
29+
->assertJsonPath('total_count', 1)
30+
->assertResponseContainsInResults($accessoryA)
31+
->assertResponseDoesNotContainInResults($accessoryB);
32+
33+
$this->actingAsForApi($userInCompanyB)
34+
->getJson(route('api.accessories.selectlist'))
35+
->assertOk()
36+
->assertJsonPath('total_count', 1)
37+
->assertResponseDoesNotContainInResults($accessoryA)
38+
->assertResponseContainsInResults($accessoryB);
39+
40+
$this->actingAsForApi($superuser)
41+
->getJson(route('api.accessories.selectlist'))
42+
->assertOk()
43+
->assertJsonPath('total_count', 2)
44+
->assertResponseContainsInResults($accessoryA)
45+
->assertResponseContainsInResults($accessoryB);
46+
}
47+
48+
public function testCanGetAccessoriesForSelectList()
49+
{
50+
[$accessoryA, $accessoryB] = Accessory::factory()->count(2)->create();
51+
52+
$this->actingAsForApi(User::factory()->viewAccessories()->create())
53+
->getJson(route('api.accessories.selectlist'))
54+
->assertOk()
55+
->assertJsonPath('total_count', 2)
56+
->assertResponseContainsInResults($accessoryA)
57+
->assertResponseContainsInResults($accessoryB);
58+
}
59+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Tests\Feature\Accessories\Api;
4+
5+
use App\Models\Accessory;
6+
use App\Models\Company;
7+
use App\Models\User;
8+
use Tests\Concerns\TestsFullMultipleCompaniesSupport;
9+
use Tests\Concerns\TestsPermissionsRequirement;
10+
use Tests\TestCase;
11+
12+
class IndexAccessoryCheckoutsTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement
13+
{
14+
public function testRequiresPermission()
15+
{
16+
$accessory = Accessory::factory()->create();
17+
18+
$this->actingAsForApi(User::factory()->create())
19+
->getJson(route('api.accessories.checkedout', $accessory))
20+
->assertForbidden();
21+
}
22+
23+
public function testAdheresToFullMultipleCompaniesSupportScoping()
24+
{
25+
[$companyA, $companyB] = Company::factory()->count(2)->create();
26+
27+
$accessoryA = Accessory::factory()->for($companyA)->create();
28+
$accessoryB = Accessory::factory()->for($companyB)->create();
29+
30+
$superuser = User::factory()->superuser()->create();
31+
$userInCompanyA = $companyA->users()->save(User::factory()->viewAccessories()->make());
32+
$userInCompanyB = $companyB->users()->save(User::factory()->viewAccessories()->make());
33+
34+
$this->settings->enableMultipleFullCompanySupport();
35+
36+
$this->actingAsForApi($userInCompanyA)
37+
->getJson(route('api.accessories.checkedout', $accessoryB))
38+
->assertStatusMessageIs('error');
39+
40+
$this->actingAsForApi($userInCompanyB)
41+
->getJson(route('api.accessories.checkedout', $accessoryA))
42+
->assertStatusMessageIs('error');
43+
44+
$this->actingAsForApi($superuser)
45+
->getJson(route('api.accessories.checkedout', $accessoryA))
46+
->assertOk();
47+
}
48+
49+
public function testCanGetAccessoryCheckouts()
50+
{
51+
[$userA, $userB] = User::factory()->count(2)->create();
52+
53+
$accessory = Accessory::factory()->checkedOutToUsers([$userA, $userB])->create();
54+
55+
$this->assertEquals(2, $accessory->checkouts()->count());
56+
57+
$this->actingAsForApi(User::factory()->viewAccessories()->create())
58+
->getJson(route('api.accessories.checkedout', $accessory))
59+
->assertOk()
60+
->assertJsonPath('total', 2)
61+
->assertJsonPath('rows.0.assigned_to.id', $userA->id)
62+
->assertJsonPath('rows.1.assigned_to.id', $userB->id);
63+
}
64+
65+
public function testCanGetAccessoryCheckoutsWithOffsetAndLimitInQueryString()
66+
{
67+
[$userA, $userB, $userC] = User::factory()->count(3)->create();
68+
69+
$accessory = Accessory::factory()->checkedOutToUsers([$userA, $userB, $userC])->create();
70+
71+
$actor = $this->actingAsForApi(User::factory()->viewAccessories()->create());
72+
73+
$actor->getJson(route('api.accessories.checkedout', ['accessory' => $accessory->id, 'limit' => 1]))
74+
->assertOk()
75+
->assertJsonPath('total', 3)
76+
->assertJsonPath('rows.0.assigned_to.id', $userA->id);
77+
78+
$actor->getJson(route('api.accessories.checkedout', ['accessory' => $accessory->id, 'limit' => 2, 'offset' => 1]))
79+
->assertOk()
80+
->assertJsonPath('total', 3)
81+
->assertJsonPath('rows.0.assigned_to.id', $userB->id)
82+
->assertJsonPath('rows.1.assigned_to.id', $userC->id);
83+
}
84+
}

tests/Feature/Accessories/Api/IndexAccessoryTest.php

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,69 @@
22

33
namespace Tests\Feature\Accessories\Api;
44

5+
use App\Models\Accessory;
6+
use App\Models\Company;
57
use App\Models\User;
8+
use Tests\Concerns\TestsFullMultipleCompaniesSupport;
9+
use Tests\Concerns\TestsPermissionsRequirement;
610
use Tests\TestCase;
711

8-
class IndexAccessoryTest extends TestCase
12+
class IndexAccessoryTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement
913
{
10-
public function testPermissionRequiredToViewAccessoriesIndex()
14+
public function testRequiresPermission()
1115
{
1216
$this->actingAsForApi(User::factory()->create())
1317
->getJson(route('api.accessories.index'))
1418
->assertForbidden();
1519
}
20+
21+
public function testAdheresToFullMultipleCompaniesSupportScoping()
22+
{
23+
[$companyA, $companyB] = Company::factory()->count(2)->create();
24+
25+
$accessoryA = Accessory::factory()->for($companyA)->create(['name' => 'Accessory A']);
26+
$accessoryB = Accessory::factory()->for($companyB)->create(['name' => 'Accessory B']);
27+
$accessoryC = Accessory::factory()->for($companyB)->create(['name' => 'Accessory C']);
28+
29+
$superUser = $companyA->users()->save(User::factory()->superuser()->make());
30+
$userInCompanyA = $companyA->users()->save(User::factory()->viewAccessories()->make());
31+
$userInCompanyB = $companyB->users()->save(User::factory()->viewAccessories()->make());
32+
33+
$this->settings->enableMultipleFullCompanySupport();
34+
35+
$this->actingAsForApi($userInCompanyA)
36+
->getJson(route('api.accessories.index'))
37+
->assertOk()
38+
->assertResponseContainsInRows($accessoryA)
39+
->assertResponseDoesNotContainInRows($accessoryB)
40+
->assertResponseDoesNotContainInRows($accessoryC);
41+
42+
$this->actingAsForApi($userInCompanyB)
43+
->getJson(route('api.accessories.index'))
44+
->assertOk()
45+
->assertResponseDoesNotContainInRows($accessoryA)
46+
->assertResponseContainsInRows($accessoryB)
47+
->assertResponseContainsInRows($accessoryC);
48+
49+
$this->actingAsForApi($superUser)
50+
->getJson(route('api.accessories.index'))
51+
->assertOk()
52+
->assertResponseContainsInRows($accessoryA)
53+
->assertResponseContainsInRows($accessoryB)
54+
->assertResponseContainsInRows($accessoryC);
55+
}
56+
57+
public function testCanGetAccessories()
58+
{
59+
$user = User::factory()->viewAccessories()->create();
60+
61+
$accessoryA = Accessory::factory()->create(['name' => 'Accessory A']);
62+
$accessoryB = Accessory::factory()->create(['name' => 'Accessory B']);
63+
64+
$this->actingAsForApi($user)
65+
->getJson(route('api.accessories.index'))
66+
->assertOk()
67+
->assertResponseContainsInRows($accessoryA)
68+
->assertResponseContainsInRows($accessoryB);
69+
}
1670
}

tests/Feature/Accessories/Api/ShowAccessoryTest.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,59 @@
33
namespace Tests\Feature\Accessories\Api;
44

55
use App\Models\Accessory;
6+
use App\Models\Company;
67
use App\Models\User;
8+
use Tests\Concerns\TestsFullMultipleCompaniesSupport;
9+
use Tests\Concerns\TestsPermissionsRequirement;
710
use Tests\TestCase;
811

9-
class ShowAccessoryTest extends TestCase
12+
class ShowAccessoryTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement
1013
{
11-
public function testPermissionRequiredToShowAccessory()
14+
public function testRequiresPermission()
1215
{
1316
$accessory = Accessory::factory()->create();
1417

1518
$this->actingAsForApi(User::factory()->create())
1619
->getJson(route('api.accessories.show', $accessory))
1720
->assertForbidden();
1821
}
22+
23+
public function testAdheresToFullMultipleCompaniesSupportScoping()
24+
{
25+
[$companyA, $companyB] = Company::factory()->count(2)->create();
26+
27+
$accessoryForCompanyA = Accessory::factory()->for($companyA)->create();
28+
29+
$superuser = User::factory()->superuser()->create();
30+
$userForCompanyB = User::factory()->for($companyB)->viewAccessories()->create();
31+
32+
$this->settings->enableMultipleFullCompanySupport();
33+
34+
$this->actingAsForApi($userForCompanyB)
35+
->getJson(route('api.accessories.show', $accessoryForCompanyA))
36+
->assertOk()
37+
->assertStatusMessageIs('error');
38+
39+
$this->actingAsForApi($superuser)
40+
->getJson(route('api.accessories.show', $accessoryForCompanyA))
41+
->assertOk()
42+
->assertJsonFragment([
43+
'id' => $accessoryForCompanyA->id,
44+
]);
45+
}
46+
47+
public function testCanGetSingleAccessory()
48+
{
49+
$accessory = Accessory::factory()->checkedOutToUser()->create(['name' => 'My Accessory']);
50+
51+
$this->actingAsForApi(User::factory()->viewAccessories()->create())
52+
->getJson(route('api.accessories.show', $accessory))
53+
->assertOk()
54+
->assertJsonFragment([
55+
'id' => $accessory->id,
56+
'name' => 'My Accessory',
57+
'checkouts_count' => 1,
58+
]);
59+
60+
}
1961
}

0 commit comments

Comments
 (0)