mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-26 06:04:08 -08:00
Merge pull request #13431 from marcusmoore/feature/custom-report-scoping
Removed unneeded `Company::scopeCompanyables` from `ReportsController`
This commit is contained in:
commit
6adaa32286
|
@ -590,7 +590,7 @@ class ReportsController extends Controller
|
||||||
$executionTime = microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
|
$executionTime = microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
|
||||||
\Log::debug('Added headers: '.$executionTime);
|
\Log::debug('Added headers: '.$executionTime);
|
||||||
|
|
||||||
$assets = \App\Models\Company::scopeCompanyables(Asset::select('assets.*'))->with(
|
$assets = Asset::select('assets.*')->with(
|
||||||
'location', 'assetstatus', 'company', 'defaultLoc', 'assignedTo',
|
'location', 'assetstatus', 'company', 'defaultLoc', 'assignedTo',
|
||||||
'model.category', 'model.manufacturer', 'supplier');
|
'model.category', 'model.manufacturer', 'supplier');
|
||||||
|
|
||||||
|
|
|
@ -432,4 +432,13 @@ class UserFactory extends Factory
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function canViewReports()
|
||||||
|
{
|
||||||
|
return $this->state(function () {
|
||||||
|
return [
|
||||||
|
'permissions' => '{"reports.view":"1"}',
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
110
tests/Feature/Reports/CustomReportTest.php
Normal file
110
tests/Feature/Reports/CustomReportTest.php
Normal file
|
@ -0,0 +1,110 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Reports;
|
||||||
|
|
||||||
|
use App\Models\Asset;
|
||||||
|
use App\Models\Company;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Testing\TestResponse;
|
||||||
|
use League\Csv\Reader;
|
||||||
|
use PHPUnit\Framework\Assert;
|
||||||
|
use Tests\Support\InteractsWithSettings;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class CustomReportTest extends TestCase
|
||||||
|
{
|
||||||
|
use InteractsWithSettings;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
TestResponse::macro(
|
||||||
|
'assertSeeTextInStreamedResponse',
|
||||||
|
function (string $needle) {
|
||||||
|
Assert::assertTrue(
|
||||||
|
collect(Reader::createFromString($this->streamedContent())->getRecords())
|
||||||
|
->pluck(0)
|
||||||
|
->contains($needle)
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
TestResponse::macro(
|
||||||
|
'assertDontSeeTextInStreamedResponse',
|
||||||
|
function (string $needle) {
|
||||||
|
Assert::assertFalse(
|
||||||
|
collect(Reader::createFromString($this->streamedContent())->getRecords())
|
||||||
|
->pluck(0)
|
||||||
|
->contains($needle)
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCustomAssetReport()
|
||||||
|
{
|
||||||
|
Asset::factory()->create(['name' => 'Asset A']);
|
||||||
|
Asset::factory()->create(['name' => 'Asset B']);
|
||||||
|
|
||||||
|
$this->actingAs(User::factory()->canViewReports()->create())
|
||||||
|
->post('reports/custom', [
|
||||||
|
'asset_name' => '1',
|
||||||
|
'asset_tag' => '1',
|
||||||
|
'serial' => '1',
|
||||||
|
])->assertOk()
|
||||||
|
->assertHeader('content-type', 'text/csv; charset=UTF-8')
|
||||||
|
->assertSeeTextInStreamedResponse('Asset A')
|
||||||
|
->assertSeeTextInStreamedResponse('Asset B');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCustomAssetReportAdheresToCompanyScoping()
|
||||||
|
{
|
||||||
|
[$companyA, $companyB] = Company::factory()->count(2)->create();
|
||||||
|
|
||||||
|
Asset::factory()->for($companyA)->create(['name' => 'Asset A']);
|
||||||
|
Asset::factory()->for($companyB)->create(['name' => 'Asset B']);
|
||||||
|
|
||||||
|
$superUser = $companyA->users()->save(User::factory()->superuser()->make());
|
||||||
|
$userInCompanyA = $companyA->users()->save(User::factory()->canViewReports()->make());
|
||||||
|
$userInCompanyB = $companyB->users()->save(User::factory()->canViewReports()->make());
|
||||||
|
|
||||||
|
$this->settings->disableMultipleFullCompanySupport();
|
||||||
|
|
||||||
|
$this->actingAs($superUser)
|
||||||
|
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
|
||||||
|
->assertSeeTextInStreamedResponse('Asset A')
|
||||||
|
->assertSeeTextInStreamedResponse('Asset B');
|
||||||
|
|
||||||
|
$this->actingAs($userInCompanyA)
|
||||||
|
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
|
||||||
|
->assertSeeTextInStreamedResponse('Asset A')
|
||||||
|
->assertSeeTextInStreamedResponse('Asset B');
|
||||||
|
|
||||||
|
$this->actingAs($userInCompanyB)
|
||||||
|
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
|
||||||
|
->assertSeeTextInStreamedResponse('Asset A')
|
||||||
|
->assertSeeTextInStreamedResponse('Asset B');
|
||||||
|
|
||||||
|
$this->settings->enableMultipleFullCompanySupport();
|
||||||
|
|
||||||
|
$this->actingAs($superUser)
|
||||||
|
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
|
||||||
|
->assertSeeTextInStreamedResponse('Asset A')
|
||||||
|
->assertSeeTextInStreamedResponse('Asset B');
|
||||||
|
|
||||||
|
$this->actingAs($userInCompanyA)
|
||||||
|
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
|
||||||
|
->assertSeeTextInStreamedResponse('Asset A')
|
||||||
|
->assertDontSeeTextInStreamedResponse('Asset B');
|
||||||
|
|
||||||
|
$this->actingAs($userInCompanyB)
|
||||||
|
->post('reports/custom', ['asset_name' => '1', 'asset_tag' => '1', 'serial' => '1'])
|
||||||
|
->assertDontSeeTextInStreamedResponse('Asset A')
|
||||||
|
->assertSeeTextInStreamedResponse('Asset B');
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue