You are here

public function EntityReferenceRevisionsSaveTest::testNeedsSave in Entity Reference Revisions 8

Test for NeedsSaveInterface implementation.

Tests that the referenced entity is saved when needsSave() is TRUE.

File

tests/src/Kernel/EntityReferenceRevisionsSaveTest.php, line 54

Class

EntityReferenceRevisionsSaveTest
Tests the entity_reference_revisions NeedsSaveInterface.

Namespace

Drupal\Tests\entity_reference_revisions\Kernel

Code

public function testNeedsSave() {

  // Add the entity_reference_revisions field to article.
  $field_storage = FieldStorageConfig::create(array(
    'field_name' => 'composite_reference',
    'entity_type' => 'node',
    'type' => 'entity_reference_revisions',
    'settings' => array(
      'target_type' => 'entity_test_composite',
    ),
  ));
  $field_storage
    ->save();
  $field = FieldConfig::create(array(
    'field_storage' => $field_storage,
    'bundle' => 'article',
  ));
  $field
    ->save();
  $text = 'Dummy text';

  // Create the test composite entity.
  $entity_test = EntityTestCompositeRelationship::create(array(
    'uuid' => $text,
    'name' => $text,
  ));
  $entity_test
    ->save();
  $text = 'Clever text';

  // Set the name to a new text.

  /** @var \Drupal\entity_composite_relationship_test\Entity\EntityTestCompositeRelationship $entity_test */
  $entity_test->name = $text;
  $entity_test
    ->setNeedsSave(TRUE);

  // Create a node with a reference to the test entity and save.
  $node = Node::create([
    'title' => $this
      ->randomMachineName(),
    'type' => 'article',
    'composite_reference' => $entity_test,
  ]);

  // Check the name is properly set and that getValue() returns the entity
  // when it is marked as needs save."
  $values = $node->composite_reference
    ->getValue();
  $this
    ->assertTrue(isset($values[0]['entity']));
  static::assertEquals($values[0]['entity']->name->value, $text);
  $node->composite_reference
    ->setValue($values);
  static::assertEquals($node->composite_reference->entity->name->value, $text);
  $node
    ->save();

  // Check that the name has been updated when the parent has been saved.

  /** @var \Drupal\entity_composite_relationship_test\Entity\EntityTestCompositeRelationship $entity_test_after */
  $entity_test_after = EntityTestCompositeRelationship::load($entity_test
    ->id());
  static::assertEquals($entity_test_after->name->value, $text);
  $new_text = 'Dummy text again';

  // Set another name and save the node without marking it as needs saving.
  $entity_test_after->name = $new_text;
  $entity_test_after
    ->setNeedsSave(FALSE);

  // Load the Node and check the composite reference entity is not returned
  // from getValue() if it is not marked as needs saving.
  $node = Node::load($node
    ->id());
  $values = $node->composite_reference
    ->getValue();
  $this
    ->assertFalse(isset($values[0]['entity']));
  $node->composite_reference = $entity_test_after;
  $node
    ->save();

  // Check the name is not updated.
  \Drupal::entityTypeManager()
    ->getStorage('entity_test_composite')
    ->resetCache();
  $entity_test_after = EntityTestCompositeRelationship::load($entity_test
    ->id());
  static::assertEquals($text, $entity_test_after->name->value);

  // Test if after delete the referenced entity there are no problems setting
  // the referencing values to the parent.
  $entity_test
    ->delete();
  $node = Node::load($node
    ->id());
  $node
    ->save();

  // Test if the needs save variable is set as false after saving.
  $entity_needs_save = EntityTestCompositeRelationship::create([
    'uuid' => $text,
    'name' => $text,
  ]);
  $entity_needs_save
    ->setNeedsSave(TRUE);
  $entity_needs_save
    ->save();
  $this
    ->assertFalse($entity_needs_save
    ->needsSave());
}