csslib_test.php 8.57 KB
Newer Older
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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * This file contains the unittests for the css optimiser in csslib.php
 *
 * @package   core_css
 * @category  phpunit
 * @copyright 2012 Sam Hemelryk
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->libdir . '/csslib.php');


/**
33
 * CSS optimiser test class.
34
35
 *
 * @package core_css
36
 * @category phpunit
37
38
39
 * @copyright 2012 Sam Hemelryk
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
40
class core_csslib_testcase extends advanced_testcase {
41

42
    public function test_background() {
43
        $optimiser = new css_optimiser();
44
45

        $cssin = '.test {background-color: #123456;}';
46
47
48
        $this->assertSame($cssin, $optimiser->process($cssin));
        $this->assertDebuggingCalled('class css_optimiser is deprecated and no longer does anything, '
            . 'please consider using stylelint to optimise your css.');
49
50
51
52
    }


    /**
53
     * Test CSS colour matching.
54
55
     */
    public function test_css_is_colour() {
56
57
        $debugstr = 'css_is_colour() is deprecated without a replacement. Please copy the implementation '
            . 'into your plugin if you need this functionality.';
58
        // First lets test hex colours.
59
        $this->assertTrue(css_is_colour('#123456'));
60
        $this->assertDebuggingCalled($debugstr);
61
        $this->assertTrue(css_is_colour('#123'));
62
        $this->assertDebuggingCalled($debugstr);
63
        $this->assertTrue(css_is_colour('#ABCDEF'));
64
        $this->assertDebuggingCalled($debugstr);
65
        $this->assertTrue(css_is_colour('#ABC'));
66
        $this->assertDebuggingCalled($debugstr);
67
        $this->assertTrue(css_is_colour('#abcdef'));
68
        $this->assertDebuggingCalled($debugstr);
69
        $this->assertTrue(css_is_colour('#abc'));
70
        $this->assertDebuggingCalled($debugstr);
71
        $this->assertTrue(css_is_colour('#aBcDeF'));
72
        $this->assertDebuggingCalled($debugstr);
73
        $this->assertTrue(css_is_colour('#aBc'));
74
        $this->assertDebuggingCalled($debugstr);
75
        $this->assertTrue(css_is_colour('#1a2Bc3'));
76
        $this->assertDebuggingCalled($debugstr);
77
        $this->assertTrue(css_is_colour('#1Ac'));
78
        $this->assertDebuggingCalled($debugstr);
79

80
        // Note the following two colour's aren't really colours but browsers process
81
82
        // them still.
        $this->assertTrue(css_is_colour('#A'));
83
        $this->assertDebuggingCalled($debugstr);
84
        $this->assertTrue(css_is_colour('#12'));
85
        $this->assertDebuggingCalled($debugstr);
86
87
88
89
        // Having four or five characters however are not valid colours and
        // browsers don't parse them. They need to fail so that broken CSS
        // stays broken after optimisation.
        $this->assertFalse(css_is_colour('#1234'));
90
        $this->assertDebuggingCalled($debugstr);
91
        $this->assertFalse(css_is_colour('#12345'));
92
        $this->assertDebuggingCalled($debugstr);
93
94

        $this->assertFalse(css_is_colour('#BCDEFG'));
95
        $this->assertDebuggingCalled($debugstr);
96
        $this->assertFalse(css_is_colour('#'));
97
        $this->assertDebuggingCalled($debugstr);
98
        $this->assertFalse(css_is_colour('#0000000'));
99
        $this->assertDebuggingCalled($debugstr);
100
        $this->assertFalse(css_is_colour('#132-245'));
101
        $this->assertDebuggingCalled($debugstr);
102
        $this->assertFalse(css_is_colour('#13 23 43'));
103
        $this->assertDebuggingCalled($debugstr);
104
        $this->assertFalse(css_is_colour('123456'));
105
        $this->assertDebuggingCalled($debugstr);
106

107
        // Next lets test real browser mapped colours.
108
        $this->assertTrue(css_is_colour('black'));
109
        $this->assertDebuggingCalled($debugstr);
110
        $this->assertTrue(css_is_colour('blue'));
111
        $this->assertDebuggingCalled($debugstr);
112
        $this->assertTrue(css_is_colour('BLACK'));
113
        $this->assertDebuggingCalled($debugstr);
114
        $this->assertTrue(css_is_colour('Black'));
115
        $this->assertDebuggingCalled($debugstr);
116
        $this->assertTrue(css_is_colour('bLACK'));
117
        $this->assertDebuggingCalled($debugstr);
118
        $this->assertTrue(css_is_colour('mediumaquamarine'));
119
        $this->assertDebuggingCalled($debugstr);
120
        $this->assertTrue(css_is_colour('mediumAquamarine'));
121
        $this->assertDebuggingCalled($debugstr);
122
        $this->assertFalse(css_is_colour('monkey'));
123
        $this->assertDebuggingCalled($debugstr);
124
        $this->assertFalse(css_is_colour(''));
125
        $this->assertDebuggingCalled($debugstr);
126
        $this->assertFalse(css_is_colour('not a colour'));
127
        $this->assertDebuggingCalled($debugstr);
128

129
        // Next lets test rgb(a) colours.
130
        $this->assertTrue(css_is_colour('rgb(255,255,255)'));
131
        $this->assertDebuggingCalled($debugstr);
132
        $this->assertTrue(css_is_colour('rgb(0, 0, 0)'));
133
        $this->assertDebuggingCalled($debugstr);
134
        $this->assertTrue(css_is_colour('RGB (255, 255   ,    255)'));
135
        $this->assertDebuggingCalled($debugstr);
136
        $this->assertTrue(css_is_colour('rgba(0,0,0,0)'));
137
        $this->assertDebuggingCalled($debugstr);
138
        $this->assertTrue(css_is_colour('RGBA(255,255,255,1)'));
139
        $this->assertDebuggingCalled($debugstr);
140
        $this->assertTrue(css_is_colour('rgbA(255,255,255,0.5)'));
141
        $this->assertDebuggingCalled($debugstr);
142
        $this->assertFalse(css_is_colour('rgb(-255,-255,-255)'));
143
        $this->assertDebuggingCalled($debugstr);
144
        $this->assertFalse(css_is_colour('rgb(256,-256,256)'));
145
        $this->assertDebuggingCalled($debugstr);
146

147
        // Now lets test HSL colours.
148
        $this->assertTrue(css_is_colour('hsl(0,0%,100%)'));
149
        $this->assertDebuggingCalled($debugstr);
150
        $this->assertTrue(css_is_colour('hsl(180, 0%, 10%)'));
151
        $this->assertDebuggingCalled($debugstr);
152
        $this->assertTrue(css_is_colour('hsl (360, 100%   ,    95%)'));
153
        $this->assertDebuggingCalled($debugstr);
154

155
        // Finally test the special values.
156
        $this->assertTrue(css_is_colour('inherit'));
157
        $this->assertDebuggingCalled($debugstr);
158
159
160
    }

    /**
161
     * Test the css_is_width function.
162
163
     */
    public function test_css_is_width() {
164
165
        $debugstr = 'css_is_width() is deprecated without a replacement. Please copy the implementation '
            . 'into your plugin if you need this functionality.';
166
        $this->assertTrue(css_is_width('0'));
167
        $this->assertDebuggingCalled($debugstr);
168
        $this->assertTrue(css_is_width('0px'));
169
        $this->assertDebuggingCalled($debugstr);
170
        $this->assertTrue(css_is_width('0em'));
171
        $this->assertDebuggingCalled($debugstr);
172
        $this->assertTrue(css_is_width('199px'));
173
        $this->assertDebuggingCalled($debugstr);
174
        $this->assertTrue(css_is_width('199em'));
175
        $this->assertDebuggingCalled($debugstr);
176
        $this->assertTrue(css_is_width('199%'));
177
        $this->assertDebuggingCalled($debugstr);
178
        $this->assertTrue(css_is_width('-1px'));
179
        $this->assertDebuggingCalled($debugstr);
180
        $this->assertTrue(css_is_width('auto'));
181
        $this->assertDebuggingCalled($debugstr);
182
        $this->assertTrue(css_is_width('inherit'));
183
        $this->assertDebuggingCalled($debugstr);
184

185
        // Valid widths but missing their unit specifier.
186
        $this->assertFalse(css_is_width('0.75'));
187
        $this->assertDebuggingCalled($debugstr);
188
        $this->assertFalse(css_is_width('3'));
189
        $this->assertDebuggingCalled($debugstr);
190
        $this->assertFalse(css_is_width('-1'));
191
        $this->assertDebuggingCalled($debugstr);
192
193

        // Totally invalid widths.
194
        $this->assertFalse(css_is_width('-'));
195
        $this->assertDebuggingCalled($debugstr);
196
        $this->assertFalse(css_is_width('bananas'));
197
        $this->assertDebuggingCalled($debugstr);
198
        $this->assertFalse(css_is_width(''));
199
        $this->assertDebuggingCalled($debugstr);
200
        $this->assertFalse(css_is_width('top'));
201
        $this->assertDebuggingCalled($debugstr);
202
    }
203
}