FieldItemTest.php 3.5 KB

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