You are here

protected function ContentEntityNormalizerTest::createMockImageEntityReferenceFieldItemList in Acquia Content Hub 8

Creates a mock field entity reference field item list with an image item.

Parameters

string $name: Name of the mock field.

bool $access: Defines whether anyone has access to this field or not.

bool $user_context: The user context used to view the field.

Return value

\Drupal\Core\Field\FieldItemListInterface|\PHPUnit_Framework_MockObject_MockObject The mocked field items.

1 call to ContentEntityNormalizerTest::createMockImageEntityReferenceFieldItemList()
ContentEntityNormalizerTest::testNormalizeImageReferenceField in tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php
Tests the normalize() method for image references.

File

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

Class

ContentEntityNormalizerTest
PHPUnit test for the ContentEntityNormalizer class.

Namespace

Drupal\Tests\acquia_contenthub\Unit\Normalizer

Code

protected function createMockImageEntityReferenceFieldItemList($name, $access = TRUE, $user_context = NULL) {
  $mock = $this
    ->createMock('Drupal\\Core\\Field\\EntityReferenceFieldItemListInterface');
  $mock
    ->method('access')
    ->with('view', $user_context)
    ->will($this
    ->returnValue($access));
  $field_def = $this
    ->createMock('\\Drupal\\Core\\Field\\FieldDefinitionInterface');
  $field_def
    ->method('getName')
    ->willReturn($name);
  $field_def
    ->method('getType')
    ->willReturn('image');
  $mock
    ->method('getValue')
    ->willReturn([
    0 => [
      'target_id' => 'test-id-image-1',
      'alt' => 'test-alt-image-value',
      'title' => 'test-alt-image-text',
      'width' => '100',
      'height' => '100',
    ],
  ]);
  $referenced_entities = [];
  $methods = [
    'id',
    'uuid',
    'getFileUri',
  ];
  $image1 = $this
    ->getMockBuilder('\\Drupal\\image\\Plugin\\Field\\FieldType\\ImageItem')
    ->disableOriginalConstructor()
    ->setMethods($methods)
    ->getMock();
  $image1
    ->method('id')
    ->willReturn('test-id-image-1');
  $image1
    ->method('uuid')
    ->willReturn('test-uuid-image-1');
  $image1
    ->method('getFileUri')
    ->willReturn('public://test-image-1.jpg');
  $referenced_entities[] = $image1;
  $mock
    ->method('getFieldDefinition')
    ->willReturn($field_def);
  $mock
    ->method('referencedEntities')
    ->willReturn($referenced_entities);
  return $mock;
}