MDL-21015 migrate testsimpletestlib to UnitTestCaseUsingDatabase
[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
8eb32141 14class simpletestlib_test extends UnitTestCaseUsingDatabase {
821e4ecf 15
8eb32141 16 function test_table_creation_and_data() {
821e4ecf 17 global $DB;
8eb32141
EL
18
19 $this->switch_to_test_db(); // All operations until end of test method will happen in test DB
20 $dbman = $DB->get_manager();
21
22 // Create table and test
23 $this->create_test_table('context', 'lib');
24 $this->assertTrue($dbman->table_exists('context'));
25
26 $sampledata = array(
27 array('contextlevel' => 10, 'instanceid' => 666, 'path' => '', 'depth' => 1),
28 array('contextlevel' => 40, 'instanceid' => 666, 'path' => '', 'depth' => 2),
29 array('contextlevel' => 50, 'instanceid' => 666, 'path' => '', 'depth' => 3));
30
31 foreach($sampledata as $key => $record) {
32 $sampledata[$key]['id'] = $DB->insert_record('context', $record);
33 }
34
35 // Just test added data and delete later
36 $this->assertEqual($DB->count_records('context'), 3);
37 $this->assertTrue($DB->record_exists('context', array('id' => $sampledata[0]['id'])));
38 $this->assertTrue($DB->get_field('context', 'contextlevel', array('id' => $sampledata[2]['id'])), $sampledata[2]['contextlevel']);
39 $DB->delete_records('context');
40 $this->assertFalse($DB->record_exists('context', array('id' => $sampledata[1]['id'])));
41 }
42
43 function test_tables_are_dropped() {
44 global $DB;
45
46 $this->switch_to_test_db(); // All operations until end of test method will happen in test DB
47 $dbman = $DB->get_manager();
48 // Previous method tearDown *must* delete all created tables, so here 'context' must not exist anymore
49 $this->assertFalse($dbman->table_exists('context'));
821e4ecf 50 }
51}
b1bc1263 52
8954245a 53
54/**
55 * Unit tests for the ContainsTagWithAttribute_test class.
56 *
57 * @copyright 2009 Tim Hunt
58 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
59 */
60class ContainsTagWithAttribute_test extends UnitTestCase {
61 function test_simple() {
62 $expectation = new ContainsTagWithAttribute('span', 'class', 'error');
63 $this->assertTrue($expectation->test('<span class="error">message</span>'));
64 }
65
66 function test_other_attrs() {
67 $expectation = new ContainsTagWithAttribute('span', 'class', 'error');
6a923128 68 $this->assertTrue($expectation->test('<span oneattr="thingy" class = "error" otherattr="thingy">message</span>'));
8954245a 69 }
70
71 function test_fails() {
72 $expectation = new ContainsTagWithAttribute('span', 'class', 'error');
73 $this->assertFalse($expectation->test('<span class="mismatch">message</span>'));
74 }
5daf5d35 75
76 function test_link() {
6a923128 77 $html = '<a href="http://www.test.com">Click Here</a>';
5daf5d35 78 $expectation = new ContainsTagWithAttribute('a', 'href', 'http://www.test.com');
6a923128 79 $this->assertTrue($expectation->test($html));
80 $this->assert(new ContainsTagWithContents('a', 'Click Here'), $html);
81 }
82
83 function test_garbage() {
84 $expectation = new ContainsTagWithAttribute('a', 'href', '!#@*%@_-)(*#0-735\\fdf//fdfg235-0970}$@}{#:~');
85 $this->assertTrue($expectation->test('<a href="!#@*%@_-)(*#0-735\\fdf//fdfg235-0970}$@}{#:~">Click Here</a>'));
86
87 }
88
89 function test_inline_js() {
90 $html = '<a title="Popup window" href="http://otheraddress.com" class="link" onclick="this.target=\'my_popup\';">Click here</a>';
91 $this->assert(new ContainsTagWithAttribute('a', 'href', 'http://otheraddress.com'), $html);
5daf5d35 92 }
91b652ac 93
94 function test_real_regression1() {
95 $expectation = new ContainsTagWithAttribute('label', 'for', 'html_select4ac387224bf9d');
96 $html = '<label for="html_select4ac387224bf9d">Cool menu</label><select name="mymenu" id="html_select4ac387224bf9d" class="menumymenu select"> <option value="0">Choose...</option><option value="10">ten</option><option value="c2">two</option></select>';
97 $this->assert($expectation, $html);
98 }
8954245a 99}
100
101/**
102 * Unit tests for the ContainsTagWithAttribute_test class.
103 *
104 * @copyright 2009 Tim Hunt
105 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
106 */
107class ContainsTagWithContents_test extends UnitTestCase {
108 function test_simple() {
109 $expectation = new ContainsTagWithContents('span', 'message');
110 $this->assertTrue($expectation->test('<span class="error">message</span>'));
111 }
112
113 function test_no_end() {
114 $expectation = new ContainsTagWithContents('span', 'message');
115 $this->assertFalse($expectation->test('<span class="error">message'));
116 }
117}
118
8926f844 119