Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / field / tests / src / Kernel / TestObjectItemTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Kernel;
4
5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8
9 /**
10  * Tests the serialization of an object.
11  *
12  * @group field
13  */
14 class TestObjectItemTest extends FieldKernelTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['field_test'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28
29     // Create a 'test_field' field and storage for validation.
30     FieldStorageConfig::create([
31       'field_name' => 'field_test',
32       'entity_type' => 'entity_test',
33       'type' => 'test_object_field',
34     ])->save();
35     FieldConfig::create([
36       'entity_type' => 'entity_test',
37       'field_name' => 'field_test',
38       'bundle' => 'entity_test',
39     ])->save();
40   }
41
42   /**
43    * Tests the serialization of a field type that has an object.
44    */
45   public function testTestObjectItem() {
46     $object = new \stdClass();
47     $object->foo = 'bar';
48     $entity = EntityTest::create();
49     $entity->field_test->value = $object;
50     $entity->save();
51
52     // Verify that the entity has been created properly.
53     $id = $entity->id();
54     $entity = EntityTest::load($id);
55     $this->assertTrue($entity->field_test->value instanceof \stdClass);
56     $this->assertEquals($object, $entity->field_test->value);
57   }
58
59 }