From 158bc006a5b72ea64445caa8c77dab7dfd074b49 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Mon, 17 Apr 2023 13:45:22 +1200 Subject: [PATCH] FIX Format value to string for readonly transformation Co-authored-by: Werner Huber --- src/StringTagField.php | 7 +++++++ tests/StringTagFieldTest.php | 34 +++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/StringTagField.php b/src/StringTagField.php index 556286f..57ebd61 100644 --- a/src/StringTagField.php +++ b/src/StringTagField.php @@ -412,4 +412,11 @@ public function setCanCreate($canCreate) return $this; } + + public function performReadonlyTransformation() + { + $field = parent::performReadonlyTransformation(); + $field->setValue(implode(', ', $this->Value())); + return $field; + } } diff --git a/tests/StringTagFieldTest.php b/tests/StringTagFieldTest.php index 024fd71..254041b 100755 --- a/tests/StringTagFieldTest.php +++ b/tests/StringTagFieldTest.php @@ -171,22 +171,26 @@ public function testSchemaIsAddedToAttributes() $this->assertNotEmpty($attributes['data-schema']); } - /** - * Ensure a source of tags matches the given string tag names - * - * @param array $expected - * @param DataList $actualSource - */ - protected function compareTagLists(array $expected, DataList $actualSource) + public function testPerformReadonlyTransformation() { - $actual = array_keys($actualSource->map('ID', 'Title')->toArray()); - sort($expected); - sort($actual); - - $this->assertEquals( - $expected, - $actual - ); + $field = new StringTagField('Tags'); + $field->setSource(['Test1', 'Test2', 'Test3']); + + // Ensure a single value can be rendered + $field->setValue(['Test2']); + $field_readonly = $field->performReadonlyTransformation(); + $this->assertEquals('Test2', $field_readonly->Value()); + + // Ensure multiple valid values are rendered + $field->setValue(['Test1', 'Test2']); + $field_readonly = $field->performReadonlyTransformation(); + $this->assertEquals('Test1, Test2', $field_readonly->Value()); + + // Ensure an value not in the source array is still rendered + // (because e.g. in history view it must have been a valid value when it was set) + $field->setValue(['Test', 'Test1']); + $field_readonly = $field->performReadonlyTransformation(); + $this->assertEquals('Test, Test1', $field_readonly->Value()); } /**