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