Mercurial > hg > isophonics-drupal-site
diff core/modules/field/tests/src/Kernel/TestObjectItemTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/modules/field/tests/src/Kernel/TestObjectItemTest.php Wed Nov 29 16:09:58 2017 +0000 @@ -0,0 +1,59 @@ +<?php + +namespace Drupal\Tests\field\Kernel; + +use Drupal\entity_test\Entity\EntityTest; +use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; + +/** + * Tests the serialization of an object. + * + * @group field + */ +class TestObjectItemTest extends FieldKernelTestBase { + + /** + * Modules to enable. + * + * @var array + */ + public static $modules = ['field_test']; + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + // Create a 'test_field' field and storage for validation. + FieldStorageConfig::create([ + 'field_name' => 'field_test', + 'entity_type' => 'entity_test', + 'type' => 'test_object_field', + ])->save(); + FieldConfig::create([ + 'entity_type' => 'entity_test', + 'field_name' => 'field_test', + 'bundle' => 'entity_test', + ])->save(); + } + + /** + * Tests the serialization of a field type that has an object. + */ + public function testTestObjectItem() { + $object = new \stdClass(); + $object->foo = 'bar'; + $entity = EntityTest::create(); + $entity->field_test->value = $object; + $entity->save(); + + // Verify that the entity has been created properly. + $id = $entity->id(); + $entity = EntityTest::load($id); + $this->assertTrue($entity->field_test->value instanceof \stdClass); + $this->assertEquals($object, $entity->field_test->value); + } + +}