ATFile/ATImage Replacement Types
================================

This test tries to make sure the new, blob-based replacement types for
`ATFile` and `ATImage` can be properly created and edited.


Files
-----

Let's start with creating a "File" content item:

  >>> from StringIO import StringIO
  >>> from plone.testing.z2 import Browser
  >>> browser = Browser(layer['app'])
  >>> from plone.app.testing import TEST_USER_NAME, TEST_USER_PASSWORD
  >>> browser.addHeader('Authorization',
  ...                   'Basic %s:%s' % (TEST_USER_NAME, TEST_USER_PASSWORD))

  >>> from plone.app.testing import TEST_USER_ID
  >>> folder = layer['portal'].portal_membership.getHomeFolder(TEST_USER_ID)
  >>> browser.open(folder.absolute_url())

  >>> browser.getLink(url='createObject?type_name=File').click()
  >>> browser.url
  'http://nohost/plone/.../portal_factory/File/file.../edit...'
  >>> browser.getControl(name='title').value = 'Foo'
  >>> control = browser.getControl(name='file_file')
  >>> control.filename = 'foo.pdf'
  >>> control.value = StringIO('%PDF-1.4 fake pdf...' + 'foo' * 1000)
  >>> browser.getControl('Save').click()

  >>> browser.url
  'http://nohost/plone/.../foo.../view'
  >>> browser.contents
  '...Info...Changes saved...
   ...Foo...foo.pdf...PDF document...'

Now let's make sure we can also edit it:

  >>> browser.getLink('Edit').click()
  >>> browser.getControl(name='title').value = 'Foobar'
  >>> browser.getControl('Replace with new file').selected = True
  >>> control = browser.getControl(name='file_file')
  >>> control.filename = 'foobar.pdf'
  >>> control.value = StringIO('%PDF-1.4 fake pdf...' + 'foobar' * 1000)
  >>> browser.getControl('Save').click()

  >>> browser.url
  'http://nohost/plone/.../foo.../view'
  >>> browser.contents
  '...Info...Changes saved...
   ...Foobar...foobar.pdf...PDF document...'

It should also be possible to remove the file again.  For this to work the
field must not be required.  That is not the default in Plone, however, so
we need to tweak the schema first:

  >>> folder.foo.getPrimaryField().required = False

  >>> browser.getLink('Edit').click()
  >>> browser.getControl('Delete current').selected = True
  >>> browser.getControl('Save').click()
  >>> 'Changes saved' in browser.contents
  True
  >>> 'PDF document' in browser.contents
  False

The file content should now be gone:

  >>> folder.foo.getFile().data
  ''


Images
------

Next a similar test is conducted for an "Image" content item:

  >>> from plone.app.blob.tests.utils import getFile
  >>> browser.open(folder.absolute_url())
  >>> browser.getLink(url='createObject?type_name=Image').click()
  >>> browser.url
  'http://nohost/plone/.../portal_factory/Image/image.../edit...'
  >>> browser.getControl(name='title').value = 'Bar'
  >>> control = browser.getControl(name='image_file')
  >>> control.filename = 'bar.gif'
  >>> control.value = getFile('image.png')
  >>> browser.getControl('Save').click()

  >>> browser.url
  'http://nohost/plone/.../bar.../view'
  >>> browser.contents
  '...Info...Changes saved...
   ...Bar...
   ...<img src="http://.../bar.../image_preview"...title="Bar"...
   ...ull-size... KB...'

Now let's make sure we can also edit it.  First we store the current image,
however, so we can check it was actually updated:

  >>> browser.open('image_preview')
  >>> original = browser.contents
  >>> browser.goBack()

  >>> browser.getLink('Edit').click()
  >>> browser.getControl(name='title').value = 'Foobar'
  >>> browser.getControl('Replace with new image').selected = True
  >>> control = browser.getControl(name='image_file')
  >>> control.filename = 'foobar.gif'
  >>> control.value = getFile('image.gif')
  >>> browser.getControl('Save').click()

  >>> browser.url
  'http://nohost/plone/.../bar.../view'
  >>> browser.contents
  '...Info...Changes saved...
   ...Foobar...
   ...<img src="http://.../bar.../image_preview"...title="Foobar"...
   ...ull-size...1 KB...'

The actual image should also have been updated, of course:

  >>> browser.open('image_preview')
  >>> current = browser.contents
  >>> original == current
  False
  >>> browser.goBack()

Viewing an image should also work when browsing its URL directly, i.e.
without the `/view` action part:

  >>> gif = getFile('image.gif').read()
  >>> url = browser.url.replace('/view', '')
  >>> browser.open(url)
  >>> browser.contents == gif
  True
  >>> browser.headers.getheader('status').upper()
  '200 OK'
  >>> browser.headers.getheader('content-type')
  'image/gif'
  >>> browser.headers.getheader('content-disposition')
  'inline; filename="foobar.gif"'

Appending `/index_html` should be the same:

  >>> browser.open(url + '/index_html')
  >>> browser.contents == gif
  True
  >>> browser.headers.getheader('status').upper()
  '200 OK'
  >>> browser.headers.getheader('content-type')
  'image/gif'
  >>> browser.headers.getheader('content-disposition')
  'inline; filename="foobar.gif"'

Let's also check the scaled versions included the un-scaled variant:

  >>> browser.open(url + '/image_thumb')
  >>> browser.contents
  'GIF87a...'
  >>> browser.headers.getheader('status').upper()
  '200 OK'
  >>> browser.headers.getheader('content-type')
  'image/gif'

  >>> browser.open(url + '/image')
  >>> browser.contents
  'GIF89a...'
  >>> browser.headers.getheader('status').upper()
  '200 OK'
  >>> browser.headers.getheader('content-type')
  'image/gif'

It should also be possible to remove the image. Again, for this to work the
field must not be required.  That is not the default in Plone, however, so
we need to tweak the schema first:

  >>> folder.bar.getPrimaryField().required = False

  >>> browser.open(url + '/view')
  >>> browser.getLink('Edit').click()
  >>> browser.getControl('Delete current').selected = True
  >>> browser.getControl('Save').click()
  >>> 'Changes saved' in browser.contents
  True
  >>> 'No image has been uploaded yet' in browser.contents
  True

The image content should now be gone:

  >>> folder.foo.getImage().data
  ''
