LocaleJavascriptTranslationTest.php 3.44 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\locale\Tests\LocaleJavascriptTranslationTest.
6 7 8 9 10
 */

namespace Drupal\locale\Tests;

use Drupal\simpletest\WebTestBase;
11
use Drupal\Component\Utility\String;
12 13 14 15

/**
 * Functional tests for JavaScript parsing for translatable strings.
 */
16
class LocaleJavascriptTranslationTest extends WebTestBase {
17 18 19 20 21 22 23 24

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('locale');

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  public static function getInfo() {
    return array(
      'name' => 'Javascript translation',
      'description' => 'Tests parsing js files for translatable strings',
      'group' => 'Locale',
    );
  }

  function testFileParsing() {
    $filename = drupal_get_path('module', 'locale') . '/tests/locale_test.js';

    // Parse the file to look for source strings.
    _locale_parse_js_file($filename);

    // Get all of the source strings that were found.
40 41 42 43 44 45
    $strings = $this->container
      ->get('locale.storage')
      ->getStrings(array(
        'type' => 'javascript',
        'name' => $filename,
      ));
46 47

    $source_strings = array();
48
    foreach ($strings as $string) {
49 50
      $source_strings[$string->source] = $string->context;
    }
51 52

    $etx = LOCALE_PLURAL_DELIMITER;
53 54
    // List of all strings that should be in the file.
    $test_strings = array(
55 56
      'Standard Call t' => '',
      'Whitespace Call t' => '',
57

58
      'Single Quote t' => '',
59
      "Single Quote \\'Escaped\\' t" => '',
60
      'Single Quote Concat strings t' => '',
61

62
      'Double Quote t' => '',
63
      "Double Quote \\\"Escaped\\\" t" => '',
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
      'Double Quote Concat strings t' => '',

      'Context !key Args t' => 'Context string',

      'Context Unquoted t' => 'Context string unquoted',
      'Context Single Quoted t' => 'Context string single quoted',
      'Context Double Quoted t' => 'Context string double quoted',

      "Standard Call plural{$etx}Standard Call @count plural" => '',
      "Whitespace Call plural{$etx}Whitespace Call @count plural" => '',

      "Single Quote plural{$etx}Single Quote @count plural" => '',
      "Single Quote \\'Escaped\\' plural{$etx}Single Quote \\'Escaped\\' @count plural" => '',

      "Double Quote plural{$etx}Double Quote @count plural" => '',
      "Double Quote \\\"Escaped\\\" plural{$etx}Double Quote \\\"Escaped\\\" @count plural" => '',

      "Context !key Args plural{$etx}Context !key Args @count plural" => 'Context string',

      "Context Unquoted plural{$etx}Context Unquoted @count plural" => 'Context string unquoted',
      "Context Single Quoted plural{$etx}Context Single Quoted @count plural" => 'Context string single quoted',
      "Context Double Quoted plural{$etx}Context Double Quoted @count plural" => 'Context string double quoted',
86 87 88 89 90 91 92
    );

    // Assert that all strings were found properly.
    foreach ($test_strings as $str => $context) {
      $args = array('%source' => $str, '%context' => $context);

      // Make sure that the string was found in the file.
93
      $this->assertTrue(isset($source_strings[$str]), String::format('Found source string: %source', $args));
94 95

      // Make sure that the proper context was matched.
96 97
      $message = $context ? String::format('Context for %source is %context', $args) : String::format('Context for %source is blank', $args);
      $this->assertTrue(isset($source_strings[$str]) && $source_strings[$str] === $context, $message);
98 99
    }

100
    $this->assertEqual(count($source_strings), count($test_strings), 'Found correct number of source strings.');
101 102
  }
}