Completed
Pull Request — master (#300)
by
unknown
01:31
created

EntityContext::createEntities()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 3
1
<?php
2
3
namespace Drupal\DrupalExtension\Context;
4
5
use Behat\Behat\Context\TranslatableContext;
6
use Behat\Mink\Element\Element;
7
8
use Behat\Gherkin\Node\TableNode;
9
10
/**
11
 * Provides pre-built step definitions for testing any Drupal entity.
12
 */
13
class EntityContext extends RawDrupalContext implements TranslatableContext
14
{
15
16
  /**
17
   * Returns list of definition translation resources paths.
18
   *
19
   * @return array
20
   */
21
  public static function getTranslationResources()
22
  {
23
    return glob(__DIR__ . '/../../../../i18n/*.xliff');
24
  }
25
26
  /**
27
   * @Given I am viewing a/an :entity_type entity/item with the :label_name :label
28
   * @Given I am viewing a/an :bundle :entity_type entity/item with the :label_name :label
29
   */
30
  public function createEntity($entity_type, $bundle = NULL, $label_name, $label)
31
  {
32
    $entity = (object)array(
33
        'step_bundle' => $bundle,
34
        $label_name => $label,
35
    );
36
    $saved = $this->entityCreate($entity_type, $entity);
37
    // Set internal browser on the new entity.
38
    //@todo: use the entity's toUrl()
39
    $this->getSession()->visit($this->locatePath('/' . $entity_type . '/' . $saved->id));
40
  }
41
42
  /**
43
   * Creates content of a given type provided in the form:
44
   * | title    | author     | status | created           |
45
   * | My title | Joe Editor | 1      | 2014-10-17 8:00am |
46
   * | ...      | ...        | ...    | ...               |
47
   *
48
   * @Given a/an :entity_type entity/item:
49
   * @Given :entity_type entities/items:
50
   * @Given a/an :bundle :entity_type entity/item:
51
   * @Given :bundle :entity_type entities/items:
52
   */
53
   public function createEntities($entity_type, $bundle = NULL, TableNode $entitiesTable)
54
   {
55
     foreach ($entitiesTable->getHash() as $entityHash) {
56
       $entity = (object)$entityHash;
57
       if (!isset($entity->step_bundle)) $entity->step_bundle = $bundle;
58
       $this->entityCreate($entity_type, $entity);
59
     }
60
   }
61
62
  /**
63
   * Creates content of the given type, provided in the form:
64
   * | title     | My entity        |
65
   * | Field One | My field value |
66
   * | author    | Joe Editor     |
67
   * | status    | 1              |
68
   * | ...       | ...            |
69
   *
70
   * @Given I am viewing a/an :entity_type entity/item:
71
   * @Given I am viewing a/an :bundle :entity_type entity/item:
72
   */
73
  public function assertViewingEntity($entity_type, $bundle = NULL, TableNode $fields)
74
  {
75
    $entity = (object)array();
76
    foreach ($fields->getRowsHash() as $field => $value) {
77
      $entity->{$field} = $value;
78
    }
79
    if (!isset($entity->step_bundle)) $entity->step_bundle = $bundle;
80
81
    $saved = $this->entityCreate($entity_type, $entity);
82
83
    // Set internal browser on the new entity.
84
    //@todo: use the entity's toUrl()
85
    $this->getSession()->visit($this->locatePath('/' . $entity_type . '/' . $saved->id));
86
  }
87
88
}
89
90