Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core;
18
 
19
/**
20
 * A set of tests for some of the gd functionality within Moodle.
21
 *
22
 * @package    core
23
 * @category   test
24
 * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
1441 ariadna 27
final class gdlib_test extends \basic_testcase {
1 efrain 28
 
29
    private $fixturepath = null;
30
 
31
    public function setUp(): void {
1441 ariadna 32
        parent::setUp();
1 efrain 33
        $this->fixturepath = __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR;
34
    }
35
 
11 efrain 36
    public function test_generate_image_thumbnail(): void {
1 efrain 37
        global $CFG;
38
        require_once($CFG->libdir . '/gdlib.php');
39
 
40
        // Test with meaningless data.
41
 
42
        // Now use a fixture.
43
        $pngpath = $this->fixturepath . 'gd-logo.png';
44
        $pngthumb = generate_image_thumbnail($pngpath, 24, 24);
45
        $this->assertTrue(is_string($pngthumb));
46
 
47
        // And check that the generated image was of the correct proportions and mimetype.
48
        $imageinfo = getimagesizefromstring($pngthumb);
49
        $this->assertEquals(24, $imageinfo[0]);
50
        $this->assertEquals(24, $imageinfo[1]);
51
        $this->assertEquals('image/png', $imageinfo['mime']);
52
    }
53
 
11 efrain 54
    public function test_generate_image_thumbnail_from_string(): void {
1 efrain 55
        global $CFG;
56
        require_once($CFG->libdir . '/gdlib.php');
57
 
58
        // Test with meaningless data.
59
 
60
        // First empty values.
61
        $this->assertFalse(generate_image_thumbnail_from_string('', 24, 24));
62
        $this->assertFalse(generate_image_thumbnail_from_string('invalid', 0, 24));
63
        $this->assertFalse(generate_image_thumbnail_from_string('invalid', 24, 0));
64
 
65
        // Now an invalid string.
66
        $this->assertFalse(generate_image_thumbnail_from_string('invalid', 24, 24));
67
 
68
        // Now use a fixture.
69
        $pngpath = $this->fixturepath . 'gd-logo.png';
70
        $pngdata = file_get_contents($pngpath);
71
        $pngthumb = generate_image_thumbnail_from_string($pngdata, 24, 24);
72
        $this->assertTrue(is_string($pngthumb));
73
 
74
        // And check that the generated image was of the correct proportions and mimetype.
75
        $imageinfo = getimagesizefromstring($pngthumb);
76
        $this->assertEquals(24, $imageinfo[0]);
77
        $this->assertEquals(24, $imageinfo[1]);
78
        $this->assertEquals('image/png', $imageinfo['mime']);
79
    }
80
 
11 efrain 81
    public function test_resize_image(): void {
1 efrain 82
        global $CFG;
83
        require_once($CFG->libdir . '/gdlib.php');
84
 
85
        $pngpath = $this->fixturepath . 'gd-logo.png';
86
 
87
        // Preferred height.
88
        $newpng = resize_image($pngpath, null, 24);
89
        $this->assertTrue(is_string($newpng));
90
        $imageinfo = getimagesizefromstring($newpng);
91
        $this->assertEquals(90, $imageinfo[0]);
92
        $this->assertEquals(24, $imageinfo[1]);
93
        $this->assertEquals('image/png', $imageinfo['mime']);
94
 
95
        // Preferred width.
96
        $newpng = resize_image($pngpath, 100, null);
97
        $this->assertTrue(is_string($newpng));
98
        $imageinfo = getimagesizefromstring($newpng);
99
        $this->assertEquals(100, $imageinfo[0]);
100
        $this->assertEquals(27, $imageinfo[1]);
101
        $this->assertEquals('image/png', $imageinfo['mime']);
102
 
103
        // Preferred width and height.
104
        $newpng = resize_image($pngpath, 50, 50);
105
        $this->assertTrue(is_string($newpng));
106
        $imageinfo = getimagesizefromstring($newpng);
107
        $this->assertEquals(50, $imageinfo[0]);
108
        $this->assertEquals(13, $imageinfo[1]);
109
        $this->assertEquals('image/png', $imageinfo['mime']);
110
    }
111
 
11 efrain 112
    public function test_resize_image_from_image(): void {
1 efrain 113
        global $CFG;
114
        require_once($CFG->libdir . '/gdlib.php');
115
 
116
        $pngpath = $this->fixturepath . 'gd-logo.png';
117
        $origimageinfo = getimagesize($pngpath);
118
        $imagecontent = file_get_contents($pngpath);
119
 
120
        // Preferred height.
121
        $imageresource = imagecreatefromstring($imagecontent);
122
        $newpng = resize_image_from_image($imageresource, $origimageinfo, null, 24);
123
        $this->assertTrue(is_string($newpng));
124
        $imageinfo = getimagesizefromstring($newpng);
125
        $this->assertEquals(90, $imageinfo[0]);
126
        $this->assertEquals(24, $imageinfo[1]);
127
        $this->assertEquals('image/png', $imageinfo['mime']);
128
 
129
        // Preferred width.
130
        $imageresource = imagecreatefromstring($imagecontent);
131
        $newpng = resize_image_from_image($imageresource, $origimageinfo, 100, null);
132
        $this->assertTrue(is_string($newpng));
133
        $imageinfo = getimagesizefromstring($newpng);
134
        $this->assertEquals(100, $imageinfo[0]);
135
        $this->assertEquals(27, $imageinfo[1]);
136
        $this->assertEquals('image/png', $imageinfo['mime']);
137
 
138
        // Preferred width and height.
139
        $imageresource = imagecreatefromstring($imagecontent);
140
        $newpng = resize_image_from_image($imageresource, $origimageinfo, 50, 50);
141
        $this->assertTrue(is_string($newpng));
142
        $imageinfo = getimagesizefromstring($newpng);
143
        $this->assertEquals(50, $imageinfo[0]);
144
        $this->assertEquals(13, $imageinfo[1]);
145
        $this->assertEquals('image/png', $imageinfo['mime']);
146
    }
147
 
148
}