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

[4.0] Fix for AccessibleMediaField #29840

Merged
merged 6 commits into from
Jul 1, 2020
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
45 changes: 30 additions & 15 deletions libraries/src/Form/Field/AccessiblemediaField.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,40 @@ public function __set($name, $value)
*/
public function setup(\SimpleXMLElement $element, $value, $group = null)
{
json_decode($value);

// Check if value is a valid JSON string.
if ($value !== '' && json_last_error() !== JSON_ERROR_NONE)
/**
* If the value is not a string, it is
* most likely within a custom field of type subform
* and the value is a stdClass with properties
* imagefile and alt_text. So it is fine.
*/
if (\is_string($value))
{
/**
* If the value is not empty and is not a valid JSON string,
* it is most likely a custom field created in Joomla 3 and
* the value is a string that contains the file name.
*/
if (file_exists(JPATH_ROOT . '/' . $value))
{
$value = '{"imagefile":"' . $value . '","alt_text":""}';
}
else
json_decode($value);

// Check if value is a valid JSON string.
if ($value !== '' && json_last_error() !== JSON_ERROR_NONE)
{
$value = '';
/**
* If the value is not empty and is not a valid JSON string,
* it is most likely a custom field created in Joomla 3 and
* the value is a string that contains the file name.
*/
if (file_exists(JPATH_ROOT . '/' . $value))
{
$value = '{"imagefile":"' . $value . '","alt_text":""}';
}
else
{
$value = '';
}
}
}
elseif (!is_object($value)
|| !property_exists($value, 'imagefile')
|| !property_exists($value, 'alt_text'))
{
return false;
}

if (!parent::setup($element, $value, $group))
{
Expand Down
2 changes: 1 addition & 1 deletion plugins/fields/media/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function onCustomFieldsBeforePrepareField($context, $item, $field)
}

// Check if the field value is an old (string) value
$field->apivalue = $this->checkValue($field->value);
$field->value = $this->checkValue($field->value);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion plugins/fields/media/tmpl/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
$class = ' class="' . htmlentities($class, ENT_COMPAT, 'UTF-8', true) . '"';
}

$value = $field->apivalue;
$value = $field->value;

$buffer = '';

if ($value)
Expand Down
50 changes: 50 additions & 0 deletions tests/Unit/Libraries/Cms/Form/Field/AccessiblemediaFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,56 @@ public function testSetupWithValueThatIsNotValid()
$this->assertEquals('', $accessiblemediafield->__get('value'));
}

/**
* Tests method to attach a Form object to the field.
* If the value is a stdClass that is valid.
* This does change nothing. The value should be the same than before.

* @return void
*
* @since __DEPLOY_VERSION__
*/
public function testSetupWithValueThatIsValidStdclass()
{
$accessiblemediafield = $this->createAccessiblemediaField();

$obj = new \stdClass;
$obj->imagefile = '/images/joomla_black.png';
$obj->alt_text = 'some alt text';

$element = new \SimpleXMLElement('<field name="testfield" />');

$this->assertTrue($accessiblemediafield->setup($element, $obj, null));
$this->assertEquals($obj, $accessiblemediafield->__get('value'));
}

/**
* Tests method to attach a Form object to the field.
* If the value is a stdClass that is not valid for the accessible media field.
* This does change in the setup method. Result is false.

* @return void
*
* @since __DEPLOY_VERSION__
*/
public function testSetupWithValueThatIsNoValidStdclass()
{
$accessiblemediafield = $this->createAccessiblemediaField();

$obj1 = new \stdClass;
$obj2 = new \stdClass;
$obj3 = new \stdClass;

$obj1->imagefile = '/images/joomla_black.png';
$obj2->alt_text = 'some alt text';

$element = new \SimpleXMLElement('<field name="testfield" />');

$this->assertFalse($accessiblemediafield->setup($element, $obj1, null));
$this->assertFalse($accessiblemediafield->setup($element, $obj2, null));
$this->assertFalse($accessiblemediafield->setup($element, $obj3, null));
}

/**
* Tests method to attach a Form object to the field.
* If the SimpleXMLElement is not of the field type, the setup method returns false.
Expand Down