drupal.test.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. (function($) {
  2. /**
  3. * Test the Drupal.checkPlain function.
  4. */
  5. Drupal.tests.testCheckPlain = {
  6. getInfo: function() {
  7. return {
  8. name: 'Check plain',
  9. description: 'Tests the Drupal.checkPlain() JavaScript function for properly escaping HTML.',
  10. group: 'System'
  11. };
  12. },
  13. test: function() {
  14. expect(9);
  15. // Test basic strings.
  16. equals(Drupal.checkPlain('test'), 'test', Drupal.t("Nothing gets replaced that doesn't need to be replaced with their escaped equivalent."));
  17. equals(Drupal.checkPlain('"test'), '"test', Drupal.t('Quotes are replaced with their escaped equivalent.'));
  18. equals(Drupal.checkPlain('Test&1'), 'Test&1', Drupal.t('Ampersands are replaced with their escaped equivalent.'));
  19. equals(Drupal.checkPlain('Test>test'), 'Test>test', Drupal.t('Greater-than signs are replaced with their escaped equivalent.'));
  20. equals(Drupal.checkPlain('Test<test'), 'Test&lt;test', Drupal.t('Less-than signs are replaced with their escaped equivalent.'));
  21. // Test other data types.
  22. equals(Drupal.checkPlain(['ampers&', 'q"ote']), 'ampers&amp;,q&quot;ote', Drupal.t('Arrays that need to have replacements have them done.'));
  23. equals(Drupal.checkPlain(1), '1', Drupal.t('Integers are left at their equivalent string value.'));
  24. // Combined tests.
  25. equals(Drupal.checkPlain('<tagname property="value">Stuff</tagname>'), '&lt;tagname property=&quot;value&quot;&gt;Stuff&lt;/tagname&gt;', Drupal.t('Full HTML tags are replaced with their escaped equivalent.'));
  26. equals(Drupal.checkPlain('Test "&".'), 'Test &quot;&amp;&quot;.', Drupal.t('A string with both quotes and ampersands replaces those entities with their escaped equivalents.'));
  27. }
  28. };
  29. /**
  30. * Tests Drupal.t().
  31. */
  32. Drupal.tests.testT = {
  33. getInfo: function() {
  34. return {
  35. name: Drupal.t('Translation'),
  36. description: Drupal.t('Tests the basic translation functionality of the Drupal.t() function, including the proper handling of variable strings.'),
  37. group: Drupal.t('System')
  38. };
  39. },
  40. setup: function() {
  41. this.originalLocale = Drupal.locale;
  42. Drupal.locale = {
  43. 'strings': {
  44. 'Translation 1': '1 noitalsnarT',
  45. 'Translation with a @placeholder': '@placeholder a with Translation',
  46. 'Translation with another %placeholder': '%placeholder in another translation',
  47. 'Literal !placeholder': 'A literal !placeholder',
  48. 'Test unspecified placeholder': 'Unspecified placeholder test'
  49. }
  50. };
  51. },
  52. test: function() {
  53. expect(9);
  54. var html = '<tag attribute="value">Apples & Oranges</tag>';
  55. var escaped = '&lt;tag attribute=&quot;value&quot;&gt;Apples &amp; Oranges&lt;/tag&gt;';
  56. // Test placeholders.
  57. equals(Drupal.t('Hello world! @html', {'@html': html}), 'Hello world! ' + escaped, Drupal.t('The "@" placeholder escapes the variable.'));
  58. equals(Drupal.t('Hello world! %html', {'%html': html}), 'Hello world! <em class="placeholder">' + escaped + '</em>', Drupal.t('The "%" placeholder escapes the variable and themes it as a placeholder.'));
  59. equals(Drupal.t('Hello world! !html', {'!html': html}), 'Hello world! ' + html, Drupal.t('The "!" placeholder passes the variable through as-is.'));
  60. equals(Drupal.t('Hello world! html', {'html': html}), 'Hello world! <em class="placeholder">' + escaped + '</em>', Drupal.t('Other placeholders act as "%" placeholders do.'));
  61. // Test actual translations.
  62. equals(Drupal.t('Translation 1'), '1 noitalsnarT', Drupal.t('Basic translations work.'));
  63. equals(Drupal.t('Translation with a @placeholder', {'@placeholder': '<script>alert("xss")</script>'}), '&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt; a with Translation', Drupal.t('Translations with the "@" placeholder work.'));
  64. equals(Drupal.t('Translation with another %placeholder', {'%placeholder': '<script>alert("xss")</script>'}), '<em class="placeholder">&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;</em> in another translation', Drupal.t('Translations with the "%" placeholder work.'));
  65. equals(Drupal.t('Literal !placeholder', {'!placeholder': '<script>alert("xss")</script>'}), 'A literal <script>alert("xss")</script>', Drupal.t('Translations with the "!" placeholder work.'));
  66. equals(Drupal.t('Test unspecified placeholder', {'placeholder': '<script>alert("xss")</script>'}), 'Unspecified <em class="placeholder">&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;</em> test', Drupal.t('Translations with unspecified placeholders work.'));
  67. },
  68. teardown: function() {
  69. }
  70. };
  71. /**
  72. * Tests Drupal.attachBehaviors().
  73. */
  74. Drupal.tests.testBehaviors = {
  75. getInfo: function() {
  76. return {
  77. name: 'JavaScript behaviors',
  78. description: 'Tests the functionality of Drupal behaviors to make sure it allows JavaScript files to attach and detach behaviors in different contexts.',
  79. group: 'System'
  80. };
  81. },
  82. setup: function() {
  83. this.originalBehaviors = Drupal.behaviors;
  84. var attachIndex = 0;
  85. var detachIndex = 0;
  86. Drupal.behaviors = {
  87. testBehavior: {
  88. attach: function(context, settings) {
  89. attachIndex++;
  90. equals(context, 'Attach context ' + attachIndex, Drupal.t('Attach context matches passed context.'));
  91. equals(settings, 'Attach settings ' + attachIndex, Drupal.t('Attach settings match passed settings.'));
  92. },
  93. detach: function(context, settings) {
  94. detachIndex++;
  95. equals(context, 'Detach context ' + detachIndex, Drupal.t('Detach context matches passed context.'));
  96. equals(settings, 'Detach settings ' + detachIndex, Drupal.t('Detach settings match passed settings.'));
  97. }
  98. }
  99. };
  100. },
  101. test: function() {
  102. expect(8);
  103. // Test attaching behaviors.
  104. Drupal.attachBehaviors('Attach context 1', 'Attach settings 1');
  105. // Test attaching behaviors again.
  106. Drupal.attachBehaviors('Attach context 2', 'Attach settings 2');
  107. // Test detaching behaviors.
  108. Drupal.detachBehaviors('Detach context 1', 'Detach settings 1');
  109. // Try detaching behaviors again.
  110. Drupal.detachBehaviors('Detach context 2', 'Detach settings 2');
  111. },
  112. teardown: function() {
  113. Drupal.behaviors = this.originalBehaviors;
  114. }
  115. };
  116. })(jQuery);