From 719a6179efe32d560a765d30303653168a402166 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 8 May 2024 17:57:20 +1200 Subject: [PATCH] MNT Add behat tests for related data --- behat.yml | 2 +- tests/Behat/Context/FixtureContext.php | 52 ++++++++++++++++++ .../features/inline-block-validation.feature | 30 +++++++++- .../non-inline-block-validation.feature | 12 +++- .../features/page-save-validation.feature | 21 ++++++- tests/Behat/files/file1.jpg | Bin 0 -> 2292 bytes 6 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 tests/Behat/files/file1.jpg 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 0000000000000000000000000000000000000000..beb5a91b0aba93d3b54dadc12cff0104307d718e GIT binary patch literal 2292 zcmbV~c|6qJ9>;%UHjExS$z>*csbTDTlq4fO5~U_1OJm>1u4{=xiK_^q9w8LDu4RZU z8557($~v-CGowgKGnQ!>GxN;U>viux_m6w;=e)k(*IB;jygu)9j^K@e2FN&K9k2ih z1OOmm0|b2FS6eqbn=>wMhfslj2ScL#15xo9v>{3_E+#l45ar-#k8(U^XOD_EGun3m zMU3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