You are here

RestfulViewEntityMultiLingualTestCase.test in RESTful 7

Same filename and directory in other branches
  1. 7.2 tests/RestfulViewEntityMultiLingualTestCase.test

Contains RestfulViewEntityMultiLingualTestCase

File

tests/RestfulViewEntityMultiLingualTestCase.test
View source
<?php

/**
 * @file
 * Contains RestfulViewEntityMultiLingualTestCase
 */
class RestfulViewEntityMultiLingualTestCase extends RestfulCurlBaseTestCase {

  /**
   * The user to run the tests as.
   */
  protected $testUser;
  public static function getInfo() {
    return array(
      'name' => 'View multilingual entity',
      'description' => 'Test the viewing of a multilingual entity.',
      'group' => 'RESTful',
    );
  }
  function setUp() {
    parent::setUp('restful_example', 'restful_test', 'entityreference', 'locale');
    $this->testUser = $this
      ->drupalCreateUser(array(
      'administer languages',
      'access administration pages',
      'administer site configuration',
    ));
    require_once DRUPAL_ROOT . '/includes/locale.inc';
    $this
      ->drupalLogin($this->testUser);
    $languages = language_list();
    if (!isset($languages['en'])) {
      locale_add_language('en', NULL, NULL, LANGUAGE_LTR, '', 'en');
    }
    if (!isset($languages['fr'])) {
      locale_add_language('fr', NULL, NULL, LANGUAGE_LTR, '', 'fr');
    }
    $this
      ->addTestFields();
  }

  /**
   * Add test fields.
   */
  protected function addTestFields() {

    // Text - single.
    $field = array(
      'field_name' => 'text_single',
      'type' => 'text_long',
      'entity_types' => array(
        'restful_test_translatable_entity',
      ),
      'cardinality' => 1,
      'translatable' => TRUE,
    );
    field_create_field($field);
    $instance = array(
      'field_name' => 'text_single',
      'bundle' => 'restful_test_translatable_entity',
      'entity_type' => 'restful_test_translatable_entity',
      'label' => t('Text single'),
      'settings' => array(
        'text_processing' => 0,
      ),
    );
    field_create_instance($instance);

    // Text - multiple.
    $field = array(
      'field_name' => 'text_multiple',
      'type' => 'text_long',
      'entity_types' => array(
        'restful_test_translatable_entity',
      ),
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      'translatable' => TRUE,
    );
    field_create_field($field);
    $instance = array(
      'field_name' => 'text_multiple',
      'bundle' => 'restful_test_translatable_entity',
      'entity_type' => 'restful_test_translatable_entity',
      'label' => t('Text multiple'),
      'settings' => array(
        'text_processing' => 0,
      ),
    );
    field_create_instance($instance);
  }

  /**
   * Test viewing an entity with translatable fields.
   */
  function testViewMultiLangualEntity() {
    $user = $this
      ->drupalCreateUser();
    $values = array(
      'name' => 'restful_test_translatable_entity',
      'uid' => $user->uid,
      'label' => 'Test translation',
    );
    $entity = entity_create('restful_test_translatable_entity', $values);
    $wrapper = entity_metadata_wrapper('restful_test_translatable_entity', $entity);
    $text1 = array(
      'en' => $this
        ->randomName(),
      'fr' => $this
        ->randomName(),
    );
    $text2 = array(
      'en' => $this
        ->randomName(),
      'fr' => $this
        ->randomName(),
    );
    foreach (array(
      'en',
      'fr',
    ) as $langcode) {
      $wrapper
        ->language($langcode);
      $wrapper->text_single
        ->set($text1[$langcode]);
      $wrapper->text_multiple
        ->set(array(
        $text1[$langcode],
        $text2[$langcode],
      ));
    }
    $wrapper
      ->save();
    $id = $entity->pid;
    foreach (array(
      'en',
      'fr',
    ) as $langcode) {
      $this
        ->assertExpectedResult($langcode, $id, $text1[$langcode], $text2[$langcode]);
    }
  }

  /**
   * Helper to test viewing an entity (GET method) in a certain language.
   *
   * @param string $langcode
   *   The language to view the entity in.
   * @param int $id
   *   The ID of the entity to test.
   * @param string $text1
   *   The first expected string (text_single and text_multiple[0]).
   * @param string $text2
   *   The second expected string (text_multiple[2]).
   */
  private function assertExpectedResult($langcode, $id, $text1, $text2) {
    $handler = restful_get_restful_handler('restful_test_translatable_entity');

    // Explicitly set the langcode.
    $handler
      ->setLangCode($langcode);
    $response = $handler
      ->get($id);
    $result = $response[0];
    $this
      ->assertEqual(trim(strip_tags($result['text_single'])), $text1, 'Entity view has correct result for "text_single" in language "' . $langcode . '".');
    $this
      ->assertEqual(trim(strip_tags($result['text_multiple'][0])), $text1, 'Entity view has correct result for the first value of "text_multiple" in language "' . $langcode . '".');
    $this
      ->assertEqual(trim(strip_tags($result['text_multiple'][1])), $text2, 'Entity view has correct result for the second value of "text_multiple" in language "' . $langcode . '".');
  }

}

Classes

Namesort descending Description
RestfulViewEntityMultiLingualTestCase @file Contains RestfulViewEntityMultiLingualTestCase