public
Last active

Step-definition for complex node structure (field collection + entity reference).

  • Download Gist
FeatureContext.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
<?php
/**
* @Given /^I am viewing a product with the following related products:$/
*/
public function assertRelatedProducts(TableNode $relatedProducts) {
// First, create a product.
$product = (object) array(
'title' => 'Parent Product',
'type' => 'product',
'uid' => 1,
);
$product = $this->getDriver()->createNode($product);
 
// Create and reference related nodes.
foreach ($relatedProducts->getHash() as $relatedProduct) {
$relatedProduct = (object) $relatedProduct;
$relatedProduct->type = 'product';
$saved = $this->getDriver()->createNode($relatedProduct);
$this->nodes[] = $saved;
 
// Create field collection item.
$fieldCollectionItem = entity_create('field_collection_item', array('field_name' => 'field_related_products'));
$fieldCollectionItem->setHostEntity('node', $product);
$fieldCollectionItem->field_product_reference[LANGUAGE_NONE][0]['target_id'] = $saved->nid;
$fieldCollectionItem->field_is_component[LANGUAGE_NONE][0]['value'] = $relatedProduct->component;
$fieldCollectionItem->save();
}
 
// Resave product node.
node_save($product);
$this->nodes[] = $product;
 
// Set internal page on the product.
$this->getSession()->visit($this->locatePath('/node/' . $product->nid));
}
}
foo.feature
Cucumber
1 2 3 4 5 6 7 8 9 10 11 12
Scenario: Related products and components
Given I am viewing a product with the following related products:
| title | component |
| Related product 1 | 0 |
| Related product 2 | 0 |
| Related component 1 | 1 |
| Related component 2 | 1 |
Then I should see the heading "Related Products"
And I should see the link "Related product 1"
And I should see the link "Related product 2"
And I should see the link "Related component 1"
And I should see the link "Related component 2"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.