Base Substitution Lookup Tests
==============================

Test and demonstrate looking up string substitutions
via named adapters.

Setup
-----

    >>> import re
    >>> from zope.component import getAdapter, queryAdapter, queryMultiAdapter
    >>> 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']
    >>> setRoles(portal, TEST_USER_ID, ['Manager'])
    >>> oid = portal.invokeFactory('Folder', 'sandbox')
    >>> sandbox = portal[oid]
    >>> sandbox.setTitle('Sandbox')

    >>> oid = sandbox.invokeFactory('Document', 'a-page')
    >>> apage = sandbox[oid]
    >>> apage.setTitle('A page')
    >>> apage.setDescription('This is a short summary')

    >>> apage.setSubject( ('keyword one', 'keyword two') )
    >>> apage.setContributors( ('contributor one', 'contributor two') )
    >>> apage.setLanguage( 'en' )
    >>> apage.setRights( 'copyright me' )

    >>> 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')
    >>> portal.portal_groups.getGroupById(
    ...     'groupreviewers').addMember('Reviewers')  # add a group cycle to test we don't have a "RuntimeError: maximum recursion depth exceeded"
    >>> login(portal, TEST_USER_NAME)
    >>> sandbox_localusers = (
    ... ('userfive', ('Contributor',)),
    ... )

    >>> for id, localroles in sandbox_localusers:
    ...     sandbox.manage_setLocalRoles(id, localroles)

    >>> 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')


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/sandbox/a-page'

'id' is an alias for id::

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


'parent_id' is an alias for id::

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

'url' is an alias for absolute_url::

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

'parent_url' is container url::

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


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

Title

    >>> getAdapter(apage, IStringSubstitution, 'title')()
    'A page'

Parent title

    >>> getAdapter(apage, IStringSubstitution, 'parent_title')()
    'Sandbox'


The rest of Minimal Dublin::

Description

    >>> getAdapter(apage, IStringSubstitution, 'description')()
    'This is a short summary'

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')()
    'private'

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


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

Creator

    >>> getAdapter(apage, IStringSubstitution, 'creator')()
    'test_user_1_'

    >>> apage.setCreators( ('usertwo', 'userfive') )
    >>> getAdapter(apage, IStringSubstitution, 'creator')()
    'usertwo'

Creator Full Name

    >>> getAdapter(apage, IStringSubstitution, 'creator_fullname')()
    'User Two'

Creator Email

    >>> getAdapter(apage, IStringSubstitution, 'creator_email')()
    'user@two.com'

Creators

    >>> getAdapter(apage, IStringSubstitution, 'creators')()
    'usertwo, userfive'

Creators Emails

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

Contributors

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

Contributors Emails

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

    >>> apage.setContributors( ('userthree', 'userfour') )
    >>> getAdapter(apage, IStringSubstitution, 'contributors_emails')()
    'user@three.com, user@four.com'

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/sandbox/a-page'



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

Everything should be in short local time format

Creation Date

    >>> result = getAdapter(apage, IStringSubstitution, 'created')()

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')()
    ''

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

    >>> sorted(getAdapter(apage, IStringSubstitution, 'member_emails')(
    ...     ).split(', '))
    ['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')()
    'currentuser@foobar.com'

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

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

    >>> sorted(getAdapter(apage, IStringSubstitution, 'member_emails')(
    ...     ).split(', '))
    ['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, '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, 'publish', comment='publish 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']
    >>> wf_tool = portal.portal_workflow
    >>> wf_tool.doActionFor(apage, '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'
