tmgmt.helper.test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /*
  3. * @file
  4. * Contains tests for Translation management
  5. */
  6. /**
  7. * Test the helper functions in tmgmt.module.
  8. */
  9. class TMGMTHelperTestCase extends TMGMTBaseTestCase {
  10. static function getInfo() {
  11. return array(
  12. 'name' => 'Helper functions Test case',
  13. 'description' => 'Helper functions for other modules',
  14. 'group' => 'Translation Management',
  15. );
  16. }
  17. /**
  18. * Tests tmgmt_job_match_item()
  19. *
  20. * @see tmgmt_job_match_item
  21. */
  22. function testTMGTJobMatchItem() {
  23. $this->loginAsAdmin();
  24. $this->setEnvironment('fr');
  25. $this->setEnvironment('es');
  26. // Add a job from en to fr and en to sp.
  27. $job_en_fr = $this->createJob('en', 'fr');
  28. $job_en_sp = $this->createJob('en', 'es');
  29. // Add a job which has existing source-target combinations.
  30. $this->assertEqual($job_en_fr->tjid, tmgmt_job_match_item('en', 'fr')->tjid);
  31. $this->assertEqual($job_en_sp->tjid, tmgmt_job_match_item('en', 'es')->tjid);
  32. // Add a job which has no existing source-target combination.
  33. $this->assertTrue(tmgmt_job_match_item('fr', 'es'));
  34. }
  35. /**
  36. * Tests the tmgmt_data_item_label() function.
  37. *
  38. * @todo: Move into a unit test case once available.
  39. */
  40. function testDataIemLabel() {
  41. $no_label = array(
  42. '#text' => 'No label',
  43. );
  44. $this->assertEqual(tmgmt_data_item_label($no_label), 'No label');
  45. $this->assertEqual(tmgmt_data_item_label($no_label, 6), 'No ...');
  46. $label = array(
  47. '#parent_label' => array(),
  48. '#label' => 'A label',
  49. );
  50. $this->assertEqual(tmgmt_data_item_label($label), 'A label');
  51. $this->assertEqual(tmgmt_data_item_label($label, 6), 'A l...');
  52. $parent_label = array(
  53. '#parent_label' => array('Parent label', 'Sub label'),
  54. '#label' => 'A label',
  55. );
  56. $this->assertEqual(tmgmt_data_item_label($parent_label), 'Parent label > Sub label');
  57. $this->assertEqual(tmgmt_data_item_label($parent_label, 18), 'Pare... > Sub ...');
  58. $nested = array(
  59. '#parent_label' => array('Parent label', 'Sub label', 'Sub-sub label'),
  60. '#label' => 'A label',
  61. );
  62. $this->assertEqual(tmgmt_data_item_label($nested), 'Parent label > Sub label > Sub-sub label');
  63. $this->assertEqual(tmgmt_data_item_label($nested, 28), 'Pare... > Sub ... > Sub-...');
  64. $long_label = array(
  65. '#parent_label' => array('Loooooooooooong label', 'Short'),
  66. '#label' => 'A label',
  67. );
  68. $this->assertEqual(tmgmt_data_item_label($long_label), 'Loooooooooooong label > Short');
  69. $this->assertEqual(tmgmt_data_item_label($long_label, 30), 'Loooooooooooong label > Short');
  70. $node_example = array(
  71. '#parent_label' => array('This is a very loooong title, so looong', 'Body', 'Delta #0', 'Body'),
  72. '#label' => 'A label',
  73. );
  74. $this->assertEqual(tmgmt_data_item_label($node_example), 'This is a very loooong title, so looong > Body > Delta #0 > Body');
  75. $this->assertEqual(tmgmt_data_item_label($node_example, 56), 'This is a very loooong title... > Body > Delta #0 > Body');
  76. }
  77. function testWordCount() {
  78. $unit_tests = array(
  79. 'empty' => array(
  80. 'text' => '',
  81. 'count' => 0,
  82. ),
  83. 'latin' => array(
  84. 'text' => 'Drupal is the best!',
  85. 'count' => 4,
  86. ),
  87. 'non-latin' => array(
  88. 'text' => 'Друпал лучший!',
  89. 'count' => 2,
  90. ),
  91. 'complex punctuation' => array(
  92. 'text' => '<[({-!ReAd@*;: ,?+MoRe...})]>\\|/',
  93. 'count' => 2,
  94. 'exclude_tags' => FALSE,
  95. ),
  96. 'repeat' => array(
  97. 'text' => 'repeat repeat',
  98. 'count' => 2,
  99. ),
  100. 'strip tags' => array(
  101. 'text' => '<a href="http://example.com">link text</a> plain text <div class="some-css-class"></div>',
  102. 'count' => 4,
  103. ),
  104. );
  105. foreach ($unit_tests as $id => $test_data) {
  106. // Set the exclude_tags flag. In case not provided the TRUE is default.
  107. $test_data += array('exclude_tags' => TRUE);
  108. if (variable_get('tmgmt_word_count_exclude_tags', TRUE) != $test_data['exclude_tags']) {
  109. variable_set('tmgmt_word_count_exclude_tags', $test_data['exclude_tags']);
  110. }
  111. $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)));
  112. }
  113. }
  114. }