Skip to content

Commit

Permalink
[#282] Updated EckTrait.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Oct 31, 2024
1 parent 92439ee commit 98f8639
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 98 deletions.
6 changes: 6 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
163 changes: 83 additions & 80 deletions src/EckTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

}
32 changes: 18 additions & 14 deletions tests/behat/features/eck.feature
Original file line number Diff line number Diff line change
@@ -1,49 +1,53 @@
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"
"""
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"
"""

0 comments on commit 98f8639

Please sign in to comment.