aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/ThumbnailerTest.php
blob: 5b6d6a4d9a96c104264a92dd3398669170d4be1e (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php

namespace Shaarli;

use PHPUnit\Framework\TestCase;
use Shaarli\Config\ConfigManager;
use WebThumbnailer\Application\ConfigManager as WTConfigManager;

/**
 * Class ThumbnailerTest
 *
 * We only make 1 thumb test because:
 *
 *   1. the thumbnailer library is itself tested
 *   2. we don't want to make too many external requests during the tests
 */
class ThumbnailerTest extends TestCase
{
    const WIDTH = 190;

    const HEIGHT = 210;

    /**
     * @var Thumbnailer;
     */
    protected $thumbnailer;

    /**
     * @var ConfigManager
     */
    protected $conf;

    protected function setUp(): void
    {
        $this->conf = new ConfigManager('tests/utils/config/configJson');
        $this->conf->set('thumbnails.mode', Thumbnailer::MODE_ALL);
        $this->conf->set('thumbnails.width', self::WIDTH);
        $this->conf->set('thumbnails.height', self::HEIGHT);
        $this->conf->set('dev.debug', true);

        $this->thumbnailer = new Thumbnailer($this->conf);
        // cache files in the sandbox
        WTConfigManager::addFile('tests/utils/config/wt.json');
    }

    protected function tearDown(): void
    {
        $this->rrmdirContent('sandbox/');
    }

    /**
     * Test a thumbnail with a custom size in 'all' mode.
     */
    public function testThumbnailAllValid()
    {
        $thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
        $this->assertNotFalse($thumb);
        $image = imagecreatefromstring(file_get_contents($thumb));
        $this->assertEquals(self::WIDTH, imagesx($image));
        $this->assertEquals(self::HEIGHT, imagesy($image));
    }

    /**
     * Test a thumbnail with a custom size in 'common' mode.
     */
    public function testThumbnailCommonValid()
    {
        $this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON);
        $thumb = $this->thumbnailer->get('https://imgur.com/jlFgGpe');
        $this->assertNotFalse($thumb);
        $image = imagecreatefromstring(file_get_contents($thumb));
        $this->assertEquals(self::WIDTH, imagesx($image));
        $this->assertEquals(self::HEIGHT, imagesy($image));
    }

    /**
     * Test a thumbnail in 'common' mode which isn't include in common websites.
     */
    public function testThumbnailCommonInvalid()
    {
        $this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON);
        $thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
        $this->assertFalse($thumb);
    }

    /**
     * Test a thumbnail that can't be retrieved.
     */
    public function testThumbnailNotValid()
    {
        $oldlog = ini_get('error_log');
        ini_set('error_log', '/dev/null');

        $thumbnailer = new Thumbnailer(new ConfigManager());
        $thumb = $thumbnailer->get('nope');
        $this->assertFalse($thumb);

        ini_set('error_log', $oldlog);
    }

    protected function rrmdirContent($dir)
    {
        if (is_dir($dir)) {
            $objects = scandir($dir);
            foreach ($objects as $object) {
                if ($object != "." && $object != "..") {
                    if (is_dir($dir."/".$object)) {
                        $this->rrmdirContent($dir."/".$object);
                    } else {
                        unlink($dir."/".$object);
                    }
                }
            }
        }
    }
}