/lib/tests/minify_test.php

https://gitlab.com/unofficial-mirrors/moodle · PHP · 123 lines · 69 code · 29 blank · 25 comment · 1 complexity · eb89fd013fa4d44d4ccd78b1ca58524b MD5 · raw file

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