Skip to content

Commit

Permalink
When indexing for getIcon, check that the returned image is an in…
Browse files Browse the repository at this point in the history
…stance of `plone.namedfile.interfaces.IImage`. fixes gh-3916
  • Loading branch information
frapell committed Mar 5, 2024
1 parent 72ec2ca commit 1ff1f2b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Products/CMFPlone/CatalogTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from plone.i18n.normalizer.base import mapUnicode
from plone.indexer import indexer
from plone.indexer.interfaces import IIndexableObject
from plone.namedfile.interfaces import IImage
from Products.CMFCore.CatalogTool import _mergedLocalRoles
from Products.CMFCore.CatalogTool import CatalogTool as BaseTool
from Products.CMFCore.indexing import processQueue
Expand Down Expand Up @@ -234,7 +235,8 @@ def getIcon(obj):
when obj is an image or has a lead image
or has an image field with name 'image': true else false
"""
return bool(getattr(obj.aq_base, "image", False))
img_field = getattr(obj.aq_base, "image", False)
return bool(IImage.providedBy(img_field))


@indexer(Interface)
Expand Down
16 changes: 16 additions & 0 deletions Products/CMFPlone/tests/testCatalogTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from plone.indexer.wrapper import IndexableObjectWrapper
from plone.uuid.interfaces import IAttributeUUID
from plone.uuid.interfaces import IUUID
from plone.namedfile.file import NamedImage
from Products.CMFCore.indexing import processQueue
from Products.CMFCore.permissions import AccessInactivePortalContent
from Products.CMFPlone.CatalogTool import CatalogTool
Expand Down Expand Up @@ -1414,6 +1415,21 @@ def test_uuid(self):
self.assertTrue(wrapped.UID)
self.assertTrue(uuid == wrapped.UID)

def test_getIcon(self):
from Products.CMFPlone.CatalogTool import getIcon

get_icon = getIcon.callable
self.assertFalse(get_icon(self.folder))
# Create an item inside the test folder
self.folder.invokeFactory("Image", "image", title="Image")
# Do not get the "image" content item
self.assertFalse(get_icon(self.folder))
# Return False if item doesn't have an image
self.assertFalse(get_icon(self.folder.image))
self.folder.image.image=NamedImage(dummy.Image())
# Item has a proper image, return True
self.assertTrue(get_icon(self.folder.image))


class TestObjectProvidedIndexExtender(unittest.TestCase):
def _index(self, object):
Expand Down
2 changes: 2 additions & 0 deletions news/3916.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
When indexing for `getIcon`, check that the returned `image` is an instance of `plone.namedfile.interfaces.IImage`
[frapell]

0 comments on commit 1ff1f2b

Please sign in to comment.