Fix history comparison fields will now show diff properly, rather than escaped html diff

This commit is contained in:
Christopher Joe 2017-02-28 13:39:30 +13:00 committed by Damian Mooyman
parent 18a6fa05e8
commit 2fafff084f
2 changed files with 57 additions and 11 deletions

View File

@ -7,10 +7,12 @@ use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\HTMLReadonlyField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
@ -450,22 +452,17 @@ class CMSPageHistoryController extends CMSMain
$form->setActions(new FieldList());
$form->addExtraClass('compare');
// Comparison views shouldn't be editable.
// Its important to convert fields *before* loading data,
// as the comparison output is HTML and not valid values for the various field types
$readonlyFields = $form->Fields()->makeReadonly();
$form->setFields($readonlyFields);
$form->loadDataFrom($record);
$form->loadDataFrom(array(
"ID" => $id,
"Version" => $fromVersion,
));
foreach ($form->Fields()->dataFields() as $field) {
$field->dontEscape = true;
}
// Comparison views shouldn't be editable.
// As the comparison output is HTML and not valid values for the various field types
$readonlyFields = $this->transformReadonly($form->Fields());
$form->setFields($readonlyFields);
return $form;
}
@ -475,4 +472,19 @@ class CMSPageHistoryController extends CMSMain
$crumbs[0]->Title = _t('CMSPagesController.MENUTITLE', 'Pages');
return $crumbs;
}
public function transformReadonly(FieldList $fields)
{
foreach($fields as &$field) {
if ($field instanceof CompositeField) {
$subfields = $this->transformReadonly($field->FieldList());
$field->setChildren($subfields);
}
if ($field->hasData() && !$field instanceof HiddenField) {
$newField = $field->castedCopy(HTMLReadonlyField::class);
$fields->replaceField($field->getName(), $newField);
}
}
return $fields;
}
}

View File

@ -1,5 +1,10 @@
<?php
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\HTMLReadonlyField;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\Versioning\Versioned;
use SilverStripe\CMS\Controllers\CMSPageHistoryController;
use SilverStripe\Dev\FunctionalTest;
@ -152,4 +157,33 @@ class CMSPageHistoryControllerTest extends FunctionalTest
$this->assertThat($checkbox[0], $this->logicalNot($this->isNull()));
$this->assertEquals('checked', (string) $checkbox[0]->attributes()->checked);
}
public function testTransformReadonly()
{
/** @var CMSPageHistoryController $history */
$history = singleton(CMSPageHistoryController::class);
$fieldList = FieldList::create([
FieldGroup::create('group', [
TextField::create('childField', 'child field'),
]),
TextField::create('field', 'field', 'My <del>value</del><ins>change</ins>'),
HiddenField::create('hiddenField', 'hidden field'),
]);
$newList = $history->transformReadonly($fieldList);
$field = $newList->dataFieldByName('field');
$this->assertTrue($field instanceof HTMLReadonlyField);
$this->assertContains('<ins>', $field->forTemplate());
$groupField = $newList->fieldByName('group');
$this->assertTrue($groupField instanceof FieldGroup);
$childField = $newList->dataFieldByName('childField');
$this->assertTrue($childField instanceof HTMLReadonlyField);
$hiddenField = $newList->dataFieldByName('hiddenField');
$this->assertTrue($hiddenField instanceof HiddenField);
}
}