Improve readability?

This commit is contained in:
Marcus Moore 2024-10-15 17:38:11 -07:00
parent d9afde4610
commit cba1a56040
No known key found for this signature in database

View file

@ -10,8 +10,10 @@ use App\Models\Location;
use App\Models\Statuslabel; use App\Models\Statuslabel;
use App\Models\Supplier; use App\Models\Supplier;
use App\Models\User; use App\Models\User;
use Generator;
use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Crypt;
use Illuminate\Testing\Fluent\AssertableJson; use Illuminate\Testing\Fluent\AssertableJson;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase; use Tests\TestCase;
class StoreAssetTest extends TestCase class StoreAssetTest extends TestCase
@ -23,51 +25,60 @@ class StoreAssetTest extends TestCase
->assertForbidden(); ->assertForbidden();
} }
public static function userProvider(): Generator
{
yield 'User in a company' => [
function () {
$jedi = Company::factory()->create();
$sith = Company::factory()->create();
$luke = User::factory()->for($jedi)->createAssets()->create();
return [
'actor' => $luke,
'company_attempting_to_associate' => $sith,
'assertions' => function ($asset) use ($jedi) {
self::assertEquals($jedi->id, $asset->company_id);
},
];
}
];
yield 'User without a company' => [
function () {
$userInNoCompany = User::factory()->createAssets()->create(['company_id' => null]);
return [
'actor' => $userInNoCompany,
'company_attempting_to_associate' => Company::factory()->create(),
'assertions' => function ($asset) {
self::assertNull($asset->company_id);
},
];
}
];
}
/** /**
* @link https://github.com/snipe/snipe-it/issues/15654 * @link https://github.com/snipe/snipe-it/issues/15654
*/ */
public function testAdheresToFullMultipleCompaniesSupportScoping() #[DataProvider('userProvider')]
public function testAdheresToFullMultipleCompaniesSupportScoping($data)
{ {
[$companyA, $companyB] = Company::factory()->count(2)->create(); ['actor' => $actor, 'company_attempting_to_associate' => $company, 'assertions' => $assertions] = $data();
$model = AssetModel::factory()->create();
$status = Statuslabel::factory()->readyToDeploy()->create();
$userInNoCompany = User::factory()
->createAssets()
->create(['company_id' => null]);
$userInCompanyA = User::factory()
->for($companyA)
->createAssets()
->create();
$this->assertNull($userInNoCompany->company_id);
$this->assertEquals($companyA->id, $userInCompanyA->company_id);
$this->settings->enableMultipleFullCompanySupport(); $this->settings->enableMultipleFullCompanySupport();
$responseForUserWithNoCompany = $this->actingAsForApi($userInNoCompany) $response = $this->actingAsForApi($actor)
->postJson(route('api.assets.store'), [ ->postJson(route('api.assets.store'), [
'asset_tag' => 'random_string', 'asset_tag' => 'random_string',
'company_id' => $companyB->id, 'company_id' => $company->id,
'model_id' => $model->id, 'model_id' => AssetModel::factory()->create()->id,
'status_id' => $status->id, 'status_id' => Statuslabel::factory()->readyToDeploy()->create()->id,
]); ]);
$responseForUserInCompanyA = $this->actingAsForApi($userInCompanyA) $asset = Asset::withoutGlobalScopes()->findOrFail($response['payload']['id']);
->postJson(route('api.assets.store'), [
'asset_tag' => 'another_string',
'company_id' => $companyB->id,
'model_id' => $model->id,
'status_id' => $status->id,
]);
$assetForUserWithNoCompany = Asset::withoutGlobalScopes()->find($responseForUserWithNoCompany['payload']['id']); $assertions($asset);
$assetForUserInCompanyA = Asset::withoutGlobalScopes()->find($responseForUserInCompanyA['payload']['id']);
// company_id should be the company_id of the user that performed the action
$this->assertNull($assetForUserWithNoCompany->company_id);
$this->assertEquals($userInCompanyA->company_id, $assetForUserInCompanyA->company_id);
} }
public function testAllAssetAttributesAreStored() public function testAllAssetAttributesAreStored()