Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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  declare(strict_types=1);
  18  
  19  namespace core\output;
  20  
  21  /**
  22   * Unit tests for the mustache_quote_helper class.
  23   *
  24   * @package   core
  25   * @category  test
  26   * @copyright 2022 TU Berlin
  27   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   * @coversDefaultClass \core\output\mustache_quote_helper
  29   */
  30  class mustache_quote_helper_test extends \basic_testcase {
  31  
  32      /**
  33       * Tests the quote helper
  34       *
  35       * @covers ::quote
  36       */
  37      public function test_quote() {
  38          $engine = new \Mustache_Engine();
  39          $context = new \Mustache_Context([
  40              'world' => '{{planet}}',
  41              'planet' => '<earth>'
  42          ]);
  43          $lambdahelper = new \Mustache_LambdaHelper($engine, $context);
  44  
  45          $quotehelper = new mustache_quote_helper();
  46  
  47          // Simple string.
  48          $this->assertEquals('"Hello world!"', $quotehelper->quote('Hello world!', $lambdahelper));
  49  
  50          // Special JSON characters in string.
  51          $this->assertEquals(
  52              '"Hello \\"world\\"! (\\b,\\f,\\n,\\r,\\t,\\\\)"',
  53              $quotehelper->quote('Hello "world"! (' . chr(8) . ",\f,\n,\r,\t,\\)", $lambdahelper)
  54          );
  55  
  56          // Double curly braces in string.
  57          $this->assertEquals(
  58              '"Hello {{=<% %>=}}{{<%={{ }}=%>world{{=<% %>=}}}}<%={{ }}=%>!"',
  59              $quotehelper->quote('{{=<% %>=}}Hello {{world}}!<%={{ }}=%>', $lambdahelper)
  60          );
  61  
  62          // Triple curly braces in string.
  63          $this->assertEquals(
  64              '"Hello {{=<% %>=}}{{{<%={{ }}=%>world{{=<% %>=}}}}}<%={{ }}=%>!"',
  65              $quotehelper->quote('{{=<% %>=}}Hello {{{world}}}!<%={{ }}=%>', $lambdahelper)
  66          );
  67  
  68          // Variable interpolation with double braces.
  69          $this->assertEquals(
  70              '"Hello &lt;earth&gt;!"',
  71              $quotehelper->quote('Hello {{planet}}!', $lambdahelper)
  72          );
  73  
  74          // Variable interpolation with triple braces.
  75          $this->assertEquals(
  76              '"Hello <earth>!"',
  77              $quotehelper->quote('Hello {{{planet}}}!', $lambdahelper)
  78          );
  79  
  80          // Variables interpolated only once.
  81          $this->assertEquals(
  82              '"Hello {{=<% %>=}}{{<%={{ }}=%>planet{{=<% %>=}}}}<%={{ }}=%>!"',
  83              $quotehelper->quote('Hello {{world}}!', $lambdahelper)
  84          );
  85      }
  86  }