def test_get_contenttype(self): self.assertEqual( get_contenttype( NamedImage(getFile('image.gif').read(), contentType='image/gif')), 'image/gif') self.assertEqual( get_contenttype( NamedImage(getFile('image.gif').read(), filename=u'image.gif')), 'image/gif') self.assertEqual( get_contenttype( NamedImage(getFile('notimage.doc').read(), filename=u'notimage.doc')), 'application/msword')
def testImageValidation(self): from plone.namedfile.field import InvalidImageFile,\ validate_image_field class FakeField(object): __name__ = 'logo' # field is empty validate_image_field(FakeField(), None) # field has an empty file image = self._makeImage() self.assertRaises( InvalidImageFile, validate_image_field, FakeField(), image) # field has an image file image._setData(zptlogo) validate_image_field(FakeField(), image) notimage = NamedImage(getFile('notimage.doc'), filename=u'notimage.doc') self.assertRaises( InvalidImageFile, validate_image_field, FakeField(), notimage)
def setUp(self): data = getFile('image.png').read() item = DummyContent() item.image = NamedImage(data, 'image/png', u'image.png') self.layer['app']._setOb('item', item) self.item = self.layer['app'].item self.scaling = ImageScaling(self.item, None)
def testImageValidation(self): from plone.namedfile.field import InvalidImageFile from plone.namedfile.field import validate_image_field from plone.namedfile.interfaces import INamedImageField from zope.interface import implementer @implementer(INamedImageField) class FakeField(object): __name__ = 'logo' # field is empty validate_image_field(FakeField(), None) # field has an empty file image = self._makeImage() self.assertRaises(InvalidImageFile, validate_image_field, FakeField(), image) # field has an image file image._setData(zptlogo) validate_image_field(FakeField(), image) notimage = NamedImage(getFile('notimage.doc'), filename=u'notimage.doc') self.assertRaises(InvalidImageFile, validate_image_field, FakeField(), notimage)
def setUp(self): self.app = self.layer['app'] data = getFile('image.png').read() item = DummyContent() item.image = NamedImage(data, 'image/png', u'image.png') self.app._setOb('item', item) self.item = self.app.item self._orig_sizes = ImageScaling._sizes
def test_get_contenttype(self): self.assertEqual( get_contenttype( NamedImage( getFile('image.gif'), contentType='image/gif')), 'image/gif') self.assertEqual( get_contenttype( NamedImage( getFile('image.gif'), filename=u'image.gif')), 'image/gif') self.assertEqual(get_contenttype( NamedImage(getFile('notimage.doc'), filename=u'notimage.doc')), 'application/msword')
def testScaleInvalidation(self): # first get the scale of the original image self.scaling.available_sizes = {'foo': (23, 23)} foo1 = self.scaling.scale('image', scale='foo') wait_to_ensure_modified() # now upload a new one and make sure the scale has changed data = getFile('image.jpg').read() self.item.image = NamedImage(data, 'image/jpeg', u'image.jpg') foo2 = self.scaling.scale('image', scale='foo') self.assertFalse(foo1.data == foo2.data, 'scale not updated?')
def setUp(self): data = getFile('image.png').read() item = DummyContent() item.image = NamedImage(data, 'image/png', u'image.png') self.layer['app']._setOb('item', item) self.item = self.layer['app'].item self.view = self.item.unrestrictedTraverse('@@images') self._orig_sizes = ImageScaling._sizes self.browser = Browser(self.layer['app']) self.browser.handleErrors = False self.browser.addHeader('Referer', self.layer['app'].absolute_url())
def test_opened_file_storable(self): data = getFile('image.gif') f = tempfile.NamedTemporaryFile(delete=False) try: path = f.name f.write(data) f.close() with open(path, 'rb') as f: fi = NamedBlobImage(f, filename=u'image.gif') finally: if os.path.exists(path): os.remove(path) self.assertEqual(303, fi.getSize())
def setUp(self): if six.PY2: raise unittest.SkipTest('Disabled in py2 for now.') data = getFile('image.png') item = DummyContent() item.image = NamedImage(data, 'image/png', u'image.png') self.layer['app']._setOb('item', item) self.item = self.layer['app'].item self.view = self.item.unrestrictedTraverse('@@images') self._orig_sizes = ImageScaling._sizes self.browser = Browser(self.layer['app']) self.browser.handleErrors = False self.browser.addHeader('Referer', self.layer['app'].absolute_url())
def setUp(self): if six.PY2: raise unittest.SkipTest("Disabled in py2 for now.") data = getFile("image.png") item = DummyContent() item.image = NamedImage(data, "image/png", u"image.png") self.layer["app"]._setOb("item", item) self.item = self.layer["app"].item self.view = self.item.unrestrictedTraverse("@@images") self._orig_sizes = ImageScaling._sizes self.browser = Browser(self.layer["app"]) self.browser.handleErrors = False self.browser.addHeader("Referer", self.layer["app"].absolute_url())
def testScaleInvalidation(self): # first view the thumbnail of the original image ImageScaling._sizes = {'thumb': (128, 128)} uid1, ext, width1, height1 = self.traverse('image/thumb') wait_to_ensure_modified() # now upload a new one and make sure the thumbnail has changed data = getFile('image.jpg').read() self.item.image = NamedImage(data, 'image/jpeg', u'image.jpg') uid2, ext, width2, height2 = self.traverse('image/thumb') self.assertNotEqual(uid1, uid2, 'thumb not updated?') # the height also differs as the original image had a size of 200, 200 # whereas the updated one has 500, 200... self.assertEqual(width1, width2) self.assertNotEqual(height1, height2)
def test_dimension_int(self): self.assertEqual(dimension_int("auto"), 0) self.assertEqual(dimension_int("0"), 0) self.assertEqual(dimension_int("25"), 25) self.assertEqual(dimension_int("1024px"), 1024) self.assertEqual(dimension_int("123.0025px"), 123) self.assertEqual(dimension_int("123.625pt"), 123) self.assertEqual(dimension_int("50%"), 50) self.assertEqual(dimension_int(42), 42) self.assertEqual(dimension_int(6.25), 6) self.assertEqual(dimension_int({}), 0) self.assertEqual(dimension_int([]), 0) self.assertEqual(dimension_int(getFile('image.svg')), 0)
def testSVGPublishThumbViaName(self): ImageScaling._sizes = {"thumb": (128, 128)} data = getFile("image.svg") svg = DummyContent() svg.image = NamedImage(data, "image/svg+xml", u"image.svg") self.layer["app"]._setOb("svg", svg) svg = self.layer["app"].svg transaction.commit() # make sure traversing works as is and with scaling # first the field without a scale name self.browser.open(self.layer["app"].absolute_url() + "/svg/@@images/image") self.assertEqual("image/svg+xml", self.browser.headers["content-type"]) self.assertEqual(self.browser.contents, getFile("image.svg")) # and last a scaled version self.browser.open( self.layer["app"].absolute_url() + "/svg/@@images/image/thumb" ) self.assertEqual("image/svg+xml", self.browser.headers["content-type"]) self.assertEqual(self.browser.contents, data)
def testPublishThumbViaName(self): ImageScaling._sizes = {'thumb': (128, 128)} transaction.commit() # make sure traversing works as is and with scaling # first the field without a scale name self.browser.open(self.layer['app'].absolute_url() + '/item/@@images/image') self.assertEqual('image/png', self.browser.headers['content-type']) self.assertEqual(self.browser.contents, getFile('image.png').read()) # and last a scaled version self.browser.open(self.layer['app'].absolute_url() + '/item/@@images/image/thumb') self.assertEqual('image/png', self.browser.headers['content-type']) assertImage(self, self.browser.contents, 'PNG', (128, 128))
def testPublishThumbViaName(self): ImageScaling._sizes = {"thumb": (128, 128)} transaction.commit() # make sure traversing works as is and with scaling # first the field without a scale name self.browser.open(self.layer["app"].absolute_url() + "/item/@@images/image") self.assertEqual("image/png", self.browser.headers["content-type"]) self.assertEqual(self.browser.contents, getFile("image.png")) # and last a scaled version self.browser.open( self.layer["app"].absolute_url() + "/item/@@images/image/thumb" ) self.assertEqual("image/png", self.browser.headers["content-type"]) assertImage(self, self.browser.contents, "PNG", (128, 128))
def testPublishThumbViaName(self): ImageScaling._sizes = {'thumb': (128, 128)} transaction.commit() # make sure traversing works as is and with scaling # first the field without a scale name self.browser.open( self.layer['app'].absolute_url() + '/item/@@images/image' ) self.assertEqual('image/png', self.browser.headers['content-type']) self.assertEqual(self.browser.contents, getFile('image.png').read()) # and last a scaled version self.browser.open( self.layer['app'].absolute_url() + '/item/@@images/image/thumb' ) self.assertEqual('image/png', self.browser.headers['content-type']) assertImage(self, self.browser.contents, 'PNG', (128, 128))
def testScaledJpegImageQuality(self): """Test image quality setting for jpeg images. Image quality not available for PNG images. """ data = getFile('image.jpg').read() item = DummyContent() item.image = NamedImage(data, 'image/jpeg', u'image.jpg') scaling = ImageScaling(item, None) # scale an image, record its size foo = scaling.scale('image', width=100, height=80) size_foo = foo.data.getSize() # let's pretend p.a.imaging set the scaling quality to "really sloppy" gsm = getGlobalSiteManager() qualitySupplier = DummyQualitySupplier() gsm.registerUtility(qualitySupplier.getQuality, IScaledImageQuality) wait_to_ensure_modified() # now scale again bar = scaling.scale('image', width=100, height=80) size_bar = bar.data.getSize() # first one should be bigger self.assertTrue(size_foo > size_bar)
def testScaledJpegImageQuality(self): """Test image quality setting for jpeg images. Image quality not available for PNG images. """ data = getFile('image.jpg').read() item = DummyContent() item.image = NamedImage(data, 'image/png', u'image.jpg') scaling = ImageScaling(item, None) # scale an image, record its size foo = scaling.scale('image', width=100, height=80) size_foo = foo.data.getSize() # let's pretend p.a.imaging set the scaling quality to "really sloppy" gsm = getGlobalSiteManager() qualitySupplier = DummyQualitySupplier() gsm.registerUtility(qualitySupplier.getQuality, IScaledImageQuality) wait_to_ensure_modified() # now scale again bar = scaling.scale('image', width=100, height=80) size_bar = bar.data.getSize() # first one should be bigger self.assertTrue(size_foo > size_bar)
def test_str_storable(self): fi = NamedBlobImage(getFile('image.gif'), filename=u'image.gif') self.assertEqual(303, fi.getSize())
def test_pdata_storable(self): pdata = Pdata(getFile('image.gif').read()) fi = NamedBlobImage(pdata, filename=u'image.gif') self.assertEqual(303, fi.getSize())
def test_filechunk_storable(self): fi = NamedBlobImage(FileChunk(getFile('image.gif').read()), filename=u'image.gif') self.assertEqual(303, fi.getSize())
def test_process_svg(self): content_type, width, height = process_svg(getFile('image.svg')) self.assertEqual(content_type, "image/svg+xml") self.assertEqual(width, 158) self.assertEqual(height, 40)
def test_get_contenttype(self): self.assertEqual( get_contenttype( NamedImage(getFile('image.svg'), contentType='image/svg+xml')), 'image/svg+xml')