コード例 #1
0
 def get_bundles(self):
     result = {}
     records = self.registry.collectionOfInterface(IBundleRegistry,
                                                   prefix="plone.bundles",
                                                   check=False)
     for name, record in records.items():
         result[name] = Bundle(record)
     return result
コード例 #2
0
    def test_cooking_missing_browserresource(self):
        registry = getUtility(IRegistry)
        registry['plone.resources.development'] = True
        bundles = registry.collectionOfInterface(IBundleRegistry,
                                                 prefix="plone.bundles")
        bundle = bundles.add('barbar')
        bundle.jscompilation = '++resource++notfound/barbar-compiled.js'
        bundle.csscompilation = '++resource++notfound/barbar-compiled.css'
        bundle.compile = False
        bundle.merge_with = 'default'

        bundle = Bundle(bundle)

        # cookWhenChangingSettings(self.portal, bundle)
        scripts = ScriptsView(self.layer['portal'], self.layer['request'],
                              None)
        scripts.update()
        results = scripts.scripts()
        # at least have jquery.min.js, config.js, require.js, etc.
        self.assertTrue(len(results) > 2)
    def test_cooking_missing(self):
        registry = getUtility(IRegistry)
        bundles = registry.collectionOfInterface(IBundleRegistry,
                                                 prefix="plone.bundles")
        bundle = bundles.add('foobar')
        bundle.jscompilation = '++plone++static/foobar-compiled.js'
        bundle.csscompilation = '++plone++static/foobar-compiled.css'

        resources = registry.collectionOfInterface(IResourceRegistry,
                                                   prefix="plone.resources")
        resource = resources.add('foobar')

        resource.js = '++plone++static/foobar.js'
        bundle.resources = ['foobar']

        bundle = Bundle(bundle)

        cookWhenChangingSettings(self.portal, bundle)
        resp = subrequest('{0}/++plone++static/foobar-compiled.js'.format(
            self.portal.absolute_url()))
        self.assertTrue('Could not find resource' in resp.getBody())
class TestScriptsViewlet(PloneTestCase.PloneTestCase):
    def test_scripts_viewlet(self):
        scripts = ScriptsView(self.layer['portal'], self.layer['request'],
                              None)
        scripts.update()
        results = scripts.scripts()
        self.assertEqual(results[0]['bundle'], 'production')
        self.assertTrue(results[0]['src'].startswith(
            'http://nohost/plone/++plone++production/++unique++'))
        self.assertTrue(results[0]['src'].endswith('/default.js'))
        self.assertEqual(results[1]['bundle'], 'production')
        self.assertTrue(results[1]['src'].startswith(
            'http://nohost/plone/++plone++production/++unique++'))
        self.assertTrue(results[1]['src'].endswith('/logged-in.js'))
        self.assertEqual(len(results), 2)

    def test_scripts_viewlet_anonymous(self):
        logout()
        scripts = ScriptsView(self.layer['portal'], self.layer['request'],
                              None)
        scripts.update()
        results = scripts.scripts()
        self.assertEqual(results[0]['bundle'], 'production')
        self.assertTrue(results[0]['src'].startswith(
            'http://nohost/plone/++plone++production/++unique++'))
        self.assertTrue(results[0]['src'].endswith('/default.js'))
        self.assertEqual(len(results), 1)

    @mock.patch.object(
        ScriptsView,
        'get_resources',
        new=lambda self: {'foo': DummyResource('++resource++foo.js')})
    def test_request_resources(self):
        add_resource_on_request(self.layer['request'], 'foo')
        scripts = ScriptsView(self.layer['portal'], self.layer['request'],
                              None)
        scripts.update()
        results = scripts.scripts()
        self.assertEqual(
            results[-1], {
                'src': 'http://nohost/plone/++resource++foo.js',
                'conditionalcomment': '',
                'resetrjs': False,
                'bundle': 'none'
            })

    def test_request_resources_not_add_same_twice(self):
        req = self.layer['request']
        add_resource_on_request(req, 'foo')
        add_resource_on_request(req, 'foo')

        self.assertEqual(len(req.enabled_resources), 1)

    def test_request_bundles_not_add_same_twice(self):
        req = self.layer['request']
        add_bundle_on_request(req, 'foo')
        add_bundle_on_request(req, 'foo')

        self.assertEqual(len(req.enabled_bundles), 1)

    @mock.patch.object(
        ScriptsView,
        'get_bundles',
        new=lambda self: {'foo': Bundle(DummyBundle('foo', enabled=False))})
    def test_add_bundle_on_request_with_subrequest(self):
        req = self.layer['request']

        # create a subrequest.
        subreq = req.clone()
        subreq['PARENT_REQUEST'] = req

        # add a bundle via the main request
        add_bundle_on_request(req, 'foo')

        scripts = ScriptsView(self.layer['portal'], subreq, None)

        # Send resource registry in development mode
        # Via a fake registry to allow accessing like this:
        # self.registry.records['plone.resources.development'].value
        scripts.registry = type(
            'reg', (object, ), {
                'records': {
                    'plone.resources.development':
                    type('val', (object, ), {'value': True})()
                }
            })()
        self.assertTrue(scripts.development)

        scripts.update()
        results = scripts.scripts()
        self.assertEqual(
            results[-1], {
                'src': 'http://nohost/plone/++resource++foo.js?version=123',
                'conditionalcomment': None,
                'bundle': 'foo'
            })

    @mock.patch.object(
        ScriptsView,
        'get_bundles',
        new=lambda self: {'foo': Bundle(DummyBundle('foo', enabled=True))})
    def test_remove_bundle_on_request_with_subrequest(self):
        req = self.layer['request']

        # create a subrequest.
        subreq = req.clone()
        subreq['PARENT_REQUEST'] = req

        # remove the enabled 'foo' bundle
        remove_bundle_on_request(req, 'foo')

        scripts = ScriptsView(self.layer['portal'], subreq, None)

        # Send resource registry in development mode
        # Via a fake registry to allow accessing like this:
        # self.registry.records['plone.resources.development'].value
        scripts.registry = type(
            'reg', (object, ), {
                'records': {
                    'plone.resources.development':
                    type('val', (object, ), {'value': True})()
                }
            })()
        self.assertTrue(scripts.development)

        scripts.update()
        results = scripts.scripts()
        self.assertEqual(filter(lambda it: 'foo' in it['src'], results), [])

    @mock.patch.object(
        ScriptsView,
        'get_resources',
        new=lambda self: {'foo': DummyResource('++resource++foo.js')})
    @mock.patch.object(
        StylesView,
        'get_resources',
        new=lambda self: {'foo': DummyResource('++resource++foo.css')})
    def test_add_resource_on_request_with_subrequest(self):
        """Check, if a resource added at a main request is picked up from a
        subrequest for creating the header scripts section.
        """
        req = self.layer['request']

        # create a subrequest.
        subreq = req.clone()
        subreq['PARENT_REQUEST'] = req

        # add a resource to main request
        add_resource_on_request(req, 'foo')

        scripts = ScriptsView(self.layer['portal'], subreq, None)
        scripts.update()
        results = scripts.scripts()
        self.assertEqual(
            results[-1], {
                'src': 'http://nohost/plone/++resource++foo.js',
                'conditionalcomment': '',
                'resetrjs': False,
                'bundle': 'none',
            })

        styles = StylesView(self.layer['portal'], subreq, None)
        styles.update()
        results = styles.styles()
        self.assertEqual(
            filter(lambda it: 'foo' in it['src'], results)[0], {
                'src': 'http://nohost/plone/++resource++foo.css',
                'conditionalcomment': '',
                'rel': 'stylesheet',
                'bundle': 'none',
            })