Skip to content

A free open source IT asset/license management system

Organize tests

Marcus Moored4cf39238742
diff --git a/tests/Feature/ReportTemplates/CreateReportTemplateTest.php b/tests/Feature/ReportTemplates/CreateReportTemplateTest.php
index f271a16747..ba742c8ad6 100644
--- a/tests/Feature/ReportTemplates/CreateReportTemplateTest.php
+++ b/tests/Feature/ReportTemplates/CreateReportTemplateTest.php
@@ -12,14 +12,14 @@ class CreateReportTemplateTest extends TestCase implements TestsPermissionsRequi
public function testRequiresPermission()
{
$this->actingAs(User::factory()->create())
- ->post(route('report-templates.store'))
+ ->post($this->getRoute())
->assertForbidden();
}
public function testSavingReportTemplateRequiresValidFields()
{
$this->actingAs(User::factory()->canViewReports()->create())
- ->post(route('report-templates.store'), [
+ ->post($this->getRoute(), [
'name' => '',
])
->assertSessionHasErrors('name');
@@ -31,7 +31,7 @@ class CreateReportTemplateTest extends TestCase implements TestsPermissionsRequi
// start on the custom report page
->from(route('reports/custom'))
->followingRedirects()
- ->post(route('report-templates.store'), [
+ ->post($this->getRoute(), [
'name' => '',
// set some values to ensure they are still present
// when returning to the custom report page.
@@ -46,7 +46,7 @@ class CreateReportTemplateTest extends TestCase implements TestsPermissionsRequi
$user = User::factory()->canViewReports()->create();
$this->actingAs($user)
- ->post(route('report-templates.store'), [
+ ->post($this->getRoute(), [
'name' => 'My Awesome Template',
'company' => '1',
'by_company_id' => ['1', '2'],
@@ -61,4 +61,9 @@ class CreateReportTemplateTest extends TestCase implements TestsPermissionsRequi
$this->assertEquals('1', $template->options['company']);
$this->assertEquals(['1', '2'], $template->options['by_company_id']);
}
+
+ private function getRoute(): string
+ {
+ return route('report-templates.store');
+ }
}
diff --git a/tests/Feature/ReportTemplates/DeleteReportTemplateTest.php b/tests/Feature/ReportTemplates/DeleteReportTemplateTest.php
index 74bc6f1578..ac6c7cbccb 100644
--- a/tests/Feature/ReportTemplates/DeleteReportTemplateTest.php
+++ b/tests/Feature/ReportTemplates/DeleteReportTemplateTest.php
@@ -14,7 +14,7 @@ class DeleteReportTemplateTest extends TestCase implements TestsPermissionsRequi
$reportTemplate = ReportTemplate::factory()->create();
$this->actingAs(User::factory()->create())
- ->post(route('report-templates.destroy', $reportTemplate->id))
+ ->post($this->getRoute($reportTemplate))
->assertForbidden();
$this->assertModelExists($reportTemplate);
@@ -25,7 +25,7 @@ class DeleteReportTemplateTest extends TestCase implements TestsPermissionsRequi
$reportTemplate = ReportTemplate::factory()->create();
$this->actingAs(User::factory()->canViewReports()->create())
- ->delete(route('report-templates.destroy', $reportTemplate))
+ ->delete($this->getRoute($reportTemplate))
->assertSessionHas('error')
->assertRedirect(route('reports/custom'));
@@ -38,9 +38,14 @@ class DeleteReportTemplateTest extends TestCase implements TestsPermissionsRequi
$reportTemplate = ReportTemplate::factory()->for($user, 'creator')->create();
$this->actingAs($user)
- ->delete(route('report-templates.destroy', $reportTemplate))
+ ->delete($this->getRoute($reportTemplate))
->assertRedirect(route('reports/custom'));
$this->assertModelMissing($reportTemplate);
}
+
+ private function getRoute(ReportTemplate $reportTemplate): string
+ {
+ return route('report-templates.destroy', $reportTemplate->id);
+ }
}
diff --git a/tests/Feature/ReportTemplates/EditReportTemplateTest.php b/tests/Feature/ReportTemplates/EditReportTemplateTest.php
index 46f91b74ff..1698a167f5 100644
--- a/tests/Feature/ReportTemplates/EditReportTemplateTest.php
+++ b/tests/Feature/ReportTemplates/EditReportTemplateTest.php
@@ -12,7 +12,7 @@ class EditReportTemplateTest extends TestCase implements TestsPermissionsRequire
public function testRequiresPermission()
{
$this->actingAs(User::factory()->create())
- ->get(route('report-templates.edit', ReportTemplate::factory()->create()))
+ ->get($this->getRoute(ReportTemplate::factory()->create()))
->assertForbidden();
}
@@ -22,7 +22,7 @@ class EditReportTemplateTest extends TestCase implements TestsPermissionsRequire
$reportTemplate = ReportTemplate::factory()->create();
$this->actingAs($user)
- ->get(route('report-templates.edit', $reportTemplate))
+ ->get($this->getRoute($reportTemplate))
->assertSessionHas('error')
->assertRedirect(route('reports/custom'));
}
@@ -33,7 +33,12 @@ class EditReportTemplateTest extends TestCase implements TestsPermissionsRequire
$reportTemplate = ReportTemplate::factory()->for($user, 'creator')->create();
$this->actingAs($user)
- ->get(route('report-templates.edit', $reportTemplate))
+ ->get($this->getRoute($reportTemplate))
->assertOk();
}
+
+ private function getRoute(ReportTemplate $reportTemplate): string
+ {
+ return route('report-templates.edit', $reportTemplate);
+ }
}
diff --git a/tests/Feature/ReportTemplates/ShowReportTemplateTest.php b/tests/Feature/ReportTemplates/ShowReportTemplateTest.php
index 0fffd0962b..9ee1fd70ff 100644
--- a/tests/Feature/ReportTemplates/ShowReportTemplateTest.php
+++ b/tests/Feature/ReportTemplates/ShowReportTemplateTest.php
@@ -12,21 +12,10 @@ class ShowReportTemplateTest extends TestCase implements TestsPermissionsRequire
public function testRequiresPermission()
{
$this->actingAs(User::factory()->create())
- ->get(route('reports/custom'))
+ ->get($this->getRoute(ReportTemplate::factory()->create()))
->assertForbidden();
}
- public function testCanLoadCustomReportPage()
- {
- $this->actingAs(User::factory()->canViewReports()->create())
- ->get(route('reports/custom'))
- ->assertOk()
- ->assertViewHas(['template' => function (ReportTemplate $template) {
- // the view should have an empty report by default
- return $template->exists() === false;
- }]);
- }
-
public function testCanLoadASavedReportTemplate()
{
$user = User::factory()->canViewReports()->create();
@@ -34,7 +23,7 @@ class ShowReportTemplateTest extends TestCase implements TestsPermissionsRequire
$user->reportTemplates()->save($reportTemplate);
$this->actingAs($user)
- ->get(route('report-templates.show', $reportTemplate))
+ ->get($this->getRoute($reportTemplate))
->assertOk()
->assertViewHas(['template' => function (ReportTemplate $templatePassedToView) use ($reportTemplate) {
return $templatePassedToView->is($reportTemplate);
@@ -46,8 +35,13 @@ class ShowReportTemplateTest extends TestCase implements TestsPermissionsRequire
$reportTemplate = ReportTemplate::factory()->create();
$this->actingAs(User::factory()->canViewReports()->create())
- ->get(route('report-templates.show', $reportTemplate))
+ ->get($this->getRoute($reportTemplate))
->assertSessionHas('error')
->assertRedirect(route('reports/custom'));
}
+
+ private function getRoute(ReportTemplate $reportTemplate): string
+ {
+ return route('report-templates.show', $reportTemplate);
+ }
}
diff --git a/tests/Feature/ReportTemplates/UpdateReportTemplateTest.php b/tests/Feature/ReportTemplates/UpdateReportTemplateTest.php
index 1054be34e4..54ab00319f 100644
--- a/tests/Feature/ReportTemplates/UpdateReportTemplateTest.php
+++ b/tests/Feature/ReportTemplates/UpdateReportTemplateTest.php
@@ -12,14 +12,14 @@ class UpdateReportTemplateTest extends TestCase implements TestsPermissionsRequi
public function testRequiresPermission()
{
$this->actingAs(User::factory()->create())
- ->post(route('report-templates.update', ReportTemplate::factory()->create()))
+ ->post($this->getRoute(ReportTemplate::factory()->create()))
->assertForbidden();
}
public function testCannotUpdateAnotherUsersReportTemplate()
{
$this->actingAs(User::factory()->canViewReports()->create())
- ->post(route('report-templates.update', ReportTemplate::factory()->create()))
+ ->post($this->getRoute(ReportTemplate::factory()->create()))
->assertSessionHas('error')
->assertRedirect(route('reports/custom'));
}
@@ -39,7 +39,7 @@ class UpdateReportTemplateTest extends TestCase implements TestsPermissionsRequi
]);
$this->actingAs($user)
- ->post(route('report-templates.update', $reportTemplate), [
+ ->post($this->getRoute($reportTemplate), [
'id' => 1,
'company' => 1,
'by_company_id' => [3],
@@ -52,4 +52,9 @@ class UpdateReportTemplateTest extends TestCase implements TestsPermissionsRequi
$this->assertEquals(1, $reportTemplate->checkmarkValue('company'));
$this->assertEquals([3], $reportTemplate->selectValues('by_company_id'));
}
+
+ private function getRoute(ReportTemplate $reportTemplate): string
+ {
+ return route('report-templates.update', $reportTemplate);
+ }
}
diff --git a/tests/Feature/Reporting/CustomReportTest.php b/tests/Feature/Reporting/CustomReportTest.php
index 4d11cb23c8..6b31a09863 100644
--- a/tests/Feature/Reporting/CustomReportTest.php
+++ b/tests/Feature/Reporting/CustomReportTest.php
@@ -4,13 +4,15 @@ namespace Tests\Feature\Reporting;
use App\Models\Asset;
use App\Models\Company;
+use App\Models\ReportTemplate;
use App\Models\User;
use Illuminate\Testing\TestResponse;
use League\Csv\Reader;
use PHPUnit\Framework\Assert;
+use Tests\Concerns\TestsPermissionsRequirement;
use Tests\TestCase;
-class CustomReportTest extends TestCase
+class CustomReportTest extends TestCase implements TestsPermissionsRequirement
{
protected function setUp(): void
{
@@ -43,6 +45,26 @@ class CustomReportTest extends TestCase
);
}
+ public function testRequiresPermission()
+ {
+ $this->actingAs(User::factory()->create())
+ ->get(route('reports/custom'))
+ ->assertForbidden();
+ }
+
+ public function testCanLoadCustomReportPage()
+ {
+ $this->actingAs(User::factory()->canViewReports()->create())
+ ->get(route('reports/custom'))
+ ->assertOk()
+ ->assertViewHas([
+ 'template' => function (ReportTemplate $template) {
+ // the view should have an empty report by default
+ return $template->exists() === false;
+ }
+ ]);
+ }
+
public function testCustomAssetReport()
{
Asset::factory()->create(['name' => 'Asset A']);