You are here

custom_formatters.test in Custom Formatters 7.2

Tests for the Custom Formatters module.

File

tests/custom_formatters.test
View source
<?php

/**
 * @file
 * Tests for the Custom Formatters module.
 */

/**
 * Class CustomFormattersTestCase.
 */
class CustomFormattersTestCase extends DrupalWebTestCase {
  protected $adminUser = NULL;
  protected $formatter = '';
  protected $node = NULL;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {

    // Setup required modules.
    $modules = func_get_args();
    if (isset($modules[0]) && is_array($modules[0])) {
      $modules = $modules[0];
    }
    $modules[] = 'custom_formatters_test';
    parent::setUp($modules);

    // Create an admin user.
    $this->adminUser = $this
      ->drupalCreateUser(array(
      'administer content types',
      'administer custom formatters',
    ));

    // Create a test node.
    $this->node = $this
      ->drupalCreateNode(array(
      'type' => 'article',
    ));

    // Login as admin user.
    $this
      ->drupalLogin($this->adminUser);
  }

  /**
   * Pass if the Custom Formatter is found.
   *
   * @param string $name
   *   The name of the formatter to check.
   * @param string $message
   *   Message to display.
   * @param string $group
   *   The group this message belong to, default to 'Other'.
   *
   * @return bool
   *   TRUE on pass, FALSE on fail.
   */
  public function assertCustomFormatterExists($name, $message = '', $group = 'Other') {
    $formatter = custom_formatters_crud_load($name);
    $message = empty($message) ? $message : t('Custom Formatter %name found.', array(
      '%name' => $name,
    ));
    return $this
      ->assert(!is_null($formatter), $message, $group);
  }

  /**
   * Create a Custom Formatter.
   *
   * @param array $values
   *   The values to set for the Custom Formatter.
   *
   * @return object
   *   The Custom Formatter object.
   */
  protected function createCustomFormatter($values = array()) {

    // Prepare the default values.
    $name = $this
      ->randomName();
    $defaults = array(
      'label' => $name,
      'name' => drupal_strtolower($name),
      'field_types' => 'text_with_summary',
    );
    $values += $defaults;

    // Create the Custom Formatter.
    ctools_include('export');
    $formatter = ctools_export_crud_new('formatters');
    foreach ($values as $key => $value) {
      $formatter->{$key} = $value;
    }
    $formatter->status = 1;
    ctools_export_crud_save('formatters', $formatter);

    // Flush Drupal caches.
    drupal_flush_all_caches();
    return $formatter;
  }

  /**
   * Set a Custom Formatter to be used by a specified field/bundle/view mode.
   *
   * @param string $formatter_name
   *   A Custom Formatter name.
   * @param string $field_name
   *   A Field name.
   * @param string $bundle_name
   *   A Node content type.
   * @param string $view_mode
   *   A Node view mode.
   */
  protected function setCustomFormatter($formatter_name, $field_name, $bundle_name, $view_mode = 'default') {
    $this
      ->drupalPost("admin/structure/types/manage/{$bundle_name}/display/{$view_mode}", array(
      "fields[{$field_name}][type]" => "custom_formatters_{$formatter_name}",
    ), t('Save'));
    $this
      ->assertText(t('Your settings have been saved.'));
  }

}

Classes

Namesort descending Description
CustomFormattersTestCase Class CustomFormattersTestCase.