Search moodle.org's
Developer Documentation

See Release Notes

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