Skip to content

Commit

Permalink
Merge pull request #1180 from creative-commoners/pulls/5/inline-submit
Browse files Browse the repository at this point in the history
MNT MNT Add behat tests for related data
  • Loading branch information
GuySartorelli authored May 8, 2024
2 parents 4e0ba30 + 719a617 commit d6c3f22
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 4 deletions.
2 changes: 1 addition & 1 deletion behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ default:
- SilverStripe\BehatExtension\Context\LoginContext
-
DNADesign\Elemental\Tests\Behat\Context\FixtureContext:
- '%paths.modules.silverstripe-elemental%/tests/Behat'
- '%paths.modules.silverstripe-elemental%/tests/Behat/files'
-
SilverStripe\Framework\Tests\Behaviour\ConfigContext:
- '%paths.modules.silverstripe-elemental%/tests/Behat/config'
Expand Down
52 changes: 52 additions & 0 deletions tests/Behat/Context/FixtureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use DNADesign\Elemental\Models\BaseElement;
use DNADesign\Elemental\Models\ElementalArea;
use DNADesign\Elemental\Models\ElementContent;
use PHPUnit\Framework\Assert;
use SilverStripe\CMS\Tests\Behaviour\FixtureContext as BaseFixtureContext;
use SilverStripe\Core\ClassInfo;
use SilverStripe\ORM\DB;
Expand Down Expand Up @@ -111,4 +112,55 @@ protected function getElementalArea(string $type, string $pageTitle): ElementalA

return $page->ElementalArea();
}

/**
* The method is copied from asset-admin SilverStripe\AssetAdmin\Tests\Behat\Context\FixtureContext
* Behat does not seem to allow two different FixtureContext files to be added in the
* same behat.yml config file
*
* Select a gallery item by type and name
*
* @Given /^I (?:(?:click on)|(?:select)) the (?:file|folder) named "([^"]+)" in the gallery$/
* @param string $name
*/
public function stepISelectGalleryItem($name)
{
$item = $this->getGalleryItem($name);
Assert::assertNotNull($item, "File named $name could not be found");
$item->click();
}

/**
* The method is copied from asset-admin SilverStripe\AssetAdmin\Tests\Behat\Context\FixtureContext
* Behat does not seem to allow two different FixtureContext files to be added in the
* same behat.yml config file
*
* Helper for finding items in the visible gallery view
*
* @param string $name Title of item
* @param int $timeout
* @return NodeElement
*/
protected function getGalleryItem($name, $timeout = 3)
{
/** @var DocumentElement $page */
$page = $this->getMainContext()->getSession()->getPage();
// Find by cell
$cell = $page->find(
'xpath',
"//div[contains(@class, 'gallery-item')]//div[contains(text(), '{$name}')]"
);
if ($cell) {
return $cell;
}
// Find by row
$row = $page->find(
'xpath',
"//tr[contains(@class, 'gallery__table-row')]//div[contains(text(), '{$name}')]"
);
if ($row) {
return $row;
}
return null;
}
}
30 changes: 29 additions & 1 deletion tests/Behat/features/inline-block-validation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Feature: Blocks are validated when inline saving individual blocks
Given I add an extension "DNADesign\Elemental\Extensions\ElementalPageExtension" to the "Page" class
And I add an extension "SilverStripe\FrameworkTest\Elemental\Extension\ElementContentExtension" to the "DNADesign\Elemental\Models\ElementContent" class
And I add an extension "SilverStripe\FrameworkTest\Elemental\Extension\NumericFieldExtension" to the "SilverStripe\Forms\NumericField" class
And a "image" "file1.jpg"
And I go to "/dev/build?flush"
And a "page" "Blocks Page" with a "My title" content element with "My content" content
And the "group" "EDITOR" has permissions "Access to 'Pages' section"
Expand All @@ -17,6 +18,10 @@ Feature: Blocks are validated when inline saving individual blocks
And I click on the caret button for block 1
And I click on the "#Form_ElementForm_1_PageElements_1_MyPageID" element
And I click on the ".ss-searchable-dropdown-field__option:nth-of-type(2)" element
And I click "Choose existing" in the ".uploadfield" element
And I press the "Back" HTML field button
And I click on the file named "file1" in the gallery
And I press the "Insert" button
And I press the "View actions" button
And I click on the ".element-editor__actions-save" element

Expand All @@ -31,7 +36,6 @@ Feature: Blocks are validated when inline saving individual blocks
# Will not be an inline save button because formDirty not set yet, intercepted by JS validation
Then I should not see a ".element-editor__actions-save" element

@sboyd
Scenario: Field validation error
When I fill in "x" for "Title" for block 1
When I press the "View actions" button
Expand All @@ -57,6 +61,30 @@ Feature: Blocks are validated when inline saving individual blocks
# Need to save the whole page to stop the alert
And I press the "Save" button

Scenario: Related data validation error with ID suffix (MyPageID)
When I click on the "#Form_ElementForm_1_PageElements_1_MyPageID" element
And I click on the ".ss-searchable-dropdown-field__option:nth-of-type(1)" element
And I press the "View actions" button
And I click on the ".element-editor__actions-save" element
Then I should see "\"My page\" is required" in the ".form__validation-message" element
When I click on the "#Form_ElementForm_1_PageElements_1_MyPageID" element
And I click on the ".ss-searchable-dropdown-field__option:nth-of-type(2)" element
And I press the "View actions" button
And I click on the ".element-editor__actions-save" element
Then I should see a "Saved 'My title' successfully" success toast

Scenario: Related data validation error without ID suffix (MyFile)
When I click on the ".uploadfield-item__remove-btn" element
And I press the "View actions" button
And I click on the ".element-editor__actions-save" element
Then I should see "\"My File\" is required" in the ".form__validation-message" element
When I click "Choose existing" in the ".uploadfield" element
And I click on the file named "file1" in the gallery
And I press the "Insert" button
And I press the "View actions" button
And I click on the ".element-editor__actions-save" element
Then I should see a "Saved 'My title' successfully" success toast

Scenario: Publishing triggers validation error
When I fill in "x" for "Title" for block 1
When I press the "View actions" button
Expand Down
12 changes: 11 additions & 1 deletion tests/Behat/features/non-inline-block-validation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Feature: Blocks are validated when non-inline saving blocks
Given I add an extension "DNADesign\Elemental\Extensions\ElementalPageExtension" to the "Page" class
And I add an extension "SilverStripe\FrameworkTest\Elemental\Extension\ElementContentExtension" to the "DNADesign\Elemental\Models\ElementContent" class
And I add an extension "SilverStripe\FrameworkTest\Elemental\Extension\NumericFieldExtension" to the "SilverStripe\Forms\NumericField" class
And a "image" "file1.jpg"
And content blocks are not in-line editable
And I go to "/dev/build?flush"
And a "page" "Blocks Page" with a "My title" content element with "My content" content
Expand All @@ -19,12 +20,19 @@ Feature: Blocks are validated when non-inline saving blocks

Scenario: Non-inline block validation

# Related has_one RequiredFields
# Related has_one RequiredFields with ID suffix (MyPageID)
When I press the "Save" button
Then I should see "\"My page\" is required" in the "#message-Form_ItemEditForm_MyPageID" element
And I click on the "#Form_ItemEditForm_MyPageID" element
And I click on the ".ss-searchable-dropdown-field__option:nth-of-type(2)" element

# Related has_one RequiredFields without ID suffix (MyFile)
Then I should see "\"My file\" is required" in the "#message-Form_ItemEditForm_MyFile" element
When I click "Choose existing" in the ".uploadfield" element
And I press the "Back" HTML field button
And I click on the file named "file1" in the gallery
And I press the "Insert" button

# RequiredFields on TextCheckboxGroupField (composite) field
When I fill in "Title" with ""
And I press the "Save" button
Expand Down Expand Up @@ -54,3 +62,5 @@ Feature: Blocks are validated when non-inline saving blocks
# Success message
When I press the "Save" button
Then I should see "Saved content block \"My title\"" in the "#Form_ItemEditForm_error" element
Then I should see "Home" in the "#Form_ItemEditForm_MyPageID" element
And I should see "file1" in the ".uploadfield-item__title" element
21 changes: 20 additions & 1 deletion tests/Behat/features/page-save-validation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Feature: Blocks are validated when page saving blocks
Given I add an extension "DNADesign\Elemental\Extensions\ElementalPageExtension" to the "Page" class
And I add an extension "SilverStripe\FrameworkTest\Elemental\Extension\ElementContentExtension" to the "DNADesign\Elemental\Models\ElementContent" class
And I add an extension "SilverStripe\FrameworkTest\Elemental\Extension\NumericFieldExtension" to the "SilverStripe\Forms\NumericField" class
And a "image" "file1.jpg"
And I go to "/dev/build?flush"
And a "page" "Blocks Page" with a "My title" content element with "My content" content
And the "group" "EDITOR" has permissions "Access to 'Pages' section"
Expand All @@ -17,10 +18,16 @@ Feature: Blocks are validated when page saving blocks
And I click on the caret button for block 1
And I click on the "#Form_ElementForm_1_PageElements_1_MyPageID" element
And I click on the ".ss-searchable-dropdown-field__option:nth-of-type(2)" element
And I click "Choose existing" in the ".uploadfield" element
And I press the "Back" HTML field button
And I click on the file named "file1" in the gallery
And I press the "Insert" button
And I press the "Save" button
And I click on the caret button for block 1

Scenario: Validation when page saving inline blocks

# Related has_one RequiredFields
# Related has_one RequiredFields with ID suffix (MyPageID)
When I click on the "#Form_ElementForm_1_PageElements_1_MyPageID" element
And I click on the ".ss-searchable-dropdown-field__option:nth-of-type(1)" element
And I press the "Save" button
Expand All @@ -29,6 +36,13 @@ Feature: Blocks are validated when page saving blocks
And I click on the "#Form_ElementForm_1_PageElements_1_MyPageID" element
And I click on the ".ss-searchable-dropdown-field__option:nth-of-type(2)" element

# Related has_one RequiredFields without ID suffix (MyFile)
When I click on the ".uploadfield-item__remove-btn" element
And I press the "Save" button
Then I should see "\"My file\" is required" in the "#Form_EditForm_error" element
# Old file will be selected at this point, so don't need to reselect
And I click on the caret button for block 1

# FormField::validate()
And I fill in "1" for "My Int" for block 1
And I press the "Save" button
Expand Down Expand Up @@ -58,3 +72,8 @@ Feature: Blocks are validated when page saving blocks
# Success message
When I press the "Save" button
Then I should see a "Saved 'Blocks Page' successfully." success toast

# Validate that related data saved correctly
When I click on the caret button for block 1
Then I should see "Home" in the "#Form_ElementForm_1_PageElements_1_MyPageID" element
And I should see "file1" in the ".uploadfield-item__title" element
Binary file added tests/Behat/files/file1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d6c3f22

Please sign in to comment.