Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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  /**
  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_final_element class. Note: highly imbricated with base_nested_element class
  32   */
  33  class backup_base_final_element_testcase extends basic_testcase {
  34  
  35      /**
  36       * Correct base_final_element tests
  37       */
  38      function test_base_final_element() {
  39  
  40          // Create instance with name
  41          $instance = new mock_base_final_element('TEST');
  42          $this->assertInstanceOf('base_final_element', $instance);
  43          $this->assertEquals($instance->get_name(), 'TEST');
  44          $this->assertNull($instance->get_value());
  45          $this->assertEquals($instance->get_attributes(), array());
  46          $this->assertNull($instance->get_parent());
  47          $this->assertEquals($instance->get_level(), 1);
  48  
  49          // Set value
  50          $instance->set_value('value');
  51          $this->assertEquals($instance->get_value(), 'value');
  52  
  53          // Create instance with name and one object attribute
  54          $instance = new mock_base_final_element('TEST', new mock_base_attribute('ATTR1'));
  55          $attrs = $instance->get_attributes();
  56          $this->assertTrue(is_array($attrs));
  57          $this->assertEquals(count($attrs), 1);
  58          $this->assertTrue($attrs['ATTR1'] instanceof base_attribute);
  59          $this->assertEquals($attrs['ATTR1']->get_name(), 'ATTR1');
  60          $this->assertNull($attrs['ATTR1']->get_value());
  61  
  62          // Create instance with name and various object attributes
  63          $attr1 = new mock_base_attribute('ATTR1');
  64          $attr1->set_value('attr1_value');
  65          $attr2 = new mock_base_attribute('ATTR2');
  66          $instance = new mock_base_final_element('TEST', array($attr1, $attr2));
  67          $attrs = $instance->get_attributes();
  68          $this->assertTrue(is_array($attrs));
  69          $this->assertEquals(count($attrs), 2);
  70          $this->assertTrue($attrs['ATTR1'] instanceof base_attribute);
  71          $this->assertEquals($attrs['ATTR1']->get_name(), 'ATTR1');
  72          $this->assertEquals($attrs['ATTR1']->get_value(), 'attr1_value');
  73          $this->assertTrue($attrs['ATTR2'] instanceof base_attribute);
  74          $this->assertEquals($attrs['ATTR2']->get_name(), 'ATTR2');
  75          $this->assertNull($attrs['ATTR2']->get_value());
  76  
  77          // Create instance with name and one string attribute
  78          $instance = new mock_base_final_element('TEST', 'ATTR1');
  79          $attrs = $instance->get_attributes();
  80          $this->assertTrue(is_array($attrs));
  81          $this->assertEquals(count($attrs), 1);
  82          $this->assertTrue($attrs['ATTR1'] instanceof base_attribute);
  83          $this->assertEquals($attrs['ATTR1']->get_name(), 'ATTR1');
  84          $this->assertNull($attrs['ATTR1']->get_value());
  85  
  86          // Create instance with name and various object attributes
  87          $instance = new mock_base_final_element('TEST', array('ATTR1', 'ATTR2'));
  88          $attrs = $instance->get_attributes();
  89          $attrs['ATTR1']->set_value('attr1_value');
  90          $this->assertTrue(is_array($attrs));
  91          $this->assertEquals(count($attrs), 2);
  92          $this->assertTrue($attrs['ATTR1'] instanceof base_attribute);
  93          $this->assertEquals($attrs['ATTR1']->get_name(), 'ATTR1');
  94          $this->assertEquals($attrs['ATTR1']->get_value(), 'attr1_value');
  95          $this->assertTrue($attrs['ATTR2'] instanceof base_attribute);
  96          $this->assertEquals($attrs['ATTR2']->get_name(), 'ATTR2');
  97          $this->assertNull($attrs['ATTR2']->get_value());
  98  
  99          // Clean values
 100          $instance = new mock_base_final_element('TEST', array('ATTR1', 'ATTR2'));
 101          $instance->set_value('instance_value');
 102          $attrs = $instance->get_attributes();
 103          $attrs['ATTR1']->set_value('attr1_value');
 104          $this->assertEquals($instance->get_value(), 'instance_value');
 105          $this->assertEquals($attrs['ATTR1']->get_value(), 'attr1_value');
 106          $instance->clean_values();
 107          $this->assertNull($instance->get_value());
 108          $this->assertNull($attrs['ATTR1']->get_value());
 109  
 110          // Get to_string() results (with values)
 111          $instance = new mock_base_final_element('TEST', array('ATTR1', 'ATTR2'));
 112          $instance->set_value('final element value');
 113          $attrs = $instance->get_attributes();
 114          $attrs['ATTR1']->set_value('attr1 value');
 115          $tostring = $instance->to_string(true);
 116          $this->assertTrue(strpos($tostring, '#TEST (level: 1)') !== false);
 117          $this->assertTrue(strpos($tostring, ' => ') !== false);
 118          $this->assertTrue(strpos($tostring, 'final element value') !== false);
 119          $this->assertTrue(strpos($tostring, 'attr1 value') !== false);
 120      }
 121  
 122      /**
 123       * Exception base_final_element tests
 124       */
 125      function test_base_final_element_exceptions() {
 126  
 127          // Create instance with invalid name
 128          try {
 129              $instance = new mock_base_final_element('');
 130              $this->fail("Expecting base_atom_struct_exception exception, none occurred");
 131          } catch (Exception $e) {
 132              $this->assertTrue($e instanceof base_atom_struct_exception);
 133          }
 134  
 135          // Create instance with incorrect (object) attribute
 136          try {
 137              $obj = new stdClass;
 138              $obj->name = 'test_attr';
 139              $instance = new mock_base_final_element('TEST', $obj);
 140              $this->fail("Expecting base_element_attribute_exception exception, none occurred");
 141          } catch (Exception $e) {
 142              $this->assertTrue($e instanceof base_element_attribute_exception);
 143          }
 144  
 145          // Create instance with array containing incorrect (object) attribute
 146          try {
 147              $obj = new stdClass;
 148              $obj->name = 'test_attr';
 149              $instance = new mock_base_final_element('TEST', array($obj));
 150              $this->fail("Expecting base_element_attribute_exception exception, none occurred");
 151          } catch (Exception $e) {
 152              $this->assertTrue($e instanceof base_element_attribute_exception);
 153          }
 154  
 155          // Create instance with array containing duplicate attributes
 156          try {
 157              $instance = new mock_base_final_element('TEST', array('ATTR1', 'ATTR2', 'ATTR1'));
 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  }