source: products/quintagroup.plonegooglesitemaps/branches/migratioin_product/quintagroup/plonegooglesitemaps/tests/testUpgrade.py @ 2839

Last change on this file since 2839 was 2839, checked in by mylan, 9 years ago

#230: some upgrade tests reorganization

  • Property svn:eol-style set to native
File size: 8.5 KB
Line 
1#
2# Tests for quintagroup.plonegooglesitemaps upgrading
3#
4
5import logging 
6from base import *
7from zope.component import getSiteManager
8from StringIO import StringIO
9
10from Products.CMFPlone.utils import _createObjectByType
11from Products.GenericSetup.upgrade import _upgrade_registry
12from archetypes.schemaextender.interfaces import ISchemaExtender
13from quintagroup.plonegooglesitemaps import config
14from quintagroup.plonegooglesitemaps import upgrades as gsm_upgrades
15from quintagroup.plonegooglesitemaps import setuphandlers as sh
16from quintagroup.canonicalpath.interfaces import ICanonicalPath
17from quintagroup.canonicalpath.interfaces import ICanonicalLink
18
19class TestUpgrade(TestCase):
20
21    def afterSetUp(self):
22        super(TestUpgrade, self).afterSetUp()
23        self.setup = self.portal.portal_setup
24        self.profile = "quintagroup.plonegooglesitemaps:default"
25
26    def getUpgradeStep(self, sortkey):
27        upgrades = self.setup.listUpgrades(self.profile, show_old=True)
28        upgrade_id = upgrades[sortkey-1]["id"]
29        step = _upgrade_registry.getUpgradeStep(self.profile, upgrade_id)
30        return step
31
32    def test_upgradeStepsRegistration(self):
33        # Test upgrade steps
34        upgrades = self.setup.listUpgrades(self.profile, show_old=True)
35        self.assertEqual(len(upgrades), 2)
36        self.assertEqual(upgrades[0]["title"].endswith("1.0 to 1.1"), True)
37        self.assertEqual(upgrades[1]["title"].endswith("1.1 to 1.2"), True)
38
39    def test_upgradeSetupRegistration(self):
40        # Test registered upgrade profiles
41        pids = [i['id'] for i in self.setup.listProfileInfo()]
42        self.assertEqual("quintagroup.plonegooglesitemaps:upgrade_1_0_to_1_1" in pids, True)
43        self.assertEqual("quintagroup.plonegooglesitemaps:upgrade_1_1_to_1_2" 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 version
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
87    def testUpgradeCallOnQIReinstall(self):
88        # Get upgrade steps
89        upgrades = _upgrade_registry.getUpgradeStepsForProfile(self.profile)
90        upgrades = dict([(u.sortkey, u) for u in upgrades.values()])
91        orig_ver = self.setup.getLastVersionForProfile(self.profile)
92        self.setup.setLastVersionForProfile(self.profile, "")
93        try:
94            # Replace original handlers with patched ones for test calls
95            called = []
96            upgrades[1].handler = lambda st:called.append("1.0 to 1.1")
97            upgrades[2].handler = lambda st:called.append("1.1 to 1.2")
98            # Run reinstallation
99            self.portal.portal_quickinstaller.reinstallProducts(products=config.PROJECTNAME)
100            # Test upgrades call
101            self.assertEqual("1.0 to 1.1" in called, True)
102            self.assertEqual("1.1 to 1.2" in called, True)
103        finally:
104            # Restore original upgrade handlers
105            upgrades[1].handler = gsm_upgrades.upgrade_1_0_to_1_1
106            upgrades[2].handler = gsm_upgrades.upgrade_1_1_to_1_2
107            self.setup.setLastVersionForProfile(self.profile, orig_ver)
108
109try:
110    from Products.qPloneGoogleSitemaps.content.sitemap import Sitemap as OldSitemap
111except ImportError:
112    PRESENT_OLD_PRODUCT = False
113else:
114    PRESENT_OLD_PRODUCT = True
115   
116class TestMigrationFromProduct(TestCase):
117
118    def afterSetUp(self):
119        super(TestMigrationFromProduct, self).afterSetUp()
120        self.patch_logger()
121        self.gs = self.portal.portal_setup
122        self.profile = u'profile-quintagroup.plonegooglesitemaps:migrate_qPGSM'
123        self.step = "migrate_qPGSM"
124
125    def patch_logger(self):
126        logger = logging.Logger("test:GSM", logging.NOTSET)
127        self.log = StringIO()
128        self.thndlr = logging.StreamHandler(self.log)
129        self.thndlr.setLevel(logging.DEBUG)
130        self.thndlr.setFormatter(logging.Formatter("%(message)s"))
131        logger.addHandler(self.thndlr)
132
133        self.orig_logger = sh.logger
134        sh.logger = logger
135
136    def beforeTearDown(self):
137        sh.logger = self.orig_logger
138
139    def chkLog(self, chkstr):
140        self.thndlr.flush()
141        self.log.seek(0)
142        logs = self.log.getvalue().split('\n')
143        return chkstr in logs
144
145    def testRemoveOldConfiglet(self):
146        chk_str = "Unregistered 'qPloneGoogleSitemaps' "\
147                  "configlet from portal_controlpanel tool."
148        # 1. No qPloneGoogleSitemaps configlet
149        self.gs.runImportStepFromProfile(self.profile, self.step)
150        self.assert_(not self.chkLog(chk_str), self.log.getvalue())
151        # 2. qPloneGoogleSitemaps configlet in portal
152        self.portal.portal_controlpanel.registerConfiglet(
153            id="qPloneGoogleSitemaps", name="qPloneGoogleSitemaps",
154            action="string:")
155        self.gs.runImportStepFromProfile(self.profile, self.step)
156        self.assert_(self.chkLog(chk_str), self.log.getvalue())
157
158    def testRemoveOldSkinLayer(self):
159        chk_str = "Removed 'qPloneGoogleSitemaps' from 'Plone Default' skin."
160        # 1. No qPloneGoogleSitemaps layer in portal_skins
161        self.gs.runImportStepFromProfile(self.profile, self.step)
162        self.assert_(not self.chkLog(chk_str), self.log.getvalue())
163        # 2. qPloneGoogleSitemaps layer in portal_skins
164        skins = self.portal.portal_skins
165        skinpath = 'qPloneGoogleSitemaps,' + skins.getSkinPath("Plone Default")
166        skins._getSelections()["Plone Default"] = skinpath
167        self.gs.runImportStepFromProfile(self.profile, self.step)
168        self.assert_(self.chkLog(chk_str), self.log.getvalue())
169
170    def testRecriateOldSitemaps(self):
171        chk_str = "Successfully replaced '/%s/sitemap.xml' Sitemap" \
172                  % self.portal.id
173        # 1. No old sitemap present in portal
174        self.gs.runImportStepFromProfile(self.profile, self.step)
175        self.assert_(not self.chkLog(chk_str), self.log.getvalue())
176        # 2. Add old sitemap object in to portal
177        self.portal.invokeFactory("Sitemap", 'sitemap.xml')
178        self.assert_('sitemap.xml' in self.portal.objectIds(), self.portal.objectIds())
179        sm = self.portal['sitemap.xml']
180        sm.__class__ = OldSitemap
181        self.gs.runImportStepFromProfile(self.profile, self.step)
182        self.assert_(self.chkLog(chk_str), self.log.getvalue())
183       
184
185def test_suite():
186    from unittest import TestSuite, makeSuite
187    suite = TestSuite()
188    suite.addTest(makeSuite(TestUpgrade))
189    if PRESENT_OLD_PRODUCT:
190        suite.addTest(makeSuite(TestMigrationFromProduct))
191
192    return suite
193
194if __name__ == '__main__':
195    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the repository browser.