1 | # |
---|
2 | # Tests for quintagroup.plonegooglesitemaps upgrading |
---|
3 | # |
---|
4 | |
---|
5 | from base import * |
---|
6 | from zope.component import getSiteManager |
---|
7 | |
---|
8 | from Products.CMFPlone.utils import _createObjectByType |
---|
9 | from Products.GenericSetup.upgrade import _upgrade_registry |
---|
10 | from archetypes.schemaextender.interfaces import ISchemaExtender |
---|
11 | from quintagroup.plonegooglesitemaps import config |
---|
12 | from quintagroup.plonegooglesitemaps import upgrades as gsm_upgrades |
---|
13 | from quintagroup.canonicalpath.interfaces import ICanonicalPath |
---|
14 | from quintagroup.canonicalpath.interfaces import ICanonicalLink |
---|
15 | from quintagroup.plonegooglesitemaps.content.newsextender import NewsExtender |
---|
16 | |
---|
17 | class TestUpgrade(TestCase): |
---|
18 | |
---|
19 | def afterSetUp(self): |
---|
20 | super(TestUpgrade, self).afterSetUp() |
---|
21 | self.setup = self.portal.portal_setup |
---|
22 | self.profile = "quintagroup.plonegooglesitemaps:default" |
---|
23 | |
---|
24 | def getUpgradeStep(self, sortkey): |
---|
25 | upgrades = self.setup.listUpgrades(self.profile, show_old=True) |
---|
26 | upgrade_id = upgrades[sortkey-1]["id"] |
---|
27 | step = _upgrade_registry.getUpgradeStep(self.profile, upgrade_id) |
---|
28 | return step |
---|
29 | |
---|
30 | def test_upgradeStepsRegistration(self): |
---|
31 | # Test upgrade steps |
---|
32 | upgrades = self.setup.listUpgrades(self.profile, show_old=True) |
---|
33 | self.assertEqual(len(upgrades), 3) |
---|
34 | self.assertEqual(upgrades[0]["title"].endswith("1.0 to 1.1"), True) |
---|
35 | self.assertEqual(upgrades[1]["title"].endswith("1.1 to 1.2"), True) |
---|
36 | self.assertEqual(upgrades[2]["title"].endswith("1.2 to 1.3"), True) |
---|
37 | |
---|
38 | def test_upgradeSetupRegistration(self): |
---|
39 | # Test registered upgrade profiles |
---|
40 | pids = [i['id'] for i in self.setup.listProfileInfo()] |
---|
41 | self.assertEqual("quintagroup.plonegooglesitemaps:upgrade_1_0_to_1_1" in pids, True) |
---|
42 | self.assertEqual("quintagroup.plonegooglesitemaps:upgrade_1_1_to_1_2" in pids, True) |
---|
43 | self.assertEqual("quintagroup.plonegooglesitemaps:upgrade_1_2_to_1_3" in pids, True) |
---|
44 | |
---|
45 | def test_step_1_0_to_1_1(self): |
---|
46 | # Prepare testing data |
---|
47 | catalog = self.portal.portal_catalog |
---|
48 | if "canonical_path" in catalog._catalog.names: |
---|
49 | catalog.delColumn("canonical_path") |
---|
50 | # Upgrade to 1.1 version |
---|
51 | step = self.getUpgradeStep(1) |
---|
52 | if step is not None: |
---|
53 | step.doStep(self.setup) |
---|
54 | # Canonical_path column must be added to portal_catalog |
---|
55 | self.assertEqual("canonical_path" in catalog._catalog.names, True) |
---|
56 | |
---|
57 | def test_step_1_1_to_1_2(self): |
---|
58 | # Prepare testing data |
---|
59 | catalog = self.portal.portal_catalog |
---|
60 | # Create container folder, update its canonical path |
---|
61 | folder = _createObjectByType('Folder', self.portal, id='test_folder') |
---|
62 | fldr_cpath = "/my_test_home_folder" |
---|
63 | fldr_clink = self.portal.absolute_url() + fldr_cpath |
---|
64 | ICanonicalPath(folder).canonical_path = fldr_cpath |
---|
65 | # Create inner document, update its canonical_path |
---|
66 | doc = _createObjectByType('Document', folder, id='test_doc') |
---|
67 | doc_cpath = "/test_folder/my_test_doc" |
---|
68 | doc_clink = self.portal.absolute_url() + doc_cpath |
---|
69 | ICanonicalPath(doc).canonical_path = doc_cpath |
---|
70 | # Add canonical_path column in catalog |
---|
71 | if not "canonical_path" in catalog._catalog.names: |
---|
72 | catalog.addColumn("canonical_path") |
---|
73 | # Upgrade to 1.2 versionb |
---|
74 | step = self.getUpgradeStep(2) |
---|
75 | if step is not None: |
---|
76 | step.doStep(self.setup) |
---|
77 | # canonical_link column replace canonical_path one in the portal_catalog |
---|
78 | self.assertEqual("canonical_link" in catalog._catalog.names, True) |
---|
79 | self.assertEqual("canonical_path" in catalog._catalog.names, False) |
---|
80 | # canonical_link property refactored from canonical_path one for inner doc |
---|
81 | self.assertNotEqual(ICanonicalPath(doc).canonical_path, doc_cpath) |
---|
82 | self.assertEqual(ICanonicalLink(doc).canonical_link, doc_clink) |
---|
83 | # canonical_link property refactored from canonical_path one for home folder |
---|
84 | self.assertNotEqual(ICanonicalPath(folder).canonical_path, fldr_cpath) |
---|
85 | self.assertEqual(ICanonicalLink(folder).canonical_link, fldr_clink) |
---|
86 | # canonical_link brain must contains updated canonical_link data |
---|
87 | brain = catalog(id="test_doc")[0] |
---|
88 | self.assertEqual(brain.canonical_link, doc_clink) |
---|
89 | brain = catalog(id="test_folder")[0] |
---|
90 | self.assertEqual(brain.canonical_link, fldr_clink) |
---|
91 | |
---|
92 | def test_step_1_2_to_1_3(self): |
---|
93 | # Prepare testing data |
---|
94 | cols = ["Language", "gsm_access", "gsm_genres", "gsm_stock"] |
---|
95 | lsm = getSiteManager(self.portal) |
---|
96 | catalog = self.portal.portal_catalog |
---|
97 | # Remove tested columns, if its exists |
---|
98 | [catalog.delColumn(col) for col in cols if col in catalog._catalog.names] |
---|
99 | # Remove schema extender adapter from local component registry, if its exists |
---|
100 | for r in tuple(lsm.registeredAdapters()): |
---|
101 | if r.factory == NewsExtender: |
---|
102 | lsm.unregisterAdapter(factory=r.factory, name=r.name, |
---|
103 | required=r.required, provided=r.provided) |
---|
104 | # Upgrade to 1.3 version |
---|
105 | step = self.getUpgradeStep(3) |
---|
106 | if step is not None: |
---|
107 | step.doStep(self.setup) |
---|
108 | # Test if columns added to portal_catalog |
---|
109 | for col in cols: |
---|
110 | self.assertEqual(col in catalog._catalog.names, True) |
---|
111 | # Test if schema extender adapter added into local component registry |
---|
112 | factories = [r.factory for r in tuple(lsm.registeredAdapters())] |
---|
113 | self.assertEqual(NewsExtender in factories, True) |
---|
114 | |
---|
115 | def testUpgradeCallOnQIReinstall(self): |
---|
116 | # Get upgrade steps |
---|
117 | upgrades = _upgrade_registry.getUpgradeStepsForProfile(self.profile) |
---|
118 | upgrades = dict([(u.sortkey, u) for u in upgrades.values()]) |
---|
119 | try: |
---|
120 | # Replace original handlers with patched ones for test calls |
---|
121 | called = [] |
---|
122 | upgrades[1].handler = lambda st:called.append("1.0 to 1.1") |
---|
123 | upgrades[2].handler = lambda st:called.append("1.1 to 1.2") |
---|
124 | upgrades[3].handler = lambda st:called.append("1.2 to 1.3") |
---|
125 | # Run reinstallation |
---|
126 | self.portal.portal_quickinstaller.reinstallProducts(products=config.PROJECTNAME) |
---|
127 | # Test upgrades call |
---|
128 | self.assertEqual("1.0 to 1.1" in called, True) |
---|
129 | self.assertEqual("1.1 to 1.2" in called, True) |
---|
130 | self.assertEqual("1.2 to 1.3" in called, True) |
---|
131 | finally: |
---|
132 | # Restore original upgrade handlers |
---|
133 | upgrades[1].handler = gsm_upgrades.upgrade_1_0_to_1_1 |
---|
134 | upgrades[2].handler = gsm_upgrades.upgrade_1_1_to_1_2 |
---|
135 | upgrades[3].handler = gsm_upgrades.upgrade_1_2_to_1_3 |
---|
136 | |
---|
137 | |
---|
138 | |
---|
139 | def test_suite(): |
---|
140 | from unittest import TestSuite, makeSuite |
---|
141 | suite = TestSuite() |
---|
142 | suite.addTest(makeSuite(TestUpgrade)) |
---|
143 | return suite |
---|
144 | |
---|
145 | if __name__ == '__main__': |
---|
146 | unittest.main(defaultTest='test_suite') |
---|