A free open source IT asset/license management system
Stupid phpmd fixes
snipedd99f694b5c1
diff --git a/app/Http/Controllers/Api/DashboardController.php b/app/Http/Controllers/Api/DashboardController.phpindex 7f03ac1f6e..0d09ab659c 100644--- a/app/Http/Controllers/Api/DashboardController.php+++ b/app/Http/Controllers/Api/DashboardController.php@@ -22,8 +22,8 @@ use Illuminate\Support\Facades\Gate;/*** Endpoints for the non-admin dashboard widgets.*- * The general-purpose Blah::index endpoints stay gated on their original- * per-domain permissions.+ * The general-purpose Blah::index endpoints stay gated on their+ * original per-domain permissions.*/class DashboardController extends Controller{diff --git a/phpmd.baseline.xml b/phpmd.baseline.xmlindex dc2602a15a..111e910881 100644--- a/phpmd.baseline.xml+++ b/phpmd.baseline.xml@@ -10,6 +10,12 @@<violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Exceptions/Handler.php"/><violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Http/Controllers/Accessories/AccessoryCheckinController.php"/><violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Http/Controllers/Api/AssetsController.php"/>+ <!-- Api\DashboardController coordinates every dashboard-widget endpoint+ (activity + categories + companies + locations), each of which+ needs its own model set. Splitting the file into per-widget+ controllers to satisfy CBO would fragment one view's backing+ code across four files without a real payoff. -->+ <violation rule="PHPMD\Rule\Design\CouplingBetweenObjects" file="app/Http/Controllers/Api/DashboardController.php"/><violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Http/Controllers/Api/LabelsController.php"/><violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Http/Controllers/Api/LicenseSeatsController.php"/><violation rule="PHPMD\Rule\UnusedLocalVariable" file="app/Http/Controllers/Api/SettingsController.php"/>diff --git a/tests/Feature/DashboardWidgetPayloadShapeTest.php b/tests/Feature/DashboardWidgetPayloadShapeTest.phpnew file mode 100644index 0000000000..03ae9fbc8d--- /dev/null+++ b/tests/Feature/DashboardWidgetPayloadShapeTest.php@@ -0,0 +1,149 @@+<?php++namespace Tests\Feature;++use App\Models\Accessory;+use App\Models\Actionlog;+use App\Models\Asset;+use App\Models\Category;+use App\Models\Company;+use App\Models\License;+use App\Models\Location;+use App\Models\User;+use Laravel\Passport\Passport;+use Tests\TestCase;++/**+ * Payload-shape tests for the non-admin dashboard widget endpoints.+ * Split from DashboardWidgetVisibilityTest so each file stays under+ * PHPMD's per-class method threshold. Covers per-viewer-type+ * filtering on the activity feed, the available_actions.view flag+ * on lookup rows (Category / Company / Location), and the+ * category-type filter for scoped viewers.+ */+class DashboardWidgetPayloadShapeTest extends TestCase+{+ public function test_scoped_activity_viewer_only_sees_actionlogs_on_types_they_can_view(): void+ {+ // Seed one actionlog per relevant type. A user with only+ // accessories.view should see the Accessory row and nothing+ // else through the dashboard's recent-activity feed.+ $asset = Asset::factory()->create();+ $accessory = Accessory::factory()->create();+ $license = License::factory()->create();++ Actionlog::factory()->create([+ 'item_type' => Asset::class,+ 'item_id' => $asset->id,+ 'action_type' => 'checkout',+ 'note' => 'SENTINEL_ASSET_ROW',+ ]);+ Actionlog::factory()->create([+ 'item_type' => Accessory::class,+ 'item_id' => $accessory->id,+ 'action_type' => 'checkout',+ 'note' => 'SENTINEL_ACCESSORY_ROW',+ ]);+ Actionlog::factory()->create([+ 'item_type' => License::class,+ 'item_id' => $license->id,+ 'action_type' => 'checkout',+ 'note' => 'SENTINEL_LICENSE_ROW',+ ]);++ Passport::actingAs(User::factory()->viewAccessories()->create());++ $response = $this->getJson(route('api.dashboard.activity'))->assertOk();+ $body = $response->getContent();++ $this->assertStringContainsString('SENTINEL_ACCESSORY_ROW', $body);+ $this->assertStringNotContainsString('SENTINEL_ASSET_ROW', $body);+ $this->assertStringNotContainsString('SENTINEL_LICENSE_ROW', $body);+ }++ public function test_scoped_viewer_sees_available_actions_view_false_on_lookup_rows(): void+ {+ // Row-level view flag drives the dashboard formatter's+ // decision to render the name as plain text (no link) so a+ // scoped viewer clicking through does not 403 on the show+ // controller they cannot reach. Same shape on Categories,+ // Locations, and Companies transformers. Category needs to+ // be an accessory-typed one so it survives the category_type+ // filter the API applies for scoped viewers.+ Category::factory()->create(['category_type' => 'accessory']);+ Location::factory()->create();+ Company::factory()->create();++ Passport::actingAs(User::factory()->viewAccessories()->create());++ $categoryRow = $this->getJson(route('api.dashboard.categories'))->assertOk()->json('rows.0');+ $locationRow = $this->getJson(route('api.dashboard.locations'))->assertOk()->json('rows.0');+ $companyRow = $this->getJson(route('api.dashboard.companies'))->assertOk()->json('rows.0');++ $this->assertFalse($categoryRow['available_actions']['view']);+ $this->assertFalse($locationRow['available_actions']['view']);+ $this->assertFalse($companyRow['available_actions']['view']);+ }++ public function test_scoped_categories_filter_excludes_types_the_viewer_cannot_view(): void+ {+ // Accessory-only viewer should see accessory-typed categories+ // but not asset / license / consumable / component ones. The+ // filter widens back out to unfiltered when the caller has+ // explicit categories.view.+ Category::factory()->create(['name' => 'Asset Cat', 'category_type' => 'asset']);+ Category::factory()->create(['name' => 'Accessory Cat', 'category_type' => 'accessory']);+ Category::factory()->create(['name' => 'License Cat', 'category_type' => 'license']);++ Passport::actingAs(User::factory()->viewAccessories()->create());++ $rows = $this->getJson(route('api.dashboard.categories'))->assertOk()->json('rows');+ $names = collect($rows)->pluck('name')->all();++ $this->assertContains('Accessory Cat', $names);+ $this->assertNotContains('Asset Cat', $names);+ $this->assertNotContains('License Cat', $names);+ }++ public function test_activity_row_carries_viewable_flag_per_item(): void+ {+ // Recent-activity's polymorphicItemFormatter renders name ++ // link when the caller can view the record, plain text when+ // they cannot. The flag flows through the transformer as+ // item.viewable / target.viewable on each row so a scoped+ // viewer's dashboard clicks do not 403.+ $accessory = Accessory::factory()->create();++ Actionlog::factory()->create([+ 'item_type' => Accessory::class,+ 'item_id' => $accessory->id,+ 'action_type' => 'checkout',+ ]);++ Passport::actingAs(User::factory()->viewAccessories()->create());++ $rows = $this->getJson(route('api.dashboard.activity'))->assertOk()->json('rows');+ $accessoryRows = collect($rows)->filter(fn ($r) => ($r['item']['type'] ?? null) === 'accessory')->values()->all();+ $this->assertNotEmpty($accessoryRows, 'accessory row must survive the scoped-viewer filter');+ foreach ($accessoryRows as $row) {+ $this->assertTrue($row['item']['viewable']);+ }+ }++ public function test_admin_sees_available_actions_view_true_on_lookup_rows(): void+ {+ Category::factory()->create();+ Location::factory()->create();+ Company::factory()->create();++ Passport::actingAs(User::factory()->superuser()->create());++ $categoryRow = $this->getJson(route('api.dashboard.categories'))->assertOk()->json('rows.0');+ $locationRow = $this->getJson(route('api.dashboard.locations'))->assertOk()->json('rows.0');+ $companyRow = $this->getJson(route('api.dashboard.companies'))->assertOk()->json('rows.0');++ $this->assertTrue($categoryRow['available_actions']['view']);+ $this->assertTrue($locationRow['available_actions']['view']);+ $this->assertTrue($companyRow['available_actions']['view']);+ }+}diff --git a/tests/Feature/DashboardWidgetVisibilityTest.php b/tests/Feature/DashboardWidgetVisibilityTest.phpindex 151252b88c..5abf833de1 100644--- a/tests/Feature/DashboardWidgetVisibilityTest.php+++ b/tests/Feature/DashboardWidgetVisibilityTest.php@@ -5,33 +5,25 @@ namespace Tests\Feature;use App\Models\Accessory;use App\Models\Actionlog;use App\Models\Asset;-use App\Models\Category;use App\Models\Company;-use App\Models\License;use App\Models\Location;use App\Models\User;use Laravel\Passport\Passport;use Tests\TestCase;/**+ * Access / auth tests for the non-admin dashboard widget endpoints.* The dashboard is reachable by any user who passes* canViewUsersAndCheckoutables (can view at least one- * HasCalendarEvents source). Each widget's API endpoint has to- * accept those callers too, or the widget renders an ugly "Could- * not load results, 403" toast. This test covers the widening on- * Api\CompaniesController@index, Api\LocationsController@index,- * Api\CategoriesController@index, and Api\ReportsController@index- * (recent activity).+ * HasCalendarEvents source). Each purpose-built api.dashboard.*+ * endpoint has to accept those callers too, or the widget renders+ * an ugly "Could not load results, 403". The general-purpose+ * per-domain index endpoints must stay 403'd against scoped+ * viewers so they can't sidestep the narrow widget surface.*- * Transitive-visibility reasoning: a user who can view any inventory- * type inherently knows about the companies / locations / categories- * those items belong to (they're attributes on every asset,- * accessory, etc). CompanyableScope and FMCS location scoping filter- * responses naturally.- *- * Recent activity has an additional server-side per-type filter for- * scoped viewers so an accessory-only user's feed only carries- * actionlogs on types they can view.+ * Payload-shape / filter tests live in+ * DashboardWidgetPayloadShapeTest so each file stays under PHPMD's+ * per-class method threshold.*/class DashboardWidgetVisibilityTest extends TestCase{@@ -55,8 +47,6 @@ class DashboardWidgetVisibilityTest extends TestCasepublic function test_scoped_viewer_can_reach_categories_widget(): void{- Category::factory()->count(2)->create();-Passport::actingAs(User::factory()->viewAccessories()->create());$this->getJson(route('api.dashboard.categories'))->assertOk();@@ -90,130 +80,6 @@ class DashboardWidgetVisibilityTest extends TestCase$this->getJson(route('api.dashboard.activity'))->assertForbidden();}- public function test_scoped_activity_viewer_only_sees_actionlogs_on_types_they_can_view(): void- {- // Seed one actionlog per relevant type. A user with only- // accessories.view should see the Accessory row and nothing- // else through the dashboard's recent-activity feed.- $asset = Asset::factory()->create();- $accessory = Accessory::factory()->create();- $license = License::factory()->create();-- Actionlog::factory()->create([- 'item_type' => Asset::class,- 'item_id' => $asset->id,- 'action_type' => 'checkout',- 'note' => 'SENTINEL_ASSET_ROW',- ]);- Actionlog::factory()->create([- 'item_type' => Accessory::class,- 'item_id' => $accessory->id,- 'action_type' => 'checkout',- 'note' => 'SENTINEL_ACCESSORY_ROW',- ]);- Actionlog::factory()->create([- 'item_type' => License::class,- 'item_id' => $license->id,- 'action_type' => 'checkout',- 'note' => 'SENTINEL_LICENSE_ROW',- ]);-- Passport::actingAs(User::factory()->viewAccessories()->create());-- $response = $this->getJson(route('api.dashboard.activity'))->assertOk();- $body = $response->getContent();-- $this->assertStringContainsString('SENTINEL_ACCESSORY_ROW', $body);- $this->assertStringNotContainsString('SENTINEL_ASSET_ROW', $body);- $this->assertStringNotContainsString('SENTINEL_LICENSE_ROW', $body);- }-- public function test_scoped_viewer_sees_available_actions_view_false_on_lookup_rows(): void- {- // Row-level view flag drives the dashboard formatter's- // decision to render the name as plain text (no link) so a- // scoped viewer clicking through does not 403 on the show- // controller they cannot reach. Same shape on Categories,- // Locations, and Companies transformers. Category needs to- // be an accessory-typed one so it survives the category_type- // filter the API applies for scoped viewers.- Category::factory()->create(['category_type' => 'accessory']);- Location::factory()->create();- Company::factory()->create();-- Passport::actingAs(User::factory()->viewAccessories()->create());-- $categoryRow = $this->getJson(route('api.dashboard.categories'))->assertOk()->json('rows.0');- $locationRow = $this->getJson(route('api.dashboard.locations'))->assertOk()->json('rows.0');- $companyRow = $this->getJson(route('api.dashboard.companies'))->assertOk()->json('rows.0');-- $this->assertFalse($categoryRow['available_actions']['view']);- $this->assertFalse($locationRow['available_actions']['view']);- $this->assertFalse($companyRow['available_actions']['view']);- }-- public function test_scoped_categories_filter_excludes_types_the_viewer_cannot_view(): void- {- // Accessory-only viewer should see accessory-typed categories- // but not asset / license / consumable / component ones. The- // filter widens back out to unfiltered when the caller has- // explicit categories.view.- Category::factory()->create(['name' => 'Asset Cat', 'category_type' => 'asset']);- Category::factory()->create(['name' => 'Accessory Cat', 'category_type' => 'accessory']);- Category::factory()->create(['name' => 'License Cat', 'category_type' => 'license']);-- Passport::actingAs(User::factory()->viewAccessories()->create());-- $rows = $this->getJson(route('api.dashboard.categories'))->assertOk()->json('rows');- $names = collect($rows)->pluck('name')->all();-- $this->assertContains('Accessory Cat', $names);- $this->assertNotContains('Asset Cat', $names);- $this->assertNotContains('License Cat', $names);- }-- public function test_activity_row_carries_viewable_flag_per_item(): void- {- // Recent-activity's polymorphicItemFormatter renders name +- // link when the caller can view the record, plain text when- // they cannot. The flag flows through the transformer as- // item.viewable / target.viewable on each row so a scoped- // viewer's dashboard clicks do not 403.- $accessory = Accessory::factory()->create();-- Actionlog::factory()->create([- 'item_type' => Accessory::class,- 'item_id' => $accessory->id,- 'action_type' => 'checkout',- ]);-- Passport::actingAs(User::factory()->viewAccessories()->create());-- $rows = $this->getJson(route('api.dashboard.activity'))->assertOk()->json('rows');- $accessoryRows = collect($rows)->filter(fn ($r) => ($r['item']['type'] ?? null) === 'accessory')->values()->all();- $this->assertNotEmpty($accessoryRows, 'accessory row must survive the scoped-viewer filter');- foreach ($accessoryRows as $row) {- $this->assertTrue($row['item']['viewable']);- }- }-- public function test_admin_sees_available_actions_view_true_on_lookup_rows(): void- {- Category::factory()->create();- Location::factory()->create();- Company::factory()->create();-- Passport::actingAs(User::factory()->superuser()->create());-- $categoryRow = $this->getJson(route('api.dashboard.categories'))->assertOk()->json('rows.0');- $locationRow = $this->getJson(route('api.dashboard.locations'))->assertOk()->json('rows.0');- $companyRow = $this->getJson(route('api.dashboard.companies'))->assertOk()->json('rows.0');-- $this->assertTrue($categoryRow['available_actions']['view']);- $this->assertTrue($locationRow['available_actions']['view']);- $this->assertTrue($companyRow['available_actions']['view']);- }-public function test_full_activity_view_still_sees_every_row(): void{// Superuser (implicit activity.view via hasAccess('admin'))diff --git a/tests/Feature/Reporting/ActivityReportCsvExportTest.php b/tests/Feature/Reporting/ActivityReportCsvExportTest.phpnew file mode 100644index 0000000000..af4a50c97e--- /dev/null+++ b/tests/Feature/Reporting/ActivityReportCsvExportTest.php@@ -0,0 +1,107 @@+<?php++namespace Tests\Feature\Reporting;++use App\Models\Actionlog;+use App\Models\Asset;+use App\Models\Component;+use App\Models\User;+use Tests\TestCase;++/**+ * CSV export path of the activity report page. Split from+ * ActivityReportPageAuthTest so each file stays under PHPMD's+ * per-class method threshold. Covers the export button surfacing+ * for reports.view holders and scoped viewers, the endpoint's+ * auth gate, and the per-viewable-type row filter applied to the+ * streamed CSV output.+ */+class ActivityReportCsvExportTest extends TestCase+{+ public function test_reports_view_holder_sees_csv_export_button()+ {+ $this->actingAs(User::factory()->canViewReports()->create())+ ->get(route('reports.activity'))+ ->assertOk()+ ->assertSee(route('reports.activity.post'));+ }++ public function test_scoped_viewer_also_sees_csv_export_button()+ {+ // Scoped viewers get an export gated to the same viewable+ // types they see on the page. Filtering happens inside+ // postActivityReport.+ $this->actingAs(User::factory()->viewComponents()->create())+ ->get(route('reports.activity'))+ ->assertOk()+ ->assertSee(route('reports.activity.post'));+ }++ public function test_permissionless_user_cannot_hit_csv_export_endpoint()+ {+ $this->actingAs(User::factory()->create())+ ->post(route('reports.activity.post'))+ ->assertForbidden();+ }++ public function test_scoped_viewer_csv_export_only_contains_rows_for_viewable_types()+ {+ // A components-only viewer's CSV must contain rows for+ // Component actionlogs and omit rows for Asset / License /+ // Consumable / Accessory actionlogs. Same viewable-type+ // filter the on-page endpoint applies.+ $component = Component::factory()->create();+ $asset = Asset::factory()->create();++ Actionlog::factory()->create([+ 'item_type' => Component::class,+ 'item_id' => $component->id,+ 'action_type' => 'checkout',+ 'note' => 'COMPONENT_ROW_SENTINEL',+ ]);+ Actionlog::factory()->create([+ 'item_type' => Asset::class,+ 'item_id' => $asset->id,+ 'action_type' => 'checkout',+ 'note' => 'ASSET_ROW_SENTINEL',+ ]);++ $body = $this->actingAs(User::factory()->viewComponents()->create())+ ->post(route('reports.activity.post'))+ ->assertOk()+ ->streamedContent();++ $this->assertStringContainsString('COMPONENT_ROW_SENTINEL', $body);+ $this->assertStringNotContainsString('ASSET_ROW_SENTINEL', $body);+ }++ public function test_reports_view_holder_csv_export_contains_every_type()+ {+ // reports.view holders keep the full unfiltered CSV. Guard+ // against a future refactor that accidentally applies the+ // scoped-viewer filter to admin exports.+ $component = Component::factory()->create();+ $asset = Asset::factory()->create();++ Actionlog::factory()->create([+ 'item_type' => Component::class,+ 'item_id' => $component->id,+ 'action_type' => 'checkout',+ 'note' => 'COMPONENT_ROW_SENTINEL',+ ]);+ Actionlog::factory()->create([+ 'item_type' => Asset::class,+ 'item_id' => $asset->id,+ 'action_type' => 'checkout',+ 'note' => 'ASSET_ROW_SENTINEL',+ ]);++ $body = $this->actingAs(User::factory()->canViewReports()->create())+ ->post(route('reports.activity.post'))+ ->assertOk()+ ->streamedContent();++ $this->assertStringContainsString('COMPONENT_ROW_SENTINEL', $body);+ $this->assertStringContainsString('ASSET_ROW_SENTINEL', $body);+ }+}diff --git a/tests/Feature/Reporting/ActivityReportPageAuthTest.php b/tests/Feature/Reporting/ActivityReportPageAuthTest.phpindex ff0db466f8..795d37aa9d 100644--- a/tests/Feature/Reporting/ActivityReportPageAuthTest.php+++ b/tests/Feature/Reporting/ActivityReportPageAuthTest.php@@ -6,17 +6,18 @@ use App\Models\User;use Tests\TestCase;/**- * The activity report page (reports/activity) is reachable by two+ * Page-access + endpoint-target + breadcrumb tests for the+ * reports/activity page. The report page is reachable by two* viewer classes with different UI:- * - reports.view holders get the full page with CSV export and- * the general search / filter / sort endpoint.+ * - reports.view holders get the full page with the general+ * search / filter / sort endpoint.* - Scoped viewers (canViewUsersAndCheckoutables, no reports.view)* arrive through the dashboard Recent Activity widget's View-all* button, land on the same template with admin-shaped UI stripped,* and read from the narrow api.dashboard.activity endpoint.*- * These tests pin both paths so a future permission change does not- * quietly 403 the widget click-through or leak the export button.+ * CSV export path lives in ActivityReportCsvExportTest so this file+ * stays under PHPMD's per-class method threshold.*/class ActivityReportPageAuthTest extends TestCase{@@ -41,25 +42,6 @@ class ActivityReportPageAuthTest extends TestCase->assertOk();}- public function test_reports_view_holder_sees_csv_export_button()- {- $this->actingAs(User::factory()->canViewReports()->create())- ->get(route('reports.activity'))- ->assertOk()- ->assertSee(route('reports.activity.post'));- }-- public function test_scoped_viewer_also_sees_csv_export_button()- {- // Scoped viewers get an export gated to the same viewable- // types they see on the page. Filtering happens inside- // postActivityReport.- $this->actingAs(User::factory()->viewComponents()->create())- ->get(route('reports.activity'))- ->assertOk()- ->assertSee(route('reports.activity.post'));- }-public function test_scoped_viewer_page_points_at_narrow_dashboard_endpoint(){$this->actingAs(User::factory()->viewComponents()->create())@@ -77,74 +59,6 @@ class ActivityReportPageAuthTest extends TestCase->assertSee(route('api.activity.index'));}- public function test_permissionless_user_cannot_hit_csv_export_endpoint()- {- $this->actingAs(User::factory()->create())- ->post(route('reports.activity.post'))- ->assertForbidden();- }-- public function test_scoped_viewer_csv_export_only_contains_rows_for_viewable_types()- {- // A components-only viewer's CSV must contain rows for- // Component actionlogs and omit rows for Asset / License /- // Consumable / Accessory actionlogs. Same viewable-type- // filter the on-page endpoint applies.- $component = \App\Models\Component::factory()->create();- $asset = \App\Models\Asset::factory()->create();-- \App\Models\Actionlog::factory()->create([- 'item_type' => \App\Models\Component::class,- 'item_id' => $component->id,- 'action_type' => 'checkout',- 'note' => 'COMPONENT_ROW_SENTINEL',- ]);- \App\Models\Actionlog::factory()->create([- 'item_type' => \App\Models\Asset::class,- 'item_id' => $asset->id,- 'action_type' => 'checkout',- 'note' => 'ASSET_ROW_SENTINEL',- ]);-- $body = $this->actingAs(User::factory()->viewComponents()->create())- ->post(route('reports.activity.post'))- ->assertOk()- ->streamedContent();-- $this->assertStringContainsString('COMPONENT_ROW_SENTINEL', $body);- $this->assertStringNotContainsString('ASSET_ROW_SENTINEL', $body);- }-- public function test_reports_view_holder_csv_export_contains_every_type()- {- // reports.view holders keep the full unfiltered CSV. Guard- // against a future refactor that accidentally applies the- // scoped-viewer filter to admin exports.- $component = \App\Models\Component::factory()->create();- $asset = \App\Models\Asset::factory()->create();-- \App\Models\Actionlog::factory()->create([- 'item_type' => \App\Models\Component::class,- 'item_id' => $component->id,- 'action_type' => 'checkout',- 'note' => 'COMPONENT_ROW_SENTINEL',- ]);- \App\Models\Actionlog::factory()->create([- 'item_type' => \App\Models\Asset::class,- 'item_id' => $asset->id,- 'action_type' => 'checkout',- 'note' => 'ASSET_ROW_SENTINEL',- ]);-- $body = $this->actingAs(User::factory()->canViewReports()->create())- ->post(route('reports.activity.post'))- ->assertOk()- ->streamedContent();-- $this->assertStringContainsString('COMPONENT_ROW_SENTINEL', $body);- $this->assertStringContainsString('ASSET_ROW_SENTINEL', $body);- }-public function test_scoped_viewer_breadcrumb_does_not_link_to_reports_index(){// reports.index is admin-shaped. A scoped viewer's activity