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

API Rename FormField Value to getFormattedValue #385

Open
wants to merge 1 commit into
base: 3.0
Choose a base branch
from
Open
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
14 changes: 7 additions & 7 deletions src/Forms/DiffField.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public function getComparisonField()
return $this->comparisonField;
}

public function Value()
public function getFormattedValue(): mixed
{
$oldValue = $this->getOutdatedField()->Value();
$newValue = $this->getComparisonField()->Value();
$oldValue = $this->getOutdatedField()->getFormattedValue();
$newValue = $this->getComparisonField()->getFormattedValue();

// Objects can't be diffed
if (is_object($oldValue) || is_object($newValue)) {
Expand All @@ -58,7 +58,7 @@ public function Value()
}
// Ensure that the emtpy placeholder value is not escaped
// The empty placeholder value is usually going to be `<i>('none')</i>`
$emptyPlaceholder = ReadonlyField::create('na')->Value();
$emptyPlaceholder = ReadonlyField::create('na')->getFormattedValue();
if ($oldValue === $newValue && $newValue === $emptyPlaceholder) {
// if both the old and new valus are empty, let the surronding <i> tags render as HTML (escape = false)
$escape = false;
Expand Down Expand Up @@ -102,14 +102,14 @@ public function getSchemaDataDefaults()

public function getSchemaStateDefaults()
{
$fromValue = $this->getOutdatedField()->Value();
$fromValue = $this->getOutdatedField()->getFormattedValue();
$toField = $this->getComparisonField();
$toValue = $toField->Value();
$toValue = $toField->getFormattedValue();

$state = array_merge(
$toField->getSchemaStateDefaults(),
parent::getSchemaStateDefaults(),
['value' => $this->Value()]
['value' => $this->getFormattedValue()]
);
$state['data']['diff'] = [
'from' => $fromValue,
Expand Down
68 changes: 31 additions & 37 deletions tests/Forms/DiffFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testScalarValuesAreDiffed()
$diffField->setComparisonField($newField);
$diffField->setValue('old');

$this->assertMatchesRegularExpression('/^<del>old<\/del> *<ins>new<\/ins>$/', $diffField->Value());
$this->assertMatchesRegularExpression('/^<del>old<\/del> *<ins>new<\/ins>$/', $diffField->getFormattedValue());
}

/**
Expand All @@ -36,7 +36,7 @@ public function testObjectValuesAreNotDiffed()
$diffField->setComparisonField($newField);
$diffField->setValue(ManyManyList::create(Group::class, 'Group_Members', 'GroupID', 'MemberID'));

$this->assertEquals('(No diff available)', $diffField->Value());
$this->assertEquals('(No diff available)', $diffField->getFormattedValue());
}

#[DataProvider('provideEscaping')]
Expand All @@ -45,59 +45,53 @@ public function testEscaping(
string $oldValue,
string $newValue,
string $expected,
string $message
) {
// get $emptyPlaceholder here instead of provideEscaping to prevent
// BadMethodCallException: No injector manifests available
// error in dataProvider method
$emptyPlaceholder = ReadonlyField::create('na')->Value();
$emptyPlaceholder = ReadonlyField::create('na')->getFormattedValue();
$emptyPlaceholderNoTags = strip_tags($emptyPlaceholder);
$expected = str_replace('$emptyPlaceholderNoTags', $emptyPlaceholderNoTags, $expected);
$expected = str_replace('$emptyPlaceholder', $emptyPlaceholder, $expected);
$newField = new $className('Test', 'Test', $oldValue);
$newField = new $className('Test', 'Test', $newValue);
$diffField = DiffField::create('DiffTest');
$diffField->setComparisonField($newField);
$diffField->setValue($newValue);
$this->assertSame($expected, $diffField->Value(), $message);
$diffField->setValue($oldValue);
$this->assertSame($expected, $diffField->getFormattedValue());
}

public static function provideEscaping()
{
return [
[
ReadonlyField::class,
'Something',
'Something <strong>bold</strong>',
'Something <del>&lt;strong&gt; bold &lt;/strong&gt;</del>',
'Non HTML field is escaped'
'readonly-add-bold' => [
'className' => ReadonlyField::class,
'oldValue' => 'Something',
'newValue' => 'Something <strong>bold</strong>',
'expected' => 'Something <ins>&lt;strong&gt; bold &lt;/strong&gt;</ins>',
Comment on lines -68 to +70
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did the expected value of this test case change? Haven't looked at the others, I assume something has gone awry.

],
[
HTMLEditorField_Readonly::class,
'Something',
'Something <strong>bold</strong>',
'Something <del><strong>bold</strong></del>',
'Non HTML field is not escaped'
'htmleditor-readonly' => [
'className' => HTMLEditorField_Readonly::class,
'oldValue' => 'Something',
'newValue' => 'Something <strong>bold</strong>',
'expected' => 'Something <ins><strong>bold</strong></ins>',
],
[
ReadonlyField::class,
'',
'',
'$emptyPlaceholder',
'No value is not escaped'
'readonly-nothing' => [
'className' => ReadonlyField::class,
'oldValue' => '',
'newValue' => '',
'expected' => "<i>('none')</i>",
],
[
ReadonlyField::class,
'',
'Something',
'<del>Something</del> <ins>$emptyPlaceholderNoTags</ins>',
'No value is escaped without tags removed when value added'
'readonly-add-something' => [
'className' => ReadonlyField::class,
'oldValue' => '',
'newValue' => 'Something',
'expected' => "<del>('none')</del> <ins>Something</ins>",
],
[
ReadonlyField::class,
'Something',
'',
'<del>$emptyPlaceholderNoTags</del> <ins>Something</ins>',
'No value is escaped without tags removed when value removed'
'readonly-remove-something' => [
'className' => ReadonlyField::class,
'oldValue' => 'Something',
'newValue' => '',
'expected' => "<del>Something</del> <ins>('none')</ins>",
],
];
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Forms/DiffTransformationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public function testTransform()
$form->loadDataFrom($oldData);

foreach ($form->Fields() as $index => $field) {
$this->assertStringContainsString($expected[$index]['before'], $field->Value(), 'Value before is shown');
$this->assertStringContainsString($expected[$index]['after'], $field->Value(), 'Value after is shown');
$this->assertStringContainsString($expected[$index]['before'], $field->getFormattedValue());
$this->assertStringContainsString($expected[$index]['after'], $field->getFormattedValue());
}
}

Expand All @@ -87,8 +87,8 @@ public function testTransformWithCompositeFields()
$form->loadDataFrom($oldData);

foreach (array_values($form->Fields()->dataFields() ?? []) as $index => $field) {
$this->assertStringContainsString($expected[$index]['before'], $field->Value(), 'Value before is shown');
$this->assertStringContainsString($expected[$index]['after'], $field->Value(), 'Value after is shown');
$this->assertStringContainsString($expected[$index]['before'], $field->getFormattedValue());
$this->assertStringContainsString($expected[$index]['after'], $field->getFormattedValue());
}
}

Expand Down
Loading