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
  >>> browser = self.getBrowser()
  >>> browser.open(self.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:

  >>> self.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:

  >>> self.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(self.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...3.5 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:

  >>> self.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:

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