Skip to content

Commit 7c2f6a5

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

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

‎tests/system/Images/GDHandlerTest.php‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\Images;
1515

16+
use org\bovigo\vfs\vfsStreamFile;
1617
use CodeIgniter\Config\Services;
1718
use CodeIgniter\Images\Exceptions\ImageException;
1819
use CodeIgniter\Images\Handlers\BaseHandler;
@@ -463,4 +464,74 @@ public function testClearMetadataReturnsSelf(): void
463464

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

0 commit comments

Comments
 (0)