You are here

class ContentEntityNormalizerTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/serialization/tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\ContentEntityNormalizerTest

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

Hierarchy

Expanded class hierarchy of ContentEntityNormalizerTest

File

core/modules/serialization/tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php, line 17
Contains \Drupal\Tests\serialization\Unit\Normalizer\ContentEntityNormalizerTest.

Namespace

Drupal\Tests\serialization\Unit\Normalizer
View source
class ContentEntityNormalizerTest extends UnitTestCase {

  /**
   * The mock entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityManager;

  /**
   * The mock serializer.
   *
   * @var \Symfony\Component\Serializer\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $serializer;

  /**
   * The normalizer under test.
   *
   * @var \Drupal\serialization\Normalizer\ContentEntityNormalizer
   */
  protected $contentEntityNormalizer;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->entityManager = $this
      ->getMock('Drupal\\Core\\Entity\\EntityManagerInterface');
    $this->contentEntityNormalizer = new ContentEntityNormalizer($this->entityManager);
    $this->serializer = $this
      ->getMockBuilder('Symfony\\Component\\Serializer\\Serializer')
      ->disableOriginalConstructor()
      ->setMethods(array(
      'normalize',
    ))
      ->getMock();
    $this->contentEntityNormalizer
      ->setSerializer($this->serializer);
  }

  /**
   * @covers ::supportsNormalization
   */
  public function testSupportsNormalization() {
    $content_mock = $this
      ->getMock('Drupal\\Core\\Entity\\ContentEntityInterface');
    $config_mock = $this
      ->getMock('Drupal\\Core\\Entity\\ConfigEntityInterface');
    $this
      ->assertTrue($this->contentEntityNormalizer
      ->supportsNormalization($content_mock));
    $this
      ->assertFalse($this->contentEntityNormalizer
      ->supportsNormalization($config_mock));
  }

  /**
   * Tests the normalize() method.
   *
   * @covers ::normalize
   */
  public function testNormalize() {
    $this->serializer
      ->expects($this
      ->any())
      ->method('normalize')
      ->with($this
      ->containsOnlyInstancesOf('Drupal\\Core\\Field\\FieldItemListInterface'), 'test_format', [
      'account' => NULL,
    ])
      ->will($this
      ->returnValue('test'));
    $definitions = array(
      'field_1' => $this
        ->createMockFieldListItem(),
      'field_2' => $this
        ->createMockFieldListItem(FALSE),
    );
    $content_entity_mock = $this
      ->createMockForContentEntity($definitions);
    $normalized = $this->contentEntityNormalizer
      ->normalize($content_entity_mock, 'test_format');
    $this
      ->assertArrayHasKey('field_1', $normalized);
    $this
      ->assertEquals('test', $normalized['field_1']);
    $this
      ->assertArrayNotHasKey('field_2', $normalized);
  }

  /**
   * Tests the normalize() method with account context passed.
   *
   * @covers ::normalize
   */
  public function testNormalizeWithAccountContext() {
    $mock_account = $this
      ->getMock('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 = array(
      'field_1' => $this
        ->createMockFieldListItem(TRUE, $mock_account),
      'field_2' => $this
        ->createMockFieldListItem(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);
  }

  /**
   * Creates a mock content entity.
   *
   * @param $definitions
   *
   * @return \PHPUnit_Framework_MockObject_MockObject
   */
  public function createMockForContentEntity($definitions) {
    $content_entity_mock = $this
      ->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityBase')
      ->disableOriginalConstructor()
      ->setMethods(array(
      'getFields',
    ))
      ->getMockForAbstractClass();
    $content_entity_mock
      ->expects($this
      ->once())
      ->method('getFields')
      ->will($this
      ->returnValue($definitions));
    return $content_entity_mock;
  }

  /**
   * Creates a mock field list item.
   *
   * @param bool $access
   *
   * @return \Drupal\Core\Field\FieldItemListInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected function createMockFieldListItem($access = TRUE, $user_context = NULL) {
    $mock = $this
      ->getMock('Drupal\\Core\\Field\\FieldItemListInterface');
    $mock
      ->expects($this
      ->once())
      ->method('access')
      ->with('view', $user_context)
      ->will($this
      ->returnValue($access));
    return $mock;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentEntityNormalizerTest::$contentEntityNormalizer protected property The normalizer under test.
ContentEntityNormalizerTest::$entityManager protected property The mock entity manager.
ContentEntityNormalizerTest::$serializer protected property The mock serializer.
ContentEntityNormalizerTest::createMockFieldListItem protected function Creates a mock field list item.
ContentEntityNormalizerTest::createMockForContentEntity public function Creates a mock content entity.
ContentEntityNormalizerTest::setUp protected function Overrides UnitTestCase::setUp
ContentEntityNormalizerTest::testNormalize public function Tests the normalize() method.
ContentEntityNormalizerTest::testNormalizeWithAccountContext public function Tests the normalize() method with account context passed.
ContentEntityNormalizerTest::testSupportsNormalization public function @covers ::supportsNormalization
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.