======================
An integration doctest
======================

Test the transforms that are included with the Products.TinyMCE package 

Let's get the portal object

    >>> self.portal
    <PloneSite at ...>

And login
   >>> self.loginAsPortalOwner()
   
Ok, that works, let's get the transformation tool

    >>> from zope.component import getUtility
    >>> from Products.PortalTransforms.interfaces import IPortalTransformsTool
    >>> transform_utility = getUtility(IPortalTransformsTool)
    
Test if TinyMCE is there

    >>> from Products.TinyMCE.interfaces.utility import ITinyMCE
    >>> tinymce_utility = getUtility(ITinyMCE)

Register text/x-tinymce-output-html mimetype and transformations for captioned images and UID's

    >>> from Products.TinyMCE.setuphandlers import install_mimetype_and_transforms
    >>> install_mimetype_and_transforms(self.portal)

Check if the transform policy is installed

    >>> policies = [mimetype for (mimetype, required) in transform_utility.listPolicies() if mimetype == "text/x-html-safe"]
    >>> print len(policies)
    1
    
Ok, we add a image to our instance and collect the UID
    >>> from Products.CMFPlone.tests import dummy
    >>> self.portal.invokeFactory('Image', id='image', title="Image", file=dummy.Image())
    'image'
    >>> image = getattr(self.portal, 'image')
    >>> image.reindexObject()
    >>> UID = image.UID()
    
We test our SGML based parser first:

    >>> text = """<html>
    ...             <head></head>
    ...             <body>
    ...                 <img src="resolveuid/%s/image_thumb" class="image-left captioned" width="200" alt="My alt text" />
    ...                 <p><img src="/plone/image.jpg" class="image-right captioned" width="200" style="border-width:1px" /></p>
    ...              </body>
    ...            </html>""" % UID
    >>> from Products.TinyMCE.transforms.parser import TinyMCEOutput
    >>> parser = TinyMCEOutput(context=self.portal)
    >>> parser.feed(text)
    >>> print parser.getResult()
    <html...>  
    >>> parser.close()

Test if it preserves newlines which should not be filtered out 
    >>> text = """<pre>This is line 1
    ...                This is line 2</pre>"""
    >>> from Products.TinyMCE.transforms.parser import TinyMCEOutput
    >>> parser = TinyMCEOutput(context=self.portal)
    >>> parser.feed(text)
    >>> print parser.getResult().find('\n') != -1
    True

Let's get the html-to-tinymce-output-html transform and run it.

    >>> text = """<html>
    ...             <head></head>
    ...             <body>
    ...                 <img src="resolveuid/%s/image_thumb" class="image-left captioned" width="200" alt="My alt text" />
    ...                 <p><img src="/plone/image.jpg" class="image-right captioned" width="200" style="border-width:1px" /></p>
    ...                 <pre>This is line 1
    ...                       This is line 2</pre>
    ...              </body>
    ...            </html>""" % UID
    >>> transformed_text = transform_utility.convert(name="html_to_tinymce_output_html", orig=text, context=self.portal)

The UID reference should be converted to an absolute url : 
    
    >>> str(transformed_text).find('<img src="http://nohost/plone/image/image_thumb" alt="My alt text" class="image-left captioned" width="200" />') != -1
    True
    
If that works, let's uninstall the whole bunch again

    >>> from Products.TinyMCE.setuphandlers import uninstall_mimetype_and_transforms
    >>> uninstall_mimetype_and_transforms(self.portal)
    
Check if the transform policy is uninstalled after this

    >>> policies = [mimetype for (mimetype, required) in transform_utility.listPolicies() if mimetype == "text/x-html-safe"]
    >>> print len(policies)
    0

Let's uninstall it again, it should check if the transforms, mimetype and policy exist

    >>> from Products.TinyMCE.setuphandlers import uninstall_mimetype_and_transforms
    >>> uninstall_mimetype_and_transforms(self.portal)

