Chris@16: drupalGetTestFiles('image'); Chris@16: $file = reset($files); Chris@16: } Chris@16: Chris@16: // Make sure we have an image in our wrapper testing file directory. Chris@18: $source_uri = \Drupal::service('file_system')->copy($file->uri, $wrapper . '://'); Chris@16: // Build the derivative image. Chris@16: $derivative_uri = $style->buildUri($source_uri); Chris@16: $derivative = $style->createDerivative($source_uri, $derivative_uri); Chris@16: Chris@16: return $derivative ? $derivative_uri : FALSE; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Count the number of images currently created for a style in a wrapper. Chris@16: */ Chris@16: public function getImageCount($style, $wrapper) { Chris@16: return count(file_scan_directory($wrapper . '://styles/' . $style->id(), '/.*/')); Chris@16: } Chris@16: Chris@16: /** Chris@16: * General test to flush a style. Chris@16: */ Chris@16: public function testFlush() { 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/config/media/image-styles/manage/' . $style_name; Chris@16: $effect_edits = [ Chris@16: 'image_resize' => [ Chris@16: 'data[width]' => 100, Chris@16: 'data[height]' => 101, Chris@16: ], Chris@16: 'image_scale' => [ Chris@16: 'data[width]' => 110, Chris@16: 'data[height]' => 111, Chris@16: 'data[upscale]' => 1, Chris@16: ], Chris@16: ]; Chris@16: Chris@16: // Add style form. 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: Chris@16: // Add each sample effect to the style. Chris@16: foreach ($effect_edits as $effect => $edit) { 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, 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: // Create an image for the 'public' wrapper. Chris@16: $image_path = $this->createSampleImage($style, 'public'); Chris@16: // Expecting to find 2 images, one is the sample.png image shown in Chris@16: // image style preview. Chris@16: $this->assertEqual($this->getImageCount($style, 'public'), 2, format_string('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path])); Chris@16: Chris@16: // Create an image for the 'private' wrapper. Chris@16: $image_path = $this->createSampleImage($style, 'private'); Chris@16: $this->assertEqual($this->getImageCount($style, 'private'), 1, format_string('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path])); Chris@16: Chris@16: // Remove the 'image_scale' effect and updates the style, which in turn Chris@16: // forces an image style flush. Chris@16: $style_path = 'admin/config/media/image-styles/manage/' . $style->id(); Chris@16: $uuids = []; Chris@16: foreach ($style->getEffects() as $uuid => $effect) { Chris@16: $uuids[$effect->getPluginId()] = $uuid; Chris@16: } Chris@16: $this->drupalPostForm($style_path . '/effects/' . $uuids['image_scale'] . '/delete', [], t('Delete')); Chris@16: $this->assertResponse(200); Chris@17: $this->drupalPostForm($style_path, [], t('Save')); Chris@16: $this->assertResponse(200); Chris@16: Chris@16: // Post flush, expected 1 image in the 'public' wrapper (sample.png). Chris@16: $this->assertEqual($this->getImageCount($style, 'public'), 1, format_string('Image style %style flushed correctly for %wrapper wrapper.', ['%style' => $style->label(), '%wrapper' => 'public'])); Chris@16: Chris@16: // Post flush, expected no image in the 'private' wrapper. Chris@16: $this->assertEqual($this->getImageCount($style, 'private'), 0, format_string('Image style %style flushed correctly for %wrapper wrapper.', ['%style' => $style->label(), '%wrapper' => 'private'])); Chris@16: } Chris@16: Chris@16: }