<?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=F4bo</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=F4bo"/>
	<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/Special:Contributions/F4bo"/>
	<updated>2026-07-28T22:48:55Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=PHPUnit&amp;diff=54320</id>
		<title>PHPUnit</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=PHPUnit&amp;diff=54320"/>
		<updated>2018-06-01T16:50:47Z</updated>

		<summary type="html">&lt;p&gt;F4bo: /* Running a single test quickly */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 2.3}}&lt;br /&gt;
&lt;br /&gt;
=What is PHPUnit=&lt;br /&gt;
PHPUnit by Sebastian Bergmann is an advanced unit testing framework for PHP. It is installed as Composer dependency and is not part of Moodle installation. To run PHPUnit tests, you have to manually install it on your development computer or test server.&lt;br /&gt;
&lt;br /&gt;
Read the excellent guide at&lt;br /&gt;
* [http://phpunit.de/manual/current/en/index.html PHPUnit Manual]&lt;br /&gt;
&lt;br /&gt;
=Installation of PHPUnit via Composer=&lt;br /&gt;
&lt;br /&gt;
* Install Composer&lt;br /&gt;
&lt;br /&gt;
Instructions for installing composer on all platforms are here: https://getcomposer.org/download/&lt;br /&gt;
&lt;br /&gt;
Install the composer.phar file to your moodle folder.&lt;br /&gt;
&lt;br /&gt;
* Execute Composer installer&lt;br /&gt;
&lt;br /&gt;
 cd /your/moodle/dirroot&lt;br /&gt;
&lt;br /&gt;
 php composer.phar install&lt;br /&gt;
&lt;br /&gt;
(If that gives you connection problems try...)&lt;br /&gt;
&lt;br /&gt;
 php composer.phar install --prefer-source&lt;br /&gt;
&lt;br /&gt;
Troubleshooting:&lt;br /&gt;
* On Windows if you are behind a proxy you will need to setup an environment variable called HTTP_PROXY with a value detailing your HTTP Proxy address and port before composer will correctly download files.&lt;br /&gt;
* You may be prompted for github credentials when installing composer dependencies.&lt;br /&gt;
** This is used to generate an personal access token to avoid being rate limited by github.&lt;br /&gt;
** If you have Two Factor Authentication enabled on your github account, or do not wish to supply your own credentials you will need to generate a token manually:&lt;br /&gt;
*** Visit https://github.com/settings/applications and request personal access token&lt;br /&gt;
*** Copy this token and add it to your [https://gist.github.com/andrewnicols/c5377ed25a9df1006ce1 ~/.composer/config.json]&lt;br /&gt;
** ( See [https://github.com/composer/composer/issues/2280 composer issue #2280] for further details of this bug.)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Detailed instructions:&lt;br /&gt;
* [http://getcomposer.org/doc/00-intro.md Composer documentation]&lt;br /&gt;
&lt;br /&gt;
Detailed instructions:&lt;br /&gt;
* [[PHPUnit installation in Windows]]&lt;br /&gt;
* [[PHPUnit installation in OS X]]&lt;br /&gt;
&lt;br /&gt;
== Uninstalling previous PEAR based version ==&lt;br /&gt;
&lt;br /&gt;
Before using composer, this page used to suggest to install phpunit via PEAR. If you did so, you may wish to uninstall that package now. This should work:&lt;br /&gt;
&lt;br /&gt;
   $ pear uninstall phpunit/DbUnit&lt;br /&gt;
   $ pear uninstall phpunit/PHPUnit&lt;br /&gt;
&lt;br /&gt;
=Initialisation of test environment=&lt;br /&gt;
&lt;br /&gt;
Our PHPUnit integration requires a dedicated database and dataroot.  First, add a new dataroot directory and prefix into your config.php, you can find examples in config-dist.php (scroll down to &#039;Section 9&#039;).&lt;br /&gt;
&lt;br /&gt;
 $CFG-&amp;gt;phpunit_prefix = &#039;phpu_&#039;;&lt;br /&gt;
 $CFG-&amp;gt;phpunit_dataroot = &#039;/home/example/phpu_moodledata&#039;;&lt;br /&gt;
&lt;br /&gt;
Some PHPUnit tests require a live internet connection. If your system does not have a direct connection to the Internet, you also need to specify your proxy in config.php - even though you normally specify it by using the admin settings user interface. (If you do not use a proxy, you can skip this step.) Check the settings on the relevant admin settings page, or from the mdl_config table in your database, if you are unsure of the correct values.&lt;br /&gt;
 &lt;br /&gt;
 // Normal proxy settings&lt;br /&gt;
 $CFG-&amp;gt;proxyhost = &#039;wwwcache.example.org&#039;;&lt;br /&gt;
 $CFG-&amp;gt;proxyport = 80;&lt;br /&gt;
 $CFG-&amp;gt;proxytype = &#039;HTTP&#039;;&lt;br /&gt;
 $CFG-&amp;gt;proxybypass = &#039;localhost, 127.0.0.1, .example.org&#039;;&lt;br /&gt;
 // Omit the next lines if your proxy doesn&#039;t need a username/password:&lt;br /&gt;
 $CFG-&amp;gt;proxyuser = &#039;systemusername&#039;;&lt;br /&gt;
 $CFG-&amp;gt;proxypassword = &#039;systempassword&#039;;&lt;br /&gt;
&lt;br /&gt;
From Moodle 2.8.5 onwards, you can also provide specific database settings for unit testing (if these are not provided, the standard database settings will be used):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$CFG-&amp;gt;phpunit_dbtype    = &#039;pgsql&#039;;      // &#039;pgsql&#039;, &#039;mariadb&#039;, &#039;mysqli&#039;, &#039;mssql&#039;, &#039;sqlsrv&#039; or &#039;oci&#039;&lt;br /&gt;
$CFG-&amp;gt;phpunit_dblibrary = &#039;native&#039;;     // &#039;native&#039; only at the moment&lt;br /&gt;
$CFG-&amp;gt;phpunit_dbhost    = &#039;127.0.0.1&#039;;  // eg &#039;localhost&#039; or &#039;db.isp.com&#039; or IP&lt;br /&gt;
$CFG-&amp;gt;phpunit_dbname    = &#039;mytestdb&#039;;     // database name, eg moodle&lt;br /&gt;
$CFG-&amp;gt;phpunit_dbuser    = &#039;postgres&#039;;   // your database username&lt;br /&gt;
$CFG-&amp;gt;phpunit_dbpass    = &#039;some_password&#039;;   // your database password&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you need to initialise the test environment using following command.&lt;br /&gt;
&lt;br /&gt;
 cd /home/example/moodle&lt;br /&gt;
 php admin/tool/phpunit/cli/init.php&lt;br /&gt;
&lt;br /&gt;
This command has to be repeated after any upgrade, plugin (un)installation or if you have added tests to a plugin you are developing:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NOTE:&#039;&#039;&#039; make sure that your php cli executable (or the one you want to use) is correctly on your path as the individual init scripts will call it repeatedly. Also, ensure en_AU locale is installed on your server.&lt;br /&gt;
&lt;br /&gt;
== LDAP ==&lt;br /&gt;
If you want to run LDAP unit tests you must have a working, configured LDAP environment on your test server. There must be a basic LDAP tree structure in place or tests will fail with &amp;quot;Search: No such object&amp;quot;. Build an LDAP tree structure in a new shell prompt:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ ldapadd -H ldap://127.0.0.1 -D &amp;quot;cn=admin,dc=yourcomputer,dc=local&amp;quot; -W&lt;br /&gt;
dn:dc=yourcomputer,dc=local&lt;br /&gt;
objectClass:dcObject&lt;br /&gt;
objectClass:organizationalUnit&lt;br /&gt;
dc:yourcomputer&lt;br /&gt;
ou:YOURCOMPUTER&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In config.php tell Moodle where to look for your test LDAP environment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Constants for auth/ldap tests.&lt;br /&gt;
define(&#039;TEST_AUTH_LDAP_HOST_URL&#039;, &#039;ldap://127.0.0.1&#039;);&lt;br /&gt;
define(&#039;TEST_AUTH_LDAP_BIND_DN&#039;, &#039;cn=admin,dc=yourcomputer,dc=local&#039;);&lt;br /&gt;
define(&#039;TEST_AUTH_LDAP_BIND_PW&#039;, &#039;*&#039;);&lt;br /&gt;
define(&#039;TEST_AUTH_LDAP_DOMAIN&#039;, &#039;dc=yourcomputer,dc=local&#039;);&lt;br /&gt;
&lt;br /&gt;
// Constants for enrol/ldap tests.&lt;br /&gt;
define(&#039;TEST_ENROL_LDAP_HOST_URL&#039;, &#039;ldap://127.0.0.1&#039;);&lt;br /&gt;
define(&#039;TEST_ENROL_LDAP_BIND_DN&#039;, &#039;cn=admin,dc=yourcomputer,dc=local&#039;);&lt;br /&gt;
define(&#039;TEST_ENROL_LDAP_BIND_PW&#039;, &#039;*&#039;);&lt;br /&gt;
define(&#039;TEST_ENROL_LDAP_DOMAIN&#039;, &#039;dc=yourcomputer,dc=local&#039;);&lt;br /&gt;
&lt;br /&gt;
// Constants for lib/ldap tests.&lt;br /&gt;
define(&#039;TEST_LDAPLIB_HOST_URL&#039;, &#039;ldap://127.0.0.1&#039;);&lt;br /&gt;
define(&#039;TEST_LDAPLIB_BIND_DN&#039;, &#039;cn=admin,dc=yourcomputer,dc=local&#039;);&lt;br /&gt;
define(&#039;TEST_LDAPLIB_BIND_PW&#039;, &#039;*&#039;);&lt;br /&gt;
define(&#039;TEST_LDAPLIB_DOMAIN&#039;, &#039;dc=yourcomputer,dc=local&#039;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Test execution=&lt;br /&gt;
&lt;br /&gt;
To execute all test suites from main configuration file execute the &amp;lt;code&amp;gt;vendor/bin/phpunit&amp;lt;/code&amp;gt; script from your &amp;lt;code&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/code&amp;gt; directory.&lt;br /&gt;
&lt;br /&gt;
 cd /home/example/moodle&lt;br /&gt;
 vendor/bin/phpunit&lt;br /&gt;
&lt;br /&gt;
The rest of examples uses &amp;lt;code&amp;gt;phpunit&amp;lt;/code&amp;gt;, please substitute it with &amp;lt;code&amp;gt;vendor/bin/phpunit&amp;lt;/code&amp;gt; or create a shortcut in your dirroot.&lt;br /&gt;
&lt;br /&gt;
In IDEs, you may need to specify the path to the PHPUnit configuration file. Use the absolute path to &amp;lt;code&amp;gt;phpunit.xml&amp;lt;/code&amp;gt; from your &amp;lt;code&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There is an alternative script for running of tests via web interface: &amp;lt;code&amp;gt;admin/tool/phpunit/webrunner.php&amp;lt;/code&amp;gt;. Use this as the last resort only when you cannot use the command-line interface on your test server. It will most probably break due to permissions problems if you try to execute it both from command-line and from webrunner. This feature is not officially supported.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===How to run only some tests===&lt;br /&gt;
&lt;br /&gt;
==== Running a single test quickly ====&lt;br /&gt;
&lt;br /&gt;
The fastest way to run a single test is:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code php&amp;gt;&lt;br /&gt;
vendor/bin/phpunit my_test_class_name my/tests/filename.php&amp;lt;/code&amp;gt;&lt;br /&gt;
so, run this command in the CLI to see a real test in action:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
vendor/bin/phpunit core_cohort_cohortlib_testcase cohort/tests/cohortlib_test.php&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is faster than other methods because it avoids the need to search for the test file, but you should be careful because it may be possible to run tests this way which are not included in the normal run. If you use this method, do at least one full test run (or --group run, as below) to ensure the test can be found.&lt;br /&gt;
&lt;br /&gt;
Please note the path name is optional if the testcase class and file names are standardised as described in [[Writing PHPUnit tests]].&lt;br /&gt;
&lt;br /&gt;
==== Using the @group annotation ====&lt;br /&gt;
&lt;br /&gt;
If you add annotations like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Unit tests for {@link stack_cas_keyval}.&lt;br /&gt;
 * @group qtype_stack&lt;br /&gt;
 */&lt;br /&gt;
class qtype_stack_cas_keyval_exception_testcase extends basic_testcase {&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to all the classes in your plugin, then you can run just the tests for your plugin by doing&lt;br /&gt;
&lt;br /&gt;
 phpunit --group qtype_stack&lt;br /&gt;
&lt;br /&gt;
Therefore, it is suggested that you annotate all your tests with the Frankenstyle name of your plugin.&lt;br /&gt;
&lt;br /&gt;
==== Using multiple phpunit.xml files ====&lt;br /&gt;
&lt;br /&gt;
It&#039;s easy to create alternative phpunit.xml files defining which tests must be run together. For reference, take a look to the default /phpunit.xml available in your base directory once the testing environment has been initialised. After creating the custom file you will be able to run those tests with&lt;br /&gt;
&lt;br /&gt;
 vendor/bin/phpunit -c path/to/your/alternative/phpunit/file.xml&lt;br /&gt;
&lt;br /&gt;
Also, for commodity, you can use this command:&lt;br /&gt;
&lt;br /&gt;
 php admin/tool/phpunit/cli/util.php --buildcomponentconfigs&lt;br /&gt;
&lt;br /&gt;
It will, automatically, create one valid phpunit.xml file within each component (plugin or subsystem) and other important directories, so later you will be able to execute tests like&lt;br /&gt;
&lt;br /&gt;
 vendor/bin/phpunit -c mod/forum[/phpunit.xml]  // Note that it&#039;s not needed to specify the name of the file (if it is &#039;phpunit.xml&#039;).&lt;br /&gt;
 vendor/bin/phpunit -c question&lt;br /&gt;
 vendor/bin/phpunit -c question/type/calculated&lt;br /&gt;
 vendor/bin/phpunit -c backup&lt;br /&gt;
 vendor/bin/phpunit -c lib/dml&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
or, also&lt;br /&gt;
&lt;br /&gt;
 cd directory/with/phpunit.xml&lt;br /&gt;
 phpunit&lt;br /&gt;
&lt;br /&gt;
=External test resources=&lt;br /&gt;
{{Moodle 2.6}}&lt;br /&gt;
By default Moodle phpunit tests contact http://download.moodle.org server when testing curl related functionality. Optionally you may checkout a local copy of the test scripts and access it instead:&lt;br /&gt;
&lt;br /&gt;
# clone https://github.com/moodlehq/moodle-exttests to web directory&lt;br /&gt;
# add to your config.php or modify phpunit.xml file &amp;lt;code php&amp;gt;define(&#039;TEST_EXTERNAL_FILES_HTTP_URL&#039;, &#039;http://localhost/moodle-exttests&#039;);&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Writing new tests=&lt;br /&gt;
* read [http://www.phpunit.de/manual/current/en/ official PHPUnit online documentation]&lt;br /&gt;
* see [[Writing PHPUnit tests]]&lt;br /&gt;
&lt;br /&gt;
=Conversion of existing SimpleTests=&lt;br /&gt;
* see [[SimpleTest conversion]]&lt;br /&gt;
&lt;br /&gt;
=PHPUnit support in IDEs=&lt;br /&gt;
&lt;br /&gt;
* [[Setting up Eclipse]]&lt;br /&gt;
* [[Setting up Netbeans]]&lt;br /&gt;
* [[Setting up PhpStorm]]&lt;br /&gt;
&lt;br /&gt;
=Common Unit Test Problems=&lt;br /&gt;
[[Common unit test problems]]&lt;br /&gt;
&lt;br /&gt;
=Performance=&lt;br /&gt;
&lt;br /&gt;
A typical run of full PHPUnit tests for Moodle 2.7 takes 10-20 minutes depending on the machine. If tests run slowly for you:&lt;br /&gt;
&lt;br /&gt;
* Ensure you are using a database and filesystem running on the same machine that is running the tests (not on a remote server).&lt;br /&gt;
* Apply developer-only performance settings to your database: [[Postgres Tuning For Developers]]&lt;br /&gt;
&lt;br /&gt;
=Command line tips=&lt;br /&gt;
&lt;br /&gt;
* Run all tests for a plugin (mod_mymodule in this example): vendor/bin/phpunit --testsuite=mod_mymodule_testsuite &lt;br /&gt;
* Run only named test: vendor/bin/phpunit --filter=test_my_test_function_name&lt;br /&gt;
* Display each test name before running it (useful e.g. if it crashes before the end and you want to know which test it was running at that point) vendor/bin/phpunit --debug (you will probably want to redirect this to a file as it gets very long).&lt;br /&gt;
&lt;br /&gt;
[[Category:Unit testing]]&lt;/div&gt;</summary>
		<author><name>F4bo</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Security&amp;diff=54319</id>
		<title>Security</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Security&amp;diff=54319"/>
		<updated>2018-06-01T13:57:40Z</updated>

		<summary type="html">&lt;p&gt;F4bo: /* Check permissions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page describes how to write secure Moodle code that is not vulnerable to anything that evil people may try to throw at it.&lt;br /&gt;
&lt;br /&gt;
The page is organised around the common types of security vulnerability. For each one, it explains&lt;br /&gt;
# what the danger is,&lt;br /&gt;
# how Moodle is designed to avoid the problem,&lt;br /&gt;
# what you need to do as a Moodle developer to keep your code secure, and&lt;br /&gt;
# what you can do as an administrator, to make your Moodle more secure.&lt;br /&gt;
The explanation of each vulnerability is on a separate page, linked to in the list below.&lt;br /&gt;
&lt;br /&gt;
This page also summarises all the key guidelines.&lt;br /&gt;
&lt;br /&gt;
==Security of web applications==&lt;br /&gt;
&lt;br /&gt;
===Secure web app requirements===&lt;br /&gt;
Some companies require maximum level of security for web applications. You can often see similar general recommendations:&lt;br /&gt;
* separate administration backend&lt;br /&gt;
* no sensitive information stored in web application&lt;br /&gt;
* communication has to be encrypted using SSL&lt;br /&gt;
* log all user actions&lt;br /&gt;
* server applications have to be completely separated&lt;br /&gt;
* no files uploaded by users on server&lt;br /&gt;
* no rich text entered by users on server (limited plain text only)&lt;br /&gt;
* validate user identity and actions via separate channel&lt;br /&gt;
* always keep every software up-to-date&lt;br /&gt;
* no 3rd party browser extensions recommended&lt;br /&gt;
* use only one web page, do not open multiple windows with different sites, close/open browser before and after using the secure app&lt;br /&gt;
&lt;br /&gt;
Web based banking systems are the best examples of these &#039;&#039;secure&#039;&#039; web applications. Security is the top most priority here, security incidents may cost money - either the customer, bank or insurance company, public image of the institution may be damaged too. Limiting factors may be cost of application development, maintenance, usability but also the cost of communication via the alternative channels.&lt;br /&gt;
&lt;br /&gt;
===Balanced security===&lt;br /&gt;
As you can see many web applications today violate the security design rules. For example web based mail system have to accept rich text messages with file attachments, mail massages often contain very sensitive information. In fact the Web 2.0 idea goes directly agains the security design rules, everybody is submitting content - only app designer/administrator should be adding trusted content.&lt;br /&gt;
&lt;br /&gt;
When designing web applications we have to find out what our users are supposed to be doing and then find some reasonable balance between features and security.&lt;br /&gt;
&lt;br /&gt;
==Moodle security design==&lt;br /&gt;
The security of web application depends on the intended use and features available for each type of user.&lt;br /&gt;
&lt;br /&gt;
===Types of users===&lt;br /&gt;
&lt;br /&gt;
====Administrators====&lt;br /&gt;
Administrators have following privileges:&lt;br /&gt;
* change all settings&lt;br /&gt;
* create courses&lt;br /&gt;
* access all courses&lt;br /&gt;
* modify language packs&lt;br /&gt;
* modify all users&lt;br /&gt;
&lt;br /&gt;
Indirectly administrators are allowed to execute shell and PHP code. Moodle administrators may be partially restricted by hardcoding settings in config.php. &lt;br /&gt;
Low level server administrators can not be restricted because they can read content of PHP files and may modify them.&lt;br /&gt;
&lt;br /&gt;
All administrators have to be fully trusted.&lt;br /&gt;
&lt;br /&gt;
====Teachers====&lt;br /&gt;
Teachers are usually creating course content, enrolling students and teaching. They usually need following privileges:&lt;br /&gt;
* upload files and submit html texts&lt;br /&gt;
* create and manage activities&lt;br /&gt;
* access student grades and other personal information&lt;br /&gt;
&lt;br /&gt;
Uploading of files with javascript, flash and other scripted is often considered to be a security risk. Unfortunately we can not remove these risks from teacher roles because even basic SCORM packages consist of html and javascript which needs to use user session.&lt;br /&gt;
&lt;br /&gt;
Browser trusts everything coming from one server, it does not know anything about our courses or origin of data. Once the file is uploaded to server it becomes part of the server application. It is not possible to differentiate between wanted and unwanted code stored on the server.&lt;br /&gt;
&lt;br /&gt;
All teachers with risky capabilities have to be trusted, it is not possible to give teacher access to students. In theory teachers may use XSS attacks to gain administrator access.&lt;br /&gt;
&lt;br /&gt;
Technically it would be possible to create system where teachers can not attack other users but it would prevent all javascript, flash, SCORM, java, html forms, SVG, etc. &lt;br /&gt;
&lt;br /&gt;
====Students====&lt;br /&gt;
Students are participating in courses, they are not trusted. Students need following privileges:&lt;br /&gt;
* post formatted text with inline images and attachments&lt;br /&gt;
* upload binary documents&lt;br /&gt;
&lt;br /&gt;
Uploaded files must not be opened directly in browser from the same server. Instead the files need to be served from different domain, or server has to force download of all files to local hard drive before opening them. Serving of untrusted files from different domain will be implemented in 2.0.&lt;br /&gt;
&lt;br /&gt;
All student submitted text has to be sanitised before printing the text on any page, this prevent XSS attacks on other users but at the same time prevents flash, javascript, SVG and all other HTML scripting. There are two types of html cleaning - less reliable blacklisting (KSES) and more robust whitelisting (HTML Purifier).&lt;br /&gt;
&lt;br /&gt;
====Guests====&lt;br /&gt;
For security reasons unregistered users can not be allowed to upload any files or submit any text that is stored in database. Guests could try to spam other users, exploit problems in html cleaning routines, abuse other vulnerabilities or try social engineering based attacks.&lt;br /&gt;
&lt;br /&gt;
Sites with enabled user sign-up  via email have to take extra care in order to prevent spamming and other types of attacks.&lt;br /&gt;
&lt;br /&gt;
===Capability risks===&lt;br /&gt;
&lt;br /&gt;
Moodle is very flexible system, administrators may define multiple roles. Each role is a set of capabilities defined at system level, roles may be modified via overrides at lower context levels. [[Risks]] are part of description of each capability, administrators have to make sure only trusted users get potentially dangerous capabilities.&lt;br /&gt;
&lt;br /&gt;
==Common types of security vulnerability==&lt;br /&gt;
&lt;br /&gt;
* [[Security:Unauthenticated access|Unauthenticated access]]&lt;br /&gt;
* [[Security:Unauthorised access|Unauthorised access]]&lt;br /&gt;
* [[Security:Cross-site_request_forgery|Cross-site request forgery]] (XSRF)&lt;br /&gt;
* [[Security:Cross-site scripting|Cross-site scripting]] (XSS)&lt;br /&gt;
* [[Security:SQL injection|SQL injection]]&lt;br /&gt;
* [[Security:Command-line injection|Command-line injection]]&lt;br /&gt;
* [[Security:Data-loss|Data-loss]]&lt;br /&gt;
* [[Security:Confidential information leakage|Confidential information leakage]]&lt;br /&gt;
* [[Security:Configuration information leakage|Configuration information leakage]]&lt;br /&gt;
* [[Security:Session fixation|Session fixation]]&lt;br /&gt;
* [[Security:Denial of service|Denial of service]]&lt;br /&gt;
* [[Security:Brute-forcing login|Brute-forcing login]]&lt;br /&gt;
* [[Security:Insecure configuration management|Insecure configuration management]]&lt;br /&gt;
* [[Security:Buffer overruns, and other platform weaknesses|Buffer overruns, and other platform weaknesses]]&lt;br /&gt;
* [[Security:Social engineering|Social engineering]]&lt;br /&gt;
&lt;br /&gt;
==Summary of the guidelines==&lt;br /&gt;
&lt;br /&gt;
===Authenticate the user===&lt;br /&gt;
&lt;br /&gt;
* With very few exceptions, every script should call &#039;&#039;&#039;[[Access_API#require_login.28.29|require_login]]&#039;&#039;&#039; or &#039;&#039;&#039;[[Access_API#require_course_login.28.29|require_course_login]]&#039;&#039;&#039; as near the start as possible.&lt;br /&gt;
&lt;br /&gt;
===Verify course and module access===&lt;br /&gt;
* all course areas have to be protected by &amp;quot;require_login&amp;quot; or &amp;quot;require_course_login&amp;quot; with correct $course parameter&lt;br /&gt;
* all module areas have to be protected by &amp;quot;require_login&amp;quot; or &amp;quot;require_course_login&amp;quot; with correct $course and $cm parameter&lt;br /&gt;
&lt;br /&gt;
===Check permissions===&lt;br /&gt;
&lt;br /&gt;
* Before allowing the user to see anything or do anything, call to &#039;&#039;&#039;[[Access_API#has_capability.28.29|has_capability]]&#039;&#039;&#039; or &#039;&#039;&#039;[[Access_API#require_capability.28.29|require_capability]]&#039;&#039;&#039;.&lt;br /&gt;
* Capabilities should be annotated with the appropriate &#039;&#039;&#039;[[Hardening_new_Roles_system#Basic_risks|risks]]&#039;&#039;&#039;.&lt;br /&gt;
* If appropriate, restrict what people can see according to &#039;&#039;&#039;groups&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
===Don&#039;t trust any input from users===&lt;br /&gt;
&lt;br /&gt;
* Use &#039;&#039;&#039;[[lib/formslib.php|moodleforms]]&#039;&#039;&#039; whenever possible, with an appropriate &#039;&#039;&#039;setType&#039;&#039;&#039; method call for each field.&lt;br /&gt;
* Before performing actions, use &#039;&#039;&#039;data_submitted() &amp;amp;&amp;amp; confirm_sesskey()&#039;&#039;&#039; to check sesskey and that you are handling a POST request. &lt;br /&gt;
* Before destroying large amounts of data, add a confirmation step.&lt;br /&gt;
* If not using a moodleform, clean input using &#039;&#039;&#039;optional_param&#039;&#039;&#039; or &#039;&#039;&#039;required_param&#039;&#039;&#039; with an appropriate &#039;&#039;&#039;PARAM_...&#039;&#039;&#039; type.&lt;br /&gt;
** Do not access $_GET, $_POST or $_REQUEST directly.&lt;br /&gt;
** Group optional_param and required_param calls together at the top of the script, to make them easy to find.&lt;br /&gt;
&lt;br /&gt;
Similarly, clean data from other external resources like RSS feeds before use.&lt;br /&gt;
&lt;br /&gt;
===Clean and escape data before output===&lt;br /&gt;
&lt;br /&gt;
* Use &#039;&#039;&#039;s&#039;&#039;&#039; or &#039;&#039;&#039;p&#039;&#039;&#039; to output plain text content.&lt;br /&gt;
* use &#039;&#039;&#039;format_string&#039;&#039;&#039; to output content with minimal HTML like multi-lang spans (for example, course and activity names).&lt;br /&gt;
* Use &#039;&#039;&#039;format_text&#039;&#039;&#039; to output all other content.&lt;br /&gt;
** Only use $options-&amp;gt;noclean if it requires a capability with RISK_XSS to input that content (for example web page resources).&lt;br /&gt;
* Before Moodle 2.0, input from optional_param or required_param must have [[Developer:Slashes|&#039;&#039;&#039;stripslashes&#039;&#039;&#039; or &#039;&#039;&#039;stripslashes_recursive&#039;&#039;&#039;]] applied if it is being output directly, rather than being stored in the database.&lt;br /&gt;
* Data destined for JavaScript should be escaped using &#039;&#039;&#039;$PAGE-&amp;gt;requires-&amp;gt;data_for_js&#039;&#039;&#039; (Moodle 2.0) or &#039;&#039;&#039;addslashes_js&#039;&#039;&#039; (Moodle 1.9).&lt;br /&gt;
&lt;br /&gt;
See [[Output functions]] for more details.&lt;br /&gt;
&lt;br /&gt;
===Escape data before storing it in the database===&lt;br /&gt;
&lt;br /&gt;
* Use the XMLDB library. This takes care of most escaping issues for you.&lt;br /&gt;
* When you must use custom SQL code, &#039;&#039;&#039;use place-holders&#039;&#039;&#039; to insert values into the queries.&lt;br /&gt;
** Before Moodle 2.0, you have to build SQL by concatenating strings. Take particular care, especially with quoting values, to avoid SQL injection vulnerabilities.&lt;br /&gt;
* Before Moodle 2.0, data loaded from the database must have [[Developer:Slashes|&#039;&#039;&#039;addslashes&#039;&#039;&#039; or &#039;&#039;&#039;addslashes_object&#039;&#039;&#039;]] applied to it before it can be written back to the database. (addslashes should no longer be use anywhere in Moodle 2.0 code.)&lt;br /&gt;
* In Moodle 2.0 variables must be passed to database queries through bound parameters&lt;br /&gt;
&lt;br /&gt;
===Escape data before using it in shell commands===&lt;br /&gt;
&lt;br /&gt;
* Avoid shell commands if at all possible.&lt;br /&gt;
** Look to see if there is a PHP library instead.&lt;br /&gt;
* If you can&#039;t avoid shell commands, use &#039;&#039;&#039;escapeshellcmd&#039;&#039;&#039; and &#039;&#039;&#039;escapeshellarg&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Log every request===&lt;br /&gt;
&lt;br /&gt;
* Every script should call &#039;&#039;&#039;add_to_log&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Other good practice===&lt;br /&gt;
&lt;br /&gt;
... that helps with security.&lt;br /&gt;
&lt;br /&gt;
* Structure your code nicely, minimising the use of global variables. This makes the flow of data, and hence security, easier to verify.&lt;br /&gt;
* Initialise objects ($x = new stdClass;) and arrays ($x = array()) before you first use them.&lt;br /&gt;
* Test every input field with tricky input to ensure that it is escaped and un-escaped the right number of times everywhere, and that Unicode characters are not corrupted. My standard test input is:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&amp;lt; &amp;gt; &amp;amp; &amp;amp;amp;lt; &amp;amp;amp;gt; &amp;amp;amp;amp; &#039; \&#039; 碁 \ \\&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[Process#Security_issues|Security issue process]]&lt;br /&gt;
* [[Coding]]&lt;br /&gt;
* The [http://cwe.mitre.org/top25/ 2010 CWE/SANS Top 25 Most Dangerous Programming Errors] - this is a generic list of common security mistakes, produced by a group of security experts. The above Moodle-specific guidelines cover most of these general points.&lt;br /&gt;
* [https://www.owasp.org/index.php/PHP_Security_Cheat_Sheet PHP Security Cheat Sheet]&lt;br /&gt;
* [https://www.owasp.org/index.php/Main_Page The Open Web Application Security Project (OWASP)] - Large collection of documentation and code samples about preventing security issues while using web technologies.&lt;br /&gt;
&lt;br /&gt;
[[Category:Security]]&lt;br /&gt;
[[Category:Coding guidelines|Security]]&lt;/div&gt;</summary>
		<author><name>F4bo</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Security&amp;diff=54318</id>
		<title>Security</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Security&amp;diff=54318"/>
		<updated>2018-06-01T13:53:19Z</updated>

		<summary type="html">&lt;p&gt;F4bo: /* Authenticate the user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page describes how to write secure Moodle code that is not vulnerable to anything that evil people may try to throw at it.&lt;br /&gt;
&lt;br /&gt;
The page is organised around the common types of security vulnerability. For each one, it explains&lt;br /&gt;
# what the danger is,&lt;br /&gt;
# how Moodle is designed to avoid the problem,&lt;br /&gt;
# what you need to do as a Moodle developer to keep your code secure, and&lt;br /&gt;
# what you can do as an administrator, to make your Moodle more secure.&lt;br /&gt;
The explanation of each vulnerability is on a separate page, linked to in the list below.&lt;br /&gt;
&lt;br /&gt;
This page also summarises all the key guidelines.&lt;br /&gt;
&lt;br /&gt;
==Security of web applications==&lt;br /&gt;
&lt;br /&gt;
===Secure web app requirements===&lt;br /&gt;
Some companies require maximum level of security for web applications. You can often see similar general recommendations:&lt;br /&gt;
* separate administration backend&lt;br /&gt;
* no sensitive information stored in web application&lt;br /&gt;
* communication has to be encrypted using SSL&lt;br /&gt;
* log all user actions&lt;br /&gt;
* server applications have to be completely separated&lt;br /&gt;
* no files uploaded by users on server&lt;br /&gt;
* no rich text entered by users on server (limited plain text only)&lt;br /&gt;
* validate user identity and actions via separate channel&lt;br /&gt;
* always keep every software up-to-date&lt;br /&gt;
* no 3rd party browser extensions recommended&lt;br /&gt;
* use only one web page, do not open multiple windows with different sites, close/open browser before and after using the secure app&lt;br /&gt;
&lt;br /&gt;
Web based banking systems are the best examples of these &#039;&#039;secure&#039;&#039; web applications. Security is the top most priority here, security incidents may cost money - either the customer, bank or insurance company, public image of the institution may be damaged too. Limiting factors may be cost of application development, maintenance, usability but also the cost of communication via the alternative channels.&lt;br /&gt;
&lt;br /&gt;
===Balanced security===&lt;br /&gt;
As you can see many web applications today violate the security design rules. For example web based mail system have to accept rich text messages with file attachments, mail massages often contain very sensitive information. In fact the Web 2.0 idea goes directly agains the security design rules, everybody is submitting content - only app designer/administrator should be adding trusted content.&lt;br /&gt;
&lt;br /&gt;
When designing web applications we have to find out what our users are supposed to be doing and then find some reasonable balance between features and security.&lt;br /&gt;
&lt;br /&gt;
==Moodle security design==&lt;br /&gt;
The security of web application depends on the intended use and features available for each type of user.&lt;br /&gt;
&lt;br /&gt;
===Types of users===&lt;br /&gt;
&lt;br /&gt;
====Administrators====&lt;br /&gt;
Administrators have following privileges:&lt;br /&gt;
* change all settings&lt;br /&gt;
* create courses&lt;br /&gt;
* access all courses&lt;br /&gt;
* modify language packs&lt;br /&gt;
* modify all users&lt;br /&gt;
&lt;br /&gt;
Indirectly administrators are allowed to execute shell and PHP code. Moodle administrators may be partially restricted by hardcoding settings in config.php. &lt;br /&gt;
Low level server administrators can not be restricted because they can read content of PHP files and may modify them.&lt;br /&gt;
&lt;br /&gt;
All administrators have to be fully trusted.&lt;br /&gt;
&lt;br /&gt;
====Teachers====&lt;br /&gt;
Teachers are usually creating course content, enrolling students and teaching. They usually need following privileges:&lt;br /&gt;
* upload files and submit html texts&lt;br /&gt;
* create and manage activities&lt;br /&gt;
* access student grades and other personal information&lt;br /&gt;
&lt;br /&gt;
Uploading of files with javascript, flash and other scripted is often considered to be a security risk. Unfortunately we can not remove these risks from teacher roles because even basic SCORM packages consist of html and javascript which needs to use user session.&lt;br /&gt;
&lt;br /&gt;
Browser trusts everything coming from one server, it does not know anything about our courses or origin of data. Once the file is uploaded to server it becomes part of the server application. It is not possible to differentiate between wanted and unwanted code stored on the server.&lt;br /&gt;
&lt;br /&gt;
All teachers with risky capabilities have to be trusted, it is not possible to give teacher access to students. In theory teachers may use XSS attacks to gain administrator access.&lt;br /&gt;
&lt;br /&gt;
Technically it would be possible to create system where teachers can not attack other users but it would prevent all javascript, flash, SCORM, java, html forms, SVG, etc. &lt;br /&gt;
&lt;br /&gt;
====Students====&lt;br /&gt;
Students are participating in courses, they are not trusted. Students need following privileges:&lt;br /&gt;
* post formatted text with inline images and attachments&lt;br /&gt;
* upload binary documents&lt;br /&gt;
&lt;br /&gt;
Uploaded files must not be opened directly in browser from the same server. Instead the files need to be served from different domain, or server has to force download of all files to local hard drive before opening them. Serving of untrusted files from different domain will be implemented in 2.0.&lt;br /&gt;
&lt;br /&gt;
All student submitted text has to be sanitised before printing the text on any page, this prevent XSS attacks on other users but at the same time prevents flash, javascript, SVG and all other HTML scripting. There are two types of html cleaning - less reliable blacklisting (KSES) and more robust whitelisting (HTML Purifier).&lt;br /&gt;
&lt;br /&gt;
====Guests====&lt;br /&gt;
For security reasons unregistered users can not be allowed to upload any files or submit any text that is stored in database. Guests could try to spam other users, exploit problems in html cleaning routines, abuse other vulnerabilities or try social engineering based attacks.&lt;br /&gt;
&lt;br /&gt;
Sites with enabled user sign-up  via email have to take extra care in order to prevent spamming and other types of attacks.&lt;br /&gt;
&lt;br /&gt;
===Capability risks===&lt;br /&gt;
&lt;br /&gt;
Moodle is very flexible system, administrators may define multiple roles. Each role is a set of capabilities defined at system level, roles may be modified via overrides at lower context levels. [[Risks]] are part of description of each capability, administrators have to make sure only trusted users get potentially dangerous capabilities.&lt;br /&gt;
&lt;br /&gt;
==Common types of security vulnerability==&lt;br /&gt;
&lt;br /&gt;
* [[Security:Unauthenticated access|Unauthenticated access]]&lt;br /&gt;
* [[Security:Unauthorised access|Unauthorised access]]&lt;br /&gt;
* [[Security:Cross-site_request_forgery|Cross-site request forgery]] (XSRF)&lt;br /&gt;
* [[Security:Cross-site scripting|Cross-site scripting]] (XSS)&lt;br /&gt;
* [[Security:SQL injection|SQL injection]]&lt;br /&gt;
* [[Security:Command-line injection|Command-line injection]]&lt;br /&gt;
* [[Security:Data-loss|Data-loss]]&lt;br /&gt;
* [[Security:Confidential information leakage|Confidential information leakage]]&lt;br /&gt;
* [[Security:Configuration information leakage|Configuration information leakage]]&lt;br /&gt;
* [[Security:Session fixation|Session fixation]]&lt;br /&gt;
* [[Security:Denial of service|Denial of service]]&lt;br /&gt;
* [[Security:Brute-forcing login|Brute-forcing login]]&lt;br /&gt;
* [[Security:Insecure configuration management|Insecure configuration management]]&lt;br /&gt;
* [[Security:Buffer overruns, and other platform weaknesses|Buffer overruns, and other platform weaknesses]]&lt;br /&gt;
* [[Security:Social engineering|Social engineering]]&lt;br /&gt;
&lt;br /&gt;
==Summary of the guidelines==&lt;br /&gt;
&lt;br /&gt;
===Authenticate the user===&lt;br /&gt;
&lt;br /&gt;
* With very few exceptions, every script should call &#039;&#039;&#039;[[Access_API#require_login.28.29|require_login]]&#039;&#039;&#039; or &#039;&#039;&#039;[[Access_API#require_course_login.28.29|require_course_login]]&#039;&#039;&#039; as near the start as possible.&lt;br /&gt;
&lt;br /&gt;
===Verify course and module access===&lt;br /&gt;
* all course areas have to be protected by &amp;quot;require_login&amp;quot; or &amp;quot;require_course_login&amp;quot; with correct $course parameter&lt;br /&gt;
* all module areas have to be protected by &amp;quot;require_login&amp;quot; or &amp;quot;require_course_login&amp;quot; with correct $course and $cm parameter&lt;br /&gt;
&lt;br /&gt;
===Check permissions===&lt;br /&gt;
&lt;br /&gt;
* Before allowing the user to see anything or do anything, call to &#039;&#039;&#039;has_capability&#039;&#039;&#039; or &#039;&#039;&#039;require_capability&#039;&#039;&#039;.&lt;br /&gt;
* Capabilities should be annotated with the appropriate &#039;&#039;&#039;[[Hardening_new_Roles_system#Basic_risks|risks]]&#039;&#039;&#039;.&lt;br /&gt;
* If appropriate, restrict what people can see according to &#039;&#039;&#039;groups&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
===Don&#039;t trust any input from users===&lt;br /&gt;
&lt;br /&gt;
* Use &#039;&#039;&#039;[[lib/formslib.php|moodleforms]]&#039;&#039;&#039; whenever possible, with an appropriate &#039;&#039;&#039;setType&#039;&#039;&#039; method call for each field.&lt;br /&gt;
* Before performing actions, use &#039;&#039;&#039;data_submitted() &amp;amp;&amp;amp; confirm_sesskey()&#039;&#039;&#039; to check sesskey and that you are handling a POST request. &lt;br /&gt;
* Before destroying large amounts of data, add a confirmation step.&lt;br /&gt;
* If not using a moodleform, clean input using &#039;&#039;&#039;optional_param&#039;&#039;&#039; or &#039;&#039;&#039;required_param&#039;&#039;&#039; with an appropriate &#039;&#039;&#039;PARAM_...&#039;&#039;&#039; type.&lt;br /&gt;
** Do not access $_GET, $_POST or $_REQUEST directly.&lt;br /&gt;
** Group optional_param and required_param calls together at the top of the script, to make them easy to find.&lt;br /&gt;
&lt;br /&gt;
Similarly, clean data from other external resources like RSS feeds before use.&lt;br /&gt;
&lt;br /&gt;
===Clean and escape data before output===&lt;br /&gt;
&lt;br /&gt;
* Use &#039;&#039;&#039;s&#039;&#039;&#039; or &#039;&#039;&#039;p&#039;&#039;&#039; to output plain text content.&lt;br /&gt;
* use &#039;&#039;&#039;format_string&#039;&#039;&#039; to output content with minimal HTML like multi-lang spans (for example, course and activity names).&lt;br /&gt;
* Use &#039;&#039;&#039;format_text&#039;&#039;&#039; to output all other content.&lt;br /&gt;
** Only use $options-&amp;gt;noclean if it requires a capability with RISK_XSS to input that content (for example web page resources).&lt;br /&gt;
* Before Moodle 2.0, input from optional_param or required_param must have [[Developer:Slashes|&#039;&#039;&#039;stripslashes&#039;&#039;&#039; or &#039;&#039;&#039;stripslashes_recursive&#039;&#039;&#039;]] applied if it is being output directly, rather than being stored in the database.&lt;br /&gt;
* Data destined for JavaScript should be escaped using &#039;&#039;&#039;$PAGE-&amp;gt;requires-&amp;gt;data_for_js&#039;&#039;&#039; (Moodle 2.0) or &#039;&#039;&#039;addslashes_js&#039;&#039;&#039; (Moodle 1.9).&lt;br /&gt;
&lt;br /&gt;
See [[Output functions]] for more details.&lt;br /&gt;
&lt;br /&gt;
===Escape data before storing it in the database===&lt;br /&gt;
&lt;br /&gt;
* Use the XMLDB library. This takes care of most escaping issues for you.&lt;br /&gt;
* When you must use custom SQL code, &#039;&#039;&#039;use place-holders&#039;&#039;&#039; to insert values into the queries.&lt;br /&gt;
** Before Moodle 2.0, you have to build SQL by concatenating strings. Take particular care, especially with quoting values, to avoid SQL injection vulnerabilities.&lt;br /&gt;
* Before Moodle 2.0, data loaded from the database must have [[Developer:Slashes|&#039;&#039;&#039;addslashes&#039;&#039;&#039; or &#039;&#039;&#039;addslashes_object&#039;&#039;&#039;]] applied to it before it can be written back to the database. (addslashes should no longer be use anywhere in Moodle 2.0 code.)&lt;br /&gt;
* In Moodle 2.0 variables must be passed to database queries through bound parameters&lt;br /&gt;
&lt;br /&gt;
===Escape data before using it in shell commands===&lt;br /&gt;
&lt;br /&gt;
* Avoid shell commands if at all possible.&lt;br /&gt;
** Look to see if there is a PHP library instead.&lt;br /&gt;
* If you can&#039;t avoid shell commands, use &#039;&#039;&#039;escapeshellcmd&#039;&#039;&#039; and &#039;&#039;&#039;escapeshellarg&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Log every request===&lt;br /&gt;
&lt;br /&gt;
* Every script should call &#039;&#039;&#039;add_to_log&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Other good practice===&lt;br /&gt;
&lt;br /&gt;
... that helps with security.&lt;br /&gt;
&lt;br /&gt;
* Structure your code nicely, minimising the use of global variables. This makes the flow of data, and hence security, easier to verify.&lt;br /&gt;
* Initialise objects ($x = new stdClass;) and arrays ($x = array()) before you first use them.&lt;br /&gt;
* Test every input field with tricky input to ensure that it is escaped and un-escaped the right number of times everywhere, and that Unicode characters are not corrupted. My standard test input is:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&amp;lt; &amp;gt; &amp;amp; &amp;amp;amp;lt; &amp;amp;amp;gt; &amp;amp;amp;amp; &#039; \&#039; 碁 \ \\&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[Process#Security_issues|Security issue process]]&lt;br /&gt;
* [[Coding]]&lt;br /&gt;
* The [http://cwe.mitre.org/top25/ 2010 CWE/SANS Top 25 Most Dangerous Programming Errors] - this is a generic list of common security mistakes, produced by a group of security experts. The above Moodle-specific guidelines cover most of these general points.&lt;br /&gt;
* [https://www.owasp.org/index.php/PHP_Security_Cheat_Sheet PHP Security Cheat Sheet]&lt;br /&gt;
* [https://www.owasp.org/index.php/Main_Page The Open Web Application Security Project (OWASP)] - Large collection of documentation and code samples about preventing security issues while using web technologies.&lt;br /&gt;
&lt;br /&gt;
[[Category:Security]]&lt;br /&gt;
[[Category:Coding guidelines|Security]]&lt;/div&gt;</summary>
		<author><name>F4bo</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Moodle_architecture&amp;diff=54317</id>
		<title>Moodle architecture</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Moodle_architecture&amp;diff=54317"/>
		<updated>2018-06-01T13:07:30Z</updated>

		<summary type="html">&lt;p&gt;F4bo: /* An overview of Moodle core */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The aim of this document is to quickly give an overview of how the Moodle works at a technical level. Hopefully this will be helpful to Administrators and Developers. However, to actually do Moodle development you will have to study the code in much more detail than is provided here. See the [[Developer_documentation|developer documentation]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What is Moodle?==&lt;br /&gt;
&lt;br /&gt;
Moodle is a Learning Management System, Course Management System, or Virtual Learning Environment, depending on which term you prefer. Its goal is to give teachers and students the tools they need to teach and learn. Moodle comes from a background of [[:en:Pedagogy|Social Constructionist pedagogy]], however, it can be used to support any style of teaching and learning.&lt;br /&gt;
&lt;br /&gt;
There are other types of software systems that are important for educational institutions, for example ePortfolios, Student Information Systems and Content repositories. Generally, Moodle does not try to re-invent these areas of functionality. Instead, tries to be the best LMS possible, and then interoperate gracefully with other systems that provide the other areas of functionality. It is, however, perfectly possible to use Moodle as a stand-alone system, without integrating it with anything else.&lt;br /&gt;
&lt;br /&gt;
Moodle is a web application written in PHP. Moodle is open source. Copyright is owned by individual contributors, not assigned to a single entity, although the company Moodle Pty Ltd in Perth Australia, owned by Moodle&#039;s founder Martin Dougiamas, manages the project.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Overview of a Moodle installation==&lt;br /&gt;
&lt;br /&gt;
A Moodle installation comprises the Moodle code executing in a PHP-capable web server; a database managed by MySQL, PostgreSQL, Microsoft SQL Server, MariaDB, or Oracle; and a file store for uploaded and generated files (the moodledata folder).&lt;br /&gt;
&lt;br /&gt;
All three parts can run on a single server; or they can be separated with many load-balanced web-servers, a database cluster, and a file-server; or anywhere between those extremes.&lt;br /&gt;
&lt;br /&gt;
Moodle is designed to be simple to install on any server that meets these basic requirements. Moodle self-installs once the code has been copied to the web server and a blank database created (see [[:en:Installing Moodle]]). Similarly, Moodle can always self-upgrade from one version to the next (see [[:en:Upgrading]]).&lt;br /&gt;
&lt;br /&gt;
==Moodle as a modular system==&lt;br /&gt;
&lt;br /&gt;
Like many successful open source systems, Moodle is structured as an application core, surrounded by numerous plugins to provide specific functionality. Moodle is designed to be highly extensible and customizable without modifying the core libraries, as doing so would create problems when upgrading Moodle to a newer version. So when customizing or extending your own Moodle install, always do so through the plugin architecture. &lt;br /&gt;
&lt;br /&gt;
Plugins in Moodle are of specific types. That is, an authentication plugin and an activity module will communicate with Moodle core using different APIs, tailored to the type of functionality the plugin provides. Functionality common to all plugins (installation, upgrade, permissions, configuration, ...) are, however, handled consistently across all plugin types.&lt;br /&gt;
&lt;br /&gt;
The standard Moodle distribution includes Moodle core and a number of plugins of each type, so that a new Moodle installation can immediately be used to start teaching and learning. After installation a Moodle site can be adapted for a particular purpose by changing the default configuration option, and by installing add-ons or removing standard plugins. Most add-ons that have been shared publicly are listed in the [http://moodle.org/plugins/index.php Moodle Plugins Directory].&lt;br /&gt;
&lt;br /&gt;
Physically, a Moodle plugin is just a folder of PHP scripts (and CSS, JavaScript, etc. if necessary). Moodle core communicates with the plugin by looking for particular entry points, often defined in the file &amp;lt;tt&amp;gt;lib.php&amp;lt;/tt&amp;gt; within the plugin.&lt;br /&gt;
&lt;br /&gt;
* See [[Communication Between Components]] on how different Moodle components and plugins communicate with each other.&lt;br /&gt;
&lt;br /&gt;
==An overview of Moodle core==&lt;br /&gt;
&lt;br /&gt;
Moodle core provides all the infrastructure necessary to build a Learning Management System. It implements the key concepts that all the different plugins will need to work with. These include:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Courses and activities:&#039;&#039;&#039;&lt;br /&gt;
A Moodle course is a sequence of activities and resources grouped into sections. Courses themselves are organized into a hierarchical set of categories within a Moodle site.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Users:&#039;&#039;&#039;&lt;br /&gt;
In moodle, users are anyone who uses the moodle system. In order to participate in course users need to be enrolled into course with a given role, such as:&lt;br /&gt;
&lt;br /&gt;
*Students&lt;br /&gt;
*Teachers&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Course enrolment&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*Enrolment gives user the possibility to participate in course as a student or teacher.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;User functionality in moodle:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
*User roles in moodle: Roles assigned to users give them a set of capabilities in given context. For example: Teacher, Student and Forum moderator are examples of roles.&lt;br /&gt;
*User&#039;s capabilities in moodle: A capability is a description of some particular Moodle feature. Capabilities are associated with roles. For example, mod/forum:replypost is a capability.&lt;br /&gt;
*Context: A context is a &amp;quot;space&amp;quot; in the Moodle, such as courses, activity modules, blocks etc.&lt;br /&gt;
*Permissions: A permission is some value that is assigned for a capability for a particular role. For example, allow or prevent.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Added facilities provided by moodle:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
*Creation and editing of user profiles: In moodle, the moment an user creates his account, a profile is created for that user. The user needs to fill in his initial details for completing his profile. The user generally always have the permission to edit his own profile anytime on moodle.&lt;br /&gt;
*Groups and cohorts: Cohorts, or site-wide groups, enable all members of a cohort to be enrolled in a course in one action, either manually or synchronised automatically.&lt;br /&gt;
*Enrolments and access control: Users are generally enrolled into some courses and according to their permission settings and the groups to which they belong, they have limited access on moodle.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;A bit more about moodle:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
*Activity and course completion: The activity completion system allows activities such as Quizzes, SCORM modules, etc. to be marked complete when specified conditions are met.&lt;br /&gt;
*Navigation, settings and configuration: The Navigation block provide easy access to view various sections of the Moodle site and includes:&lt;br /&gt;
**My home - a personalised home page displaying links to the courses a user is associated with and activity information (such as unread forum posts and upcoming assignments)&lt;br /&gt;
**Site pages - links to site pages and resources from the front page of Moodle&lt;br /&gt;
**My profile - quick links allowing a user to view their profile, forums posts, blogs and messages as well as manage their private files&lt;br /&gt;
**My courses - lists (by course shortname) and links to courses the user is associated with. Click the course&#039;s shortname to view the front page of the course or use the arrows to navigate quickly to a specific section, resource or activity.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;JavaScript library:&#039;&#039;&#039;&lt;br /&gt;
*Moodle has adopted the Yahoo User Interface library. There is also a nice system for loading the additional JavaScript files required by each page.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Upgrading moodle:&#039;&#039;&#039;&lt;br /&gt;
Moodle can be upgraded in four simple steps:&lt;br /&gt;
&lt;br /&gt;
#Make sure that your server can run the latest Moodle version&lt;br /&gt;
#You should always be prepared to &amp;quot;roll back&amp;quot; if there&#039;s an issue with your data or some custom code you&#039;ve added. So before committing, create a test install and always make backups.&lt;br /&gt;
#At this stage you can replace the Moodle code on your server with the version you downloaded and check for the plugins.&lt;br /&gt;
#Perform the upgrade by triggering the upgrade from the admin page.&lt;br /&gt;
&lt;br /&gt;
(More information about upgrading can be found here : [[:en:Upgrade overview|Upgrade overview]])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Logs and statistics in moodle:&#039;&#039;&#039;&lt;br /&gt;
*Statistics in moodle: The statistics graphs and tables show how many hits there have been on various parts of your site during various time frames. They do not show how many distinct users there have been. They are processed daily at a time you specify. You must enable statistics before you will see anything.&lt;br /&gt;
*Log in moodle: Logs in Moodle are activity reports. Logs are available at site level and course level.&lt;br /&gt;
&lt;br /&gt;
==The most important plugin types==&lt;br /&gt;
&lt;br /&gt;
Please see the page [[Plugin types]] for the full list. A selection of the most common ones is highlighted here.&lt;br /&gt;
&lt;br /&gt;
===Activities and resources===&lt;br /&gt;
&lt;br /&gt;
Activities and resources are the most basic individual components that make up a course and are the main tools for teaching and learning. Some examples of resources are: pages, links and IMS content packages. Examples of activities include: forums, wikis, quizzes, and assignments.&lt;br /&gt;
&lt;br /&gt;
Activities are by far the largest type of plugin in terms of amount of code. A forum or wiki system could be a software project in its own right. Both activities and resources are installed in the &amp;lt;tt&amp;gt;mod&amp;lt;/tt&amp;gt; folder.&lt;br /&gt;
&lt;br /&gt;
===Blocks===&lt;br /&gt;
&lt;br /&gt;
Blocks are small bits of interface functionality that can be added to (normally the sides of) pages. Many blocks provide additional views of data stored and modified elsewhere. Blocks live in the &amp;lt;tt&amp;gt;blocks&amp;lt;/tt&amp;gt; folder.&lt;br /&gt;
&lt;br /&gt;
===Themes===&lt;br /&gt;
&lt;br /&gt;
The overall visual style of a Moodle site, a particular course, or all courses in a category, can be changed by selecting a different theme at these different levels. Themes are the standard way of making aesthetic changes to your Moodle application, decoupling presentation from content and functionality.&lt;br /&gt;
&lt;br /&gt;
Themes live in the &amp;lt;tt&amp;gt;theme&amp;lt;/tt&amp;gt; folder, and published themes are listed in the [http://moodle.org/mod/data/view.php?id=6552 Themes database].&lt;br /&gt;
&lt;br /&gt;
===Language packs===&lt;br /&gt;
&lt;br /&gt;
Moodle is internationalised. That is, you can get language packs for many different languages. Language packs are normally downloaded and installed via the Moodle administration screens, but they can also be downloaded manually from [http://download.moodle.org/langpack/ Moodle.org: language packs].&lt;br /&gt;
&lt;br /&gt;
Administrators can also manually change any of the standard user interface strings if the terms used in the installed language pack are not appropriate.&lt;br /&gt;
&lt;br /&gt;
===Course formats===&lt;br /&gt;
&lt;br /&gt;
control how the structure of the course, a sequence of activities grouped into sections, is presented to the users. Course formats live in the &amp;lt;tt&amp;gt;course/format&amp;lt;/tt&amp;gt; folder.&lt;br /&gt;
&lt;br /&gt;
===Authentication plugins===&lt;br /&gt;
&lt;br /&gt;
control how users log in. Moodle can manage usernames and passwords itself, or use those stored in LDAP or another database. Alternatively, Moodle can use a number of single-sign-on schemes. Authentication plugins live in the &amp;lt;tt&amp;gt;auth&amp;lt;/tt&amp;gt; folder.&lt;br /&gt;
&lt;br /&gt;
===Enrolment plugins===&lt;br /&gt;
&lt;br /&gt;
control which users are enrolled in which courses. Again this can be by synchronising with another system, perhaps a student information system, or it can be tracked internally by Moodle. Enrolment plugins live in the &amp;lt;tt&amp;gt;enrol&amp;lt;/tt&amp;gt; folder. (Moodle was created by an Australian, so enrol is the correct spelling ;-).)&lt;br /&gt;
&lt;br /&gt;
===Repository plugins===&lt;br /&gt;
&lt;br /&gt;
Ways for users to get content (files) into Moodle, either by uploading from their hard drive, or by getting the file from another location on the Internet, perhaps Drop Box, Google Docs, or Flickr. Repository plugins live in the &amp;lt;tt&amp;gt;repository&amp;lt;/tt&amp;gt; folder.&lt;br /&gt;
&lt;br /&gt;
==How Moodle code is organised==&lt;br /&gt;
&lt;br /&gt;
Moodle mostly follows a [http://martinfowler.com/eaaCatalog/transactionScript.html transaction script] approach. That is, suppose you are looking at a Forum. The URL will be &amp;lt;tt&amp;gt;.../mod/forum/view.php?id=1234&amp;lt;/tt&amp;gt;. and &amp;lt;tt&amp;gt;mod/forum/view.php&amp;lt;/tt&amp;gt; the PHP script that generates that page. One could argue that transaction script is not an appropriate pattern for an application as complex as Moodle. However, it is a very natural architecture for a PHP application, and Moodle is an aggregate of many different plugins, rather than a single complex application.&lt;br /&gt;
&lt;br /&gt;
Behind that basic transaction script approach, a lot of the core functionality has been refactored out into libraries (mostly in the &amp;lt;tt&amp;gt;lib&amp;lt;/tt&amp;gt; folder). This provides elements of a [http://martinfowler.com/eaaCatalog/domainModel.html domain model]. The Moodle project started before PHP could handle object-oriented code, however, so don&#039;t expect an object-oriented domain model except in some of the more recent parts of the Moodle code.&lt;br /&gt;
&lt;br /&gt;
There are two layers used to separate presentation from the business logic. The outer layer is the theme (see above) which controls the more visual aspects of the Moodle interface. Then there are renderer classes which generate the HTML to be output from the data supplied by the transaction scripts and the domain model. Unfortunately, neither PHP, nor the Moodle architecture, enforces a clear separation of the UI layer. It is possible for sloppy developers to make a mess, and this has happened in the past. The code in the standard Moodle distribution is gradually being cleaned up.&lt;br /&gt;
&lt;br /&gt;
==The Moodle database==&lt;br /&gt;
&lt;br /&gt;
The Moodle database comprises many tables (more than 250) because the whole database is an aggregate of the core tables and the tables belonging to each plugin. Fortunately, this large structure is understandable, because the tables for one particular plugin typically only link to each other and a few core tables. See [[Database_schema_introduction|Database schema introduction]] for more information.&lt;br /&gt;
&lt;br /&gt;
The Moodle database structure is defined in &amp;lt;tt&amp;gt;install.xml&amp;lt;/tt&amp;gt; files inside the &amp;lt;tt&amp;gt;db&amp;lt;/tt&amp;gt; folder in each plugin. For example &amp;lt;tt&amp;gt;mod/forum/db/install.xml&amp;lt;/tt&amp;gt; contains the database definition for the forum module. &amp;lt;tt&amp;gt;lib/db/install.xml&amp;lt;/tt&amp;gt; defines the tables used by Moodle core. The &amp;lt;tt&amp;gt;install.xml&amp;lt;/tt&amp;gt; files contain comments that should explain the purpose of each table and column. These comments can be turned into human-readable documentation by going to Site administration -&amp;gt; Development -&amp;gt; XMLDB editor in your Moodle installation, and clicking on the [Doc] link.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[:en:Administrator documentation]]&lt;br /&gt;
* [[Developer_documentation|Developer documentation]]&lt;br /&gt;
* [[Database_schema_introduction|Database schema introduction]]&lt;br /&gt;
* [http://moodle.org/mod/data/view.php?id=6009 Modules and plugins] and [http://moodle.org/mod/data/view.php?id=6552 themes] databases&lt;br /&gt;
* [http://moodle.org/mod/forum/discuss.php?d=64465 Forum thread with a vigorous argument about whether an architecture document is a good idea, which eventually lead to this page being rewritten]&lt;br /&gt;
* [http://www.aosabook.org/en/moodle.html The chapter of The Architecture of Open Source Applications about Moodle]&lt;br /&gt;
&lt;br /&gt;
[[Category:Coding guidelines]]&lt;/div&gt;</summary>
		<author><name>F4bo</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Testing&amp;diff=54307</id>
		<title>Testing</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Testing&amp;diff=54307"/>
		<updated>2018-05-31T13:20:17Z</updated>

		<summary type="html">&lt;p&gt;F4bo: /* QA regression testing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is the top level page regarding all testing activities around the Moodle project.  Testing is essential to make sure that developed code does what it is meant to do, without causing new problems.&lt;br /&gt;
&lt;br /&gt;
==Manual testing==&lt;br /&gt;
&lt;br /&gt;
===Code testing===&lt;br /&gt;
&lt;br /&gt;
Code is tested as part of reviewing at some key parts of the [[Process|Moodle development process]].&lt;br /&gt;
&lt;br /&gt;
* Development - the developer of some code should test their own work on a wide variety of environments for correctness and performance&lt;br /&gt;
* Peer review - developers often test each others work early in the development process&lt;br /&gt;
* Integration reviews - Our integration team tests code weekly while they are evaluating suitability for integration into Moodle.&lt;br /&gt;
&lt;br /&gt;
===Integration functional testing===&lt;br /&gt;
&lt;br /&gt;
On Wednesday (all timezones) our Moodle HQ developers spend the day to manually test the functionality of all the issues that have been integrated that week. Developers submitting patches should always cover the patch with unit tests and/or Behat behavioural tests. Still, all issues are tested by a human and it is usually worth it.&lt;br /&gt;
&lt;br /&gt;
* [[Testing of integrated issues]]&lt;br /&gt;
&lt;br /&gt;
===QA regression testing===&lt;br /&gt;
&lt;br /&gt;
For versions up to 2.4 all the [[QA testing]] was conducted manually before each major release.  This process will soon be replaced by a combination of automated systems (see below) and a more lightweight manual user acceptance testing (UAT) cycle involving the Moodle community prior to release.&lt;br /&gt;
&lt;br /&gt;
* [[QA testing]]&lt;br /&gt;
&lt;br /&gt;
==Automated testing==&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
&lt;br /&gt;
Moodle 2.3 and later fully supports PHPUnit tests as part of the code.  These are automated tests of very low-level code functionality that a developer should write as part of any new code.&lt;br /&gt;
&lt;br /&gt;
* [[PHPUnit_integration]]&lt;br /&gt;
&lt;br /&gt;
===Continuous integration testing===&lt;br /&gt;
&lt;br /&gt;
As soon as code is added to the integration repository, our continuous integration server tests the new code for:&lt;br /&gt;
&lt;br /&gt;
* Coding guidelines&lt;br /&gt;
* PHPUnit tests&lt;br /&gt;
* SimpleTest unit tests on older versions of Moodle&lt;br /&gt;
* Detect unresolved merge conflicts&lt;br /&gt;
* Compare databases upgraded from previous versions&lt;br /&gt;
* Check the version.php is correct&lt;br /&gt;
&lt;br /&gt;
A failure here notifies the integrators that the build has failed.&lt;br /&gt;
&lt;br /&gt;
===QA regression testing===&lt;br /&gt;
&lt;br /&gt;
Every day, an automated build in a test server runs a large number of tests concerning key functions of Moodle, to make sure that everything still works and that some new fix in Moodle hasn&#039;t caused problems elsewhere.&lt;br /&gt;
&lt;br /&gt;
These tests must pass completely before a new release can be made.&lt;br /&gt;
&lt;br /&gt;
* [[Acceptance testing]] using the Behat framework&lt;br /&gt;
* Performance testing using JMeter.&lt;br /&gt;
&lt;br /&gt;
Moodle uses a sponsored version of [https://www.browserstack.com BrowserStack] for testing on multiple browsers.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Quality Assurance]]&lt;/div&gt;</summary>
		<author><name>F4bo</name></author>
	</entry>
</feed>