1 |
efrain |
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_content_exception;
|
|
|
20 |
use base_atom_struct_exception;
|
|
|
21 |
use mock_base_atom;
|
|
|
22 |
|
|
|
23 |
defined('MOODLE_INTERNAL') || die();
|
|
|
24 |
|
|
|
25 |
// Include all the needed stuff
|
|
|
26 |
require_once(__DIR__.'/fixtures/structure_fixtures.php');
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Unit test case the base_atom class.
|
|
|
31 |
*
|
|
|
32 |
* Note: as it's abstract we are testing mock_base_atom instantiable class instead
|
|
|
33 |
*
|
|
|
34 |
* @package core_backup
|
|
|
35 |
* @category test
|
|
|
36 |
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
|
|
|
37 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
38 |
*/
|
|
|
39 |
class baseatom_test extends \basic_testcase {
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Correct base_atom_tests
|
|
|
43 |
*/
|
11 |
efrain |
44 |
function test_base_atom(): void {
|
1 |
efrain |
45 |
$name_with_all_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
|
|
|
46 |
$value_to_test = 'Some <value> to test';
|
|
|
47 |
|
|
|
48 |
// Create instance with correct names
|
|
|
49 |
$instance = new mock_base_atom($name_with_all_chars);
|
|
|
50 |
$this->assertInstanceOf('base_atom', $instance);
|
|
|
51 |
$this->assertEquals($instance->get_name(), $name_with_all_chars);
|
|
|
52 |
$this->assertFalse($instance->is_set());
|
|
|
53 |
$this->assertNull($instance->get_value());
|
|
|
54 |
|
|
|
55 |
// Set value
|
|
|
56 |
$instance->set_value($value_to_test);
|
|
|
57 |
$this->assertEquals($instance->get_value(), $value_to_test);
|
|
|
58 |
$this->assertTrue($instance->is_set());
|
|
|
59 |
|
|
|
60 |
// Clean value
|
|
|
61 |
$instance->clean_value();
|
|
|
62 |
$this->assertFalse($instance->is_set());
|
|
|
63 |
$this->assertNull($instance->get_value());
|
|
|
64 |
|
|
|
65 |
// Get to_string() results (with values)
|
|
|
66 |
$instance = new mock_base_atom($name_with_all_chars);
|
|
|
67 |
$instance->set_value($value_to_test);
|
|
|
68 |
$tostring = $instance->to_string(true);
|
|
|
69 |
$this->assertTrue(strpos($tostring, $name_with_all_chars) !== false);
|
|
|
70 |
$this->assertTrue(strpos($tostring, ' => ') !== false);
|
|
|
71 |
$this->assertTrue(strpos($tostring, $value_to_test) !== false);
|
|
|
72 |
|
|
|
73 |
// Get to_string() results (without values)
|
|
|
74 |
$tostring = $instance->to_string(false);
|
|
|
75 |
$this->assertTrue(strpos($tostring, $name_with_all_chars) !== false);
|
|
|
76 |
$this->assertFalse(strpos($tostring, ' => '));
|
|
|
77 |
$this->assertFalse(strpos($tostring, $value_to_test));
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Throwing exception base_atom tests
|
|
|
82 |
*/
|
11 |
efrain |
83 |
function test_base_atom_exceptions(): void {
|
1 |
efrain |
84 |
// empty names
|
|
|
85 |
try {
|
|
|
86 |
$instance = new mock_base_atom('');
|
|
|
87 |
$this->fail("Expecting base_atom_struct_exception exception, none occurred");
|
|
|
88 |
} catch (\Exception $e) {
|
|
|
89 |
$this->assertTrue($e instanceof base_atom_struct_exception);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
// whitespace names
|
|
|
93 |
try {
|
|
|
94 |
$instance = new mock_base_atom('TESTING ATOM');
|
|
|
95 |
$this->fail("Expecting base_atom_struct_exception exception, none occurred");
|
|
|
96 |
} catch (\Exception $e) {
|
|
|
97 |
$this->assertTrue($e instanceof base_atom_struct_exception);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
// ascii names
|
|
|
101 |
try {
|
|
|
102 |
$instance = new mock_base_atom('TESTING-ATOM');
|
|
|
103 |
$this->fail("Expecting base_atom_struct_exception exception, none occurred");
|
|
|
104 |
} catch (\Exception $e) {
|
|
|
105 |
$this->assertTrue($e instanceof base_atom_struct_exception);
|
|
|
106 |
}
|
|
|
107 |
try {
|
|
|
108 |
$instance = new mock_base_atom('TESTING_ATOM_Á');
|
|
|
109 |
$this->fail("Expecting base_atom_struct_exception exception, none occurred");
|
|
|
110 |
} catch (\Exception $e) {
|
|
|
111 |
$this->assertTrue($e instanceof base_atom_struct_exception);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
// setting already set value
|
|
|
115 |
$instance = new mock_base_atom('TEST');
|
|
|
116 |
$instance->set_value('test');
|
|
|
117 |
try {
|
|
|
118 |
$instance->set_value('test');
|
|
|
119 |
$this->fail("Expecting base_atom_content_exception exception, none occurred");
|
|
|
120 |
} catch (\Exception $e) {
|
|
|
121 |
$this->assertTrue($e instanceof base_atom_content_exception);
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
}
|