diff --git a/src/FocalPointEffectBase.php b/src/FocalPointEffectBase.php index 420c3d0..d58e903 100644 --- a/src/FocalPointEffectBase.php +++ b/src/FocalPointEffectBase.php @@ -205,7 +205,7 @@ abstract class FocalPointEffectBase extends ResizeImageEffect implements Contain // Check if we are generating a preview image. If so get the focal point // from the query parameter, otherwise use the crop position. - $preview_value = \Drupal::request()->query->get('focal_point_preview_value'); // @todo: unit tests break because of this. + $preview_value = $this->getPreviewValue(); if (is_null($preview_value)) { $focal_point = $crop->position(); } @@ -237,4 +237,13 @@ abstract class FocalPointEffectBase extends ResizeImageEffect implements Contain return $anchor; } + + /** + * Get the 'focal_point_preview_value' query string value. + * + * @return string|NULL + */ + protected function getPreviewValue() { + return \Drupal::request()->query->get('focal_point_preview_value'); + } } diff --git a/tests/src/Unit/Effects/FocalPointEffectsTest.php b/tests/src/Unit/Effects/FocalPointEffectsTest.php index ec6ae69..b082d9a 100644 --- a/tests/src/Unit/Effects/FocalPointEffectsTest.php +++ b/tests/src/Unit/Effects/FocalPointEffectsTest.php @@ -97,11 +97,15 @@ class FocalPointEffectsTest extends UnitTestCase { ]); // Use reflection to test a private/protected method. - $effect = new FocalPointCropImageEffect([], 'plugin_id', [], $logger->reveal(), $focal_point_manager, $crop_storage->reveal(), $immutable_config->reveal()); - $effect_reflection = new \ReflectionClass(FocalPointCropImageEffect::class); + $effect = new TestFocalPointEffectBase([], 'plugin_id', [], $logger->reveal(), $focal_point_manager, $crop_storage->reveal(), $immutable_config->reveal()); + $effect_reflection = new \ReflectionClass(TestFocalPointEffectBase::class); $method = $effect_reflection->getMethod('calculateAnchor'); $method->setAccessible(TRUE); $this->assertSame($expected_anchor, $method->invokeArgs($effect, [$image->reveal(), $crop->reveal(), $original_image_size])); + + $effect->setTestingPreview(TRUE); + $expected_anchor = ['x' => 0, 'y' => 0]; + $this->assertSame($expected_anchor, $method->invokeArgs($effect, [$image->reveal(), $crop->reveal(), $original_image_size])); } /** @@ -251,3 +255,25 @@ class FocalPointEffectsTest extends UnitTestCase { } } + +class TestFocalPointEffectBase extends FocalPointEffectBase { + + /** + * @var bool + */ + protected $testingPreview = FALSE; + + /** + * @return null|string + */ + protected function getPreviewValue() { + return $this->testingPreview ? '0x0' : NULL; + } + + /** + * @param bool $testing_preview + */ + public function setTestingPreview($testing_preview) { + $this->testingPreview = $testing_preview; + } +} \ No newline at end of file