Issue Description
=================

The Description field of an Issue is always unicode now:

    >>> issue = self.portal.foo.foo
    >>> issue.Description()
    u''
    >>> type(issue.Description())
    <type 'unicode'>

Since we are testing this, we will also demonstrate that the
Description method returns the description field when it has been set
and the details field otherwise::

    >>> issue.update(details='details field')
    >>> issue.Description()
    u'details field'
    >>> issue.update(description='description field')
    >>> issue.Description()
    u'description field'

But anyway, Description always is of type unicode.  This was not
always the case.  And that could give problems while reindexing an
issue.  We cheat to test that by changing the Description method into
an attribute::

    >>> issue.Description = 'plain old string'
    >>> issue.Description
    'plain old string'
    >>> issue.reindexObject()

At this point it is still fine, but this changes when the Description
is non-ascii.  If on your system sys.getdefaultencoding() returns
something else than 'ascii' you may not actually run into problems.
But we cannot assume that.  At first all looks fine, a reindex gives
no complaints::

    >>> issue.Description = 'Café René'
    >>> issue.reindexObject()

But now we undo our cheating by removing the Description attribute
from the issue::

    >>> del issue.__dict__['Description']
    >>> issue.Description()
    u'description field'

Now we come to the meat of the matter.  A reindex of this object now
fails, as during the reindexing (actually in the updateMetadata
method) the non-ascii Description string that is in the catalog gets
turned into unicode to compare it with the unicode Description of the
issue and this fails.  Actually, on Plone 4 it goes fine.  But we are
not really interested in a test that checks that old, removed code
used to have a failure, so we remove one test here.  *IF* it goes
wrong, the way to fix this is by first unindexing the object.  Then a
reindex works fine again::

    >>> issue.unindexObject()
    >>> issue.reindexObject()

The workaround is to clear and rebuild the portal_catalog.  But there
is also migration for this so it can be done automatically for all
issues.  Let's hack some wrong Descriptions in the existing issues::

    >>> catalog = self.portal.portal_catalog
    >>> issues = [x.getObject() for x in catalog(portal_type='PoiIssue')]
    >>> for issue in issues:
    ...     issue.unindexObject()
    ...     issue.Description = 'Café René'
    ...     issue.reindexObject()
    ...     del issue.__dict__['Description']

Reindexing all the issues might fail but if it does we are not
interested anymore.

The migration code is usually called with portal_setup as context, but
let's call use a different context to show that also works::

    >>> from Products.Poi.migration import fix_descriptions
    >>> fix_descriptions(self.portal.foo)

Now everything is fine again:

    >>> for issue in issues:
    ...     issue.reindexObject()
