From fe5492ff8520182f49664712ec0f5f097177a4d6 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Fri, 15 Nov 2024 14:00:54 +1300 Subject: [PATCH] FIX Avoid infinite recursive loop with attributes in schemadata --- code/Forms/UploadField.php | 2 - .../AssetAdmin/Forms/UploadField.ss | 2 +- tests/php/Forms/UploadFieldTest.php | 40 ++++++++++++++----- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/code/Forms/UploadField.php b/code/Forms/UploadField.php index 3417880ee..2f235b368 100644 --- a/code/Forms/UploadField.php +++ b/code/Forms/UploadField.php @@ -288,8 +288,6 @@ public function getAttributes() 'type' => 'file', 'multiple' => $this->getIsMultiUpload(), 'id' => $this->ID(), - 'data-schema' => json_encode($this->getSchemaData()), - 'data-state' => json_encode($this->getSchemaState()), ); $attributes = array_merge($attributes, $this->attributes); diff --git a/templates/SilverStripe/AssetAdmin/Forms/UploadField.ss b/templates/SilverStripe/AssetAdmin/Forms/UploadField.ss index 0868a5074..f32d18777 100644 --- a/templates/SilverStripe/AssetAdmin/Forms/UploadField.ss +++ b/templates/SilverStripe/AssetAdmin/Forms/UploadField.ss @@ -7,4 +7,4 @@ <% end_if %> - /> + /> diff --git a/tests/php/Forms/UploadFieldTest.php b/tests/php/Forms/UploadFieldTest.php index 9196121f2..1dff55cea 100644 --- a/tests/php/Forms/UploadFieldTest.php +++ b/tests/php/Forms/UploadFieldTest.php @@ -88,10 +88,29 @@ public function testGetAttributes() Form::create($admin, 'MyForm', FieldList::create($field), FieldList::create()); $attributes = $field->getAttributes(); + $this->assertSame('entwine-uploadfield uploadfield myfield', $attributes['class']); + $this->assertSame('file', $attributes['type']); + $this->assertSame(false, $attributes['multiple']); + $this->assertSame('Form_MyForm_MyField', $attributes['id']); + } + + public function testSchemaInRenderedField(): void + { + $field = new UploadField('MyField'); + $field->addExtraClass('myfield'); + $field->setIsMultiUpload(false); + $field->setFolderName('/'); + /** @var Image $image */ + $image = $this->objFromFixture(Image::class, 'image1'); + $field->setItems(new ArrayList([$image])); + $admin = AssetAdmin::create(); + Form::create($admin, 'MyForm', FieldList::create($field), FieldList::create()); + $schema = [ 'name' => 'MyField', 'id' => 'Form_MyForm_MyField', 'type' => 'file', + 'schemaType' => 'Custom', 'component' => 'UploadField', 'holderId' => 'Form_MyForm_MyField_Holder', 'title' => 'My field', @@ -102,10 +121,13 @@ public function testGetAttributes() 'leftTitle' => null, 'readOnly' => false, 'disabled' => false, - 'autoFocus' => false, 'customValidationMessage' => '', 'validation' => [], - 'attributes' => [], + 'attributes' => [ + 'class' => 'entwine-uploadfield uploadfield myfield', + 'multiple' => false, + ], + 'autoFocus' => false, 'data' => [ 'endpoints' => [ 'createFile' => [ @@ -114,14 +136,13 @@ public function testGetAttributes() 'payloadFormat' => 'urlencoded', ], ], - 'multi' => false, - 'parentid' => 0, 'maxFilesize' => $field->getAllowedMaxFileSize() / 1024 / 1024, 'maxFiles' => null, + 'multi' => false, + 'parentid' => 0, 'canUpload' => true, 'canAttach' => true, ], - 'schemaType' => 'Custom' ]; $state = [ 'name' => 'MyField', @@ -132,13 +153,10 @@ public function testGetAttributes() 'files' => [ $admin->getMinimalistObjectFromData($image) ], ], ]; - $this->assertSame('entwine-uploadfield uploadfield myfield', $attributes['class']); - $this->assertSame('file', $attributes['type']); - $this->assertSame(false, $attributes['multiple']); - $this->assertSame('Form_MyForm_MyField', $attributes['id']); // Check schema / state are encoded in this field - $this->assertEquals($schema, json_decode($attributes['data-schema'] ?? '', true)); - $this->assertEquals($state, json_decode($attributes['data-state'] ?? '', true)); + $html = $field->Field(); + $this->assertStringContainsString(htmlspecialchars(json_encode($schema)), $html); + $this->assertStringContainsString(htmlspecialchars(json_encode($state)), $html); } }