diff --git a/tests/Feature/Accessories/Ui/CreateAccessoryWithFullMultipleCompanySupportTest.php b/tests/Feature/Accessories/Ui/CreateAccessoryWithFullMultipleCompanySupportTest.php new file mode 100644 index 0000000000..02b01c6665 --- /dev/null +++ b/tests/Feature/Accessories/Ui/CreateAccessoryWithFullMultipleCompanySupportTest.php @@ -0,0 +1,87 @@ + [ + function () { + $jedi = Company::factory()->create(); + $sith = Company::factory()->create(); + $luke = User::factory()->for($jedi)->createAccessories()->create(); + + return [ + 'actor' => $luke, + 'company_attempting_to_associate' => $sith, + 'assertions' => function ($accessory) use ($jedi) { + self::assertEquals($jedi->id, $accessory->company_id); + }, + ]; + } + ]; + + yield "User without a company should result in accessory's company_id being null" => [ + function () { + $userInNoCompany = User::factory()->createAccessories()->create(['company_id' => null]); + + return [ + 'actor' => $userInNoCompany, + 'company_attempting_to_associate' => Company::factory()->create(), + 'assertions' => function ($accessory) { + self::assertNull($accessory->company_id); + }, + ]; + } + ]; + + yield "Super-User assigning across companies should result in accessory's company_id being set to what was provided" => [ + function () { + $superUser = User::factory()->superuser()->create(['company_id' => null]); + $company = Company::factory()->create(); + + return [ + 'actor' => $superUser, + 'company_attempting_to_associate' => $company, + 'assertions' => function ($accessory) use ($company) { + self::assertEquals($accessory->company_id, $company->id); + }, + ]; + } + ]; + } + + #[Group('focus')] + #[DataProvider('userProvider')] + public function testAdheresToFullMultipleCompaniesSupportScoping($data) + { + ['actor' => $actor, 'company_attempting_to_associate' => $company, 'assertions' => $assertions] = $data(); + + $this->settings->enableMultipleFullCompanySupport(); + + $this->actingAs($actor) + ->post(route('accessories.store'), [ + 'redirect_option' => 'index', + 'name' => 'My Cool Accessory', + 'qty' => '1', + 'category_id' => Category::factory()->create()->id, + 'company_id' => $company->id, + ]); + + $accessory = Accessory::withoutGlobalScopes()->where([ + 'name' => 'My Cool Accessory', + ])->sole(); + + $assertions($accessory); + } +} diff --git a/tests/Feature/Assets/Api/StoreAssetTest.php b/tests/Feature/Assets/Api/StoreAssetTest.php index 683a274c35..26fb1f78c0 100644 --- a/tests/Feature/Assets/Api/StoreAssetTest.php +++ b/tests/Feature/Assets/Api/StoreAssetTest.php @@ -10,10 +10,9 @@ use App\Models\Location; use App\Models\Statuslabel; use App\Models\Supplier; use App\Models\User; -use Generator; use Illuminate\Support\Facades\Crypt; use Illuminate\Testing\Fluent\AssertableJson; -use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; use Tests\TestCase; class StoreAssetTest extends TestCase @@ -25,77 +24,6 @@ class StoreAssetTest extends TestCase ->assertForbidden(); } - public static function userProvider(): Generator - { - yield "User in a company should result in user's company_id being used" => [ - 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 should result in asset's company_id being null" => [ - 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); - }, - ]; - } - ]; - - yield "Super-User assigning across companies should result in asset's company_id being set to what was provided" => [ - function () { - $superUser = User::factory()->superuser()->create(['company_id' => null]); - $company = Company::factory()->create(); - - return [ - 'actor' => $superUser, - 'company_attempting_to_associate' => $company, - 'assertions' => function ($asset) use ($company) { - self::assertEquals($asset->company_id, $company->id); - }, - ]; - } - ]; - } - - /** - * @link https://github.com/snipe/snipe-it/issues/15654 - */ - #[DataProvider('userProvider')] - public function testAdheresToFullMultipleCompaniesSupportScoping($data) - { - ['actor' => $actor, 'company_attempting_to_associate' => $company, 'assertions' => $assertions] = $data(); - - $this->settings->enableMultipleFullCompanySupport(); - - $response = $this->actingAsForApi($actor) - ->postJson(route('api.assets.store'), [ - 'asset_tag' => 'random_string', - 'company_id' => $company->id, - 'model_id' => AssetModel::factory()->create()->id, - 'status_id' => Statuslabel::factory()->readyToDeploy()->create()->id, - ]); - - $asset = Asset::withoutGlobalScopes()->findOrFail($response['payload']['id']); - - $assertions($asset); - } - public function testAllAssetAttributesAreStored() { $company = Company::factory()->create(); @@ -636,6 +564,7 @@ class StoreAssetTest extends TestCase /** * @link https://app.shortcut.com/grokability/story/24475 */ + #[Group('focus')] public function testCompanyIdNeedsToBeInteger() { $this->actingAsForApi(User::factory()->createAssets()->create()) diff --git a/tests/Feature/Assets/Api/StoreAssetWithFullMultipleCompanySupportTest.php b/tests/Feature/Assets/Api/StoreAssetWithFullMultipleCompanySupportTest.php new file mode 100644 index 0000000000..e3c4e6fe87 --- /dev/null +++ b/tests/Feature/Assets/Api/StoreAssetWithFullMultipleCompanySupportTest.php @@ -0,0 +1,88 @@ + [ + 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 should result in asset's company_id being null" => [ + 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); + }, + ]; + } + ]; + + yield "Super-User assigning across companies should result in asset's company_id being set to what was provided" => [ + function () { + $superUser = User::factory()->superuser()->create(['company_id' => null]); + $company = Company::factory()->create(); + + return [ + 'actor' => $superUser, + 'company_attempting_to_associate' => $company, + 'assertions' => function ($asset) use ($company) { + self::assertEquals($asset->company_id, $company->id); + }, + ]; + } + ]; + } + + /** + * @link https://github.com/snipe/snipe-it/issues/15654 + */ + #[Group('focus')] + #[DataProvider('userProvider')] + public function testAdheresToFullMultipleCompaniesSupportScoping($data) + { + ['actor' => $actor, 'company_attempting_to_associate' => $company, 'assertions' => $assertions] = $data(); + + $this->settings->enableMultipleFullCompanySupport(); + + $response = $this->actingAsForApi($actor) + ->postJson(route('api.assets.store'), [ + 'asset_tag' => 'random_string', + 'company_id' => $company->id, + 'model_id' => AssetModel::factory()->create()->id, + 'status_id' => Statuslabel::factory()->readyToDeploy()->create()->id, + ]); + + $asset = Asset::withoutGlobalScopes()->findOrFail($response['payload']['id']); + + $assertions($asset); + } +} diff --git a/tests/Feature/Assets/Ui/StoreAssetWithFullMultipleCompanySupportTest.php b/tests/Feature/Assets/Ui/StoreAssetWithFullMultipleCompanySupportTest.php new file mode 100644 index 0000000000..6c15e4ada3 --- /dev/null +++ b/tests/Feature/Assets/Ui/StoreAssetWithFullMultipleCompanySupportTest.php @@ -0,0 +1,85 @@ + [ + 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 should result in asset's company_id being null" => [ + 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); + }, + ]; + } + ]; + + yield "Super-User assigning across companies should result in asset's company_id being set to what was provided" => [ + function () { + $superUser = User::factory()->superuser()->create(['company_id' => null]); + $company = Company::factory()->create(); + + return [ + 'actor' => $superUser, + 'company_attempting_to_associate' => $company, + 'assertions' => function ($asset) use ($company) { + self::assertEquals($asset->company_id, $company->id); + }, + ]; + } + ]; + } + + #[Group('focus')] + #[DataProvider('userProvider')] + public function testAdheresToFullMultipleCompaniesSupportScoping($data) + { + ['actor' => $actor, 'company_attempting_to_associate' => $company, 'assertions' => $assertions] = $data(); + + $this->settings->enableMultipleFullCompanySupport(); + + $this->actingAs($actor) + ->post(route('hardware.store'), [ + 'asset_tags' => ['1' => '1234'], + 'model_id' => AssetModel::factory()->create()->id, + 'status_id' => Statuslabel::factory()->create()->id, + 'company_id' => $company->id, + ]); + + $asset = Asset::where('asset_tag', '1234')->sole(); + + $assertions($asset); + } +}