Skip to content

Commit

Permalink
Merge pull request #597 from jean/github-main
Browse files Browse the repository at this point in the history
Skip non-existent resources specified by relative path
  • Loading branch information
ebrehault committed Jan 25, 2015
2 parents 0968f37 + 7b47cda commit 59de85f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 18 deletions.
11 changes: 8 additions & 3 deletions Products/CMFPlomino/PlominoForm.py
Expand Up @@ -374,12 +374,17 @@ def _get_resource_urls(self, field_name):
for url in value:
url = url.strip()
if url:
if not url.lower().startswith(('http', '//')):
if not url.lower().startswith(('http', '/')):
if url.startswith('./'):
url = url[2:]
resource = self.unrestrictedTraverse(url, None)
# unrestrictedTraverse knows '../' but not './'
resource = self.unrestrictedTraverse(url[2:], None)
else:
resource = self.unrestrictedTraverse(url, None)
if resource:
url = resource.absolute_url()
else:
logger.info('Missing resource: %s' % url)
continue
yield url

def getResourcesCSS(self):
Expand Down
80 changes: 65 additions & 15 deletions Products/CMFPlomino/tests/form-resources.txt
Expand Up @@ -2,12 +2,14 @@
Test samples databases
===========================

::

>>> portal = layer['portal']
>>> id = portal.invokeFactory('Folder', id='samples')
>>> folder = portal.samples
>>> base_url = folder.absolute_url()

Create some users:
Create some users::

>>> memberName = 'siteManager'
>>> portal.portal_membership.addMember(memberName, memberName, ('Member, Manager',), '', {'fullname': memberName, 'email': memberName+'@dummy.fr',} )
Expand All @@ -26,7 +28,7 @@ Create some users:
>>> memberName = 'demouser'
>>> portal.portal_membership.addMember(memberName, memberName, ('Member',), '', {'fullname': memberName, 'email': memberName+'@dummy.fr',} )

Create the database:
Create the database::

>>> import os.path
>>> dir, _f = os.path.split(os.path.abspath(__file__))
Expand All @@ -40,18 +42,18 @@ Create the database:
>>> xmlstring=f.read()
>>> db.importDesignFromXML(xmlstring)
>>> db.refreshDB()
[...]
[...]
>>> f.close()

Set plomino roles and permissions:
Set plomino roles and permissions::

>>> db.manage_setLocalRoles('userManager', ['PlominoManager'])
>>> db.manage_setLocalRoles('userDesigner', ['PlominoDesigner'])
>>> db.manage_setLocalRoles('userEditor', ['PlominoEditor'])
>>> db.manage_setLocalRoles('userAuthor', ['PlominoAuthor'])
>>> db.manage_setLocalRoles('userReader', ['PlominoReader'])

Create the browser object we'll be using and log in as userEditor:
Create the browser object we'll be using and log in as userEditor::

>>> browser = Browser(layer['app'])
>>> transaction.commit() # enable the browser to see our changes
Expand All @@ -62,7 +64,7 @@ Create the browser object we'll be using and log in as userEditor:
>>> browser.getControl('Password').value = 'userManager'
>>> browser.getControl('Log in').click()

Import form-resources-example db:
Import form-resources-example db::

>>> browser.open(portal_url)
>>> browser.getLink('Log out').click()
Expand All @@ -72,7 +74,7 @@ Import form-resources-example db:
>>> browser.getControl('Log in').click()

Scenario: frmBirthday has one JS resource and one CSS resource
- both are loaded with the form and with a document
- both are loaded with the form and with a document::

>>> from re import compile
>>> browser.open(base_url+'/example-with-form-resources')
Expand Down Expand Up @@ -134,10 +136,59 @@ Scenario: frmBirthday has one JS resource and one CSS resource
True
>>> bool(css_re1.search(browser.contents))
True

Scenario: add two more resources, both to JS and to CSS, as multi-line strings
- all six resources are loaded as separate <script> and <link> elements


Scenario: add two resources at relative paths that don't exist in Plone
- they are dropped on the floor (not passed to browser)::

>>> browser.open(example_url+'/frmBirthday/edit')
>>> browser.getControl(name='ResourcesJS').value = './resources/missing.js'
>>> browser.getControl(name='ResourcesCSS').value = './random/missing.css'
>>> browser.getControl(name='form.button.save').click()

>>> js_pattern2 = r'<script[^<]*src=".*resources/missing.js"[^<]*</script>'
>>> css_pattern2 = r'<link[^>]*href=".*random/missing.css"[^>]*/>'
>>> js_re2=compile(js_pattern2)
>>> css_re2=compile(css_pattern2)

>>> browser.open(example_url+'/frmBirthday/OpenForm')
>>> bool(js_re2.search(browser.contents))
False
>>> bool(css_re2.search(browser.contents))
False

>>> browser.open(example_url+'/frmBirthday/OpenBareForm')
>>> bool(js_re2.search(browser.contents))
False
>>> bool(css_re2.search(browser.contents))
False

>>> browser.open(document_url + '/OpenDocument')
>>> bool(js_re2.search(browser.contents))
False
>>> bool(css_re2.search(browser.contents))
False

>>> browser.open(document_url + '/OpenBareDocument')
>>> bool(js_re2.search(browser.contents))
False
>>> bool(css_re2.search(browser.contents))
False

>>> browser.open(document_url + '/EditDocument')
>>> bool(js_re2.search(browser.contents))
False
>>> bool(css_re2.search(browser.contents))
False

>>> browser.open(document_url + '/EditBareDocument')
>>> bool(js_re2.search(browser.contents))
False
>>> bool(css_re2.search(browser.contents))
False

Scenario: add three resources, both to JS and to CSS, as multi-line strings
- all six resources are loaded as separate <script> and <link> elements::

>>> browser.open(example_url+'/frmBirthday/edit')
>>> longjs = "./resources/test.js\n/testroot.js\nhttp://google.com"
>>> longcss = "./resources/test.css\n/testroot.css\nhttp://google.com"
Expand All @@ -150,7 +201,6 @@ Scenario: add two more resources, both to JS and to CSS, as multi-line strings
'./resources/test.js\r\n/testroot.js\r\nhttp://google.com'
>>> browser.getControl(name='ResourcesCSS').value
'./resources/test.css\r\n/testroot.css\r\nhttp://google.com'


>>> js_pattern2 = r'<script[^<]*src="/testroot.js"[^<]*</script>'
>>> css_pattern2 = r'<link[^>]*href="/testroot.css"[^>]*/>'
Expand Down Expand Up @@ -244,9 +294,9 @@ Scenario: add two more resources, both to JS and to CSS, as multi-line strings
True
>>> bool(css_re3.search(browser.contents))
True

Scenario: no JS or CSS resources
- none of the previous <script> and <link> elements are found
- none of the previous <script> and <link> elements are found::

>>> browser.open(example_url+'/frmBirthday/edit')
>>> browser.getControl(name='ResourcesJS').value = ''
Expand Down Expand Up @@ -342,4 +392,4 @@ Scenario: no JS or CSS resources
False
>>> bool(css_re3.search(browser.contents))
False

0 comments on commit 59de85f

Please sign in to comment.