Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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_attribute;
  21  use base_element_attribute_exception;
  22  use mock_base_attribute;
  23  use mock_base_final_element;
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  // Include all the needed stuff
  28  require_once (__DIR__.'/fixtures/structure_fixtures.php');
  29  
  30  
  31  /**
  32   * Unit test case the base_final_element class.
  33   *
  34   * Note: highly imbricated with base_nested_element class
  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 basefinalelement_test extends \basic_testcase {
  42  
  43      /**
  44       * Correct base_final_element tests
  45       */
  46      function test_base_final_element() {
  47  
  48          // Create instance with name
  49          $instance = new mock_base_final_element('TEST');
  50          $this->assertInstanceOf('base_final_element', $instance);
  51          $this->assertEquals($instance->get_name(), 'TEST');
  52          $this->assertNull($instance->get_value());
  53          $this->assertEquals($instance->get_attributes(), array());
  54          $this->assertNull($instance->get_parent());
  55          $this->assertEquals($instance->get_level(), 1);
  56  
  57          // Set value
  58          $instance->set_value('value');
  59          $this->assertEquals($instance->get_value(), 'value');
  60  
  61          // Create instance with name and one object attribute
  62          $instance = new mock_base_final_element('TEST', new mock_base_attribute('ATTR1'));
  63          $attrs = $instance->get_attributes();
  64          $this->assertTrue(is_array($attrs));
  65          $this->assertEquals(count($attrs), 1);
  66          $this->assertTrue($attrs['ATTR1'] instanceof base_attribute);
  67          $this->assertEquals($attrs['ATTR1']->get_name(), 'ATTR1');
  68          $this->assertNull($attrs['ATTR1']->get_value());
  69  
  70          // Create instance with name and various object attributes
  71          $attr1 = new mock_base_attribute('ATTR1');
  72          $attr1->set_value('attr1_value');
  73          $attr2 = new mock_base_attribute('ATTR2');
  74          $instance = new mock_base_final_element('TEST', array($attr1, $attr2));
  75          $attrs = $instance->get_attributes();
  76          $this->assertTrue(is_array($attrs));
  77          $this->assertEquals(count($attrs), 2);
  78          $this->assertTrue($attrs['ATTR1'] instanceof base_attribute);
  79          $this->assertEquals($attrs['ATTR1']->get_name(), 'ATTR1');
  80          $this->assertEquals($attrs['ATTR1']->get_value(), 'attr1_value');
  81          $this->assertTrue($attrs['ATTR2'] instanceof base_attribute);
  82          $this->assertEquals($attrs['ATTR2']->get_name(), 'ATTR2');
  83          $this->assertNull($attrs['ATTR2']->get_value());
  84  
  85          // Create instance with name and one string attribute
  86          $instance = new mock_base_final_element('TEST', 'ATTR1');
  87          $attrs = $instance->get_attributes();
  88          $this->assertTrue(is_array($attrs));
  89          $this->assertEquals(count($attrs), 1);
  90          $this->assertTrue($attrs['ATTR1'] instanceof base_attribute);
  91          $this->assertEquals($attrs['ATTR1']->get_name(), 'ATTR1');
  92          $this->assertNull($attrs['ATTR1']->get_value());
  93  
  94          // Create instance with name and various object attributes
  95          $instance = new mock_base_final_element('TEST', array('ATTR1', 'ATTR2'));
  96          $attrs = $instance->get_attributes();
  97          $attrs['ATTR1']->set_value('attr1_value');
  98          $this->assertTrue(is_array($attrs));
  99          $this->assertEquals(count($attrs), 2);
 100          $this->assertTrue($attrs['ATTR1'] instanceof base_attribute);
 101          $this->assertEquals($attrs['ATTR1']->get_name(), 'ATTR1');
 102          $this->assertEquals($attrs['ATTR1']->get_value(), 'attr1_value');
 103          $this->assertTrue($attrs['ATTR2'] instanceof base_attribute);
 104          $this->assertEquals($attrs['ATTR2']->get_name(), 'ATTR2');
 105          $this->assertNull($attrs['ATTR2']->get_value());
 106  
 107          // Clean values
 108          $instance = new mock_base_final_element('TEST', array('ATTR1', 'ATTR2'));
 109          $instance->set_value('instance_value');
 110          $attrs = $instance->get_attributes();
 111          $attrs['ATTR1']->set_value('attr1_value');
 112          $this->assertEquals($instance->get_value(), 'instance_value');
 113          $this->assertEquals($attrs['ATTR1']->get_value(), 'attr1_value');
 114          $instance->clean_values();
 115          $this->assertNull($instance->get_value());
 116          $this->assertNull($attrs['ATTR1']->get_value());
 117  
 118          // Get to_string() results (with values)
 119          $instance = new mock_base_final_element('TEST', array('ATTR1', 'ATTR2'));
 120          $instance->set_value('final element value');
 121          $attrs = $instance->get_attributes();
 122          $attrs['ATTR1']->set_value('attr1 value');
 123          $tostring = $instance->to_string(true);
 124          $this->assertTrue(strpos($tostring, '#TEST (level: 1)') !== false);
 125          $this->assertTrue(strpos($tostring, ' => ') !== false);
 126          $this->assertTrue(strpos($tostring, 'final element value') !== false);
 127          $this->assertTrue(strpos($tostring, 'attr1 value') !== false);
 128      }
 129  
 130      /**
 131       * Exception base_final_element tests
 132       */
 133      function test_base_final_element_exceptions() {
 134  
 135          // Create instance with invalid name
 136          try {
 137              $instance = new mock_base_final_element('');
 138              $this->fail("Expecting base_atom_struct_exception exception, none occurred");
 139          } catch (\Exception $e) {
 140              $this->assertTrue($e instanceof base_atom_struct_exception);
 141          }
 142  
 143          // Create instance with incorrect (object) attribute
 144          try {
 145              $obj = new \stdClass;
 146              $obj->name = 'test_attr';
 147              $instance = new mock_base_final_element('TEST', $obj);
 148              $this->fail("Expecting base_element_attribute_exception exception, none occurred");
 149          } catch (\Exception $e) {
 150              $this->assertTrue($e instanceof base_element_attribute_exception);
 151          }
 152  
 153          // Create instance with array containing incorrect (object) attribute
 154          try {
 155              $obj = new \stdClass;
 156              $obj->name = 'test_attr';
 157              $instance = new mock_base_final_element('TEST', array($obj));
 158              $this->fail("Expecting base_element_attribute_exception exception, none occurred");
 159          } catch (\Exception $e) {
 160              $this->assertTrue($e instanceof base_element_attribute_exception);
 161          }
 162  
 163          // Create instance with array containing duplicate attributes
 164          try {
 165              $instance = new mock_base_final_element('TEST', array('ATTR1', 'ATTR2', 'ATTR1'));
 166              $this->fail("Expecting base_element_attribute_exception exception, none occurred");
 167          } catch (\Exception $e) {
 168              $this->assertTrue($e instanceof base_element_attribute_exception);
 169          }
 170      }
 171  }