3
// This file is part of Moodle - http://moodle.org/
5
// Moodle is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
10
// Moodle is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// GNU General Public License for more details.
15
// You should have received a copy of the GNU General Public License
16
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19
* Unit tests for base_atom.class.php
22
* @subpackage backup-tests
23
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
24
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
// Prevent direct access to this file
28
if (!defined('MOODLE_INTERNAL')) {
29
die('Direct access to this script is forbidden.');
32
require_once($CFG->dirroot . '/backup/util/structure/base_atom.class.php');
35
* Unit test case the base_atom class. Note: as it's abstract we are testing
36
* mock_base_atom instantiable class instead
38
class base_atom_test extends UnitTestCase {
40
public static $includecoverage = array('/backup/util/structure/base_atom.class.php');
43
* Correct base_atom_tests
45
function test_base_atom() {
46
$name_with_all_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
47
$value_to_test = 'Some <value> to test';
49
// Create instance with correct names
50
$instance = new mock_base_atom($name_with_all_chars);
51
$this->assertIsA($instance, 'base_atom');
52
$this->assertEqual($instance->get_name(), $name_with_all_chars);
53
$this->assertFalse($instance->is_set());
54
$this->assertNull($instance->get_value());
57
$instance->set_value($value_to_test);
58
$this->assertEqual($instance->get_value(), $value_to_test);
59
$this->assertTrue($instance->is_set());
62
$instance->clean_value();
63
$this->assertFalse($instance->is_set());
64
$this->assertNull($instance->get_value());
66
// Get to_string() results (with values)
67
$instance = new mock_base_atom($name_with_all_chars);
68
$instance->set_value($value_to_test);
69
$tostring = $instance->to_string(true);
70
$this->assertTrue(strpos($tostring, $name_with_all_chars) !== false);
71
$this->assertTrue(strpos($tostring, ' => ') !== false);
72
$this->assertTrue(strpos($tostring, $value_to_test) !== false);
74
// Get to_string() results (without values)
75
$tostring = $instance->to_string(false);
76
$this->assertTrue(strpos($tostring, $name_with_all_chars) !== false);
77
$this->assertFalse(strpos($tostring, ' => '));
78
$this->assertFalse(strpos($tostring, $value_to_test));
82
* Throwing exception base_atom tests
84
function test_base_atom_exceptions() {
87
$instance = new mock_base_atom('');
88
$this->fail("Expecting base_atom_struct_exception exception, none occurred");
89
} catch (Exception $e) {
90
$this->assertTrue($e instanceof base_atom_struct_exception);
95
$instance = new mock_base_atom('TESTING ATOM');
96
$this->fail("Expecting base_atom_struct_exception exception, none occurred");
97
} catch (Exception $e) {
98
$this->assertTrue($e instanceof base_atom_struct_exception);
103
$instance = new mock_base_atom('TESTING-ATOM');
104
$this->fail("Expecting base_atom_struct_exception exception, none occurred");
105
} catch (Exception $e) {
106
$this->assertTrue($e instanceof base_atom_struct_exception);
109
$instance = new mock_base_atom('TESTING_ATOM_Á');
110
$this->fail("Expecting base_atom_struct_exception exception, none occurred");
111
} catch (Exception $e) {
112
$this->assertTrue($e instanceof base_atom_struct_exception);
115
// setting already set value
116
$instance = new mock_base_atom('TEST');
117
$instance->set_value('test');
119
$instance->set_value('test');
120
$this->fail("Expecting base_atom_content_exception exception, none occurred");
121
} catch (Exception $e) {
122
$this->assertTrue($e instanceof base_atom_content_exception);
128
* Instantiable class extending base_atom in order to be able to perform tests
130
class mock_base_atom extends base_atom {
131
// Nothing new in this class, just an instantiable base_atom class
132
// with the is_set() method public for testing purposes
133
public function is_set() {
134
return parent::is_set();