def setUp(self):
     super(PreviewFunctionalTestCase, self).setUp()
     # Add Sunburst skin if it does not exist.
     dummy_sunburst_skin(self.portal)
     # Add fake script to avoid strange problem.
     fake_externalEditorEnabled(self.portal)
     # Create new skin based on Sunburst Theme and make this the
     # default skin.
     new_default_skin(self.portal)
    def testSkinSwitchedOnFakeTraversalEvent(self):
        response = self.publish(
            self.folder_path + '/@@switchDefaultSkin?skin_name=Sunburst%20Theme',
            basic=self.basic_auth)
        self.assertEqual(response.getStatus(), 302)
        self.assertEqual(self.folder.absolute_url(),
                         response.getHeader("Location"))

        # Create new skin based on Sunburst Theme and make this the
        # default skin.
        new_default_skin(self.portal)
        self.assertEqual("Monty Python Skin", self.folder.getCurrentSkinName())

        request = TestRequest(SERVER_URL='http://localhost')
        event = FakeTraversalEvent(self.folder, request)
        switch_skin(self.folder, event)
        self.assertEqual("Sunburst Theme", self.folder.getCurrentSkinName())
        self.assertEqual(0, request.get("editskinswitched", 0))
    def testSkinSwitchedOnRealTraversalEvent(self):
        # Create new skin based on Sunburst Theme and make this the
        # default skin.
        new_default_skin(self.portal)
        response = self.publish(
            self.folder_path + '/getCurrentSkinName',
            basic=self.basic_auth)
        self.assertEqual("Monty Python Skin", response.getBody())

        response = self.publish(
            self.folder_path + '/@@switchDefaultSkin?skin_name=Sunburst%20Theme',
            basic=self.basic_auth)
        self.assertEqual(response.getStatus(), 302)
        self.assertEqual(self.folder.absolute_url(),
                         response.getHeader("Location"))

        response = self.publish(
            self.folder_path + '/getCurrentSkinName',
            basic=self.basic_auth)
        self.assertEqual("Sunburst Theme", response.getBody())
    def testSkinSwitchUsingFallbackForm(self):
        self.loginAsPortalOwner()
        folder_url = self.folder.absolute_url()
        portal_url = self.portal.absolute_url()
        # Add a second folder:
        self.portal.invokeFactory('Folder', id='folder2')
        folder2 = self.portal.folder2
        wf_tool = getToolByName(self.portal, 'portal_workflow')
        wf_tool.doActionFor(folder2, 'publish')
        folder2_url = folder2.absolute_url()
        # Add a sub folder:
        folder2.invokeFactory('Folder', id='sub_folder')
        sub_folder = folder2.sub_folder
        wf_tool.doActionFor(sub_folder, 'publish')
        sub_folder_url = sub_folder.absolute_url()
        # Create a new default skin.
        new_default_skin(self.portal)

        # Set the default skin for the first folder.
        browser = Browser()
        browser.handleErrors = False
        self._login(browser)
        browser.open(folder_url + "/select_skin")
        control = browser.getControl(name="skin_name")
        self.assertEqual([], control.value)
        control.value = ["Monty Python Skin"]

        # Saving the form redirects back to the folder.
        browser.getControl(name="form.button.Save").click()
        self.assertEqual(folder_url, browser.url)

        # Going back to the form has the skin selected.
        browser.open(folder_url + "/select_skin")
        control = browser.getControl(name="skin_name")
        self.assertEqual(["Monty Python Skin"], control.value)

        # Set the default skin for the second folder.  We have to do
        # this as portal owner.
        self._login(browser, login_name=ptc.portal_owner)
        browser.open(folder2_url + "/select_skin")
        control = browser.getControl(name="skin_name")
        self.assertEqual([], control.value)
        control.value = ["Sunburst Theme"]
        browser.getControl(name="form.button.Save").click()
        browser.open(folder2_url + "/select_skin")
        control = browser.getControl(name="skin_name")
        self.assertEqual(["Sunburst Theme"], control.value)

        # Set a different default skin for the sub folder.
        browser.open(sub_folder_url + "/select_skin")
        control = browser.getControl(name="skin_name")
        self.assertEqual([], control.value)
        control.value = ["Monty Python Skin"]
        browser.getControl(name="form.button.Save").click()
        browser.open(sub_folder_url + "/select_skin")
        control = browser.getControl(name="skin_name")
        self.assertEqual(["Monty Python Skin"], control.value)

        # What is the current skin name in a few contexts?
        self._login(browser)
        browser.open(portal_url + '/getCurrentSkinName')
        self.assertEqual(browser.contents, 'Monty Python Skin')
        browser.open(folder_url + '/getCurrentSkinName')
        self.assertEqual(browser.contents, 'Monty Python Skin')
        browser.open(folder2_url + '/getCurrentSkinName')
        self.assertEqual(browser.contents, 'Sunburst Theme')
        browser.open(sub_folder_url + '/getCurrentSkinName')
        self.assertEqual(browser.contents, 'Monty Python Skin')

        # Check the effect this has when visiting these contexts.  We
        # do this with an almost empty browser view that shows a
        # viewlet that is specifically registered for the Monty Python
        # theme and not the Sunburst Theme theme.  Plus a viewlet that
        # shows which marker interfaces the request provides.

        # First the portal root:
        browser.open(portal_url + '/@@viewlet-test')
        self.assertTrue('We want a shrubbery!' in browser.contents)
        self.assertTrue('interfaces.IMyTheme' in browser.contents)

        # Then the first folder:
        browser.open(folder_url + '/@@viewlet-test')
        self.assertTrue('We want a shrubbery!' in browser.contents)
        self.assertTrue('interfaces.IMyTheme' in browser.contents)

        # Then the second folder:
        browser.open(folder2_url + '/@@viewlet-test')
        self.assertFalse('We want a shrubbery!' in browser.contents)
        self.assertFalse('interfaces.IMyTheme' in browser.contents)

        # Then the sub folder:
        browser.open(sub_folder_url + '/@@viewlet-test')
        self.assertTrue('We want a shrubbery!' in browser.contents)
        self.assertTrue('interfaces.IMyTheme' in browser.contents)