From 98f8639760e2eb6e93ceb1f096283db336c52709 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Thu, 31 Oct 2024 10:56:21 +1100 Subject: [PATCH] [#282] Updated `EckTrait`. --- MIGRATION.md | 6 ++ README.md | 8 +- src/EckTrait.php | 163 ++++++++++++++++--------------- tests/behat/features/eck.feature | 32 +++--- 4 files changed, 111 insertions(+), 98 deletions(-) diff --git a/MIGRATION.md b/MIGRATION.md index 0975a71..1e67d22 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -7,6 +7,12 @@ A migration map of the step definitions available in v2 to v3. | **[`DraggableViewsTrait`](src/DraggableViewsTrait.php) ([example](tests/behat/features/draggableviews.feature))** | | | `Then I save draggable views :view_id view :views_display_id display :bundle items in the following order:` | `When I save the draggable views items of the view :view_id and the display :views_display_id for the :bundle content in the following order:` | |   | | +| **[`EckTrait`](src/EckTrait.php) ([example](tests/behat/features/eck.feature))** | | +| `Given :bundle :entity_type entities:` | `Given the following eck :bundle :entity_type entities exist:` | +| `Given no :bundle :entity_type entities:` | `Given the following eck :bundle :entity_type entities do not exist:` | +| `When I edit :bundle :entity_type with title :label` | `When I edit eck :bundle :entity_type entity with the title :title` | +| `When I visit :bundle :entity_type with title :label` | `When I visit eck :bundle :entity_type entity with the title :title` | +|   | | | **[`KeyboardTrait`](src/KeyboardTrait.php) ([example](tests/behat/features/keyboard.feature))** | | | `Given I press the :keys keys` | `When I press the keys :keys` | | `Given I press the :keys keys on :selector` | `When I press the keys :keys on the element :selector` | diff --git a/README.md b/README.md index 2588957..6dc53fc 100644 --- a/README.md +++ b/README.md @@ -87,10 +87,10 @@ For migration from v2 to v3, see [MIGRATION.md](MIGRATION.md). | `When I save the draggable views items of the view :view_id and the display :views_display_id for the :bundle content in the following order:` | Save the order of the draggable items. | |   | | | **[`EckTrait`](src/EckTrait.php) ([example](tests/behat/features/eck.feature))** | | -| `Given :bundle :entity_type entities:` | Create ECK entities. | -| `Given no :bundle :entity_type entities:` | Remove custom entities by field. | -| `When I edit :bundle :entity_type with title :label` | Navigate to the edit page for the specified ECK entity type and title. | -| `When I visit :bundle :entity_type with title :label` | Navigate to the view page for the specified ECK entity type and title. | +| `Given the following eck :bundle :entity_type entities exist:` | Create ECK entities. | +| `Given the following eck :bundle :entity_type entities do not exist:` | Remove custom entities by field. | +| `When I edit eck :bundle :entity_type entity with the title :title` | Navigate to the edit page for the specified ECK entity type and title. | +| `When I visit eck :bundle :entity_type entity with the title :title` | Navigate to the view page for the specified ECK entity type and title. | |   | | | **[`ElementTrait`](src/ElementTrait.php) ([example](tests/behat/features/element.feature))** | | | `Then I( should) see the :selector element with the :attribute attribute set to :value` | Assert that an element with the specified selector and attribute value exists. | diff --git a/src/EckTrait.php b/src/EckTrait.php index 1787327..f41bcab 100644 --- a/src/EckTrait.php +++ b/src/EckTrait.php @@ -24,15 +24,45 @@ trait EckTrait { */ protected $eckEntities = []; + /** + * Remove ECK types and entities. + * + * @AfterScenario + */ + public function eckEntitiesCleanAll(AfterScenarioScope $scope): void { + // Allow to skip this by adding a tag. + if ($scope->getScenario()->hasTag('behat-steps-skip:' . __FUNCTION__)) { + return; + } + + $entity_ids_by_type = []; + foreach ($this->eckEntities as $entity_type => $content_entities) { + /** @var \Drupal\eck\EckEntityInterface $content_entity */ + foreach ($content_entities as $content_entity) { + $entity_ids_by_type[$entity_type][] = $content_entity->id(); + } + } + + foreach ($entity_ids_by_type as $entity_type => $entity_ids) { + $controller = \Drupal::entityTypeManager()->getStorage($entity_type); + $entities = $controller->loadMultiple($entity_ids); + $controller->delete($entities); + } + + $this->eckEntities = []; + } + /** * Create eck entities. * - * Provide entity data in the following format: + * @code + * Given the following eck "contact" "contact_type" entities exist: * | title | field_marine_animal | field_fish_type | ... | * | Snook | Fish | Marine fish | 10 | - * | ... | ... | ... | ... | + * | ... | ... | ... | ... | + * @endcode * - * @Given :bundle :entity_type entities: + * @Given the following eck :bundle :entity_type entities exist: */ public function eckEntitiesCreate(string $bundle, string $entity_type, TableNode $table): void { $filtered_table = TableNode::fromList($table->getColumn(0)); @@ -44,16 +74,17 @@ public function eckEntitiesCreate(string $bundle, string $entity_type, TableNode /** * Remove custom entities by field. * - * Provide custom entity data in the following format: - * + * @code + * Given the following eck "contact" "contact_type" entities do not exist: * | field | value | * | field_a | Entity label | + * @endcode * - * @Given no :bundle :entity_type entities: + * @Given the following eck :bundle :entity_type entities do not exist: */ public function eckDeleteEntities(string $bundle, string $entity_type, TableNode $table): void { - foreach ($table->getHash() as $nodeHash) { - $entity_ids = $this->eckLoadMultiple($entity_type, $bundle, $nodeHash); + foreach ($table->getHash() as $node_hash) { + $entity_ids = $this->eckLoadMultiple($entity_type, $bundle, $node_hash); $controller = \Drupal::entityTypeManager()->getStorage($entity_type); $entities = $controller->loadMultiple($entity_ids); @@ -64,31 +95,57 @@ public function eckDeleteEntities(string $bundle, string $entity_type, TableNode } /** - * Remove ECK types and entities. + * Navigate to view entity page with specified type and title. * - * @AfterScenario + * @code + * When I visit eck "contact" "contact_type" entity with the title "Test contact" + * @endcode + * + * @When I visit eck :bundle :entity_type entity with the title :title */ - public function eckEntitiesCleanAll(AfterScenarioScope $scope): void { - // Allow to skip this by adding a tag. - if ($scope->getScenario()->hasTag('behat-steps-skip:' . __FUNCTION__)) { - return; - } + public function eckVisitEntityPageWithTitle(string $bundle, string $entity_type, string $title): void { + $entity_type_manager = \Drupal::entityTypeManager(); + $entity_ids = $this->eckLoadMultiple($entity_type, $bundle, [ + 'title' => $title, + ]); - $entity_ids_by_type = []; - foreach ($this->eckEntities as $entity_type => $content_entities) { - /** @var \Drupal\eck\EckEntityInterface $content_entity */ - foreach ($content_entities as $content_entity) { - $entity_ids_by_type[$entity_type][] = $content_entity->id(); - } + if (empty($entity_ids)) { + throw new \RuntimeException(sprintf('Unable to find "%s" page "%s"', $entity_type, $title)); } - foreach ($entity_ids_by_type as $entity_type => $entity_ids) { - $controller = \Drupal::entityTypeManager()->getStorage($entity_type); - $entities = $controller->loadMultiple($entity_ids); - $controller->delete($entities); + $entity_id = current($entity_ids); + $entity = $entity_type_manager->getStorage($entity_type)->load($entity_id); + $path = $entity->toUrl('canonical')->toString(); + print $path; + + $this->getSession()->visit($path); + } + + /** + * Navigate to edit eck entity page with specified type and title. + * + * @code + * @When I edit eck "contact" "contact_type" entity with the title "Test contact" + * @endcode + * + * @When I edit eck :bundle :entity_type entity with the title :title + */ + public function eckEditEntityWithTitle(string $bundle, string $entity_type, string $title): void { + $entity_type_manager = \Drupal::entityTypeManager(); + $entity_ids = $this->eckLoadMultiple($entity_type, $bundle, [ + 'title' => $title, + ]); + + if (empty($entity_ids)) { + throw new \RuntimeException(sprintf('Unable to find "%s" page "%s"', $entity_type, $title)); } - $this->eckEntities = []; + $entity_id = current($entity_ids); + $entity = $entity_type_manager->getStorage($entity_type)->load($entity_id); + $path = $entity->toUrl('edit-form')->toString(); + print $path; + + $this->getSession()->visit($path); } /** @@ -147,58 +204,4 @@ protected function eckCreateEntity(string $entity_type, \StdClass $entity): void } } - /** - * Navigate to edit eck entity page with specified type and title. - * - * @code - * When I edit "contact" "contact_type" with title "Test contact" - * @endcode - * - * @When I edit :bundle :entity_type with title :label - */ - public function eckEditEntityWithTitle(string $bundle, string $entity_type, string $label): void { - $entity_type_manager = \Drupal::entityTypeManager(); - $entity_ids = $this->eckLoadMultiple($entity_type, $bundle, [ - 'title' => $label, - ]); - - if (empty($entity_ids)) { - throw new \RuntimeException(sprintf('Unable to find %s page "%s"', $entity_type, $label)); - } - - $entity_id = current($entity_ids); - $entity = $entity_type_manager->getStorage($entity_type)->load($entity_id); - $path = $entity->toUrl('edit-form')->toString(); - print $path; - - $this->getSession()->visit($path); - } - - /** - * Navigate to view entity page with specified type and title. - * - * @code - * When I visit "contact" "contact_type" with title "Test contact" - * @endcode - * - * @When I visit :bundle :entity_type with title :label - */ - public function eckVisitEntityPageWithTitle(string $bundle, string $entity_type, string $label): void { - $entity_type_manager = \Drupal::entityTypeManager(); - $entity_ids = $this->eckLoadMultiple($entity_type, $bundle, [ - 'title' => $label, - ]); - - if (empty($entity_ids)) { - throw new \RuntimeException(sprintf('Unable to find %s page "%s"', $entity_type, $label)); - } - - $entity_id = current($entity_ids); - $entity = $entity_type_manager->getStorage($entity_type)->load($entity_id); - $path = $entity->toUrl('canonical')->toString(); - print $path; - - $this->getSession()->visit($path); - } - } diff --git a/tests/behat/features/eck.feature b/tests/behat/features/eck.feature index f515cef..4a5cbdc 100644 --- a/tests/behat/features/eck.feature +++ b/tests/behat/features/eck.feature @@ -1,43 +1,47 @@ Feature: Check that EckTrait works Background: - Given no test_bundle test_entity_type entities: + Given the following eck "test_bundle" "test_entity_type" entities do not exist: | title | | [TEST] ECK Entity | And "tags" terms: | name | | T2 | - And test_bundle test_entity_type entities: + And the following eck "test_bundle" "test_entity_type" entities exist: | title | field_test_text | field_test_reference | | [TEST] ECK test1 | Test text field | T2 | @api - Scenario: Assert "When I edit :bundle :entity_type with title :label" + Scenario: Assert "I visit eck :bundle :entity_type entity with the title :title" works as expected. Given I am logged in as a user with the "administrator" role - When I edit test_bundle test_entity_type with title "[TEST] ECK test1" - Then I should see "Edit test bundle [TEST] ECK test1" - And I visit test_bundle test_entity_type with title "[TEST] ECK test1" - And I should see "[TEST] ECK test1" + When I visit eck "test_bundle" "test_entity_type" entity with the title "[TEST] ECK Entity" + Then I should see "[TEST] ECK test1" And I should see "T2" @api @trait:EckTrait - Scenario: Assert navigate to entity type with specified bundle and title. + Scenario: Assert navigate "I visit eck :bundle :entity_type entity with the title :title" works as expected. Given some behat configuration - And scenario steps tagged with "@api": + And scenario steps: """ Given I am logged in as a user with the "administrator" role - When I visit "test_bundle" "test_entity_type" with title "[Test] Entity Custom" + And I visit eck "test_bundle" "test_entity_type" entity with the title "[TEST] ECK Entity non-existing" """ When I run "behat --no-colors" Then it should fail with an exception: """ - Unable to find test_entity_type page "[Test] Entity Custom" + Unable to find "test_entity_type" page "[TEST] ECK Entity non-existing" """ + @api + Scenario: Assert "When I edit eck :bundle :entity_type entity with the title :title" works as expected. + Given I am logged in as a user with the "administrator" role + When I edit eck "contact" "contact_type" entity with the title "Test contact" + Then I should see "Edit test bundle [TEST] ECK test1" + @api @trait:EckTrait - Scenario: Assert edit to entity type with specified bundle and title. + Scenario: Assert negative "When I edit eck :bundle :entity_type entity with the title :title" works as expected. Given some behat configuration - And scenario steps tagged with "@api": + And scenario steps: """ Given I am logged in as a user with the "administrator" role When I edit "test_bundle" "test_entity_type" with title "[Test] Entity Custom" @@ -45,5 +49,5 @@ Feature: Check that EckTrait works When I run "behat --no-colors" Then it should fail with an exception: """ - Unable to find test_entity_type page "[Test] Entity Custom" + Unable to find "test_entity_type" page "[Test] Entity Custom" """