Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / inline_entity_form / src / Tests / InlineEntityFormTestBase.php
1 <?php
2
3 namespace Drupal\inline_entity_form\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Base Class for Inline Entity Form Tests.
9  */
10 abstract class InlineEntityFormTestBase extends WebTestBase {
11
12   /**
13    * User with permissions to create content.
14    *
15    * @var \Drupal\user\Entity\User
16    */
17   protected $user;
18
19   /**
20    * Node storage.
21    *
22    * @var \Drupal\Core\Entity\ContentEntityStorageInterface;
23    */
24   protected $nodeStorage;
25
26   /**
27    * Field config storage.
28    *
29    * @var \Drupal\Core\Config\Entity\ConfigEntityStorage
30    */
31   protected $fieldStorageConfigStorage;
32
33   /**
34    * Field config storage.
35    *
36    * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
37    */
38   protected $fieldConfigStorage;
39
40   /**
41    * {@inheritdoc}
42    */
43   protected function setUp() {
44     parent::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');
48   }
49
50
51   /**
52    * Gets IEF button name.
53    *
54    * @param array $xpath
55    *   Xpath of the button.
56    *
57    * @return string
58    *   The name of the button.
59    */
60   protected function getButtonName($xpath) {
61     $retval = '';
62     /** @var \SimpleXMLElement[] $elements */
63     if ($elements = $this->xpath($xpath)) {
64       foreach ($elements[0]->attributes() as $name => $value) {
65         if ($name == 'name') {
66           $retval = $value;
67           break;
68         }
69       }
70     }
71     return $retval;
72   }
73
74   /**
75    * Passes if no node is found for the title.
76    *
77    * @param $title
78    *   Node title to check.
79    * @param $message
80    *   Message to display.
81    */
82   protected function assertNoNodeByTitle($title, $message = '') {
83     if (!$message) {
84       $message = "No node with title: $title";
85     }
86     $node = $this->getNodeByTitle($title);
87
88     $this->assertTrue(empty($node), $message);
89   }
90
91   /**
92    * Passes if node is found for the title.
93    *
94    * @param $title
95    *   Node title to check.
96    * @param $message
97    *   Message to display.
98    */
99   protected function assertNodeByTitle($title, $bundle = NULL, $message = '') {
100     if (!$message) {
101       $message = "Node with title found: $title";
102     }
103     $node = $this->getNodeByTitle($title);
104     if ($this->assertTrue(!empty($node), $message)) {
105       if ($bundle) {
106         $this->assertEqual($node->bundle(), $bundle, "Node is correct bundle: $bundle");
107       }
108     }
109   }
110
111   /**
112    * Ensures that an entity with a specific label exists.
113    *
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.
120    */
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');
126
127     $query = $entity_type_manager->getStorage($entity_type_id)->getQuery();
128     $query->condition($label_key, $label);
129
130     if ($bundle && $bundle_key) {
131       $query->condition($bundle_key, $bundle);
132     }
133
134     $result = $query->execute();
135     $this->assertTrue(!empty($result));
136   }
137
138   /**
139    * Checks for check correct fields on form displays based on exported config
140    * in inline_entity_form_test module.
141    *
142    * @param $form_display
143    *   The form display to check.
144    */
145   protected function checkFormDisplayFields($form_display, $prefix) {
146     $form_display_fields = [
147       'node.ief_test_custom.default' => [
148         'expected' => [
149           '[title][0][value]',
150           '[uid][0][target_id]',
151           '[created][0][value][date]',
152           '[created][0][value][time]',
153           '[promote][value]',
154           '[sticky][value]',
155           '[positive_int][0][value]',
156         ],
157         'unexpected' => [],
158       ],
159       'node.ief_test_custom.inline' => [
160         'expected' => [
161           '[title][0][value]',
162           '[positive_int][0][value]',
163         ],
164         'unexpected' => [
165           '[uid][0][target_id]',
166           '[created][0][value][date]',
167           '[created][0][value][time]',
168           '[promote][value]',
169           '[sticky][value]',
170         ],
171       ],
172     ];
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);
177       }
178       foreach ($fields['unexpected'] as $unexpected_field) {
179         $this->assertNoFieldByName($prefix . $unexpected_field, NULL);
180       }
181     }
182     else {
183       // Test calling unexported form display if we are here.
184       throw new \Exception('Form display not found: ' . $form_display);
185     }
186   }
187
188 }