You are here

public function FieldCollectionBasicTestCase::testCRUD in Field collection 8

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/FieldCollectionBasicTestCase.php \Drupal\Tests\field_collection\Functional\FieldCollectionBasicTestCase::testCRUD()

Tests CRUD.

File

tests/src/Functional/FieldCollectionBasicTestCase.php, line 41

Class

FieldCollectionBasicTestCase
Test basics.

Namespace

Drupal\Tests\field_collection\Functional

Code

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}->value, '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] = [
    'field_collection_item' => $field_collection_item_2,
  ];
  $node
    ->save();
  $this->nodeStorage
    ->resetCache([
    $node
      ->id(),
  ]);
  $node = Node::load($node
    ->id());
  $this
    ->assertTrue(!empty($field_collection_item_2
    ->id()) && !empty($field_collection_item_2
    ->getRevisionId()), 'Another field_collection_item has been saved.');
  $this
    ->assertEqual(count(FieldCollectionItem::loadMultiple()), 2, 'Field_collection_items have been stored.');
  $this
    ->assertEqual($field_collection_item
    ->id(), $node->{$this->field_collection_name}->value, '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]->value, '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]->value) && !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]->value) && !empty($node->{$this->field_collection_name}[0]->revision_id), 'Removed field collection item is archived.');
}