Chris@0: imageFactory = $this->container->get('image.factory'); Chris@0: Chris@0: // Pick a file for testing. Chris@0: $file = current($this->drupalGetTestFiles('image')); Chris@0: $this->file = $file->uri; Chris@0: Chris@0: // Setup a dummy image to work with. Chris@0: $this->image = $this->getImage(); Chris@0: Chris@0: // Clear out any hook calls. Chris@0: $this->imageTestReset(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets up an image with the custom toolkit. Chris@0: * Chris@0: * @return \Drupal\Core\Image\ImageInterface Chris@0: * The image object. Chris@0: */ Chris@0: protected function getImage() { Chris@0: $image = $this->imageFactory->get($this->file, 'test'); Chris@0: $this->assertTrue($image->isValid(), 'Image file was parsed.'); Chris@0: return $image; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Assert that all of the specified image toolkit operations were called Chris@0: * exactly once once, other values result in failure. Chris@0: * Chris@0: * @param $expected Chris@0: * Array with string containing with the operation name, e.g. 'load', Chris@0: * 'save', 'crop', etc. Chris@0: */ Chris@0: public function assertToolkitOperationsCalled(array $expected) { Chris@0: // If one of the image operations is expected, apply should be expected as Chris@0: // well. Chris@0: $operations = [ Chris@0: 'resize', Chris@0: 'rotate', Chris@0: 'crop', Chris@0: 'desaturate', Chris@0: 'create_new', Chris@0: 'scale', Chris@0: 'scale_and_crop', Chris@0: 'my_operation', Chris@0: 'convert', Chris@0: ]; Chris@0: if (count(array_intersect($expected, $operations)) > 0 && !in_array('apply', $expected)) { Chris@0: $expected[] = 'apply'; Chris@0: } Chris@0: Chris@0: // Determine which operations were called. Chris@0: $actual = array_keys(array_filter($this->imageTestGetAllCalls())); Chris@0: Chris@0: // Determine if there were any expected that were not called. Chris@0: $uncalled = array_diff($expected, $actual); Chris@0: if (count($uncalled)) { Chris@17: $this->assertTrue(FALSE, new FormattableMarkup('Expected operations %expected to be called but %uncalled was not called.', ['%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled)])); Chris@0: } Chris@0: else { Chris@17: $this->assertTrue(TRUE, new FormattableMarkup('All the expected operations were called: %expected', ['%expected' => implode(', ', $expected)])); Chris@0: } Chris@0: Chris@0: // Determine if there were any unexpected calls. Chris@0: // If all unexpected calls are operations and apply was expected, we do not Chris@0: // count it as an error. Chris@0: $unexpected = array_diff($actual, $expected); Chris@0: if (count($unexpected) && (!in_array('apply', $expected) || count(array_intersect($unexpected, $operations)) !== count($unexpected))) { Chris@17: $this->assertTrue(FALSE, new FormattableMarkup('Unexpected operations were called: %unexpected.', ['%unexpected' => implode(', ', $unexpected)])); Chris@0: } Chris@0: else { Chris@0: $this->assertTrue(TRUE, 'No unexpected operations were called.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Resets/initializes the history of calls to the test toolkit functions. Chris@0: */ Chris@0: public function imageTestReset() { Chris@0: // Keep track of calls to these operations Chris@0: $results = [ Chris@0: 'parseFile' => [], Chris@0: 'save' => [], Chris@0: 'settings' => [], Chris@0: 'apply' => [], Chris@0: 'resize' => [], Chris@0: 'rotate' => [], Chris@0: 'crop' => [], Chris@0: 'desaturate' => [], Chris@0: 'create_new' => [], Chris@0: 'scale' => [], Chris@0: 'scale_and_crop' => [], Chris@0: 'convert' => [], Chris@0: ]; Chris@0: \Drupal::state()->set('image_test.results', $results); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets an array of calls to the test toolkit. Chris@0: * Chris@0: * @return array Chris@0: * An array keyed by operation name ('parseFile', 'save', 'settings', Chris@0: * 'resize', 'rotate', 'crop', 'desaturate') with values being arrays of Chris@0: * parameters passed to each call. Chris@0: */ Chris@0: public function imageTestGetAllCalls() { Chris@0: return \Drupal::state()->get('image_test.results') ?: []; Chris@0: } Chris@0: Chris@0: }