View source
<?php
namespace Drupal\Tests\imageapi_optimize\Kernel;
use Drupal\imageapi_optimize\Entity\ImageAPIOptimizePipeline;
use Drupal\KernelTests\KernelTestBase;
class ImageOptimizePipelineTest extends KernelTestBase {
public static $modules = [
'system',
'imageapi_optimize',
'imageapi_optimize_module_test',
];
public function testValidImagePipeline() {
$original_image_data = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg==');
$image_uri = $this
->createUri('Файл для тестирования ' . $this
->randomMachineName() . '.png', $original_image_data);
$this
->assertStringEqualsFile($image_uri, $original_image_data, 'Image data written to file successfully');
$pipeline = ImageAPIOptimizePipeline::create([
'name' => 'test',
]);
$pipeline
->addProcessor([
'id' => 'imageapi_optimize_module_test_blackpng',
]);
$pipeline
->applyToImage($image_uri);
$this
->assertStringEqualsFile($image_uri, base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII='));
}
public function testFailureImagePipeline() {
$original_image_data = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg==');
$image_uri = $this
->createUri('Файл для тестирования ' . $this
->randomMachineName() . '.png', $original_image_data);
$this
->assertStringEqualsFile($image_uri, $original_image_data, 'Image data written to file successfully.');
$pipeline = ImageAPIOptimizePipeline::create([
'name' => 'test',
]);
$pipeline
->addProcessor([
'id' => 'imageapi_optimize_module_test_failedgreenpng',
]);
$pipeline
->applyToImage($image_uri);
$this
->assertStringEqualsFile($image_uri, $original_image_data, 'Original image preserved.');
}
public function testCompoundFailureImagePipeline() {
$original_image_data = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg==');
$image_uri = $this
->createUri('Файл для тестирования ' . $this
->randomMachineName() . '.png', $original_image_data);
$this
->assertStringEqualsFile($image_uri, $original_image_data, 'Image data written to file successfully.');
$pipeline = ImageAPIOptimizePipeline::create([
'name' => 'test',
]);
$pipeline
->addProcessor([
'id' => 'imageapi_optimize_module_test_appendcharacters',
]);
$pipeline
->addProcessor([
'id' => 'imageapi_optimize_module_test_failedgreenpng',
]);
$pipeline
->addProcessor([
'id' => 'imageapi_optimize_module_test_appendcharacters',
]);
$pipeline
->applyToImage($image_uri);
$this
->assertFileEqualsString($original_image_data . str_repeat('1', 20), $image_uri);
}
public function testCompoundImagePipeline() {
$original_image_data = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg==');
$image_uri = $this
->createUri('Файл для тестирования ' . $this
->randomMachineName() . '.png', $original_image_data);
$this
->assertStringEqualsFile($image_uri, $original_image_data, 'Image data written to file successfully.');
$pipeline = ImageAPIOptimizePipeline::create([
'name' => 'test',
]);
$pipeline
->addProcessor([
'id' => 'imageapi_optimize_module_test_appendcharacters',
]);
$pipeline
->addProcessor([
'id' => 'imageapi_optimize_module_test_appendcharacters',
]);
$pipeline
->applyToImage($image_uri);
$this
->assertFileEqualsString($original_image_data . str_repeat('1', 20), $image_uri);
}
public static function assertFileEqualsString($extectedString, $actualFile, $message = '', $canonicalize = false, $ignoreCase = false) {
self::assertFileExists($actualFile, $message);
self::assertEquals($extectedString, file_get_contents($actualFile), $message, 0, 10, $canonicalize, $ignoreCase);
}
public function createUri($filepath = NULL, $contents = NULL, $scheme = NULL) {
if (!isset($filepath)) {
$filepath = 'Файл для тестирования ' . $this
->randomMachineName();
}
if (!isset($scheme)) {
$scheme = file_default_scheme();
}
$filepath = $scheme . '://' . $filepath;
if (!isset($contents)) {
$contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data.";
}
file_put_contents($filepath, $contents);
$this
->assertFileExists($filepath, t('The test file exists on the disk.'));
return $filepath;
}
public function testNonExistentImagePipeline() {
$image_uri = file_default_scheme() . '://Файл для тестирования ' . $this
->randomMachineName() . '.png';
$this
->assertFileNotExists($image_uri, t('The test file does not exist on the disk.'));
$pipeline = ImageAPIOptimizePipeline::create([
'name' => 'test',
]);
$pipeline
->addProcessor([
'id' => 'imageapi_optimize_module_test_appendcharacters',
]);
$result = $pipeline
->applyToImage($image_uri);
$this
->assertFalse($result, 'Image pipeline failed to apply gracefully.');
}
}