source: products/quintagroup.plonegooglesitemaps/trunk/quintagroup/plonegooglesitemaps/tests/testUpgrade.py @ 3152

Last change on this file since 3152 was 3152, checked in by zidane, 9 years ago

fixes pep8

  • Property svn:eol-style set to native
File size: 8.6 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
19
20class TestUpgrade(TestCase):
21
22    def afterSetUp(self):
23        super(TestUpgrade, self).afterSetUp()
24        self.setup = self.portal.portal_setup
25        self.profile = "quintagroup.plonegooglesitemaps:default"
26
27    def getUpgradeStep(self, sortkey):
28        upgrades = self.setup.listUpgrades(self.profile, show_old=True)
29        upgrade_id = upgrades[sortkey - 1]["id"]
30        step = _upgrade_registry.getUpgradeStep(self.profile, upgrade_id)
31        return step
32
33    def test_upgradeStepsRegistration(self):
34        # Test upgrade steps
35        upgrades = self.setup.listUpgrades(self.profile, show_old=True)
36        self.assertEqual(len(upgrades), 2)
37        self.assertEqual(upgrades[0]["title"].endswith("1.0 to 1.1"), True)
38        self.assertEqual(upgrades[1]["title"].endswith("1.1 to 1.2"), True)
39
40    def test_upgradeSetupRegistration(self):
41        # Test registered upgrade profiles
42        pids = [i['id'] for i in self.setup.listProfileInfo()]
43        self.assertEqual("quintagroup.plonegooglesitemaps:upgrade_1_0_to_1_1" \
44                         in pids, True)
45        self.assertEqual("quintagroup.plonegooglesitemaps:upgrade_1_1_to_1_2" \
46                         in pids, True)
47
48    def test_step_1_0_to_1_1(self):
49        # Prepare testing data
50        catalog = self.portal.portal_catalog
51        if "canonical_path" in catalog._catalog.names:
52            catalog.delColumn("canonical_path")
53        # Upgrade to 1.1 version
54        step = self.getUpgradeStep(1)
55        if step is not None:
56            step.doStep(self.setup)
57        # Canonical_path column must be added to portal_catalog
58        self.assertEqual("canonical_path" in catalog._catalog.names, True)
59
60    def test_step_1_1_to_1_2(self):
61        # Prepare testing data
62        catalog = self.portal.portal_catalog
63        # Create container folder, update its canonical path
64        folder = _createObjectByType('Folder', self.portal, id='test_folder')
65        fldr_cpath = "/my_test_home_folder"
66        fldr_clink = self.portal.absolute_url() + fldr_cpath
67        ICanonicalPath(folder).canonical_path = fldr_cpath
68        # Create inner document, update its canonical_path
69        doc = _createObjectByType('Document', folder, id='test_doc')
70        doc_cpath = "/test_folder/my_test_doc"
71        doc_clink = self.portal.absolute_url() + doc_cpath
72        ICanonicalPath(doc).canonical_path = doc_cpath
73        # Add canonical_path column in catalog
74        if not "canonical_path" in catalog._catalog.names:
75            catalog.addColumn("canonical_path")
76        # Upgrade to 1.2 version
77        step = self.getUpgradeStep(2)
78        if step is not None:
79            step.doStep(self.setup)
80        # canonical_link column replace canonical_path
81        # one in the portal_catalog
82        self.assertEqual("canonical_link" in catalog._catalog.names, True)
83        self.assertEqual("canonical_path" in catalog._catalog.names, False)
84        # canonical_link property refactored from canonical_path one
85        # for inner doc
86        self.assertNotEqual(ICanonicalPath(doc).canonical_path, doc_cpath)
87        self.assertEqual(ICanonicalLink(doc).canonical_link, doc_clink)
88        # canonical_link property refactored from canonical_path one
89        # for home folder
90        self.assertNotEqual(ICanonicalPath(folder).canonical_path, fldr_cpath)
91        self.assertEqual(ICanonicalLink(folder).canonical_link, fldr_clink)
92
93    def testUpgradeCallOnQIReinstall(self):
94        # Get upgrade steps
95        upgrades = _upgrade_registry.getUpgradeStepsForProfile(self.profile)
96        upgrades = dict([(u.sortkey, u) for u in upgrades.values()])
97        orig_ver = self.setup.getLastVersionForProfile(self.profile)
98        self.setup.setLastVersionForProfile(self.profile, "")
99        try:
100            # Replace original handlers with patched ones for test calls
101            called = []
102            upgrades[1].handler = lambda st: called.append("1.0 to 1.1")
103            upgrades[2].handler = lambda st: called.append("1.1 to 1.2")
104            # Run reinstallation
105            qi = self.portal.portal_quickinstaller
106            qi.reinstallProducts(products=config.PROJECTNAME)
107            # Test upgrades call
108            self.assertEqual("1.0 to 1.1" in called, True)
109            self.assertEqual("1.1 to 1.2" in called, True)
110        finally:
111            # Restore original upgrade handlers
112            upgrades[1].handler = gsm_upgrades.upgrade_1_0_to_1_1
113            upgrades[2].handler = gsm_upgrades.upgrade_1_1_to_1_2
114            self.setup.setLastVersionForProfile(self.profile, orig_ver)
115
116try:
117    from Products.qPloneGoogleSitemaps.content.sitemap import Sitemap \
118        as OldSitemap
119except ImportError:
120    PRESENT_OLD_PRODUCT = False
121else:
122    PRESENT_OLD_PRODUCT = True
123
124
125class TestMigrationFromProduct(TestCase):
126
127    def afterSetUp(self):
128        super(TestMigrationFromProduct, self).afterSetUp()
129        self.patch_logger()
130        self.gs = self.portal.portal_setup
131        self.profile = u'profile-quintagroup.plonegooglesitemaps:migrate_qPGSM'
132        self.step = "migrate_qPGSM"
133
134    def patch_logger(self):
135        logger = logging.Logger("test:GSM", logging.NOTSET)
136        self.log = StringIO()
137        self.thndlr = logging.StreamHandler(self.log)
138        self.thndlr.setLevel(logging.DEBUG)
139        self.thndlr.setFormatter(logging.Formatter("%(message)s"))
140        logger.addHandler(self.thndlr)
141
142        self.orig_logger = sh.logger
143        sh.logger = logger
144
145    def beforeTearDown(self):
146        sh.logger = self.orig_logger
147
148    def chkLog(self, chkstr):
149        self.thndlr.flush()
150        self.log.seek(0)
151        logs = self.log.getvalue().split('\n')
152        return chkstr in logs
153
154    def testRemoveOldConfiglet(self):
155        chk_str = "Unregistered 'qPloneGoogleSitemaps' "\
156                  "configlet from portal_controlpanel tool."
157        # 1. No qPloneGoogleSitemaps configlet
158        self.gs.runImportStepFromProfile(self.profile, self.step)
159        self.assert_(not self.chkLog(chk_str), self.log.getvalue())
160        # 2. qPloneGoogleSitemaps configlet in portal
161        self.portal.portal_controlpanel.registerConfiglet(
162            id="qPloneGoogleSitemaps", name="qPloneGoogleSitemaps",
163            action="string:")
164        self.gs.runImportStepFromProfile(self.profile, self.step)
165        self.assert_(self.chkLog(chk_str), self.log.getvalue())
166
167    def testRemoveOldSkinLayer(self):
168        chk_str = "Removed 'qPloneGoogleSitemaps' from 'Plone Default' skin."
169        # 1. No qPloneGoogleSitemaps layer in portal_skins
170        self.gs.runImportStepFromProfile(self.profile, self.step)
171        self.assert_(not self.chkLog(chk_str), self.log.getvalue())
172        # 2. qPloneGoogleSitemaps layer in portal_skins
173        skins = self.portal.portal_skins
174        skinpath = 'qPloneGoogleSitemaps,' + skins.getSkinPath("Plone Default")
175        skins._getSelections()["Plone Default"] = skinpath
176        self.gs.runImportStepFromProfile(self.profile, self.step)
177        self.assert_(self.chkLog(chk_str), self.log.getvalue())
178
179    def testRecriateOldSitemaps(self):
180        chk_str = "Successfully replaced '/%s/sitemap.xml' Sitemap" \
181                  % self.portal.id
182        # 1. No old sitemap present in portal
183        self.gs.runImportStepFromProfile(self.profile, self.step)
184        self.assert_(not self.chkLog(chk_str), self.log.getvalue())
185        # 2. Add old sitemap object in to portal
186        self.portal.invokeFactory("Sitemap", 'sitemap.xml')
187        self.assert_('sitemap.xml' in self.portal.objectIds(),
188                     self.portal.objectIds())
189        sm = self.portal['sitemap.xml']
190        sm.__class__ = OldSitemap
191        self.gs.runImportStepFromProfile(self.profile, self.step)
192        self.assert_(self.chkLog(chk_str), self.log.getvalue())
193
194
195def test_suite():
196    from unittest import TestSuite, makeSuite
197    suite = TestSuite()
198    suite.addTest(makeSuite(TestUpgrade))
199    if PRESENT_OLD_PRODUCT:
200        suite.addTest(makeSuite(TestMigrationFromProduct))
201
202    return suite
203
204if __name__ == '__main__':
205    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the repository browser.