Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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_backup;
  18  
  19  use base_atom_struct_exception;
  20  use base_element_struct_exception;
  21  use mock_base_attribute;
  22  use mock_base_final_element;
  23  use mock_base_nested_element;
  24  use mock_base_optigroup;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  // Include all the needed stuff
  29  require_once (__DIR__.'/fixtures/structure_fixtures.php');
  30  
  31  /**
  32   * Unit test case the base_optigroup class.
  33   *
  34   * Note: highly imbricated with nested/final base elements
  35   *
  36   * @package   core_backup
  37   * @category  test
  38   * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class baseoptigroup_test extends \basic_testcase {
  42  
  43      /**
  44       * Correct creation tests (s)
  45       */
  46      function test_creation() {
  47          $instance = new mock_base_optigroup('optigroup', null, true);
  48          $this->assertInstanceOf('base_optigroup', $instance);
  49          $this->assertEquals($instance->get_name(), 'optigroup');
  50          $this->assertNull($instance->get_parent());
  51          $this->assertEquals($instance->get_children(), array());
  52          $this->assertEquals($instance->get_level(), 1);
  53          $this->assertTrue($instance->is_multiple());
  54  
  55          // Get to_string() results (with values)
  56          $child1 = new mock_base_nested_element('child1', null, new mock_base_final_element('four'));
  57          $child2 = new mock_base_nested_element('child2', null, new mock_base_final_element('five'));
  58          $instance->add_child($child1);
  59          $instance->add_child($child2);
  60          $children = $instance->get_children();
  61          $final_elements = $children['child1']->get_final_elements();
  62          $final_elements['four']->set_value('final4value');
  63          $final_elements['four']->add_attributes('attr4');
  64          $grandchild = new mock_base_nested_element('grandchild', new mock_base_attribute('attr5'));
  65          $child2->add_child($grandchild);
  66          $attrs = $grandchild->get_attributes();
  67          $attrs['attr5']->set_value('attr5value');
  68          $tostring = $instance->to_string(true);
  69          $this->assertTrue(strpos($tostring, '!optigroup (level: 1)') !== false);
  70          $this->assertTrue(strpos($tostring, '?child2 (level: 2) =>') !== false);
  71          $this->assertTrue(strpos($tostring, ' => ') !== false);
  72          $this->assertTrue(strpos($tostring, '#four (level: 3) => final4value') !== false);
  73          $this->assertTrue(strpos($tostring, '@attr5 => attr5value') !== false);
  74          $this->assertTrue(strpos($tostring, '#five (level: 3) => not set') !== false);
  75      }
  76  
  77      /**
  78       * Incorrect creation tests (attributes and final elements)
  79       */
  80      function test_wrong_creation() {
  81  
  82          // Create instance with invalid name
  83          try {
  84              $instance = new mock_base_nested_element('');
  85              $this->fail("Expecting base_atom_struct_exception exception, none occurred");
  86          } catch (\Exception $e) {
  87              $this->assertTrue($e instanceof base_atom_struct_exception);
  88          }
  89  
  90          // Create instance with incorrect (object) final element
  91          try {
  92              $obj = new \stdClass;
  93              $obj->name = 'test_attr';
  94              $instance = new mock_base_nested_element('TEST', null, $obj);
  95              $this->fail("Expecting base_element_struct_exception exception, none occurred");
  96          } catch (\Exception $e) {
  97              $this->assertTrue($e instanceof base_element_struct_exception);
  98          }
  99  
 100          // Create instance with array containing incorrect (object) final element
 101          try {
 102              $obj = new \stdClass;
 103              $obj->name = 'test_attr';
 104              $instance = new mock_base_nested_element('TEST', null, array($obj));
 105              $this->fail("Expecting base_element_struct_exception exception, none occurred");
 106          } catch (\Exception $e) {
 107              $this->assertTrue($e instanceof base_element_struct_exception);
 108          }
 109  
 110          // Create instance with array containing duplicate final elements
 111          try {
 112              $instance = new mock_base_nested_element('TEST', null, array('VAL1', 'VAL2', 'VAL1'));
 113              $this->fail("Expecting base_element_struct_exception exception, none occurred");
 114          } catch (\Exception $e) {
 115              $this->assertTrue($e instanceof base_element_struct_exception);
 116          }
 117  
 118          // Try to get value of base_nested_element
 119          $instance = new mock_base_nested_element('TEST');
 120          try {
 121              $instance->get_value();
 122              $this->fail("Expecting base_element_struct_exception exception, none occurred");
 123          } catch (\Exception $e) {
 124              $this->assertTrue($e instanceof base_element_struct_exception);
 125          }
 126  
 127          // Try to set value of base_nested_element
 128          $instance = new mock_base_nested_element('TEST');
 129          try {
 130              $instance->set_value('some_value');
 131              $this->fail("Expecting base_element_struct_exception exception, none occurred");
 132          } catch (\Exception $e) {
 133              $this->assertTrue($e instanceof base_element_struct_exception);
 134          }
 135  
 136          // Try to clean one value of base_nested_element
 137          $instance = new mock_base_nested_element('TEST');
 138          try {
 139              $instance->clean_value('some_value');
 140              $this->fail("Expecting base_element_struct_exception exception, none occurred");
 141          } catch (\Exception $e) {
 142              $this->assertTrue($e instanceof base_element_struct_exception);
 143          }
 144      }
 145  }