diff --git a/tests/TagFieldTest.php b/tests/TagFieldTest.php index 4ebfc8d..356f405 100755 --- a/tests/TagFieldTest.php +++ b/tests/TagFieldTest.php @@ -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 *