2009-05-24 14:16:17 +02:00
|
|
|
<?php
|
2010-12-20 03:19:17 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
use Filesystem as SS_Filesystem;
|
|
|
|
use League\Flysystem\Filesystem;
|
|
|
|
use SilverStripe\Filesystem\Flysystem\AssetAdapter;
|
|
|
|
use SilverStripe\Filesystem\Flysystem\FlysystemAssetStore;
|
|
|
|
use SilverStripe\Filesystem\Flysystem\FlysystemUrlPlugin;
|
|
|
|
|
2010-12-20 03:19:17 +01:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-12-20 03:19:17 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2009-05-24 14:16:17 +02:00
|
|
|
class ImageTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'ImageTest.yml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setUp() {
|
2010-12-20 03:19:17 +01:00
|
|
|
parent::setUp();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-07-16 12:12:15 +02:00
|
|
|
// Execute specific subclass
|
|
|
|
if(get_class($this) == "ImageTest") {
|
|
|
|
$this->markTestSkipped(sprintf('Skipping %s ', get_class($this)));
|
2012-10-25 00:28:39 +02:00
|
|
|
return;
|
2010-12-20 03:19:17 +01:00
|
|
|
}
|
2015-06-10 01:16:11 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Set backend root to /ImageTest
|
|
|
|
AssetStoreTest_SpyStore::activate('ImageTest');
|
|
|
|
|
2015-06-10 01:16:11 +02:00
|
|
|
// Copy test images for each of the fixture references
|
2015-09-15 04:52:02 +02:00
|
|
|
$files = File::get()->exclude('ClassName', 'Folder');
|
|
|
|
foreach($files as $image) {
|
2015-10-19 06:27:27 +02:00
|
|
|
$filePath = AssetStoreTest_SpyStore::getLocalPath($image); // Only correct for test asset store
|
2015-09-15 04:52:02 +02:00
|
|
|
$sourcePath = BASE_PATH . '/framework/tests/model/testimages/' . $image->Name;
|
2015-06-10 01:16:11 +02:00
|
|
|
if(!file_exists($filePath)) {
|
2015-09-15 04:52:02 +02:00
|
|
|
SS_Filesystem::makeFolder(dirname($filePath));
|
|
|
|
if (!copy($sourcePath, $filePath)) {
|
|
|
|
user_error('Failed to copy test images', E_USER_ERROR);
|
|
|
|
}
|
2015-06-10 01:16:11 +02:00
|
|
|
}
|
|
|
|
}
|
2012-10-25 00:28:39 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
public function tearDown() {
|
2015-09-15 04:52:02 +02:00
|
|
|
AssetStoreTest_SpyStore::reset();
|
2012-10-25 00:28:39 +02:00
|
|
|
parent::tearDown();
|
2010-12-20 03:19:17 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetTagWithTitle() {
|
2015-09-15 04:52:02 +02:00
|
|
|
Config::inst()->update('DBFile', 'force_resample', false);
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2009-07-08 02:06:16 +02:00
|
|
|
$image = $this->objFromFixture('Image', 'imageWithTitle');
|
2015-09-15 04:52:02 +02:00
|
|
|
$expected = '<img src="/assets/ImageTest/folder/444065542b/test-image.png" alt="This is a image Title" />';
|
|
|
|
$actual = trim($image->getTag());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-24 14:16:17 +02:00
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetTagWithoutTitle() {
|
2015-09-15 04:52:02 +02:00
|
|
|
Config::inst()->update('DBFile', 'force_resample', false);
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2009-07-08 02:06:16 +02:00
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
2015-09-15 04:52:02 +02:00
|
|
|
$expected = '<img src="/assets/ImageTest/folder/444065542b/test-image.png" alt="test image" />';
|
|
|
|
$actual = trim($image->getTag());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-24 14:16:17 +02:00
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetTagWithoutTitleContainingDots() {
|
2015-09-15 04:52:02 +02:00
|
|
|
Config::inst()->update('DBFile', 'force_resample', false);
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2009-07-08 02:06:16 +02:00
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitleContainingDots');
|
2015-09-15 04:52:02 +02:00
|
|
|
$expected = '<img src="/assets/ImageTest/folder/46affab704/test.image.with.dots.png" alt="test.image.with.dots" />';
|
|
|
|
$actual = trim($image->getTag());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-24 14:16:17 +02:00
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
/**
|
|
|
|
* Tests that multiple image manipulations may be performed on a single Image
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testMultipleGenerateManipulationCalls() {
|
2010-12-20 03:19:17 +01:00
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
$imageFirst = $image->ScaleWidth(200);
|
2010-12-20 03:19:17 +01:00
|
|
|
$this->assertNotNull($imageFirst);
|
|
|
|
$expected = 200;
|
|
|
|
$actual = $imageFirst->getWidth();
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
$imageSecond = $imageFirst->ScaleHeight(100);
|
2010-12-20 03:19:17 +01:00
|
|
|
$this->assertNotNull($imageSecond);
|
|
|
|
$expected = 100;
|
|
|
|
$actual = $imageSecond->getHeight();
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
/**
|
|
|
|
* Tests that image manipulations that do not affect the resulting dimensions
|
|
|
|
* of the output image do not resample the file.
|
|
|
|
*/
|
|
|
|
public function testReluctanceToResampling() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
|
|
|
$this->assertTrue($image->isSize(300, 300));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-06-28 05:20:28 +02:00
|
|
|
// Set width to 300 pixels
|
2015-06-13 01:42:51 +02:00
|
|
|
$imageScaleWidth = $image->ScaleWidth(300);
|
|
|
|
$this->assertEquals($imageScaleWidth->getWidth(), 300);
|
|
|
|
$this->assertEquals($image->Filename, $imageScaleWidth->Filename);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
// Set height to 300 pixels
|
2015-06-13 01:42:51 +02:00
|
|
|
$imageScaleHeight = $image->ScaleHeight(300);
|
|
|
|
$this->assertEquals($imageScaleHeight->getHeight(), 300);
|
|
|
|
$this->assertEquals($image->Filename, $imageScaleHeight->Filename);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
// Crop image to 300 x 300
|
2015-06-13 01:42:51 +02:00
|
|
|
$imageCropped = $image->Fill(300, 300);
|
2013-03-21 00:15:28 +01:00
|
|
|
$this->assertTrue($imageCropped->isSize(300, 300));
|
|
|
|
$this->assertEquals($image->Filename, $imageCropped->Filename);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
// Resize (padded) to 300 x 300
|
2015-06-13 01:42:51 +02:00
|
|
|
$imageSized = $image->Pad(300, 300);
|
2013-03-21 00:15:28 +01:00
|
|
|
$this->assertTrue($imageSized->isSize(300, 300));
|
|
|
|
$this->assertEquals($image->Filename, $imageSized->Filename);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
// Padded image 300 x 300 (same as above)
|
2015-06-13 01:42:51 +02:00
|
|
|
$imagePadded = $image->Pad(300, 300);
|
2013-03-21 00:15:28 +01:00
|
|
|
$this->assertTrue($imagePadded->isSize(300, 300));
|
|
|
|
$this->assertEquals($image->Filename, $imagePadded->Filename);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
// Resized (stretched) to 300 x 300
|
|
|
|
$imageStretched = $image->ResizedImage(300, 300);
|
|
|
|
$this->assertTrue($imageStretched->isSize(300, 300));
|
|
|
|
$this->assertEquals($image->Filename, $imageStretched->Filename);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
// Fit (various options)
|
|
|
|
$imageFit = $image->Fit(300, 600);
|
|
|
|
$this->assertTrue($imageFit->isSize(300, 300));
|
|
|
|
$this->assertEquals($image->Filename, $imageFit->Filename);
|
|
|
|
$imageFit = $image->Fit(600, 300);
|
|
|
|
$this->assertTrue($imageFit->isSize(300, 300));
|
|
|
|
$this->assertEquals($image->Filename, $imageFit->Filename);
|
|
|
|
$imageFit = $image->Fit(300, 300);
|
|
|
|
$this->assertTrue($imageFit->isSize(300, 300));
|
|
|
|
$this->assertEquals($image->Filename, $imageFit->Filename);
|
2013-03-21 00:15:28 +01:00
|
|
|
}
|
2014-06-28 05:20:28 +02:00
|
|
|
|
|
|
|
/**
|
2016-03-08 21:50:18 +01:00
|
|
|
* Tests that a URL to a resampled image is provided when force_resample is
|
2015-07-16 21:03:52 +02:00
|
|
|
* set to true, if the resampled file is smaller than the original.
|
2014-06-28 05:20:28 +02:00
|
|
|
*/
|
|
|
|
public function testForceResample() {
|
2015-07-16 21:03:52 +02:00
|
|
|
$imageHQ = $this->objFromFixture('Image', 'highQualityJPEG');
|
|
|
|
$imageHQR = $imageHQ->Resampled();
|
|
|
|
$imageLQ = $this->objFromFixture('Image', 'lowQualityJPEG');
|
|
|
|
$imageLQR = $imageLQ->Resampled();
|
2016-01-20 01:16:27 +01:00
|
|
|
|
2015-07-16 21:03:52 +02:00
|
|
|
// Test resampled file is served when force_resample = true
|
2015-09-15 04:52:02 +02:00
|
|
|
Config::inst()->update('DBFile', 'force_resample', true);
|
2015-07-16 21:03:52 +02:00
|
|
|
$this->assertLessThan($imageHQ->getAbsoluteSize(), $imageHQR->getAbsoluteSize(), 'Resampled image is smaller than original');
|
|
|
|
$this->assertEquals($imageHQ->getURL(), $imageHQR->getSourceURL(), 'Path to a resampled image was returned by getURL()');
|
2016-01-20 01:16:27 +01:00
|
|
|
|
2015-07-16 21:03:52 +02:00
|
|
|
// Test original file is served when force_resample = true but original file is low quality
|
|
|
|
$this->assertGreaterThanOrEqual($imageLQ->getAbsoluteSize(), $imageLQR->getAbsoluteSize(), 'Resampled image is larger or same size as original');
|
|
|
|
$this->assertNotEquals($imageLQ->getURL(), $imageLQR->getSourceURL(), 'Path to the original image file was returned by getURL()');
|
2016-01-20 01:16:27 +01:00
|
|
|
|
2015-07-16 21:03:52 +02:00
|
|
|
// Test original file is served when force_resample = false
|
2015-09-15 04:52:02 +02:00
|
|
|
Config::inst()->update('DBFile', 'force_resample', false);
|
2015-07-16 21:03:52 +02:00
|
|
|
$this->assertNotEquals($imageHQ->getURL(), $imageHQR->getSourceURL(), 'Path to the original image file was returned by getURL()');
|
2014-06-28 05:20:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
public function testImageResize() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
|
|
|
$this->assertTrue($image->isSize(300, 300));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
// Test normal resize
|
2015-06-13 01:42:51 +02:00
|
|
|
$resized = $image->Pad(150, 100);
|
2013-03-21 00:15:28 +01:00
|
|
|
$this->assertTrue($resized->isSize(150, 100));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
// Test cropped resize
|
2015-06-13 01:42:51 +02:00
|
|
|
$cropped = $image->Fill(100, 200);
|
2013-03-21 00:15:28 +01:00
|
|
|
$this->assertTrue($cropped->isSize(100, 200));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
// Test padded resize
|
2015-06-13 01:42:51 +02:00
|
|
|
$padded = $image->Pad(200, 100);
|
2013-03-21 00:15:28 +01:00
|
|
|
$this->assertTrue($padded->isSize(200, 100));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
// Test Fit
|
|
|
|
$ratio = $image->Fit(80, 160);
|
2013-03-21 00:15:28 +01:00
|
|
|
$this->assertTrue($ratio->isSize(80, 80));
|
2015-06-13 01:42:51 +02:00
|
|
|
|
|
|
|
// Test FitMax
|
|
|
|
$fitMaxDn = $image->FitMax(200, 100);
|
|
|
|
$this->assertTrue($fitMaxDn->isSize(100, 100));
|
|
|
|
$fitMaxUp = $image->FitMax(500, 400);
|
|
|
|
$this->assertTrue($fitMaxUp->isSize(300, 300));
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
//Test ScaleMax
|
|
|
|
$scaleMaxWDn = $image->ScaleMaxWidth(200);
|
|
|
|
$this->assertTrue($scaleMaxWDn->isSize(200, 200));
|
|
|
|
$scaleMaxWUp = $image->ScaleMaxWidth(400);
|
|
|
|
$this->assertTrue($scaleMaxWUp->isSize(300, 300));
|
|
|
|
$scaleMaxHDn = $image->ScaleMaxHeight(200);
|
|
|
|
$this->assertTrue($scaleMaxHDn->isSize(200, 200));
|
|
|
|
$scaleMaxHUp = $image->ScaleMaxHeight(400);
|
|
|
|
$this->assertTrue($scaleMaxHUp->isSize(300, 300));
|
|
|
|
|
|
|
|
// Test FillMax
|
|
|
|
$cropMaxDn = $image->FillMax(200, 100);
|
|
|
|
$this->assertTrue($cropMaxDn->isSize(200, 100));
|
|
|
|
$cropMaxUp = $image->FillMax(400, 200);
|
|
|
|
$this->assertTrue($cropMaxUp->isSize(300, 150));
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
// Test Clip
|
|
|
|
$clipWDn = $image->CropWidth(200);
|
|
|
|
$this->assertTrue($clipWDn->isSize(200, 300));
|
|
|
|
$clipWUp = $image->CropWidth(400);
|
|
|
|
$this->assertTrue($clipWUp->isSize(300, 300));
|
|
|
|
$clipHDn = $image->CropHeight(200);
|
|
|
|
$this->assertTrue($clipHDn->isSize(300, 200));
|
|
|
|
$clipHUp = $image->CropHeight(400);
|
|
|
|
$this->assertTrue($clipHUp->isSize(300, 300));
|
2013-03-21 00:15:28 +01:00
|
|
|
}
|
2013-04-15 12:17:00 +02:00
|
|
|
|
|
|
|
/**
|
2014-11-18 23:05:07 +01:00
|
|
|
* @expectedException InvalidArgumentException
|
2013-04-15 12:17:00 +02:00
|
|
|
*/
|
|
|
|
public function testGenerateImageWithInvalidParameters() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
2015-09-15 04:52:02 +02:00
|
|
|
$image->ScaleHeight('String');
|
2015-06-13 01:42:51 +02:00
|
|
|
$image->Pad(600,600,'XXXXXX');
|
2013-04-15 12:17:00 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-15 12:17:00 +02:00
|
|
|
public function testCacheFilename() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
2015-06-13 01:42:51 +02:00
|
|
|
$imageFirst = $image->Pad(200,200,'CCCCCC');
|
2015-09-15 04:52:02 +02:00
|
|
|
$imageFilename = $imageFirst->getURL();
|
2013-04-15 12:17:00 +02:00
|
|
|
// Encoding of the arguments is duplicated from cacheFilename
|
2015-07-28 03:54:49 +02:00
|
|
|
$neededPart = 'Pad' . Convert::base64url_encode(array(200,200,'CCCCCC'));
|
2013-04-15 12:17:00 +02:00
|
|
|
$this->assertContains($neededPart, $imageFilename, 'Filename for cached image is correctly generated');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-02 19:38:02 +02:00
|
|
|
/**
|
2016-03-08 21:50:18 +01:00
|
|
|
* Test that propertes from the source Image are inherited by resampled images
|
2015-09-02 19:38:02 +02:00
|
|
|
*/
|
|
|
|
public function testPropertyInheritance() {
|
|
|
|
$testString = 'This is a test';
|
|
|
|
$origImage = $this->objFromFixture('Image', 'imageWithTitle');
|
|
|
|
$origImage->TestProperty = $testString;
|
|
|
|
$resampled = $origImage->ScaleWidth(10);
|
|
|
|
$this->assertEquals($resampled->TestProperty, $testString);
|
|
|
|
$resampled2 = $resampled->ScaleWidth(5);
|
|
|
|
$this->assertEquals($resampled2->TestProperty, $testString);
|
|
|
|
}
|
2016-03-08 12:20:51 +01:00
|
|
|
|
|
|
|
public function testShortcodeHandlerFallsBackToFileProperties() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithTitle');
|
|
|
|
$parser = new ShortcodeParser();
|
|
|
|
$parser->register('image', array('Image', 'handle_shortcode'));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
sprintf(
|
|
|
|
'<img src="%s" alt="%s">',
|
|
|
|
$image->Link(),
|
|
|
|
$image->Title
|
|
|
|
),
|
|
|
|
$parser->parse(sprintf('[image id=%d]', $image->ID))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShortcodeHandlerUsesShortcodeProperties() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithTitle');
|
|
|
|
$parser = new ShortcodeParser();
|
|
|
|
$parser->register('image', array('Image', 'handle_shortcode'));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
sprintf(
|
|
|
|
'<img src="%s" alt="Alt content" title="Title content">',
|
|
|
|
$image->Link()
|
|
|
|
),
|
|
|
|
$parser->parse(sprintf(
|
|
|
|
'[image id="%d" alt="Alt content" title="Title content"]',
|
|
|
|
$image->ID
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShortcodeHandlerAddsDefaultAttributes() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
|
|
|
$parser = new ShortcodeParser();
|
|
|
|
$parser->register('image', array('Image', 'handle_shortcode'));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
sprintf(
|
|
|
|
'<img src="%s" alt="%s">',
|
|
|
|
$image->Link(),
|
|
|
|
$image->Title
|
|
|
|
),
|
|
|
|
$parser->parse(sprintf(
|
|
|
|
'[image id="%d"]',
|
|
|
|
$image->ID
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|