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 {
|
2010-12-20 03:19:17 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'ImageTest.yml';
|
2012-10-25 00:28:39 +02:00
|
|
|
|
|
|
|
protected $origBackend;
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setUp() {
|
2012-10-25 00:28:39 +02:00
|
|
|
if(get_class($this) == "ImageTest")
|
|
|
|
$this->skipTest = true;
|
|
|
|
|
2010-12-20 03:19:17 +01:00
|
|
|
parent::setUp();
|
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
if($this->skipTest)
|
|
|
|
return;
|
|
|
|
|
|
|
|
$this->origBackend = Image::get_backend();
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
foreach($folderIDs as $folderID) {
|
|
|
|
$folder = DataObject::get_by_id('Folder', $folderID);
|
|
|
|
|
|
|
|
if(!file_exists(BASE_PATH."/$folder->Filename")) mkdir(BASE_PATH."/$folder->Filename");
|
|
|
|
}
|
2012-10-25 00:28:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
Image::set_backend($this->origBackend);
|
|
|
|
|
|
|
|
/* 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");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the test folders that we've crated */
|
|
|
|
$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");
|
|
|
|
}
|
2010-12-20 03:19:17 +01:00
|
|
|
}
|
2012-10-25 00:28:39 +02:00
|
|
|
|
|
|
|
parent::tearDown();
|
2010-12-20 03:19:17 +01:00
|
|
|
}
|
2009-05-24 14:16:17 +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');
|
2012-09-26 23:34:00 +02:00
|
|
|
$expected = '<img src="' . Director::baseUrl()
|
|
|
|
. 'assets/ImageTest/test_image.png" alt="This is a image Title" />';
|
2009-05-24 14:16:17 +02:00
|
|
|
$actual = $image->getTag();
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
2010-10-15 05:17:14 +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');
|
|
|
|
|
|
|
|
$imageFirst = $image->SetWidth(200);
|
|
|
|
$this->assertNotNull($imageFirst);
|
|
|
|
$expected = 200;
|
|
|
|
$actual = $imageFirst->getWidth();
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
|
|
|
|
$imageSecond = $imageFirst->setHeight(100);
|
|
|
|
$this->assertNotNull($imageSecond);
|
|
|
|
$expected = 100;
|
|
|
|
$actual = $imageSecond->getHeight();
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
2011-10-31 18:16:03 +01: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() {
|
2013-08-21 08:54:05 +02:00
|
|
|
|
2013-03-21 00:15:28 +01:00
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
|
|
|
$this->assertTrue($image->isSize(300, 300));
|
|
|
|
|
|
|
|
// Set width to 50 pixels
|
|
|
|
$imageSetWidth = $image->SetWidth(300);
|
|
|
|
$this->assertEquals($imageSetWidth->getWidth(), 300);
|
|
|
|
$this->assertEquals($image->Filename, $imageSetWidth->Filename);
|
|
|
|
|
|
|
|
// Set height to 300 pixels
|
|
|
|
$imageSetHeight = $image->SetHeight(300);
|
|
|
|
$this->assertEquals($imageSetHeight->getHeight(), 300);
|
|
|
|
$this->assertEquals($image->Filename, $imageSetHeight->Filename);
|
|
|
|
|
|
|
|
// Crop image to 300 x 300
|
|
|
|
$imageCropped = $image->CroppedImage(300, 300);
|
|
|
|
$this->assertTrue($imageCropped->isSize(300, 300));
|
|
|
|
$this->assertEquals($image->Filename, $imageCropped->Filename);
|
|
|
|
|
|
|
|
// Resize (padded) to 300 x 300
|
|
|
|
$imageSized = $image->SetSize(300, 300);
|
|
|
|
$this->assertTrue($imageSized->isSize(300, 300));
|
|
|
|
$this->assertEquals($image->Filename, $imageSized->Filename);
|
|
|
|
|
|
|
|
// Padded image 300 x 300 (same as above)
|
|
|
|
$imagePadded = $image->PaddedImage(300, 300);
|
|
|
|
$this->assertTrue($imagePadded->isSize(300, 300));
|
|
|
|
$this->assertEquals($image->Filename, $imagePadded->Filename);
|
|
|
|
|
|
|
|
// Resized (stretched) to 300 x 300
|
|
|
|
$imageStretched = $image->ResizedImage(300, 300);
|
|
|
|
$this->assertTrue($imageStretched->isSize(300, 300));
|
|
|
|
$this->assertEquals($image->Filename, $imageStretched->Filename);
|
|
|
|
|
|
|
|
// SetRatioSize (various options)
|
|
|
|
$imageSetRatioSize = $image->SetRatioSize(300, 600);
|
|
|
|
$this->assertTrue($imageSetRatioSize->isSize(300, 300));
|
|
|
|
$this->assertEquals($image->Filename, $imageSetRatioSize->Filename);
|
|
|
|
$imageSetRatioSize = $image->SetRatioSize(600, 300);
|
|
|
|
$this->assertTrue($imageSetRatioSize->isSize(300, 300));
|
|
|
|
$this->assertEquals($image->Filename, $imageSetRatioSize->Filename);
|
|
|
|
$imageSetRatioSize = $image->SetRatioSize(300, 300);
|
|
|
|
$this->assertTrue($imageSetRatioSize->isSize(300, 300));
|
|
|
|
$this->assertEquals($image->Filename, $imageSetRatioSize->Filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testImageResize() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
|
|
|
$this->assertTrue($image->isSize(300, 300));
|
|
|
|
|
|
|
|
// Test normal resize
|
|
|
|
$resized = $image->SetSize(150, 100);
|
|
|
|
$this->assertTrue($resized->isSize(150, 100));
|
|
|
|
|
|
|
|
// Test cropped resize
|
|
|
|
$cropped = $image->CroppedImage(100, 200);
|
|
|
|
$this->assertTrue($cropped->isSize(100, 200));
|
|
|
|
|
|
|
|
// Test padded resize
|
|
|
|
$padded = $image->PaddedImage(200, 100);
|
|
|
|
$this->assertTrue($padded->isSize(200, 100));
|
|
|
|
|
|
|
|
// Test SetRatioSize
|
|
|
|
$ratio = $image->SetRatioSize(80, 160);
|
|
|
|
$this->assertTrue($ratio->isSize(80, 80));
|
|
|
|
}
|
2013-04-15 12:17:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException PHPUnit_Framework_Error
|
|
|
|
*/
|
|
|
|
public function testGenerateImageWithInvalidParameters() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
|
|
|
$image->setHeight('String');
|
|
|
|
$image->PaddedImage(600,600,'XXXXXX');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCacheFilename() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
|
|
|
$imageFirst = $image->SetSize(200,200);
|
|
|
|
$imageFilename = $imageFirst->getFullPath();
|
|
|
|
// Encoding of the arguments is duplicated from cacheFilename
|
|
|
|
$neededPart = 'SetSize' . base64_encode(json_encode(array(200,200)));
|
|
|
|
$this->assertContains($neededPart, $imageFilename, 'Filename for cached image is correctly generated');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultipleGenerateManipulationCalls_Regeneration() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
|
|
|
|
$folder = new SS_FileFinder();
|
|
|
|
|
|
|
|
$imageFirst = $image->SetSize(200,200);
|
|
|
|
$this->assertNotNull($imageFirst);
|
|
|
|
$expected = 200;
|
|
|
|
$actual = $imageFirst->getWidth();
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
|
|
|
|
$imageSecond = $imageFirst->setHeight(100);
|
|
|
|
$this->assertNotNull($imageSecond);
|
|
|
|
$expected = 100;
|
|
|
|
$actual = $imageSecond->getHeight();
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
|
|
|
|
$imageThird = $imageSecond->PaddedImage(600,600,'0F0F0F');
|
|
|
|
// Encoding of the arguments is duplicated from cacheFilename
|
|
|
|
$argumentString = base64_encode(json_encode(array(600,600,'0F0F0F')));
|
|
|
|
$this->assertNotNull($imageThird);
|
|
|
|
$this->assertContains($argumentString, $imageThird->getFullPath(), 'Image contains background color for padded resizement');
|
|
|
|
|
|
|
|
$imageThirdPath = $imageThird->getFullPath();
|
|
|
|
$filesInFolder = $folder->find(dirname($imageThirdPath));
|
|
|
|
$this->assertEquals(3, count($filesInFolder), 'Image folder contains only the expected number of images before regeneration');
|
|
|
|
|
|
|
|
$hash = md5_file($imageThirdPath);
|
|
|
|
$this->assertEquals(3, $image->regenerateFormattedImages(), 'Cached images were regenerated in the right number');
|
|
|
|
$this->assertEquals($hash, md5_file($imageThirdPath), 'Regeneration of third image is correct');
|
|
|
|
|
|
|
|
/* Check that no other images exist, to ensure that the regeneration did not create other images */
|
|
|
|
$this->assertEquals($filesInFolder, $folder->find(dirname($imageThirdPath)), 'Image folder contains only the expected image files after regeneration');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRegenerateImages() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithMetacharacters');
|
|
|
|
$image_generated = $image->SetWidth(200);
|
|
|
|
$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');
|
|
|
|
$this->assertEquals($image_generated->getWidth(), 200, 'Resized image has correct width after regeneration call');
|
|
|
|
$this->assertTrue(file_exists($p), 'Resized image exists after regeneration call');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRegenerateImagesWithRenaming() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithMetacharacters');
|
|
|
|
$image_generated = $image->SetWidth(200);
|
|
|
|
$p = $image_generated->getFullPath();
|
|
|
|
$this->assertTrue(file_exists($p), 'Resized image exists after creation call');
|
|
|
|
|
|
|
|
// Encoding of the arguments is duplicated from cacheFilename
|
|
|
|
$oldArgumentString = base64_encode(json_encode(array(200)));
|
|
|
|
$newArgumentString = base64_encode(json_encode(array(300)));
|
|
|
|
|
|
|
|
$newPath = str_replace($oldArgumentString, $newArgumentString, $p);
|
|
|
|
$newRelative = str_replace($oldArgumentString, $newArgumentString, $image_generated->getFileName());
|
|
|
|
rename($p, $newPath);
|
|
|
|
$this->assertFalse(file_exists($p), 'Resized image does not exist after movement call under old name');
|
|
|
|
$this->assertTrue(file_exists($newPath), 'Resized image exists after movement call under new name');
|
|
|
|
$this->assertEquals(1, $image->regenerateFormattedImages(), 'Cached images were regenerated in the right number');
|
|
|
|
|
|
|
|
$image_generated_2 = new Image_Cached($newRelative);
|
|
|
|
$this->assertEquals(300, $image_generated_2->getWidth(), 'Cached image was regenerated with correct width');
|
|
|
|
}
|
2013-03-21 00:15:28 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGeneratedImageDeletion() {
|
2011-10-31 18:16:03 +01:00
|
|
|
$image = $this->objFromFixture('Image', 'imageWithMetacharacters');
|
|
|
|
$image_generated = $image->SetWidth(200);
|
|
|
|
$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() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithMetacharacters');
|
|
|
|
|
|
|
|
$firstImage = $image->SetWidth(200);
|
|
|
|
$firstImagePath = $firstImage->getFullPath();
|
|
|
|
$this->assertTrue(file_exists($firstImagePath));
|
|
|
|
|
|
|
|
$secondImage = $firstImage->SetHeight(100);
|
|
|
|
$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() {
|
|
|
|
$image = $this->objFromFixture('Image', 'imageWithMetacharacters');
|
|
|
|
$firstImage = $image->SetWidth(200);
|
|
|
|
$firstImagePath = $firstImage->getRelativePath();
|
|
|
|
$this->assertEquals($firstImagePath, $firstImage->Filename);
|
|
|
|
|
|
|
|
$secondImage = $firstImage->SetHeight(100);
|
|
|
|
$secondImagePath = $secondImage->getRelativePath();
|
|
|
|
$this->assertEquals($secondImagePath, $secondImage->Filename);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|