<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.moodle.org/dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Markstevens</id>
	<title>MoodleDocs - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.moodle.org/dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Markstevens"/>
	<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/Special:Contributions/Markstevens"/>
	<updated>2026-04-21T07:26:53Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Editor_file_management&amp;diff=15148</id>
		<title>Editor file management</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Editor_file_management&amp;diff=15148"/>
		<updated>2009-08-04T03:09:52Z</updated>

		<summary type="html">&lt;p&gt;Markstevens: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This document is to start a usability discussion about the best way to implement a particular new feature in Moodle 2.0. &lt;br /&gt;
&lt;br /&gt;
HTML texts may have associated embedded media, such as images, videos and sounds.   These are added to the HTML file using the HTML editor (which in Moodle 2.0 is a modified version of TinyMCE) and stored in the &amp;quot;file area&amp;quot; associated with the text.  This applies to all texts in Moodle 2.0, ranging from simple forum posts to full-blown mini-web sites in the Resource module.&lt;br /&gt;
&lt;br /&gt;
There may be more than one file, and files may change or require deletion, so they need to be managed.&lt;br /&gt;
&lt;br /&gt;
The question we need to solve is: &amp;quot;What is the best interface to manage and browse these files?&amp;quot; &lt;br /&gt;
&lt;br /&gt;
== Conventions ==&lt;br /&gt;
&lt;br /&gt;
[[Image:wordpress2.8-upload-a.png|thumb|Wordpress 2.8]][[Image:wordpress2.8-upload-b.png|thumb|Wordpress 2.8]]&lt;br /&gt;
Example 1: Office document (Word, OpenOffice, Google Docs).  Once the &amp;quot;Open&amp;quot; dialog is used to bring the media into the page, the only way it can be removed from the document is by deleting it from the document directly.  Office applications do not have a way of managing media separately from the document view.&lt;br /&gt;
&lt;br /&gt;
Example 2: Dreamweaver. &lt;br /&gt;
&lt;br /&gt;
Example 3: Box net.  Documents can be made and images added, but there is no interface in the document to manage media.  However, when looking at the filesystem view one can see the media in a folder and can edit it.   Obviously changing names and deleting things will break the links in the HTML.&lt;br /&gt;
&lt;br /&gt;
Example 4: Wordpress: Separate tabs for Uploading from PC, uploading from URL, and selecting an image from Media Library (files already on server). When image is uploaded in first two, there is a list of the files already uploaded. In any of the three tabs, when an image is selected, options for its display are shown, along with an option to delete. See: http://demo.opensourcecms.com/wordpress/wp-admin/page-new.php (user:admin, password:demo123 or see http://php.opensourcecms.com/scripts/details.php?scriptid=88&amp;amp;name=WordPress)&lt;br /&gt;
&lt;br /&gt;
(TODO: more examples)&lt;br /&gt;
&lt;br /&gt;
== Related Moodle 2 features == &lt;br /&gt;
&lt;br /&gt;
===File picker ===&lt;br /&gt;
&lt;br /&gt;
The file picker is designed to mimic the file open dialog.  &lt;br /&gt;
&lt;br /&gt;
As usual, the button/link/menu that initiates the file picker can specify what mime types are being looked for, and the resulting display is limited to those repositories that support those mime types, and only files matching the mime types are shown. &lt;br /&gt;
&lt;br /&gt;
===File manager form element ===&lt;br /&gt;
&lt;br /&gt;
The file manager form element allows any form to have a managed set of files, such as a set of attachments to a forum post.  Files can be added (by calling the file picker) or deleted (by pressing a little X next to any file).&lt;br /&gt;
&lt;br /&gt;
 [[Image:Filemanagerform.png]]&lt;br /&gt;
&lt;br /&gt;
==Mockups==&lt;br /&gt;
&lt;br /&gt;
Click on this diagram to see the full-sized version:&lt;br /&gt;
&lt;br /&gt;
[[Image:Editor_file_management.png]]&lt;br /&gt;
&lt;br /&gt;
=== Mockup A ===&lt;br /&gt;
&lt;br /&gt;
File manager in the Insert/Edit dialog.  &lt;br /&gt;
&lt;br /&gt;
Pros:&lt;br /&gt;
* Management is kept separate from the file picker &lt;br /&gt;
* Interface is similar to file manager forms element &lt;br /&gt;
* No extra icon needed in editor toolbar&lt;br /&gt;
&lt;br /&gt;
Cons:&lt;br /&gt;
* Insert/Edit image dialog needs to be modified, and maintained &lt;br /&gt;
* harder to deal with many files &lt;br /&gt;
* Interface clutter for people who won&#039;t use this feature&lt;br /&gt;
* Files can be deleted here resulting in missing media from the HTML.&lt;br /&gt;
&lt;br /&gt;
=== Mockup B ===&lt;br /&gt;
&lt;br /&gt;
File manager is implemented as a separate repository inside the filepicker eg &amp;quot;Current files&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Pros:&lt;br /&gt;
* Very easy to implement across Moodle, no changes to editors are needed.&lt;br /&gt;
* Makes sense when re-choosing images already chosen (eg. to add another copy of the same image).&lt;br /&gt;
&lt;br /&gt;
Cons:&lt;br /&gt;
* Breaks the paradigm of the &amp;quot;file picker&amp;quot; because files can be deleted, zips uploaded etc. &lt;br /&gt;
* &amp;quot;Current files&amp;quot; difficult to separate logically from the &amp;quot;Local files&amp;quot;. &lt;br /&gt;
* Files can be deleted here resulting in missing media from the HTML.&lt;br /&gt;
* Constantly clutters repository list with an item that many will not use.&lt;br /&gt;
* Inconsistent with the filemanager form element (used for attachments etc)&lt;br /&gt;
* Either we show all files (ignore mimetype) or only show files matching mimetype (incomplete management)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mockup C ===&lt;br /&gt;
&lt;br /&gt;
File manager is implemented separately from file picking, as a new tinymce plugin. &lt;br /&gt;
&lt;br /&gt;
Pros:&lt;br /&gt;
* Functionality is separate from the file picker so file picker paradigm preserved &lt;br /&gt;
* People who don&#039;t need it don&#039;t have to see it (unclutters interface)&lt;br /&gt;
* Interface is very similar to the file manager form element &lt;br /&gt;
&lt;br /&gt;
Cons:&lt;br /&gt;
* Requires a new tinymce plugin that needs maintenance &lt;br /&gt;
* Requires a whole new icon in the editor toolbar.&lt;br /&gt;
* Files can be deleted here resulting in missing media from the HTML.&lt;br /&gt;
&lt;br /&gt;
==Some use cases in more detail==&lt;br /&gt;
&lt;br /&gt;
(In progress)&lt;br /&gt;
&lt;br /&gt;
===Add a new image to a post somewhere in Moodle===&lt;br /&gt;
&lt;br /&gt;
* Click on the image button &lt;br /&gt;
* Click on the search button &lt;br /&gt;
* Select a file from a repository (or upload) &lt;br /&gt;
* Add metadata and preferences. &lt;br /&gt;
* Insert the image &lt;br /&gt;
&lt;br /&gt;
===Add an image AGAIN to a post somewhere in Moodle=== &lt;br /&gt;
* Click on the image button &lt;br /&gt;
* Click on the search button &lt;br /&gt;
* Select the image from the &amp;quot;Recent files&amp;quot; repository &lt;br /&gt;
* Add metadata and preferences again&lt;br /&gt;
* Insert the image &lt;br /&gt;
&lt;br /&gt;
===Add an attachment to anything===&lt;br /&gt;
* Click on &amp;quot;Add&amp;quot; in the file manager form element&lt;br /&gt;
* Select a file from a repository (or upload)&lt;br /&gt;
* File is added &lt;br /&gt;
&lt;br /&gt;
==Discussion==&lt;br /&gt;
&lt;br /&gt;
So, what is best?&lt;br /&gt;
&lt;br /&gt;
Discussion is here:  [http://moodle.org/mod/forum/discuss.php?d=127238 RFC: Editor file management in Moodle 2.0]&lt;/div&gt;</summary>
		<author><name>Markstevens</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Unit_test_API&amp;diff=3976</id>
		<title>Unit test API</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Unit_test_API&amp;diff=3976"/>
		<updated>2009-07-29T08:46:15Z</updated>

		<summary type="html">&lt;p&gt;Markstevens: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.7}}Location: &#039;&#039;Administration &amp;gt; Reports &amp;gt; Unit tests&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The purpose of Unit Tests is to evaluate the individual parts of a program (functions, and methods of classes) to make sure that each element individually does the right thing. Unit Tests can be one of the first steps in a quality control process for developing or tweaking Moodle code.  The next steps will involve other forms of testing to ensure that these different parts work together properly. &lt;br /&gt;
&lt;br /&gt;
The unit testing framework is based on the [http://www.simpletest.org/ SimpleTest] framework.&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;
# Administration ► Development ► Unit tests (moodle &amp;gt;= 2.0, Administration ► Reports ► Unit tests Moodle &amp;lt;= 1.9)&lt;br /&gt;
# Click on the &#039;&#039;&#039;Reports&#039;&#039;&#039; link near the bottom of the page.&lt;br /&gt;
# Click the &#039;&#039;&#039;Run tests&#039;&#039;&#039; button and wait.&lt;br /&gt;
&lt;br /&gt;
This finds all the tests in Moodle and runs them. You can run a subset of the tests by entering a path (for example question/type) in the &#039;Only run tests in&#039; box. Similarly, if a test fails, you get some links in the failure message to make it easy to re-run just those tests.&lt;br /&gt;
&lt;br /&gt;
== Writing new tests ==&lt;br /&gt;
&lt;br /&gt;
As an example, suppose we wanted to write some tests for the string_manager class in mod/quiz/editlib.php.&lt;br /&gt;
&lt;br /&gt;
=== Where to put the tests ===&lt;br /&gt;
&lt;br /&gt;
The unit test report finds tests by looking for files called &#039;test....php&#039; inside folders called &#039;simpletest&#039;.&lt;br /&gt;
&lt;br /&gt;
So, for our example, we want to create called something like &#039;&#039;&#039;mod/quiz/simpletest/testeditlib.php&#039;&#039;&#039;. The skeleton of this file should look like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Unit tests for (some of) mod/quiz/editlib.php.&lt;br /&gt;
 *&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;
if (!defined(&#039;MOODLE_INTERNAL&#039;)) {&lt;br /&gt;
    die(&#039;Direct access to this script is forbidden.&#039;); //  It must be included from a Moodle page&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Make sure the code being tested is accessible.&lt;br /&gt;
require_once($CFG-&amp;gt;dirroot . &#039;/mod/quiz/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 quiz_editlib_test extends UnitTestCase {&lt;br /&gt;
    function test_something() {&lt;br /&gt;
        // Do the test here.&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // ... more test methods.&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/code&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 particular thing you want to test, and you should try to name the function to describe what is being tested - without making the name too ridiculously long!&lt;br /&gt;
&lt;br /&gt;
=== A test function ===&lt;br /&gt;
&lt;br /&gt;
The a test function typically looks like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function test_move_question_up() {&lt;br /&gt;
    // Setup fixture&lt;br /&gt;
&lt;br /&gt;
    // Exercise SUT&lt;br /&gt;
    $newlayout = quiz_move_question_up(&#039;1,2,0&#039;, 2);&lt;br /&gt;
&lt;br /&gt;
    // Validate outcome&lt;br /&gt;
    $this-&amp;gt;assertEqual($newlayout, &#039;2,1,0&#039;);&lt;br /&gt;
&lt;br /&gt;
    // Teardown fixture&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the [http://xunitpatterns.com/Four%20Phase%20Test.html four phase test pattern]. Those comments use a lot of testing jargon. The fixture is the background situation that needs to be set up before the test runs. SUT is short for &#039;situation under test&#039;. This is where you call the function or method that you want to test. Then you check to see if the function did the right thing. Finally, you have to clean up the fixture you created. With luck there is nothing to do here&lt;br /&gt;
&lt;br /&gt;
In this simple example, there is no setup or teardown to do. We just call the function we are testing with some sample input, and check that the return value is what we expect.&lt;br /&gt;
&lt;br /&gt;
=== Shared setUp and tearDown methods ===&lt;br /&gt;
&lt;br /&gt;
If all your test cases relate to the same area of code, then they may all need to same bit of fixture set up. For example, all the tests in lib/simpletest/teststringmanager.php need an instance of the string_manager class to test.&lt;br /&gt;
&lt;br /&gt;
To avoid duplicating code, you can override 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. For example, in lib/simpletest/teststringmanager.php there are setUp and tearDown methods that do something like:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public function setUp() {&lt;br /&gt;
    // ...&lt;br /&gt;
    $this-&amp;gt;stringmanager = new string_manager(...);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function tearDown() {&lt;br /&gt;
    $this-&amp;gt;stringmanager = null;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Then, each test can use $this-&amp;gt;stringmanager without having to worry about the details of how it is set up.&lt;br /&gt;
&lt;br /&gt;
=== Further information ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;SimpleTest&#039;&#039; documentation is at: http://www.simpletest.org/.&lt;br /&gt;
&lt;br /&gt;
== Changes to your existing code to make it work with unit testing ==&lt;br /&gt;
&lt;br /&gt;
The whole point of unit testing is to test each piece of functionality separately. You can only do this is only possible to isolate that function and call it individually, perhaps after setting up a few other things.&lt;br /&gt;
&lt;br /&gt;
Therefore, it is good if you can write your code to depend on as few other things as possible. &lt;br /&gt;
&lt;br /&gt;
=== Include paths ===&lt;br /&gt;
&lt;br /&gt;
Includes like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
require_once(&#039;../../config.php&#039;); // Won&#039;t work.&lt;br /&gt;
&amp;lt;/code&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;code php&amp;gt;&lt;br /&gt;
require_once(dirname(__FILE__) . &#039;/../../config.php&#039;); // Do this.&lt;br /&gt;
&amp;lt;/code&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 global variables until you have done a global statement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&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.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&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.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Calls to global functions ===&lt;br /&gt;
Testing a class method that calls global functions can be problematic. At least, it&#039;s always complex, because we can&#039;t control what goes on in the global functions. We can&#039;t override the global functions or mock them in our unit tests. If the global functions themselves are well tested, this may not be a big problem, but most global functions are not well tested.&lt;br /&gt;
&lt;br /&gt;
==== Bridge Pattern ====&lt;br /&gt;
If your code needs to rely extensively on some public API, you could use the [http://en.wikipedia.org/wiki/Bridge_pattern bridge pattern] to decouple your code from that API. This way, when you write unit tests, you can override the bridging class or mock it, and control its outputs while you focus exclusively on testing your code.&lt;br /&gt;
&lt;br /&gt;
An basic example follows: Imagine that I do not trust the &#039;&#039;get_string()&#039;&#039; global function, but my code needs to use it. Initially my code has strong coupling with &#039;&#039;get_string()&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class myclass {&lt;br /&gt;
    public function print_stuff($stuff) {&lt;br /&gt;
        echo get_string($stuff);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now let&#039;s write a bridging class to solve this coupling issue and use it instead of &#039;&#039;get_string()&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class languageBridge {&lt;br /&gt;
    public function get_string($stuff,$module=&#039;moodle&#039;) {&lt;br /&gt;
        echo get_string($stuff, $module);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class myclass {&lt;br /&gt;
    public $lang_bridge;&lt;br /&gt;
    public function __construct() {&lt;br /&gt;
        $this-&amp;gt;lang_bridge = new languageBridge();&lt;br /&gt;
    }&lt;br /&gt;
    public function print_stuff($stuff) {&lt;br /&gt;
        echo $this-&amp;gt;lang_bridge-&amp;gt;get_string($stuff);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is yet another example using a bridging method to decouple from the Moodle core API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
class workshop_api {&lt;br /&gt;
    /**&lt;br /&gt;
     * This is a method we want to unittest&lt;br /&gt;
     */&lt;br /&gt;
    public function get_peer_reviewers($context) {&lt;br /&gt;
        static $users=null;&lt;br /&gt;
        if (is_null($users)) {&lt;br /&gt;
            $users = $this-&amp;gt;get_users_by_capability($context, &#039;mod/workshop:peerassess&#039;, &lt;br /&gt;
                        &#039;u.id, u.lastname, u.firstname&#039;, &#039;u.lastname,u.firstname&#039;, &#039;&#039;, &#039;&#039;, &#039;&#039;, &#039;&#039;, false, false, true);&lt;br /&gt;
        }&lt;br /&gt;
        return $users;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**  &lt;br /&gt;
     * Bridging method to decouple from Moodle core API&lt;br /&gt;
     */&lt;br /&gt;
    protected function get_users_by_capability() {&lt;br /&gt;
        $args = func_get_args();&lt;br /&gt;
        return call_user_func_array(&#039;get_users_by_capability&#039;, $args);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Warning:&#039;&#039;&#039; Here are some comments on the examples above expressing that the bridge pattern should be used very carefully.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;I think that is a case of unit tests leading to worse software design, in that you are not using the standard API for something. But if you really want to unit test, I can&#039;t think of a better solution.&amp;quot;&lt;br /&gt;
* &amp;quot;I think this can be OK if used very selectively. Unfortunately I don&#039;t think it&#039;s the solution if you want to decouple the very complex and deeply nested Moodle functions from each other&amp;quot;&lt;br /&gt;
* &amp;quot;...your code is designed to be part of Moodle, so decoupling from a standard Moodle API is perverse.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Unit testing in 2.0 ==&lt;br /&gt;
&lt;br /&gt;
{{Moodle 2.0}}&lt;br /&gt;
With the Objectification of the Database libraries in Moodle 2.0, new and better approaches to Unit testing can be used. Here is a sample of a simple test case: (in course/simpletest)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 require_once($CFG-&amp;gt;dirroot . &#039;/course/lib.php&#039;);&lt;br /&gt;
 &lt;br /&gt;
 global $DB;&lt;br /&gt;
 Mock::generate(get_class($DB), &#039;mockDB&#039;);&lt;br /&gt;
 &lt;br /&gt;
 class courselib_test extends UnitTestCase {&lt;br /&gt;
     var $realDB;&lt;br /&gt;
 &lt;br /&gt;
     function setUp() {&lt;br /&gt;
         global $DB;&lt;br /&gt;
         $this-&amp;gt;realDB = $DB;&lt;br /&gt;
         $DB           = new mockDB();&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     function tearDown() {&lt;br /&gt;
         global $DB;&lt;br /&gt;
         $DB = $this-&amp;gt;realDB;&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     function testMoveSection() {&lt;br /&gt;
         global $DB;&lt;br /&gt;
         $course = new stdClass();&lt;br /&gt;
         $course-&amp;gt;id = 1;&lt;br /&gt;
 &lt;br /&gt;
         $sections = array();&lt;br /&gt;
         for ($i = 1; $i &amp;lt; 11; $i++) {&lt;br /&gt;
             $sections[$i]          = new stdClass();&lt;br /&gt;
             $sections[$i]-&amp;gt;id      = $i;&lt;br /&gt;
             $sections[$i]-&amp;gt;section = $i - 1;&lt;br /&gt;
         }&lt;br /&gt;
 &lt;br /&gt;
         $DB-&amp;gt;expectOnce(&#039;get_records&#039;, array(&#039;course_sections&#039;, array(&#039;course&#039; =&amp;gt; $course-&amp;gt;id)));&lt;br /&gt;
         $DB-&amp;gt;setReturnValue(&#039;get_records&#039;, $sections);&lt;br /&gt;
         $this-&amp;gt;assertFalse(move_section($course, 2, 3));&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See also &#039;&#039;&#039;UnitTestCaseUsingDatabase&#039;&#039;&#039; in lib/simpletestlib.php.&lt;br /&gt;
&lt;br /&gt;
=== Testing HTML output ===&lt;br /&gt;
&lt;br /&gt;
(work in progress - to be documented properly once the API stabilizes)&lt;br /&gt;
&lt;br /&gt;
* ContainsTagWithAttribute($tag, $attribute, $value)&lt;br /&gt;
* ContainsTagWithAttributes($tag, $attributes)&lt;br /&gt;
* ContainsTagWithContents($tag, $content)&lt;br /&gt;
* ContainsEmptyTag($tag)&lt;br /&gt;
&lt;br /&gt;
The syntax is &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$this-&amp;gt;assert(new ContainsTagWithAttribute($tag, $attribute, $value), $html);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Code coverage analysis==&lt;br /&gt;
&amp;lt;p class=&amp;quot;note&amp;quot;&amp;gt;&#039;&#039;&#039;Note:&#039;&#039;&#039; This section is a work in progress. Please use the [[{{TALKPAGENAME}}|page comments]] or an appropriate [http://moodle.org/course/view.php?id=5 moodle.org forum] for any recommendations/suggestions for improvement.{{{info|}}}&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Moodle 2.0}}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;[[Wikipedia:Code_coverage|Code coverage]]&#039;&#039;&#039; is a technique, strongly tied with software testing, that allows to &#039;&#039;&#039;check and improve the quality of the tests&#039;&#039;&#039; by measuring the degree of source code that is being covered by them. With Moodle supporting more and more tests each day (slowly towards a &#039;&#039;&#039;Test Driven Development&#039;&#039;&#039; model) we need to integrate some tool into our development process helping to analyse the quality of ours tests.&lt;br /&gt;
&lt;br /&gt;
Right now (in Moodle 2.0) we are using [http://www.simpletest.org/ SimpleTest], one simple and great tool to perform all the tests. Unluckily, it doesn&#039;t support code coverage analysis at all. In the other hand, other PHP unit testing products like [http://www.phpunit.de/ PHPUnit], more complex and powerful, have built-in support for that technique, but switching to a new product is out from our current [[Roadmap]] plans.&lt;br /&gt;
&lt;br /&gt;
So, after some readings and comparisons, Moodle will implement its own extensions to SimpleTest, in order to fulfil the main goal of having statement/line code coverage analysis working under Moodle 2.0 onwards. To achieve this, also [http://developer.spikesource.com/projects/phpcoverage Spike PHPCoverage], a basic code-coverage tool, will be used and extended. You can find the details of the implementation of this tool at MDL-19579.&lt;br /&gt;
&lt;br /&gt;
=== Changes ===&lt;br /&gt;
&lt;br /&gt;
To enable code coverage in your tests, only a few modifications need to be performed to your current code:&lt;br /&gt;
&lt;br /&gt;
# Change your test classes by adding two (public static) attributes: &#039;&#039;&#039;$includecoverage&#039;&#039;&#039; and &#039;&#039;&#039;$excludecoverage&#039;&#039;&#039;, both arrays, being used to inform to the code coverage tool (via reflection) about which source code files and dirs must be covered/skipped by the analysis.&lt;br /&gt;
# Use &#039;&#039;&#039;[http://cvs.moodle.org/moodle/lib/simpletestcoveragelib.php?view=markup simpletestcoveragelib.php]&#039;&#039;&#039; instead of simpletestlib.php in your caller scripts.&lt;br /&gt;
# Use the &#039;&#039;&#039;autogroup_test_coverage&#039;&#039;&#039; class instead of the AutoGroupTest one (see below for details) in your caller scripts.&lt;br /&gt;
&lt;br /&gt;
(note that only the 1st point above is needed for new unit tests being created because both 2 and 3 (changes in caller scripts) are already implemented in Moodle and awaiting your cool unit tests.&lt;br /&gt;
&lt;br /&gt;
That&#039;s all! With those 3 basic changes, you will end with a complete code coverage report available for further analysis.&lt;br /&gt;
&lt;br /&gt;
=== API ===&lt;br /&gt;
&lt;br /&gt;
When using code coverage within Moodle there are two alternative APIs available, both providing the same code coverage reports at the end, but doing that in a different way. Here they are:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Internal (hidden) coverage API&#039;&#039;&#039;: This API is completely hidden beyond the Unit testing API and you won&#039;t need to know the details about it. Just perform the 1-2-3 changes described above and, after running the tests you&#039;ll get the final report available for being used immediately, without needing to perform anything in your code. His major drawback: it only can perform &#039;&#039;&#039;one&#039;&#039;&#039; &amp;quot;code coverage session&amp;quot; (a.k.a. instrumentation), so it&#039;s only suitable for testing scripts using only one unit test execution. One example of this type of unit testing is  [http://cvs.moodle.org/moodle/admin/report/unittest/index.php?view=markup admin/report/unittest/index.php] where only one (big) test-group is executed.&lt;br /&gt;
* &#039;&#039;&#039;External (explicit) coverage API&#039;&#039;&#039;: This API needs extra coding as long as coverage instantiating, configuration and report generation happens in the main script. It&#039;s a bit more complex but, in the other hand, it support &#039;&#039;&#039;multiple&#039;&#039;&#039; instrumentations to be performed, and gives you more control about the code coverage process.  One example of this type of unit testing is  [http://cvs.moodle.org/moodle/admin/report/unittest/dbtest.php?view=markup admin/report/unittest/dbtest.php] where multiple (one for each DB being tested) test-group are executed.&lt;br /&gt;
&lt;br /&gt;
So, first of all (point 1 in prev section - usage), we need to define, for each unit test, which files / directories (relative to dirroot) we want to analyse with the tool. Here it&#039;s one example, for the &#039;&#039;&#039;dml_test&#039;&#039;&#039; unit test ([http://cvs.moodle.org/moodle/lib/dml/simpletest/testdml.php?view=markup /lib/dml/simpletest/testdml.php]), all we need to add to these lines to the class declaration :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
public  static $includecoverage = array(&#039;lib/dml&#039;);&lt;br /&gt;
public  static $excludecoverage = array(&#039;lib/dml/somedir&#039;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By doing so, the code coverage tool will know which are the target files to perform coverage analysis/reporting and will do that for all the files (recursively) in the &#039;&#039;lib/dml&#039;&#039;&#039; dir but excluding the &#039;&#039;lib/dml/somedir&#039;&#039; directory (recursively too). Note that both attributes are arrays so multiple paths can be specified in any of them. Also note that the dir where the UnitTest is stored is automatically excluded (usually &#039;&#039;simpletest&#039;&#039; dirs).&lt;br /&gt;
&lt;br /&gt;
And, as said, that&#039;s all you need to fulfil in order to get current/new unit tests being analysed by the code coverage tool by current Moodle scripts. The documentation below is only interesting for developers wanting to create new scripts able to perform unit testing with code coverage (points 2 and 3 in prev section - usage).&lt;br /&gt;
&lt;br /&gt;
==== Internal coverage API ====&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$test = new autogroup_test_coverage($showsearch, $test_name, $performcoverage, $coveragename, $coveragedir);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Create one new autogroup test object with code coverage support, there you can specify if you want to perform coverage (true/false), the name of the report (title) and the directory where the final report will be created (under moodledata/codecoverage). &lt;br /&gt;
Optionally you can add more files and directories (relative to dirroot) to the list of files to be covered / ignored by using these functions (in case the defined in point 1 aren&#039;t enough).&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$test-&amp;gt;add_coverage_include_path($path);&lt;br /&gt;
$test-&amp;gt;add_coverage_exclude_path($path);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
And then, affter adding a bunch of unit tests to the group, you simply invoke the test execution with code coverage support to end with a nice code coverage report under &#039;&#039;dataroot/codecoverage/$coveragedir&#039;&#039;:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$test-&amp;gt;run($unit_test_reporter);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
(don&#039;t forget that this API supports only &#039;&#039;&#039;one&#039;&#039;&#039; instrumentation to be performed)&lt;br /&gt;
&lt;br /&gt;
And that&#039;s all!&lt;br /&gt;
&lt;br /&gt;
==== External coverage API ====&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$covreporter = new moodle_coverage_reporter($coveragename, $coveragedir);&lt;br /&gt;
$covrecorder = new moodle_coverage_recorder($covreporter);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Create one coverage reporter, by passing its title and output directory as parameters.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$test = new autogroup_test_coverage($showsearch, $test_name, $performcoverage);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Create one new autogroup test object with code coverage support, you don&#039;t need to specify the title and dir here (as already have been defined by the moodle_coverage_reporter object).&lt;br /&gt;
Optionally you can add more files and directories (relative to dirroot) to the list of files to be covered / ignored by using these functions (in case the defined in point 1 aren&#039;t enough).&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$test-&amp;gt;add_coverage_include_path($path);&lt;br /&gt;
$test-&amp;gt;add_coverage_exclude_path($path);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Then, after adding a bunch of unit tests to the group, you simply invoke the test execution with code coverage support with:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$test-&amp;gt;run_with_external_coverage($unit_test_reporter, $covrecorder);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
(don&#039;t forget that this API supports &#039;&#039;&#039;multiple&#039;&#039;&#039; instrumentations to be performed)&lt;br /&gt;
&lt;br /&gt;
And finally, you generate the code coverage report (under &#039;&#039;dataroot/codecoverage/$coveragedir&#039;&#039;) using:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$covrecorder-&amp;gt;generate_report();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Once more, that&#039;s all!&lt;br /&gt;
&lt;br /&gt;
==== Final notes ====&lt;br /&gt;
* Note that there are some more methods available in the moodle_coverage_recorder class. They will allow to control starting/stopping instrumentations by hand and other minor things but they shouldn&#039;t really be used. The run() and run_with_external_coverage() methods should be enough in 99% of cases.&lt;br /&gt;
* Not being part of the API, but used by it, there is one &#039;&#039;&#039;[http://cvs.moodle.org/moodle/admin/report/unittest/coveragefile.php?view=markup overagefile.php]&#039;&#039;&#039; script under &#039;&#039;admin/report/unittest&#039;&#039; responsible for serving the coverage report files from within Moodle. See current scripts in that dir to see how can be used.&lt;br /&gt;
* All the test execution / reporting / coverage utilities must be protected with the &#039;moodle/site:config&#039; permission.&lt;br /&gt;
&lt;br /&gt;
== A warning ==&lt;br /&gt;
&lt;br /&gt;
In &#039;xUnit Test Patterns&#039; there is a [http://xunitpatterns.com/TestAutomationRoadmap.html scale of testing difficulty] that goes from 1. to 6. Moodle is definitely at number 6. on that scale &#039;Non-object-oriented legacy software&#039;. It then goes recommend that you don&#039;t start to learn about unit testing with that sort of software :-(&lt;br /&gt;
&lt;br /&gt;
== Further reading about unit testing ==&lt;br /&gt;
&lt;br /&gt;
[http://manning.com/reiersol/ PHP in Action] has an excellent chapter explaining unit testing in PHP with &#039;&#039;simpletest&#039;&#039;. (Although the rest of that book advocates a style of programming that is very different from the style used in Moodle.)&lt;br /&gt;
&lt;br /&gt;
[http://www.pragprog.com/titles/utj/pragmatic-unit-testing-in-java-with-junit Pragmatic Unit Testing in Java with JUnit] is also a very good introduction, despite being in the wrong programming language. JUnit and Simpletest are very similar.&lt;br /&gt;
&lt;br /&gt;
[http://xunitpatterns.com/ xUnit Test Patterns] is the ultimate unit test book. I think it teaches you everything you could learn about unit testing by reading a book. The only way to learn more would be years of experience. It has really great advice for dealing with the kind of messy problems you get in a big, real project like Moodle. &lt;br /&gt;
&lt;br /&gt;
{{CategoryDeveloper}}&lt;br /&gt;
[[Category:Quality Assurance]]&lt;/div&gt;</summary>
		<author><name>Markstevens</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=User:Mark_Stevens&amp;diff=21883</id>
		<title>User:Mark Stevens</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=User:Mark_Stevens&amp;diff=21883"/>
		<updated>2009-07-24T03:29:19Z</updated>

		<summary type="html">&lt;p&gt;Markstevens: New page: Moodle advocate since 2004.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Moodle advocate since 2004.&lt;/div&gt;</summary>
		<author><name>Markstevens</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Talk:Site_registration&amp;diff=27760</id>
		<title>Talk:Site registration</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Talk:Site_registration&amp;diff=27760"/>
		<updated>2009-02-18T09:07:32Z</updated>

		<summary type="html">&lt;p&gt;Markstevens: New section: Editing registered sites&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Michael, thanks for writing up the guidelines for reviewing new Moodle sites :-) I just came across [[Verification of sites on moodle.org]] and wondered whether the information could be added to [[Site registration]] and then the page deleted. What do you think? --[[User:Helen Foster|Helen Foster]] 06:48, 25 September 2008 (CDT)&lt;br /&gt;
&lt;br /&gt;
Helen, I&#039;ve updated/merged the instructions that appear on the &amp;quot;check new registrations page&amp;quot; so [[Verification of sites on moodle.org]] can be deleted.&lt;br /&gt;
&lt;br /&gt;
== Editing registered sites ==&lt;br /&gt;
&lt;br /&gt;
Hi All,&lt;br /&gt;
&lt;br /&gt;
As we all know, the registered sites lists contain many duplicate and/or no longer existent sites.  It&#039;s been suggested that the lists be divided regionally/alphabetically for maintenance.  For example, I used to check middle east sites, but the list has grown so much I can barely keep up with one country now.  Countries with thousands of sites could be divided alphabetically.&lt;br /&gt;
&lt;br /&gt;
Any takers?&lt;/div&gt;</summary>
		<author><name>Markstevens</name></author>
	</entry>
</feed>