Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Avoid infinite recursive loop with attributes in schemadata #1510

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading