Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an upgrade step to update the dexterity indexer behavior name #301

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/300.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add an upgrade step to fix the dexterity indexer behavior
31 changes: 31 additions & 0 deletions plone/app/upgrade/v60/betas.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,34 @@ def _set_icon_expr(action, icon_expr):
_set_icon_expr(action, new)
elif action.icon_expr != new:
logger.info("Skipping action %r, it looks customized", action_path)


def rename_dexteritytextindexer_behavior(context):
"""Rename collective.dexteritytextindexer behavior to plone.textindexer"""
portal_types = getToolByName(context, "portal_types")

# Gather the FTIs that have the obsolete behavior,
# it can appear with the name or the interface identifier
old_behaviors = {
"collective.dexteritytextindexer",
"collective.dexteritytextindexer.behavior.IDexterityTextIndexer",
}
ftis_to_fix = (
fti
for fti in portal_types.objectValues("Dexterity FTI")
if set(fti.behaviors) & old_behaviors
)

for fti in ftis_to_fix:
# Rename the behavior
behaviors = [
"plone.textindexer" if behavior in old_behaviors else behavior
for behavior in fti.behaviors
]

# Ensure we did not have the behavior more than once
while behaviors.count("plone.textindexer") > 1:
behaviors.remove("plone.textindexer")

# Set the updated behaviors
fti.behaviors = tuple(behaviors)
5 changes: 5 additions & 0 deletions plone/app/upgrade/v60/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@
handler=".betas.add_action_icons"
/>

<gs:upgradeStep
title="Rename the behavior collective.dexteritytextindexer to plone.textindexer"
handler=".betas.rename_dexteritytextindexer_behavior"
/>

</gs:upgradeSteps>

</configure>
32 changes: 32 additions & 0 deletions plone/app/upgrade/v60/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from plone.registry.interfaces import IRegistry
from Products.CMFCore.utils import getToolByName
from zope.component import getUtility
from plone.dexterity.interfaces import IDexterityFTI

import unittest

Expand Down Expand Up @@ -134,3 +135,34 @@ def test_upgrade_plone_module_profiles_multilingual(self):
self.assertEqual(setup.getLastVersionForProfile(profile_id), UNKNOWN)
upgrade_plone_module_profiles(setup)
self.assertEqual(setup.getLastVersionForProfile(profile_id), UNKNOWN)

def test_rename_dexteritytextindexer_behavior(self):
from plone.app.upgrade.v60.betas import rename_dexteritytextindexer_behavior

portal = self.layer["portal"]

fti = getUtility(IDexterityFTI, name="Document")

original_behaviors = fti.behaviors
expected_behaviors = original_behaviors + ("plone.textindexer",)
# If the dexteritytextindexer behavior is not present nothing should change
rename_dexteritytextindexer_behavior(portal)
self.assertTupleEqual(fti.behaviors, original_behaviors)

# If the dexteritytextindexer behavior is present it should be renamed
fti.behaviors = fti.behaviors + ("collective.dexteritytextindexer",)
rename_dexteritytextindexer_behavior(portal)
self.assertTupleEqual(fti.behaviors, expected_behaviors)

# If the old and new dexteritytextindexer behaviors are present,
# we should only have the new one
fti.behaviors = fti.behaviors + ("collective.dexteritytextindexer",)
rename_dexteritytextindexer_behavior(portal)
self.assertTupleEqual(fti.behaviors, expected_behaviors)

# Check that the fix also works with the interface identifier
fti.behaviors = original_behaviors + (
"collective.dexteritytextindexer.behavior.IDexterityTextIndexer",
)
rename_dexteritytextindexer_behavior(portal)
self.assertTupleEqual(fti.behaviors, expected_behaviors)