From 5e1dece304a6aebcad17a0a066aa07f05397fd78 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Fri, 13 May 2022 21:49:51 +0100 Subject: [PATCH] tests: Make mmv.ui.metadataPanel.js pass with non-English locale My browser is configured with en-GB as its locale. This meant that when qqx was passed down, it fell back to my browser's default locale and thus made the tests fail, as the expected data was set against lang=en. Ironically, MMV actually prefers the en-GB date format, and outside tests even replaces 'en' with 'en-GB' when formatting dates in the user interface. It was only in the tests that it asserted the 'en' format that would otherwise never be used by MMV. Ironically, the reason my locale is affecting the test is because we switched from hardcoded lang=en to hardcoded lang=qqx which is meant to disable any influence from localisation. However, when passed to browser-native Intl API, qqx is not valid and thus caused us to not have a hardcoded 'en' but a completely variable UI language based on the developer's browser settings. Bug: T250045 Change-Id: I94ad25cc7060ac5e9db41c6b6767aa9fd687a0db --- resources/mmv/ui/mmv.ui.metadataPanel.js | 8 +++++++- tests/qunit/mmv/ui/mmv.ui.metadataPanel.test.js | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/resources/mmv/ui/mmv.ui.metadataPanel.js b/resources/mmv/ui/mmv.ui.metadataPanel.js index 4057a7fbd..db920e8bb 100644 --- a/resources/mmv/ui/mmv.ui.metadataPanel.js +++ b/resources/mmv/ui/mmv.ui.metadataPanel.js @@ -844,7 +844,13 @@ MPP.formatDate = function ( dateString ) { var date, lang = mw.config.get( 'wgUserLanguage' ); - if ( lang === 'en' ) { lang = 'en-GB'; } // for D MMMM YYYY format + if ( lang === 'en' || lang === 'qqx' ) { + // prefer "D MMMM YYYY" format + // avoid passing invalid "qqx" to native toLocaleString(), + // which would cause developer's browser locale to be used, + // and thus sometimes cause tests to fail. + lang = 'en-GB'; + } date = new Date( dateString ); try { if ( date instanceof Date && !isNaN( date ) ) { diff --git a/tests/qunit/mmv/ui/mmv.ui.metadataPanel.test.js b/tests/qunit/mmv/ui/mmv.ui.metadataPanel.test.js index 3494919aa..57522a29b 100644 --- a/tests/qunit/mmv/ui/mmv.ui.metadataPanel.test.js +++ b/tests/qunit/mmv/ui/mmv.ui.metadataPanel.test.js @@ -145,7 +145,7 @@ assert.strictEqual( panel.creditField.$element.find( '.mw-mmv-source' ).html(), 'LostBar', 'Source text is correctly set' ); // Either multimediaviewer-credit-popup-text or multimediaviewer-credit-popup-text-more. assert.ok( creditPopupText === '(multimediaviewer-credit-popup-text)' || creditPopupText === '(multimediaviewer-credit-popup-text-more)', 'Source tooltip is correctly set' ); - assert.strictEqual( panel.$datetime.text(), '(multimediaviewer-datetime-created: August 26, 2013)', 'Correct date is displayed' ); + assert.strictEqual( panel.$datetime.text(), '(multimediaviewer-datetime-created: 26 August 2013)', 'Correct date is displayed' ); assert.strictEqual( panel.$license.text(), '(multimediaviewer-license-cc-by-2.0)', 'License is correctly set' ); assert.ok( panel.$license.prop( 'target' ), 'License information opens in new window' ); assert.strictEqual( panel.$restrictions.children().last().children().hasClass( 'mw-mmv-restriction-default' ), true, 'Default restriction is correctly displayed last' ); @@ -154,7 +154,7 @@ panel.setImageInfo( image, imageData, repoData ); clock.tick( 10 ); - assert.strictEqual( panel.$datetime.text(), '(multimediaviewer-datetime-uploaded: August 25, 2013)', 'Correct date is displayed' ); + assert.strictEqual( panel.$datetime.text(), '(multimediaviewer-datetime-uploaded: 25 August 2013)', 'Correct date is displayed' ); clock.restore(); } );