Chris@0: manager = $this->container->get('plugin.manager.image.effect'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test the image_resize_effect() function. Chris@0: */ Chris@0: public function testResizeEffect() { Chris@0: $this->assertImageEffect('image_resize', [ Chris@0: 'width' => 1, Chris@0: 'height' => 2, Chris@0: ]); Chris@0: $this->assertToolkitOperationsCalled(['resize']); Chris@0: Chris@0: // Check the parameters. Chris@0: $calls = $this->imageTestGetAllCalls(); Chris@0: $this->assertEqual($calls['resize'][0][0], 1, 'Width was passed correctly'); Chris@0: $this->assertEqual($calls['resize'][0][1], 2, 'Height was passed correctly'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test the image_scale_effect() function. Chris@0: */ Chris@0: public function testScaleEffect() { Chris@0: // @todo: need to test upscaling. Chris@0: $this->assertImageEffect('image_scale', [ Chris@0: 'width' => 10, Chris@0: 'height' => 10, Chris@0: ]); Chris@0: $this->assertToolkitOperationsCalled(['scale']); Chris@0: Chris@0: // Check the parameters. Chris@0: $calls = $this->imageTestGetAllCalls(); Chris@0: $this->assertEqual($calls['scale'][0][0], 10, 'Width was passed correctly'); Chris@0: $this->assertEqual($calls['scale'][0][1], 10, 'Height was based off aspect ratio and passed correctly'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test the image_crop_effect() function. Chris@0: */ Chris@0: public function testCropEffect() { Chris@0: // @todo should test the keyword offsets. Chris@0: $this->assertImageEffect('image_crop', [ Chris@0: 'anchor' => 'top-1', Chris@0: 'width' => 3, Chris@0: 'height' => 4, Chris@0: ]); Chris@0: $this->assertToolkitOperationsCalled(['crop']); Chris@0: Chris@0: // Check the parameters. Chris@0: $calls = $this->imageTestGetAllCalls(); Chris@0: $this->assertEqual($calls['crop'][0][0], 0, 'X was passed correctly'); Chris@0: $this->assertEqual($calls['crop'][0][1], 1, 'Y was passed correctly'); Chris@0: $this->assertEqual($calls['crop'][0][2], 3, 'Width was passed correctly'); Chris@0: $this->assertEqual($calls['crop'][0][3], 4, 'Height was passed correctly'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the ConvertImageEffect plugin. Chris@0: */ Chris@0: public function testConvertEffect() { Chris@0: // Test jpeg. Chris@0: $this->assertImageEffect('image_convert', [ Chris@0: 'extension' => 'jpeg', Chris@0: ]); Chris@0: $this->assertToolkitOperationsCalled(['convert']); Chris@0: Chris@0: // Check the parameters. Chris@0: $calls = $this->imageTestGetAllCalls(); Chris@0: $this->assertEqual($calls['convert'][0][0], 'jpeg', 'Extension was passed correctly'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test the image_scale_and_crop_effect() function. Chris@0: */ Chris@0: public function testScaleAndCropEffect() { Chris@0: $this->assertImageEffect('image_scale_and_crop', [ Chris@0: 'width' => 5, Chris@0: 'height' => 10, Chris@0: ]); Chris@0: $this->assertToolkitOperationsCalled(['scale_and_crop']); Chris@0: Chris@0: // Check the parameters. Chris@0: $calls = $this->imageTestGetAllCalls(); Chris@17: $this->assertEqual($calls['scale_and_crop'][0][0], 7.5, 'X was computed and passed correctly'); Chris@17: $this->assertEqual($calls['scale_and_crop'][0][1], 0, 'Y was computed and passed correctly'); Chris@17: $this->assertEqual($calls['scale_and_crop'][0][2], 5, 'Width was computed and passed correctly'); Chris@17: $this->assertEqual($calls['scale_and_crop'][0][3], 10, 'Height was computed and passed correctly'); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Test the image_scale_and_crop_effect() function with an anchor. Chris@17: */ Chris@17: public function testScaleAndCropEffectWithAnchor() { Chris@17: $this->assertImageEffect('image_scale_and_crop', [ Chris@17: 'anchor' => 'top-1', Chris@17: 'width' => 5, Chris@17: 'height' => 10, Chris@17: ]); Chris@17: $this->assertToolkitOperationsCalled(['scale_and_crop']); Chris@17: Chris@17: // Check the parameters. Chris@17: $calls = $this->imageTestGetAllCalls(); Chris@17: $this->assertEqual($calls['scale_and_crop'][0][0], 0, 'X was computed and passed correctly'); Chris@17: $this->assertEqual($calls['scale_and_crop'][0][1], 1, 'Y was computed and passed correctly'); Chris@17: $this->assertEqual($calls['scale_and_crop'][0][2], 5, 'Width was computed and passed correctly'); Chris@17: $this->assertEqual($calls['scale_and_crop'][0][3], 10, 'Height was computed and passed correctly'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test the image_desaturate_effect() function. Chris@0: */ Chris@0: public function testDesaturateEffect() { Chris@0: $this->assertImageEffect('image_desaturate', []); Chris@0: $this->assertToolkitOperationsCalled(['desaturate']); Chris@0: Chris@0: // Check the parameters. Chris@0: $calls = $this->imageTestGetAllCalls(); Chris@0: $this->assertEqual(count($calls['desaturate'][0]), 0, 'No parameters were passed.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test the image_rotate_effect() function. Chris@0: */ Chris@0: public function testRotateEffect() { Chris@0: // @todo: need to test with 'random' => TRUE Chris@0: $this->assertImageEffect('image_rotate', [ Chris@0: 'degrees' => 90, Chris@0: 'bgcolor' => '#fff', Chris@0: ]); Chris@0: $this->assertToolkitOperationsCalled(['rotate']); Chris@0: Chris@0: // Check the parameters. Chris@0: $calls = $this->imageTestGetAllCalls(); Chris@0: $this->assertEqual($calls['rotate'][0][0], 90, 'Degrees were passed correctly'); Chris@0: $this->assertEqual($calls['rotate'][0][1], '#fff', 'Background color was passed correctly'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test image effect caching. Chris@0: */ Chris@0: public function testImageEffectsCaching() { Chris@0: $image_effect_definitions_called = &drupal_static('image_module_test_image_effect_info_alter'); Chris@0: Chris@0: // First call should grab a fresh copy of the data. Chris@0: $manager = $this->container->get('plugin.manager.image.effect'); Chris@0: $effects = $manager->getDefinitions(); Chris@0: $this->assertTrue($image_effect_definitions_called === 1, 'image_effect_definitions() generated data.'); Chris@0: Chris@0: // Second call should come from cache. Chris@0: drupal_static_reset('image_module_test_image_effect_info_alter'); Chris@0: $cached_effects = $manager->getDefinitions(); Chris@0: $this->assertTrue($image_effect_definitions_called === 0, 'image_effect_definitions() returned data from cache.'); Chris@0: Chris@0: $this->assertTrue($effects == $cached_effects, 'Cached effects are the same as generated effects.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests if validation errors are passed plugin form to the parent form. Chris@0: */ Chris@0: public function testEffectFormValidationErrors() { Chris@0: $account = $this->drupalCreateUser(['administer image styles']); Chris@0: $this->drupalLogin($account); Chris@0: /** @var \Drupal\image\ImageStyleInterface $style */ Chris@0: $style = ImageStyle::load('thumbnail'); Chris@0: // Image Scale is the only effect shipped with 'thumbnail', by default. Chris@0: $uuids = $style->getEffects()->getInstanceIds(); Chris@0: $uuid = key($uuids); Chris@0: Chris@0: // We are posting the form with both, width and height, empty. Chris@0: $edit = ['data[width]' => '', 'data[height]' => '']; Chris@0: $path = 'admin/config/media/image-styles/manage/thumbnail/effects/' . $uuid; Chris@0: $this->drupalPostForm($path, $edit, t('Update effect')); Chris@0: // Check that the error message has been displayed. Chris@0: $this->assertText(t('Width and height can not both be blank.')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts the effect processing of an image effect plugin. Chris@0: * Chris@0: * @param string $effect_name Chris@0: * The name of the image effect to test. Chris@0: * @param array $data Chris@0: * The data to pass to the image effect. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the assertion succeeded, FALSE otherwise. Chris@0: */ Chris@0: protected function assertImageEffect($effect_name, array $data) { Chris@0: $effect = $this->manager->createInstance($effect_name, ['data' => $data]); Chris@0: return $this->assertTrue($effect->applyEffect($this->image), 'Function returned the expected value.'); Chris@0: } Chris@0: Chris@0: }