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

This commit is contained in:
El RIDO 2017-03-26 09:24:42 +02:00
parent e1ea14627f
commit 37f5d99bc4
No known key found for this signature in database
GPG key ID: 0F5C940A6BD81F92
5 changed files with 80 additions and 11 deletions

1
.gitignore vendored
View file

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

View file

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

View file

@ -13,7 +13,9 @@ var jsc = require('jsverify'),
}) })
), ),
// schemas supported by the whatwg-url library // 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.$ = global.jQuery = require('./jquery-3.1.1');
global.sjcl = require('./sjcl-1.0.6'); global.sjcl = require('./sjcl-1.0.6');
@ -22,6 +24,11 @@ global.RawDeflate = require('./rawdeflate-0.5');
require('./rawinflate-0.3'); require('./rawinflate-0.3');
require('./privatebin'); require('./privatebin');
// redirect console messages to log file
console.warn = console.error = function (msg) {
logFile.write(msg + '\n');
}
describe('Helper', function () { describe('Helper', function () {
describe('secondsToHuman', function () { describe('secondsToHuman', function () {
after(function () { after(function () {
@ -339,12 +346,30 @@ describe('I18n', function () {
'returns message ID unchanged if no translation found', 'returns message ID unchanged if no translation found',
'string', 'string',
function (messageId) { function (messageId) {
messageId = messageId.replace(/%(s|d)/g, '%%'); messageId = messageId.replace(/%(s|d)/g, '%%');
var result = $.PrivateBin.I18n.translate(messageId); var plurals = [messageId, messageId + 's'],
fake = [messageId],
result = $.PrivateBin.I18n.translate(messageId);
$.PrivateBin.I18n.reset(); $.PrivateBin.I18n.reset();
var alias = $.PrivateBin.I18n._(messageId); var alias = $.PrivateBin.I18n._(messageId);
$.PrivateBin.I18n.reset(); $.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( 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 () { describe('Model', function () {

View file

@ -69,7 +69,7 @@ if ($MARKDOWN):
<?php <?php
endif; 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]> <!--[if lt IE 10]>
<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;} #oldienotice {display:block;}</style> <style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;} #oldienotice {display:block;}</style>
<![endif]--> <![endif]-->

View file

@ -47,7 +47,7 @@ if ($MARKDOWN):
<?php <?php
endif; 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]> <!--[if lt IE 10]>
<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;} #oldienotice {display:block;}</style> <style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;} #oldienotice {display:block;}</style>
<![endif]--> <![endif]-->