<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ca">
	<id>https://docs.moodle.org/2x/ca/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Audun</id>
	<title>MoodleDocs - Contribucions de l&amp;#039;usuari [ca]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.moodle.org/2x/ca/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Audun"/>
	<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/Especial:Contribucions/Audun"/>
	<updated>2026-04-21T08:28:21Z</updated>
	<subtitle>Contribucions de l&amp;#039;usuari</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/DML_functions&amp;diff=16669</id>
		<title>Broken/DML functions</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/DML_functions&amp;diff=16669"/>
		<updated>2006-10-05T21:46:02Z</updated>

		<summary type="html">&lt;p&gt;Audun: Spelling/grammar&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this page you&#039;ll access to the available functions under Moodle to be able to access to DB data. You should use &#039;&#039;&#039;exclusively&#039;&#039;&#039; these functions in order to retrieve or modify DB contents because these functions provide an high level of abstraction and guarantee that your DB manipulation will work against different RDBMS.&lt;br /&gt;
&lt;br /&gt;
Where possible, tricks and examples will be documented here in order to make developers&#039;s life a bit easier. Of course, feel free to clarify, complete and add more info to all this documentation. It will be welcome, absolutely!&lt;br /&gt;
&lt;br /&gt;
== Main info ==&lt;br /&gt;
&lt;br /&gt;
== The functions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      &#039;&#039;&#039;Seeing if any records exist match a given criteria&#039;&#039;&#039;&lt;br /&gt;
          o record_exists($table, $field1=&#039;&#039;, $value1=&#039;&#039;, $field2=&#039;&#039;, $value2=&#039;&#039;, $field3=&#039;&#039;, $value3=&#039;&#039;) &lt;br /&gt;
            Test whether a record exists in a table where all the given fields match the given values.&lt;br /&gt;
          o record_exists_select($table, $select=&#039;&#039;) &lt;br /&gt;
            Test whether any records exists in a table which match a particular WHERE clause.&lt;br /&gt;
          o record_exists_sql($sql) &lt;br /&gt;
            Test whether a SQL SELECT statement returns any records.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      &#039;&#039;&#039;Seeing how many records match a given criteria&#039;&#039;&#039;&lt;br /&gt;
          o count_records($table, $field1=&#039;&#039;, $value1=&#039;&#039;, $field2=&#039;&#039;, $value2=&#039;&#039;, $field3=&#039;&#039;, $value3=&#039;&#039;) &lt;br /&gt;
            Count the records in a table where all the given fields match the given values.&lt;br /&gt;
          o count_records_select($table, $select=&#039;&#039;, $countitem=&#039;COUNT(*)&#039;) &lt;br /&gt;
            Count the records in a table which match a particular WHERE clause.&lt;br /&gt;
          o count_records_sql($sql) &lt;br /&gt;
            Get the result of a SQL SELECT COUNT(...) query.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      &#039;&#039;&#039;Retrieving a single record&#039;&#039;&#039;&lt;br /&gt;
          o get_record($table, $field1, $value1, $field2=&#039;&#039;, $value2=&#039;&#039;, $field3=&#039;&#039;, $value3=&#039;&#039;, $fields=&#039;*&#039;) &lt;br /&gt;
            Get a single record as an object&lt;br /&gt;
          o get_record_select($table, $select=&#039;&#039;, $fields=&#039;*&#039;) &lt;br /&gt;
            Gets one record from a table, as an object&lt;br /&gt;
          o get_record_sql($sql, $expectmultiple=false, $nolimit=false) &lt;br /&gt;
            Get a single record as an object using an SQL statement&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      &#039;&#039;&#039;Getting an array of records&#039;&#039;&#039;&lt;br /&gt;
          o get_records($table, $field=&#039;&#039;, $value=&#039;&#039;, $sort=&#039;&#039;, $fields=&#039;*&#039;, $limitfrom=&#039;&#039;, $limitnum=&#039;&#039;) &lt;br /&gt;
            Get a number of records as an array of objects.&lt;br /&gt;
          o get_records_select($table, $select=&#039;&#039;, $sort=&#039;&#039;, $fields=&#039;*&#039;, $limitfrom=&#039;&#039;, $limitnum=&#039;&#039;) &lt;br /&gt;
            Get a number of records as an array of objects.&lt;br /&gt;
          o get_records_list($table, $field=&#039;&#039;, $values=&#039;&#039;, $sort=&#039;&#039;, $fields=&#039;*&#039;, $limitfrom=&#039;&#039;, $limitnum=&#039;&#039;) &lt;br /&gt;
            Get a number of records as an array of objects.&lt;br /&gt;
          o get_records_sql($sql, $limitfrom=&#039;&#039;, $limitnum=&#039;&#039;) &lt;br /&gt;
            Get a number of records as an array of objects.&lt;br /&gt;
          o get_records_menu($table, $field=&#039;&#039;, $value=&#039;&#039;, $sort=&#039;&#039;, $fields=&#039;*&#039;) &lt;br /&gt;
            Get the first two columns from a number of records as an associative array.&lt;br /&gt;
          o get_records_select_menu($table, $select=&#039;&#039;, $sort=&#039;&#039;, $fields=&#039;*&#039;) &lt;br /&gt;
            Get the first two columns from a number of records as an associative array.&lt;br /&gt;
          o get_records_sql_menu($sql) &lt;br /&gt;
            Get the first two columns from a number of records as an associative array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      &#039;&#039;&#039;Getting a particular field from the database&#039;&#039;&#039;&lt;br /&gt;
          o get_field($table, $return, $field1, $value1, $field2=&#039;&#039;, $value2=&#039;&#039;, $field3=&#039;&#039;, $value3=&#039;&#039;) &lt;br /&gt;
            Get a single value from a table row where all the given fields match the given values.&lt;br /&gt;
          o get_field_select($table, $return, $select) &lt;br /&gt;
            Get a single value from a table row where a particular select clause is true.&lt;br /&gt;
          o get_field_sql($sql) &lt;br /&gt;
            Get a single value from a table.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      &#039;&#039;&#039;Setting a particular field in the database&#039;&#039;&#039;&lt;br /&gt;
          o set_field($table, $newfield, $newvalue, $field1, $value1, $field2=&#039;&#039;, $value2=&#039;&#039;, $field3=&#039;&#039;, $value3=&#039;&#039;) &lt;br /&gt;
            Set a single field in every table row where all the given fields match the given values.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      &#039;&#039;&#039;Deleting Records&#039;&#039;&#039;&lt;br /&gt;
          o delete_records($table, $field1=&#039;&#039;, $value1=&#039;&#039;, $field2=&#039;&#039;, $value2=&#039;&#039;, $field3=&#039;&#039;, $value3=&#039;&#039;) &lt;br /&gt;
            Delete the records from a table where all the given fields match the given values.&lt;br /&gt;
          o delete_records_select($table, $select=&#039;&#039;) &lt;br /&gt;
            Delete one or more records from a table&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      &#039;&#039;&#039;Inserting Records&#039;&#039;&#039;&lt;br /&gt;
          o insert_record($table, $dataobject, $returnid=true, $primarykey=&#039;id&#039;) &lt;br /&gt;
            Insert a record into a table and return the &amp;quot;id&amp;quot; field if required&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      &#039;&#039;&#039;Updating Records&#039;&#039;&#039;&lt;br /&gt;
          o update_record($table, $dataobject) &lt;br /&gt;
            Update a record in a table&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[DDL functions]]: Where all the functions used to handle DB objects ([[wikipedia:Data_Definition_Language|DDL]]) are defined.&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:XMLDB]]&lt;br /&gt;
[[Category:Database]]&lt;/div&gt;</summary>
		<author><name>Audun</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/Unit_tests&amp;diff=16607</id>
		<title>Broken/Unit tests</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/Unit_tests&amp;diff=16607"/>
		<updated>2006-10-04T07:25:13Z</updated>

		<summary type="html">&lt;p&gt;Audun: /* Changes to your existing code to make it work with unit testing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The purpose of unit testing is to test the individual parts of the program (functions, and methods of classes) to make sure that each individually does the right thing. Once unit testing has given you the confidence that each part works, then you should use other forms of testing to ensure that the different parts work together properly.&lt;br /&gt;
&lt;br /&gt;
The unit testing framework is based on the [http://www.lastcraft.com/simple_test.php SimpleTest] framework. It was incorporated into Moodle by Nick Freear and Tim Hunt from [http://www.open.ac.uk/ The Open University].&lt;br /&gt;
&lt;br /&gt;
== Running the unit tests in Moodle ==&lt;br /&gt;
&lt;br /&gt;
=== Running the basic tests ===&lt;br /&gt;
&lt;br /&gt;
# Log in with an admin account. &lt;br /&gt;
# Go to the admin screen.&lt;br /&gt;
# Click on the &#039;&#039;&#039;Reports&#039;&#039;&#039; link near the bottom of the page.&lt;br /&gt;
# Click on the &#039;&#039;&#039;Run the unit tests&#039;&#039;&#039; link.&lt;br /&gt;
# Wait for the tests to run.&lt;br /&gt;
&lt;br /&gt;
=== Options for running the tests ===&lt;br /&gt;
&lt;br /&gt;
At the bottom of the tests page, there is form that lets you adjust the options used when running the tests.&lt;br /&gt;
&lt;br /&gt;
==== Show passes as well as fails ====&lt;br /&gt;
&lt;br /&gt;
Normally, only details of the tests that have failed are printed. Turning on this options shows details of all the passes too.&lt;br /&gt;
&lt;br /&gt;
==== Show the search for test files ====&lt;br /&gt;
&lt;br /&gt;
The tests to run are found automatically be searching the codebase for files whose names match &#039;&#039;&#039;test*.php&#039;&#039;&#039; in directories called &#039;&#039;&#039;simpletest&#039;&#039;&#039;. Turning on this option will print a list of the folders searched and the test files found. This is sometimes useful for debugging.&lt;br /&gt;
&lt;br /&gt;
This option is particularly useful when one of your test files has a syntax error. When this happens, you sometimes just get a blank page with no error message. Turning on the show search option lets you see which test file it was that gave the error. If necessary, you can enable this option manually by adding &amp;quot;showsearch=1&amp;quot; to the end of the URL.&lt;br /&gt;
&lt;br /&gt;
==== Run a thorough test (may be slow) ====&lt;br /&gt;
&lt;br /&gt;
If you turn on this option, then as well as looking for files called &#039;&#039;&#039;test*.php&#039;&#039;&#039;, the search also looks for files called &#039;&#039;&#039;slowtest*.php&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
To be useful, the full test run should find most bugs, but not take too long to complete. So if you have very, very detailed tests of an area of the code, it may be better to select a subset for everday testing, and only use the more detailed tests when a bug is reported, or you are doing new development in that area of the code.&lt;br /&gt;
&lt;br /&gt;
This option is most useful when combined with the next option.&lt;br /&gt;
&lt;br /&gt;
==== Only run tests in ====&lt;br /&gt;
&lt;br /&gt;
Normally, tests from all parts of the codebase are run. However, when you are just doing development of one part of the code, that is a waste of time. You can type the name of a folder (for example &#039;&#039;&#039;mod/quiz&#039;&#039;&#039;) or a particular test file (for example &#039;&#039;&#039;lib/simpletest/testdatalib.php&#039;&#039;&#039;) and then only those tests will be run.&lt;br /&gt;
&lt;br /&gt;
[[Image:RunOnlyTheseTests.png|right]] Instead of typing a path into this box, there is an easier way. Whenever a pass or fail is displayed, the name of the test file is printed. Each section of the path name is a link to run only the tests in that folder or file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Writing new tests ==&lt;br /&gt;
&lt;br /&gt;
As an example, suppose we wanted to start writing tests for the functions in the file &#039;question/editlib.php&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Where to put the tests ===&lt;br /&gt;
&lt;br /&gt;
If you have read the first half of this page and were paying attention, you can probably work out that you should create a folder called &#039;&#039;&#039;question/testeditlib&#039;&#039;&#039;, and create a file in there called something like &#039;&#039;&#039;testeditlib.php&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
The skeleton of this file should look like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Unit tests for (some of) question/editlib.php.&lt;br /&gt;
 *&lt;br /&gt;
 * @copyright &amp;amp;copy; 2006 The Open University&lt;br /&gt;
 * @author T.J.Hunt@open.ac.uk&lt;br /&gt;
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License&lt;br /&gt;
 * @package question&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
/** */&lt;br /&gt;
require_once(dirname(__FILE__) . &#039;/../../config.php&#039;);&lt;br /&gt;
&lt;br /&gt;
global $CFG;&lt;br /&gt;
require_once($CFG-&amp;gt;libdir . &#039;/simpletestlib.php&#039;); // Include the test libraries&lt;br /&gt;
require_once($CFG-&amp;gt;dirroot . &#039;/question/editlib.php&#039;); // Include the code to test&lt;br /&gt;
&lt;br /&gt;
/** This class contains the test cases for the functions in editlib.php. */&lt;br /&gt;
class question_editlib_test extends UnitTestCase {&lt;br /&gt;
    function test_get_default_question_category() {&lt;br /&gt;
        // Do the test here/&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That is, you have a class called something_test, and in that class you have lots of methods called test_something. Normally, you have one test method for each function you want to test, and you may as well called the test method &#039;&#039;&#039;test_name_of_function_being_tested&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Inside a test function ===&lt;br /&gt;
&lt;br /&gt;
The inside of a test function tyically looks like this: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;function test_get_default_question_category() {&lt;br /&gt;
    // Set things up in preparation for the test.&lt;br /&gt;
&lt;br /&gt;
    // Call the function you want to test.&lt;br /&gt;
&lt;br /&gt;
    // Check that the result is what you expected.&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
=== Test data ===&lt;br /&gt;
&lt;br /&gt;
TODO&lt;br /&gt;
&lt;br /&gt;
=== setUp and tearDown methods ===&lt;br /&gt;
&lt;br /&gt;
If all your test cases relate to the same area of code, and so need the same set of test data, then you can create a method called &amp;lt;code&amp;gt;setUp()&amp;lt;/code&amp;gt; that sets up the test data. If present, this method will be called before each test method. You can write a matching &amp;lt;code&amp;gt;tearDown()&amp;lt;/code&amp;gt; method if there is any clean-up that needs to be done after each test case has run.&lt;br /&gt;
&lt;br /&gt;
If you have some test test cases the need one sort of setup, and some other test cases that need a different setup, consider splitting your tests into two separate classes, each with its own &amp;lt;code&amp;gt;setUp()&amp;lt;/code&amp;gt; method.&lt;br /&gt;
&lt;br /&gt;
=== Further information ===&lt;br /&gt;
&lt;br /&gt;
The simpletest documentation is at: http://simpletest.sourceforge.net/.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Changes to your existing code to make it work with unit testing ==&lt;br /&gt;
&lt;br /&gt;
When code is being tested, it gets included from inside one of the simpletest library function. If the code is expecting to be run directly (for example, if it is a view.php or index.php function), you are likely to get errors because that expectation is no longer true.&lt;br /&gt;
&lt;br /&gt;
=== Include paths ===&lt;br /&gt;
&lt;br /&gt;
Includes like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;require_once(&#039;../../config.php&#039;); // Won&#039;t work.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
won&#039;t work. Instead, the more robust option is &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;require_once(dirname(__FILE__) . &#039;/../../config.php&#039;); // Do this.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Access to global variables ===&lt;br /&gt;
&lt;br /&gt;
Because your code was included from within a function, you can&#039;t access access global variables until you have done a global statement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;require_once(dirname(__FILE__) . &#039;/../../config.php&#039;);&lt;br /&gt;
require_once($CFG-&amp;gt;libdir . &#039;/moodlelib.php&#039;); // Won&#039;t work.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;require_once(dirname(__FILE__) . &#039;/../../config.php&#039;);&lt;br /&gt;
&lt;br /&gt;
global $CFG; // You need this.&lt;br /&gt;
require_once($CFG-&amp;gt;libdir . &#039;/moodlelib.php&#039;); // Will work now.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More about unit testing in general ==&lt;br /&gt;
&lt;br /&gt;
The best book I know about unit testing is &#039;&#039;&#039;Pragmatic Unit Testing in Java with JUnit&#039;&#039; by Andrew Hunt (no relation) and David Thomas. I know, this book is not called Pragmatic Unit Testing in PHP with SimpleTest. However, it is an excellent book - short, to the point, and very practical. Most of what it says is not specific to Java and JUnit and it is obvious how to apply it in our testing setup.&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Audun</name></author>
	</entry>
</feed>