You are here

public function FocalPointFieldWidgetTest::testValidateFocalPoint in Focal Point 8

Testing focal point validation.

@covers ::validateFocalPoint

@dataProvider providerValidateFocalPoint

File

tests/src/Unit/FieldWidgets/FocalPointFieldWidgetTest.php, line 70

Class

FocalPointFieldWidgetTest
@coversDefaultClass \Drupal\focal_point\Plugin\Field\FieldWidget\FocalPointImageWidget

Namespace

Drupal\Tests\focal_point\Unit\FieldWidgets

Code

public function testValidateFocalPoint($value, $is_valid) {
  $this->testElement['#value'] = $value;

  // Test that an invalid focal point value sets a form error and a valid
  // focal point value does not.
  if ($is_valid === TRUE) {
    $this->testFormState
      ->expects($this
      ->never())
      ->method('setError');
  }
  else {
    $this->testFormState
      ->expects($this
      ->once())
      ->method('setError')
      ->will($this
      ->returnSelf());
  }
  $element = [
    '#title' => 'foo',
    '#value' => $value,
  ];

  // Create a focal point image widget and test the validate method. Note that
  // an additional argument was added to the ImageWidget constructor in 8.5.
  if (version_compare(Drupal::VERSION, '8.5', '<')) {
    $focalPointImageWidget = new FocalPointImageWidget([], [], $this
      ->prophesize(FieldDefinitionInterface::class)
      ->reveal(), [], [], $this
      ->prophesize(ElementInfoManagerInterface::class)
      ->reveal());
  }
  else {
    $focalPointImageWidget = new FocalPointImageWidget([], [], $this
      ->prophesize(FieldDefinitionInterface::class)
      ->reveal(), [], [], $this
      ->prophesize(ElementInfoManagerInterface::class)
      ->reveal(), $this
      ->prophesize(ImageFactory::class)
      ->reveal());
  }
  $focalPointImageWidget::validateFocalPoint($element, $this->testFormState);
}