Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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  use core_minify;
  20  
  21  /**
  22   * Class core_minify_testcase.
  23   *
  24   * core_minify related tests.
  25   *
  26   * @package    core
  27   * @category   test
  28   * @copyright  2013 Petr Skoda {@link http://skodak.org}
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class minify_test extends \advanced_testcase {
  32      public function test_css() {
  33          $css = "
  34  body {
  35  background: #fff;
  36  margin: 0;
  37  padding: 0;
  38  color: #281f18;
  39  }";
  40  
  41          $this->assertSame("body{background:#fff;margin:0;padding:0;color:#281f18}", core_minify::css($css));
  42      }
  43  
  44      public function test_css_files() {
  45          global $CFG;
  46  
  47          $testfile1 = "$CFG->tempdir/test1.css";
  48          $testfile2 = "$CFG->tempdir/test2.css";
  49          $testfile3 = "$CFG->tempdir/test3.css";
  50  
  51          $css1 = "
  52  body {
  53  background: #fff;
  54  margin: 0;
  55  padding: 0;
  56  color: #281f18;
  57  }";
  58  
  59          $css2 = "body{}";
  60  
  61          file_put_contents($testfile1, $css1);
  62          file_put_contents($testfile2, $css2);
  63  
  64          $files = array($testfile1, $testfile2);
  65  
  66          $this->assertSame("body{background:#fff;margin:0;padding:0;color:#281f18}\n", core_minify::css_files($files));
  67  
  68  
  69          $files = array($testfile1, $testfile2, $testfile3);
  70  
  71          $this->assertStringStartsWith("body{background:#fff;margin:0;padding:0;color:#281f18}\n\n\n\n/* Cannot read CSS file ",
  72              @core_minify::css_files($files));
  73  
  74          unlink($testfile1);
  75          unlink($testfile2);
  76      }
  77  
  78      public function test_js() {
  79          $js = "
  80  function hm()
  81  {
  82  }
  83  ";
  84  
  85          $this->assertSame("function hm(){}", core_minify::js($js));
  86  
  87          $js = "function hm{}";
  88          $result = core_minify::js($js);
  89          $this->assertStringContainsString($js, $result);
  90      }
  91  
  92      public function test_js_files() {
  93          global $CFG;
  94  
  95          $testfile1 = "$CFG->tempdir/test1.js";
  96          $testfile2 = "$CFG->tempdir/test2.js";
  97          $testfile3 = "$CFG->tempdir/test3.js";
  98  
  99          $js1 = "
 100  function hm()
 101  {
 102  }
 103  ";
 104  
 105          $js2 = "function oh(){}";
 106  
 107          file_put_contents($testfile1, $js1);
 108          file_put_contents($testfile2, $js2);
 109  
 110          $files = array($testfile1, $testfile2);
 111  
 112          $this->assertSame("function hm(){};\nfunction oh(){}", core_minify::js_files($files));
 113  
 114          $files = array($testfile1, $testfile2, $testfile3);
 115  
 116          $this->assertStringStartsWith("function hm(){};\nfunction oh(){};\n\n\n// Cannot read JS file ",
 117              @core_minify::js_files($files));
 118  
 119          unlink($testfile1);
 120          unlink($testfile2);
 121      }
 122  }