From bacda923e2370f07c4ffe70a434430d452dafbf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Geelen=20=28sge=29?= Date: Fri, 24 Jun 2022 07:11:08 +0000 Subject: [PATCH] [FIX] web_editor: adapt link unit tests Change the unit test to reflect new spec: * Some TLD are white listed to not required a subdomain to be considered a valid URL. => .com, .net, .org + all ccTLD * URL of a link need to be changed to equal the label, is the label is a new valid URL. * Domain Extention cannot be only digit task-2858621 X-original-commit: c13ba3ec771334c9cd7708305b40b9bf1a234bcc Part-of: odoo/odoo#97297 --- .../odoo-editor/test/spec/copyPaste.test.js | 23 +++ .../editor/odoo-editor/test/spec/link.test.js | 132 +++++++++++++++++- .../odoo-editor/test/spec/urlRegex.test.js | 58 +++++++- 3 files changed, 206 insertions(+), 7 deletions(-) diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/copyPaste.test.js b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/copyPaste.test.js index fb0f90c5823..8142d41b1e0 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/copyPaste.test.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/copyPaste.test.js @@ -1260,6 +1260,29 @@ describe('Copy and paste', () => { }, contentAfter: '

abhttp://www.xyz.com[]cd

', }); + await testEditor(BasicEditor, { + contentBefore: '

ab[]cd

', + stepFunction: async editor => { + await pasteText(editor, 'random'); + }, + contentAfter: '

abrandom[]cd

', + }); + }); + it('should paste and transform an URL in a existing link if pasting valid url', async () => { + await testEditor(BasicEditor, { + contentBefore: '

a[]cd

', + stepFunction: async editor => { + await pasteText(editor, 'https://www.xyz.xdc'); + }, + contentAfter: '

ahttps://www.xyz.xdc[]cd

', + }); + await testEditor(BasicEditor, { + contentBefore: '

ab[].comd

', + stepFunction: async editor => { + await pasteText(editor, 'oom'); + }, + contentAfter: '

aboom[].comd

', + }); }); }); describe('range not collapsed', async () => { diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/link.test.js b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/link.test.js index a1cc177ec73..81c8fd3be6e 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/link.test.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/link.test.js @@ -7,6 +7,7 @@ import { insertLineBreak, testEditor, createLink, + undo } from '../utils.js'; const convertToLink = createLink; @@ -20,7 +21,7 @@ const testUrlRegex = (url) => { }); } const testNotUrlRegex = (url) => { - it(`should be a link: ${url}`, () => { + it(`should NOT be a link: ${url}`, () => { window.chai.assert.notExists(url.match(URL_REGEX)); window.chai.assert.notExists(url.match(URL_REGEX_WITH_INFOS)); }); @@ -28,14 +29,25 @@ const testNotUrlRegex = (url) => { describe('Link', () => { describe('regex', () => { + testUrlRegex('google.com'); + testUrlRegex('google.co.uk'); testUrlRegex('http://google.com'); testUrlRegex('https://google.com'); + testUrlRegex('https://www.google.com'); + testNotUrlRegex('google.shop'); + testUrlRegex('google.com/'); + testUrlRegex('http://google.com/'); + testUrlRegex('https://google.com/'); + testUrlRegex('https://google.co.uk/'); + testUrlRegex('https://www.google.com/'); + testNotUrlRegex('google.shop/'); testUrlRegex('http://google.com/foo#test'); testUrlRegex('a.bcd.ef'); testUrlRegex('a.bc.de'); - testNotUrlRegex('google.com'); testNotUrlRegex('a.bc.d'); testNotUrlRegex('a.b.bc'); + testNotUrlRegex('20.08.2022'); + testNotUrlRegex('31.12'); }); describe('insert Link', () => { // This fails, but why would the cursor stay inside the link @@ -162,6 +174,113 @@ describe('Link', () => { }); }); }); + describe('edit link label', () => { + describe('range collapsed', () => { + it('should not change the url when a link is not edited', async () => { + await testEditor(BasicEditor, { + contentBefore: '

agoogle.comb

', + contentAfter: '

agoogle.comb

', + }); + await testEditor(BasicEditor, { + contentBefore: '

agoogle.combcd[]

', + stepFunction: async editor => { + await insertText(editor, 'e'); + }, + contentAfter: '

agoogle.combcde[]

', + }); + }); + it('should change the url when the label change', async () => { + await testEditor(BasicEditor, { + contentBefore: '

agoogle.co[]b

', + stepFunction: async editor => { + await insertText(editor, 'm'); + }, + contentAfter: '

agoogle.com[]b

', + }); + await testEditor(BasicEditor, { + contentBefore: '

ago[]gle.comb

', + stepFunction: async editor => { + await insertText(editor, 'o'); + }, + contentAfter: '

agoo[]gle.comb

', + }); + await testEditor(BasicEditor, { + contentBefore: '

ago[]gle.comb

', + stepFunction: async editor => { + await insertText(editor, 'o'); + }, + contentAfter: '

agoo[]gle.comb

', + }); + await testEditor(BasicEditor, { + contentBefore: '

ahttp://go[]gle.comb

', + stepFunction: async editor => { + await insertText(editor, 'o'); + }, + contentAfter: '

ahttp://goo[]gle.comb

', + }); + }); + it('should change the url in one step', async () => { + await testEditor(BasicEditor, { + contentBefore: '

agoogle.co[]b

', + stepFunction: async editor => { + await insertText(editor, 'm'); + await undo(editor); + }, + contentAfter: '

agoogle.co[]b

', + }); + }); + it('should not change the url when the label change', async () => { + await testEditor(BasicEditor, { + contentBefore: '

agoogle.com[]b

', + stepFunction: async editor => { + await insertText(editor, 'u'); + }, + contentAfter: '

agoogle.comu[]b

', + }); + }); + }); + describe('range not collapsed', () => { + it('should change the url when the label change', async () => { + await testEditor(BasicEditor, { + contentBefore: '

agoogle.[com]b

', + stepFunction: async editor => { + await insertText(editor, 'be'); + }, + contentAfter: '

agoogle.be[]b

', + }); + await testEditor(BasicEditor, { + contentBefore: '

a[yahoo].comb

', + stepFunction: async editor => { + await insertText(editor, 'google'); + }, + contentAfter: '

agoogle[].comb

', + }); + await testEditor(BasicEditor, { + contentBefore: '

ago[gle.c]omb

', + stepFunction: async editor => { + await insertText(editor, '.c'); + }, + contentAfter: '

ago.c[]omb

', + }); + }); + it('should not change the url when the label change', async () => { + await testEditor(BasicEditor, { + contentBefore: '

agoogl[e.com]b

', + stepFunction: async editor => { + await insertText(editor, 'e'); + }, + contentAfter: '

agoogle[]b

', + }); + await testEditor(BasicEditor, { + contentBefore: '

agoogle.[com]b

', + stepFunction: async editor => { + await insertText(editor, 'vvv'); + }, + contentAfter: '

agoogle.vvv[]b

', + }); + }); + }); + }); describe('remove link', () => { describe('range collapsed', () => { it('should remove the link if collapsed range at the end of a link', async () => { @@ -317,6 +436,15 @@ describe('Link', () => { contentAfter: '

abc[]d

', }); }); + it('should add two character after the link', async () => { + await testEditor(BasicEditor, { + contentBefore: '

ab[]e

', + stepFunction: async editor => { + await insertText(editor, 'cd'); + }, + contentAfter: '

abcd[]e

', + }); + }); it('should add a character after the link if range just after link', async () => { await testEditor(BasicEditor, { contentBefore: '

ab[]d

', diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/urlRegex.test.js b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/urlRegex.test.js index 5b84700f78a..a111d80de21 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/urlRegex.test.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/urlRegex.test.js @@ -1,20 +1,24 @@ -import { URL_REGEX } from '../../src/OdooEditor.js'; +import { URL_REGEX, URL_REGEX_WITH_INFOS } from '../../src/OdooEditor.js'; describe('urlRegex', () => { - it('should not match foo.com', () => { + it('should match foo.com', () => { const url = 'foo.com'; const text = `abc ${url} abc`; const match = text.match(URL_REGEX); + chai.expect(match[0]).to.be.equal(url); + }); + it('should not match foo.else', () => { + const url = 'foo.else'; + const text = `abc ${url} abc`; + const match = text.match(URL_REGEX); chai.expect(match).to.be.equal(null); }); - it('should match abc.abc.abc', () => { const url = 'abc.abc.abc'; const text = `abc ${url} abc`; const match = text.match(URL_REGEX); chai.expect(match[0]).to.be.equal(url); }); - it('should match http://abc.abc.abc', () => { const url = 'http://abc.abc.abc'; const text = `abc ${url} abc`; @@ -27,7 +31,6 @@ describe('urlRegex', () => { const match = text.match(URL_REGEX); chai.expect(match[0]).to.be.equal(url); }); - it('should match 1234-abc.runbot007.odoo.com/web#id=3&menu_id=221', () => { const url = '1234-abc.runbot007.odoo.com/web#id=3&menu_id=221'; const text = `abc ${url} abc`; @@ -41,3 +44,48 @@ describe('urlRegex', () => { chai.expect(match[0]).to.be.equal(url); }); }); + +describe('urlRegex with infos', () => { + it('should match foo.com', () => { + const url = 'foo.com'; + const text = `abc ${url} abc`; + const match = text.match(URL_REGEX_WITH_INFOS); + chai.expect(match[0]).to.be.equal(url); + }); + it('should not match foo.else', () => { + const url = 'foo.else'; + const text = `abc ${url} abc`; + const match = text.match(URL_REGEX_WITH_INFOS); + chai.expect(match).to.be.equal(null); + }); + it('should match abc.abc.abc', () => { + const url = 'abc.abc.abc'; + const text = `abc ${url} abc`; + const match = text.match(URL_REGEX_WITH_INFOS); + chai.expect(match[0]).to.be.equal(url); + }); + it('should match http://abc.abc.abc', () => { + const url = 'http://abc.abc.abc'; + const text = `abc ${url} abc`; + const match = text.match(URL_REGEX_WITH_INFOS); + chai.expect(match[0]).to.be.equal(url); + }); + it('should match https://abc.abc.abc', () => { + const url = 'https://abc.abc.abc'; + const text = `abc ${url} abc`; + const match = text.match(URL_REGEX_WITH_INFOS); + chai.expect(match[0]).to.be.equal(url); + }); + it('should match 1234-abc.runbot007.odoo.com/web#id=3&menu_id=221', () => { + const url = '1234-abc.runbot007.odoo.com/web#id=3&menu_id=221'; + const text = `abc ${url} abc`; + const match = text.match(URL_REGEX_WITH_INFOS); + chai.expect(match[0]).to.be.equal(url); + }); + it('should match https://1234-abc.runbot007.odoo.com/web#id=3&menu_id=221', () => { + const url = 'https://1234-abc.runbot007.odoo.com/web#id=3&menu_id=221'; + const text = `abc ${url} abc`; + const match = text.match(URL_REGEX_WITH_INFOS); + chai.expect(match[0]).to.be.equal(url); + }); +});