Test for mail template
======================

Checking MockMailHost

    >>> self.portal.MailHost
    <MockMailHost at ...>

Test starting conversations, replying and modifying comments in a default
member-posting forum.

First, some set-up:

    >>> from Products.Five import zcml
    >>> import Products
    >>> zcml.load_config('configure.zcml', package=Products.PloneboardSubscription)
    >>> import Products.PloneboardSubscription
    >>> zcml.load_config('configure.zcml', package=Products.PloneboardSubscription.browser)

    >>> from Products.Ploneboard.tests import utils
    >>> utils.setUpDefaultMembersBoardAndForum(self)

    >>> new_mail_template = """Dear John:
    ... This is a notification of [PORTAL_TITLE].
    ...
    ... [URLS]
    ...
    ... Your url is in the mail.
    ... Thanks.
    ...
    ... Yours sincerely,
    ... Webmaster""".split('\n')

    >>> tool = self.portal.portal_pbnotification
    >>> tool.manage_changeProperties(mail_template=new_mail_template)
    >>> new_mail_template == list(tool.getProperty('mail_template'))
    True

    >>> browser = self.browser
    >>> utils.logoutThenLoginAs(self, browser, 'member1')

View forum
----------

The forum created behind the scenes should now be shown here.

    >>> browser.open(self.board.absolute_url())
    >>> browser.contents
    '...Forum 1...'

If we go to the forum, there are no conversations shown.

    >>> browser.getLink('Forum 1').click()
    >>> browser.contents
    '...No conversations in this forum yet...'

Look for Subscribe link
-----------------------

    >>> browser.getLink('Subscribe').click()

There is now one forum as key
    >>> tool.subscribers.has_key(tool.getObjId(self.forum))
    True

There is one member as value
    >>> from sets import Set
    >>> len(tool.subscribers[tool.getObjId(self.forum)])
    1
    >>> Set(tool.subscribers[tool.getObjId(self.forum)])
    Set(['member1'])

Add a new conversation as member2
----------------------------------

Now we can add a new conversation. We set a title and some body text. The body
text can contain HTML as well.

    >>> utils.logoutThenLoginAs(self, browser, 'member2')
    >>> browser.open(self.board.absolute_url())
    >>> browser.getLink('Forum 1').click()
    >>> browser.getControl('Start a new Conversation').click()
    >>> browser.url
    '.../add_conversation_form...'
    >>> browser.getControl('Title').value = 'Conversation1'
    >>> browser.getControl('Body text').value = 'Some <b>body</b> text'

Submit the form, and we should be returned to the forum view. The conversation
should exist, and we should be able to view it.

    >>> browser.getControl(name='form.button.Post').click()
    >>> browser.url.startswith(self.forum.absolute_url())
    True
    >>> conversation = self.forum.getConversations()[0]

    >>> import re
    >>> browser.getLink(url=re.compile('\/%s$' % conversation.getId())).click()

This should have triggered a notification. Check the MockMailHost
------------------------------------------------------------------

    >>> len(self.portal.MailHost.messages)
        1

    >>> self.portal.MailHost.messages[0]
    'From: <>\nTo: Member one <member1@example.com>...'

Testing if conversation url is in the mail and formatted as text
    >>> '\n%s' % conversation.absolute_url() in self.portal.MailHost.messages[0]
    True

Testing if [PORTAL_TITLE] is well replaced
    >>> 'This is a notification of Plone site.' in self.portal.MailHost.messages[0]
    True

Checking static parts of the mail
    >>> s1 = [x for x in new_mail_template if x not in ['', '[URLS]']]
    >>> s1 = [x for x in s1 if '[PORTAL_TITLE]' not in x]
    >>> s2 = [x for x in s1 if x in self.portal.MailHost.messages[0]]
    >>> s1 == s2
    True

    >>> self.portal.MailHost.reset()

Test html format
================

    >>> tool.html_format = True
    >>> tool.html_format
    True

    >>> new_mail_template = """Dear John:<br />
    ... This is a notification of [PORTAL_TITLE].<br /><br />
    ... 
    ... [URLS]
    ... 
    ... <br /><br />Your url is in the mail.<br />
    ... Thanks.<br /><br />
    ... 
    ... Yours sincerely,<br />
    ... Webmaster<br />""".split('\n')

    >>> tool.manage_changeProperties(mail_template=new_mail_template)
    >>> new_mail_template == list(tool.getProperty('mail_template'))
    True

Add a reply as member2
----------------------------------

    >>> utils.logoutThenLoginAs(self, browser, 'member2')
    >>> browser.open(self.board.absolute_url())
    >>> browser.getLink('Forum 1').click()
    >>> browser.getLink(url=re.compile('\/%s$' % conversation.getId())).click()
    >>> browser.getControl(name='text').value = 'A quick reply to test html format'
    >>> browser.getControl(name='form.button.Post').click()
    >>> browser.url.startswith(conversation.absolute_url())
    True
    >>> browser.contents
    '...A quick reply to test html format...'

This should have triggered a notification. Check the MockMailHost
------------------------------------------------------------------

    >>> len(self.portal.MailHost.messages)
        1

    >>> self.portal.MailHost.messages[0]
    'From: <>\nTo: Member one <member1@example.com>...'

Testing if conversation url is in the mail and formatted as html
    >>> '<h3><a href="%s' % conversation.absolute_url() in self.portal.MailHost.messages[0]
    True

Testing if [PORTAL_TITLE] is well replaced
    >>> 'This is a notification of Plone site.' in self.portal.MailHost.messages[0]
    True

Checking static parts of the mail
    >>> s1 = [x for x in new_mail_template if x not in ['', '[URLS]']]
    >>> s1 = [x for x in s1 if '[PORTAL_TITLE]' not in x]
    >>> s2 = [x for x in s1 if x in self.portal.MailHost.messages[0]]
    >>> s1 == s2
    True

    >>> self.portal.MailHost.reset()


Test [FORUMS]
=============

    >>> tool.html_format = True
    >>> tool.html_format
    True

    >>> new_mail_template = """Dear John:<br />
    ... This is a notification of [PORTAL_TITLE].<br /><br />
    ... 
    ... [FORUMS]
    ... 
    ... <br /><br />Your url is in the mail.<br />
    ... Thanks.<br /><br />
    ... 
    ... Yours sincerely,<br />
    ... Webmaster<br />""".split('\n')

    >>> tool.manage_changeProperties(mail_template=new_mail_template)
    >>> new_mail_template == list(tool.getProperty('mail_template'))
    True

Add a reply as member2
----------------------

    >>> utils.logoutThenLoginAs(self, browser, 'member2')
    >>> browser.open(self.board.absolute_url())
    >>> browser.getLink('Forum 1').click()
    >>> browser.getLink(url=re.compile('\/%s$' % conversation.getId())).click()
    >>> browser.getControl(name='text').value = 'A quick reply to test FORUMS format'
    >>> browser.getControl(name='form.button.Post').click()
    >>> browser.url.startswith(conversation.absolute_url())
    True
    >>> browser.contents
    '...A quick reply to test FORUMS format...'

This should have triggered a notification. Check the MockMailHost
------------------------------------------------------------------

    >>> len(self.portal.MailHost.messages)
        1

    >>> self.portal.MailHost.messages[0]
    'From: <>\nTo: Member one <member1@example.com>...'

Testing if forum is included
    >>> ">Forum 1: %s<"%conversation.Title() in self.portal.MailHost.messages[0]
    True

Testing if conversation url is in the mail and well formatted
    >>> '<h3><a href="%s' % conversation.absolute_url() in self.portal.MailHost.messages[0]
    True

Testing if [PORTAL_TITLE] is well replaced
    >>> 'This is a notification of Plone site.' in self.portal.MailHost.messages[0]
    True

Checking static parts of the mail
    >>> s1 = [x for x in new_mail_template if x not in ['', '[FORUMS]']]
    >>> s1 = [x for x in s1 if '[PORTAL_TITLE]' not in x]
    >>> s2 = [x for x in s1 if x in self.portal.MailHost.messages[0]]
    >>> s1 == s2
    True

    >>> self.portal.MailHost.reset()


Test [COMMENTS]
===============

    >>> tool.html_format = True
    >>> tool.html_format
    True

    >>> new_mail_template = """Dear John:<br />
    ... This is a notification of [PORTAL_TITLE].<br /><br />
    ... 
    ... [COMMENTS]
    ... 
    ... <br /><br />Your url is in the mail.<br />
    ... Thanks.<br /><br />
    ... 
    ... Yours sincerely,<br />
    ... Webmaster<br />""".split('\n')

    >>> tool.manage_changeProperties(mail_template=new_mail_template)
    >>> new_mail_template == list(tool.getProperty('mail_template'))
    True

Add a reply as member2
----------------------

    >>> utils.logoutThenLoginAs(self, browser, 'member2')
    >>> browser.open(self.board.absolute_url())
    >>> browser.getLink('Forum 1').click()
    >>> browser.getLink(url=re.compile('\/%s$' % conversation.getId())).click()
    >>> browser.getControl(name='text').value = 'A quick reply to test COMMENTS format'
    >>> browser.getControl(name='form.button.Post').click()
    >>> browser.url.startswith(conversation.absolute_url())
    True
    >>> browser.contents
    '...A quick reply to test COMMENTS format...'

This should have triggered a notification. Check the MockMailHost
------------------------------------------------------------------

    >>> len(self.portal.MailHost.messages)
        1

    >>> self.portal.MailHost.messages[0]
    'From: <>\nTo: Member one <member1@example.com>...'

Testing if forum is included
    >>> ">Forum 1: %s<"%conversation.Title() in self.portal.MailHost.messages[0]
    True

Testing if conversation url is in the mail and well formatted
    >>> '<h3><a href="%s' % conversation.absolute_url() in self.portal.MailHost.messages[0]
    True

Testing if the added comment is in the mail
    >>> 'A quick reply to test COMMENTS format' in self.portal.MailHost.messages[0]
    True

Checking static parts of the mail
    >>> s1 = [x for x in new_mail_template if x not in ['', '[COMMENTS]']]
    >>> s1 = [x for x in s1 if '[PORTAL_TITLE]' not in x]
    >>> s2 = [x for x in s1 if x in self.portal.MailHost.messages[0]]
    >>> s1 == s2
    True

    >>> self.portal.MailHost.reset()


Testing one conversation mail for multiple users
================================================

Subscribing other users
    >>> utils.logoutThenLoginAs(self, browser, 'manager1')
    >>> browser.open(self.board.absolute_url())
    >>> browser.getLink('Forum 1').click()
    >>> browser.getLink(url=re.compile('\/%s$' % conversation.getId())).click()
    >>> browser.getLink('Subscribe').click()

    >>> utils.logoutThenLoginAs(self, browser, 'reviewer1')
    >>> browser.open(self.board.absolute_url())
    >>> browser.getLink('Forum 1').click()
    >>> browser.getLink(url=re.compile('\/%s$' % conversation.getId())).click()
    >>> browser.getLink('Subscribe').click()

browser.getLink('Subscribe').click()

There are still one forum as key and now one conv
    >>> tool.subscribers.has_key(tool.getObjId(self.forum))
    True
    >>> tool.subscribers.has_key(tool.getObjId(conversation))
    True

There is one subscriber for the forum and 2 for the conversation
    >>> Set(tool.subscribers[tool.getObjId(self.forum)])
    Set(['member1'])
    >>> Set(tool.subscribers[tool.getObjId(conversation)])
    Set(['reviewer1', 'manager1'])

Setting template
----------------

    >>> tool.html_format
    True

    >>> new_mail_template = """Dear John:<br />
    ... This is a notification of [PORTAL_TITLE].<br /><br />
    ... 
    ... [COMMENTS]
    ... 
    ... <br /><br />Your url is in the mail.<br />
    ... Thanks.<br /><br />
    ... 
    ... Yours sincerely,<br />
    ... Webmaster<br />""".split('\n')

    >>> tool.manage_changeProperties(mail_template=new_mail_template)
    >>> new_mail_template == list(tool.getProperty('mail_template'))
    True

Add a reply as member2
----------------------

    >>> utils.logoutThenLoginAs(self, browser, 'member2')
    >>> browser.open(self.board.absolute_url())
    >>> browser.getLink('Forum 1').click()
    >>> browser.getLink(url=re.compile('\/%s$' % conversation.getId())).click()
    >>> browser.getControl(name='text').value = 'A quick reply to test multiple users'
    >>> browser.getControl(name='form.button.Post').click()
    >>> browser.url.startswith(conversation.absolute_url())
    True
    >>> browser.contents
    '...A quick reply to test multiple users...'

This should have triggered a notification. Check the MockMailHost
------------------------------------------------------------------

    >>> len(self.portal.MailHost.messages)
    3

Testing if forum is included
    >>> ">Forum 1: %s<"%conversation.Title() in self.portal.MailHost.messages[0]
    True

Testing if conversation url is in the mail and well formatted
    >>> '<h3><a href="%s' % conversation.absolute_url() in self.portal.MailHost.messages[0]
    True

Testing if the added comment is in the mail
    >>> 'A quick reply to test multiple users' in self.portal.MailHost.messages[0]
    True
    >>> self.portal.MailHost.messages[0].count('A quick reply to test multiple users')
    1

Checking static parts of the mail
    >>> s1 = [x for x in new_mail_template if x not in ['', '[COMMENTS]']]
    >>> s1 = [x for x in s1 if '[PORTAL_TITLE]' not in x]
    >>> s2 = [x for x in s1 if x in self.portal.MailHost.messages[0]]
    >>> s1 == s2
    True

    >>> self.portal.MailHost.reset()

Testing multiple comments mail for multiple users
=================================================

Some settings
    >>> tool.send_interval = 300
    >>> tool.auto_subscribe = True

Add 2 replies as member2
------------------------

    >>> utils.logoutThenLoginAs(self, browser, 'member2')
    >>> browser.open(self.board.absolute_url())
    >>> browser.getLink('Forum 1').click()
    >>> browser.getLink(url=re.compile('\/%s$' % conversation.getId())).click()
    >>> browser.getControl(name='text').value = 'A quick reply on conv1, part1'
    >>> browser.getControl(name='form.button.Post').click()
    >>> browser.url.startswith(conversation.absolute_url())
    True
    >>> browser.contents
    '...A quick reply on conv1, part1...'
    >>> browser.getControl(name='text').value = 'A quick reply on conv1, part2'
    >>> browser.getControl(name='form.button.Post').click()
    >>> browser.url.startswith(conversation.absolute_url())
    True
    >>> browser.contents
    '...A quick reply on conv1, part2...'

This shouldn't have triggered a notification because the send interval.
-----------------------------------------------------------------------

    >>> len(self.portal.MailHost.messages)
    0

Add a new conversation as member1
----------------------------------

    >>> utils.logoutThenLoginAs(self, browser, 'member1')
    >>> browser.open(self.board.absolute_url())
    >>> browser.getLink('Forum 1').click()
    >>> browser.getControl('Start a new Conversation').click()
    >>> browser.url
    '.../add_conversation_form...'
    >>> browser.getControl('Title').value = 'Conversation2'
    >>> browser.getControl('Body text').value = 'Some <b>body</b> text'
    >>> browser.getControl(name='form.button.Post').click()
    >>> browser.url.startswith(self.forum.absolute_url())
    True
    >>> conversation2 = tool.portal_catalog(portal_type='PloneboardConversation', Title='Conversation2')[0].getObject()
    >>> conversation2.Title()
    'Conversation2'

    >>> import re
    >>> browser.getLink(url=re.compile('\/%s$' % conversation2.getId())).click()

Add a reply as member2
----------------------

    >>> utils.logoutThenLoginAs(self, browser, 'member2')
    >>> browser.open(self.board.absolute_url())
    >>> browser.getLink('Forum 1').click()
    >>> browser.getLink(url=re.compile('\/%s$' % conversation2.getId())).click()
    >>> browser.getControl(name='text').value = 'A quick reply on conv2, part1'
    >>> browser.getControl(name='form.button.Post').click()
    >>> browser.url.startswith(conversation2.absolute_url())
    True
    >>> browser.contents
    '...A quick reply on conv2, part1...'
    >>> browser.getControl(name='text').value = 'A quick reply on conv2, part2'
    >>> browser.getControl(name='form.button.Post').click()
    >>> browser.url.startswith(conversation2.absolute_url())
    True
    >>> browser.contents
    '...A quick reply on conv2, part2...'

Some checks before testing results
----------------------------------

    >>> len(self.portal.MailHost.messages)
    0

There are still one forum as key and now two conv
    >>> len(tool.subscribers.keys())
    3
    >>> tool.subscribers.has_key(tool.getObjId(self.forum))
    True
    >>> tool.subscribers.has_key(tool.getObjId(conversation))
    True
    >>> tool.subscribers.has_key(tool.getObjId(conversation2))
    True

Check subscribers
    >>> Set(tool.subscribers[tool.getObjId(self.forum)])
    Set(['member1'])
    >>> Set(tool.subscribers[tool.getObjId(conversation)])
    Set(['reviewer1', 'manager1', 'member2'])
    >>> Set(tool.subscribers[tool.getObjId(conversation2)])
    Set(['member1', 'member2'])

Force the tool
--------------

Number of pending comments: 2 conv1 replies, one new conv, 2 conv2 replies
    >>> len(tool.pending)
    5
    >>> tool.process_pending()

This should have triggered a notification. Check the MockMailHost
------------------------------------------------------------------

# Each user must receive a mail
# conversation (2 replies): member1, manager1, reviewer1
# conversation2 (creation): nobody at the creation but member2 at process_pending time !
# conversation2 (2 replies): member1
    >>> len(self.portal.MailHost.messages)
    4
