]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/ThumbnailerTest.php
Merge pull request #1698 from ArthurHoaro/feature/plugins-search-filter
[github/shaarli/Shaarli.git] / tests / ThumbnailerTest.php
CommitLineData
1b93137e
A
1<?php
2
e85b7a05
A
3namespace Shaarli;
4
e85b7a05
A
5use Shaarli\Config\ConfigManager;
6use WebThumbnailer\Application\ConfigManager as WTConfigManager;
1b93137e
A
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 */
e85b7a05 16class ThumbnailerTest extends TestCase
1b93137e 17{
e85b7a05
A
18 const WIDTH = 190;
19
20 const HEIGHT = 210;
21
1b93137e 22 /**
e85b7a05 23 * @var Thumbnailer;
1b93137e 24 */
e85b7a05
A
25 protected $thumbnailer;
26
b302b3c5
A
27 /**
28 * @var ConfigManager
29 */
30 protected $conf;
31
8f60e120 32 protected function setUp(): void
1b93137e 33 {
b302b3c5
A
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);
e85b7a05 39
b302b3c5 40 $this->thumbnailer = new Thumbnailer($this->conf);
e85b7a05
A
41 // cache files in the sandbox
42 WTConfigManager::addFile('tests/utils/config/wt.json');
43 }
44
8f60e120 45 protected function tearDown(): void
e85b7a05
A
46 {
47 $this->rrmdirContent('sandbox/');
48 }
1b93137e 49
e85b7a05 50 /**
b302b3c5 51 * Test a thumbnail with a custom size in 'all' mode.
e85b7a05 52 */
b302b3c5 53 public function testThumbnailAllValid()
e85b7a05
A
54 {
55 $thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
1b93137e
A
56 $this->assertNotFalse($thumb);
57 $image = imagecreatefromstring(file_get_contents($thumb));
e85b7a05
A
58 $this->assertEquals(self::WIDTH, imagesx($image));
59 $this->assertEquals(self::HEIGHT, imagesy($image));
1b93137e
A
60 }
61
b302b3c5
A
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
1b93137e
A
85 /**
86 * Test a thumbnail that can't be retrieved.
1b93137e
A
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 }
e85b7a05 99
067c2dd8
V
100 protected function rrmdirContent($dir)
101 {
e85b7a05
A
102 if (is_dir($dir)) {
103 $objects = scandir($dir);
104 foreach ($objects as $object) {
105 if ($object != "." && $object != "..") {
067c2dd8 106 if (is_dir($dir."/".$object)) {
e85b7a05 107 $this->rrmdirContent($dir."/".$object);
067c2dd8 108 } else {
e85b7a05 109 unlink($dir."/".$object);
067c2dd8 110 }
e85b7a05
A
111 }
112 }
113 }
114 }
1b93137e 115}