From 37f5d99bc4d2d4853a4f134ff2cd85825d9f0a41 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 26 Mar 2017 09:24:42 +0200 Subject: [PATCH] finalizing tests for I18n class, AJAX loading of translations needs to be tested in browser, mocked for now --- .gitignore | 1 + js/privatebin.js | 10 +++---- js/test.js | 76 ++++++++++++++++++++++++++++++++++++++++++++--- tpl/bootstrap.php | 2 +- tpl/page.php | 2 +- 5 files changed, 80 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index a752f8c..c17e3b4 100644 --- a/.gitignore +++ b/.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 diff --git a/js/privatebin.js b/js/privatebin.js index 17b4457..8cf7683 100644 --- a/js/privatebin.js +++ b/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 diff --git a/js/test.js b/js/test.js index 6f193f1..462d6ff 100644 --- a/js/test.js +++ b/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 () { diff --git a/tpl/bootstrap.php b/tpl/bootstrap.php index c55a766..6fc01bd 100644 --- a/tpl/bootstrap.php +++ b/tpl/bootstrap.php @@ -69,7 +69,7 @@ if ($MARKDOWN): - + diff --git a/tpl/page.php b/tpl/page.php index 6316a18..46b8df1 100644 --- a/tpl/page.php +++ b/tpl/page.php @@ -47,7 +47,7 @@ if ($MARKDOWN): - +