-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoe_paragraphs.behat.inc
85 lines (70 loc) · 3.19 KB
/
oe_paragraphs.behat.inc
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* @file
* Contains \OeParagraphsSubContext.
*/
declare(strict_types=1);
use Behat\Gherkin\Node\TableNode;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\DrupalExtension\Context\DrupalSubContextBase;
use Drupal\oe_paragraphs\Traits\EntityFieldTrait;
use Drupal\oe_paragraphs\Traits\EntityReferenceTrait;
use Drupal\oe_paragraphs\Traits\EntityTrait;
use PHPUnit\Framework\Assert;
/**
* Behat step definitions for testing the OE Paragraphs module.
*/
class OeParagraphsSubContext extends DrupalSubContextBase {
use EntityFieldTrait;
use EntityReferenceTrait;
use EntityTrait;
/**
* Checks that the given entity bundle supports the given paragraph types.
*
* If there are multiple fields that reference paragraph types, only the first
* field will be checked.
*
* @param string $entity_type_label
* The human readable label of the entity type to check.
* @param string $bundle_label
* The human readable label of the bundle to check.
* @param \Behat\Gherkin\Node\TableNode $table
* A table with a single column containing the human readable labels of the
* paragraph types that are expected to be present in the bundle.
*
* @Then the following paragraph types are available for :bundle_label :entity_type_label:
*/
public function assertParagraphTypesExistInEntityBundle(string $entity_type_label, string $bundle_label, TableNode $table): void {
// Retrieve the machine names of the entity type and bundle.
$entity_type_id = $this->getEntityTypeIdByLabel($entity_type_label);
$bundle_id = $this->getBundleIdByLabel($entity_type_id, $bundle_label);
// Retrieve the machine names of the paragraph types.
$exp_par_type_ids = array_map(function ($label) {
return $this->getEntityIdByLabel('paragraphs_type', $label);
}, $table->getColumn(0));
// Create a new entity of the given type and bundle so we can verify if it
// references the given paragraph types.
$entity = $this->createNewEntity($entity_type_id, $bundle_id);
// Don't bother continuing the test if this is a config entity.
if (!$entity instanceof ContentEntityInterface) {
throw new \RuntimeException("The '$entity_type_id' entity type cannot reference paragraph types since it is not a content entity.");
}
$field_names = $this->getFieldNamesByFieldType($entity_type_id, $bundle_id, 'entity_reference_revisions');
foreach ($field_names as $field_name) {
// Only check fields that reference paragraph types.
if ($this->getEntityReferenceTargetEntityId($entity, $field_name) !== 'paragraph') {
continue;
};
$actual_par_type_ids = $this->getEntityReferenceTargetBundles($entity, $field_name);
// Check if the target paragraph types match the expected ones.
sort($actual_par_type_ids);
sort($exp_par_type_ids);
Assert::assertEquals($exp_par_type_ids, $actual_par_type_ids);
// We found the field and the paragraph types match the expectation, so
// we're done.
return;
}
// No field was found that references paragraph types.
throw new \RuntimeException("The '$bundle_id' bundle of the '$entity_type_id' entity does not reference any paragraph types.");
}
}