Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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_external;
  18  
  19  use advanced_testcase;
  20  
  21  /**
  22   * Unit tests for core_external\external_description.
  23   *
  24   * @package    core
  25   * @category   test
  26   * @copyright  2023 Jun Pataleta
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   * @coversDefaultClass external_value
  29   */
  30  class external_value_test extends advanced_testcase {
  31  
  32      /**
  33       * Data provider for the required param test.
  34       *
  35       * @return array[]
  36       */
  37      public function required_param_provider(): array {
  38          return [
  39              [ VALUE_DEFAULT, false ],
  40              [ VALUE_REQUIRED, false ],
  41              [ VALUE_OPTIONAL, false ],
  42              [ 'aaa', true, 'aaa' ],
  43              [ [VALUE_OPTIONAL], true, 'Array: ' . VALUE_OPTIONAL ],
  44              [ -1000, true, -1000 ],
  45          ];
  46      }
  47  
  48      /**
  49       * Tests the constructor for the $required parameter validation.
  50       *
  51       * @dataProvider required_param_provider
  52       * @param int $required The required param being tested.
  53       * @param bool $debuggingexpected Whether debugging is expected.
  54       * @param mixed $requiredstr The string value of the $required param in the debugging message.
  55       * @return void
  56       */
  57      public function test_required_param_validation($required, $debuggingexpected, $requiredstr = '') {
  58          $externalvalue = new external_value(PARAM_INT, 'Cool description', $required);
  59          if ($debuggingexpected) {
  60              $this->assertDebuggingCalled("Invalid \$required parameter value: '{$requiredstr}' .
  61                  It must be either VALUE_DEFAULT, VALUE_REQUIRED, or VALUE_OPTIONAL", DEBUG_DEVELOPER);
  62          }
  63          $this->assertEquals(PARAM_INT, $externalvalue->type);
  64          $this->assertEquals('Cool description', $externalvalue->desc);
  65          $this->assertEquals($required, $externalvalue->required);
  66      }
  67  }