Skip to content

Commit 00cf519

Browse files
committed
test: add GDHandler unit tests for save and security coverage
1 parent b635434 commit 00cf519

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

‎tests/system/Images/GDHandlerTest.php‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
use CodeIgniter\Test\CIUnitTestCase;
2020
use org\bovigo\vfs\vfsStream;
2121
use org\bovigo\vfs\vfsStreamDirectory;
22+
use org\bovigo\vfs\vfsStreamFile;
2223
use PHPUnit\Framework\Attributes\Group;
24+
use Throwable;
2325

2426
/**
2527
* Unit testing for the GD image handler.
@@ -463,4 +465,75 @@ public function testClearMetadataReturnsSelf(): void
463465

464466
$this->assertSame($this->handler, $result);
465467
}
468+
469+
public function testSaveFailed(): void
470+
{
471+
foreach (['gif', 'jpeg', 'png', 'webp'] as $type) {
472+
if ($type === 'webp' && ! function_exists('imagecreatefromwebp')) {
473+
$this->markTestSkipped('webp is not supported.');
474+
}
475+
476+
$this->handler->withFile($this->origin . 'ci-logo.' . $type);
477+
$this->handler->getResource(); // make sure resource is loaded
478+
479+
try {
480+
$this->handler->save($this->start . 'wontwork/ci-logo.' . $type);
481+
$this->fail('Exception should have been thrown for ' . $type);
482+
} catch (Throwable $e) {
483+
$this->assertInstanceOf(ImageException::class, $e);
484+
$this->assertSame(lang('Images.saveFailed'), $e->getMessage());
485+
}
486+
}
487+
}
488+
489+
public function testImageCopyTargetWithMaxQuality(): void
490+
{
491+
foreach (['gif', 'jpeg', 'png', 'webp'] as $type) {
492+
if ($type === 'webp' && ! function_exists('imagecreatefromwebp')) {
493+
$this->markTestSkipped('webp is not supported.');
494+
}
495+
496+
$this->handler->withFile($this->origin . 'ci-logo.' . $type);
497+
$this->assertTrue($this->handler->save($this->start . 'work/ci-logo.' . $type, 100));
498+
$this->assertTrue($this->root->hasChild('work/ci-logo.' . $type));
499+
500+
/** @var vfsStreamFile $child */
501+
$child = $this->root->getChild('work/ci-logo.' . $type);
502+
503+
$this->assertSame(
504+
file_get_contents($this->origin . 'ci-logo.' . $type),
505+
$child->getContent(),
506+
);
507+
}
508+
}
509+
510+
public function testSaveUnsupportedImageType(): void
511+
{
512+
$this->handler->withFile($this->path);
513+
514+
// Force an invalid image type to ensure proper exception handling
515+
$image = $this->handler->getFile();
516+
$image->imageType = 9999;
517+
518+
$this->expectException(ImageException::class);
519+
$this->expectExceptionMessage(lang('Images.unsupportedImageCreate'));
520+
521+
// save() should throw an exception instead of fatal error in GD
522+
$this->handler->save($this->start . 'work/ci-logo.jpg');
523+
}
524+
525+
public function testProcessUnsupportedImageType(): void
526+
{
527+
$this->handler->withFile($this->path);
528+
529+
// Force an invalid image type to trigger getImageResource default case
530+
$image = $this->handler->getFile();
531+
$image->imageType = 9999;
532+
533+
$this->expectException(ImageException::class);
534+
$this->expectExceptionMessage('Ima');
535+
536+
// Calling any process function that triggers ensureResource/createImage
537+
$this->handler->resize(10, 10);
538+
}
466539
}

0 commit comments

Comments
 (0)