===============
Person metadata
===============

Person data (name, address, ...) has some metadata which gets
automatically set resp. changed.

Set up
======

Log in as an editor:

>>> browser = get_browser('editor')
>>> browser.open('http://localhost/ab')


Creation date
=============

The creation date is set on creation:

>>> from datetime import datetime
>>> from icemac.addressbook.testing import get_messages
>>> from zope.dublincore.interfaces import IZopeDublinCore
>>> browser.getLink('person').click()
>>> browser.getControl('last name').value = 'Tester'
>>> browser.getControl('Add').click()
>>> get_messages(browser)
['"Tester" added.']
>>> browser.url
'http://localhost/ab/@@person-list.html'
>>> browser.getLink('Tester').click()
>>> browser.url
'http://localhost/ab/Person'
>>> address_book = layer['addressbook']
>>> isinstance(IZopeDublinCore(address_book['Person']).created, datetime)
True


Creator and modifier
====================

Set at creation
---------------

The title of the principal who created the object is stored:

>>> from icemac.addressbook.metadata.interfaces import IEditor
>>> IEditor(address_book['Person']).creator
u'global editor'

This principal is also stored as last modifier:

>>> IEditor(address_book['Person']).modifier
u'global editor'

Set at modification
-------------------

When another principal changes the object his title is recorded as
modifier but the creator keeps unchanged.

Set up
~~~~~~

At first we have to create a new principal and log in:

>>> from icemac.addressbook.testing import create_user, get_messages
>>> create_user(address_book, address_book, u'Hans', u'Tester', u'hans@test.de',
...             u'12345678', ('Editor',))
>>> editor = get_browser()
>>> editor.open('http://localhost/ab/Person')
>>> editor.getControl('User Name').value = 'hans@test.de'
>>> editor.getControl('Password').value = '12345678'
>>> editor.getControl('Log in').click()
>>> get_messages(editor)
['You have been logged-in successfully.']
>>> editor.url
'http://localhost/ab/Person'

Modifier
~~~~~~~~

Modifing the object records the principal's title as modifier does not
change the creator value:

>>> editor.getControl('notes').value = 'I was here.'
>>> editor.getControl('Apply').click()
>>> get_messages(editor)
['Data successfully updated.']
>>> IEditor(address_book['Person']).creator
u'global editor'
>>> IEditor(address_book['Person']).modifier
u'Tester, Hans'


Modification Date
=================

Correct date shown
------------------

In the user interface the modification date for each entry is
displayed separately. To show this let's change the modification dates
to known values:

>>> import pytz
>>> person = address_book['Person']
>>> IZopeDublinCore(person).modified = datetime(2001, 1, 1, tzinfo=pytz.utc)
>>> IZopeDublinCore(person['PostalAddress']).modified = (
...     datetime(2002, 2, 2, tzinfo=pytz.utc))
>>> IZopeDublinCore(person['PhoneNumber']).modified = (
...     datetime(2003, 3, 3, tzinfo=pytz.utc))
>>> IZopeDublinCore(person['EMailAddress']).modified = (
...     datetime(2004, 4, 4, tzinfo=pytz.utc))
>>> IZopeDublinCore(person['HomePageAddress']).modified = (
...     datetime(2005, 5, 5, tzinfo=pytz.utc))
>>> browser.reload() # does transaction.commit()

The modification date of each entry is displayed:

>>> entry_xpath = "//form/div/div/fieldset[%s]/fieldset/div[4]/div/span"
>>> [x.text for x in browser.etree.xpath(entry_xpath % 1)]
['01/01/01 00:00']
>>> [x.text for x in browser.etree.xpath(entry_xpath % 3)]
['02/02/02 00:00']
>>> [x.text for x in browser.etree.xpath(entry_xpath % 4)]
['03/03/03 00:00']
>>> [x.text for x in browser.etree.xpath(entry_xpath % 5)]
['04/04/04 00:00']
>>> [x.text for x in browser.etree.xpath(entry_xpath % 6)]
['05/05/05 00:00']


Editing changes
---------------

Editing one of the entries only changes the modification date for this
one entry. To ease the comparisions in the test we set all
modification dates to ``None``:

>>> default_moddate = datetime(2000, 1, 1, tzinfo=pytz.utc)
>>> IZopeDublinCore(address_book['Person']).modified = default_moddate
>>> for entry in address_book['Person'].values():
...     IZopeDublinCore(entry).modified = default_moddate
>>> browser.reload()

>>> browser.getControl('city').value = 'Heretown'
>>> browser.getControl('Apply').click()
>>> get_messages(browser)
['Data successfully updated.']
>>> browser.url
'http://localhost/ab/@@person-list.html'
>>> person = address_book['Person']
>>> print IZopeDublinCore(person).modified == default_moddate
True
>>> IZopeDublinCore(person['PhoneNumber']).modified == default_moddate
True
>>> IZopeDublinCore(person['HomePageAddress']).modified == default_moddate
True
>>> IZopeDublinCore(person['EMailAddress']).modified == default_moddate
True
>>> IZopeDublinCore(person['PostalAddress']).modified == default_moddate
False

When a field of the person is changed the modification dates of the
entries do not change:

>>> IZopeDublinCore(person['PostalAddress']).modified = default_moddate
>>> browser.getLink('Tester').click()
>>> browser.getControl('first name').value = 'Hans'
>>> browser.getControl('Apply').click()
>>> get_messages(browser)
['Data successfully updated.']
>>> browser.url
'http://localhost/ab/@@person-list.html'
>>> person = address_book['Person']
>>> print IZopeDublinCore(person).modified == default_moddate
False
>>> IZopeDublinCore(person['PhoneNumber']).modified == default_moddate
True
>>> IZopeDublinCore(person['HomePageAddress']).modified == default_moddate
True
>>> IZopeDublinCore(person['EMailAddress']).modified == default_moddate
True
>>> IZopeDublinCore(person['PostalAddress']).modified == default_moddate
True
