You are here

TamperPlugins.test in Tamper 7

Contains tests for Tamper plugins.

File

src/Tests/TamperPlugins.test
View source
<?php

/**
 * @file
 * Contains tests for Tamper plugins.
 */

/**
 * Base class for Feeds Tamper plugin tests.
 */
abstract class TamperPluginsTestBase extends DrupalUnitTestCase {
  public function setUp() {
    $this->moduleDirectory = dirname(dirname(dirname(__FILE__)));

    // Load main Tamper classes.
    require_once $this->moduleDirectory . '/src/Tamper.php';
    $this
      ->loadPlugin($this->moduleDirectory);
    parent::setUp();
  }

  /**
   * The plugin id being tested.
   *
   * @var string
   */
  protected $pluginId;

  /**
   * The plugin info array.
   *
   * @var array
   */
  protected $pluginInfo;

  /**
   * The module directory.
   *
   * @var string
   */
  protected $moduleDirectory;

  /**
   * Loads the plugin info and the plugin class.
   *
   * Modules implementing their own tests can change this as necessary.
   *
   * @param string $module_directory
   *   The root path of the module directory.
   */
  protected function loadPlugin($module_directory) {
    $plugin = NULL;
    require_once $module_directory . '/plugins/' . $this->pluginId . '.inc';
    $plugin['class'] = array(
      'class' => $plugin['class'],
    );
    $this->pluginInfo = $plugin;
    $class = $this->pluginInfo['class']['class'];
    require_once $module_directory . '/src/Plugins/' . $class . '.php';
  }

  /**
   * Returns a new plugin definition.
   *
   * @return TamperPluginDefinition
   *   A plugin definition.
   */
  protected function getPluginDefinition() {
    return new TamperPluginDefinition($this->pluginId, $this->pluginInfo);
  }

  /**
   * Returns a new plugin instance.
   *
   * @param array $settings
   *   (optional) The initial settings.
   *
   * @return TamperPluginInterface
   *   A tamper plugin instance.
   */
  protected function getPluginInstance(array $settings = array()) {
    return $this
      ->getPluginDefinition()
      ->createInstance($settings);
  }

}

/**
 * Tests for convert_case.
 */
class TamperConvertCaseTest extends TamperPluginsTestBase {

  /**
   * {@inheritdoc}
   */
  protected $pluginId = 'convert_case';
  public static function getInfo() {
    return array(
      'name' => 'Plugin: Convert case',
      'description' => 'Tests for the convert case plugin.',
      'group' => 'Tamper',
    );
  }

  /**
   * Basic functionality tests.
   */
  public function test() {
    $plugin = $this
      ->getPluginInstance();
    $this
      ->assertEqual('TEXT', $plugin
      ->execute('text'));
    $plugin
      ->setConfiguration(array(
      'mode' => 'lower',
    ));
    $this
      ->assertEqual('text', $plugin
      ->execute('TEXT'));
    $plugin
      ->setConfiguration(array(
      'mode' => 'ucfirst',
    ));
    $this
      ->assertEqual('Text', $plugin
      ->execute('text'));
    $plugin
      ->setConfiguration(array(
      'mode' => 'ucwords',
    ));
    $this
      ->assertEqual('This Is The Text', $plugin
      ->execute('this is the text'));
  }

}

Classes

Namesort descending Description
TamperConvertCaseTest Tests for convert_case.
TamperPluginsTestBase Base class for Feeds Tamper plugin tests.