Explorar o código

finalizing tests for I18n class, AJAX loading of translations needs to be tested in browser, mocked for now

El RIDO %!s(int64=9) %!d(string=hai) anos
pai
achega
37f5d99bc4
Modificáronse 5 ficheiros con 80 adicións e 11 borrados
  1. 1 0
      .gitignore
  2. 5 5
      js/privatebin.js
  3. 72 4
      js/test.js
  4. 1 1
      tpl/bootstrap.php
  5. 1 1
      tpl/page.php

+ 1 - 0
.gitignore

@@ -30,6 +30,7 @@ vendor/**/build_phar.php
 
 # Ignore local node modules, unit testing logs, api docs and eclipse project files
 js/node_modules/
+js/test.log
 tst/log/
 tst/ConfigurationCombinationsTest.php
 .settings

+ 5 - 5
js/privatebin.js

@@ -306,7 +306,7 @@ jQuery.PrivateBin = function($, sjcl, Base64, RawDeflate) {
      * @param  {object} document
      * @class
      */
-    var I18n = (function (window, document) {
+    var I18n = (function () {
         var me = {};
 
         /**
@@ -544,14 +544,14 @@ jQuery.PrivateBin = function($, sjcl, Base64, RawDeflate) {
          * @name   I18n.reset
          * @function
          */
-        me.reset = function()
+        me.reset = function(mockLanguage, mockTranslations)
         {
-            language = null;
-            translations = {};
+            language = mockLanguage || null;
+            translations = mockTranslations || {};
         }
 
         return me;
-    })(window, document);
+    })();
 
     /**
      * handles everything related to en/decryption

+ 72 - 4
js/test.js

@@ -13,7 +13,9 @@ var jsc = require('jsverify'),
         })
     ),
     // schemas supported by the whatwg-url library
-    schemas = ['ftp','gopher','http','https','ws','wss'];
+    schemas = ['ftp','gopher','http','https','ws','wss'],
+    supportedLanguages = ['de', 'es', 'fr', 'it', 'no', 'pl', 'pt', 'oc', 'ru', 'sl', 'zh'],
+    logFile = require('fs').createWriteStream('test.log');
 
 global.$ = global.jQuery = require('./jquery-3.1.1');
 global.sjcl = require('./sjcl-1.0.6');
@@ -22,6 +24,11 @@ global.RawDeflate = require('./rawdeflate-0.5');
 require('./rawinflate-0.3');
 require('./privatebin');
 
+// redirect console messages to log file
+console.warn = console.error = function (msg) {
+    logFile.write(msg + '\n');
+}
+
 describe('Helper', function () {
     describe('secondsToHuman', function () {
         after(function () {
@@ -339,12 +346,30 @@ describe('I18n', function () {
             'returns message ID unchanged if no translation found',
             'string',
             function (messageId) {
-                messageId = messageId.replace(/%(s|d)/g, '%%');
-                var result = $.PrivateBin.I18n.translate(messageId);
+                messageId   = messageId.replace(/%(s|d)/g, '%%');
+                var plurals = [messageId, messageId + 's'],
+                    fake    = [messageId],
+                    result  = $.PrivateBin.I18n.translate(messageId);
                 $.PrivateBin.I18n.reset();
+
                 var alias = $.PrivateBin.I18n._(messageId);
                 $.PrivateBin.I18n.reset();
-                return messageId === result && messageId === alias;
+
+                var p_result = $.PrivateBin.I18n.translate(plurals);
+                $.PrivateBin.I18n.reset();
+
+                var p_alias = $.PrivateBin.I18n._(plurals);
+                $.PrivateBin.I18n.reset();
+
+                var f_result = $.PrivateBin.I18n.translate(fake);
+                $.PrivateBin.I18n.reset();
+
+                var f_alias = $.PrivateBin.I18n._(fake);
+                $.PrivateBin.I18n.reset();
+
+                return messageId === result && messageId === alias &&
+                    messageId === p_result && messageId === p_alias &&
+                    messageId === f_result && messageId === f_alias;
             }
         );
         jsc.property(
@@ -366,6 +391,49 @@ describe('I18n', function () {
             }
         );
     });
+
+    describe('getPluralForm', function () {
+        before(function () {
+            $.PrivateBin.I18n.reset();
+        });
+
+        jsc.property(
+            'returns valid key for plural form',
+            jsc.elements(supportedLanguages),
+            'integer',
+            function(language, n) {
+                $.PrivateBin.I18n.reset(language);
+                var result = $.PrivateBin.I18n.getPluralForm(n);
+                // arabic seems to have the highest plural count with 6 forms
+                return result >= 0 && result <= 5;
+            }
+        );
+    });
+
+    // loading of JSON via AJAX needs to be tested in the browser, this just mocks it
+    // TODO: This needs to be tested using a browser.
+    describe('loadTranslations', function () {
+        before(function () {
+            $.PrivateBin.I18n.reset();
+        });
+
+        jsc.property(
+            'downloads and handles any supported language',
+            jsc.elements(supportedLanguages),
+            function(language) {
+                var clean = jsdom('', {url: 'https://privatebin.net/', cookie: ['lang=' + language]});
+
+                $.PrivateBin.I18n.reset('en');
+                $.PrivateBin.I18n.loadTranslations();
+                $.PrivateBin.I18n.reset(language, require('../i18n/' + language + '.json'));
+                var result = $.PrivateBin.I18n.translate('en'),
+                    alias  = $.PrivateBin.I18n._('en');
+
+                clean();
+                return language === result && language === alias;
+            }
+        );
+    });
 });
 
 describe('Model', function () {

+ 1 - 1
tpl/bootstrap.php

@@ -69,7 +69,7 @@ if ($MARKDOWN):
 <?php
 endif;
 ?>
-		<script type="text/javascript" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-F8kopj0TJxt5GurjX2bql90Trr8IXRbX26E0nnjiCsxWuL0Rnds2DbgwPo28qgD+b+nnyxljcpHx2EUM+aoULw==" crossorigin="anonymous"></script>
+		<script type="text/javascript" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-7WGautcQxef6PeNh1sNcdCFCNRNo2uULN7QCgjqd+fWalRubtu1mtMEz8BLQ8sKgzPRF8E6dqgBQJ5ycwt03gA==" crossorigin="anonymous"></script>
 		<!--[if lt IE 10]>
 		<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;} #oldienotice {display:block;}</style>
 		<![endif]-->

+ 1 - 1
tpl/page.php

@@ -47,7 +47,7 @@ if ($MARKDOWN):
 <?php
 endif;
 ?>
-		<script type="text/javascript" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-F8kopj0TJxt5GurjX2bql90Trr8IXRbX26E0nnjiCsxWuL0Rnds2DbgwPo28qgD+b+nnyxljcpHx2EUM+aoULw==" crossorigin="anonymous"></script>
+		<script type="text/javascript" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-7WGautcQxef6PeNh1sNcdCFCNRNo2uULN7QCgjqd+fWalRubtu1mtMEz8BLQ8sKgzPRF8E6dqgBQJ5ycwt03gA==" crossorigin="anonymous"></script>
 		<!--[if lt IE 10]>
 		<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;} #oldienotice {display:block;}</style>
 		<![endif]-->