MDL-15204 Update complete
[moodle.git] / lib / dml / simpletest / testdmllib.php
CommitLineData
73f7ad71 1<?php
2/**
3 * Unit tests for dmllib
4 * @package dmllib
5 */
6
7if (!defined('MOODLE_INTERNAL')) {
8 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
9}
10
73f7ad71 11class dmllib_test extends UnitTestCase {
12 private $tables = array();
73f7ad71 13 private $db;
14
15 function setUp() {
e6b4f00e 16 global $CFG, $DB, $EXT_TEST_DB;
17
18 if (isset($EXT_TEST_DB)) {
19 $this->db = $EXT_TEST_DB;
20 } else {
21 $this->db = $DB;
22 }
73f7ad71 23
e6b4f00e 24 $dbmanager = $this->db->get_manager();
73f7ad71 25
26 $table = new xmldb_table("testtable");
e6b4f00e 27 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
28 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
29 $table->add_field('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM,
73f7ad71 30 array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general');
e6b4f00e 31 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null);
32 $table->add_field('intro', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
33 $table->add_field('logo', XMLDB_TYPE_BINARY, 'big', null, XMLDB_NOTNULL, null, null, null);
34 $table->add_field('assessed', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
35 $table->add_field('assesstimestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
36 $table->add_field('assesstimefinish', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
37 $table->add_field('scale', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
38 $table->add_field('maxbytes', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
39 $table->add_field('forcesubscribe', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
40 $table->add_field('trackingtype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1');
41 $table->add_field('rsstype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
42 $table->add_field('rssarticles', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
43 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
44 $table->add_field('grade', XMLDB_TYPE_NUMBER, '20,0', XMLDB_UNSIGNED, null, null, null, null, null);
45 $table->add_field('percent', XMLDB_TYPE_NUMBER, '5,2', null, null, null, null, null, null);
46 $table->add_field('warnafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
47 $table->add_field('blockafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
48 $table->add_field('blockperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
49 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
50 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
73f7ad71 51 $table->setComment("This is a test'n drop table. You can drop it safely");
52
e6b4f00e 53 if ($dbmanager->table_exists($table)) {
54 $dbmanager->drop_table($table, true, false);
55 }
73f7ad71 56
e6b4f00e 57 $dbmanager->create_table($table);
58 $tables[$table->getName()] = $table;
73f7ad71 59
73f7ad71 60 }
61
62 function tearDown() {
63 foreach ($this->tables as $key => $table) {
64 if ($this->dbmanager->table_exists($table)) {
65 $this->dbmanager->drop_table($table, true, false);
66 }
67 }
e6b4f00e 68 $this->tables = array();
73f7ad71 69 }
70
71 function test_fix_sql_params() {
e6b4f00e 72 $DB = $this->db; // do not use global $DB!
73
73f7ad71 74 // Malformed table placeholder
75 $sql = "SELECT * FROM [testtable]";
e6b4f00e 76 $sqlarray = $DB->fix_sql_params($sql);
73f7ad71 77 $this->assertEqual($sql, $sqlarray[0]);
78
79 // Correct table placeholder substitution
80 $sql = "SELECT * FROM {testtable}";
e6b4f00e 81 $sqlarray = $DB->fix_sql_params($sql);
82 $this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable", $sqlarray[0]);
73f7ad71 83
84 // Malformed param placeholders
85 $sql = "SELECT * FROM {testtable} WHERE name = ?param1";
86 $params = array('param1' => 'first record');
e6b4f00e 87 $sqlarray = $DB->fix_sql_params($sql, $params);
88 $this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable WHERE name = ?param1", $sqlarray[0]);
73f7ad71 89
90 // Mixed param types (colon and dollar)
91 $sql = "SELECT * FROM {testtable} WHERE name = :param1, rsstype = \$1";
92 $params = array('param1' => 'first record', 'param2' => 1);
93 try {
e6b4f00e 94 $sqlarray = $DB->fix_sql_params($sql, $params);
73f7ad71 95 } catch (Exception $e) {
96 $this->assertEqual('error() call: ERROR: Mixed types of sql query parameters!!', $e->getMessage());
97 }
98
99 // Mixed param types (question and dollar)
100 $sql = "SELECT * FROM {testtable} WHERE name = ?, rsstype = \$1";
101 $params = array('param1' => 'first record', 'param2' => 1);
102 $exception_caught = false;
103 try {
e6b4f00e 104 $sqlarray = $DB->fix_sql_params($sql, $params);
73f7ad71 105 } catch (Exception $e) {
106 $exception_caught = true;
107 }
108 $this->assertTrue($exception_caught);
109
110 // Too many params in sql
111 $sql = "SELECT * FROM {testtable} WHERE name = ?, rsstype = ?, course = ?";
112 $params = array('first record', 1);
113 $exception_caught = false;
114 try {
e6b4f00e 115 $sqlarray = $DB->fix_sql_params($sql, $params);
73f7ad71 116 } catch (Exception $e) {
117 $exception_caught = true;
118 }
119 $this->assertTrue($exception_caught);
120
121 // Too many params in array: no error
122 $params[] = 1;
123 $params[] = time();
124 $exception_caught = false;
125 $sqlarray = null;
126
127 try {
e6b4f00e 128 $sqlarray = $DB->fix_sql_params($sql, $params);
73f7ad71 129 } catch (Exception $e) {
130 $exception_caught = true;
131 }
132 $this->assertFalse($exception_caught);
133 $this->assertTrue($sqlarray[0]);
134
135 // Named params missing from array
136 $sql = "SELECT * FROM {testtable} WHERE name = :name, rsstype = :rsstype";
137 $params = array('wrongname' => 'first record', 'rsstype' => 1);
138 $exception_caught = false;
139 try {
e6b4f00e 140 $sqlarray = $DB->fix_sql_params($sql, $params);
73f7ad71 141 } catch (Exception $e) {
142 $exception_caught = true;
143 }
144 $this->assertTrue($exception_caught);
145
146 // Duplicate named param in query
147 $sql = "SELECT * FROM {testtable} WHERE name = :name, rsstype = :name";
148 $params = array('name' => 'first record', 'rsstype' => 1);
149 $exception_caught = false;
150 try {
e6b4f00e 151 $sqlarray = $DB->fix_sql_params($sql, $params);
73f7ad71 152 } catch (Exception $e) {
153 $exception_caught = true;
154 }
155 $this->assertTrue($exception_caught);
156
157 // Unsupported Bound params
158 $sql = "SELECT * FROM {testtable} WHERE name = $1, rsstype = $2";
159 $params = array('first record', 1);
160 $exception_caught = false;
161 try {
e6b4f00e 162 $sqlarray = $DB->fix_sql_params($sql, $params);
73f7ad71 163 } catch (Exception $e) {
164 $exception_caught = true;
165 }
166 $this->assertTrue($exception_caught);
167
168 // Correct named param placeholders
169 $sql = "SELECT * FROM {testtable} WHERE name = :name, rsstype = :rsstype";
170 $params = array('name' => 'first record', 'rsstype' => 1);
e6b4f00e 171 $sqlarray = $DB->fix_sql_params($sql, $params);
172 $this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable WHERE name = ?, rsstype = ?", $sqlarray[0]);
73f7ad71 173 $this->assertEqual(2, count($sqlarray[1]));
174
175 // Correct ? params
176 $sql = "SELECT * FROM {testtable} WHERE name = ?, rsstype = ?";
177 $params = array('first record', 1);
e6b4f00e 178 $sqlarray = $DB->fix_sql_params($sql, $params);
179 $this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable WHERE name = ?, rsstype = ?", $sqlarray[0]);
73f7ad71 180 $this->assertEqual(2, count($sqlarray[1]));
181
182 }
183
184}
185
186?>