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.

Differences Between: [Versions 310 and 400]

   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  /**
  18   * PHP lang parser test.
  19   *
  20   * @package    tool_customlang
  21   * @copyright  2015 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_customlang\local\mlang;
  26  
  27  use advanced_testcase;
  28  use moodle_exception;
  29  
  30  /**
  31   * PHP lang parser test class.
  32   *
  33   * @package    tool_customlang
  34   * @copyright  2015 Damyon Wiese
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class phpparser_test extends advanced_testcase {
  38  
  39  
  40      /**
  41       * Test get instance static method.
  42       *
  43       */
  44      public function test_get_instance(): void {
  45  
  46          $instance = phpparser::get_instance();
  47  
  48          $this->assertInstanceOf('tool_customlang\local\mlang\phpparser', $instance);
  49          $this->assertEquals($instance, phpparser::get_instance());
  50      }
  51  
  52      /**
  53       * Test get instance parse method.
  54       *
  55       * @dataProvider parse_provider
  56       * @param string $phpcode PHP code to test
  57       * @param array $expected Expected result
  58       * @param bool $exception if an exception is expected
  59       */
  60      public function test_parse(string $phpcode, array $expected, bool $exception): void {
  61  
  62          $instance = phpparser::get_instance();
  63  
  64          if ($exception) {
  65              $this->expectException(moodle_exception::class);
  66          }
  67  
  68          $strings = $instance->parse($phpcode);
  69  
  70          $this->assertEquals(count($expected), count($strings));
  71          foreach ($strings as $key => $langstring) {
  72              $this->assertEquals($expected[$key][0], $langstring->id);
  73              $this->assertEquals($expected[$key][1], $langstring->text);
  74          }
  75      }
  76  
  77      /**
  78       * Data provider for the test_parse.
  79       *
  80       * @return  array
  81       */
  82      public function parse_provider() : array {
  83          return [
  84              'Invalid PHP code' => [
  85                  'No PHP code', [], false
  86              ],
  87              'No PHP open tag' => [
  88                  "\$string['example'] = 'text';\n", [], false
  89              ],
  90              'One string code' => [
  91                  "<?php \$string['example'] = 'text';\n", [['example', 'text']], false
  92              ],
  93              'Extra spaces' => [
  94                  "<?php \$string['example']   =   'text';\n", [['example', 'text']], false
  95              ],
  96              'Extra tabs' => [
  97                  "<?php \$string['example']\t=\t'text';\n", [['example', 'text']], false
  98              ],
  99              'Double quote string' => [
 100                  "<?php
 101                      \$string['example'] = \"text\";
 102                      \$string[\"example2\"] = 'text2';
 103                      \$string[\"example3\"] = \"text3\";
 104                  ", [
 105                      ['example', 'text'],
 106                      ['example2', 'text2'],
 107                      ['example3', 'text3'],
 108                  ], false
 109              ],
 110              'Multiple lines strings' => [
 111                  "<?php
 112                      \$string['example'] = 'First line\nsecondline';
 113                      \$string['example2'] = \"First line\nsecondline2\";
 114                  ", [
 115                      ['example', "First line\nsecondline"],
 116                      ['example2', "First line\nsecondline2"],
 117                  ], false
 118              ],
 119              'Two strings code' => [
 120                  "<?php
 121                      \$string['example'] = 'text';
 122                      \$string['example2'] = 'text2';
 123                  ", [
 124                      ['example', 'text'],
 125                      ['example2', 'text2'],
 126                  ], false
 127              ],
 128              'Scaped characters' => [
 129                  "<?php
 130                      \$string['example'] = 'Thos are \\' quotes \" 1';
 131                      \$string['example2'] = \"Thos are ' quotes \\\" 2\";
 132                  ", [
 133                      ['example', "Thos are ' quotes \" 1"],
 134                      ['example2', "Thos are ' quotes \" 2"],
 135                  ], false
 136              ],
 137              'PHP with single line comments' => [
 138                  "<?php
 139                      // This is a comment.
 140                      \$string['example'] = 'text';
 141                      // This is another commment.
 142                  ", [
 143                      ['example', 'text'],
 144                  ], false
 145              ],
 146              'PHP with block comments' => [
 147                  "<?php
 148                      /* This is a block comment. */
 149                      \$string['example'] = 'text';
 150                      /* This is another
 151                      block comment. */
 152                  ", [
 153                      ['example', 'text'],
 154                  ], false
 155              ],
 156              'Wrong variable name' => [
 157                  "<?php
 158                      \$stringwrong['example'] = 'text';
 159                      \$wringstring['example'] = 'text';
 160                  ", [], false
 161              ],
 162              'Single line commented valid line' => [
 163                  "<?php
 164                      // \$string['example'] = 'text';
 165                  ", [], false
 166              ],
 167              'Block commented valid line' => [
 168                  "<?php
 169                      /*
 170                      \$string['example'] = 'text';
 171                      */
 172                  ", [], false
 173              ],
 174              'Syntax error 1 (double assignation)' => [
 175                  "<?php
 176                      \$string['example'] = 'text' = 'wrong';
 177                  ", [], true
 178              ],
 179              'Syntax error 2 (no closing string)' => [
 180                  "<?php
 181                      \$string['example'] = 'wrong;
 182                  ", [], true
 183              ],
 184              'Syntax error 3 (Array without key)' => [
 185                  "<?php
 186                      \$string[] = 'wrong';
 187                  ", [], true
 188              ],
 189              'Syntax error 4 (Array not open)' => [
 190                  "<?php
 191                      \$string'example'] = 'wrong';
 192                  ", [], true
 193              ],
 194              'Syntax error 5 (Array not closed)' => [
 195                  "<?php
 196                      \$string['example' = 'wrong';
 197                  ", [], true
 198              ],
 199              'Syntax error 6 (Missing assignment)' => [
 200                  "<?php
 201                      \$string['example'] 'wrong';
 202                  ", [], true
 203              ],
 204          ];
 205      }
 206  
 207  }