Skip to content

Commit

Permalink
Fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfendeksilverstripe committed Dec 21, 2023
1 parent f2f6a9c commit e0d8752
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions tests/TagFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,104 @@ public function testItSavesToHasOne()
$this->assertEquals($tag->ID, $record->PrimaryTagID, 'The tag is saved to a has_one');
}

/**
* @dataProvider rawValueStoreCasesProvider
*/
public function testItSavesToRawValue(bool $rawValueEnabled, mixed $values, mixed $expected): void
{
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
$tag = new TagFieldTestBlogTag();
$tag->Title = 'RawValueTest';
$tag->write();

$field = new TagField('InMemoryOnlyProperty', '', new DataList(TagFieldTestBlogTag::class));
$field->setAllowRawValue($rawValueEnabled);

$field->setValue($values);
$field->saveInto($record);

$this->assertEquals($expected, $record->InMemoryOnlyProperty, 'We expect raw value to be stored');
}

public function rawValueStoreCasesProvider(): array
{
return [
'raw value disabled' => [
false,
'SampleValue1',
null,
],
'raw value enabled (single value)' => [
true,
'SampleValue1',
[
'SampleValue1',
],
],
'raw value enabled (multiple values)' => [
true,
[
'SampleValue1',
'SampleValue2',
],
[
'SampleValue1',
'SampleValue2',
],
],
];
}

/**
* @dataProvider rawValueLoadCasesProvider
*/
public function testItLoadsFromRawValue(bool $rawValueEnabled, mixed $values, mixed $expected): void
{
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
$tag = new TagFieldTestBlogTag();
$tag->Title = 'RawValueTest';
$tag->write();

$field = new TagField('InMemoryOnlyProperty', '', new DataList(TagFieldTestBlogTag::class));
$field->setAllowRawValue($rawValueEnabled);
$record->InMemoryOnlyProperty = $values;

$field->loadFrom($record);

$this->assertEquals($expected, $field->Value(), 'We expect raw value to be loaded');
}

public function rawValueLoadCasesProvider(): array
{
return [
'raw value disabled' => [
false,
null,
[],
],
'raw value enabled (single value)' => [
true,
[
'SampleValue1',
],
[
'SampleValue1',
],
],
'raw value enabled (multiple values)' => [
true,
[
'SampleValue1',
'SampleValue2',
],
[
'SampleValue1',
'SampleValue2',
],
],
];
}

/**
* @param string $name
*
Expand Down

0 comments on commit e0d8752

Please sign in to comment.