aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/includes/PhotodriverTest.php
blob: db98835897dc9f13198f8f8e7e89963ff8fcf956 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php

namespace Zotlabs\Tests\Unit\includes;

use Zotlabs\Tests\Unit\UnitTestCase;

/**
 * @brief Unit Test cases for include/photo/photo_driver.php file.
 */
class PhotodriverTest extends UnitTestCase {

	public function testPhotofactoryReturnsNullForUnsupportedType() {
		$photo = \photo_factory('', 'image/bmp');
		$this->assertNull($photo);
	}

	public function testPhotofactoryReturnsPhotogdIfConfigIgnore_imagickIsSet() {
		\Zotlabs\Lib\Config::Set('system', 'ignore_imagick', true);

		$photo = \photo_factory(file_get_contents('images/hz-16.png'), 'image/png');
		$this->assertInstanceOf('Zotlabs\Photo\PhotoGd', $photo);
	}

	// Helper to create a temporary image file
	private function createTempImage($type = 'jpeg'): string
	{
		$tmp = tempnam(sys_get_temp_dir(), 'img');
		switch ($type) {
			case 'png':
				$im = imagecreatetruecolor(10, 10);
				imagepng($im, $tmp);
				imagedestroy($im);
				break;
			case 'jpeg':
			default:
				$im = imagecreatetruecolor(10, 10);
				imagejpeg($im, $tmp);
				imagedestroy($im);
				break;
		}
		return $tmp;
	}

	public function testGuessImageTypeFromRawData()
	{
		$filename = 'irrelevant';
		$data = [
			'body' => file_get_contents($this->createTempImage('jpeg'))
		];
		$result = guess_image_type($filename, $data);
		$this->assertEquals('image/jpeg', $result);
	}

	public function testGuessImageTypeFromLocalFile()
	{
		$file = $this->createTempImage('png');
		$result = guess_image_type($file);
		$this->assertEquals('image/png', $result);
		unlink($file);
	}

	public function testGuessImageTypeFromHeaders()
	{
		$filename = 'irrelevant';
		$data = [
			'header' => "Content-Type: image/jpeg\nOther: value"
		];
		$result = guess_image_type($filename, $data);
		$this->assertEquals('image/jpeg', $result);
	}

	public function testGuessImageTypeUnknownTypeReturnsNull()
	{
		$filename = 'not_an_image.txt';
		$data = [
			'body' => 'not an image'
		];
		$result = guess_image_type($filename, $data);
		$this->assertNull($result);
	}
}