You are here

class NodeTypeThumbnailFormHelperTest in Acquia Lift Connector 8

NodeTypeThumbnailFormHelper Test.

@coversDefaultClass Drupal\acquia_lift\Service\Helper\NodeTypeThumbnailFormHelper @group acquia_lift

Hierarchy

Expanded class hierarchy of NodeTypeThumbnailFormHelperTest

File

tests/src/Unit/Service/Helper/NodeTypeThumbnailFormHelperTest.php, line 24
Contains \Drupal\Tests\acquia_lift\Service\Helper\NodeTypeThumbnailFormHelperTest.

Namespace

Drupal\Tests\acquia_lift\Service\Helper
View source
class NodeTypeThumbnailFormHelperTest extends UnitTestCase {
  use SettingsDataTrait;

  /**
   * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  private $configFactory;

  /**
   * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  private $entityManager;

  /**
   * @var \Drupal\Core\Config\Config|\PHPUnit_Framework_MockObject_MockObject
   */
  private $settings;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->configFactory = $this
      ->getMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
    $this->entityManager = $this
      ->getMock('Drupal\\Core\\Entity\\EntityManagerInterface');
    $this->settings = $this
      ->getMockBuilder('Drupal\\Core\\Config\\Config')
      ->disableOriginalConstructor()
      ->getMock();
    $this->configFactory
      ->expects($this
      ->once())
      ->method('getEditable')
      ->with('acquia_lift.settings')
      ->willReturn($this->settings);
  }

  /**
   * Tests the getForm() method, no field for this entity.
   *
   * @covers ::getForm
   */
  public function testGetFormNoField() {
    $form_helper = new NodeTypeThumbnailFormHelper($this->configFactory, $this->entityManager);
    $this->entityManager
      ->expects($this
      ->once())
      ->method('getFieldDefinitions')
      ->with('node', 'article')
      ->willReturn([]);
    $form = $form_helper
      ->getForm('article');
    $this
      ->assertRegexp('/no image field/', $form['no_image_field']['#markup']);
  }

  /**
   * Tests the getForm() method, no style.
   *
   * @covers ::getForm
   */
  public function testGetFormWithNoStyle() {
    $form_helper = new NodeTypeThumbnailFormHelper($this->configFactory, $this->entityManager);
    $field_definitions = [
      'field_description' => $this
        ->getFieldDefinition('description'),
      'field_image' => $this
        ->getFieldDefinition('image'),
    ];
    $this->entityManager
      ->expects($this
      ->once())
      ->method('getFieldDefinitions')
      ->with('node', 'article')
      ->willReturn($field_definitions);
    $form = $form_helper
      ->getForm('article');
    $this
      ->assertRegexp('/no image style/', $form['no_image_styles']['#markup']);
  }

  /**
   * Tests the getForm() method, with an image field and with style.
   *
   * @covers ::getForm
   */
  public function testGetFormWithFieldAndStyle() {
    $form_helper = new NodeTypeThumbnailFormHelper($this->configFactory, $this->entityManager);
    $field_image_definition = $this
      ->getFieldDefinition('image');
    $field_definitions = [
      'field_description' => $this
        ->getFieldDefinition('description'),
      'field_image' => $field_image_definition,
      'field_entity_reference' => $this
        ->getFieldDefinition('entity_reference'),
    ];
    $entity_reference_field_definitions = [
      'field_child_image' => $this
        ->getFieldDefinition('image'),
      'field_image' => $field_image_definition,
    ];
    $entity_type_definition = $this
      ->getMock('Drupal\\Core\\Entity\\EntityTypeInterface');
    $this->entityManager
      ->expects($this
      ->at(0))
      ->method('getFieldDefinitions')
      ->with('node', 'article')
      ->willReturn($field_definitions);
    $this->settings
      ->expects($this
      ->once())
      ->method('get')
      ->with('thumbnail')
      ->willReturn($this
      ->getValidThumbnailSettings());
    $this->entityManager
      ->expects($this
      ->at(1))
      ->method('getDefinition')
      ->with('entity_reference_setting')
      ->willReturn($entity_type_definition);
    $entity_type_definition
      ->expects($this
      ->once())
      ->method('isSubclassOf')
      ->with('\\Drupal\\Core\\Entity\\FieldableEntityInterface')
      ->willReturn(TRUE);
    $this->entityManager
      ->expects($this
      ->at(2))
      ->method('getFieldDefinitions')
      ->with('entity_reference_setting', 'entity_reference')
      ->willReturn($entity_reference_field_definitions);
    ImageStyleOptions::$return = [
      'medium' => 'Medium',
    ];
    $form = $form_helper
      ->getForm('article');
    $expected_field_options = [
      'field_image' => 'Image Label (field_image)',
      'field_entity_reference->field_child_image' => 'Entity_reference Label->Image Label (field_entity_reference->field_child_image)',
    ];
    $this
      ->assertEquals('Acquia Lift', $form['#title']);
    $this
      ->assertEquals($expected_field_options, $form['field']['#options']);
    $this
      ->assertEquals('field_media->field_image', $form['field']['#default_value']);
    $this
      ->assertEquals([
      'medium' => 'Medium',
    ], $form['style']['#options']);
    $this
      ->assertEquals('medium', $form['style']['#default_value']);
  }

  /**
   * Tests the saveSettings() method.
   *
   * @covers ::saveSettings
   */
  public function testSaveSettings() {
    $set_settings = [
      'article' => [
        'some_settings',
      ],
    ];
    $this->settings
      ->expects($this
      ->at(0))
      ->method('get')
      ->with('thumbnail')
      ->willReturn($this
      ->getValidThumbnailSettings());
    $this->settings
      ->expects($this
      ->at(1))
      ->method('set')
      ->with('thumbnail', $set_settings)
      ->willReturn($this->settings);
    $this->settings
      ->expects($this
      ->at(2))
      ->method('save');
    $form_helper = new NodeTypeThumbnailFormHelper($this->configFactory, $this->entityManager);
    $form_helper
      ->saveSettings('article', [
      'some_settings',
    ]);
  }

  /**
   * Get FieldDefinition mock.
   *
   * @param string $type
   *   FieldDefinition type.
   */
  private function getFieldDefinition($type = 'other') {
    $field_definition = $this
      ->getMock('Drupal\\Core\\Field\\FieldDefinitionInterface');
    $field_definition
      ->expects($this
      ->at(0))
      ->method('getType')
      ->willReturn($type);
    $field_definition
      ->expects($this
      ->at(1))
      ->method('getSetting')
      ->with('target_type')
      ->willReturn($type . '_setting');
    $field_definition
      ->expects($this
      ->at(2))
      ->method('getLabel')
      ->willReturn(ucfirst($type) . ' Label');
    return $field_definition;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NodeTypeThumbnailFormHelperTest::$configFactory private property
NodeTypeThumbnailFormHelperTest::$entityManager private property
NodeTypeThumbnailFormHelperTest::$settings private property
NodeTypeThumbnailFormHelperTest::getFieldDefinition private function Get FieldDefinition mock.
NodeTypeThumbnailFormHelperTest::setUp public function Overrides UnitTestCase::setUp
NodeTypeThumbnailFormHelperTest::testGetFormNoField public function Tests the getForm() method, no field for this entity.
NodeTypeThumbnailFormHelperTest::testGetFormWithFieldAndStyle public function Tests the getForm() method, with an image field and with style.
NodeTypeThumbnailFormHelperTest::testGetFormWithNoStyle public function Tests the getForm() method, no style.
NodeTypeThumbnailFormHelperTest::testSaveSettings public function Tests the saveSettings() method.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
SettingsDataTrait::getValidCredentialSettings private function Get a valid credential settings array.
SettingsDataTrait::getValidFieldMappingsSettings private function Get a valid field mappings settings array.
SettingsDataTrait::getValidFrontEndCredentialSettings private function Get a valid front end credential settings.
SettingsDataTrait::getValidIdentitySettings private function Get a valid identity settings array.
SettingsDataTrait::getValidThumbnailSettings private function Get a valid thumbnail settings array.
SettingsDataTrait::getValidVisibilitySettings private function Get a valid visibility settings array.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed 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.