You are here

public function ContentEntityNormalizerTest::createMockForContentEntity in Acquia Content Hub 8

Creates a mock content entity.

Parameters

array $definitions: The field definitions.

array $languages: The languages that this fake entity should have.

bool $is_new: TRUE if the entity is new, FALSE otherwise.

Return value

\PHPUnit_Framework_MockObject_MockObject The fake ContentEntity.

14 calls to ContentEntityNormalizerTest::createMockForContentEntity()
ContentEntityNormalizerTest::testGetFieldTypeMapping in tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php
Test the getFieldTypeMapping method.
ContentEntityNormalizerTest::testNormalizeImageReferenceField in tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php
Tests the normalize() method for image references.
ContentEntityNormalizerTest::testNormalizeOneField in tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php
Tests the normalize() method.
ContentEntityNormalizerTest::testNormalizeOneFieldMultiValued in tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php
Tests the normalize() method.
ContentEntityNormalizerTest::testNormalizeReferenceField in tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php
Tests the normalize() method.

... See full list

File

tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php, line 865

Class

ContentEntityNormalizerTest
PHPUnit test for the ContentEntityNormalizer class.

Namespace

Drupal\Tests\acquia_contenthub\Unit\Normalizer

Code

public function createMockForContentEntity(array $definitions, array $languages, $is_new = FALSE) {
  $enabled_methods = [
    'getFields',
    'getEntityTypeId',
    'uuid',
    'get',
    'getTranslationLanguages',
    'getTranslation',
    'hasField',
    'toUrl',
    'access',
    'hasLinkTemplate',
    'isNew',
  ];
  $content_entity_mock = $this
    ->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityBase')
    ->disableOriginalConstructor()
    ->setMethods($enabled_methods)
    ->getMockForAbstractClass();
  $content_entity_mock
    ->method('getFields')
    ->willReturn($definitions);
  $content_entity_mock
    ->method('isNew')
    ->willReturn($is_new);

  // Return the given content.
  $content_entity_mock
    ->method('get')
    ->willReturnCallback(function ($name) use ($definitions) {
    if (isset($definitions[$name])) {
      return $definitions[$name];
    }
    return NULL;
  });
  $content_entity_mock
    ->method('hasField')
    ->willReturnCallback(function ($name) use ($definitions) {
    if (isset($definitions[$name])) {
      return TRUE;
    }
    return FALSE;
  });
  $content_entity_mock
    ->method('getEntityTypeId')
    ->willReturn('node');
  $content_entity_mock
    ->method('uuid')
    ->willReturn('custom-uuid');
  $content_entity_mock
    ->method('getTranslation')
    ->willReturn($content_entity_mock);
  $languages = $this
    ->createMockLanguageList($languages);
  $content_entity_mock
    ->method('getTranslationLanguages')
    ->willReturn($languages);
  $url = $this
    ->getMockBuilder('Drupal\\Core\\Url')
    ->disableOriginalConstructor()
    ->getMock();
  $url
    ->method('toString')
    ->willReturn('http://localhost/node/1');
  $content_entity_mock
    ->method('toUrl')
    ->willReturn($url);
  $access = $this
    ->createMock('Drupal\\Core\\Access\\AccessResultInterface');
  $access
    ->method('isAllowed')
    ->willReturn(TRUE);
  $content_entity_mock
    ->method('access')
    ->withAnyParameters()
    ->willReturn($access);
  $content_entity_mock
    ->method('hasLinkTemplate')
    ->willReturn(TRUE);
  return $content_entity_mock;
}