A simple test the make sure the traversal adapters for image scales are
working correctly.  First we have to create an image to start with:

  >>> from plone.app.testing import TEST_USER_ID, setRoles
  >>> setRoles(layer['portal'], TEST_USER_ID, ['Manager'])
  >>> from plone.app.imaging.tests.utils import getData
  >>> data = getData('image.gif')
  >>> portal = layer['portal']
  >>> portal.invokeFactory('Image', id='foo', title='Foo', image=data)
  'foo'
  >>> portal.foo.getField('image').removeScales(portal.foo)

  >>> from transaction import commit
  >>> commit()

Then we can use a testbrowser to access the image itself and a scaled down
version:

  >>> from plone.testing.z2 import Browser
  >>> from plone.app.testing import TEST_USER_NAME, TEST_USER_PASSWORD
  >>> browser = Browser(layer['app'])
  >>> browser.addHeader('Authorization', 'Basic %s:%s' % (TEST_USER_NAME, TEST_USER_PASSWORD))
  >>> browser.open('http://nohost/plone/foo')
  >>> browser.contents
  'GIF89a...'
  >>> len(browser.contents) == len(data)
  True

Let's analyse the image a little further:

  >>> from PIL.Image import open
  >>> from StringIO import StringIO
  >>> image = open(StringIO(browser.contents))
  >>> image.format
  'GIF'
  >>> image.size
  (200, 200)

Now check the scaled version:

  >>> browser.open('http://nohost/plone/foo/image_thumb')
  >>> browser.contents
  'GIF87a...'
  >>> image = open(StringIO(browser.contents))
  >>> image.format
  'GIF'
  >>> image.size
  (128, 128)

We also want to be able to traverse to the image scale in path expressions,
which don't use publish traversal:

  >>> from Products.CMFCore.Expression import Expression, getExprContext
  >>> def eval_expression(expr):
  ...     econtext = getExprContext(layer['portal'])
  ...     return Expression(expr)(econtext)
  >>> eval_expression('portal/foo/image_thumb')
  <ImageScale at ...>

And let's make sure that we can check for the scale with an 'exists'
expression:

  >>> bool(eval_expression('exists:portal/foo/image_thumb'))
  True

Content can be added with ids that correspond to field names and scales:

  >>> import os
  >>> import shutil
  >>> from plone.app.imaging.tests import base
  >>> test_image_scale_name = os.path.join(
  ...     base.TESTS_PATH,
  ...     "tmp",
  ...     "image_thumb.jpg",
  ... )
  >>> shutil.copyfile(
  ...     os.path.join(base.TESTS_PATH, "image.jpg"),
  ...     test_image_scale_name,
  ... )

  >>> import __builtin__
  >>> portal.portal_setup.runAllImportStepsFromProfile("plone.app.imaging:testing")
  {...types: 'News Item Folder'...}
  >>> with __builtin__.open(test_image_scale_name) as test_image_scale_name_opened:
  ...     portal.invokeFactory(
  ...         "News Item Folder",
  ...         id="bar-folder",
  ...         title="Bar Folder",
  ...         image=test_image_scale_name_opened,
  ...     )
  'bar-folder'
  >>> commit()

  >>> browser.handleErrors = False
  >>> browser.open("http://nohost/plone/bar-folder/")
  >>> browser.getLink(url="createObject?type_name=Image").click()
  >>> with __builtin__.open(test_image_scale_name) as test_image_scale_name_opened:
  ...     browser.getControl(name="id").value = "image_thumb"
  ...     browser.getControl(name="image_file").add_file(
  ...         test_image_scale_name_opened, "image/jpg", "image_thumb.jpg")
  ...     browser.getControl("Save").click()
  >>> browser.url
  'http://nohost/plone/bar-folder/image_thumb/view'
  >>> print(browser.contents)
  <!DOCTYPE html>
  <html...
  >>> browser.open("http://nohost/plone/bar-folder/image_thumb")
  >>> browser.contents
  '\xff\xd8\xff\xe0\x00\x10JFIF...
