2023-08-08 17:10:36 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Reports;
|
|
|
|
|
|
|
|
use App\Models\Asset;
|
2023-08-08 17:21:39 -07:00
|
|
|
use App\Models\Company;
|
2023-08-08 17:10:36 -07:00
|
|
|
use App\Models\User;
|
2023-08-08 17:21:39 -07:00
|
|
|
use Illuminate\Testing\TestResponse;
|
2023-08-08 17:10:36 -07:00
|
|
|
use League\Csv\Reader;
|
|
|
|
use Tests\Support\InteractsWithSettings;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class CustomReportTest extends TestCase
|
|
|
|
{
|
|
|
|
use InteractsWithSettings;
|
|
|
|
|
|
|
|
public function testCustomAssetReport()
|
|
|
|
{
|
|
|
|
Asset::factory()->create(['name' => 'Asset A']);
|
|
|
|
Asset::factory()->create(['name' => 'Asset B']);
|
|
|
|
|
|
|
|
$response = $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');
|
|
|
|
|
2023-08-08 17:21:39 -07:00
|
|
|
$this->assertResponseContains($response, 'Asset A');
|
|
|
|
$this->assertResponseContains($response, '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();
|
|
|
|
|
|
|
|
$response = $this->actingAs($superUser)
|
|
|
|
->post('reports/custom', [
|
|
|
|
'asset_name' => '1',
|
|
|
|
'asset_tag' => '1',
|
|
|
|
'serial' => '1',
|
|
|
|
]);
|
|
|
|
$this->assertResponseContains($response, 'Asset A');
|
|
|
|
$this->assertResponseContains($response, 'Asset B');
|
|
|
|
|
|
|
|
$response = $this->actingAs($userInCompanyA)
|
|
|
|
->post('reports/custom', [
|
|
|
|
'asset_name' => '1',
|
|
|
|
'asset_tag' => '1',
|
|
|
|
'serial' => '1',
|
|
|
|
]);
|
|
|
|
$this->assertResponseContains($response, 'Asset A');
|
|
|
|
$this->assertResponseContains($response, 'Asset B');
|
|
|
|
|
|
|
|
$response = $this->actingAs($userInCompanyB)
|
|
|
|
->post('reports/custom', [
|
|
|
|
'asset_name' => '1',
|
|
|
|
'asset_tag' => '1',
|
|
|
|
'serial' => '1',
|
|
|
|
]);
|
|
|
|
$this->assertResponseContains($response, 'Asset A');
|
|
|
|
$this->assertResponseContains($response, 'Asset B');
|
2023-08-08 17:10:36 -07:00
|
|
|
|
2023-08-08 17:21:39 -07:00
|
|
|
$this->settings->enableMultipleFullCompanySupport();
|
|
|
|
|
|
|
|
$response = $this->actingAs($superUser)
|
|
|
|
->post('reports/custom', [
|
|
|
|
'asset_name' => '1',
|
|
|
|
'asset_tag' => '1',
|
|
|
|
'serial' => '1',
|
|
|
|
]);
|
|
|
|
$this->assertResponseContains($response, 'Asset A');
|
|
|
|
$this->assertResponseContains($response, 'Asset B');
|
|
|
|
|
|
|
|
$response = $this->actingAs($userInCompanyA)
|
|
|
|
->post('reports/custom', [
|
|
|
|
'asset_name' => '1',
|
|
|
|
'asset_tag' => '1',
|
|
|
|
'serial' => '1',
|
|
|
|
]);
|
|
|
|
$this->assertResponseContains($response, 'Asset A');
|
|
|
|
$this->assertResponseDoesNotContain($response, 'Asset B');
|
|
|
|
|
|
|
|
$response = $this->actingAs($userInCompanyB)
|
|
|
|
->post('reports/custom', [
|
|
|
|
'asset_name' => '1',
|
|
|
|
'asset_tag' => '1',
|
|
|
|
'serial' => '1',
|
|
|
|
]);
|
|
|
|
$this->assertResponseDoesNotContain($response, 'Asset A');
|
|
|
|
$this->assertResponseContains($response, 'Asset B');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function assertResponseContains(TestResponse $response, string $needle)
|
|
|
|
{
|
|
|
|
$this->assertTrue(
|
|
|
|
collect(Reader::createFromString($response->streamedContent())->getRecords())
|
|
|
|
->pluck(0)
|
|
|
|
->contains($needle)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function assertResponseDoesNotContain(TestResponse $response, string $needle)
|
|
|
|
{
|
|
|
|
$this->assertFalse(
|
|
|
|
collect(Reader::createFromString($response->streamedContent())->getRecords())
|
|
|
|
->pluck(0)
|
|
|
|
->contains($needle)
|
|
|
|
);
|
2023-08-08 17:10:36 -07:00
|
|
|
}
|
|
|
|
}
|