~ubuntu-branches/ubuntu/trusty/moodle/trusty-proposed

« back to all changes in this revision

Viewing changes to backup/util/structure/simpletest/testbaseatom.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2013-07-19 08:52:46 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20130719085246-yebwditc2exoap2r
Tags: 2.5.1-1
* New upstream version: 2.5.1.
  - Fixes security issues:
    CVE-2013-2242 CVE-2013-2243 CVE-2013-2244 CVE-2013-2245
    CVE-2013-2246
* Depend on apache2 instead of obsolete apache2-mpm-prefork.
* Use packaged libphp-phpmailer (closes: #429339), adodb,
  HTMLPurifier, PclZip.
* Update debconf translations, thanks Salvatore Merone, Pietro Tollot,
  Joe Hansen, Yuri Kozlov, Holger Wansing, Américo Monteiro,
  Adriano Rafael Gomes, victory, Michał Kułach.
  (closes: #716972, #716986, #717080, #717108, #717278)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
 
3
 
// This file is part of Moodle - http://moodle.org/
4
 
//
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.
9
 
//
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.
14
 
//
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/>.
17
 
 
18
 
/**
19
 
 * Unit tests for base_atom.class.php
20
 
 *
21
 
 * @package    moodlecore
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
25
 
 */
26
 
 
27
 
// Prevent direct access to this file
28
 
if (!defined('MOODLE_INTERNAL')) {
29
 
    die('Direct access to this script is forbidden.');
30
 
}
31
 
 
32
 
require_once($CFG->dirroot . '/backup/util/structure/base_atom.class.php');
33
 
 
34
 
/**
35
 
 * Unit test case the base_atom class. Note: as it's abstract we are testing
36
 
 * mock_base_atom instantiable class instead
37
 
 */
38
 
class base_atom_test extends UnitTestCase {
39
 
 
40
 
    public static $includecoverage = array('/backup/util/structure/base_atom.class.php');
41
 
 
42
 
    /**
43
 
     * Correct base_atom_tests
44
 
     */
45
 
    function test_base_atom() {
46
 
        $name_with_all_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
47
 
        $value_to_test = 'Some <value> to test';
48
 
 
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());
55
 
 
56
 
        // Set value
57
 
        $instance->set_value($value_to_test);
58
 
        $this->assertEqual($instance->get_value(), $value_to_test);
59
 
        $this->assertTrue($instance->is_set());
60
 
 
61
 
        // Clean value
62
 
        $instance->clean_value();
63
 
        $this->assertFalse($instance->is_set());
64
 
        $this->assertNull($instance->get_value());
65
 
 
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);
73
 
 
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));
79
 
    }
80
 
 
81
 
    /**
82
 
     * Throwing exception base_atom tests
83
 
     */
84
 
    function test_base_atom_exceptions() {
85
 
        // empty names
86
 
        try {
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);
91
 
        }
92
 
 
93
 
        // whitespace names
94
 
        try {
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);
99
 
        }
100
 
 
101
 
        // ascii names
102
 
        try {
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);
107
 
        }
108
 
        try {
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);
113
 
        }
114
 
 
115
 
        // setting already set value
116
 
        $instance = new mock_base_atom('TEST');
117
 
        $instance->set_value('test');
118
 
        try {
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);
123
 
        }
124
 
    }
125
 
}
126
 
 
127
 
/**
128
 
 * Instantiable class extending base_atom in order to be able to perform tests
129
 
 */
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();
135
 
    }
136
 
}