def setUp(self):
     """Custom shared utility setup for tests."""
     self.portal = self.layer['portal']
     setRoles(self.portal, TEST_USER_ID, ['Manager'])
     login(self.portal, TEST_USER_NAME)
     file_path = os.path.join(os.path.dirname(__file__), "file.pdf")
     image_path = os.path.join(os.path.dirname(__file__), "image.jpg")
     self.file = api.content.create(
         type='File',
         title='file',
         container=self.portal,
         file=open(file_path))
     self.image = api.content.create(
         type='Image',
         title='image',
         container=self.portal,
         image=open(image_path))
     self.document = api.content.create(
         type='Document',
         title='A page',
         container=self.portal)
     self.file_provider = ILinkEnhancerProvider(self.file, None)
     self.img_provider = ILinkEnhancerProvider(self.image, None)
 def enhance_node_infos(self, node):
     """
     Add additional infos, if the node needs them
     """
     text = node.text
     obj = self.get_obj_from_link(node)
     if not obj:
         # the url is broken!
         return
     enhancer_provider = ILinkEnhancerProvider(obj, None)
     if not enhancer_provider:
         # there isn't an adapter registered for this content-type
         return
     link_details = enhancer_provider.get_link_details()
     if not link_details:
         return
     additional_infos = [x for x in (
         link_details.get('extension'),
         link_details.get('size')) if x]
     if additional_infos and text:
         text = " %s (%s)" % (text, ", ".join(additional_infos))
     if link_details.get('icon_url'):
         icon_tag = etree.Element("img")
         icon_tag.set("src", link_details.get('icon_url'))
         icon_tag.set("class", "attachmentLinkIcon")
         node.insert(0, icon_tag)
     if text:
         # move text after the image
         icon_tag.tail = text
         node.text = ""
     else:
         icon_tag.tail = " "
         node_children = node.getchildren()
         if node_children:
             node_children[-1].tail = " (%s)" % ", ".join(additional_infos)
     if link_details.get('url_suffix'):
         self.update_href(node, link_details)
class TestAdapter(unittest.TestCase):
    """
    Test that collective.outputfilters.enhancelinks is properly installed.
    """

    layer = COLLECTIVE_OUTPUTFILTERS_enhancelinks_FUNCTIONAL_TESTING

    def setUp(self):
        """Custom shared utility setup for tests."""
        self.portal = self.layer['portal']
        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        login(self.portal, TEST_USER_NAME)
        file_path = os.path.join(os.path.dirname(__file__), "file.pdf")
        image_path = os.path.join(os.path.dirname(__file__), "image.jpg")
        self.file = api.content.create(
            type='File',
            title='file',
            container=self.portal,
            file=open(file_path))
        self.image = api.content.create(
            type='Image',
            title='image',
            container=self.portal,
            image=open(image_path))
        self.document = api.content.create(
            type='Document',
            title='A page',
            container=self.portal)
        self.file_provider = ILinkEnhancerProvider(self.file, None)
        self.img_provider = ILinkEnhancerProvider(self.image, None)

    def test_adapter_guess_mimetype(self):
        """Test if the method find the correct mimetype"""
        # I pass False because it isn't a DX content
        mimetype = self.file_provider.guess_mimetype(
            self.file.getFile().getContentType(),
            self.file.getFile().filename)
        self.assertEqual(len(mimetype), 1)
        self.assertEqual(mimetype[0].id, 'PDF document')
        img_mimetype = self.img_provider.guess_mimetype(
            self.image.getFile().getContentType(),
            self.image.getFile().filename)
        self.assertEqual(len(img_mimetype), 1)
        self.assertEqual(img_mimetype[0].id, 'JPEG image')

    def test_adapter_format_obj_size(self):
        """Test if the method returns a correct obj size"""
        self.assertEqual(
            self.file_provider.get_formatted_size(self.file.getFile()),
            '8.4 KB')
        self.assertEqual(
            self.img_provider.get_formatted_size(self.image.getFile()),
            '5.0 KB')

    def test_adapter_extract_infos_from_mime(self):
        """ Test if the method returns the correct infos """
        mimetype = self.file_provider.guess_mimetype(
            self.file.getFile().getContentType(),
            self.file.getFile().filename)
        infos = self.file_provider.extract_infos(
            self.file.getFile(),
            mimetype)
        self.assertEqual(infos.get('icon_url'), 'http://nohost/plone/pdf.png')
        self.assertEqual(infos.get('extension'), 'pdf')
        self.assertEqual(infos.get('size'), '8.4 KB')
        self.assertEqual(infos.get('url_suffix'), '/at_download/file/file.pdf')

        mimetype = self.img_provider.guess_mimetype(
            self.image.getFile().getContentType(),
            self.image.getFile().filename)
        infos = self.img_provider.extract_infos(
            self.image.getFile(),
            mimetype)
        self.assertEqual(infos.get('icon_url'), 'http://nohost/plone/image.png')
        self.assertEqual(infos.get('extension'), 'jpg')
        self.assertEqual(infos.get('size'), '5.0 KB')
        self.assertEqual(infos.get('url_suffix'), '')

    def test_adapter_for_file(self):
        """Test if the view returns the correct infos for a file"""
        infos = self.file_provider.get_link_details()
        self.assertEqual(infos.get('icon_url'), 'http://nohost/plone/pdf.png')
        self.assertEqual(infos.get('extension'), 'pdf')
        self.assertEqual(infos.get('size'), '8.4 KB')
        self.assertEqual(infos.get('url_suffix'), '/at_download/file/file.pdf')

    def test_adapter_for_image(self):
        """Test if the view returns the correct infos for an image"""
        infos = self.img_provider.get_link_details()
        self.assertEqual(infos.get('icon_url'), 'http://nohost/plone/image.png')
        self.assertEqual(infos.get('extension'), 'jpg')
        self.assertEqual(infos.get('size'), '5.0 KB')
        self.assertEqual(infos.get('url_suffix'), '')