Organize tests
This commit is contained in:
parent
82f4cc799b
commit
0ac1dd314a
6 changed files with 186 additions and 145 deletions
66
tests/Feature/ReportTemplates/CreateReportTemplateTest.php
Normal file
66
tests/Feature/ReportTemplates/CreateReportTemplateTest.php
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\ReportTemplates;
|
||||||
|
|
||||||
|
use App\Models\ReportTemplate;
|
||||||
|
use App\Models\User;
|
||||||
|
use Tests\Support\InteractsWithSettings;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class CreateReportTemplateTest extends TestCase
|
||||||
|
{
|
||||||
|
use InteractsWithSettings;
|
||||||
|
|
||||||
|
public function testSavingReportTemplateRequiresCorrectPermission()
|
||||||
|
{
|
||||||
|
$this->actingAs(User::factory()->create())
|
||||||
|
->post(route('report-templates.store'))
|
||||||
|
->assertForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSavingReportTemplateRequiresValidFields()
|
||||||
|
{
|
||||||
|
$this->actingAs(User::factory()->canViewReports()->create())
|
||||||
|
->post(route('report-templates.store'), [
|
||||||
|
'name' => '',
|
||||||
|
])
|
||||||
|
->assertSessionHasErrors('name');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRedirectingAfterValidationErrorRestoresInputs()
|
||||||
|
{
|
||||||
|
$this->actingAs(User::factory()->canViewReports()->create())
|
||||||
|
// start on the custom report page
|
||||||
|
->from(route('reports/custom'))
|
||||||
|
->followingRedirects()
|
||||||
|
->post(route('report-templates.store'), [
|
||||||
|
'name' => '',
|
||||||
|
// set some values to ensure they are still present
|
||||||
|
// when returning to the custom report page.
|
||||||
|
'by_company_id' => [2, 3]
|
||||||
|
])->assertViewHas(['template' => function (ReportTemplate $reportTemplate) {
|
||||||
|
return data_get($reportTemplate, 'options.by_company_id') === [2, 3];
|
||||||
|
}]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCanSaveAReportTemplate()
|
||||||
|
{
|
||||||
|
$user = User::factory()->canViewReports()->create();
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->post(route('report-templates.store'), [
|
||||||
|
'name' => 'My Awesome Template',
|
||||||
|
'company' => '1',
|
||||||
|
'by_company_id' => ['1', '2'],
|
||||||
|
])
|
||||||
|
->assertRedirect();
|
||||||
|
|
||||||
|
$template = $user->reportTemplates->first(function ($report) {
|
||||||
|
return $report->name === 'My Awesome Template';
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assertNotNull($template);
|
||||||
|
$this->assertEquals('1', $template->options['company']);
|
||||||
|
$this->assertEquals(['1', '2'], $template->options['by_company_id']);
|
||||||
|
}
|
||||||
|
}
|
21
tests/Feature/ReportTemplates/DeleteReportTemplateTest.php
Normal file
21
tests/Feature/ReportTemplates/DeleteReportTemplateTest.php
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Feature\ReportTemplates;
|
||||||
|
|
||||||
|
use Tests\Support\InteractsWithSettings;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class DeleteReportTemplateTest extends TestCase
|
||||||
|
{
|
||||||
|
use InteractsWithSettings;
|
||||||
|
|
||||||
|
public function testDeletingReportTemplateRequiresCorrectPermission()
|
||||||
|
{
|
||||||
|
$this->markTestIncomplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCanDeleteAReportTemplate()
|
||||||
|
{
|
||||||
|
$this->markTestIncomplete();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,145 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Tests\Feature\ReportTemplates;
|
|
||||||
|
|
||||||
use App\Models\ReportTemplate;
|
|
||||||
use App\Models\User;
|
|
||||||
use Illuminate\Database\Eloquent\Collection;
|
|
||||||
use Tests\Support\InteractsWithSettings;
|
|
||||||
use Tests\TestCase;
|
|
||||||
|
|
||||||
class ReportTemplateTest extends TestCase
|
|
||||||
{
|
|
||||||
use InteractsWithSettings;
|
|
||||||
|
|
||||||
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 testSavedTemplatesAreScopedToTheUser()
|
|
||||||
{
|
|
||||||
// Given there is a saved template for one user
|
|
||||||
ReportTemplate::factory()->create(['name' => 'Report A']);
|
|
||||||
|
|
||||||
// When loading reports/custom while acting as another user that also has a saved template
|
|
||||||
$user = User::factory()->canViewReports()
|
|
||||||
->has(ReportTemplate::factory(['name' => 'Report B']))
|
|
||||||
->create();
|
|
||||||
|
|
||||||
// The user should not see the other user's template (in view as 'report_templates')
|
|
||||||
$this->actingAs($user)
|
|
||||||
->get(route('reports/custom'))
|
|
||||||
->assertViewHas(['report_templates' => function (Collection $reports) {
|
|
||||||
return $reports->pluck('name')->doesntContain('Report A');
|
|
||||||
}]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testCanLoadASavedReportTemplate()
|
|
||||||
{
|
|
||||||
$user = User::factory()->canViewReports()->create();
|
|
||||||
$reportTemplate = ReportTemplate::factory()->make(['name' => 'My Awesome Template']);
|
|
||||||
$user->reportTemplates()->save($reportTemplate);
|
|
||||||
|
|
||||||
$this->actingAs($user)
|
|
||||||
->get(route('report-templates.show', $reportTemplate))
|
|
||||||
->assertOk()
|
|
||||||
->assertViewHas(['template' => function (ReportTemplate $templatePassedToView) use ($reportTemplate) {
|
|
||||||
return $templatePassedToView->is($reportTemplate);
|
|
||||||
}]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testCanSaveAReportTemplate()
|
|
||||||
{
|
|
||||||
$user = User::factory()->canViewReports()->create();
|
|
||||||
|
|
||||||
$this->actingAs($user)
|
|
||||||
->post(route('report-templates.store'), [
|
|
||||||
'name' => 'My Awesome Template',
|
|
||||||
'company' => '1',
|
|
||||||
'by_company_id' => ['1', '2'],
|
|
||||||
])
|
|
||||||
->assertRedirect();
|
|
||||||
|
|
||||||
$template = $user->reportTemplates->first(function ($report) {
|
|
||||||
return $report->name === 'My Awesome Template';
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->assertNotNull($template);
|
|
||||||
$this->assertEquals('1', $template->options['company']);
|
|
||||||
$this->assertEquals(['1', '2'], $template->options['by_company_id']);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testSavingReportTemplateRequiresValidFields()
|
|
||||||
{
|
|
||||||
$this->actingAs(User::factory()->canViewReports()->create())
|
|
||||||
->post(route('report-templates.store'), [
|
|
||||||
'name' => '',
|
|
||||||
])
|
|
||||||
->assertSessionHasErrors('name');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testCanUpdateAReportTemplate()
|
|
||||||
{
|
|
||||||
$this->markTestIncomplete();
|
|
||||||
|
|
||||||
$user = User::factory()->canViewReports()->create();
|
|
||||||
|
|
||||||
$reportTemplate = ReportTemplate::factory()->for($user)->create();
|
|
||||||
|
|
||||||
$this->actingAs($user)
|
|
||||||
->get(route('report-templates.edit', $reportTemplate))
|
|
||||||
->assertOk();
|
|
||||||
|
|
||||||
// @todo:
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testRedirectingAfterValidationErrorRestoresInputs()
|
|
||||||
{
|
|
||||||
$this->actingAs(User::factory()->canViewReports()->create())
|
|
||||||
// start on the custom report page
|
|
||||||
->from(route('reports/custom'))
|
|
||||||
->followingRedirects()
|
|
||||||
->post(route('report-templates.store'), [
|
|
||||||
'name' => '',
|
|
||||||
// set some values to ensure they are still present
|
|
||||||
// when returning to the custom report page.
|
|
||||||
'by_company_id' => [2, 3]
|
|
||||||
])->assertViewHas(['template' => function (ReportTemplate $reportTemplate) {
|
|
||||||
return data_get($reportTemplate, 'options.by_company_id') === [2, 3];
|
|
||||||
}]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testUpdatingReportTemplateRequiresValidFields()
|
|
||||||
{
|
|
||||||
$this->markTestIncomplete();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testSavingReportTemplateRequiresCorrectPermission()
|
|
||||||
{
|
|
||||||
$this->actingAs(User::factory()->create())
|
|
||||||
->post(route('report-templates.store'))
|
|
||||||
->assertForbidden();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testUpdatingReportTemplateRequiresCorrectPermission()
|
|
||||||
{
|
|
||||||
$this->markTestIncomplete();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testCanDeleteAReportTemplate()
|
|
||||||
{
|
|
||||||
$this->markTestIncomplete();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testDeletingReportTemplateRequiresCorrectPermission()
|
|
||||||
{
|
|
||||||
$this->markTestIncomplete();
|
|
||||||
}
|
|
||||||
}
|
|
38
tests/Feature/ReportTemplates/ShowReportTemplateTest.php
Normal file
38
tests/Feature/ReportTemplates/ShowReportTemplateTest.php
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Feature\ReportTemplates;
|
||||||
|
|
||||||
|
use App\Models\ReportTemplate;
|
||||||
|
use App\Models\User;
|
||||||
|
use Tests\Support\InteractsWithSettings;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class ShowReportTemplateTest extends TestCase
|
||||||
|
{
|
||||||
|
use InteractsWithSettings;
|
||||||
|
|
||||||
|
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();
|
||||||
|
$reportTemplate = ReportTemplate::factory()->make(['name' => 'My Awesome Template']);
|
||||||
|
$user->reportTemplates()->save($reportTemplate);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->get(route('report-templates.show', $reportTemplate))
|
||||||
|
->assertOk()
|
||||||
|
->assertViewHas(['template' => function (ReportTemplate $templatePassedToView) use ($reportTemplate) {
|
||||||
|
return $templatePassedToView->is($reportTemplate);
|
||||||
|
}]);
|
||||||
|
}
|
||||||
|
}
|
38
tests/Feature/ReportTemplates/UpdateReportTemplateTest.php
Normal file
38
tests/Feature/ReportTemplates/UpdateReportTemplateTest.php
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Feature\ReportTemplates;
|
||||||
|
|
||||||
|
use App\Models\ReportTemplate;
|
||||||
|
use App\Models\User;
|
||||||
|
use Tests\Support\InteractsWithSettings;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class UpdateReportTemplateTest extends TestCase
|
||||||
|
{
|
||||||
|
use InteractsWithSettings;
|
||||||
|
|
||||||
|
public function testUpdatingReportTemplateRequiresCorrectPermission()
|
||||||
|
{
|
||||||
|
$this->markTestIncomplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUpdatingReportTemplateRequiresValidFields()
|
||||||
|
{
|
||||||
|
$this->markTestIncomplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCanUpdateAReportTemplate()
|
||||||
|
{
|
||||||
|
$this->markTestIncomplete();
|
||||||
|
|
||||||
|
$user = User::factory()->canViewReports()->create();
|
||||||
|
|
||||||
|
$reportTemplate = ReportTemplate::factory()->for($user)->create();
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->get(route('report-templates.edit', $reportTemplate))
|
||||||
|
->assertOk();
|
||||||
|
|
||||||
|
// @todo:
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,10 +5,33 @@ namespace Tests\Unit;
|
||||||
use App\Models\Department;
|
use App\Models\Department;
|
||||||
use App\Models\Location;
|
use App\Models\Location;
|
||||||
use App\Models\ReportTemplate;
|
use App\Models\ReportTemplate;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
|
use Tests\Support\InteractsWithSettings;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
class ReportTemplateTest extends TestCase
|
class ReportTemplateTest extends TestCase
|
||||||
{
|
{
|
||||||
|
use InteractsWithSettings;
|
||||||
|
|
||||||
|
public function testSavedTemplatesAreScopedToTheUser()
|
||||||
|
{
|
||||||
|
// Given there is a saved template for one user
|
||||||
|
ReportTemplate::factory()->create(['name' => 'Report A']);
|
||||||
|
|
||||||
|
// When loading reports/custom while acting as another user that also has a saved template
|
||||||
|
$user = User::factory()->canViewReports()
|
||||||
|
->has(ReportTemplate::factory(['name' => 'Report B']))
|
||||||
|
->create();
|
||||||
|
|
||||||
|
// The user should not see the other user's template (in view as 'report_templates')
|
||||||
|
$this->actingAs($user)
|
||||||
|
->get(route('reports/custom'))
|
||||||
|
->assertViewHas(['report_templates' => function (Collection $reports) {
|
||||||
|
return $reports->pluck('name')->doesntContain('Report A');
|
||||||
|
}]);
|
||||||
|
}
|
||||||
|
|
||||||
public function testParsingValuesOnNonExistentReportTemplate()
|
public function testParsingValuesOnNonExistentReportTemplate()
|
||||||
{
|
{
|
||||||
$unsavedTemplate = new ReportTemplate;
|
$unsavedTemplate = new ReportTemplate;
|
||||||
|
|
Loading…
Add table
Reference in a new issue