work on bulk tests, switching branches to check something
This commit is contained in:
@@ -11,7 +11,7 @@ use App\Models\Manufacturer;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class DestroyManufacturerAction
|
||||
class DeleteManufacturerAction
|
||||
{
|
||||
/**
|
||||
* @throws ModelStillHasAssets
|
||||
@@ -20,8 +20,9 @@ class DestroyManufacturerAction
|
||||
* @throws ModelStillHasLicenses
|
||||
* @throws ModelStillHasConsumables
|
||||
*/
|
||||
static function run(Manufacturer $manufacturer): bool
|
||||
static function run(?Manufacturer $manufacturer): bool
|
||||
{
|
||||
Manufacturer::firstOrFail($manufacturer->id);
|
||||
$manufacturer->loadCount([
|
||||
'assets as assets_count',
|
||||
'accessories as accessories_count',
|
||||
@@ -55,6 +56,7 @@ class DestroyManufacturerAction
|
||||
}
|
||||
|
||||
$manufacturer->delete();
|
||||
//dd($manufacturer);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Actions\Manufacturers\DestroyManufacturerAction;
|
||||
use App\Actions\Manufacturers\DeleteManufacturerAction;
|
||||
use App\Exceptions\ModelStillHasAccessories;
|
||||
use App\Exceptions\ModelStillHasAssets;
|
||||
use App\Exceptions\ModelStillHasChildren;
|
||||
@@ -195,7 +195,7 @@ class ManufacturersController extends Controller
|
||||
{
|
||||
$this->authorize('delete', $manufacturer);
|
||||
try {
|
||||
DestroyManufacturerAction::run($manufacturer);
|
||||
DeleteManufacturerAction::run($manufacturer);
|
||||
} catch (ModelStillHasChildren $e) {
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/manufacturers/message.assoc_users')));
|
||||
} catch (\Exception $e) {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\Categories\DestroyCategoryAction;
|
||||
use App\Actions\Manufacturers\DestroyManufacturerAction;
|
||||
use App\Actions\Manufacturers\DeleteManufacturerAction;
|
||||
use App\Exceptions\ModelStillHasAccessories;
|
||||
use App\Exceptions\ModelStillHasAssetMaintenances;
|
||||
use App\Exceptions\ModelStillHasAssetModels;
|
||||
@@ -11,16 +11,17 @@ use App\Exceptions\ModelStillHasAssets;
|
||||
use App\Exceptions\ModelStillHasComponents;
|
||||
use App\Exceptions\ModelStillHasConsumables;
|
||||
use App\Exceptions\ModelStillHasLicenses;
|
||||
use App\Models\Manufacturer;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class BulkManufacturersController extends Controller
|
||||
{
|
||||
public function destroy($ids)
|
||||
public function destroy(Request $request)
|
||||
{
|
||||
$errors = [];
|
||||
foreach ($ids as $id) {
|
||||
foreach ($request->ids as $id) {
|
||||
try {
|
||||
DestroyManufacturerAction::run(manufacturer: $id);
|
||||
DeleteManufacturerAction::run(manufacturer: $id);
|
||||
} catch (ModelStillHasAccessories|ModelStillHasAssets|ModelStillHasComponents|ModelStillHasConsumables|ModelStillHasLicenses $e) {
|
||||
$errors[] = `{$id} still has {$id->thing}`;
|
||||
} catch (\Exception $e) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\Manufacturers\DestroyManufacturerAction;
|
||||
use App\Actions\Manufacturers\DeleteManufacturerAction;
|
||||
use App\Exceptions\ModelStillHasAccessories;
|
||||
use App\Exceptions\ModelStillHasAssets;
|
||||
use App\Exceptions\ModelStillHasChildren;
|
||||
@@ -169,7 +169,7 @@ class ManufacturersController extends Controller
|
||||
{
|
||||
$this->authorize('delete', $manufacturer);
|
||||
try {
|
||||
DestroyManufacturerAction::run($manufacturer);
|
||||
DeleteManufacturerAction::run($manufacturer);
|
||||
} catch (ModelStillHasChildren $e) {
|
||||
return redirect()->route('manufacturers.index')->with('error', trans('admin/manufacturers/message.assoc_users'));
|
||||
} catch (\Exception $e) {
|
||||
@@ -178,11 +178,12 @@ class ManufacturersController extends Controller
|
||||
|
||||
// Soft delete the manufacturer if active, permanent delete if is already deleted
|
||||
// do we really want to do that?...
|
||||
if ($manufacturer->deleted_at === null) {
|
||||
$manufacturer->delete();
|
||||
} else {
|
||||
$manufacturer->forceDelete();
|
||||
}
|
||||
// commenting it out for now because it's weird
|
||||
//if ($manufacturer->deleted_at === null) {
|
||||
// $manufacturer->delete();
|
||||
//} else {
|
||||
// $manufacturer->forceDelete();
|
||||
//}
|
||||
// Redirect to the manufacturers management page
|
||||
return redirect()->route('manufacturers.index')->with('success', trans('admin/manufacturers/message.delete.success'));
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Tests\Feature\Manufacturers\Ui;
|
||||
|
||||
use App\Models\User;
|
||||
use Tests\Concerns\TestsPermissionsRequirement;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -9,7 +10,11 @@ class BulkDeleteManufacturersTest extends TestCase implements TestsPermissionsRe
|
||||
{
|
||||
public function testRequiresPermission()
|
||||
{
|
||||
// TODO: Implement testRequiresPermission() method.
|
||||
$this->actingAs(User::factory()->create())
|
||||
->delete(route('manufacturers.bulk.delete'), [
|
||||
'ids' => [1, 2, 3]
|
||||
])
|
||||
->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_manufacturer_cannot_be_bulk_deleted_if_models_still_associated()
|
||||
|
||||
@@ -2,6 +2,10 @@
|
||||
|
||||
namespace Tests\Feature\Manufacturers\Ui;
|
||||
|
||||
use App\Models\Accessory;
|
||||
use App\Models\Category;
|
||||
use App\Models\Manufacturer;
|
||||
use App\Models\User;
|
||||
use Tests\Concerns\TestsPermissionsRequirement;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -9,16 +13,32 @@ class DeleteManufacturersTest extends TestCase implements TestsPermissionsRequir
|
||||
{
|
||||
public function testRequiresPermission()
|
||||
{
|
||||
// TODO: Implement testRequiresPermission() method.
|
||||
$this->actingAs(User::factory()->create())
|
||||
->delete(route('categories.destroy', Category::factory()->create()))
|
||||
->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_manufacturer_cannot_be_deleted_if_models_still_associated()
|
||||
{
|
||||
// TODO: implement
|
||||
$manufacturer = Manufacturer::factory()->create();
|
||||
Accessory::factory()->for($manufacturer)->create();
|
||||
|
||||
$this->actingAs(User::factory()->deleteManufacturers()->create())
|
||||
->delete(route('manufacturers.destroy', $manufacturer));
|
||||
|
||||
$this->assertNotSoftDeleted($manufacturer);
|
||||
}
|
||||
|
||||
public function test_manufacturer_can_be_deleted()
|
||||
{
|
||||
// TODO: implement
|
||||
$manufacturer = Manufacturer::factory()->create();
|
||||
|
||||
$this->assertDatabaseHas('manufacturers', ['id' => $manufacturer->id]);
|
||||
|
||||
$this->actingAs(User::factory()->deleteManufacturers()->create())
|
||||
->delete(route('manufacturers.destroy', $manufacturer))
|
||||
->assertRedirect(route('manufacturers.index'));
|
||||
|
||||
$this->assertSoftDeleted($manufacturer);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user