snipe-it/tests/Unit/ReportTemplateTest.php

176 lines
6 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Unit;
use App\Models\Location;
2023-12-18 12:55:48 -08:00
use App\Models\ReportTemplate;
use Tests\TestCase;
2023-12-18 12:55:48 -08:00
class ReportTemplateTest extends TestCase
{
2023-11-30 16:57:21 -08:00
public function testParsingCheckmarkValue()
{
2023-12-18 12:55:48 -08:00
$savedReport = ReportTemplate::factory()->create([
'options' => [
'is_a_checkbox_field' => '1',
],
]);
$this->assertEquals('1', $savedReport->checkmarkValue('is_a_checkbox_field'));
$this->assertEquals('0', $savedReport->checkmarkValue('non_existent_key'));
2023-12-18 12:55:48 -08:00
$this->assertEquals('1', (new ReportTemplate)->checkmarkValue('is_a_checkbox_field'));
}
2023-11-30 16:57:21 -08:00
public function testParsingTextValue()
{
2023-12-18 12:55:48 -08:00
$savedReport = ReportTemplate::factory()->create([
'options' => [
'is_a_text_field' => 'some text',
],
]);
$this->assertEquals('some text', $savedReport->textValue('is_a_text_field'));
$this->assertEquals('', $savedReport->textValue('non_existent_key'));
2023-12-18 12:55:48 -08:00
$this->assertEquals('', (new ReportTemplate)->textValue('is_a_text_field'));
}
public function testParsingRadioValue()
{
2023-12-18 12:55:48 -08:00
$savedReport = ReportTemplate::factory()->create([
'options' => [
'is_a_radio_field' => null,
],
]);
$this->assertEquals('return_value', $savedReport->radioValue('is_a_radio_field', null, 'return_value'));
$this->assertEquals(null, $savedReport->radioValue('is_a_radio_field', 'another_value', 'return_value'));
$this->assertNull($savedReport->radioValue('non_existent_key', '1', true));
}
2023-11-30 12:12:57 -08:00
2023-11-30 16:57:21 -08:00
public function testParsingSelectValue()
{
2023-12-18 12:55:48 -08:00
$savedReport = ReportTemplate::factory()->create([
2023-11-30 16:57:21 -08:00
'options' => [
'is_a_text_field_as_well' => '4',
2023-11-30 16:57:21 -08:00
'contains_a_null_value' => null,
],
]);
$this->assertEquals('4', $savedReport->selectValue('is_a_text_field_as_well'));
2023-11-30 16:57:21 -08:00
$this->assertEquals('', $savedReport->selectValue('non_existent_key'));
$this->assertNull($savedReport->selectValue('contains_a_null_value'));
}
2023-11-30 12:12:57 -08:00
public function testParsingSelectValues()
2023-11-30 16:57:21 -08:00
{
2023-12-18 12:55:48 -08:00
$savedReport = ReportTemplate::factory()->create([
2023-11-30 16:57:21 -08:00
'options' => [
'is_an_array' => ['2', '3', '4'],
'is_an_array_containing_null' => [null],
],
]);
$this->assertEquals(['2', '3', '4'], $savedReport->selectValues('is_an_array'));
$this->assertEquals(null, $savedReport->selectValues('non_existent_key'));
$this->assertNull($savedReport->selectValues('is_an_array_containing_null'));
}
public function testSelectValueDoesNotIncludeDeletedOrNonExistentModels()
2023-11-30 12:12:57 -08:00
{
[$locationA, $locationB] = Location::factory()->count(2)->create();
$invalidId = 10000;
$templateWithValidId = ReportTemplate::factory()->create([
'options' => ['single_value' => $locationA->id],
]);
$templateWithDeletedId = ReportTemplate::factory()->create([
'options' => ['single_value' => $locationB->id],
]);
$locationB->delete();
$templateWithInvalidId = ReportTemplate::factory()->create([
'options' => ['single_value' => $invalidId],
]);
$this->assertEquals(
$locationA->id,
$templateWithValidId->selectValue('single_value', Location::class)
);
2023-11-30 16:57:21 -08:00
$this->assertNull($templateWithDeletedId->selectValue('single_value', Location::class));
$this->assertNull($templateWithInvalidId->selectValue('single_value', Location::class));
2023-11-30 12:12:57 -08:00
}
2023-11-30 16:57:21 -08:00
public function testSelectValuesDoNotIncludeDeletedOrNonExistentModels()
2023-11-30 12:12:57 -08:00
{
[$locationA, $locationB] = Location::factory()->count(2)->create();
2023-12-21 12:15:00 -08:00
$invalidId = 10000;
$savedReport = ReportTemplate::factory()->create([
'options' => [
2023-12-21 12:50:21 -08:00
'array_of_ids' => [
$locationA->id,
$locationB->id,
2023-12-21 12:15:00 -08:00
$invalidId,
],
],
]);
$locationB->delete();
2023-12-21 12:50:21 -08:00
$parsedValues = $savedReport->selectValues('array_of_ids', Location::class);
2023-12-21 12:15:00 -08:00
$this->assertContains($locationA->id, $parsedValues);
$this->assertNotContains($locationB->id, $parsedValues);
$this->assertNotContains($invalidId, $parsedValues);
2023-11-30 12:12:57 -08:00
}
2023-11-30 16:57:21 -08:00
2023-12-07 11:00:26 -08:00
public function testGracefullyHandlesSingleSelectBecomingMultiSelect()
{
$this->markTestIncomplete();
}
public function testGracefullyHandlesMultiSelectBecomingSingleSelect()
{
$this->markTestIncomplete();
}
2023-11-30 16:57:21 -08:00
public function testDeletedCustomFieldsDoNotCauseAnIssue()
{
$this->markTestIncomplete();
}
2023-11-30 17:07:39 -08:00
public function testDateRangesAreNotStored()
{
$this->markTestIncomplete();
// This might not be a test we implement, but it's a place to ask a question:
// Should we be saving and restoring date ranges?
// A use-case I can see is running a report at the end of the month for the date ranges for that month.
// Maybe it's better to leave those off so users are gently prompted to enter the ranges for each run?
// Another option would be to have checkbox that asks the user if they would like to save the dates?
// I'm not sure how helpful that is, and it would probably be a future feature if implemented.
}
public function testSavedReportHasDefaultValuesSet()
{
$this->markTestIncomplete();
// Quick thought: I think deleted_assets should be set to null so that
// "Exclude Deleted Assets" is selected when using a new'd up SavedReport.
}
public function testOldValuesStillWorkAfterTheseChanges()
{
$this->markTestIncomplete();
// Another marker that won't actually be a test case:
// We need to make sure that any behavior involving using "old" input.
// I explicitly removed the old()s from the "deleted_assets" radio buttons.
// The "x-selects" partials still include them, but I haven't tested them yet.
}
}