MDL-14905 Started on the DDL functional tests. Added an ugly but temporary hack into...
[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
11require_once($CFG->libdir . '/simpletestlib/web_tester.php');
12require_once($CFG->libdir . '/dmllib.php');
13require_once($CFG->libdir . '/dml/mysql_adodb_moodle_database.php');
14
15class dmllib_test extends UnitTestCase {
16 private $tables = array();
17 private $dbmanager;
18 private $db;
19
20 function setUp() {
21 global $CFG;
22
23 $this->db = new mysqli_adodb_moodle_database();
24 $this->db->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->dbpersist, $CFG->prefix);
25 $this->dbmanager = $this->db->get_manager();
26
27 $table = new xmldb_table("testtable");
28 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
29 $table->addFieldInfo('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
30 $table->addFieldInfo('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM,
31 array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general');
32 $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null);
33 $table->addFieldInfo('intro', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
34 $table->addFieldInfo('logo', XMLDB_TYPE_BINARY, 'big', null, XMLDB_NOTNULL, null, null, null);
35 $table->addFieldInfo('assessed', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
36 $table->addFieldInfo('assesstimestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
37 $table->addFieldInfo('assesstimefinish', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
38 $table->addFieldInfo('scale', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
39 $table->addFieldInfo('maxbytes', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
40 $table->addFieldInfo('forcesubscribe', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
41 $table->addFieldInfo('trackingtype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1');
42 $table->addFieldInfo('rsstype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
43 $table->addFieldInfo('rssarticles', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
44 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
45 $table->addFieldInfo('grade', XMLDB_TYPE_NUMBER, '20,0', XMLDB_UNSIGNED, null, null, null, null, null);
46 $table->addFieldInfo('percent', XMLDB_TYPE_NUMBER, '5,2', null, null, null, null, null, null);
47 $table->addFieldInfo('warnafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
48 $table->addFieldInfo('blockafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
49 $table->addFieldInfo('blockperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
50 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
51 $table->addIndexInfo('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
52
53 $table->setComment("This is a test'n drop table. You can drop it safely");
54
55 $this->dbmanager->create_table($table);
56 $this->tables[] = $table;
57
58 // insert records
59 $datafile = $CFG->libdir . '/dml/simpletest/fixtures/testdata.xml';
60 $xml = simplexml_load_file($datafile);
61
62 foreach ($xml->record as $record) {
63 $this->db->insert_record('testtable', $record);
64 }
65 }
66
67 function tearDown() {
68 foreach ($this->tables as $key => $table) {
69 if ($this->dbmanager->table_exists($table)) {
70 $this->dbmanager->drop_table($table, true, false);
71 }
72 }
73 unset($this->tables);
74
75 setup_DB();
76 }
77
78 function test_insert_record() {
79
80 }
81
82 function test_get_record_select() {
83 $record = $this->db->get_record_select('testtable', 'id = 1');
84 }
85
86 function test_fix_sql_params() {
87 // Malformed table placeholder
88 $sql = "SELECT * FROM [testtable]";
89 $sqlarray = $this->db->fix_sql_params($sql);
90 $this->assertEqual($sql, $sqlarray[0]);
91
92 // Correct table placeholder substitution
93 $sql = "SELECT * FROM {testtable}";
94 $sqlarray = $this->db->fix_sql_params($sql);
95 $this->assertEqual("SELECT * FROM {$this->db->get_prefix()}testtable", $sqlarray[0]);
96
97 // Malformed param placeholders
98 $sql = "SELECT * FROM {testtable} WHERE name = ?param1";
99 $params = array('param1' => 'first record');
100 $sqlarray = $this->db->fix_sql_params($sql, $params);
101 $this->assertEqual("SELECT * FROM {$this->db->get_prefix()}testtable WHERE name = ?param1", $sqlarray[0]);
102
103 // Mixed param types (colon and dollar)
104 $sql = "SELECT * FROM {testtable} WHERE name = :param1, rsstype = \$1";
105 $params = array('param1' => 'first record', 'param2' => 1);
106 try {
107 $sqlarray = $this->db->fix_sql_params($sql, $params);
108 } catch (Exception $e) {
109 $this->assertEqual('error() call: ERROR: Mixed types of sql query parameters!!', $e->getMessage());
110 }
111
112 // Mixed param types (question and dollar)
113 $sql = "SELECT * FROM {testtable} WHERE name = ?, rsstype = \$1";
114 $params = array('param1' => 'first record', 'param2' => 1);
115 $exception_caught = false;
116 try {
117 $sqlarray = $this->db->fix_sql_params($sql, $params);
118 } catch (Exception $e) {
119 $exception_caught = true;
120 }
121 $this->assertTrue($exception_caught);
122
123 // Too many params in sql
124 $sql = "SELECT * FROM {testtable} WHERE name = ?, rsstype = ?, course = ?";
125 $params = array('first record', 1);
126 $exception_caught = false;
127 try {
128 $sqlarray = $this->db->fix_sql_params($sql, $params);
129 } catch (Exception $e) {
130 $exception_caught = true;
131 }
132 $this->assertTrue($exception_caught);
133
134 // Too many params in array: no error
135 $params[] = 1;
136 $params[] = time();
137 $exception_caught = false;
138 $sqlarray = null;
139
140 try {
141 $sqlarray = $this->db->fix_sql_params($sql, $params);
142 } catch (Exception $e) {
143 $exception_caught = true;
144 }
145 $this->assertFalse($exception_caught);
146 $this->assertTrue($sqlarray[0]);
147
148 // Named params missing from array
149 $sql = "SELECT * FROM {testtable} WHERE name = :name, rsstype = :rsstype";
150 $params = array('wrongname' => 'first record', 'rsstype' => 1);
151 $exception_caught = false;
152 try {
153 $sqlarray = $this->db->fix_sql_params($sql, $params);
154 } catch (Exception $e) {
155 $exception_caught = true;
156 }
157 $this->assertTrue($exception_caught);
158
159 // Duplicate named param in query
160 $sql = "SELECT * FROM {testtable} WHERE name = :name, rsstype = :name";
161 $params = array('name' => 'first record', 'rsstype' => 1);
162 $exception_caught = false;
163 try {
164 $sqlarray = $this->db->fix_sql_params($sql, $params);
165 } catch (Exception $e) {
166 $exception_caught = true;
167 }
168 $this->assertTrue($exception_caught);
169
170 // Unsupported Bound params
171 $sql = "SELECT * FROM {testtable} WHERE name = $1, rsstype = $2";
172 $params = array('first record', 1);
173 $exception_caught = false;
174 try {
175 $sqlarray = $this->db->fix_sql_params($sql, $params);
176 } catch (Exception $e) {
177 $exception_caught = true;
178 }
179 $this->assertTrue($exception_caught);
180
181 // Correct named param placeholders
182 $sql = "SELECT * FROM {testtable} WHERE name = :name, rsstype = :rsstype";
183 $params = array('name' => 'first record', 'rsstype' => 1);
184 $sqlarray = $this->db->fix_sql_params($sql, $params);
185 $this->assertEqual("SELECT * FROM {$this->db->get_prefix()}testtable WHERE name = ?, rsstype = ?", $sqlarray[0]);
186 $this->assertEqual(2, count($sqlarray[1]));
187
188 // Correct ? params
189 $sql = "SELECT * FROM {testtable} WHERE name = ?, rsstype = ?";
190 $params = array('first record', 1);
191 $sqlarray = $this->db->fix_sql_params($sql, $params);
192 $this->assertEqual("SELECT * FROM {$this->db->get_prefix()}testtable WHERE name = ?, rsstype = ?", $sqlarray[0]);
193 $this->assertEqual(2, count($sqlarray[1]));
194
195 }
196
197}
198
199?>