]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/ThumbnailerTest.php
Feature: add a Server administration page
[github/shaarli/Shaarli.git] / tests / ThumbnailerTest.php
1 <?php
2
3 namespace Shaarli;
4
5 use Shaarli\Config\ConfigManager;
6 use WebThumbnailer\Application\ConfigManager as WTConfigManager;
7
8 /**
9 * Class ThumbnailerTest
10 *
11 * We only make 1 thumb test because:
12 *
13 * 1. the thumbnailer library is itself tested
14 * 2. we don't want to make too many external requests during the tests
15 */
16 class ThumbnailerTest extends TestCase
17 {
18 const WIDTH = 190;
19
20 const HEIGHT = 210;
21
22 /**
23 * @var Thumbnailer;
24 */
25 protected $thumbnailer;
26
27 /**
28 * @var ConfigManager
29 */
30 protected $conf;
31
32 protected function setUp(): void
33 {
34 $this->conf = new ConfigManager('tests/utils/config/configJson');
35 $this->conf->set('thumbnails.mode', Thumbnailer::MODE_ALL);
36 $this->conf->set('thumbnails.width', self::WIDTH);
37 $this->conf->set('thumbnails.height', self::HEIGHT);
38 $this->conf->set('dev.debug', true);
39
40 $this->thumbnailer = new Thumbnailer($this->conf);
41 // cache files in the sandbox
42 WTConfigManager::addFile('tests/utils/config/wt.json');
43 }
44
45 protected function tearDown(): void
46 {
47 $this->rrmdirContent('sandbox/');
48 }
49
50 /**
51 * Test a thumbnail with a custom size in 'all' mode.
52 */
53 public function testThumbnailAllValid()
54 {
55 $thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
56 $this->assertNotFalse($thumb);
57 $image = imagecreatefromstring(file_get_contents($thumb));
58 $this->assertEquals(self::WIDTH, imagesx($image));
59 $this->assertEquals(self::HEIGHT, imagesy($image));
60 }
61
62 /**
63 * Test a thumbnail with a custom size in 'common' mode.
64 */
65 public function testThumbnailCommonValid()
66 {
67 $this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON);
68 $thumb = $this->thumbnailer->get('https://imgur.com/jlFgGpe');
69 $this->assertNotFalse($thumb);
70 $image = imagecreatefromstring(file_get_contents($thumb));
71 $this->assertEquals(self::WIDTH, imagesx($image));
72 $this->assertEquals(self::HEIGHT, imagesy($image));
73 }
74
75 /**
76 * Test a thumbnail in 'common' mode which isn't include in common websites.
77 */
78 public function testThumbnailCommonInvalid()
79 {
80 $this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON);
81 $thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
82 $this->assertFalse($thumb);
83 }
84
85 /**
86 * Test a thumbnail that can't be retrieved.
87 */
88 public function testThumbnailNotValid()
89 {
90 $oldlog = ini_get('error_log');
91 ini_set('error_log', '/dev/null');
92
93 $thumbnailer = new Thumbnailer(new ConfigManager());
94 $thumb = $thumbnailer->get('nope');
95 $this->assertFalse($thumb);
96
97 ini_set('error_log', $oldlog);
98 }
99
100 protected function rrmdirContent($dir)
101 {
102 if (is_dir($dir)) {
103 $objects = scandir($dir);
104 foreach ($objects as $object) {
105 if ($object != "." && $object != "..") {
106 if (is_dir($dir."/".$object)) {
107 $this->rrmdirContent($dir."/".$object);
108 } else {
109 unlink($dir."/".$object);
110 }
111 }
112 }
113 }
114 }
115 }