<commit_before>PHPUnit testing support in Moodle
==================================


Installation
------------
1. install PHPUnit PEAR extension - see [PHPUnit docs](http://www.phpunit.de/manual/current/en/installation.html) for more details
2. edit main config.php - add $CFG->phpunit_prefix and $CFG->phpunit_dataroot - see config-dist.php for more details
3. execute `php admin/tool/phpunit/cli/util.php --install` to initialise test database
4. it is necessary to reinitialise the test database manually after every upgrade or installation of new plugins


Test execution
--------------
* optionally generate phpunit.xml by executing `php admin/tool/phpunit/cli/util.php --buildconfig` - it collects test cases from all plugins
* execute `phpunit` shell command from dirroot directory
* you can also execute a single test `phpunit core_phpunit_basic_testcase lib/tests/phpunit_test.php`
* or all tests in one directory `phpunit --configuration phpunit.xml lib/tests/*_test.php`
* it is possible to create custom configuration files in xml format and use `phpunit -c myconfig.xml`


How to add more tests
---------------------
1. create `tests` directory in any plugin
2. add `*_test.php` files with custom class that extends `basic_testcase` or `advanced_testcase`
3. manually add all core unit test locations to `phpunit.xml.dist`


How to convert existing tests
-----------------------------
1. create new test file in `xxx/tests/yyy_test.php`
2. copy contents of the old test file
3. replace `extends UnitTestCase` with `extends basic_testcase`
4. fix setUp, tearDown, asserts, etc.
5. some old SimpleTest tests can be executed directly - mocking, database operations, assert(), etc. does not work, you may need to add `global $CFG;` before includes


FAQs
----
* Why is it necessary to execute the tests from the command line? PHPUnit is designed to be executed from shell, existing Moodle globals and constants would interfere with it.
* Why `tests` subdirectory? It is very unlikely that it will collide with any plugin name because plugin names use singular form.
* Why is it necessary to include core and plugin suites in configuration files? PHPUnit does not seem to allow dynamic loading of tests from our dir structure.


TODO
----
* add plugin callbacks to data generator
* convert remaining tests
* hide old SimpleTests and FUnctional DB tests in UI
* shell script that prepares everything for the first execution
* optionally support for execution of tests and cli/util.php from web UI (to be implemented via shell execution)<commit_msg>MDL-32149 final readme cleanup for stage 2 of PHPUnit integration<commit_after>PHPUnit testing support in Moodle
==================================


Installation
------------
1. install PHPUnit PEAR extension - see [PHPUnit docs](http://www.phpunit.de/manual/current/en/installation.html) for more details
2. edit main config.php - add $CFG->phpunit_prefix and $CFG->phpunit_dataroot - see config-dist.php for more details
3. execute `php admin/tool/phpunit/cli/util.php --install` from dirroot to initialise test database and dataroot
4. it is necessary to reinitialise the test database manually after every upgrade or installation of new plugins


Test execution
--------------
* optionally generate phpunit.xml by executing `php admin/tool/phpunit/cli/util.php --buildconfig` - it collects test cases from all plugins
* execute `phpunit` shell command from dirroot directory
* you can also execute a single test `phpunit core_phpunit_basic_testcase lib/tests/phpunit_test.php`
* or all tests in one directory `phpunit --configuration phpunit.xml lib/tests/*_test.php`
* it is possible to create custom configuration files in xml format and use `phpunit -c myconfig.xml`


How to add more tests
---------------------
1. create `tests` directory in any plugin if does not already exist
2. add `*_test.php` files with custom class that extends `basic_testcase` or `advanced_testcase`
3. execute your new test, for example `phpunit core_phpunit_basic_testcase local/mytest/tests/mytest_test.php`
4. optionally build new phpunit.xml by executing `php admin/tool/phpunit/cli/util.php --buildconfig` and run all tests
5. core developers have to add all core unit test locations to `phpunit.xml.dist`


How to convert existing tests
-----------------------------
1. create new test file in `xxx/tests/yyy_test.php`
2. copy contents of the old test file
3. replace `extends UnitTestCase` with `extends basic_testcase`
4. fix setUp, tearDown, asserts, etc.
5. some old SimpleTest tests can be executed directly - mocking, database operations, assert(), etc. does not work, you may need to add `global $CFG;` before includes


FAQs
----
* Why is it necessary to execute the tests from the command line? PHPUnit is designed to be executed from shell, existing Moodle globals and constants would interfere with it.
* Why `tests` subdirectory? It is very unlikely that it will collide with any plugin name because plugin names use singular form.
* Why is it necessary to include core and plugin suites in configuration files? PHPUnit does not seem to allow dynamic loading of tests from our dir structure.


TODO
----
* add plugin callbacks to data generator
* convert remaining tests
* delete all simpletests
* hide old SimpleTests in UI and delete Functional DB tests
* shell script that prepares everything for the first execution
* optional support for execution of tests and cli/util.php from web UI (to be implemented via shell execution)
{'fraction_non_alphanumeric': 0.09998178838098706, 'fraction_numerical': 0.005827718084137679, 'mean_word_length': 3.9388489208633093, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 11}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3998742', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1389, 'n_words': 714}