| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
const { License } = require( 'mmv' ); |
| 19 |
|
| 20 |
( function () { |
| 21 |
|
| 22 |
QUnit.module( 'mmv.model.License', QUnit.newMwEnvironment() ); |
| 23 |
|
| 24 |
QUnit.test( 'License constructor sense check', ( assert ) => { |
| 25 |
const shortName = 'CC-BY-SA-3.0'; |
| 26 |
const internalName = 'cc-by-sa-3.0'; |
| 27 |
const longName = 'Creative Commons Attribution--Share-Alike 3.0'; |
| 28 |
const url = 'http://creativecommons.org/licenses/by-sa/3.0/'; |
| 29 |
|
| 30 |
let license = new License( shortName ); |
| 31 |
assert.true( license instanceof License, 'License created successfully' ); |
| 32 |
assert.strictEqual( license.shortName, shortName, 'License has correct short name' ); |
| 33 |
assert.strictEqual( license.internalName, undefined, 'License has no internal name' ); |
| 34 |
assert.strictEqual( license.longName, undefined, 'License has no long name' ); |
| 35 |
assert.strictEqual( license.deedUrl, undefined, 'License has no deed URL' ); |
| 36 |
|
| 37 |
license = new License( shortName, internalName, longName, url ); |
| 38 |
assert.true( license instanceof License, 'License created successfully' ); |
| 39 |
assert.strictEqual( license.shortName, shortName, 'License has correct short name' ); |
| 40 |
assert.strictEqual( license.internalName, internalName, 'License has correct internal name' ); |
| 41 |
assert.strictEqual( license.longName, longName, 'License has correct long name' ); |
| 42 |
assert.strictEqual( license.deedUrl, url, 'License has correct deed URL' ); |
| 43 |
|
| 44 |
assert.throws( () => { |
| 45 |
license = new License(); |
| 46 |
}, 'License cannot be created without a short name' ); |
| 47 |
} ); |
| 48 |
|
| 49 |
QUnit.test( 'getShortName()', ( assert ) => { |
| 50 |
const existingMessageKey = 'Internal name that does exist'; |
| 51 |
const nonExistingMessageKey = 'Internal name that does not exist'; |
| 52 |
const license1 = new License( 'Shortname' ); |
| 53 |
const license2 = new License( 'Shortname', nonExistingMessageKey ); |
| 54 |
const license3 = new License( 'Shortname', existingMessageKey ); |
| 55 |
const oldMwMessage = mw.message; |
| 56 |
const oldMwMessagesExists = mw.messages.exists; |
| 57 |
|
| 58 |
mw.message = function ( name ) { |
| 59 |
return name === 'multimediaviewer-license-' + existingMessageKey ? |
| 60 |
{ text: function () { |
| 61 |
return 'Translated name'; |
| 62 |
} } : |
| 63 |
oldMwMessage.apply( mw, arguments ); |
| 64 |
}; |
| 65 |
mw.messages.exists = function ( name ) { |
| 66 |
return name === 'multimediaviewer-license-' + existingMessageKey ? |
| 67 |
true : oldMwMessagesExists.apply( mw.messages, arguments ); |
| 68 |
}; |
| 69 |
|
| 70 |
assert.strictEqual( license1.getShortName(), 'Shortname', |
| 71 |
'Short name is returned when there is no translated name' ); |
| 72 |
assert.strictEqual( license2.getShortName(), 'Shortname', |
| 73 |
'Short name is returned when translated name is missing' ); |
| 74 |
assert.strictEqual( license3.getShortName(), 'Translated name', |
| 75 |
'Translated name is returned when it exists' ); |
| 76 |
|
| 77 |
mw.message = oldMwMessage; |
| 78 |
mw.messages.exists = oldMwMessagesExists; |
| 79 |
} ); |
| 80 |
|
| 81 |
QUnit.test( 'getShortLink()', ( assert ) => { |
| 82 |
const license1 = new License( 'lorem ipsum' ); |
| 83 |
const license2 = new License( 'lorem ipsum', 'lipsum' ); |
| 84 |
const license3 = new License( 'lorem ipsum', 'lipsum', 'Lorem ipsum dolor sit amet' ); |
| 85 |
const license4 = new License( 'lorem ipsum', 'lipsum', 'Lorem ipsum dolor sit amet', |
| 86 |
'http://www.lipsum.com/' ); |
| 87 |
|
| 88 |
assert.strictEqual( license1.getShortLink(), 'lorem ipsum', |
| 89 |
'Code for license without link is formatted correctly' ); |
| 90 |
assert.strictEqual( license2.getShortLink(), 'lorem ipsum', |
| 91 |
'Code for license without link is formatted correctly' ); |
| 92 |
assert.strictEqual( license3.getShortLink(), 'lorem ipsum', |
| 93 |
'Code for license without link is formatted correctly' ); |
| 94 |
|
| 95 |
const $html = $( license4.getShortLink() ); |
| 96 |
assert.strictEqual( $html.text(), 'lorem ipsum', |
| 97 |
'Text for license with link is formatted correctly' ); |
| 98 |
assert.strictEqual( $html.prop( 'href' ), 'http://www.lipsum.com/', |
| 99 |
'URL for license with link is formatted correctly' ); |
| 100 |
assert.strictEqual( $html.prop( 'title' ), 'Lorem ipsum dolor sit amet', |
| 101 |
'Title for license with link is formatted correctly' ); |
| 102 |
} ); |
| 103 |
|
| 104 |
QUnit.test( 'isCc()', ( assert ) => { |
| 105 |
let license = new License( 'CC-BY-SA-2.0', 'cc-by-sa-2.0', |
| 106 |
'Creative Commons Attribution - ShareAlike 2.0', |
| 107 |
'http://creativecommons.org/licenses/by-sa/2.0/' ); |
| 108 |
assert.strictEqual( license.isCc(), true, 'CC license recognized' ); |
| 109 |
|
| 110 |
license = new License( 'Public Domain', 'pd', |
| 111 |
'Public Domain for lack of originality' ); |
| 112 |
assert.strictEqual( license.isCc(), false, 'Non-CC license not recognized' ); |
| 113 |
|
| 114 |
license = new License( 'MIT' ); |
| 115 |
assert.strictEqual( license.isCc(), false, 'Non-CC license with no internal name not recognized' ); |
| 116 |
} ); |
| 117 |
|
| 118 |
QUnit.test( 'isPd()', ( assert ) => { |
| 119 |
let license = new License( 'Public Domain', 'pd', |
| 120 |
'Public Domain for lack of originality' ); |
| 121 |
assert.strictEqual( license.isPd(), true, 'PD license recognized' ); |
| 122 |
|
| 123 |
license = new License( 'CC-BY-SA-2.0', 'cc-by-sa-2.0', |
| 124 |
'Creative Commons Attribution - ShareAlike 2.0', |
| 125 |
'http://creativecommons.org/licenses/by-sa/2.0/' ); |
| 126 |
assert.strictEqual( license.isPd(), false, 'Non-PD license not recognized' ); |
| 127 |
|
| 128 |
license = new License( 'MIT' ); |
| 129 |
assert.strictEqual( license.isPd(), false, 'Non-PD license with no internal name not recognized' ); |
| 130 |
} ); |
| 131 |
|
| 132 |
QUnit.test( 'isFree()', ( assert ) => { |
| 133 |
let license = new License( 'CC-BY-SA-2.0', 'cc-by-sa-2.0', |
| 134 |
'Creative Commons Attribution - ShareAlike 2.0', |
| 135 |
'http://creativecommons.org/licenses/by-sa/2.0/' ); |
| 136 |
assert.strictEqual( license.isFree(), true, 'Licenses default to free' ); |
| 137 |
|
| 138 |
license = new License( 'Fair use', 'fairuse', |
| 139 |
'Fair use', undefined, undefined, true ); |
| 140 |
assert.strictEqual( license.isFree(), false, 'Non-free flag handled correctly' ); |
| 141 |
} ); |
| 142 |
|
| 143 |
QUnit.test( 'needsAttribution()', ( assert ) => { |
| 144 |
let license = new License( 'CC-BY-SA-2.0', 'cc-by-sa-2.0', |
| 145 |
'Creative Commons Attribution - ShareAlike 2.0', |
| 146 |
'http://creativecommons.org/licenses/by-sa/2.0/' ); |
| 147 |
assert.strictEqual( license.needsAttribution(), true, 'Licenses assumed to need attribution by default' ); |
| 148 |
|
| 149 |
license = new License( 'Public Domain', 'pd', |
| 150 |
'Public Domain for lack of originality', false ); |
| 151 |
assert.strictEqual( license.needsAttribution(), false, 'Attribution required flag handled correctly' ); |
| 152 |
} ); |
| 153 |
}() ); |