2009-05-24 14:16:17 +02:00
|
|
|
<?php
|
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-10-25 00:28:39 +02:00
|
|
|
protected $origBackend;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setUp() {
|
2014-04-22 03:28:44 +02:00
|
|
|
if(get_class($this) == "ImageTest") $this->skipTest = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 03:19:17 +01:00
|
|
|
parent::setUp();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-22 03:28:44 +02:00
|
|
|
$this->origBackend = Image::get_backend();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
if($this->skipTest)
|
|
|
|
return;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 03:19:17 +01:00
|
|
|
if(!file_exists(ASSETS_PATH)) mkdir(ASSETS_PATH);
|
|
|
|
|
|
|
|
// Create a test folders for each of the fixture references
|
|
|
|
$folderIDs = $this->allFixtureIDs('Folder');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 03:19:17 +01:00
|
|
|
foreach($folderIDs as $folderID) {
|
|
|
|
$folder = DataObject::get_by_id('Folder', $folderID);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-20 03:19:17 +01:00
|
|
|
if(!file_exists(BASE_PATH."/$folder->Filename")) mkdir(BASE_PATH."/$folder->Filename");
|
|
|
|
}
|
2015-06-10 01:16:11 +02:00
|
|
|
|
|
|
|
// Copy test images for each of the fixture references
|
|
|
|
$imageIDs = $this->allFixtureIDs('Image');
|
|
|
|
foreach($imageIDs as $imageID) {
|
|
|
|
$image = DataObject::get_by_id('Image', $imageID);
|
|
|
|
$filePath = BASE_PATH."/$image->Filename";
|
|
|
|
$sourcePath = str_replace('assets/ImageTest/', 'framework/tests/model/testimages/', $filePath);
|
|
|
|
if(!file_exists($filePath)) {
|
|
|
|
if (!copy($sourcePath, $filePath)) user_error('Failed to copy test images', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
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() {
|
2014-04-22 03:28:44 +02:00
|
|
|
if($this->origBackend) Image::set_backend($this->origBackend);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-10 01:16:11 +02:00
|
|
|
// Remove the test files that we've created
|
2010-12-20 03:19:17 +01:00
|
|
|
$fileIDs = $this->allFixtureIDs('Image');
|
|
|
|
foreach($fileIDs as $fileID) {
|
|
|
|
$file = DataObject::get_by_id('Image', $fileID);
|
2012-10-25 00:28:39 +02:00
|
|
|
if($file && file_exists(BASE_PATH."/$file->Filename")) unlink(BASE_PATH."/$file->Filename");
|
|
|
|
}
|
|
|
|
|
2015-06-10 01:16:11 +02:00
|
|
|
// Remove the test folders that we've created
|
2012-10-25 00:28:39 +02:00
|
|
|
$folderIDs = $this->allFixtureIDs('Folder');
|
|
|
|
foreach($folderIDs as $folderID) {
|
|
|
|
$folder = DataObject::get_by_id('Folder', $folderID);
|
|
|
|
if($folder && file_exists(BASE_PATH."/$folder->Filename")) {
|
|
|
|
Filesystem::removeFolder(BASE_PATH."/$folder->Filename");
|
|
|
|
}
|
2013-04-06 12:48:00 +02:00
|
|
|
if($folder && file_exists(BASE_PATH."/".$folder->Filename."_resampled")) {
|
|
|
|
Filesystem::removeFolder(BASE_PATH."/".$folder->Filename."_resampled");
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2010-12-20 03:19:17 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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() {
|
2009-07-08 02:06:16 +02:00
|
|
|
$image = $this->objFromFixture('Image', 'imageWithTitle');
|
2014-08-15 08:53:05 +02:00
|
|
|
$expected = '<img src="' . Director::baseUrl()
|
2012-09-26 23:34:00 +02:00
|
|
|
. 'assets/ImageTest/test_image.png" alt="This is a image Title" />';
|
2009-05-24 14:16:17 +02:00
|
|
|
$actual = $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() {
|
2009-07-08 02:06:16 +02:00
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
2010-10-15 05:17:14 +02:00
|
|
|
$expected = '<img src="' . Director::baseUrl() . 'assets/ImageTest/test_image.png" alt="test_image" />';
|
2009-05-24 14:16:17 +02:00
|
|
|
$actual = $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() {
|
2009-07-08 02:06:16 +02:00
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitleContainingDots');
|
2012-09-26 23:34:00 +02:00
|
|
|
$expected = '<img src="' . Director::baseUrl()
|
|
|
|
. 'assets/ImageTest/test.image.with.dots.png" alt="test.image.with.dots" />';
|
2009-05-24 14:16:17 +02:00
|
|
|
$actual = $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
|
|
|
|
2010-12-20 03:19:17 +01:00
|
|
|
$imageSecond = $imageFirst->setHeight(100);
|
|
|
|
$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
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that image manipulations that do not affect the resulting dimensions
|
|
|
|
* of the output image resample the file when force_resample is set to true.
|
|
|
|
*/
|
|
|
|
public function testForceResample() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
|
|
|
$this->assertTrue($image->isSize(300, 300));
|
|
|
|
|
|
|
|
$origForceResample = Config::inst()->get('Image', 'force_resample');
|
|
|
|
Config::inst()->update('Image', 'force_resample', true);
|
|
|
|
|
|
|
|
// Set width to 300 pixels
|
2015-06-13 01:42:51 +02:00
|
|
|
$imageScaleWidth = $image->ScaleWidth(300);
|
|
|
|
$this->assertEquals($imageScaleWidth->getWidth(), 300);
|
|
|
|
$this->assertNotEquals($image->Filename, $imageScaleWidth->Filename);
|
2014-06-28 05:20:28 +02:00
|
|
|
|
|
|
|
// Set height to 300 pixels
|
2015-06-13 01:42:51 +02:00
|
|
|
$imageScaleHeight = $image->ScaleHeight(300);
|
|
|
|
$this->assertEquals($imageScaleHeight->getHeight(), 300);
|
|
|
|
$this->assertNotEquals($image->Filename, $imageScaleHeight->Filename);
|
2014-06-28 05:20:28 +02:00
|
|
|
|
|
|
|
// Crop image to 300 x 300
|
2015-06-13 01:42:51 +02:00
|
|
|
$imageCropped = $image->Fill(300, 300);
|
2014-06-28 05:20:28 +02:00
|
|
|
$this->assertTrue($imageCropped->isSize(300, 300));
|
|
|
|
$this->assertNotEquals($image->Filename, $imageCropped->Filename);
|
|
|
|
|
|
|
|
// Resize (padded) to 300 x 300
|
2015-06-13 01:42:51 +02:00
|
|
|
$imageSized = $image->Pad(300, 300);
|
2014-06-28 05:20:28 +02:00
|
|
|
$this->assertTrue($imageSized->isSize(300, 300));
|
|
|
|
$this->assertNotEquals($image->Filename, $imageSized->Filename);
|
|
|
|
|
|
|
|
// Padded image 300 x 300 (same as above)
|
2015-06-13 01:42:51 +02:00
|
|
|
$imagePadded = $image->Pad(300, 300);
|
2014-06-28 05:20:28 +02:00
|
|
|
$this->assertTrue($imagePadded->isSize(300, 300));
|
|
|
|
$this->assertNotEquals($image->Filename, $imagePadded->Filename);
|
|
|
|
|
|
|
|
// Resized (stretched) to 300 x 300
|
|
|
|
$imageStretched = $image->ResizedImage(300, 300);
|
|
|
|
$this->assertTrue($imageStretched->isSize(300, 300));
|
|
|
|
$this->assertNotEquals($image->Filename, $imageStretched->Filename);
|
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
// Fit (various options)
|
|
|
|
$imageFit = $image->Fit(300, 600);
|
|
|
|
$this->assertTrue($imageFit->isSize(300, 300));
|
|
|
|
$this->assertNotEquals($image->Filename, $imageFit->Filename);
|
|
|
|
$imageFit = $image->Fit(600, 300);
|
|
|
|
$this->assertTrue($imageFit->isSize(300, 300));
|
|
|
|
$this->assertNotEquals($image->Filename, $imageFit->Filename);
|
|
|
|
$imageFit = $image->Fit(300, 300);
|
|
|
|
$this->assertTrue($imageFit->isSize(300, 300));
|
|
|
|
$this->assertNotEquals($image->Filename, $imageFit->Filename);
|
2014-06-28 05:20:28 +02:00
|
|
|
Config::inst()->update('Image', 'force_resample', $origForceResample);
|
|
|
|
}
|
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));
|
|
|
|
|
|
|
|
//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));
|
|
|
|
|
|
|
|
// 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');
|
|
|
|
$image->setHeight('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');
|
2013-04-15 12:17:00 +02:00
|
|
|
$imageFilename = $imageFirst->getFullPath();
|
|
|
|
// 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
|
|
|
|
2013-04-15 12:17:00 +02:00
|
|
|
public function testMultipleGenerateManipulationCalls_Regeneration() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
|
|
|
$folder = new SS_FileFinder();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
$imageFirst = $image->Pad(200,200);
|
2013-04-15 12:17:00 +02:00
|
|
|
$this->assertNotNull($imageFirst);
|
|
|
|
$expected = 200;
|
|
|
|
$actual = $imageFirst->getWidth();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-15 12:17:00 +02:00
|
|
|
$this->assertEquals($expected, $actual);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-15 12:17:00 +02:00
|
|
|
$imageSecond = $imageFirst->setHeight(100);
|
|
|
|
$this->assertNotNull($imageSecond);
|
|
|
|
$expected = 100;
|
|
|
|
$actual = $imageSecond->getHeight();
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
$imageThird = $imageSecond->Pad(600,600,'0F0F0F');
|
2013-04-15 12:17:00 +02:00
|
|
|
// Encoding of the arguments is duplicated from cacheFilename
|
2015-07-28 03:54:49 +02:00
|
|
|
$argumentString = Convert::base64url_encode(array(600,600,'0F0F0F'));
|
2013-04-15 12:17:00 +02:00
|
|
|
$this->assertNotNull($imageThird);
|
2014-03-30 08:51:38 +02:00
|
|
|
$this->assertContains($argumentString, $imageThird->getFullPath(),
|
|
|
|
'Image contains background color for padded resizement');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-08-17 18:31:12 +02:00
|
|
|
$resampledFolder = dirname($image->getFullPath()) . "/_resampled";
|
|
|
|
$filesInFolder = $folder->find($resampledFolder);
|
2014-03-30 08:51:38 +02:00
|
|
|
$this->assertEquals(3, count($filesInFolder),
|
|
|
|
'Image folder contains only the expected number of images before regeneration');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-08-17 18:31:12 +02:00
|
|
|
$imageThirdPath = $imageThird->getFullPath();
|
2015-10-06 05:35:44 +02:00
|
|
|
$stats = getimagesize($imageThirdPath);
|
2014-03-30 08:51:38 +02:00
|
|
|
$this->assertEquals(3, $image->regenerateFormattedImages(),
|
|
|
|
'Cached images were regenerated in the right number');
|
2015-10-06 05:35:44 +02:00
|
|
|
$this->assertEquals($stats, getimagesize($imageThirdPath), 'Regeneration of third image is correct');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-15 12:17:00 +02:00
|
|
|
/* Check that no other images exist, to ensure that the regeneration did not create other images */
|
2015-08-17 18:31:12 +02:00
|
|
|
$this->assertEquals($filesInFolder, $folder->find($resampledFolder),
|
2014-03-30 08:51:38 +02:00
|
|
|
'Image folder contains only the expected image files after regeneration');
|
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 testRegenerateImages() {
|
2015-08-17 18:31:12 +02:00
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
2015-06-13 01:42:51 +02:00
|
|
|
$image_generated = $image->ScaleWidth(200);
|
2013-04-15 12:17:00 +02:00
|
|
|
$p = $image_generated->getFullPath();
|
|
|
|
$this->assertTrue(file_exists($p), 'Resized image exists after creation call');
|
|
|
|
$this->assertEquals(1, $image->regenerateFormattedImages(), 'Cached images were regenerated correct');
|
2014-03-30 08:51:38 +02:00
|
|
|
$this->assertEquals($image_generated->getWidth(), 200,
|
|
|
|
'Resized image has correct width after regeneration call');
|
2013-04-15 12:17:00 +02:00
|
|
|
$this->assertTrue(file_exists($p), 'Resized image exists after regeneration call');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-02 19:38:02 +02:00
|
|
|
/**
|
|
|
|
* Test that propertes from the source Image are inherited by resampled images
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-06-10 01:16:11 +02:00
|
|
|
/**
|
|
|
|
* Tests that cached images are regenerated properly after a cached file is renamed with new arguments
|
|
|
|
* ToDo: This doesn't seem like something that is worth testing - what is the point of this?
|
|
|
|
*/
|
2013-04-15 12:17:00 +02:00
|
|
|
public function testRegenerateImagesWithRenaming() {
|
2015-08-17 18:31:12 +02:00
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
2015-06-13 01:42:51 +02:00
|
|
|
$image_generated = $image->ScaleWidth(200);
|
2013-04-15 12:17:00 +02:00
|
|
|
$p = $image_generated->getFullPath();
|
|
|
|
$this->assertTrue(file_exists($p), 'Resized image exists after creation call');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-15 12:17:00 +02:00
|
|
|
// Encoding of the arguments is duplicated from cacheFilename
|
2015-07-28 03:54:49 +02:00
|
|
|
$oldArgumentString = Convert::base64url_encode(array(200));
|
|
|
|
$newArgumentString = Convert::base64url_encode(array(300));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-15 12:17:00 +02:00
|
|
|
$newPath = str_replace($oldArgumentString, $newArgumentString, $p);
|
2015-08-17 18:31:12 +02:00
|
|
|
if(!file_exists(dirname($newPath))) mkdir(dirname($newPath));
|
2013-04-15 12:17:00 +02:00
|
|
|
$newRelative = str_replace($oldArgumentString, $newArgumentString, $image_generated->getFileName());
|
|
|
|
rename($p, $newPath);
|
2015-06-10 01:16:11 +02:00
|
|
|
$this->assertFalse(file_exists($p), 'Resized image does not exist at old path after renaming');
|
|
|
|
$this->assertTrue(file_exists($newPath), 'Resized image exists at new path after renaming');
|
2014-03-30 08:51:38 +02:00
|
|
|
$this->assertEquals(1, $image->regenerateFormattedImages(),
|
|
|
|
'Cached images were regenerated in the right number');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-15 12:17:00 +02:00
|
|
|
$image_generated_2 = new Image_Cached($newRelative);
|
|
|
|
$this->assertEquals(300, $image_generated_2->getWidth(), 'Cached image was regenerated with correct width');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGeneratedImageDeletion() {
|
2015-08-17 18:31:12 +02:00
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
2015-06-13 01:42:51 +02:00
|
|
|
$image_generated = $image->ScaleWidth(200);
|
2011-10-31 18:16:03 +01:00
|
|
|
$p = $image_generated->getFullPath();
|
2013-04-06 12:48:00 +02:00
|
|
|
$this->assertTrue(file_exists($p), 'Resized image exists after creation call');
|
|
|
|
$numDeleted = $image->deleteFormattedImages();
|
|
|
|
$this->assertEquals(1, $numDeleted, 'Expected one image to be deleted, but deleted ' . $numDeleted . ' images');
|
|
|
|
$this->assertFalse(file_exists($p), 'Resized image not existing after deletion call');
|
2011-10-31 18:16:03 +01:00
|
|
|
}
|
2013-08-26 10:16:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that generated images with multiple image manipulations are all deleted
|
|
|
|
*/
|
|
|
|
public function testMultipleGenerateManipulationCallsImageDeletion() {
|
2015-08-17 18:31:12 +02:00
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
2013-08-26 10:16:42 +02:00
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
$firstImage = $image->ScaleWidth(200);
|
2013-08-26 10:16:42 +02:00
|
|
|
$firstImagePath = $firstImage->getFullPath();
|
|
|
|
$this->assertTrue(file_exists($firstImagePath));
|
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
$secondImage = $firstImage->ScaleHeight(100);
|
2013-08-26 10:16:42 +02:00
|
|
|
$secondImagePath = $secondImage->getFullPath();
|
|
|
|
$this->assertTrue(file_exists($secondImagePath));
|
|
|
|
|
|
|
|
$image->deleteFormattedImages();
|
|
|
|
$this->assertFalse(file_exists($firstImagePath));
|
|
|
|
$this->assertFalse(file_exists($secondImagePath));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests path properties of cached images with multiple image manipulations
|
|
|
|
*/
|
|
|
|
public function testPathPropertiesCachedImage() {
|
2015-08-17 18:31:12 +02:00
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
2015-06-13 01:42:51 +02:00
|
|
|
$firstImage = $image->ScaleWidth(200);
|
2013-08-26 10:16:42 +02:00
|
|
|
$firstImagePath = $firstImage->getRelativePath();
|
|
|
|
$this->assertEquals($firstImagePath, $firstImage->Filename);
|
|
|
|
|
2015-06-13 01:42:51 +02:00
|
|
|
$secondImage = $firstImage->ScaleHeight(100);
|
2013-08-26 10:16:42 +02:00
|
|
|
$secondImagePath = $secondImage->getRelativePath();
|
|
|
|
$this->assertEquals($secondImagePath, $secondImage->Filename);
|
|
|
|
}
|
2014-07-17 11:54:23 +02:00
|
|
|
|
2015-08-17 18:31:12 +02:00
|
|
|
/**
|
|
|
|
* Tests the static function Image::strip_resampled_prefix, to ensure that
|
|
|
|
* the original filename can be extracted from the path of transformed images,
|
|
|
|
* both in current and previous formats
|
|
|
|
*/
|
|
|
|
public function testStripResampledPrefix() {
|
|
|
|
$orig_image = $this->objFromFixture('Image', 'imageWithoutTitleContainingDots');
|
|
|
|
|
|
|
|
// current format (3.3+). Example:
|
|
|
|
// assets/ImageTest/_resampled/ScaleHeightWzIwMF0=/ScaleWidthWzQwMF0=/test.image.with.dots.png;
|
|
|
|
$firstImage = $orig_image->ScaleWidth(200);
|
|
|
|
$secondImage = $firstImage->ScaleHeight(200);
|
|
|
|
$paths_1 = $firstImage->Filename;
|
|
|
|
$paths_2 = $secondImage->Filename;
|
|
|
|
|
|
|
|
// 3.2 format (did not work for multiple transformations)
|
|
|
|
$paths_3 = 'assets/ImageTest/_resampled/ScaleHeightWzIwMF0=-test.image.with.dots.png';
|
|
|
|
|
|
|
|
// 3.1 (and earlier) format (did not work for multiple transformations)
|
|
|
|
$paths_4 = 'assets/ImageTest/_resampled/ScaleHeight200-test.image.with.dots.png';
|
|
|
|
|
|
|
|
$this->assertEquals($orig_image->Filename, Image::strip_resampled_prefix($paths_1));
|
|
|
|
$this->assertEquals($orig_image->Filename, Image::strip_resampled_prefix($paths_2));
|
|
|
|
$this->assertEquals($orig_image->Filename, Image::strip_resampled_prefix($paths_3));
|
|
|
|
$this->assertEquals($orig_image->Filename, Image::strip_resampled_prefix($paths_4));
|
|
|
|
}
|
|
|
|
|
2014-07-17 11:54:23 +02:00
|
|
|
/**
|
|
|
|
* Test all generate methods
|
|
|
|
*/
|
|
|
|
public function testGenerateMethods() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
|
|
|
$generateMethods = $this->getGenerateMethods();
|
|
|
|
|
|
|
|
// test each generate method
|
|
|
|
foreach ($generateMethods as $method) {
|
|
|
|
$generatedImage = $image->$method(333, 333, 'FFFFFF');
|
|
|
|
$this->assertFileExists(
|
|
|
|
$generatedImage->getFullPath(),
|
|
|
|
'Formatted ' . $method . ' image exists'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test deleteFormattedImages() against all generate methods
|
|
|
|
*/
|
|
|
|
public function testDeleteFormattedImages() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
|
|
|
$generateMethods = $this->getGenerateMethods();
|
|
|
|
|
|
|
|
// get paths for each generate method
|
|
|
|
$paths = array();
|
|
|
|
foreach ($generateMethods as $method) {
|
|
|
|
$generatedImage = $image->$method(333, 333, 'FFFFFF');
|
|
|
|
$paths[$method] = $generatedImage->getFullPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete formatted images
|
|
|
|
$image->deleteFormattedImages();
|
|
|
|
|
|
|
|
// test that all formatted images are deleted
|
|
|
|
foreach ($paths as $method => $path) {
|
|
|
|
$this->assertFalse(
|
|
|
|
file_exists($path),
|
|
|
|
'Formatted ' . $method . ' image does not exist'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $custom include methods added dynamically at runtime
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getGenerateMethods($custom = true) {
|
|
|
|
$generateMethods = array();
|
|
|
|
$methodNames = Image::create()->allMethodNames($custom);
|
|
|
|
|
|
|
|
foreach ($methodNames as $methodName) {
|
|
|
|
if (substr($methodName, 0, 8) == 'generate' && $methodName != 'generateformattedimage') {
|
|
|
|
$format = substr($methodName, 8);
|
|
|
|
$generateMethods[] = $format;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $generateMethods;
|
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|