3 namespace Drupal\inline_entity_form\Tests;
5 use Drupal\simpletest\WebTestBase;
8 * Base Class for Inline Entity Form Tests.
10 abstract class InlineEntityFormTestBase extends WebTestBase {
13 * User with permissions to create content.
15 * @var \Drupal\user\Entity\User
22 * @var \Drupal\Core\Entity\ContentEntityStorageInterface;
24 protected $nodeStorage;
27 * Field config storage.
29 * @var \Drupal\Core\Config\Entity\ConfigEntityStorage
31 protected $fieldStorageConfigStorage;
34 * Field config storage.
36 * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
38 protected $fieldConfigStorage;
43 protected function setUp() {
45 $this->nodeStorage = $this->container->get('entity_type.manager')->getStorage('node');
46 $this->fieldStorageConfigStorage = $this->container->get('entity_type.manager')->getStorage('field_storage_config');
47 $this->fieldConfigStorage = $this->container->get('entity_type.manager')->getStorage('field_config');
52 * Gets IEF button name.
55 * Xpath of the button.
58 * The name of the button.
60 protected function getButtonName($xpath) {
62 /** @var \SimpleXMLElement[] $elements */
63 if ($elements = $this->xpath($xpath)) {
64 foreach ($elements[0]->attributes() as $name => $value) {
65 if ($name == 'name') {
75 * Passes if no node is found for the title.
78 * Node title to check.
82 protected function assertNoNodeByTitle($title, $message = '') {
84 $message = "No node with title: $title";
86 $node = $this->getNodeByTitle($title);
88 $this->assertTrue(empty($node), $message);
92 * Passes if node is found for the title.
95 * Node title to check.
99 protected function assertNodeByTitle($title, $bundle = NULL, $message = '') {
101 $message = "Node with title found: $title";
103 $node = $this->getNodeByTitle($title);
104 if ($this->assertTrue(!empty($node), $message)) {
106 $this->assertEqual($node->bundle(), $bundle, "Node is correct bundle: $bundle");
112 * Ensures that an entity with a specific label exists.
114 * @param string $label
115 * The label of the entity.
116 * @param string $entity_type_id
117 * The entity type ID.
118 * @param string $bundle
119 * (optional) The bundle this entity should have.
121 protected function assertEntityByLabel($label, $entity_type_id = 'node', $bundle = NULL) {
122 $entity_type_manager = \Drupal::entityTypeManager();
123 $entity_type = $entity_type_manager->getDefinition($entity_type_id);
124 $label_key = $entity_type->getKey('label');
125 $bundle_key = $entity_type->getKey('bundle');
127 $query = $entity_type_manager->getStorage($entity_type_id)->getQuery();
128 $query->condition($label_key, $label);
130 if ($bundle && $bundle_key) {
131 $query->condition($bundle_key, $bundle);
134 $result = $query->execute();
135 $this->assertTrue(!empty($result));
139 * Checks for check correct fields on form displays based on exported config
140 * in inline_entity_form_test module.
142 * @param $form_display
143 * The form display to check.
145 protected function checkFormDisplayFields($form_display, $prefix) {
146 $form_display_fields = [
147 'node.ief_test_custom.default' => [
150 '[uid][0][target_id]',
151 '[created][0][value][date]',
152 '[created][0][value][time]',
155 '[positive_int][0][value]',
159 'node.ief_test_custom.inline' => [
162 '[positive_int][0][value]',
165 '[uid][0][target_id]',
166 '[created][0][value][date]',
167 '[created][0][value][time]',
173 if ($fields = $form_display_fields[$form_display]) {
174 $this->assert('debug', 'Checking form dispaly: ' . $form_display);
175 foreach ($fields['expected'] as $expected_field) {
176 $this->assertFieldByName($prefix . $expected_field);
178 foreach ($fields['unexpected'] as $unexpected_field) {
179 $this->assertNoFieldByName($prefix . $unexpected_field, NULL);
183 // Test calling unexported form display if we are here.
184 throw new \Exception('Form display not found: ' . $form_display);