unittests: NOBUG further pagelib unit test fixes
[moodle.git] / lib / simpletest / testsimpletestlib.php
CommitLineData
821e4ecf 1<?php
2/**
b1bc1263 3 * Unit tests for (some of) ../simpletestlib.php.
821e4ecf 4 *
821e4ecf 5 * @author T.J.Hunt@open.ac.uk
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
b1bc1263 7 * @package SimpleTestEx
821e4ecf 8 */
9
10if (!defined('MOODLE_INTERNAL')) {
11 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
12}
13
58fa5d6f 14class simpletestlib_test extends FakeDBUnitTestCase {
821e4ecf 15
b1bc1263 16 function test_load_delete_test_data() {
821e4ecf 17 global $DB;
821e4ecf 18 $contexts = $this->load_test_data('context',
19 array('contextlevel', 'instanceid', 'path', 'depth'), array(
20 array(10, 666, '', 1),
21 array(40, 666, '', 2),
22 array(50, 666, '', 3),
23 ));
821e4ecf 24
25 // Just test load_test_data and delete_test_data for now.
26 $this->assertTrue($DB->record_exists('context', array('id' => $contexts[1]->id)));
b1bc1263 27 $this->assertTrue($DB->get_field('context', 'contextlevel', array('id' => $contexts[2]->id)), $contexts[2]->contextlevel);
821e4ecf 28 $this->delete_test_data('context', $contexts);
29 $this->assertFalse($DB->record_exists('context', array('id' => $contexts[1]->id)));
30 }
31}
b1bc1263 32
8954245a 33
34/**
35 * Unit tests for the ContainsTagWithAttribute_test class.
36 *
37 * @copyright 2009 Tim Hunt
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class ContainsTagWithAttribute_test extends UnitTestCase {
41 function test_simple() {
42 $expectation = new ContainsTagWithAttribute('span', 'class', 'error');
43 $this->assertTrue($expectation->test('<span class="error">message</span>'));
44 }
45
46 function test_other_attrs() {
47 $expectation = new ContainsTagWithAttribute('span', 'class', 'error');
6a923128 48 $this->assertTrue($expectation->test('<span oneattr="thingy" class = "error" otherattr="thingy">message</span>'));
8954245a 49 }
50
51 function test_fails() {
52 $expectation = new ContainsTagWithAttribute('span', 'class', 'error');
53 $this->assertFalse($expectation->test('<span class="mismatch">message</span>'));
54 }
5daf5d35 55
56 function test_link() {
6a923128 57 $html = '<a href="http://www.test.com">Click Here</a>';
5daf5d35 58 $expectation = new ContainsTagWithAttribute('a', 'href', 'http://www.test.com');
6a923128 59 $this->assertTrue($expectation->test($html));
60 $this->assert(new ContainsTagWithContents('a', 'Click Here'), $html);
61 }
62
63 function test_garbage() {
64 $expectation = new ContainsTagWithAttribute('a', 'href', '!#@*%@_-)(*#0-735\\fdf//fdfg235-0970}$@}{#:~');
65 $this->assertTrue($expectation->test('<a href="!#@*%@_-)(*#0-735\\fdf//fdfg235-0970}$@}{#:~">Click Here</a>'));
66
67 }
68
69 function test_inline_js() {
70 $html = '<a title="Popup window" href="http://otheraddress.com" class="link" onclick="this.target=\'my_popup\';">Click here</a>';
71 $this->assert(new ContainsTagWithAttribute('a', 'href', 'http://otheraddress.com'), $html);
5daf5d35 72 }
8954245a 73}
74
75/**
76 * Unit tests for the ContainsTagWithAttribute_test class.
77 *
78 * @copyright 2009 Tim Hunt
79 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
80 */
81class ContainsTagWithContents_test extends UnitTestCase {
82 function test_simple() {
83 $expectation = new ContainsTagWithContents('span', 'message');
84 $this->assertTrue($expectation->test('<span class="error">message</span>'));
85 }
86
87 function test_no_end() {
88 $expectation = new ContainsTagWithContents('span', 'message');
89 $this->assertFalse($expectation->test('<span class="error">message'));
90 }
91}
92
821e4ecf 93?>