From d61ff90ca6c6ad2f977dd3e5b793e5f6ed24a94b Mon Sep 17 00:00:00 2001
From: Olga Bulat
' - const iconStyle = 'style="height:22px!important;margin-left:3px;vertical-align:text-bottom;"' - const assetPathBase = 'https://mirrors.creativecommons.org/presskit/icons' - const assetPathRef = '?ref=chooser-v1' - let licenseIcons = `` - if (shortLicenseName.includes('CC0')) { - shortLicenseName = 'CC CC0 1.0' - } - licenseIcons += shortLicenseName - .slice(3, shortLicenseName.length - 4) - .split('-') - .map(attr => `` - ).join('') - const licenseHref = licenseUrl(shortToAttr(shortLicenseName)) - dataForHtmlGeneration.licenseLink = - `${shortLicenseName}${licenseIcons}` +const CC_NAMESPACE = { + NAME: 'xmlns:cc', + URI: 'http://creativecommons.org/ns#' +} +const DCT_NAMESPACE = { + NAME: 'xmlns:dct', + URI: 'http://purl.org/dc/terms/' +} +const ICON_STYLE = 'height:22px!important;margin-left:3px;vertical-align:text-bottom;' +const ICON_BASE_URL = 'https://mirrors.creativecommons.org/presskit/icons/' +/** + * Generate html for creator: + * 1. If the creator name is blank, even if creator link is provided, return a blank string + * 2. If only creator name is provided, return a span with proper metadata + * 3. If both creator name and URL are provided, returns an 'a' element with proper data and metadata + * @param {string} creatorName + * @param {string} creatorProfileUrl + * @returns {string} + */ +function generateCreatorCode(creatorName, creatorProfileUrl) { + let creator = '' if (creatorName) { if (creatorProfileUrl) { - dataForHtmlGeneration.creator = - `${creatorName}` + const absoluteUrl = creatorProfileUrl.startsWith('http') ? creatorProfileUrl : `http://${creatorProfileUrl}` + creator = + `${creatorName}` } else { - dataForHtmlGeneration.creator = `${creatorName}` + creator = `${creatorName}` } } + return creator +} + +/** + * Generate html for work title: + * 1. If the work title is blank, event if work link is provided, return blank string + * 2. If only work title is provided, return a span with proper metadata + * 3. If both title and URL are provided, returns an 'a' element with proper data and metadata + * @param {string} workTitle + * @param {string} workUrl + * @returns {string} + */ +function generateWorkCode(workTitle, workUrl) { + let workCode = '' if (workTitle) { if (workUrl) { - dataForHtmlGeneration.workTitle = `${workTitle}` + const absoluteUrl = workUrl.startsWith('http') ? workUrl : `http://${workUrl}` + workCode = `${workTitle}` } else { - dataForHtmlGeneration.workTitle = `${workTitle}` + workCode = `${workTitle}` } } + return workCode +} + +/** + * Generates the html for the rich text license information, including license name, + * link to the license deed, and license icons + * @param {LicenseAttributes} licenseAttr + * @param {ShortLicenseName} shortLicenseName + * @returns {string} HTML code for the license + */ +function generateLicenseCode(licenseAttr, shortLicenseName) { + const iconStyle = `style="${ICON_STYLE}"` + const assetPathRef = '?ref=chooser-v1' + const licenseIconsCode = ['cc', ...licenseIconsArr(licenseAttr)] + .map(attr => `` + ).join('') + + return (` + ${shortLicenseName}${licenseIconsCode}`) +} + +/** + * Generate data for use in attribution HTML through i18n + * @param attributionDetails + * @param {ShortLicenseName} shortLicenseName + * @returns {{creator: string, workTitle: string, licenseLink: string, htmlString: string}} + */ +function generateHTML(attributionDetails, shortLicenseName) { + const dataForHtmlGeneration = {} + const { creatorName, creatorProfileUrl, workTitle, workUrl } = attributionDetails + dataForHtmlGeneration.paragraph = + `
'
+ const licenseAttr = shortToAttr(shortLicenseName)
+ dataForHtmlGeneration.license = generateLicenseCode(licenseAttr, shortLicenseName)
+ dataForHtmlGeneration.creator = generateCreatorCode(creatorName, creatorProfileUrl)
+ dataForHtmlGeneration.work = generateWorkCode(workTitle, workUrl)
return dataForHtmlGeneration
}
export {
defaultAttributes, CC0Attributes, CCBYAttributes, shortToAttr, attrToShort,
- attrToFull, licenseUrl, licenseSlug, licenseIconsArr, generateHTML, updateVisibleEnabledStatus
+ attrToFull, licenseUrl, licenseSlug, licenseIconsArr, generateHTML, updateVisibleEnabledStatus,
+ CC_NAMESPACE, DCT_NAMESPACE, LICENSES, ICON_STYLE, ICON_BASE_URL
}
diff --git a/src/utils/licenses.js b/src/utils/licenses.js
new file mode 100644
index 00000000..ac258d1b
--- /dev/null
+++ b/src/utils/licenses.js
@@ -0,0 +1,99 @@
+/** @typedef {{ATTRIBUTES: LicenseAttributes, SLUG: string, FULL: string, URL: string, SHORT: string}} LicenseProperties */
+
+/**
+ *
+ * @type {{CC0: LicenseProperties, CC_BY: LicenseProperties, CC_BY_NC:LicenseProperties, CC_BY_NC_SA: LicenseProperties, CC_BY_ND: LicenseProperties, CC_BY_SA: LicenseProperties, CC_BY_NC_ND: LicenseProperties}}
+ */
+export const LICENSES = {
+ CC0: {
+ ATTRIBUTES: {
+ BY: false,
+ SA: false,
+ NC: false,
+ ND: false
+ },
+ FULL: 'CC0 1.0 Universal',
+ SHORT: 'CC0 1.0',
+ SLUG: 'cc0',
+ URL: 'https://creativecommons.org/publicdomain/zero/1.0',
+ ICONS: ['cc', 'zero']
+ },
+ CC_BY: {
+ ATTRIBUTES: {
+ BY: true,
+ SA: false,
+ NC: false,
+ ND: false
+ },
+ FULL: 'Attribution 4.0 International',
+ SHORT: 'CC BY 4.0',
+ SLUG: 'cc-by',
+ URL: 'https://creativecommons.org/licenses/by/4.0',
+ ICONS: ['cc', 'by']
+ },
+ CC_BY_SA: {
+ ATTRIBUTES: {
+ BY: true,
+ SA: true,
+ NC: false,
+ ND: false
+ },
+ FULL: 'Attribution-ShareAlike 4.0 International',
+ SHORT: 'CC BY-SA 4.0',
+ SLUG: 'cc-by-sa',
+ URL: 'https://creativecommons.org/licenses/by-sa/4.0',
+ ICONS: ['cc', 'by', 'sa']
+ },
+ CC_BY_NC: {
+ ATTRIBUTES: {
+ BY: true,
+ SA: false,
+ NC: true,
+ ND: false
+ },
+ FULL: 'Attribution-NonCommercial 4.0 International',
+ SHORT: 'CC BY-NC 4.0',
+ SLUG: 'cc-by-nc',
+ URL: 'https://creativecommons.org/licenses/by-nc/4.0',
+ ICONS: ['cc', 'by', 'nc']
+ },
+ CC_BY_NC_SA: {
+ ATTRIBUTES: {
+ BY: true,
+ SA: true,
+ NC: true,
+ ND: false
+ },
+ FULL: 'Attribution-NonCommercial-ShareAlike 4.0 International',
+ SHORT: 'CC BY-NC-SA 4.0',
+ SLUG: 'cc-by-nc-sa',
+ URL: 'https://creativecommons.org/licenses/by-nc-sa/4.0',
+ ICONS: ['cc', 'by', 'nc', 'sa']
+ },
+ CC_BY_NC_ND: {
+ ATTRIBUTES: {
+ BY: true,
+ SA: false,
+ NC: true,
+ ND: true
+ },
+ FULL: 'Attribution-NonCommercial-NoDerivatives 4.0 International',
+ SHORT: 'CC BY-NC-ND 4.0',
+ SLUG: 'cc-by-nc-nd',
+ URL: 'https://creativecommons.org/licenses/by-nc-nd/4.0',
+ ICONS: ['cc', 'by', 'nc', 'nd']
+ },
+ CC_BY_ND: {
+ ATTRIBUTES: {
+ BY: true,
+ SA: false,
+ NC: false,
+ ND: true
+ },
+ FULL: 'Attribution-NoDerivatives 4.0 International',
+ SHORT: 'CC BY-ND 4.0',
+ SLUG: 'cc-by-nd',
+ URL: 'https://creativecommons.org/licenses/by-nd/4.0',
+ ICONS: ['cc', 'by', 'nd']
+ }
+}
diff --git a/tests/e2e/specs/LicenseCode.js b/tests/e2e/specs/LicenseCode.js
index 16418a4f..b8d00f8d 100644
--- a/tests/e2e/specs/LicenseCode.js
+++ b/tests/e2e/specs/LicenseCode.js
@@ -22,12 +22,10 @@ module.exports = {
.setValue('input[placeholder="www.author.com/work.jpg"]', 'www.author.com/work.jpg')
.assert.elementPresent('p[class="license-text"] a')
.getAttribute('p > span > a:nth-child(1)', 'href', function(result) {
- const urlString = result.value.split('/').slice(3).join('/')
- this.assert.equal(urlString, 'www.author.com/work.jpg')
+ this.assert.equal(result.value, 'http://www.author.com/work.jpg')
})
.getAttribute('p > span > a:nth-child(2)', 'href', function(result) {
- const urlString = result.value.split('/').slice(3).join('/')
- this.assert.equal(urlString, 'www.author.com')
+ this.assert.equal(result.value, 'http://www.author.com/')
})
.getAttribute('p > span > a:nth-child(4)', 'href', function(result) {
const urlString = result.value.split('/').slice(3).join('/')
diff --git a/tests/unit/specs/components/LicenseCode.spec.js b/tests/unit/specs/components/LicenseCode.spec.js
index d885a60d..93313d0c 100644
--- a/tests/unit/specs/components/LicenseCode.spec.js
+++ b/tests/unit/specs/components/LicenseCode.spec.js
@@ -1,42 +1,39 @@
-import { mount, createLocalVue, config } from '@vue/test-utils'
-import LicenseCode from '@/components/LicenseCode.vue'
-import VueI18n from 'vue-i18n'
import Vuex from 'vuex'
+import VueI18n from 'vue-i18n'
+import { mount, createLocalVue } from '@vue/test-utils'
+import LicenseCode from '@/components/LicenseCode.vue'
+import createStore from '@/store'
+import { CCBYAttributes } from '@/utils/license-utilities'
+
+const TEST_DATA = {
+ creatorName: 'Jane Doe',
+ creatorProfileUrl: 'www.author.com',
+ workTitle: 'My work',
+ workUrl: 'www.author.com/picture.jpg'
+}
describe('LicenseCode.vue', () => {
let wrapper
let state
- let store
+ let localVue
- // Always creates a shallow instance of component
+ // Vue i18n is looking for locale key in messages,
+ // i.e. $t('app') becomes 'messages.
+ My work
+
+ by
+ Jane Doe is licensed under
+ CC BY 4.0
+
+
+
+ My work
+
+ by
+ Jane Doe is licensed under
+ CC BY 4.0
+
+
+
+ My work
+
+ by
+ Jane Doe is licensed under
+ CC BY 4.0
+
+
+ license-use.richtext.full-text
+
+ This work
+
+
+ is licensed under
+ CC BY 4.0
+
',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('creator name', () => {
-// const attr = { creatorName: 'John' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'John',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('creator name + url', () => {
-// const attr = { creatorName: 'John', creatorProfileUrl: 'j@doe.com' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'John',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('work title', () => {
-// const attr = { workTitle: 'Foo' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('work title + url', () => {
-// const attr = { workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name; work title', () => {
-// const attr = { workTitle: 'Foo', creatorName: 'Jane' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name + url; work title', () => {
-// const attr = { creatorName: 'Jane', creatorProfileUrl: 'j@doe.com', workTitle: 'Foo' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name; work title + url', () => {
-// const attr = { creatorName: 'Jane', workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name + url; work title + url', () => {
-// const attr = { creatorName: 'Jane', creatorProfileUrl: 'j@doe.com', workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// })
-//
-// describe('generateHTML CC BY', function testGenerateHTML() {
-// const license = 'CC BY 4.0'
-// test('empty attr', () => {
-// const attr = {}
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('creator name', () => {
-// const attr = { creatorName: 'John' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'John',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('creator name + url', () => {
-// const attr = { creatorName: 'John', creatorProfileUrl: 'j@doe.com' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'John',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('work title', () => {
-// const attr = { workTitle: 'Foo' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('work title + url', () => {
-// const attr = { workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name; work title', () => {
-// const attr = { workTitle: 'Foo', creatorName: 'Jane' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name + url; work title', () => {
-// const attr = { creatorName: 'Jane', creatorProfileUrl: 'j@doe.com', workTitle: 'Foo' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name; work title + url', () => {
-// const attr = { creatorName: 'Jane', workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name + url; work title + url', () => {
-// const attr = { creatorName: 'Jane', creatorProfileUrl: 'j@doe.com', workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// })
-//
-// describe('generateHTML CC BY-SA', function testGenerateHTML() {
-// const license = 'CC BY-SA 4.0'
-// test('empty attr', () => {
-// const attr = {}
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('creator name', () => {
-// const attr = { creatorName: 'John' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'John',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('creator name + url', () => {
-// const attr = { creatorName: 'John', creatorProfileUrl: 'j@doe.com' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'John',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('work title', () => {
-// const attr = { workTitle: 'Foo' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('work title + url', () => {
-// const attr = { workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name; work title', () => {
-// const attr = { workTitle: 'Foo', creatorName: 'Jane' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name + url; work title', () => {
-// const attr = { creatorName: 'Jane', creatorProfileUrl: 'j@doe.com', workTitle: 'Foo' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name; work title + url', () => {
-// const attr = { creatorName: 'Jane', workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name + url; work title + url', () => {
-// const attr = { creatorName: 'Jane', creatorProfileUrl: 'j@doe.com', workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// })
-//
-// describe('generateHTML CC BY-NC', function testGenerateHTML() {
-// const license = 'CC BY-NC 4.0'
-// test('empty attr', () => {
-// const attr = {}
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('creator name', () => {
-// const attr = { creatorName: 'John' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'John',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('creator name + url', () => {
-// const attr = { creatorName: 'John', creatorProfileUrl: 'j@doe.com' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'John',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('work title', () => {
-// const attr = { workTitle: 'Foo' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('work title + url', () => {
-// const attr = { workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name; work title', () => {
-// const attr = { workTitle: 'Foo', creatorName: 'Jane' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name + url; work title', () => {
-// const attr = { creatorName: 'Jane', creatorProfileUrl: 'j@doe.com', workTitle: 'Foo' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name; work title + url', () => {
-// const attr = { creatorName: 'Jane', workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name + url; work title + url', () => {
-// const attr = { creatorName: 'Jane', creatorProfileUrl: 'j@doe.com', workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// })
-//
-// describe('generateHTML CC BY-NC-SA', function testGenerateHTML() {
-// const license = 'CC BY-NC-SA 4.0'
-// test('empty attr', () => {
-// const attr = {}
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('creator name', () => {
-// const attr = { creatorName: 'John' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'John',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('creator name + url', () => {
-// const attr = { creatorName: 'John', creatorProfileUrl: 'j@doe.com' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'John',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('work title', () => {
-// const attr = { workTitle: 'Foo' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('work title + url', () => {
-// const attr = { workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name; work title', () => {
-// const attr = { workTitle: 'Foo', creatorName: 'Jane' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name + url; work title', () => {
-// const attr = { creatorName: 'Jane', creatorProfileUrl: 'j@doe.com', workTitle: 'Foo' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name; work title + url', () => {
-// const attr = { creatorName: 'Jane', workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name + url; work title + url', () => {
-// const attr = { creatorName: 'Jane', creatorProfileUrl: 'j@doe.com', workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// })
-//
-// describe('generateHTML CC BY-ND', function testGenerateHTML() {
-// const license = 'CC BY-ND 4.0'
-// test('empty attr', () => {
-// const attr = {}
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('creator name', () => {
-// const attr = { creatorName: 'John' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'John',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('creator name + url', () => {
-// const attr = { creatorName: 'John', creatorProfileUrl: 'j@doe.com' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'John',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('work title', () => {
-// const attr = { workTitle: 'Foo' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('work title + url', () => {
-// const attr = { workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name; work title', () => {
-// const attr = { workTitle: 'Foo', creatorName: 'Jane' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name + url; work title', () => {
-// const attr = { creatorName: 'Jane', creatorProfileUrl: 'j@doe.com', workTitle: 'Foo' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name; work title + url', () => {
-// const attr = { creatorName: 'Jane', workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name + url; work title + url', () => {
-// const attr = { creatorName: 'Jane', creatorProfileUrl: 'j@doe.com', workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// })
-//
-// describe('generateHTML CC BY-NC-ND', function testGenerateHTML() {
-// const license = 'CC BY-NC-ND 4.0'
-// test('empty attr', () => {
-// const attr = {}
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('creator name', () => {
-// const attr = { creatorName: 'John' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'John',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('creator name + url', () => {
-// const attr = { creatorName: 'John', creatorProfileUrl: 'j@doe.com' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'John',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: ''
-// })
-// })
-// test('work title', () => {
-// const attr = { workTitle: 'Foo' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('work title + url', () => {
-// const attr = { workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: '',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name; work title', () => {
-// const attr = { workTitle: 'Foo', creatorName: 'Jane' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name + url; work title', () => {
-// const attr = { creatorName: 'Jane', creatorProfileUrl: 'j@doe.com', workTitle: 'Foo' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name; work title + url', () => {
-// const attr = { creatorName: 'Jane', workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// test('creator name + url; work title + url', () => {
-// const attr = { creatorName: 'Jane', creatorProfileUrl: 'j@doe.com', workTitle: 'Foo', workUrl: 'www.foo.bar' }
-// expect(generateHTML(attr, license)).toEqual({
-// creator: 'Jane',
-// htmlString: ' ',
-// licenseIconsLink: '',
-// workTitle: 'Foo'
-// })
-// })
-// })
diff --git a/tests/unit/specs/utils/license-utilities.test.js b/tests/unit/specs/utils/license-utilities.test.js
new file mode 100644
index 00000000..c76077a7
--- /dev/null
+++ b/tests/unit/specs/utils/license-utilities.test.js
@@ -0,0 +1,304 @@
+/* eslint-disable no-undef */
+import {
+ attrToFull,
+ attrToShort,
+ generateHTML,
+ licenseIconsArr,
+ licenseSlug,
+ licenseUrl,
+ shortToAttr,
+ updateVisibleEnabledStatus,
+ LICENSES,
+ CC_NAMESPACE,
+ DCT_NAMESPACE,
+ ICON_BASE_URL,
+ ICON_STYLE
+} from '@/utils/license-utilities'
+import { mount } from '@vue/test-utils'
+import TestComponent from './TestComponent'
+
+const attributesToTest = [
+ {
+ attr: {},
+ fullResult: undefined
+ },
+ {
+ attr: { BY: false },
+ fullResult: LICENSES.CC0.FULL,
+ shortResult: LICENSES.CC0.SHORT
+ },
+ {
+ attr: { SA: false, BY: false },
+ fullResult: LICENSES.CC0.FULL,
+ shortResult: LICENSES.CC0.SHORT
+ },
+ {
+ attr: { BY: true },
+ fullResult: LICENSES.CC_BY.FULL,
+ shortResult: LICENSES.CC_BY.SHORT
+ }
+
+]
+describe('attrToFull edge cases', function testAttrToFull() {
+ attributesToTest.forEach((option) => {
+ const { attr, fullResult } = option
+ it(`Attributes ${JSON.stringify(attr)} should return <${fullResult}>`, () => {
+ expect(attrToFull(attr)).toEqual(fullResult)
+ })
+ })
+})
+describe('attrToFull all licenses', () => {
+ Object.values(LICENSES).forEach((license) => {
+ const { ATTRIBUTES: attr, FULL: result } = license
+ it(`${JSON.stringify(attr)} should return <${result}>`, () => {
+ expect(attrToFull(attr)).toEqual(result)
+ })
+ })
+})
+describe('attrToShort all licenses', () => {
+ Object.values(LICENSES).forEach((license) => {
+ const { ATTRIBUTES: attr, SHORT: result } = license
+ it(`${JSON.stringify(attr)} should return <${result}>`, () => {
+ expect(attrToShort(attr)).toEqual(result)
+ })
+ })
+})
+describe('shortToAttr all licenses', () => {
+ Object.values(LICENSES).forEach((license) => {
+ const { ATTRIBUTES: attr, SHORT: short } = license
+ it(`<${short}> should return ${JSON.stringify(attr)}`, () => {
+ expect(shortToAttr(short)).toEqual(attr)
+ })
+ })
+})
+describe('license slug', () => {
+ Object.values(LICENSES).forEach((license) => {
+ const { SHORT: short, SLUG: slug } = license
+ it(`<${short}> should return ${slug}`, () => {
+ expect(licenseSlug(short)).toEqual(slug)
+ })
+ })
+})
+
+describe('shortToAttr', function testAttrToShort() {
+ test('gibberish string #1', () => {
+ const str = 'CC 4.0'
+ expect(shortToAttr(str)).toEqual(undefined)
+ })
+})
+
+describe('licenseIconsArr', function testLicenseIconsArr() {
+ test('CC0 #1: empty attr', () => {
+ const attr = {}
+ expect(licenseIconsArr(attr)).toEqual(['zero'])
+ })
+ test('CC0 #2: BY: false', () => {
+ const attr = { BY: false }
+ expect(licenseIconsArr(attr)).toEqual(['zero'])
+ })
+ test('CC BY', () => {
+ const attr = { BY: true }
+ expect(licenseIconsArr(attr)).toEqual(['by'])
+ })
+ test('CC BY SA', () => {
+ const attr = { BY: true, SA: true }
+ expect(licenseIconsArr(attr)).toEqual(['by', 'sa'])
+ })
+ test('CC BY NC', () => {
+ const attr = { BY: true, NC: true }
+ expect(licenseIconsArr(attr)).toEqual(['by', 'nc'])
+ })
+ test('CC BY NC SA', () => {
+ const attr = { BY: true, NC: true, SA: true }
+ expect(licenseIconsArr(attr)).toEqual(['by', 'nc', 'sa'])
+ })
+ test('CC BY ND', () => {
+ const attr = { BY: true, ND: true }
+ expect(licenseIconsArr(attr)).toEqual(['by', 'nd'])
+ })
+ test('CC BY NC ND', () => {
+ const attr = { BY: true, NC: true, ND: true }
+ expect(licenseIconsArr(attr)).toEqual(['by', 'nc', 'nd'])
+ })
+})
+
+describe('license url', () => {
+ Object.values(LICENSES).forEach((license) => {
+ const { ATTRIBUTES: attr, URL: url } = license
+ it(`${JSON.stringify(attr)} should return ${url}`, () => {
+ expect(licenseUrl(attr)).toEqual(url)
+ })
+ it(`${JSON.stringify(attr)} for web should return ${url} with a ref value`, () => {
+ const [urlForWeb, ref] = licenseUrl(attr, 'web').split('/?')
+ expect(urlForWeb).toEqual(url)
+ expect(ref).toEqual('ref=chooser-v1')
+ })
+ })
+})
+
+describe('updateVisibleEnabledStatus', function testUpdateVisibleEnabledStatus() {
+ test('empty object', () => {
+ const attr = {}
+ expect(updateVisibleEnabledStatus(attr)).toEqual({
+ enabled: ['FS', 'BY', 'NC', 'ND', 'SA', 'AD'],
+ stepsDisabledDue: '',
+ visible: ['FS', 'BY', 'NC', 'ND', 'SA', 'AD']
+ })
+ })
+ test('FS: true, BY: false', () => {
+ const attr = { FS: true, BY: false }
+ expect(updateVisibleEnabledStatus(attr)).toEqual({
+ enabled: ['FS', 'DD', 'CW', 'AD'],
+ stepsDisabledDue: 'CC0',
+ visible: ['FS', 'DD', 'CW', 'AD']
+ })
+ })
+ test('FS: true, BY: true ', () => {
+ const attr = { FS: true, BY: true }
+ expect(updateVisibleEnabledStatus(attr)).toEqual({
+ enabled: ['FS', 'DD', 'AD'],
+ stepsDisabledDue: '',
+ visible: ['FS', 'DD', 'AD']
+ })
+ })
+ test('FS: false, BY: false', () => {
+ const attr = { FS: false, BY: false }
+ expect(updateVisibleEnabledStatus(attr)).toEqual({
+ enabled: ['FS', 'BY', 'CW', 'AD'],
+ stepsDisabledDue: 'CC0',
+ visible: ['FS', 'BY', 'NC', 'ND', 'SA', 'CW', 'AD']
+ })
+ })
+ test('FS: false, BY: true, ND: true', () => {
+ const attr = { FS: false, BY: true, ND: true }
+ expect(updateVisibleEnabledStatus(attr)).toEqual({
+ enabled: ['FS', 'BY', 'NC', 'ND', 'AD'],
+ stepsDisabledDue: 'CC0',
+ visible: ['FS', 'BY', 'NC', 'ND', 'SA', 'AD']
+ })
+ })
+ test('FS: false, BY: true, ND: false', () => {
+ const attr = { FS: false, BY: true, ND: false }
+ expect(updateVisibleEnabledStatus(attr)).toEqual({
+ enabled: ['FS', 'BY', 'NC', 'ND', 'SA', 'AD'],
+ stepsDisabledDue: '',
+ visible: ['FS', 'BY', 'NC', 'ND', 'SA', 'AD']
+ })
+ })
+})
+
+describe('generateHTML', function testGenerateHTML() {
+ const TEST_DATA = {
+ CREATOR: 'John',
+ WORK_TITLE: 'Foo',
+ PROFILE_URL: 'www.john.com',
+ WORK_URL: 'www.john.com/foo.jpg'
+ }
+ // For each kind of Attribution data present, check:
+ // 1. Correct namespaces of RDFa data a applied
+ // 2. License link has correct rel and href attributes
+ // 3. License link has correct text
+ // 4. There are corresponding icons for the license
+ // 5. Creator text and link are correct, and have proper metadata
+ // 6. Work text and link are correct, and have proper metadata
+ const hasIconSourcesErrors = (sources, licenses) => {
+ const urlRef = '?ref=chooser-v1" target="_blank" rel="noopener noreferrer"'
+ if (sources.length !== licenses.length) {
+ return `Incorrect number of licenses, expected ${licenses.length}, got ${sources.length}`
+ }
+ sources.forEach((source) => {
+ if (!source.startsWith(urlRef)) {
+ return `${source} doesn't start with correct base`
+ }
+ const license = source
+ .replace(ICON_BASE_URL, '')
+ .replace(urlRef, '')
+ .replace('.svg', '')
+ if (!licenses.includes(license)) {
+ return `${license} is not supposed to be present`
+ }
+ })
+ return false
+ }
+
+ const attributionOptions = {
+ blank: {},
+ onlyCreator: { creatorName: TEST_DATA.CREATOR },
+ onlyWork: { workTitle: TEST_DATA.WORK_TITLE },
+ onlyCreatorUrl: { creatorProfileUrl: TEST_DATA.PROFILE_URL },
+ onlyWorkUrl: { workUrl: TEST_DATA.WORK_URL },
+ allData: {
+ creatorName: TEST_DATA.CREATOR,
+ workUrl: TEST_DATA.WORK_URL,
+ workTitle: TEST_DATA.WORK_TITLE,
+ creatorProfileUrl: TEST_DATA.PROFILE_URL
+ }
+ }
+ Object.keys(attributionOptions).forEach((option) => {
+ describe(`${option} with CC0 license should return correct HTML`, () => {
+ const currentLicense = LICENSES.CC0
+ const currentAttributionOptions = attributionOptions[option]
+ const generatedHtml = generateHTML(currentAttributionOptions, currentLicense.SHORT)
+ const wrapper = mount(TestComponent, { propsData: { attribution: generatedHtml } })
+ const expectedWorkTitle = currentAttributionOptions.workTitle || ''
+ const expectedCreatorName = currentAttributionOptions.creatorName || ''
+ const expectedWorkUrl = currentAttributionOptions.workUrl || ''
+ const expectedCreatorProfileUrl = currentAttributionOptions.creatorProfileUrl || ''
+
+ it('has correct wrapper', () => {
+ const para = wrapper.find('.html-string > p')
+ expect(Object.keys(para.attributes()).length).toEqual(3)
+ expect(para.attributes()[CC_NAMESPACE.NAME]).toEqual(CC_NAMESPACE.URI)
+ expect(para.attributes()[DCT_NAMESPACE.NAME]).toEqual(DCT_NAMESPACE.URI)
+ })
+ it(`has correct license link ${currentLicense.SHORT} and license icons `, () => {
+ Object.values(LICENSES).forEach((license) => {
+ const licenseLinkElement = wrapper.find('.license-link').find('a')
+ const licenseImages = (wrapper) => wrapper.findAll('img')
+
+ expect(licenseLinkElement.attributes('rel')).toBe('license')
+ expect(licenseLinkElement.attributes('href')).toBe(currentLicense.URL)
+ expect(licenseLinkElement.text()).toBe(currentLicense.SHORT)
+
+ expect(licenseImages(wrapper).at(0).attributes().style).toBe(ICON_STYLE)
+ const iconSources = licenseImages(wrapper).wrappers.map(img => img.attributes().src)
+ expect(hasIconSourcesErrors(iconSources, currentLicense.ICONS)).toBe(false)
+ })
+ })
+ it(`has correct work attribution: "${expectedWorkTitle}" at url "${expectedWorkUrl}"`, () => {
+ const workTitle = wrapper.find('.work-title')
+ if (!expectedWorkTitle) {
+ expect(workTitle.text()).toBeFalsy()
+ } else {
+ expect(workTitle.text()).toBe(expectedWorkTitle)
+ if (!expectedWorkUrl) {
+ const titleMetaAttribute = workTitle.find('span').attributes().property
+ expect(titleMetaAttribute).toEqual('dct:title')
+ } else {
+ const titleLink = workTitle.find('a')
+ expect(titleLink.attributes().rel).toEqual('cc:attributionURL')
+ expect(titleLink.attributes().property).toEqual('dct:title')
+ expect(titleLink.attributes().href).toEqual(`http://${expectedWorkUrl}`)
+ }
+ }
+ })
+ it(`has correct creator attribution: "${expectedCreatorName}" at url "${expectedCreatorProfileUrl}"`, () => {
+ const creator = wrapper.find('.creator')
+ expect(creator.text()).toBe(expectedCreatorName)
+ if (expectedCreatorName) {
+ if (expectedCreatorProfileUrl) {
+ const creatorLinkAttributes = creator.find('a').attributes()
+ expect(creatorLinkAttributes.rel).toEqual('cc:attributionURL dct:creator')
+ expect(creatorLinkAttributes.property).toEqual('cc:attributionName')
+ expect(creatorLinkAttributes.href).toEqual(`http://${expectedCreatorProfileUrl}`)
+ } else {
+ expect(creator.find('span').attributes().property).toEqual('cc:attributionName')
+ }
+ } else {
+ expect(creator.find('span').exists()).toBeFalsy()
+ expect(creator.find('a').exists()).toBeFalsy()
+ }
+ })
+ })
+ })
+})