-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathEntityCreateTest.php
More file actions
150 lines (128 loc) · 5.01 KB
/
EntityCreateTest.php
File metadata and controls
150 lines (128 loc) · 5.01 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* @file
* Contains \Drupal\Tests\rules\Integration\Action\EntityCreateTest.
*/
namespace Drupal\Tests\rules\Integration\Action;
use Drupal\Core\Entity\EntityStorageBase;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\rules\Context\ContextDefinition;
use Drupal\Tests\rules\Integration\RulesEntityIntegrationTestBase;
/**
* @coversDefaultClass \Drupal\rules\Plugin\RulesAction\EntityCreate
* @group rules_actions
*/
class EntityCreateTest extends RulesEntityIntegrationTestBase {
/**
* A constant that will be used instead of an entity.
*/
const ENTITY_REPLACEMENT = 'This is a fake entity';
/**
* The action to be tested.
*
* @var \Drupal\rules\Core\RulesActionInterface
*/
protected $action;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
// Prepare some mocked bundle field definitions. This is needed because
// EntityCreateDeriver adds required contexts for required fields, and
// assumes that the bundle field is required.
$bundle_field_definition = $this->prophesize(BaseFieldDefinition::class);
$bundle_field_definition_optional = $this->prophesize(BaseFieldDefinition::class);
$bundle_field_definition_required = $this->prophesize(BaseFieldDefinition::class);
// The next methods are mocked because EntityCreateDeriver executes them,
// and the mocked field definition is instantiated without the necessary
// information.
$bundle_field_definition->getCardinality()->willReturn(1)
->shouldBeCalled();
$bundle_field_definition->getType()->willReturn('string')
->shouldBeCalled();
$bundle_field_definition->getLabel()->willReturn('Bundle')
->shouldBeCalled();
$bundle_field_definition->getDescription()
->willReturn('Bundle mock description')
->shouldBeCalled();
$bundle_field_definition_required->getCardinality()->willReturn(1)
->shouldBeCalled();
$bundle_field_definition_required->getType()->willReturn('string')
->shouldBeCalled();
$bundle_field_definition_required->getLabel()->willReturn('Required field')
->shouldBeCalled();
$bundle_field_definition_required->getDescription()
->willReturn('Required field mock description')
->shouldBeCalled();
$bundle_field_definition_required->isRequired()
->willReturn(TRUE)
->shouldBeCalled();
$bundle_field_definition_optional->isRequired()
->willReturn(FALSE)
->shouldBeCalled();
// Prepare mocked entity storage.
$entity_type_storage = $this->prophesize(EntityStorageBase::class);
$entity_type_storage->create(['bundle' => 'test', 'field_required' => NULL])
->willReturn(self::ENTITY_REPLACEMENT);
// Return the mocked storage controller.
$this->entityTypeManager->getStorage('test')
->willReturn($entity_type_storage->reveal());
// Return a mocked list of base fields definitions.
$this->entityFieldManager->getBaseFieldDefinitions('test')
->willReturn([
'bundle' => $bundle_field_definition->reveal(),
'field_required' => $bundle_field_definition_required->reveal(),
'field_optional' => $bundle_field_definition_optional->reveal(),
]);
// Instantiate the action we are testing.
$this->action = $this->actionManager->createInstance('rules_entity_create:test');
}
/**
* Tests the summary.
*
* @covers ::summary
*/
public function testSummary() {
$this->assertEquals('Create a new test', $this->action->summary());
}
/**
* Tests the action execution.
*
* @covers ::execute
*/
public function testActionExecution() {
$this->action->setContextValue('bundle', 'test');
$this->action->execute();
$entity = $this->action->getProvidedContext('entity')->getContextValue();
$this->assertEquals(self::ENTITY_REPLACEMENT, $entity);
}
/**
* Tests context definitions for the bundle and required fields.
*
* @covers \Drupal\rules\Plugin\RulesAction\EntityCreateDeriver::getDerivativeDefinitions
*/
public function testRequiredContexts() {
$context_definitions = $this->action->getContextDefinitions();
$this->assertCount(2, $context_definitions);
$this->assertArrayHasKey('bundle', $context_definitions);
$this->assertEquals(ContextDefinition::ASSIGNMENT_RESTRICTION_INPUT, $context_definitions['bundle']->getAssignmentRestriction());
$this->assertTrue($context_definitions['bundle']->isRequired());
$this->assertArrayHasKey('field_required', $context_definitions);
$this->assertNull($context_definitions['field_required']->getAssignmentRestriction());
$this->assertFalse($context_definitions['field_required']->isRequired());
}
/**
* Tests the context refining.
*
* @covers ::refineContextDefinitions
*/
public function testRefiningContextDefinitions() {
$this->action->setContextValue('bundle', 'bundle_test');
$this->action->refineContextDefinitions([]);
$this->assertEquals(
$this->action->getProvidedContextDefinition('entity')
->getDataType(), 'entity:test:bundle_test'
);
}
}