Context Wrapper Substitution Lookup Tests
=========================================

This package provides an adapter accessed via IContextWrapper that allow you to
define custom string substitutions for messages that are not stored within DB
without having to use 'session_data_manager' or any other session object.

Setup
-----

    >>> import re
    >>> from zope.component import getAdapter, queryAdapter, queryMultiAdapter, getGlobalSiteManager
    >>> from plone.app.testing import TEST_USER_ID
    >>> from plone.app.testing import TEST_USER_NAME
    >>> from plone.app.testing import SITE_OWNER_NAME
    >>> from plone.app.testing import login
    >>> from plone.app.testing import setRoles
    >>> from plone.stringinterp.interfaces import IStringSubstitution, IStringSubstitutionInfo
    >>> app = layer['app']
    >>> portal = layer['portal']
    >>> login(app, SITE_OWNER_NAME)
    >>> oid = portal.invokeFactory('Document', 'front-page')
    >>> apage = portal[oid]
    >>> apage.setTitle('Welcome to Plone')
    >>> apage.setDescription('Congratulations! You have successfully installed Plone.')
    >>> apage.setSubject( ('keyword one', 'keyword two') )
    >>> apage.setContributors( ('contributor one', 'contributor two') )
    >>> apage.setLanguage( 'en' )
    >>> apage.setRights( 'copyright me' )
    >>> portal.portal_workflow.doActionFor(apage, 'publish')

    >>> from DateTime import DateTime
    >>> expires = DateTime(2009, 9, 9)
    >>> apage.setExpirationDate(expires)
    >>> groups = (('groupreviewers', ()),)
    >>> users = (
    ... ('userone', 'User One', 'user@one.com',  ('Manager', 'Member'), ()),
    ... ('usertwo', 'User Two', 'user@two.com',  ('Member',), ('groupreviewers',)),
    ... ('userthree', 'User Three', 'user@three.com',  ('Owner', 'Member'), ()),
    ... ('userfour', 'User Four', 'user@four.com',  ('Member', 'Editor'), ()),
    ... ('userfive', 'User Five', 'user@five.com',  ('Member', 'Contributor'), ()),
    ... )
    >>> login(app, SITE_OWNER_NAME)
    >>> for id, roles in groups:
    ...     foo = portal.portal_groups.addGroup(id, roles=roles)
    >>> for id, fname, email, roles, groups in users:
    ...     portal.portal_membership.addMember(id, 'secret', roles, [])
    ...     member = portal.portal_membership.getMemberById(id)
    ...     member.setMemberProperties({'fullname': fname, 'email': email})
    ...     for groupname in groups:
    ...         group = portal.portal_groups.getGroupById(groupname)
    ...         group.addMember(id)
    >>> portal.portal_groups.getGroupById(
    ...     'Reviewers').addMember('groupreviewers')
    >>> login(portal, TEST_USER_NAME)
    >>> apage_localusers = (
    ... ('userfour', ('Reviewer',)),
    ... )
    >>> for id, localroles in apage_localusers:
    ...     apage.manage_setLocalRoles(id, localroles)
    >>> portal.portal_membership.getAuthenticatedMember().setProperties(email='currentuser@foobar.com')
    >>> portal.portal_membership.getAuthenticatedMember().setProperties(fullname='Current User')

Context Wrapper
---------------

    >>> from plone.stringinterp.interfaces import IContextWrapper
    >>> apage = IContextWrapper(apage)(
    ...         message=u"A cool message here",
    ...         items=['a', 'b', 'c'],
    ...         langs={'en': "English", "fr": "French"}
    ... )

Get custom messages from Wrapper

    >>> sm = getGlobalSiteManager()
    >>> from zope.interface import Interface
    >>> from plone.stringinterp.adapters import BaseSubstitution

    >>> class CustomMessageSubstitution(BaseSubstitution):
    ...     def safe_call(self):
    ...         return self.wrapper.message


    >>> sm.registerAdapter(CustomMessageSubstitution, (Interface, ), IStringSubstitution, name=u"custom_message")
    >>> getAdapter(apage, IStringSubstitution, 'custom_message')()
    'A cool message here'

    >>> class CustomItemsSubstitution(BaseSubstitution):
    ...     def safe_call(self):
    ...         return self.wrapper.items

    >>> sm.registerAdapter(CustomItemsSubstitution, (Interface, ), IStringSubstitution, name=u"custom_items")
    >>> getAdapter(apage, IStringSubstitution, 'custom_items')()
    ['a', 'b', 'c']

    >>> class CustomLangsSubstitution(BaseSubstitution):
    ...     def safe_call(self):
    ...         return self.wrapper.langs

    >>> sm.registerAdapter(CustomLangsSubstitution, (Interface, ), IStringSubstitution, name=u"custom_langs")
    >>> langs = getAdapter(apage, IStringSubstitution, 'custom_langs')()
    >>> langs['en']
    'English'

    >>> langs['fr']
    'French'


Even if you use a IContextWrapper adapter the rest of the registered substitutions will work as before.

Negative Cases
--------------

We shouldn't get an adapter where we haven't defined one::

Object with no title

    >>> queryAdapter(app, IStringSubstitution, 'Title')

Non-existent substitution

    >>> queryAdapter(portal, IStringSubstitution, 'NoTitle')



Listing Available String Substitutions
--------------------------------------

    We can get a list of all of the available substitutions, ready
    to use in a template::

    >>> subinfo = queryMultiAdapter((portal, portal.REQUEST), name=u'stringinterp_info')
    >>> subinfo
    <Products.Five...SubstitutionInfo object at ...>

    >>> subinfo.substitutionList()[0]['category']
    'All Content'


Basic Content
-------------

Ask for an adapter to get the URL::

    >>> adapter = getAdapter(apage, IStringSubstitution, 'absolute_url')

    >>> adapter
    <plone.stringinterp.adapters.UrlSubstitution object at ...>

    >>> adapter()
    'http://nohost/plone/front-page'

'id' is an alias for id::

    >>> getAdapter(apage, IStringSubstitution, 'id')()
    'front-page'


'parent_id' is an alias for id::

    >>> getAdapter(apage, IStringSubstitution, 'parent_id')()
    'plone'

'url' is an alias for absolute_url::

    >>> getAdapter(apage, IStringSubstitution, 'url')()
    'http://nohost/plone/front-page'

'parent_url' is container url::

    >>> getAdapter(apage, IStringSubstitution, 'parent_url')()
    'http://nohost/plone'


Minimal Dublin Core
-------------------

Title

    >>> getAdapter(apage, IStringSubstitution, 'title')()
    'Welcome to Plone'

Parent title

    >>> getAdapter(apage, IStringSubstitution, 'parent_title')()
    'Plone site'


The rest of Minimal Dublin::

Description

    >>> getAdapter(apage, IStringSubstitution, 'description')()
    'Congratulations! You have successfully installed Plone.'

Content type

    >>> getAdapter(apage, IStringSubstitution, 'type')()
    'Page'


Let's try some non-ASCII::

   >>> import six
   >>> if six.PY2:
   ...     apage.setTitle('Plone in Español'.decode('utf8'))
   ... else:
   ...     apage.setTitle('Plone in Español')
   >>> getAdapter(apage, IStringSubstitution, 'title')()
   'Plone in Espa\xf1ol'



Workflow Aware
--------------

Review State

    >>> getAdapter(apage, IStringSubstitution, 'review_state')()
    'published'

    >>> getAdapter(apage, IStringSubstitution, 'review_state_title')()
    'Published'


IDublinCore
-----------

Creators

    >>> getAdapter(apage, IStringSubstitution, 'creators')()
    'admin'

Contributors

    >>> getAdapter(apage, IStringSubstitution, 'contributors')()
    'contributor one, contributor two'

Subject
    >>> getAdapter(apage, IStringSubstitution, 'subject')()
    'keyword one, keyword two'

Keywords (alias for subject)

    >>> getAdapter(apage, IStringSubstitution, 'keywords')()
    'keyword one, keyword two'

Format

    >>> getAdapter(apage, IStringSubstitution, 'format')()
    'text/html'

Language

    >>> getAdapter(apage, IStringSubstitution, 'language')()
    'en'

Rights

    >>> getAdapter(apage, IStringSubstitution, 'rights')()
    'copyright me'

Identifier

    >>> getAdapter(apage, IStringSubstitution, 'identifier')()
     'http://nohost/plone/front-page'



ICatalogableDublinCore
----------------------

Everything should be in short local time format

Creation Date

    >>> result = getAdapter(apage, IStringSubstitution, 'created')()
    >>> re.match(r'... \d\d, \d\d\d\d \d\d:\d\d .M$', result) is not None
    True

Effective Date

    >>> result = getAdapter(apage, IStringSubstitution, 'effective')()
    >>> re.match(r'... \d\d, \d\d\d\d \d\d:\d\d .M$', result) is not None
    True


Expiration Date

    >>> datestring = getAdapter(apage, IStringSubstitution, 'expires')()
    >>> DateTime(datestring) == expires
    True


Modification Date

    >>> result = getAdapter(apage, IStringSubstitution, 'modified')()
    >>> re.match(r'... \d\d, \d\d\d\d \d\d:\d\d .M$', result) is not None
    True


IContentish -- emails for members having a role on context
----------------------------------------------------------

    >>> getAdapter(apage, IStringSubstitution, 'owner_emails')()
    'user@three.com'

    >>> sorted(getAdapter(apage, IStringSubstitution, 'reviewer_emails')().split(', '))
    ['user@four.com', 'user@two.com']

    >>> getAdapter(apage, IStringSubstitution, 'contributor_emails')()
    'user@five.com'

    >>> getAdapter(apage, IStringSubstitution, 'editor_emails')()
    'user@four.com'

    >>> getAdapter(apage, IStringSubstitution, 'reader_emails')()
    ''

    >>> getAdapter(apage, IStringSubstitution, 'manager_emails')()
    'user@one.com'

    >>> sorted(getAdapter(apage, IStringSubstitution, 'member_emails')(
    ...     ).split(', '))
    ['currentuser@foobar.com', 'user@five.com', 'user@four.com', 'user@one.com',
     'user@three.com', 'user@two.com']

When a user is removed from the user folder, the ownership
information, local roles, and group memberships for that user can
remain even though there's no user object available.  The email
adapters can handle this case.

    >>> login(app, SITE_OWNER_NAME)
    >>> portal.portal_membership.deleteMembers(
    ...     ('usertwo', 'userthree'), delete_localroles=0)
    ('usertwo', 'userthree')
    >>> login(portal, TEST_USER_NAME)

    >>> getAdapter(apage, IStringSubstitution, 'owner_emails')()
    ''

    >>> getAdapter(apage, IStringSubstitution, 'reviewer_emails')()
    'user@four.com'

    >>> getAdapter(apage, IStringSubstitution, 'manager_emails')()
    'user@one.com'

    >>> sorted(getAdapter(apage, IStringSubstitution, 'member_emails')(
    ...     ).split(', '))
    ['currentuser@foobar.com', 'user@five.com', 'user@four.com', 'user@one.com']

IContentish -- info on current user
-----------------------------------

    >>> getAdapter(apage, IStringSubstitution, 'user_email')()
    'currentuser@foobar.com'

    >>> getAdapter(apage, IStringSubstitution, 'user_fullname')()
    'Current User'

    >>> getAdapter(apage, IStringSubstitution, 'user_id')() == TEST_USER_ID
    True


IContentish -- info on last change, workflow or version
-------------------------------------------------------

Inspect a version change (the most recent change)

    >>> setRoles(portal, TEST_USER_ID, ['Owner',])
    >>> from Products.CMFCore.utils import getToolByName
    >>> pr = getToolByName(portal, 'portal_repository', None)
    >>> pr.save(apage.context, 'change comment')

Initial revision

    >>> getAdapter(apage, IStringSubstitution, 'change_comment')()
    'change comment'

Change title

    >>> getAdapter(apage, IStringSubstitution, 'change_title')()
    'Edit'

Change type

    >>> getAdapter(apage, IStringSubstitution, 'change_type')()
    'versioning'

Change author

    >>> getAdapter(apage, IStringSubstitution, 'change_authorid')() == TEST_USER_NAME
    True

Let's prove that the very expensive fetching of the change data
is cached. To do so, I'll retract the item and check that the
last change comment is unchanged.

    >>> setRoles(portal, TEST_USER_ID, ['Owner','Reviewer'])
    >>> wf_tool = portal.portal_workflow
    >>> wf_tool.doActionFor(apage.context, 'retract', comment='retract it!')

    >>> getAdapter(apage, IStringSubstitution, 'change_comment')()
    'change comment'

Let's create a new object in order to bypass the caching; we'll
use it to test a workflow change

    >>> portal.invokeFactory('Document', 'target')
    'target'

    >>> apage = portal['target']
    >>> apage = IContextWrapper(apage)(
    ...         message=u"A cool message here",
    ...         items=['a', 'b', 'c'],
    ...         langs={'en': "English", "fr": "French"}
    ... )
    >>> wf_tool = portal.portal_workflow
    >>> wf_tool.doActionFor(apage.context, 'publish', comment='publish it!')

Review state

    >>> getAdapter(apage, IStringSubstitution, 'review_state')()
    'published'

Comment

    >>> getAdapter(apage, IStringSubstitution, 'change_comment')()
    'publish it!'

Change title

    >>> getAdapter(apage, IStringSubstitution, 'change_title')()
    'Publish'

Change type

    >>> getAdapter(apage, IStringSubstitution, 'change_type')()
    'workflow'

Change author

    >>> getAdapter(apage, IStringSubstitution, 'change_authorid')() == TEST_USER_ID
    True

Portal infos
------------

Portal title

    >>> getAdapter(apage, IStringSubstitution, 'portal_title')()
    'Plone site'

Portal URL

    >>> getAdapter(apage, IStringSubstitution, 'portal_url')()
    'http://nohost/plone'
