function ContentEntityNormalizerTest::testNormalizeWithAccountContext

Same name 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 \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)
        ->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.