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 401 and 402] [Versions 401 and 403]

   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_backup;
  18  
  19  use backup_structure_dbops;
  20  
  21  /**
  22   * Tests for backup_structure_dbops
  23   *
  24   * @package    core_backup
  25   * @category   test
  26   * @copyright  2023 Andrew Lyons <andrew@nicols.co.uk>
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   * @covers \backup_structure_dbops
  29   */
  30  class backup_structure_dbops_test extends \advanced_testcase {
  31      public static function setUpBeforeClass(): void {
  32          global $CFG;
  33          parent::setUpBeforeClass();
  34          require_once("{$CFG->dirroot}/backup/util/includes/backup_includes.php");
  35      }
  36  
  37      /**
  38       * Tests for convert_params_to_values.
  39       *
  40       * @dataProvider convert_params_to_values_provider
  41       * @param array $params
  42       * @param mixed $processor
  43       * @param array $expected
  44       */
  45      public function test_convert_params_to_values(
  46          array $params,
  47          $processor,
  48          array $expected
  49      ): void {
  50          if (is_callable($processor)) {
  51              $newprocessor = $this->createMock(\backup_structure_processor::class);
  52              $newprocessor->method('get_var')->willReturnCallback($processor);
  53              $processor = $newprocessor;
  54          }
  55  
  56          $result = backup_structure_dbops::convert_params_to_values($params, $processor);
  57  
  58          $this->assertEqualsCanonicalizing($expected, $result);
  59      }
  60  
  61      /**
  62       * Data provider for convert_params_to_values_provider.
  63       */
  64      public static function convert_params_to_values_provider(): array {
  65          return [
  66              'String value is not processed' => [
  67                  ['/0/1/2/345'],
  68                  null,
  69                  ['/0/1/2/345'],
  70              ],
  71              'Positive integer' => [
  72                  [123, 456],
  73                  null,
  74                  [123, 456],
  75              ],
  76              'Negative integer' => [
  77                  [-42],
  78                  function () {
  79                      return 'Life, the Universe, and Everything';
  80                  },
  81                  ['Life, the Universe, and Everything'],
  82              ],
  83              'Mix of strings, and ints with a processor' => [
  84                  ['foo', 123, 'bar', -42],
  85                  function () {
  86                      return 'Life, the Universe, and Everything';
  87                  },
  88                  ['foo', 123, 'bar', 'Life, the Universe, and Everything'],
  89              ],
  90          ];
  91      }
  92  
  93      /**
  94       * Tests for convert_params_to_values with an atom.
  95       */
  96      public function test_convert_params_to_values_with_atom(): void {
  97          $atom = $this->createMock(\base_atom::class);
  98          $atom->method('is_set')->willReturn(true);
  99          $atom->method('get_value')->willReturn('Some atomised value');
 100  
 101          $result = backup_structure_dbops::convert_params_to_values([$atom], null);
 102  
 103          $this->assertEqualsCanonicalizing(['Some atomised value'], $result);
 104      }
 105  
 106      /**
 107       * Tests for convert_params_to_values with an atom without any value.
 108       */
 109      public function test_convert_params_to_values_with_atom_no_value(): void {
 110          $atom = $this->createMock(\base_atom::class);
 111          $atom->method('is_set')->willReturn(false);
 112          $atom->method('get_name')->willReturn('Atomisd name');
 113  
 114          $this->expectException(\base_element_struct_exception::class);
 115          backup_structure_dbops::convert_params_to_values([$atom], null);
 116      }
 117  }