You are here

public function NodeTypePreviewImageFormTest::testSaveSettingsAddDefaultImageStyleOption in Acquia Content Hub 8

Tests the saveSettings() method, add default image style option.

@covers ::saveSettings

File

tests/src/Unit/Form/NodeTypePreviewImageFormTest.php, line 235

Class

NodeTypePreviewImageFormTest
PHPUnit test for the NodeTypePreviewImageForm class.

Namespace

Drupal\Tests\acquia_contenthub\Unit\Form

Code

public function testSaveSettingsAddDefaultImageStyleOption() {
  $image_style = $this
    ->getMockBuilder('Drupal\\image\\Entity\\ImageStyle')
    ->disableOriginalConstructor()
    ->getMock();
  $image_style
    ->expects($this
    ->once())
    ->method('addImageEffect')
    ->with([
    'id' => 'image_scale_and_crop',
    'weight' => 1,
    'data' => [
      'width' => 150,
      'height' => 150,
    ],
  ]);
  $image_style
    ->expects($this
    ->once())
    ->method('save');
  $this->entityTypeRepository = $this
    ->createMock('Drupal\\Core\\Entity\\EntityTypeRepositoryInterface');
  $entity_storage = $this
    ->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
  $this->entityTypeManager = $this
    ->createMock('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface');

  // Setting up the Container.
  $container = new Container();
  $container
    ->set('entity_type.manager', $this->entityTypeManager);
  $container
    ->set('entity_type.repository', $this->entityTypeRepository);
  \Drupal::setContainer($container);
  $this->entityTypeRepository
    ->expects($this
    ->once())
    ->method('getEntityTypeFromClass')
    ->with('Drupal\\image\\Entity\\ImageStyle')
    ->willReturn($image_style);
  $this->entityTypeManager
    ->expects($this
    ->once())
    ->method('getStorage')
    ->with($image_style)
    ->willReturn($entity_storage);
  $image_style_create_argument = [
    'label' => 'Acquia Content Hub Preview Image (150×150)',
    'name' => 'acquia_contenthub_preview_image',
  ];
  $entity_storage
    ->expects($this
    ->once())
    ->method('create')
    ->with($image_style_create_argument)
    ->willReturn($image_style);
  $this->contenthubEntityConfig
    ->expects($this
    ->at(0))
    ->method('setPreviewImageField')
    ->with('article', 'some_field');
  $this->contenthubEntityConfig
    ->expects($this
    ->at(1))
    ->method('setPreviewImageStyle')
    ->with('article', NodeTypePreviewImageForm::PREVIEW_IMAGE_DEFAULT_KEY);
  $this->contenthubEntityConfig
    ->expects($this
    ->at(2))
    ->method('save');
  $preview_image_form = new NodeTypePreviewImageForm($this->contenthubEntityManager, $this->entityTypeManager, $this->entityFieldManager);
  $preview_image_form
    ->setStringTranslation($this
    ->getStringTranslationStub());
  $preview_image_form
    ->saveSettings('article', [
    'field' => 'some_field',
    'style' => NodeTypePreviewImageForm::PREVIEW_IMAGE_ADD_DEFAULT_KEY,
  ]);
}