diff --git a/behat.yml b/behat.yml index 5bf5117e..96670f81 100644 --- a/behat.yml +++ b/behat.yml @@ -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' diff --git a/tests/Behat/Context/FixtureContext.php b/tests/Behat/Context/FixtureContext.php index 564434bd..4b2ced91 100644 --- a/tests/Behat/Context/FixtureContext.php +++ b/tests/Behat/Context/FixtureContext.php @@ -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; @@ -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; + } } diff --git a/tests/Behat/features/inline-block-validation.feature b/tests/Behat/features/inline-block-validation.feature index 63f4d71a..9eed13a8 100644 --- a/tests/Behat/features/inline-block-validation.feature +++ b/tests/Behat/features/inline-block-validation.feature @@ -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" @@ -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 @@ -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 @@ -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 diff --git a/tests/Behat/features/non-inline-block-validation.feature b/tests/Behat/features/non-inline-block-validation.feature index 48eb7d07..03565415 100644 --- a/tests/Behat/features/non-inline-block-validation.feature +++ b/tests/Behat/features/non-inline-block-validation.feature @@ -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 @@ -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 @@ -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 diff --git a/tests/Behat/features/page-save-validation.feature b/tests/Behat/features/page-save-validation.feature index 3bfdd723..e1d2718a 100644 --- a/tests/Behat/features/page-save-validation.feature +++ b/tests/Behat/features/page-save-validation.feature @@ -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" @@ -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 @@ -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 @@ -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 diff --git a/tests/Behat/files/file1.jpg b/tests/Behat/files/file1.jpg new file mode 100644 index 00000000..beb5a91b Binary files /dev/null and b/tests/Behat/files/file1.jpg differ