View source
<?php
namespace Drupal\Tests\jsonapi\Kernel\Serializer;
use Drupal\Core\Render\Markup;
use Drupal\jsonapi\Normalizer\Value\FieldNormalizerValue;
use Drupal\jsonapi_test_data_type\TraversableObject;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\jsonapi\Kernel\JsonapiKernelTestBase;
use Drupal\user\Entity\User;
class SerializerTest extends JsonapiKernelTestBase {
public static $modules = [
'serialization',
'system',
'node',
'user',
'field',
'text',
'filter',
'jsonapi_test_data_type',
];
protected $node;
protected $sut;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installSchema('system', [
'sequences',
]);
$this
->installSchema('node', [
'node_access',
]);
$this
->installSchema('user', [
'users_data',
]);
$this->user = User::create([
'name' => $this
->randomString(),
'status' => 1,
]);
$this->user
->save();
NodeType::create([
'type' => 'foo',
])
->save();
$this
->createTextField('node', 'foo', 'field_text', 'Text');
$this->node = Node::create([
'title' => 'Test Node',
'type' => 'foo',
'field_text' => [
'value' => 'This is some text.',
'format' => 'text_plain',
],
'uid' => $this->user
->id(),
]);
$this->node
->save();
$this->sut = $this->container
->get('jsonapi.serializer_do_not_use_removal_imminent');
}
public function testFallbackNormalizer() {
$context = [
'account' => $this->user,
];
$value = $this->sut
->normalize($this->node->field_text, 'api_json', $context);
$this
->assertTrue($value instanceof FieldNormalizerValue);
$nested_field = [
$this->node->field_text,
];
$traversableObject = new TraversableObject();
$value = $this->sut
->normalize($traversableObject, 'api_json', $context);
$this
->assertEquals($traversableObject->property, $value);
$value = $this->sut
->normalize($nested_field, 'api_json', $context);
$this
->assertTrue($value[0] instanceof FieldNormalizerValue);
$data = Markup::create('<h2>Test Markup</h2>');
$value = $this->sut
->normalize($data, 'api_json', $context);
$this
->assertEquals('<h2>Test Markup</h2>', $value);
}
}