tests/src/Functional/ExternalNormalizersTest.php | 70 ++++++++++++------------ 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/tests/src/Functional/ExternalNormalizersTest.php b/tests/src/Functional/ExternalNormalizersTest.php index 9694a9a..953e2c9 100644 --- a/tests/src/Functional/ExternalNormalizersTest.php +++ b/tests/src/Functional/ExternalNormalizersTest.php @@ -21,6 +21,21 @@ use Drupal\user\RoleInterface; class ExternalNormalizersTest extends BrowserTestBase { /** + * The original value for the test field. + * + * @var string + */ + const VALUE_ORIGINAL = 'Llamas are super awesome!'; + + /** + * The expected overridden value for the test field. + * + * @see \Drupal\jsonapi_test_field_type\Normalizer\StringNormalizer + * @see \Drupal\jsonapi_test_data_type\Normalizer\StringNormalizer + */ + const VALUE_OVERRIDDEN = 'Llamas are NOT awesome!'; + + /** * {@inheritdoc} */ protected static $modules = [ @@ -29,6 +44,13 @@ class ExternalNormalizersTest extends BrowserTestBase { ]; /** + * The test entity. + * + * @var \Drupal\entity_test\Entity\EntityTest + */ + protected $entity; + + /** * {@inheritdoc} */ public function setUp() { @@ -67,22 +89,13 @@ class ExternalNormalizersTest extends BrowserTestBase { * @see jsonapi.normalizers */ public function testFormatAgnosticFieldTypeNormalizers() { - $value = 'Llamas are super awesome!'; - - $entity_test = EntityTest::create([ - 'name' => 'Llama', - 'type' => 'entity_test', - 'field_test' => $value, - ]); - $entity_test->save(); - // Asserts the entity contains the value we set. - $this->assertSame($value, $entity_test->field_test->value); + $this->assertSame(static::VALUE_ORIGINAL, $this->entity->field_test->value); // Asserts normalizing the entity using core's 'serializer' service DOES // yield the value we set. - $core_normalization = $this->container->get('serializer')->normalize($entity_test); - $this->assertSame('Llamas are super awesome!', $core_normalization['field_test'][0]['value']); + $core_normalization = $this->container->get('serializer')->normalize($this->entity); + $this->assertSame(static::VALUE_ORIGINAL, $core_normalization['field_test'][0]['value']); // Install test module that contains a high-priority alternative normalizer: // \Drupal\jsonapi_test_field_type\Normalizer\StringNormalizer::normalize(). @@ -91,18 +104,17 @@ class ExternalNormalizersTest extends BrowserTestBase { // Asserts normalizing the entity using core's 'serializer' service DOES NOT // ANYMORE yield the value we set. - $core_normalization = $this->container->get('serializer')->normalize($entity_test); - $this->assertNotSame($value, $core_normalization['field_test'][0]['value']); - $this->assertSame('Llamas are NOT awesome!', $core_normalization['field_test'][0]['value']); + $core_normalization = $this->container->get('serializer')->normalize($this->entity); + $this->assertSame(static::VALUE_OVERRIDDEN, $core_normalization['field_test'][0]['value']); // Asserts that this does NOT affect the JSON API normalization. // @todo Remove line below in favor of commented line in https://www.drupal.org/project/jsonapi/issues/2878463. - $url = Url::fromRoute('jsonapi.entity_test--entity_test.individual', ['entity_test'=> $entity_test->uuid()]); + $url = Url::fromRoute('jsonapi.entity_test--entity_test.individual', ['entity_test'=> $this->entity->uuid()]); /* $url = $this->entity->toUrl('jsonapi'); */ $client = $this->getSession()->getDriver()->getClient()->getClient(); $response = $client->request('GET', $url->setAbsolute(TRUE)->toString()); $document = Json::decode((string) $response->getBody()); - $this->assertSame($value, $document['data']['attributes']['field_test']); + $this->assertSame(static::VALUE_ORIGINAL, $document['data']['attributes']['field_test']); } /** @@ -111,22 +123,13 @@ class ExternalNormalizersTest extends BrowserTestBase { * @see jsonapi.normalizers */ public function testFormatAgnosticDataTypeNormalizers() { - $value = 'Llamas are super awesome!'; - - $entity_test = EntityTest::create([ - 'name' => 'Llama', - 'type' => 'entity_test', - 'field_test' => $value, - ]); - $entity_test->save(); - // Asserts the entity contains the value we set. - $this->assertSame($value, $entity_test->field_test->value); + $this->assertSame(static::VALUE_ORIGINAL, $this->entity->field_test->value); // Asserts normalizing the entity using core's 'serializer' service DOES // yield the value we set. - $core_normalization = $this->container->get('serializer')->normalize($entity_test); - $this->assertSame('Llamas are super awesome!', $core_normalization['field_test'][0]['value']); + $core_normalization = $this->container->get('serializer')->normalize($this->entity); + $this->assertSame(static::VALUE_ORIGINAL, $core_normalization['field_test'][0]['value']); // Install test module that contains a high-priority alternative normalizer: // \Drupal\jsonapi_test_field_type\Normalizer\StringNormalizer::normalize(). @@ -135,18 +138,17 @@ class ExternalNormalizersTest extends BrowserTestBase { // Asserts normalizing the entity using core's 'serializer' service DOES NOT // ANYMORE yield the value we set. - $core_normalization = $this->container->get('serializer')->normalize($entity_test); - $this->assertNotSame($value, $core_normalization['field_test'][0]['value']); - $this->assertSame('Llamas are NOT awesome!', $core_normalization['field_test'][0]['value']); + $core_normalization = $this->container->get('serializer')->normalize($this->entity); + $this->assertSame(static::VALUE_OVERRIDDEN, $core_normalization['field_test'][0]['value']); // Asserts that this DOES affect the JSON API normalization. // @todo Remove line below in favor of commented line in https://www.drupal.org/project/jsonapi/issues/2878463. - $url = Url::fromRoute('jsonapi.entity_test--entity_test.individual', ['entity_test'=> $entity_test->uuid()]); + $url = Url::fromRoute('jsonapi.entity_test--entity_test.individual', ['entity_test'=> $this->entity->uuid()]); /* $url = $this->entity->toUrl('jsonapi'); */ $client = $this->getSession()->getDriver()->getClient()->getClient(); $response = $client->request('GET', $url->setAbsolute(TRUE)->toString()); $document = Json::decode((string) $response->getBody()); - $this->assertSame('Llamas are NOT awesome!', $document['data']['attributes']['field_test']); + $this->assertSame(static::VALUE_OVERRIDDEN, $document['data']['attributes']['field_test']); } } \ No newline at end of file