Skip to content

Commit 95686f0

Browse files
committed
[docs] Document new externallib_testcase
1 parent 70cdc6a commit 95686f0

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

docs/apis/subsystems/external/testing.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ Writing unit tests for an external service function is no different to writing u
2020

2121
## How to write an external function PHPUnit test
2222

23-
You should create one unit test testcase for each external service file, and it should be named after the file that it tests.
23+
You should create one unit test testcase for each external service class, and it should be named after the class that it tests.
2424

2525
For example, if you have written a service function in `[componentfolder]/classes/external/get_fruit.php`, you should write a unit test in `[componentfolder]/tests/external/get_fruit_test.php`.
2626

27+
:::note External Services Testcase
28+
29+
An external testcase has been created to make testing external services easier.
30+
31+
When creating your tests, you can extend the `\core_external\tests\externallib_testcase` class instead of `\advanced_testcase`.
32+
33+
:::
34+
2735
```php title="mod/kitchen/tests/external/get_fruit_test.php"
2836
<?php
2937
// This file is part of Moodle - http://moodle.org/
@@ -41,28 +49,20 @@ For example, if you have written a service function in `[componentfolder]/classe
4149
// You should have received a copy of the GNU General Public License
4250
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
4351

52+
namespace mod_kitchen\external;
53+
4454
/**
4555
* Unit tests for the get_fruit function of the kitchen.
4656
*
4757
* @package mod_kitchen
4858
* @category external
49-
* @copyright 20XX Your Name
59+
* @copyright Your Name
5060
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5161
*/
52-
53-
namespace mod_kitchen\external;
54-
55-
defined('MOODLE_INTERNAL') || die();
56-
57-
global $CFG;
58-
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
59-
60-
class get_fruit_test extends externallib_advanced_testcase {
61-
62+
#[\PHPUnit\Framework\Attributes\CoversClass(get_fruit::class)]
63+
class get_fruit_test extends \core_external\tests\externallib_testcase {
6264
/**
6365
* Test the execute function when capabilities are present.
64-
*
65-
* @covers \mod_fruit\external\get_fruit::execute
6666
*/
6767
public function test_capabilities(): void {
6868
$this->resetAfterTest(true);
@@ -96,8 +96,6 @@ class get_fruit_test extends externallib_advanced_testcase {
9696

9797
/**
9898
* Test the execute function when capabilities are missing.
99-
*
100-
* @covers \mod_fruit\external\get_fruit::execute
10199
*/
102100
public function test_capabilities_missing(): void {
103101
global $USER;

docs/devupdate.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@ The `maxsections` setting in course formats is now deprecated. Previously, this
2828
Although the `maxsections` setting remains available for now, it is marked as deprecated and will be removed in Moodle 6.0. Also, the `get_max_sections` from `core_courseformat\base` is also deprecated and will be removed in Moodle 6.0.
2929

3030
If your format plugin relies on `maxsections`, you should add a custom setting in your plugin to control section limits. For reference, see the week format plugin, which now uses its own setting for this functionality.
31+
32+
## Unit Testing
33+
34+
### Testing External Service classes
35+
36+
<Since versions={["5.1", "5.0.7", "4.5.11", "4.4.21"]} issueNumber="MDL-86301" />
37+
38+
A new `\core_external\tests\externallib_testcase` has been introduced to replace the `\externallib_advanced_testcase` class. The new class can be autoloaded, and is available from Moodle 4.4 onwards.

0 commit comments

Comments
 (0)