public function FieldCollectionBasicTestCase::testCRUD in Field collection 8.3
Same name and namespace in other branches
- 8 tests/src/Functional/FieldCollectionBasicTestCase.php \Drupal\Tests\field_collection\Functional\FieldCollectionBasicTestCase::testCRUD()
Tests CRUD.
File
- tests/
src/ Functional/ FieldCollectionBasicTestCase.php, line 43
Class
- FieldCollectionBasicTestCase
- Test basics.
Namespace
Drupal\Tests\field_collection\FunctionalCode
public function testCRUD() {
/** @var \Drupal\node\NodeInterface $node */
/** @var \Drupal\field_collection\FieldCollectionItemInterface $field_collection_item */
list($node, $field_collection_item) = $this
->createNodeWithFieldCollection('article');
$this
->assertEqual($field_collection_item
->id(), $node->{$this->field_collection_name}->target_id, 'A field_collection_item has been successfully created and referenced.');
$this
->assertEqual($field_collection_item->revision_id->value, $node->{$this->field_collection_name}->revision_id, 'The new field_collection_item has the correct revision.');
// Test adding an additional field_collection_item.
$field_collection_item_2 = FieldCollectionItem::create([
'field_name' => $this->field_collection_name,
]);
$field_collection_item_2->{$this->inner_field_name}
->setValue(2);
$node->{$this->field_collection_name}[1] = [
'entity' => $field_collection_item_2,
];
$node
->save();
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = Node::load($node
->id());
$this
->assertTrue(!empty($field_collection_item_2
->id()), 'Second field_collection_item saved.');
$this
->assertTrue(!empty($field_collection_item_2
->getRevisionId()), 'Second field_collection_item has a revision.');
$this
->assertEqual(count(FieldCollectionItem::loadMultiple()), 2, 'Field_collection_items have been stored.');
$this
->assertEqual($field_collection_item
->id(), $node->{$this->field_collection_name}->target_id, 'Existing reference has been kept during update.');
$this
->assertEqual($field_collection_item
->getRevisionId(), $node->{$this->field_collection_name}[0]->revision_id, 'Revision: Existing reference has been kept during update.');
$this
->assertEqual($field_collection_item_2
->id(), $node->{$this->field_collection_name}[1]->target_id, 'New field_collection_item has been properly referenced.');
$this
->assertEqual($field_collection_item_2
->getRevisionId(), $node->{$this->field_collection_name}[1]->revision_id, 'Revision: New field_collection_item has been properly referenced.');
// Make sure deleting the field collection item removes the reference.
$field_collection_item_2
->delete();
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = Node::load($node
->id());
$this
->assertTrue(!isset($node->{$this->field_collection_name}[1]), 'Reference correctly deleted.');
// Make sure field_collections are removed during deletion of the host.
$node
->delete();
$this
->assertIdentical(FieldCollectionItem::loadMultiple(), [], 'field_collection_item deleted when the host is deleted.');
// Try deleting nodes with collections without any values.
$node = $this
->drupalCreateNode([
'type' => 'article',
]);
$node
->delete();
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = Node::load($node
->id());
$this
->assertFalse($node);
// Test creating a field collection entity with a not-yet saved host entity.
$node = $this
->drupalCreateNode([
'type' => 'article',
]);
$field_collection_item = FieldCollectionItem::create([
'field_name' => $this->field_collection_name,
]);
$field_collection_item->{$this->inner_field_name}
->setValue(3);
$field_collection_item
->setHostEntity($node);
$field_collection_item
->save();
// Now the node should have been saved with the collection and the link
// should have been established.
$this
->assertTrue(!empty($node
->id()), 'Node has been saved with the collection.');
$this
->assertTrue(count($node->{$this->field_collection_name}) == 1 && !empty($node->{$this->field_collection_name}[0]->target_id) && !empty($node->{$this->field_collection_name}[0]->revision_id), 'Link has been established.');
// Again, test creating a field collection with a not-yet saved host entity,
// but this time save both entities via the host.
$node = $this
->drupalCreateNode([
'type' => 'article',
]);
$field_collection_item = FieldCollectionItem::create([
'field_name' => $this->field_collection_name,
]);
$field_collection_item->{$this->inner_field_name}
->setValue(4);
$field_collection_item
->setHostEntity($node);
$node
->save();
$this
->assertTrue(!empty($field_collection_item
->id()) && !empty($field_collection_item
->getRevisionId()), 'Removed field collection item still exists.');
$this
->assertTrue(count($node->{$this->field_collection_name}) == 1 && !empty($node->{$this->field_collection_name}[0]->target_id) && !empty($node->{$this->field_collection_name}[0]->revision_id), 'Removed field collection item is archived.');
// Test with multiple revisions of the host entity.
list($node, $field_collection_item) = $this
->createNodeWithFieldCollection('article');
$node
->save();
$this->nodeStorage
->resetCache([
$node
->id(),
]);
// Load revision 1.
$node = Node::load($node
->id());
$field_collection_item = $node
->get($this->field_collection_name)[0]
->getFieldCollectionItem();
// Remember revision id and inner field value.
$revision_1_id = $node
->getRevisionId();
$revision_1_value = $field_collection_item->{$this->inner_field_name}->value;
// Update the inner field.
$revision_2_value = $revision_1_value + 1;
$field_collection_item->{$this->inner_field_name}
->setValue($revision_2_value);
// Save new revision of the host.
$node
->setNewRevision();
$node
->save();
$revision_2_id = $node
->getRevisionId();
// Make sure we really did get a new revision.
$this
->assertTrue($revision_2_id > $revision_1_value, 'New revision of node created');
$this->nodeStorage
->resetCache([
$node
->id(),
]);
// Retrieve revision 1 again.
$node = \Drupal::entityTypeManager()
->getStorage('node')
->loadRevision($revision_1_id);
$field_collection_item = $node
->get($this->field_collection_name)[0]
->getFieldCollectionItem(FALSE, FALSE);
// Did we get the correct inner field value?
$this
->assertTrue($field_collection_item->{$this->inner_field_name}->value == $revision_1_value, 'Correct value returned from revision 1.');
$this->nodeStorage
->resetCache([
$node
->id(),
]);
// Retrieve revision 2 again.
$node = \Drupal::entityTypeManager()
->getStorage('node')
->loadRevision($revision_2_id);
$field_collection_item = $node
->get($this->field_collection_name)[0]
->getFieldCollectionItem(FALSE, FALSE);
// Did we get the correct inner field value?
$this
->assertTrue($field_collection_item->{$this->inner_field_name}->value == $revision_2_value, 'Correct value returned from revision 2.');
}