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 | |
---|
16 | class TestUpgrade(TestCase): |
---|
17 | |
---|
18 | def afterSetUp(self): |
---|
19 | super(TestUpgrade, self).afterSetUp() |
---|
20 | self.setup = self.portal.portal_setup |
---|
21 | self.profile = "quintagroup.plonegooglesitemaps:default" |
---|
22 | |
---|
23 | def getUpgradeStep(self, sortkey): |
---|
24 | upgrades = self.setup.listUpgrades(self.profile, show_old=True) |
---|
25 | upgrade_id = upgrades[sortkey-1]["id"] |
---|
26 | step = _upgrade_registry.getUpgradeStep(self.profile, upgrade_id) |
---|
27 | return step |
---|
28 | |
---|
29 | def test_upgradeStepsRegistration(self): |
---|
30 | # Test upgrade steps |
---|
31 | upgrades = self.setup.listUpgrades(self.profile, show_old=True) |
---|
32 | self.assertEqual(len(upgrades), 2) |
---|
33 | self.assertEqual(upgrades[0]["title"].endswith("1.0 to 1.1"), True) |
---|
34 | self.assertEqual(upgrades[1]["title"].endswith("1.1 to 1.2"), True) |
---|
35 | |
---|
36 | def test_upgradeSetupRegistration(self): |
---|
37 | # Test registered upgrade profiles |
---|
38 | pids = [i['id'] for i in self.setup.listProfileInfo()] |
---|
39 | self.assertEqual("quintagroup.plonegooglesitemaps:upgrade_1_0_to_1_1" in pids, True) |
---|
40 | self.assertEqual("quintagroup.plonegooglesitemaps:upgrade_1_1_to_1_2" in pids, True) |
---|
41 | |
---|
42 | def test_step_1_0_to_1_1(self): |
---|
43 | # Prepare testing data |
---|
44 | catalog = self.portal.portal_catalog |
---|
45 | if "canonical_path" in catalog._catalog.names: |
---|
46 | catalog.delColumn("canonical_path") |
---|
47 | # Upgrade to 1.1 version |
---|
48 | step = self.getUpgradeStep(1) |
---|
49 | if step is not None: |
---|
50 | step.doStep(self.setup) |
---|
51 | # Canonical_path column must be added to portal_catalog |
---|
52 | self.assertEqual("canonical_path" in catalog._catalog.names, True) |
---|
53 | |
---|
54 | def test_step_1_1_to_1_2(self): |
---|
55 | # Prepare testing data |
---|
56 | catalog = self.portal.portal_catalog |
---|
57 | # Create container folder, update its canonical path |
---|
58 | folder = _createObjectByType('Folder', self.portal, id='test_folder') |
---|
59 | fldr_cpath = "/my_test_home_folder" |
---|
60 | fldr_clink = self.portal.absolute_url() + fldr_cpath |
---|
61 | ICanonicalPath(folder).canonical_path = fldr_cpath |
---|
62 | # Create inner document, update its canonical_path |
---|
63 | doc = _createObjectByType('Document', folder, id='test_doc') |
---|
64 | doc_cpath = "/test_folder/my_test_doc" |
---|
65 | doc_clink = self.portal.absolute_url() + doc_cpath |
---|
66 | ICanonicalPath(doc).canonical_path = doc_cpath |
---|
67 | # Add canonical_path column in catalog |
---|
68 | if not "canonical_path" in catalog._catalog.names: |
---|
69 | catalog.addColumn("canonical_path") |
---|
70 | # Upgrade to 1.2 version |
---|
71 | step = self.getUpgradeStep(2) |
---|
72 | if step is not None: |
---|
73 | step.doStep(self.setup) |
---|
74 | # canonical_link column replace canonical_path one in the portal_catalog |
---|
75 | self.assertEqual("canonical_link" in catalog._catalog.names, True) |
---|
76 | self.assertEqual("canonical_path" in catalog._catalog.names, False) |
---|
77 | # canonical_link property refactored from canonical_path one for inner doc |
---|
78 | self.assertNotEqual(ICanonicalPath(doc).canonical_path, doc_cpath) |
---|
79 | self.assertEqual(ICanonicalLink(doc).canonical_link, doc_clink) |
---|
80 | # canonical_link property refactored from canonical_path one for home folder |
---|
81 | self.assertNotEqual(ICanonicalPath(folder).canonical_path, fldr_cpath) |
---|
82 | self.assertEqual(ICanonicalLink(folder).canonical_link, fldr_clink) |
---|
83 | |
---|
84 | def testUpgradeCallOnQIReinstall(self): |
---|
85 | # Get upgrade steps |
---|
86 | upgrades = _upgrade_registry.getUpgradeStepsForProfile(self.profile) |
---|
87 | upgrades = dict([(u.sortkey, u) for u in upgrades.values()]) |
---|
88 | orig_ver = self.setup.getLastVersionForProfile(self.profile) |
---|
89 | self.setup.setLastVersionForProfile(self.profile, "") |
---|
90 | try: |
---|
91 | # Replace original handlers with patched ones for test calls |
---|
92 | called = [] |
---|
93 | upgrades[1].handler = lambda st:called.append("1.0 to 1.1") |
---|
94 | upgrades[2].handler = lambda st:called.append("1.1 to 1.2") |
---|
95 | # Run reinstallation |
---|
96 | self.portal.portal_quickinstaller.reinstallProducts(products=config.PROJECTNAME) |
---|
97 | # Test upgrades call |
---|
98 | self.assertEqual("1.0 to 1.1" in called, True) |
---|
99 | self.assertEqual("1.1 to 1.2" in called, True) |
---|
100 | finally: |
---|
101 | # Restore original upgrade handlers |
---|
102 | upgrades[1].handler = gsm_upgrades.upgrade_1_0_to_1_1 |
---|
103 | upgrades[2].handler = gsm_upgrades.upgrade_1_1_to_1_2 |
---|
104 | self.setup.setLastVersionForProfile(self.profile, orig_ver) |
---|
105 | |
---|
106 | |
---|
107 | def test_suite(): |
---|
108 | from unittest import TestSuite, makeSuite |
---|
109 | suite = TestSuite() |
---|
110 | suite.addTest(makeSuite(TestUpgrade)) |
---|
111 | return suite |
---|
112 | |
---|
113 | if __name__ == '__main__': |
---|
114 | unittest.main(defaultTest='test_suite') |
---|