Skip to content

[docs] Document new externallib_testcase #1419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions docs/apis/subsystems/external/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ Writing unit tests for an external service function is no different to writing u

## How to write an external function PHPUnit test

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

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`.

:::note External Services Testcase

An external testcase has been created to make testing external services easier.

When creating your tests, you can extend the `\core_external\tests\externallib_testcase` class instead of `\advanced_testcase`.

:::

```php title="mod/kitchen/tests/external/get_fruit_test.php"
<?php
// This file is part of Moodle - http://moodle.org/
Expand All @@ -41,28 +49,20 @@ For example, if you have written a service function in `[componentfolder]/classe
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace mod_kitchen\external;

/**
* Unit tests for the get_fruit function of the kitchen.
*
* @package mod_kitchen
* @category external
* @copyright 20XX Your Name
* @copyright Your Name
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_kitchen\external;

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');

class get_fruit_test extends externallib_advanced_testcase {

#[\PHPUnit\Framework\Attributes\CoversClass(get_fruit::class)]
class get_fruit_test extends \core_external\tests\externallib_testcase {
/**
* Test the execute function when capabilities are present.
*
* @covers \mod_fruit\external\get_fruit::execute
*/
public function test_capabilities(): void {
$this->resetAfterTest(true);
Expand Down Expand Up @@ -96,8 +96,6 @@ class get_fruit_test extends externallib_advanced_testcase {

/**
* Test the execute function when capabilities are missing.
*
* @covers \mod_fruit\external\get_fruit::execute
*/
public function test_capabilities_missing(): void {
global $USER;
Expand Down
8 changes: 8 additions & 0 deletions docs/devupdate.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ The `maxsections` setting in course formats is now deprecated. Previously, this
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.

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.

## Unit Testing

### Testing External Service classes

<Since versions={["5.1", "5.0.7", "4.5.11", "4.4.21"]} issueNumber="MDL-86301" />

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.