Chris@16: drupalGetTestFiles('image'); Chris@16: $file = reset($files); Chris@18: $file_path = \Drupal::service('file_system')->copy($file->uri, 'public://'); Chris@16: } Chris@16: Chris@16: return $style->buildUrl($file_path) ? $file_path : FALSE; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Count the number of images currently create for a style. Chris@16: */ Chris@16: public function getImageCount(ImageStyleInterface $style) { Chris@16: return count(file_scan_directory('public://styles/' . $style->id(), '/.*/')); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Test creating an image style with a numeric name and ensuring it can be Chris@16: * applied to an image. Chris@16: */ Chris@16: public function testNumericStyleName() { Chris@16: $style_name = rand(); Chris@16: $style_label = $this->randomString(); Chris@16: $edit = [ Chris@16: 'name' => $style_name, Chris@16: 'label' => $style_label, Chris@16: ]; Chris@16: $this->drupalPostForm('admin/config/media/image-styles/add', $edit, t('Create new style')); Chris@16: $this->assertRaw(t('Style %name was created.', ['%name' => $style_label])); Chris@16: $options = image_style_options(); Chris@16: $this->assertTrue(array_key_exists($style_name, $options), format_string('Array key %key exists.', ['%key' => $style_name])); Chris@16: } Chris@16: Chris@16: /** Chris@16: * General test to add a style, add/remove/edit effects to it, then delete it. Chris@16: */ Chris@16: public function testStyle() { Chris@16: $admin_path = 'admin/config/media/image-styles'; Chris@16: Chris@16: // Setup a style to be created and effects to add to it. Chris@16: $style_name = strtolower($this->randomMachineName(10)); Chris@16: $style_label = $this->randomString(); Chris@16: $style_path = $admin_path . '/manage/' . $style_name; Chris@16: $effect_edits = [ Chris@16: 'image_resize' => [ Chris@16: 'width' => 100, Chris@16: 'height' => 101, Chris@16: ], Chris@16: 'image_scale' => [ Chris@16: 'width' => 110, Chris@16: 'height' => 111, Chris@16: 'upscale' => 1, Chris@16: ], Chris@16: 'image_scale_and_crop' => [ Chris@16: 'width' => 120, Chris@16: 'height' => 121, Chris@16: ], Chris@16: 'image_crop' => [ Chris@16: 'width' => 130, Chris@16: 'height' => 131, Chris@16: 'anchor' => 'left-top', Chris@16: ], Chris@16: 'image_desaturate' => [ Chris@16: // No options for desaturate. Chris@16: ], Chris@16: 'image_rotate' => [ Chris@16: 'degrees' => 5, Chris@16: 'random' => 1, Chris@16: 'bgcolor' => '#FFFF00', Chris@16: ], Chris@16: ]; Chris@16: Chris@16: // Add style form. Chris@16: Chris@16: $edit = [ Chris@16: 'name' => $style_name, Chris@16: 'label' => $style_label, Chris@16: ]; Chris@16: $this->drupalPostForm($admin_path . '/add', $edit, t('Create new style')); Chris@16: $this->assertRaw(t('Style %name was created.', ['%name' => $style_label])); Chris@16: Chris@16: // Ensure that the expected entity operations are there. Chris@16: $this->drupalGet($admin_path); Chris@16: $this->assertLinkByHref($style_path); Chris@16: $this->assertLinkByHref($style_path . '/flush'); Chris@16: $this->assertLinkByHref($style_path . '/delete'); Chris@16: Chris@16: // Add effect form. Chris@16: Chris@16: // Add each sample effect to the style. Chris@16: foreach ($effect_edits as $effect => $edit) { Chris@16: $edit_data = []; Chris@16: foreach ($edit as $field => $value) { Chris@16: $edit_data['data[' . $field . ']'] = $value; Chris@16: } Chris@16: // Add the effect. Chris@16: $this->drupalPostForm($style_path, ['new' => $effect], t('Add')); Chris@16: if (!empty($edit)) { Chris@16: $this->drupalPostForm(NULL, $edit_data, t('Add effect')); Chris@16: } Chris@16: } Chris@16: Chris@16: // Load the saved image style. Chris@16: $style = ImageStyle::load($style_name); Chris@16: Chris@16: // Ensure that third party settings were added to the config entity. Chris@16: // These are added by a hook_image_style_presave() implemented in Chris@16: // image_module_test module. Chris@16: $this->assertEqual('bar', $style->getThirdPartySetting('image_module_test', 'foo'), 'Third party settings were added to the image style.'); Chris@16: Chris@16: // Ensure that the image style URI matches our expected path. Chris@18: $style_uri_path = $style->toUrl()->toString(); Chris@16: $this->assertTrue(strpos($style_uri_path, $style_path) !== FALSE, 'The image style URI is correct.'); Chris@16: Chris@16: // Confirm that all effects on the image style have settings that match Chris@16: // what was saved. Chris@16: $uuids = []; Chris@16: foreach ($style->getEffects() as $uuid => $effect) { Chris@16: // Store the uuid for later use. Chris@16: $uuids[$effect->getPluginId()] = $uuid; Chris@16: $effect_configuration = $effect->getConfiguration(); Chris@16: foreach ($effect_edits[$effect->getPluginId()] as $field => $value) { Chris@17: $this->assertEqual($value, $effect_configuration['data'][$field], new FormattableMarkup('The %field field in the %effect effect has the correct value of %value.', ['%field' => $field, '%effect' => $effect->getPluginId(), '%value' => $value])); Chris@16: } Chris@16: } Chris@16: Chris@16: // Assert that every effect was saved. Chris@16: foreach (array_keys($effect_edits) as $effect_name) { Chris@16: $this->assertTrue(isset($uuids[$effect_name]), format_string( Chris@16: 'A %effect_name effect was saved with ID %uuid', Chris@16: [ Chris@16: '%effect_name' => $effect_name, Chris@16: '%uuid' => $uuids[$effect_name], Chris@16: ])); Chris@16: } Chris@16: Chris@16: // Image style overview form (ordering and renaming). Chris@16: Chris@16: // Confirm the order of effects is maintained according to the order we Chris@16: // added the fields. Chris@16: $effect_edits_order = array_keys($effect_edits); Chris@16: $order_correct = TRUE; Chris@16: $index = 0; Chris@16: foreach ($style->getEffects() as $effect) { Chris@16: if ($effect_edits_order[$index] != $effect->getPluginId()) { Chris@16: $order_correct = FALSE; Chris@16: } Chris@16: $index++; Chris@16: } Chris@16: $this->assertTrue($order_correct, 'The order of the effects is correctly set by default.'); Chris@16: Chris@16: // Test the style overview form. Chris@16: // Change the name of the style and adjust the weights of effects. Chris@16: $style_name = strtolower($this->randomMachineName(10)); Chris@16: $style_label = $this->randomMachineName(); Chris@16: $weight = count($effect_edits); Chris@16: $edit = [ Chris@16: 'name' => $style_name, Chris@16: 'label' => $style_label, Chris@16: ]; Chris@16: foreach ($style->getEffects() as $uuid => $effect) { Chris@16: $edit['effects[' . $uuid . '][weight]'] = $weight; Chris@16: $weight--; Chris@16: } Chris@16: Chris@16: // Create an image to make sure it gets flushed after saving. Chris@16: $image_path = $this->createSampleImage($style); Chris@16: $this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path])); Chris@16: Chris@17: $this->drupalPostForm($style_path, $edit, t('Save')); Chris@16: Chris@16: // Note that after changing the style name, the style path is changed. Chris@16: $style_path = 'admin/config/media/image-styles/manage/' . $style_name; Chris@16: Chris@16: // Check that the URL was updated. Chris@16: $this->drupalGet($style_path); Chris@16: $this->assertTitle(t('Edit style @name | Drupal', ['@name' => $style_label])); Chris@16: $this->assertResponse(200, format_string('Image style %original renamed to %new', ['%original' => $style->id(), '%new' => $style_name])); Chris@16: Chris@16: // Check that the available image effects are properly sorted. Chris@16: $option = $this->xpath('//select[@id=:id]//option', [':id' => 'edit-new--2']); Chris@16: $this->assertEquals('Ajax test', $option[1]->getText(), '"Ajax test" is the first selectable effect.'); Chris@16: Chris@16: // Check that the image was flushed after updating the style. Chris@16: // This is especially important when renaming the style. Make sure that Chris@16: // the old image directory has been deleted. Chris@16: $this->assertEqual($this->getImageCount($style), 0, format_string('Image style %style was flushed after renaming the style and updating the order of effects.', ['%style' => $style->label()])); Chris@16: Chris@16: // Load the style by the new name with the new weights. Chris@16: $style = ImageStyle::load($style_name); Chris@16: Chris@16: // Confirm the new style order was saved. Chris@16: $effect_edits_order = array_reverse($effect_edits_order); Chris@16: $order_correct = TRUE; Chris@16: $index = 0; Chris@16: foreach ($style->getEffects() as $effect) { Chris@16: if ($effect_edits_order[$index] != $effect->getPluginId()) { Chris@16: $order_correct = FALSE; Chris@16: } Chris@16: $index++; Chris@16: } Chris@16: $this->assertTrue($order_correct, 'The order of the effects is correctly set by default.'); Chris@16: Chris@16: // Image effect deletion form. Chris@16: Chris@16: // Create an image to make sure it gets flushed after deleting an effect. Chris@16: $image_path = $this->createSampleImage($style); Chris@16: $this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path])); Chris@16: Chris@16: // Delete the 'image_crop' effect from the style. Chris@16: $this->drupalPostForm($style_path . '/effects/' . $uuids['image_crop'] . '/delete', [], t('Delete')); Chris@16: // Confirm that the form submission was successful. Chris@16: $this->assertResponse(200); Chris@16: $image_crop_effect = $style->getEffect($uuids['image_crop']); Chris@16: $this->assertRaw(t('The image effect %name has been deleted.', ['%name' => $image_crop_effect->label()])); Chris@16: // Confirm that there is no longer a link to the effect. Chris@16: $this->assertNoLinkByHref($style_path . '/effects/' . $uuids['image_crop'] . '/delete'); Chris@16: // Refresh the image style information and verify that the effect was Chris@16: // actually deleted. Chris@16: $entity_type_manager = $this->container->get('entity_type.manager'); Chris@16: $style = $entity_type_manager->getStorage('image_style')->loadUnchanged($style->id()); Chris@16: $this->assertFalse($style->getEffects()->has($uuids['image_crop']), format_string( Chris@16: 'Effect with ID %uuid no longer found on image style %style', Chris@16: [ Chris@16: '%uuid' => $uuids['image_crop'], Chris@16: '%style' => $style->label(), Chris@16: ])); Chris@16: Chris@16: // Additional test on Rotate effect, for transparent background. Chris@16: $edit = [ Chris@16: 'data[degrees]' => 5, Chris@16: 'data[random]' => 0, Chris@16: 'data[bgcolor]' => '', Chris@16: ]; Chris@16: $this->drupalPostForm($style_path, ['new' => 'image_rotate'], t('Add')); Chris@16: $this->drupalPostForm(NULL, $edit, t('Add effect')); Chris@16: $entity_type_manager = $this->container->get('entity_type.manager'); Chris@16: $style = $entity_type_manager->getStorage('image_style')->loadUnchanged($style_name); Chris@16: $this->assertEqual(count($style->getEffects()), 6, 'Rotate effect with transparent background was added.'); Chris@16: Chris@16: // Style deletion form. Chris@16: Chris@16: // Delete the style. Chris@16: $this->drupalPostForm($style_path . '/delete', [], t('Delete')); Chris@16: Chris@16: // Confirm the style directory has been removed. Chris@16: $directory = file_default_scheme() . '://styles/' . $style_name; Chris@16: $this->assertFalse(is_dir($directory), format_string('Image style %style directory removed on style deletion.', ['%style' => $style->label()])); Chris@16: Chris@16: $this->assertFalse(ImageStyle::load($style_name), format_string('Image style %style successfully deleted.', ['%style' => $style->label()])); Chris@16: Chris@16: // Test empty text when there are no image styles. Chris@16: Chris@16: // Delete all image styles. Chris@16: foreach (ImageStyle::loadMultiple() as $image_style) { Chris@16: $image_style->delete(); Chris@16: } Chris@16: Chris@16: // Confirm that the empty text is correct on the image styles page. Chris@16: $this->drupalGet($admin_path); Chris@16: $this->assertRaw(t('There are currently no styles. Add a new one.', [ Chris@18: ':url' => Url::fromRoute('image.style_add')->toString(), Chris@16: ])); Chris@16: Chris@16: } Chris@16: Chris@16: /** Chris@16: * Test deleting a style and choosing a replacement style. Chris@16: */ Chris@16: public function testStyleReplacement() { Chris@16: // Create a new style. Chris@16: $style_name = strtolower($this->randomMachineName(10)); Chris@16: $style_label = $this->randomString(); Chris@16: $style = ImageStyle::create(['name' => $style_name, 'label' => $style_label]); Chris@16: $style->save(); Chris@16: $style_path = 'admin/config/media/image-styles/manage/'; Chris@16: Chris@16: // Create an image field that uses the new style. Chris@16: $field_name = strtolower($this->randomMachineName(10)); Chris@16: $this->createImageField($field_name, 'article'); Chris@16: entity_get_display('node', 'article', 'default') Chris@16: ->setComponent($field_name, [ Chris@16: 'type' => 'image', Chris@16: 'settings' => ['image_style' => $style_name], Chris@16: ]) Chris@16: ->save(); Chris@16: Chris@16: // Create a new node with an image attached. Chris@16: $test_image = current($this->drupalGetTestFiles('image')); Chris@16: $nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName()); Chris@16: $node = Node::load($nid); Chris@16: Chris@16: // Get node field original image URI. Chris@16: $fid = $node->get($field_name)->target_id; Chris@16: $original_uri = File::load($fid)->getFileUri(); Chris@16: Chris@16: // Test that image is displayed using newly created style. Chris@16: $this->drupalGet('node/' . $nid); Chris@16: $this->assertRaw(file_url_transform_relative($style->buildUrl($original_uri)), format_string('Image displayed using style @style.', ['@style' => $style_name])); Chris@16: Chris@16: // Rename the style and make sure the image field is updated. Chris@16: $new_style_name = strtolower($this->randomMachineName(10)); Chris@16: $new_style_label = $this->randomString(); Chris@16: $edit = [ Chris@16: 'name' => $new_style_name, Chris@16: 'label' => $new_style_label, Chris@16: ]; Chris@17: $this->drupalPostForm($style_path . $style_name, $edit, t('Save')); Chris@16: $this->assertText(t('Changes to the style have been saved.'), format_string('Style %name was renamed to %new_name.', ['%name' => $style_name, '%new_name' => $new_style_name])); Chris@16: $this->drupalGet('node/' . $nid); Chris@16: Chris@16: // Reload the image style using the new name. Chris@16: $style = ImageStyle::load($new_style_name); Chris@16: $this->assertRaw(file_url_transform_relative($style->buildUrl($original_uri)), 'Image displayed using style replacement style.'); Chris@16: Chris@16: // Delete the style and choose a replacement style. Chris@16: $edit = [ Chris@16: 'replacement' => 'thumbnail', Chris@16: ]; Chris@16: $this->drupalPostForm($style_path . $new_style_name . '/delete', $edit, t('Delete')); Chris@16: $message = t('The image style %name has been deleted.', ['%name' => $new_style_label]); Chris@16: $this->assertRaw($message); Chris@16: Chris@16: $replacement_style = ImageStyle::load('thumbnail'); Chris@16: $this->drupalGet('node/' . $nid); Chris@16: $this->assertRaw(file_url_transform_relative($replacement_style->buildUrl($original_uri)), 'Image displayed using style replacement style.'); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Verifies that editing an image effect does not cause it to be duplicated. Chris@16: */ Chris@16: public function testEditEffect() { Chris@16: // Add a scale effect. Chris@16: $style_name = 'test_style_effect_edit'; Chris@16: $this->drupalGet('admin/config/media/image-styles/add'); Chris@16: $this->drupalPostForm(NULL, ['label' => 'Test style effect edit', 'name' => $style_name], t('Create new style')); Chris@16: $this->drupalPostForm(NULL, ['new' => 'image_scale_and_crop'], t('Add')); Chris@16: $this->drupalPostForm(NULL, ['data[width]' => '300', 'data[height]' => '200'], t('Add effect')); Chris@16: $this->assertText(t('Scale and crop 300×200')); Chris@16: Chris@16: // There should normally be only one edit link on this page initially. Chris@16: $this->clickLink(t('Edit')); Chris@16: $this->drupalPostForm(NULL, ['data[width]' => '360', 'data[height]' => '240'], t('Update effect')); Chris@16: $this->assertText(t('Scale and crop 360×240')); Chris@16: Chris@16: // Check that the previous effect is replaced. Chris@16: $this->assertNoText(t('Scale and crop 300×200')); Chris@16: Chris@16: // Add another scale effect. Chris@16: $this->drupalGet('admin/config/media/image-styles/add'); Chris@16: $this->drupalPostForm(NULL, ['label' => 'Test style scale edit scale', 'name' => 'test_style_scale_edit_scale'], t('Create new style')); Chris@16: $this->drupalPostForm(NULL, ['new' => 'image_scale'], t('Add')); Chris@16: $this->drupalPostForm(NULL, ['data[width]' => '12', 'data[height]' => '19'], t('Add effect')); Chris@16: Chris@16: // Edit the scale effect that was just added. Chris@16: $this->clickLink(t('Edit')); Chris@16: $this->drupalPostForm(NULL, ['data[width]' => '24', 'data[height]' => '19'], t('Update effect')); Chris@16: Chris@16: // Add another scale effect and make sure both exist. Click through from Chris@16: // the overview to make sure that it is possible to add new effect then. Chris@16: $this->drupalGet('admin/config/media/image-styles'); Chris@16: $rows = $this->xpath('//table/tbody/tr'); Chris@16: $i = 0; Chris@16: foreach ($rows as $row) { Chris@16: if ($row->find('css', 'td')->getText() === 'Test style scale edit scale') { Chris@16: $this->clickLink('Edit', $i); Chris@16: break; Chris@16: } Chris@16: $i++; Chris@16: } Chris@16: $this->drupalPostForm(NULL, ['new' => 'image_scale'], t('Add')); Chris@16: $this->drupalPostForm(NULL, ['data[width]' => '12', 'data[height]' => '19'], t('Add effect')); Chris@16: $this->assertText(t('Scale 24×19')); Chris@16: $this->assertText(t('Scale 12×19')); Chris@16: Chris@16: // Try to edit a nonexistent effect. Chris@16: $uuid = $this->container->get('uuid'); Chris@16: $this->drupalGet('admin/config/media/image-styles/manage/' . $style_name . '/effects/' . $uuid->generate()); Chris@16: $this->assertResponse(404); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Test flush user interface. Chris@16: */ Chris@16: public function testFlushUserInterface() { Chris@16: $admin_path = 'admin/config/media/image-styles'; Chris@16: Chris@16: // Create a new style. Chris@16: $style_name = strtolower($this->randomMachineName(10)); Chris@16: $style = ImageStyle::create(['name' => $style_name, 'label' => $this->randomString()]); Chris@16: $style->save(); Chris@16: Chris@16: // Create an image to make sure it gets flushed. Chris@16: $files = $this->drupalGetTestFiles('image'); Chris@16: $image_uri = $files[0]->uri; Chris@16: $derivative_uri = $style->buildUri($image_uri); Chris@16: $this->assertTrue($style->createDerivative($image_uri, $derivative_uri)); Chris@16: $this->assertEqual($this->getImageCount($style), 1); Chris@16: Chris@16: // Go to image styles list page and check if the flush operation link Chris@16: // exists. Chris@16: $this->drupalGet($admin_path); Chris@16: $flush_path = $admin_path . '/manage/' . $style_name . '/flush'; Chris@16: $this->assertLinkByHref($flush_path); Chris@16: Chris@16: // Flush the image style derivatives using the user interface. Chris@16: $this->drupalPostForm($flush_path, [], t('Flush')); Chris@16: Chris@16: // The derivative image file should have been deleted. Chris@16: $this->assertEqual($this->getImageCount($style), 0); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests image style configuration import that does a delete. Chris@16: */ Chris@16: public function testConfigImport() { Chris@16: // Create a new style. Chris@16: $style_name = strtolower($this->randomMachineName(10)); Chris@16: $style_label = $this->randomString(); Chris@16: $style = ImageStyle::create(['name' => $style_name, 'label' => $style_label]); Chris@16: $style->save(); Chris@16: Chris@16: // Create an image field that uses the new style. Chris@16: $field_name = strtolower($this->randomMachineName(10)); Chris@16: $this->createImageField($field_name, 'article'); Chris@16: entity_get_display('node', 'article', 'default') Chris@16: ->setComponent($field_name, [ Chris@16: 'type' => 'image', Chris@16: 'settings' => ['image_style' => $style_name], Chris@16: ]) Chris@16: ->save(); Chris@16: Chris@16: // Create a new node with an image attached. Chris@16: $test_image = current($this->drupalGetTestFiles('image')); Chris@16: $nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName()); Chris@16: $node = Node::load($nid); Chris@16: Chris@16: // Get node field original image URI. Chris@16: $fid = $node->get($field_name)->target_id; Chris@16: $original_uri = File::load($fid)->getFileUri(); Chris@16: Chris@16: // Test that image is displayed using newly created style. Chris@16: $this->drupalGet('node/' . $nid); Chris@16: $this->assertRaw(file_url_transform_relative($style->buildUrl($original_uri)), format_string('Image displayed using style @style.', ['@style' => $style_name])); Chris@16: Chris@16: // Copy config to sync, and delete the image style. Chris@16: $sync = $this->container->get('config.storage.sync'); Chris@16: $active = $this->container->get('config.storage'); Chris@16: // Remove the image field from the display, to avoid a dependency error Chris@16: // during import. Chris@16: EntityViewDisplay::load('node.article.default') Chris@16: ->removeComponent($field_name) Chris@16: ->save(); Chris@16: $this->copyConfig($active, $sync); Chris@16: $sync->delete('image.style.' . $style_name); Chris@16: $this->configImporter()->import(); Chris@16: Chris@16: $this->assertFalse(ImageStyle::load($style_name), 'Style deleted after config import.'); Chris@16: $this->assertEqual($this->getImageCount($style), 0, 'Image style was flushed after being deleted by config import.'); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests access for the image style listing. Chris@16: */ Chris@16: public function testImageStyleAccess() { Chris@16: $style = ImageStyle::create(['name' => 'style_foo', 'label' => $this->randomString()]); Chris@16: $style->save(); Chris@16: Chris@16: $this->drupalGet('admin/config/media/image-styles'); Chris@16: $this->clickLink(t('Edit')); Chris@16: $this->assertRaw(t('Select a new effect')); Chris@16: } Chris@16: Chris@16: }