Skip to content

Commit

Permalink
FIX Avoid infinite recursive loop with attributes in schemadata
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Nov 19, 2024
1 parent 43758ab commit fe5492f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
2 changes: 0 additions & 2 deletions code/Forms/UploadField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion templates/SilverStripe/AssetAdmin/Forms/UploadField.ss
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
</div>
<% end_if %>
</div>
<input $AttributesHTML <% include SilverStripe/Forms/AriaAttributes %> />
<input $AttributesHTML $SchemaAttributesHtml <% include SilverStripe/Forms/AriaAttributes %> />
40 changes: 29 additions & 11 deletions tests/php/Forms/UploadFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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' => [
Expand All @@ -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',
Expand All @@ -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);
}
}

0 comments on commit fe5492f

Please sign in to comment.