You are here

tmgmt.base.entity.test in Translation Management Tool 7

File

tests/tmgmt.base.entity.test
View source
<?php

/*
 * @file
 * Contains tests for Translation management
 */

/**
 * Utility test case class with helper methods to create entities and their
 * fields with populated translatable content. Extend this class if you create
 * tests in which you need Drupal entities and/or fields.
 */
abstract class TMGMTEntityTestCaseUtility extends TMGMTBaseTestCase {
  public $field_names = array();

  /**
   * Creates node type with several text fields with different cardinality.
   *
   * Internally it calls TMGMTEntityTestCaseUtility::attachFields() to create
   * and attach fields to newly created bundle. You can than use
   * $this->field_names['node']['YOUR_BUNDLE_NAME'] to access them.
   *
   * @param string $machine_name
   *   Machine name of the node type.
   * @param string $human_name
   *   Human readable name of the node type.
   * @param int $language_content_type
   *   Either 0 (disabled), 1 (language enabled but no translations),
   *   TRANSLATION_ENABLED or ENTITY_TRANSLATION_ENABLED.
   * pparam bool $attach_fields
   *   (optional) If fields with the same translatability should automatically
   *   be attached to the node type.
   */
  function createNodeType($machine_name, $human_name, $language_content_type = 0, $attach_fields = TRUE) {

    // Create new bundle.
    $type = array(
      'type' => $machine_name,
      'name' => $human_name,
      'base' => 'node_content',
      'description' => '',
      'custom' => 1,
      'modified' => 1,
      'locked' => 0,
    );
    $type = node_type_set_defaults($type);
    node_type_save($type);
    node_add_body_field($type);
    node_types_rebuild();

    // Set content type to be translatable as specified by
    // $language_content_type.
    $edit = array();
    $edit['language_content_type'] = $language_content_type;
    $this
      ->drupalPost('admin/structure/types/manage/' . $machine_name, $edit, t('Save content type'));
    $translatable = FALSE;
    if (defined('ENTITY_TRANSLATION_ENABLED') && $language_content_type == ENTITY_TRANSLATION_ENABLED) {
      $translatable = TRUE;
    }

    // Push in also the body field.
    $this->field_names['node'][$machine_name][] = 'body';
    if ($attach_fields) {
      $this
        ->attachFields('node', $machine_name, $translatable);
    }

    // Change body field to be translatable.
    $body = field_info_field('body');
    $body['translatable'] = $translatable;
    field_update_field($body);
  }

  /**
   * Creates taxonomy vocabulary with custom fields.
   *
   * To create and attach fields it internally calls
   * TMGMTEntityTestCaseUtility::attachFields(). You can than access these
   * fields calling $this->field_names['node']['YOUR_BUNDLE_NAME'].
   *
   * @param string $machine_name
   *   Vocabulary machine name.
   * @param string $human_name
   *   Vocabulary human readable name.
   * @param bool|array $fields_translatable
   *   Flag or definition array to determine which or all fields should be
   *   translatable.
   *
   * @return stdClass
   *   Created vocabulary object.
   */
  function createTaxonomyVocab($machine_name, $human_name, $fields_translatable = TRUE) {
    $vocabulary = new stdClass();
    $vocabulary->name = $human_name;
    $vocabulary->machine_name = $machine_name;
    taxonomy_vocabulary_save($vocabulary);
    $this
      ->attachFields('taxonomy_term', $vocabulary->machine_name, $fields_translatable);
    $info = variable_get('entity_translation_taxonomy', array());
    $info[$machine_name] = TRUE;
    variable_set('entity_translation_taxonomy', $info);
    return $vocabulary;
  }

  /**
   * Creates fields of type text and text_with_summary of different cardinality.
   *
   * It will attach created fields to provided entity name and bundle.
   *
   * Field names will be stored in $this->field_names['entity']['bundle']
   * through which you can access them.
   *
   * @param string $entity_name
   *   Entity name to which fields should be attached.
   * @param string $bundle
   *   Bundle name to which fields should be attached.
   * @param bool|array $translatable
   *   Flag or definition array to determine which or all fields should be
   *   translatable.
   */
  function attachFields($entity_name, $bundle, $translatable = TRUE) {

    // Create several text fields.
    $field_types = array(
      'text',
      'text_with_summary',
    );
    for ($i = 0; $i <= 5; $i++) {
      $field_type = $field_types[array_rand($field_types, 1)];
      $field_name = drupal_strtolower($this
        ->randomName());

      // Create a field.
      $field = array(
        'field_name' => $field_name,
        'type' => $field_type,
        'cardinality' => mt_rand(1, 5),
        'translatable' => is_array($translatable) && isset($translatable[$i]) ? $translatable[$i] : (bool) $translatable,
      );
      field_create_field($field);

      // Create an instance of the previously created field.
      $instance = array(
        'field_name' => $field_name,
        'entity_type' => $entity_name,
        'bundle' => $bundle,
        'label' => $this
          ->randomName(10),
        'description' => $this
          ->randomString(30),
        'widget' => array(
          'type' => $field_type == 'text' ? 'text_textfield' : 'text_textarea_with_summary',
          'label' => $this
            ->randomString(10),
        ),
      );
      field_create_instance($instance);

      // Store field names in case there are needed outside this method.
      $this->field_names[$entity_name][$bundle][] = $field_name;
    }
  }

  /**
   * Creates a node of a given bundle.
   *
   * It uses $this->field_names to populate content of attached fields.
   *
   * @param string $bundle
   *   Node type name.
   * @param string $sourcelang
   *   Source lang of the node to be created.
   *
   * @return object
   *   Newly created node object.
   */
  function createNode($bundle, $sourcelang = 'en') {
    $node = array(
      'type' => $bundle,
      'language' => $sourcelang,
      // Ensure that the body field is defined for the node language.
      'body' => array(
        $sourcelang => array(
          0 => array(),
        ),
      ),
    );
    foreach ($this->field_names['node'][$bundle] as $field_name) {
      $field_info = field_info_field($field_name);
      $cardinality = $field_info['cardinality'] == FIELD_CARDINALITY_UNLIMITED ? 1 : $field_info['cardinality'];
      $field_langcode = field_is_translatable('node', $field_info) ? $sourcelang : LANGUAGE_NONE;

      // Create two deltas for each field.
      for ($delta = 0; $delta <= $cardinality; $delta++) {
        $node[$field_name][$field_langcode][$delta]['value'] = $this
          ->randomName(20);
        if ($field_info['type'] == 'text_with_summary') {
          $node[$field_name][$field_langcode][$delta]['summary'] = $this
            ->randomName(10);
        }
      }
    }
    return $this
      ->drupalCreateNode($node);
  }

  /**
   * Creates a taxonomy term of a given vocabulary.
   *
   * It uses $this->field_names to populate content of attached fields. You can
   * access fields values using
   * $this->field_names['taxonomy_term'][$vocabulary->machine_name].
   *
   * @param object $vocabulary
   *   Vocabulary object for which the term should be created.
   *
   * @param string $langcode
   *   The language code to be set as the entity source language.
   *
   * @return object
   *   Newly created node object.
   */
  function createTaxonomyTerm($vocabulary, $langcode = 'en') {

    // When an entity is being saved, the entity_translation module initializes
    // a translation fetching the language from an entity. But the taxonomy
    // terms have no entity language key, so its langcode will be the set to the
    // default one.

    /* @see entity_translation_field_attach_insert() */

    /* @see EntityTranslationDefaultHandler::initTranslations() */

    /* @see EntityTranslationDefaultHandler::getLanguage() */
    $settings_variable_name = 'entity_translation_settings_taxonomy_term__' . $vocabulary->machine_name;
    variable_set($settings_variable_name, array(
      'default_language' => $langcode,
    ));
    $term = new stdClass();
    $term->name = $this
      ->randomName();
    $term->description = $this
      ->randomName();
    $term->vid = $vocabulary->vid;
    foreach ($this->field_names['taxonomy_term'][$vocabulary->machine_name] as $field_name) {
      $field_info = field_info_field($field_name);
      $cardinality = $field_info['cardinality'] == FIELD_CARDINALITY_UNLIMITED ? 1 : $field_info['cardinality'];
      $field_lang = $field_info['translatable'] ? $langcode : LANGUAGE_NONE;

      // Create two deltas for each field.
      for ($delta = 0; $delta <= $cardinality; $delta++) {
        $term->{$field_name}[$field_lang][$delta]['value'] = $this
          ->randomName(20);
        if ($field_info['type'] == 'text_with_summary') {
          $term->{$field_name}[$field_lang][$delta]['summary'] = $this
            ->randomName(10);
        }
      }
    }
    taxonomy_term_save($term);
    return taxonomy_term_load($term->tid);
  }

}

Classes

Namesort descending Description
TMGMTEntityTestCaseUtility Utility test case class with helper methods to create entities and their fields with populated translatable content. Extend this class if you create tests in which you need Drupal entities and/or fields.