You are here

tmgmt.helper.test in Translation Management Tool 7

File

tests/tmgmt.helper.test
View source
<?php

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

/**
 * Test the helper functions in tmgmt.module.
 */
class TMGMTHelperTestCase extends TMGMTBaseTestCase {
  static function getInfo() {
    return array(
      'name' => 'Helper functions Test case',
      'description' => 'Helper functions for other modules',
      'group' => 'Translation Management',
    );
  }

  /**
   * Tests tmgmt_job_match_item()
   *
   * @see tmgmt_job_match_item
   */
  function testTMGTJobMatchItem() {
    $this
      ->loginAsAdmin();
    $this
      ->setEnvironment('fr');
    $this
      ->setEnvironment('es');

    // Add a job from en to fr and en to sp.
    $job_en_fr = $this
      ->createJob('en', 'fr');
    $job_en_sp = $this
      ->createJob('en', 'es');

    // Add a job which has existing source-target combinations.
    $this
      ->assertEqual($job_en_fr->tjid, tmgmt_job_match_item('en', 'fr')->tjid);
    $this
      ->assertEqual($job_en_sp->tjid, tmgmt_job_match_item('en', 'es')->tjid);

    // Add a job which has no existing source-target combination.
    $this
      ->assertTrue(tmgmt_job_match_item('fr', 'es'));
  }

  /**
   * Tests the tmgmt_data_item_label() function.
   *
   * @todo: Move into a unit test case once available.
   */
  function testDataIemLabel() {
    $no_label = array(
      '#text' => 'No label',
    );
    $this
      ->assertEqual(tmgmt_data_item_label($no_label), 'No label');
    $this
      ->assertEqual(tmgmt_data_item_label($no_label, 6), 'No ...');
    $label = array(
      '#parent_label' => array(),
      '#label' => 'A label',
    );
    $this
      ->assertEqual(tmgmt_data_item_label($label), 'A label');
    $this
      ->assertEqual(tmgmt_data_item_label($label, 6), 'A l...');
    $parent_label = array(
      '#parent_label' => array(
        'Parent label',
        'Sub label',
      ),
      '#label' => 'A label',
    );
    $this
      ->assertEqual(tmgmt_data_item_label($parent_label), 'Parent label > Sub label');
    $this
      ->assertEqual(tmgmt_data_item_label($parent_label, 18), 'Pare... > Sub ...');
    $nested = array(
      '#parent_label' => array(
        'Parent label',
        'Sub label',
        'Sub-sub label',
      ),
      '#label' => 'A label',
    );
    $this
      ->assertEqual(tmgmt_data_item_label($nested), 'Parent label > Sub label > Sub-sub label');
    $this
      ->assertEqual(tmgmt_data_item_label($nested, 28), 'Pare... > Sub ... > Sub-...');
    $long_label = array(
      '#parent_label' => array(
        'Loooooooooooong label',
        'Short',
      ),
      '#label' => 'A label',
    );
    $this
      ->assertEqual(tmgmt_data_item_label($long_label), 'Loooooooooooong label > Short');
    $this
      ->assertEqual(tmgmt_data_item_label($long_label, 30), 'Loooooooooooong label > Short');
    $node_example = array(
      '#parent_label' => array(
        'This is a very loooong title, so looong',
        'Body',
        'Delta #0',
        'Body',
      ),
      '#label' => 'A label',
    );
    $this
      ->assertEqual(tmgmt_data_item_label($node_example), 'This is a very loooong title, so looong > Body > Delta #0 > Body');
    $this
      ->assertEqual(tmgmt_data_item_label($node_example, 56), 'This is a very loooong title... > Body > Delta #0 > Body');
  }
  function testWordCount() {
    $unit_tests = array(
      'empty' => array(
        'text' => '',
        'count' => 0,
      ),
      'latin' => array(
        'text' => 'Drupal is the best!',
        'count' => 4,
      ),
      'non-latin' => array(
        'text' => 'Друпал лучший!',
        'count' => 2,
      ),
      'complex punctuation' => array(
        'text' => '<[({-!ReAd@*;: ,?+MoRe...})]>\\|/',
        'count' => 2,
        'exclude_tags' => FALSE,
      ),
      'repeat' => array(
        'text' => 'repeat repeat',
        'count' => 2,
      ),
      'strip tags' => array(
        'text' => '<a href="http://example.com">link text</a> plain text <div class="some-css-class"></div>',
        'count' => 4,
      ),
    );
    foreach ($unit_tests as $id => $test_data) {

      // Set the exclude_tags flag. In case not provided the TRUE is default.
      $test_data += array(
        'exclude_tags' => TRUE,
      );
      if (variable_get('tmgmt_word_count_exclude_tags', TRUE) != $test_data['exclude_tags']) {
        variable_set('tmgmt_word_count_exclude_tags', $test_data['exclude_tags']);
      }
      $this
        ->assertEqual($real_count = tmgmt_word_count($test_data['text']), $desirable_count = $test_data['count'], t('!test_id: Real count (=!real_count) should be equal to desirable (=!desirable_count)', array(
        '!test_id' => $id,
        '!real_count' => $real_count,
        '!desirable_count' => $desirable_count,
      )));
    }
  }

}

Classes

Namesort descending Description
TMGMTHelperTestCase Test the helper functions in tmgmt.module.