From 2b3ccae5f9042e184d6a8ae16d08c5c817d9fd41 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sun, 5 Sep 2010 15:48:19 +0000 Subject: [PATCH] MDL-14679 more get_field() unittests --- lib/dml/simpletest/testdml.php | 39 ++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/lib/dml/simpletest/testdml.php b/lib/dml/simpletest/testdml.php index ebdafc69a40..981308062aa 100755 --- a/lib/dml/simpletest/testdml.php +++ b/lib/dml/simpletest/testdml.php @@ -1045,6 +1045,8 @@ class dml_test extends UnitTestCase { } public function test_get_field() { + global $CFG; + $DB = $this->tdb; $dbman = $DB->get_manager(); @@ -1059,15 +1061,38 @@ class dml_test extends UnitTestCase { $id1 = $DB->insert_record($tablename, array('course' => 3)); $DB->insert_record($tablename, array('course' => 5)); - $DB->insert_record($tablename, array('course' => 7)); + $DB->insert_record($tablename, array('course' => 5)); - $this->assertTrue($course = $DB->get_field($tablename, 'course', array('id' => $id1))); - $this->assertEqual(3, $course); - - // Add one get_field() example where the field being get is also one condition - // to check we haven't problem with any type of param (specially NAMED ones) - $DB->get_field($tablename, 'course', array('course' => 3)); + $this->assertEqual(3, $DB->get_field($tablename, 'course', array('id' => $id1))); $this->assertEqual(3, $DB->get_field($tablename, 'course', array('course' => 3))); + + $this->assertIdentical(false, $DB->get_field($tablename, 'course', array('course' => 11), IGNORE_MISSING)); + try { + $DB->get_field($tablename, 'course', array('course' => 4), MUST_EXIST); + $this->assertFail('Exception expected due to missing record'); + } catch (dml_exception $ex) { + $this->assertTrue(true); + } + + $olddebug = $CFG->debug; // Save current debug settings + $olddisplay = $CFG->debugdisplay; + $CFG->debug = DEBUG_DEVELOPER; + $CFG->debugdisplay = true; + + ob_start(); // catch debug warning + $this->assertEqual(5, $DB->get_field($tablename, 'course', array('course' => 5), IGNORE_MULTIPLE)); + $debuginfo = ob_get_contents(); + ob_end_clean(); + $this->assertTrue($debuginfo === ''); + + ob_start(); // catch debug warning + $this->assertEqual(5, $DB->get_field($tablename, 'course', array('course' => 5), IGNORE_MISSING)); + $debuginfo = ob_get_contents(); + ob_end_clean(); + $this->assertFalse($debuginfo === ''); + + $CFG->debug = $olddebug; // Restore original debug settings + $CFG->debugdisplay = $olddisplay; } public function test_get_field_select() {