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

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

Let's get the portal object

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

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

We test our SGML based parser first:

    >>> text = """<html>
    ...             <head></head>
    ...             <body>
    ...                 <img src="resolveuid/943f469a781852145c7cddb3d99a5241/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>"""
    >>> from Products.TinyMCE.transforms.parser import TinyMCEOutput
    >>> parser = TinyMCEOutput(context=self.portal)
    >>> parser.feed(text)
    >>> print parser.getResult()
    <html...>  
    >>> parser.close()

Test if it preservers 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.
This is not a full functional test as we don't add a document and image / link with captioned images or UID's yet, it should leave the UID intact

    >>> text = """<html>
    ...             <head></head>
    ...             <body>
    ...                 <img src="resolveuid/943f469a781852145c7cddb3d99a5241/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>"""
    >>> transformed_text = transform_utility.convert(name="html_to_tinymce_output_html", orig=text, context=self.portal)
    >>> print transformed_text
    <html...>



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)

