FieldItemTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Drupal\KernelTests\Core\Field;
  3. use Drupal\entity_test\Entity\EntityTest;
  4. use Drupal\entity_test\Entity\EntityTestMulRev;
  5. use Drupal\field\Entity\FieldConfig;
  6. use Drupal\field\Entity\FieldStorageConfig;
  7. use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
  8. /**
  9. * Test field item methods.
  10. *
  11. * @group Field
  12. */
  13. class FieldItemTest extends EntityKernelTestBase {
  14. /**
  15. * @var string
  16. */
  17. protected $fieldName;
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function setUp() {
  22. parent::setUp();
  23. $this->container->get('state')->set('entity_test.field_test_item', TRUE);
  24. $this->entityTypeManager->clearCachedDefinitions();
  25. $entity_type_id = 'entity_test_mulrev';
  26. $this->installEntitySchema($entity_type_id);
  27. $this->fieldName = mb_strtolower($this->randomMachineName());
  28. /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
  29. FieldStorageConfig::create([
  30. 'field_name' => $this->fieldName,
  31. 'type' => 'field_test',
  32. 'entity_type' => $entity_type_id,
  33. 'cardinality' => 1,
  34. ])->save();
  35. FieldConfig::create([
  36. 'entity_type' => $entity_type_id,
  37. 'field_name' => $this->fieldName,
  38. 'bundle' => $entity_type_id,
  39. 'label' => 'Test field',
  40. ])->save();
  41. $this->entityTypeManager->clearCachedDefinitions();
  42. $definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($entity_type_id);
  43. $this->assertTrue(!empty($definitions[$this->fieldName]));
  44. }
  45. /**
  46. * Tests the field item save workflow.
  47. */
  48. public function testSaveWorkflow() {
  49. $entity = EntityTestMulRev::create([
  50. 'name' => $this->randomString(),
  51. 'field_test_item' => $this->randomString(),
  52. $this->fieldName => $this->randomString(),
  53. ]);
  54. // Save a new entity and verify that the initial field value is overwritten
  55. // with a value containing the entity id, which implies a resave. Check that
  56. // the entity data structure and the stored values match.
  57. $this->assertSavedFieldItemValue($entity, "field_test:{$this->fieldName}:1:1");
  58. // Update the entity and verify that the field value is overwritten on
  59. // presave if it is not resaved.
  60. $this->assertSavedFieldItemValue($entity, 'overwritten');
  61. // Flag the field value as needing to be resaved and verify it actually is.
  62. $entity->field_test_item->value = $entity->{$this->fieldName}->value = 'resave';
  63. $this->assertSavedFieldItemValue($entity, "field_test:{$this->fieldName}:1:3");
  64. }
  65. /**
  66. * Checks that the saved field item value matches the expected one.
  67. *
  68. * @param \Drupal\entity_test\Entity\EntityTest $entity
  69. * The test entity.
  70. * @param $expected_value
  71. * The expected field item value.
  72. *
  73. * @return bool
  74. * TRUE if the item value matches expectations, FALSE otherwise.
  75. */
  76. protected function assertSavedFieldItemValue(EntityTest $entity, $expected_value) {
  77. $entity->setNewRevision(TRUE);
  78. $entity->save();
  79. $base_field_expected_value = str_replace($this->fieldName, 'field_test_item', $expected_value);
  80. $result = $this->assertEqual($entity->field_test_item->value, $base_field_expected_value);
  81. $result = $result && $this->assertEqual($entity->{$this->fieldName}->value, $expected_value);
  82. $entity = $this->reloadEntity($entity);
  83. $result = $result && $this->assertEqual($entity->field_test_item->value, $base_field_expected_value);
  84. $result = $result && $this->assertEqual($entity->{$this->fieldName}->value, $expected_value);
  85. return $result;
  86. }
  87. }