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

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

fixes pyflakes and pylint

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