function ContentEntityNormalizerTest::testNormalizeWithAccountContext

Same name and namespace in other branches
  1. 8.9.x core/modules/serialization/tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\ContentEntityNormalizerTest::testNormalizeWithAccountContext()
  2. 10 core/modules/serialization/tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\ContentEntityNormalizerTest::testNormalizeWithAccountContext()
  3. 11.x 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 <a href="/api/drupal/core%21modules%21serialization%21src%21Normalizer%21ContentEntityNormalizer.php/class/ContentEntityNormalizer/9" title="Normalizes/denormalizes Drupal content entities into an array structure." class="local">\Drupal\serialization\Normalizer\ContentEntityNormalizer</a> @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)
        ->willReturn('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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.