Skip to content

Commit

Permalink
MNT Updated thumbnail unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Dec 21, 2023
1 parent da4d16a commit 0d15880
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
16 changes: 5 additions & 11 deletions tests/php/GraphQL/FileTypeCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,22 @@ public function testThumbnail()
$image->setFromLocalFile(__DIR__.'/../Forms/fixtures/largeimage.png', 'TestImage.png');
$image->write();

// Image original is unset
$thumbnail = FileTypeResolver::resolveFileThumbnail($image, [], [], null);
$this->assertNull($thumbnail);

// Generate thumbnails by viewing this file's data
$assetAdmin->getObjectFromData($image, false);

// Image original is NOT unset - we generate as soon as we request information about the file.
// protected image should have inline thumbnail
$thumbnail = FileTypeResolver::resolveFileThumbnail($image, [], [], null);
$this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWAAAADr', $thumbnail);
$this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWAAAAEI', $thumbnail);

// public image should have url
$image->publishSingle();
$thumbnail = FileTypeResolver::resolveFileThumbnail($image, [], [], null);
$this->assertEquals('/assets/FileTypeCreatorTest/TestImage__FitMaxWzM1MiwyNjRd.png', $thumbnail);
$this->assertEquals('/assets/FileTypeCreatorTest/TestImage__FillWzM1MiwyNjRd.png', $thumbnail);

// Public assets can be set to inline
ThumbnailGenerator::config()->merge('thumbnail_links', [
AssetStore::VISIBILITY_PUBLIC => ThumbnailGenerator::INLINE,
]);
$thumbnail = FileTypeResolver::resolveFileThumbnail($image, [], [], null);
$this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWAAAADr', $thumbnail);
$this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWAAAAEI', $thumbnail);

// Protected assets can be set to url
// This uses protected asset adapter, so not direct asset link
Expand All @@ -79,6 +73,6 @@ public function testThumbnail()
]);
$image->doUnpublish();
$thumbnail = FileTypeResolver::resolveFileThumbnail($image, [], [], null);
$this->assertEquals('/assets/8cf6c65fa7/TestImage__FitMaxWzM1MiwyNjRd.png', $thumbnail);
$this->assertEquals('/assets/8cf6c65fa7/TestImage__FillWzM1MiwyNjRd.png', $thumbnail);
}
}
16 changes: 5 additions & 11 deletions tests/php/GraphQL/Legacy/FileTypeCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,22 @@ public function testThumbnail()
$image->setFromLocalFile(__DIR__.'/../../Forms/fixtures/largeimage.png', 'TestImage.png');
$image->write();

// Image original is unset
$thumbnail = $type->resolveThumbnailField($image, [], [], null);
$this->assertNull($thumbnail);

// Generate thumbnails by viewing this file's data
$assetAdmin->getObjectFromData($image, false);

// Image original is NOT unset - we generate as soon as we request information about the file.
// protected image should have inline thumbnail
$thumbnail = $type->resolveThumbnailField($image, [], [], null);
$this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWAAAADr', $thumbnail);
$this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWAAAAEI', $thumbnail);

// public image should have url
$image->publishSingle();
$thumbnail = $type->resolveThumbnailField($image, [], [], null);
$this->assertEquals('/assets/FileTypeCreatorTest/TestImage__FitMaxWzM1MiwyNjRd.png', $thumbnail);
$this->assertEquals('/assets/FileTypeCreatorTest/TestImage__FillWzM1MiwyNjRd.png', $thumbnail);

// Public assets can be set to inline
ThumbnailGenerator::config()->merge('thumbnail_links', [
AssetStore::VISIBILITY_PUBLIC => ThumbnailGenerator::INLINE,
]);
$thumbnail = $type->resolveThumbnailField($image, [], [], null);
$this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWAAAADr', $thumbnail);
$this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWAAAAEI', $thumbnail);

// Protected assets can be set to url
// This uses protected asset adapter, so not direct asset link
Expand All @@ -80,6 +74,6 @@ public function testThumbnail()
]);
$image->doUnpublish();
$thumbnail = $type->resolveThumbnailField($image, [], [], null);
$this->assertEquals('/assets/8cf6c65fa7/TestImage__FitMaxWzM1MiwyNjRd.png', $thumbnail);
$this->assertEquals('/assets/8cf6c65fa7/TestImage__FillWzM1MiwyNjRd.png', $thumbnail);
}
}
26 changes: 20 additions & 6 deletions tests/php/Model/ThumbnailGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testGenerateThumbnail()
// protected image should have inline thumbnail
$thumbnail = $generator->generateThumbnail($image, 100, 200);
$this->assertEquals(100, $thumbnail->getWidth());
$this->assertEquals(50, $thumbnail->getHeight()); // Note: Aspect ratio of original image retained
$this->assertEquals(200, $thumbnail->getHeight());

// Build non-image
$file = new File();
Expand All @@ -57,6 +57,20 @@ public function testGenerateThumbnail()
$this->assertNull($thumbnail);
}

public function testGenerateThumbnailFitMax()
{
ThumbnailGenerator::config()->set('method', 'FitMax');
$generator = new ThumbnailGenerator();
// Build image
$image = new Image();
$image->setFromLocalFile(__DIR__ . '/../Forms/fixtures/testimage.png', 'TestImage.png');
$image->write();

$thumbnail = $generator->generateThumbnail($image, 100, 200);
$this->assertEquals(100, $thumbnail->getWidth());
$this->assertEquals(50, $thumbnail->getHeight()); // Note: Aspect ratio of original image retained
}

public function testGenerateLink()
{
$generator = new ThumbnailGenerator();
Expand All @@ -82,7 +96,7 @@ public function testGenerateLink()

// protected image should have inline thumbnail
$thumbnail = $generator->generateThumbnailLink($image, 100, 200);
$this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAy', $thumbnail);
$this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAADI', $thumbnail);

// but not if it's too big
Config::nest();
Expand All @@ -92,20 +106,20 @@ public function testGenerateLink()
$this->assertNull($thumbnail);
// With graceful thumbnails, it should come back as a URL
$thumbnail = $generator->generateThumbnailLink($image, 100, 200, true);
$this->assertMatchesRegularExpression('#/assets/[A-Za-z0-9]+/TestImage__FitMaxWzEwMCwyMDBd\.png$#', $thumbnail);
$this->assertMatchesRegularExpression('#/assets/[A-Za-z0-9]+/TestImage__FillWzEwMCwyMDBd\.png$#', $thumbnail);
Config::unnest();

// public image should have url
$image->publishSingle();
$thumbnail = $generator->generateThumbnailLink($image, 100, 200);
$this->assertEquals('/assets/ThumbnailGeneratorTest/TestImage__FitMaxWzEwMCwyMDBd.png', $thumbnail);
$this->assertEquals('/assets/ThumbnailGeneratorTest/TestImage__FillWzEwMCwyMDBd.png', $thumbnail);

// Public assets can be set to inline
ThumbnailGenerator::config()->merge('thumbnail_links', [
AssetStore::VISIBILITY_PUBLIC => ThumbnailGenerator::INLINE,
]);
$thumbnail = $generator->generateThumbnailLink($image, 100, 200);
$this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAy', $thumbnail);
$this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAADI', $thumbnail);

// Protected assets can be set to url
// This uses protected asset adapter, so not direct asset link
Expand All @@ -114,6 +128,6 @@ public function testGenerateLink()
]);
$image->doUnpublish();
$thumbnail = $generator->generateThumbnailLink($image, 100, 200);
$this->assertEquals('/assets/906835357d/TestImage__FitMaxWzEwMCwyMDBd.png', $thumbnail);
$this->assertEquals('/assets/906835357d/TestImage__FillWzEwMCwyMDBd.png', $thumbnail);
}
}

0 comments on commit 0d15880

Please sign in to comment.