2013-09-10 13:30:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import itertools
|
|
|
|
|
|
|
|
import unittest2
|
|
|
|
from lxml import etree as ET, html
|
|
|
|
from lxml.html import builder as h
|
|
|
|
|
|
|
|
from openerp.tests import common
|
|
|
|
|
|
|
|
def attrs(**kwargs):
|
|
|
|
return dict(('data-oe-%s' % key, str(value)) for key, value in kwargs.iteritems())
|
|
|
|
class TestViewSaving(common.TransactionCase):
|
|
|
|
def eq(self, a, b):
|
|
|
|
self.assertEqual(a.tag, b.tag)
|
|
|
|
self.assertEqual(a.attrib, b.attrib)
|
2014-02-27 12:59:34 +00:00
|
|
|
self.assertEqual((a.text or '').strip(), (b.text or '').strip())
|
|
|
|
self.assertEqual((a.tail or '').strip(), (b.tail or '').strip())
|
2013-09-10 13:30:55 +00:00
|
|
|
for ca, cb in itertools.izip_longest(a, b):
|
|
|
|
self.eq(ca, cb)
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(TestViewSaving, self).setUp()
|
|
|
|
self.arch = h.DIV(
|
|
|
|
h.DIV(
|
|
|
|
h.H3("Column 1"),
|
|
|
|
h.UL(
|
|
|
|
h.LI("Item 1"),
|
|
|
|
h.LI("Item 2"),
|
|
|
|
h.LI("Item 3"))),
|
|
|
|
h.DIV(
|
|
|
|
h.H3("Column 2"),
|
|
|
|
h.UL(
|
|
|
|
h.LI("Item 1"),
|
2013-10-09 13:31:12 +00:00
|
|
|
h.LI(h.SPAN("My Company", attrs(model='res.company', id=1, field='name', type='char'))),
|
|
|
|
h.LI(h.SPAN("+00 00 000 00 0 000", attrs(model='res.company', id=1, field='phone', type='char')))
|
2013-09-10 13:30:55 +00:00
|
|
|
))
|
|
|
|
)
|
|
|
|
self.view_id = self.registry('ir.ui.view').create(self.cr, self.uid, {
|
|
|
|
'name': "Test View",
|
|
|
|
'type': 'qweb',
|
|
|
|
'arch': ET.tostring(self.arch, encoding='utf-8').decode('utf-8')
|
|
|
|
})
|
|
|
|
|
|
|
|
def test_embedded_extraction(self):
|
|
|
|
fields = self.registry('ir.ui.view').extract_embedded_fields(
|
|
|
|
self.cr, self.uid, self.arch, context=None)
|
|
|
|
|
|
|
|
expect = [
|
2013-10-09 13:31:12 +00:00
|
|
|
h.SPAN("My Company", attrs(model='res.company', id=1, field='name', type='char')),
|
|
|
|
h.SPAN("+00 00 000 00 0 000", attrs(model='res.company', id=1, field='phone', type='char')),
|
2013-09-10 13:30:55 +00:00
|
|
|
]
|
|
|
|
for actual, expected in itertools.izip_longest(fields, expect):
|
|
|
|
self.eq(actual, expected)
|
|
|
|
|
|
|
|
def test_embedded_save(self):
|
2013-10-09 13:31:12 +00:00
|
|
|
embedded = h.SPAN("+00 00 000 00 0 000", attrs(
|
|
|
|
model='res.company', id=1, field='phone', type='char'))
|
2013-09-10 13:30:55 +00:00
|
|
|
|
|
|
|
self.registry('ir.ui.view').save_embedded_field(self.cr, self.uid, embedded)
|
|
|
|
|
|
|
|
company = self.registry('res.company').browse(self.cr, self.uid, 1)
|
|
|
|
self.assertEqual(company.phone, "+00 00 000 00 0 000")
|
|
|
|
|
|
|
|
@unittest2.skip("save conflict for embedded (saved by third party or previous version in page) not implemented")
|
|
|
|
def test_embedded_conflict(self):
|
|
|
|
e1 = h.SPAN("My Company", attrs(model='res.company', id=1, field='name'))
|
|
|
|
e2 = h.SPAN("Leeroy Jenkins", attrs(model='res.company', id=1, field='name'))
|
|
|
|
|
|
|
|
View = self.registry('ir.ui.view')
|
|
|
|
|
|
|
|
View.save_embedded_field(self.cr, self.uid, e1)
|
|
|
|
# FIXME: more precise exception
|
|
|
|
with self.assertRaises(Exception):
|
|
|
|
View.save_embedded_field(self.cr, self.uid, e2)
|
|
|
|
|
|
|
|
def test_embedded_to_field_ref(self):
|
|
|
|
View = self.registry('ir.ui.view')
|
2013-09-10 14:34:06 +00:00
|
|
|
embedded = h.SPAN("My Company", attrs(expression="bob"))
|
2013-09-10 13:30:55 +00:00
|
|
|
self.eq(
|
|
|
|
View.to_field_ref(self.cr, self.uid, embedded, context=None),
|
2013-09-10 14:34:06 +00:00
|
|
|
h.SPAN({'t-field': 'bob'})
|
2013-09-10 13:30:55 +00:00
|
|
|
)
|
|
|
|
|
2013-09-19 09:25:46 +00:00
|
|
|
def test_to_field_ref_keep_attributes(self):
|
|
|
|
View = self.registry('ir.ui.view')
|
|
|
|
|
|
|
|
att = attrs(expression="bob", model="res.company", id=1, field="name")
|
|
|
|
att['id'] = "whop"
|
|
|
|
att['class'] = "foo bar"
|
|
|
|
embedded = h.SPAN("My Company", att)
|
|
|
|
|
|
|
|
self.eq(View.to_field_ref(self.cr, self.uid, embedded, context=None),
|
|
|
|
h.SPAN({'t-field': 'bob', 'class': 'foo bar', 'id': 'whop'}))
|
|
|
|
|
2013-09-10 13:30:55 +00:00
|
|
|
def test_replace_arch(self):
|
|
|
|
replacement = h.P("Wheee")
|
|
|
|
|
2013-12-17 11:20:34 +00:00
|
|
|
result = self.registry('ir.ui.view').replace_arch_section(
|
|
|
|
self.cr, self.uid, self.view_id, None, replacement)
|
|
|
|
|
|
|
|
self.eq(result, h.DIV("Wheee"))
|
|
|
|
|
|
|
|
def test_replace_arch_2(self):
|
|
|
|
replacement = h.DIV(h.P("Wheee"))
|
|
|
|
|
2013-09-10 13:30:55 +00:00
|
|
|
result = self.registry('ir.ui.view').replace_arch_section(
|
|
|
|
self.cr, self.uid, self.view_id, None, replacement)
|
|
|
|
|
|
|
|
self.eq(result, replacement)
|
|
|
|
|
|
|
|
def test_fixup_arch(self):
|
|
|
|
replacement = h.H1("I am the greatest title alive!")
|
|
|
|
|
|
|
|
result = self.registry('ir.ui.view').replace_arch_section(
|
|
|
|
self.cr, self.uid, self.view_id, '/div/div[1]/h3',
|
|
|
|
replacement)
|
|
|
|
|
|
|
|
self.eq(result, h.DIV(
|
|
|
|
h.DIV(
|
2013-12-17 11:20:34 +00:00
|
|
|
h.H3("I am the greatest title alive!"),
|
2013-09-10 13:30:55 +00:00
|
|
|
h.UL(
|
|
|
|
h.LI("Item 1"),
|
|
|
|
h.LI("Item 2"),
|
|
|
|
h.LI("Item 3"))),
|
|
|
|
h.DIV(
|
|
|
|
h.H3("Column 2"),
|
|
|
|
h.UL(
|
|
|
|
h.LI("Item 1"),
|
2013-10-09 13:31:12 +00:00
|
|
|
h.LI(h.SPAN("My Company", attrs(model='res.company', id=1, field='name', type='char'))),
|
|
|
|
h.LI(h.SPAN("+00 00 000 00 0 000", attrs(model='res.company', id=1, field='phone', type='char')))
|
2013-09-10 13:30:55 +00:00
|
|
|
))
|
|
|
|
))
|
|
|
|
|
|
|
|
def test_multiple_xpath_matches(self):
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
self.registry('ir.ui.view').replace_arch_section(
|
|
|
|
self.cr, self.uid, self.view_id, '/div/div/h3',
|
|
|
|
h.H6("Lol nope"))
|
|
|
|
|
|
|
|
def test_save(self):
|
|
|
|
Company = self.registry('res.company')
|
|
|
|
View = self.registry('ir.ui.view')
|
|
|
|
|
|
|
|
replacement = ET.tostring(h.DIV(
|
|
|
|
h.H3("Column 2"),
|
|
|
|
h.UL(
|
|
|
|
h.LI("wob wob wob"),
|
2013-10-09 13:31:12 +00:00
|
|
|
h.LI(h.SPAN("Acme Corporation", attrs(model='res.company', id=1, field='name', expression="bob", type='char'))),
|
|
|
|
h.LI(h.SPAN("+12 3456789", attrs(model='res.company', id=1, field='phone', expression="edmund", type='char'))),
|
2013-09-10 13:30:55 +00:00
|
|
|
)
|
|
|
|
), encoding='utf-8')
|
|
|
|
View.save(self.cr, self.uid, res_id=self.view_id, value=replacement,
|
|
|
|
xpath='/div/div[2]')
|
|
|
|
|
|
|
|
company = Company.browse(self.cr, self.uid, 1)
|
|
|
|
self.assertEqual(company.name, "Acme Corporation")
|
|
|
|
self.assertEqual(company.phone, "+12 3456789")
|
|
|
|
self.eq(
|
|
|
|
ET.fromstring(View.browse(self.cr, self.uid, self.view_id).arch.encode('utf-8')),
|
|
|
|
h.DIV(
|
|
|
|
h.DIV(
|
|
|
|
h.H3("Column 1"),
|
|
|
|
h.UL(
|
|
|
|
h.LI("Item 1"),
|
|
|
|
h.LI("Item 2"),
|
|
|
|
h.LI("Item 3"))),
|
|
|
|
h.DIV(
|
|
|
|
h.H3("Column 2"),
|
|
|
|
h.UL(
|
|
|
|
h.LI("wob wob wob"),
|
2013-09-10 14:34:06 +00:00
|
|
|
h.LI(h.SPAN({'t-field': "bob"})),
|
|
|
|
h.LI(h.SPAN({'t-field': "edmund"}))
|
|
|
|
))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
[FIX] website, base: escaping and unescaping html
When saving a template in version 8.0, html would be saved as it should
be displayed once on the site. In particular, if some text should be
escaped once send to the browser, it will be saved as such.
But when rendering, a text node content is unescaped two times:
* for translation which seems wrong since we already use .text of a node
which already escaped it, doing it one more time is bad,
* when rendering the template, since the html template is stored in xml,
This commit remove superfluous unescaping for translation, and add an
escaping when saving the changed template content.
closes #7967
opw-646889
2015-08-10 07:20:45 +00:00
|
|
|
def test_save_escaped_text(self):
|
2015-08-18 06:42:41 +00:00
|
|
|
""" Test saving html special chars in text nodes """
|
[FIX] website, base: escaping and unescaping html
When saving a template in version 8.0, html would be saved as it should
be displayed once on the site. In particular, if some text should be
escaped once send to the browser, it will be saved as such.
But when rendering, a text node content is unescaped two times:
* for translation which seems wrong since we already use .text of a node
which already escaped it, doing it one more time is bad,
* when rendering the template, since the html template is stored in xml,
This commit remove superfluous unescaping for translation, and add an
escaping when saving the changed template content.
closes #7967
opw-646889
2015-08-10 07:20:45 +00:00
|
|
|
view_id = self.registry('ir.ui.view').create(self.cr, self.uid, {
|
2015-08-18 06:42:41 +00:00
|
|
|
'arch':'<t><p><h1>hello world</h1></p></t>',
|
[FIX] website, base: escaping and unescaping html
When saving a template in version 8.0, html would be saved as it should
be displayed once on the site. In particular, if some text should be
escaped once send to the browser, it will be saved as such.
But when rendering, a text node content is unescaped two times:
* for translation which seems wrong since we already use .text of a node
which already escaped it, doing it one more time is bad,
* when rendering the template, since the html template is stored in xml,
This commit remove superfluous unescaping for translation, and add an
escaping when saving the changed template content.
closes #7967
opw-646889
2015-08-10 07:20:45 +00:00
|
|
|
'type':'qweb'
|
|
|
|
})
|
|
|
|
view = self.registry('ir.ui.view').browse(self.cr, self.uid, view_id)
|
2015-08-18 06:42:41 +00:00
|
|
|
# script and style text nodes should not escaped client side
|
|
|
|
replacement = '<script>1 && "hello & world"</script>'
|
|
|
|
view.save(replacement, xpath='/t/p/h1')
|
|
|
|
self.assertIn(
|
|
|
|
replacement.replace('&', '&'),
|
|
|
|
view.arch,
|
|
|
|
'inline script should be escaped server side'
|
|
|
|
)
|
|
|
|
self.assertIn(
|
|
|
|
replacement,
|
|
|
|
view.render(),
|
|
|
|
'inline script should not be escaped when rendering'
|
|
|
|
)
|
|
|
|
# common text nodes should be be escaped client side
|
|
|
|
replacement = 'world &amp; &lt;b&gt;cie'
|
|
|
|
view.save(replacement, xpath='/t/p')
|
|
|
|
self.assertIn(replacement, view.arch, 'common text node should not be escaped server side')
|
|
|
|
self.assertIn(
|
|
|
|
replacement,
|
|
|
|
view.render().replace('&', '&'),
|
|
|
|
'text node characters wrongly unescaped when rendering'
|
|
|
|
)
|
[FIX] website, base: escaping and unescaping html
When saving a template in version 8.0, html would be saved as it should
be displayed once on the site. In particular, if some text should be
escaped once send to the browser, it will be saved as such.
But when rendering, a text node content is unescaped two times:
* for translation which seems wrong since we already use .text of a node
which already escaped it, doing it one more time is bad,
* when rendering the template, since the html template is stored in xml,
This commit remove superfluous unescaping for translation, and add an
escaping when saving the changed template content.
closes #7967
opw-646889
2015-08-10 07:20:45 +00:00
|
|
|
|
2013-09-17 08:57:53 +00:00
|
|
|
def test_save_only_embedded(self):
|
|
|
|
Company = self.registry('res.company')
|
|
|
|
company_id = 1
|
|
|
|
Company.write(self.cr, self.uid, company_id, {'name': "Foo Corporation"})
|
|
|
|
|
|
|
|
node = html.tostring(h.SPAN(
|
|
|
|
"Acme Corporation",
|
2013-10-09 13:31:12 +00:00
|
|
|
attrs(model='res.company', id=company_id, field="name", expression='bob', type='char')))
|
2013-09-17 08:57:53 +00:00
|
|
|
|
|
|
|
self.registry('ir.ui.view').save(self.cr, self.uid, res_id=company_id,value=node)
|
|
|
|
|
|
|
|
company = Company.browse(self.cr, self.uid, company_id)
|
|
|
|
self.assertEqual(company.name, "Acme Corporation")
|
|
|
|
|
|
|
|
|
2013-09-10 14:34:06 +00:00
|
|
|
def test_field_tail(self):
|
|
|
|
View = self.registry('ir.ui.view')
|
|
|
|
replacement = ET.tostring(
|
2013-10-09 13:31:12 +00:00
|
|
|
h.LI(h.SPAN("+12 3456789", attrs(
|
|
|
|
model='res.company', id=1, type='char',
|
|
|
|
field='phone', expression="edmund")),
|
2013-09-10 14:34:06 +00:00
|
|
|
"whop whop"
|
|
|
|
), encoding="utf-8")
|
|
|
|
View.save(self.cr, self.uid, res_id = self.view_id, value=replacement,
|
|
|
|
xpath='/div/div[2]/ul/li[3]')
|
|
|
|
|
|
|
|
self.eq(
|
|
|
|
ET.fromstring(View.browse(self.cr, self.uid, self.view_id).arch.encode('utf-8')),
|
|
|
|
h.DIV(
|
|
|
|
h.DIV(
|
|
|
|
h.H3("Column 1"),
|
|
|
|
h.UL(
|
|
|
|
h.LI("Item 1"),
|
|
|
|
h.LI("Item 2"),
|
|
|
|
h.LI("Item 3"))),
|
|
|
|
h.DIV(
|
|
|
|
h.H3("Column 2"),
|
|
|
|
h.UL(
|
|
|
|
h.LI("Item 1"),
|
2013-10-09 13:31:12 +00:00
|
|
|
h.LI(h.SPAN("My Company", attrs(model='res.company', id=1, field='name', type='char'))),
|
2013-09-10 14:34:06 +00:00
|
|
|
h.LI(h.SPAN({'t-field': "edmund"}), "whop whop"),
|
2013-09-10 13:30:55 +00:00
|
|
|
))
|
|
|
|
)
|
|
|
|
)
|