You are here

class TestToolkit in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php \Drupal\image_test\Plugin\ImageToolkit\TestToolkit

Defines a Test toolkit for image manipulation within Drupal.

Plugin annotation


@ImageToolkit(
  id = "test",
  title = @Translation("A dummy toolkit that works")
)

Hierarchy

Expanded class hierarchy of TestToolkit

File

core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php, line 27
Contains \Drupal\image_test\Plugin\ImageToolkit\TestToolkit.

Namespace

Drupal\image_test\Plugin\ImageToolkit
View source
class TestToolkit extends ImageToolkitBase {

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * Image type represented by a PHP IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG).
   *
   * @var int
   */
  protected $type;

  /**
   * The width of the image.
   *
   * @var int
   */
  protected $width;

  /**
   * The height of the image.
   *
   * @var int
   */
  protected $height;

  /**
   * Constructs a TestToolkit object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param array $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface $operation_manager
   *   The toolkit operation manager.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state key value store.
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitOperationManagerInterface $operation_manager, LoggerInterface $logger, ConfigFactoryInterface $config_factory, StateInterface $state) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $operation_manager, $logger, $config_factory);
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('image.toolkit.operation.manager'), $container
      ->get('logger.channel.image'), $container
      ->get('config.factory'), $container
      ->get('state'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $this
      ->logCall('settings', func_get_args());
    $form['test_parameter'] = array(
      '#type' => 'number',
      '#title' => $this
        ->t('Test toolkit parameter'),
      '#description' => $this
        ->t('A toolkit parameter for testing purposes.'),
      '#min' => 0,
      '#max' => 100,
      '#default_value' => $this->configFactory
        ->getEditable('system.image.test_toolkit')
        ->get('test_parameter', FALSE),
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getValue([
      'test',
      'test_parameter',
    ]) == 0) {
      $form_state
        ->setErrorByName('test][test_parameter', $this
        ->t('Test parameter should be different from 0.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configFactory
      ->getEditable('system.image.test_toolkit')
      ->set('test_parameter', $form_state
      ->getValue(array(
      'test',
      'test_parameter',
    )))
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  public function isValid() {
    return isset($this->type);
  }

  /**
   * {@inheritdoc}
   */
  public function parseFile() {
    $this
      ->logCall('parseFile', func_get_args());
    $data = @getimagesize($this
      ->getSource());
    if ($data && in_array($data[2], static::supportedTypes())) {
      $this
        ->setType($data[2]);
      $this->width = $data[0];
      $this->height = $data[1];
      return TRUE;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function save($destination) {
    $this
      ->logCall('save', func_get_args());

    // Return false so that image_save() doesn't try to chmod the destination
    // file that we didn't bother to create.
    return FALSE;
  }

  /**
   * Stores the values passed to a toolkit call.
   *
   * @param string $op
   *   One of the toolkit methods 'parseFile', 'save', 'settings', or 'apply'.
   * @param array $args
   *   Values passed to hook.
   *
   * @see \Drupal\system\Tests\Image\ToolkitTestBase::imageTestReset()
   * @see \Drupal\system\Tests\Image\ToolkitTestBase::imageTestGetAllCalls()
   */
  protected function logCall($op, $args) {
    $results = $this->state
      ->get('image_test.results') ?: array();
    $results[$op][] = $args;

    // A call to apply is also logged under its operation name whereby the
    // array of arguments are logged as separate arguments, this because at the
    // ImageInterface level we still have methods named after the operations.
    if ($op === 'apply') {
      $operation = array_shift($args);
      $results[$operation][] = array_values(reset($args));
    }
    $this->state
      ->set('image_test.results', $results);
  }

  /**
   * {@inheritdoc}
   */
  public function getWidth() {
    return $this->width;
  }

  /**
   * {@inheritdoc}
   */
  public function getHeight() {
    return $this->height;
  }

  /**
   * Returns the type of the image.
   *
   * @return int
   *   The image type represented by a PHP IMAGETYPE_* constant (e.g.
   *   IMAGETYPE_JPEG).
   */
  public function getType() {
    return $this->type;
  }

  /**
   * Sets the PHP type of the image.
   *
   * @param int $type
   *   The image type represented by a PHP IMAGETYPE_* constant (e.g.
   *   IMAGETYPE_JPEG).
   *
   * @return $this
   */
  public function setType($type) {
    if (in_array($type, static::supportedTypes())) {
      $this->type = $type;
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getMimeType() {
    return $this
      ->getType() ? image_type_to_mime_type($this
      ->getType()) : '';
  }

  /**
   * {@inheritdoc}
   */
  public static function isAvailable() {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSupportedExtensions() {
    $extensions = array();
    foreach (static::supportedTypes() as $image_type) {
      $extensions[] = Unicode::strtolower(image_type_to_extension($image_type, FALSE));
    }
    return $extensions;
  }

  /**
   * Returns a list of image types supported by the toolkit.
   *
   * @return array
   *   An array of available image types. An image type is represented by a PHP
   *   IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG, IMAGETYPE_PNG, etc.).
   */
  protected static function supportedTypes() {
    return array(
      IMAGETYPE_PNG,
      IMAGETYPE_JPEG,
      IMAGETYPE_GIF,
    );
  }

  /**
   * {@inheritdoc}
   */
  public function apply($operation, array $arguments = array()) {
    $this
      ->logCall('apply', func_get_args());
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
ImageToolkitBase::$configFactory protected property The config factory.
ImageToolkitBase::$logger protected property A logger instance.
ImageToolkitBase::$operationManager protected property The image toolkit operation manager.
ImageToolkitBase::$source protected property Path of the image file.
ImageToolkitBase::getRequirements public function Gets toolkit requirements in a format suitable for hook_requirements(). Overrides ImageToolkitInterface::getRequirements 1
ImageToolkitBase::getSource public function Gets the source path of the image file. Overrides ImageToolkitInterface::getSource
ImageToolkitBase::getToolkitOperation protected function Gets a toolkit operation plugin instance.
ImageToolkitBase::setSource public function Sets the source path of the image file. Overrides ImageToolkitInterface::setSource
PluginBase::$configuration protected property Configuration information passed into the plugin. 2
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TestToolkit::$height protected property The height of the image.
TestToolkit::$state protected property The state service.
TestToolkit::$type protected property Image type represented by a PHP IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG).
TestToolkit::$width protected property The width of the image.
TestToolkit::apply public function Applies a toolkit operation to an image. Overrides ImageToolkitBase::apply
TestToolkit::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
TestToolkit::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
TestToolkit::getHeight public function Returns the height of the image. Overrides ImageToolkitInterface::getHeight
TestToolkit::getMimeType public function Returns the MIME type of the image file. Overrides ImageToolkitInterface::getMimeType
TestToolkit::getSupportedExtensions public static function Returns a list of image file extensions supported by the toolkit. Overrides ImageToolkitInterface::getSupportedExtensions
TestToolkit::getType public function Returns the type of the image.
TestToolkit::getWidth public function Returns the width of the image. Overrides ImageToolkitInterface::getWidth
TestToolkit::isAvailable public static function Verifies that the Image Toolkit is set up correctly. Overrides ImageToolkitInterface::isAvailable 1
TestToolkit::isValid public function Checks if the image is valid. Overrides ImageToolkitInterface::isValid
TestToolkit::logCall protected function Stores the values passed to a toolkit call.
TestToolkit::parseFile public function Determines if a file contains a valid image. Overrides ImageToolkitInterface::parseFile
TestToolkit::save public function Writes an image resource to a destination file. Overrides ImageToolkitInterface::save
TestToolkit::setType public function Sets the PHP type of the image.
TestToolkit::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
TestToolkit::supportedTypes protected static function Returns a list of image types supported by the toolkit.
TestToolkit::validateConfigurationForm public function Form validation handler. Overrides ImageToolkitBase::validateConfigurationForm
TestToolkit::__construct public function Constructs a TestToolkit object. Overrides ImageToolkitBase::__construct