Merge branch 'MDL-49360-28' of git://github.com/lameze/moodle into MOODLE_28_STABLE
[moodle.git] / lib / tests / minify_test.php
blob75de3b8d93d571192a2bb3b69d83ad9757dbd28b
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.
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/>.
17 /**
18 * core_minify related tests.
20 * @package core
21 * @category phpunit
22 * @copyright 2013 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Class core_minify_testcase.
32 class core_minify_testcase extends advanced_testcase {
33 public function test_css() {
34 $css = "
35 body {
36 background: #fff;
37 margin: 0;
38 padding: 0;
39 color: #281f18;
40 }";
42 $this->assertSame("body{background:#fff;margin:0;padding:0;color:#281f18}", core_minify::css($css));
45 public function test_css_files() {
46 global $CFG;
48 $testfile1 = "$CFG->tempdir/test1.css";
49 $testfile2 = "$CFG->tempdir/test2.css";
50 $testfile3 = "$CFG->tempdir/test3.css";
52 $css1 = "
53 body {
54 background: #fff;
55 margin: 0;
56 padding: 0;
57 color: #281f18;
58 }";
60 $css2 = "body{}";
62 file_put_contents($testfile1, $css1);
63 file_put_contents($testfile2, $css2);
65 $files = array($testfile1, $testfile2);
67 $this->assertSame("body{background:#fff;margin:0;padding:0;color:#281f18}\nbody{}", core_minify::css_files($files));
70 $files = array($testfile1, $testfile2, $testfile3);
72 $this->assertStringStartsWith("body{background:#fff;margin:0;padding:0;color:#281f18}\nbody{}\n\n\n/* Cannot read CSS file ", @core_minify::css_files($files));
74 unlink($testfile1);
75 unlink($testfile2);
78 public function test_js() {
79 $js = "
80 function hm()
85 $this->assertSame("function hm(){}", core_minify::js($js));
87 $js = "function hm{}";
88 $result = core_minify::js($js);
89 $this->assertStringStartsWith("\ntry {console.log('Error: Minimisation of JavaScript failed!');} catch (e) {}", $result);
90 $this->assertContains($js, $result);
93 public function test_js_files() {
94 global $CFG;
96 $testfile1 = "$CFG->tempdir/test1.js";
97 $testfile2 = "$CFG->tempdir/test2.js";
98 $testfile3 = "$CFG->tempdir/test3.js";
100 $js1 = "
101 function hm()
106 $js2 = "function oh(){}";
108 file_put_contents($testfile1, $js1);
109 file_put_contents($testfile2, $js2);
111 $files = array($testfile1, $testfile2);
113 $this->assertSame("function hm(){};\nfunction oh(){}", core_minify::js_files($files));
115 $files = array($testfile1, $testfile2, $testfile3);
117 $this->assertStringStartsWith("function hm(){};\nfunction oh(){};\n\n\n// Cannot read JS file ", @core_minify::js_files($files));
119 unlink($testfile1);
120 unlink($testfile2);