-
Notifications
You must be signed in to change notification settings - Fork 115
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 Retain value that failed validation on client side #930
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -240,19 +240,27 @@ public function stepIFillInForForBlock($value, $name, $blockNumber) | |
public function theFieldForBlockShouldContain($field, $blockNumber, $negate, $content) | ||
{ | ||
$block = $this->getSpecificBlock($blockNumber); | ||
$field = $this->findFieldInBlock($block, $field); | ||
$isTinyMCE = $field->getAttribute('data-editor') === 'tinyMCE'; | ||
$fieldElem = $this->findFieldInBlock($block, $field); | ||
$isTinyMCE = $fieldElem->getAttribute('data-editor') === 'tinyMCE'; | ||
|
||
if ($isTinyMCE) { | ||
$this->cmsContext->theHtmlFieldShouldContain( | ||
$field->getAttribute('name'), | ||
$fieldElem->getAttribute('name'), | ||
$negate, | ||
$content | ||
); | ||
} elseif ($negate) { | ||
$this->assertFieldNotContains($field, $content); | ||
return; | ||
} | ||
|
||
$actual = (string) $fieldElem->getValue(); | ||
$regex = '/^' . preg_quote($content, '/') . '$/ui'; | ||
|
||
if ($negate) { | ||
$message = sprintf('The field "%s" value is "%s", but "%s" expected.', $field, $actual, $content); | ||
Assert::isTrue((bool) preg_match($regex, $actual), $message); | ||
} else { | ||
$this->assertFieldContains($field, $content); | ||
$message = sprintf('The field "%s" value is "%s", but it should not be.', $field, $actual); | ||
Assert::isFalse((bool) preg_match($regex, $actual), $message); | ||
} | ||
Comment on lines
-252
to
264
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new assertions mirror what |
||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
@javascript | ||
Feature: Don't lose content when page or block is invalid | ||
As a CMS user | ||
I want to retain my unsaved content when a validation error occurs | ||
So that I can fix the content and save it without recreating content | ||
|
||
Background: | ||
Given I add an extension "DNADesign\Elemental\Extensions\ElementalPageExtension" to the "Page" class | ||
And a "page" "Blocks Page" with a "Alice's Block" content element with "original content" content | ||
And the "group" "EDITOR" has permissions "Access to 'Pages' section" | ||
And I am logged in as a member of "EDITOR" group | ||
|
||
# The "unsaved changes" dialog causes errors unless this is tagged with "@unsavedChanges" | ||
@unsavedChanges | ||
Scenario: If a page is invalid, changes aren't lost | ||
Given I add an extension "DNADesign\Elemental\Tests\Src\ValidationFailedExtension" to the "Page" class | ||
And I go to "/admin/pages" | ||
And I left click on "Blocks Page" in the tree | ||
Then I should see a list of blocks | ||
And I should see "Alice's Block" | ||
And I should not see the ".element-editor-header__version-state--unsaved" element | ||
When I click on the caret button for block 1 | ||
And I fill in "<p>New sample content</p>" for "Content" for block 1 | ||
And I fill in "Charlie's Block" for "Title" for block 1 | ||
And I press the "Save" button | ||
Then I should see a "Validation error" error toast | ||
And I should see "Page is invalid" | ||
And I should see the ".element-editor-header__version-state--unsaved" element | ||
When I click on the caret button for block 1 | ||
Then the "Content" field for block 1 should contain "New sample content" | ||
And the "Title" field for block 1 should contain "Charlie's Block" | ||
And I should see the ".element-editor-header__version-state--unsaved" element | ||
|
||
@unsavedChanges | ||
Scenario: If a block is invalid, changes aren't lost | ||
Given I add an extension "DNADesign\Elemental\Tests\Src\ValidationFailedExtension" to the "DNADesign\Elemental\Models\BaseElement" class | ||
And I go to "/admin/pages" | ||
And I left click on "Blocks Page" in the tree | ||
Then I should see a list of blocks | ||
And I should see "Alice's Block" | ||
And I should not see the ".element-editor-header__version-state--unsaved" element | ||
When I click on the caret button for block 1 | ||
And I fill in "<p>New sample content</p>" for "Content" for block 1 | ||
And I fill in "Charlie's Block" for "Title" for block 1 | ||
And I press the "Save" button | ||
Then I should see a "Validation error" error toast | ||
And I should see "ElementContent is invalid" | ||
And I should see the ".element-editor-header__version-state--unsaved" element | ||
When I click on the caret button for block 1 | ||
Then the "Content" field for block 1 should contain "New sample content" | ||
And the "Title" field for block 1 should contain "Charlie's Block" | ||
And I should see the ".element-editor-header__version-state--unsaved" element |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace DNADesign\Elemental\Tests\Src; | ||
|
||
use SilverStripe\Core\ClassInfo; | ||
use SilverStripe\Core\Extension; | ||
use SilverStripe\Dev\TestOnly; | ||
use SilverStripe\ORM\ValidationResult; | ||
|
||
class ValidationFailedExtension extends Extension implements TestOnly | ||
{ | ||
public const INVALID_TITLE_MESSAGE = '%s is invalid'; | ||
|
||
public function validate(ValidationResult $result) | ||
{ | ||
$result->addFieldError('Title', sprintf(static::INVALID_TITLE_MESSAGE, ClassInfo::shortName($this->owner))); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition doesn't explicitly test for anything to see if it's swapping to a new page - but it doesn't need to. This condition evaluates to
true
when navigating to a new page. I've confirmed this under the following scenarios: