You are here

public function ContentEntityNormalizerTest::testNormalizeWithAccountContext in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/serialization/tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\ContentEntityNormalizerTest::testNormalizeWithAccountContext()

Tests the normalize() method with account context passed.

@covers ::normalize

File

core/modules/serialization/tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php, line 94

Class

ContentEntityNormalizerTest
@coversDefaultClass \Drupal\serialization\Normalizer\ContentEntityNormalizer @group serialization

Namespace

Drupal\Tests\serialization\Unit\Normalizer

Code

public function testNormalizeWithAccountContext() {
  $mock_account = $this
    ->createMock('Drupal\\Core\\Session\\AccountInterface');
  $context = [
    'account' => $mock_account,
  ];
  $this->serializer
    ->expects($this
    ->any())
    ->method('normalize')
    ->with($this
    ->containsOnlyInstancesOf('Drupal\\Core\\Field\\FieldItemListInterface'), 'test_format', $context)
    ->will($this
    ->returnValue('test'));

  // The mock account should get passed directly into the access() method on
  // field items from $context['account'].
  $definitions = [
    'field_1' => $this
      ->createMockFieldListItem(TRUE, FALSE, $mock_account),
    'field_2' => $this
      ->createMockFieldListItem(FALSE, FALSE, $mock_account),
  ];
  $content_entity_mock = $this
    ->createMockForContentEntity($definitions);
  $normalized = $this->contentEntityNormalizer
    ->normalize($content_entity_mock, 'test_format', $context);
  $this
    ->assertArrayHasKey('field_1', $normalized);
  $this
    ->assertEquals('test', $normalized['field_1']);
  $this
    ->assertArrayNotHasKey('field_2', $normalized);
}