FieldMissingTypeTest.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Drupal\KernelTests\Core\Field;
  3. use Drupal\Component\Plugin\Exception\PluginNotFoundException;
  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. * Tests the exception when missing a field type.
  10. *
  11. * @group Field
  12. */
  13. class FieldMissingTypeTest extends EntityKernelTestBase {
  14. /**
  15. * Set to FALSE because we are hacking a field storage to use a fake type.
  16. *
  17. * @see \Drupal\Core\Config\Development\ConfigSchemaChecker
  18. *
  19. * @var bool
  20. */
  21. protected $strictConfigSchema = FALSE;
  22. /**
  23. * @var string
  24. */
  25. protected $fieldName;
  26. /**
  27. * {@inheritdoc}
  28. */
  29. protected function setUp() {
  30. parent::setUp();
  31. $entity_type_id = 'entity_test_mulrev';
  32. $this->installEntitySchema($entity_type_id);
  33. $this->fieldName = mb_strtolower($this->randomMachineName());
  34. /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
  35. FieldStorageConfig::create([
  36. 'field_name' => $this->fieldName,
  37. 'type' => 'text',
  38. 'entity_type' => $entity_type_id,
  39. 'cardinality' => 1,
  40. ])->save();
  41. FieldConfig::create([
  42. 'entity_type' => $entity_type_id,
  43. 'field_name' => $this->fieldName,
  44. 'bundle' => $entity_type_id,
  45. 'label' => 'Test field',
  46. ])->save();
  47. }
  48. /**
  49. * Tests the exception thrown when missing a field type in field storages.
  50. *
  51. * @see \Drupal\field\FieldStorageConfigStorage::mapFromStorageRecords()
  52. */
  53. public function testFieldStorageMissingType() {
  54. $this->expectException(PluginNotFoundException::class);
  55. $this->expectExceptionMessage("Unable to determine class for field type 'foo_field_storage' found in the 'field.storage.entity_test_mulrev.{$this->fieldName}' configuration");
  56. $entity = EntityTestMulRev::create([
  57. 'name' => $this->randomString(),
  58. 'field_test_item' => $this->randomString(),
  59. $this->fieldName => $this->randomString(),
  60. ]);
  61. $entity->save();
  62. // Hack the field storage to use a non-existent field type.
  63. $this->config('field.storage.entity_test_mulrev.' . $this->fieldName)->set('type', 'foo_field_storage')->save();
  64. \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
  65. EntityTestMulRev::load($entity->id());
  66. }
  67. /**
  68. * Tests the exception thrown when missing a field type in fields.
  69. *
  70. * @see \Drupal\field\FieldConfigStorageBase::mapFromStorageRecords()
  71. */
  72. public function testFieldMissingType() {
  73. $this->expectException(PluginNotFoundException::class);
  74. $this->expectExceptionMessage("Unable to determine class for field type 'foo_field' found in the 'field.field.entity_test_mulrev.entity_test_mulrev.{$this->fieldName}' configuration");
  75. $entity = EntityTestMulRev::create([
  76. 'name' => $this->randomString(),
  77. 'field_test_item' => $this->randomString(),
  78. $this->fieldName => $this->randomString(),
  79. ]);
  80. $entity->save();
  81. // Hack the field to use a non-existent field type.
  82. $this->config('field.field.entity_test_mulrev.entity_test_mulrev.' . $this->fieldName)->set('field_type', 'foo_field')->save();
  83. \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
  84. EntityTestMulRev::load($entity->id());
  85. }
  86. }