From e0d875285ca472230334e1e488dce4506748b3db Mon Sep 17 00:00:00 2001 From: Mojmir Fendek Date: Fri, 22 Dec 2023 09:42:15 +1300 Subject: [PATCH] Fixes. --- tests/TagFieldTest.php | 98 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) 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 *