-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathEntitySaveTest.php
More file actions
163 lines (135 loc) · 5.24 KB
/
EntitySaveTest.php
File metadata and controls
163 lines (135 loc) · 5.24 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
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* @file
* Contains \Drupal\Tests\rules\Integration\Action\EntitySaveTest.
*/
namespace Drupal\Tests\rules\Integration\Action;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\TypedData\EntityDataDefinition;
use Drupal\Core\Entity\EntityStorageBase;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Tests\rules\Integration\RulesEntityIntegrationTestBase;
/**
* @coversDefaultClass \Drupal\rules\Plugin\RulesAction\EntitySave
* @group rules_actions
*/
class EntitySaveTest extends RulesEntityIntegrationTestBase {
/**
* The action to be tested.
*
* @var \Drupal\rules\Core\RulesActionInterface
*/
protected $action;
/**
* The mocked entity used for testing.
*
* @var \Drupal\Core\Entity\EntityInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $entity;
/**
* A constant that will be used instead of an entity.
*/
const ENTITY_REPLACEMENT = 'This is a fake entity';
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->entity = $this->prophesizeEntity(EntityInterface::class);
// 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.
$entity_definition = $this->prophesize(EntityDataDefinition::class);
$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 EntitySaveDeriver 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->save(['entity' => $this->entity, '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_save:test');
}
/**
* Tests the summary.
*
* @covers ::summary
*/
public function testSummary() {
$this->assertEquals('Save entity', $this->action->summary());
}
/**
* Tests the action execution.
*
* @covers ::execute
*/
public function testActionExecution() {
$this->entity->save()->shouldBeCalledTimes(1);
// @todo Exception: The entity context is not a valid context.
$this->action->setContextValue('entity', $this->entity->reveal())
->setContextValue('immediate', TRUE);
$this->action->execute();
$entity = $this->action->getProvidedContext('entity')->getContextValue();
$this->assertEquals(self::ENTITY_REPLACEMENT, $entity);
}
/**
* Tests the action execution when saving immediately.
*
* @covers ::execute
*/
public function testActionExecutionImmediately() {
$this->entity->save()->shouldBeCalledTimes(1);
$this->action->setContextValue('entity', $this->entity->reveal())
->setContextValue('immediate', TRUE);
$this->action->execute();
$this->assertEquals($this->action->autoSaveContext(), [], 'Action returns nothing for auto saving since the entity has been saved already.');
}
/**
* Tests the action execution when saving is postponed.
*
* @covers ::execute
*/
public function testActionExecutionPostponed() {
$this->entity->save()->shouldNotBeCalled();
$this->action->setContextValue('entity', $this->entity->reveal());
$this->action->execute();
$this->assertEquals($this->action->autoSaveContext(), ['entity'], 'Action returns the entity context name for auto saving.');
}
}