Skip to content

Commit fed7638

Browse files
committed
DEF-3115: Adding testing for subplugins
1 parent ddad73d commit fed7638

6 files changed

Lines changed: 250 additions & 0 deletions

File tree

classes/workflow_manager.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ public function get_step_class_names($steptype = null) {
235235
]
236236
];
237237
foreach ($plugins as $plugin => $dir) {
238+
if (!PHPUNIT_TEST && $plugin == 'testplugin') {
239+
continue;
240+
}
238241
$dirs[] = (object)[
239242
'path' => $dir . '/classes',
240243
'namespace' => "trigger_$plugin",
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace trigger_testplugin\steps\lookups;
18+
use tool_trigger\steps\lookups\base_lookup_step;
19+
20+
/**
21+
* A lookup step for testing purposes.
22+
*
23+
* @package trigger_testplugin
24+
* @copyright 2025 Moodle US
25+
* @author Oscar Nadjar <oscar.nadjar@moodle.com>
26+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27+
*/
28+
class subplugin_lookup_test_step extends base_lookup_step {
29+
30+
/**
31+
* The fields supplied by this step.
32+
* A string containing all the cohorts a user is assigned to.
33+
*
34+
* @var array
35+
*/
36+
private static $stepfields = array(
37+
'testlookupresult'
38+
);
39+
40+
protected function init() {
41+
}
42+
43+
/**
44+
* {@inheritDoc}
45+
* @see \tool_trigger\steps\base\base_step::execute()
46+
*/
47+
public function execute($step, $trigger, $event, $stepresults) {
48+
global $DB;
49+
$stepresults = [ 'testlookupresult' => 'test' ];
50+
return [true, $stepresults];
51+
}
52+
53+
/**
54+
* {@inheritDoc}
55+
* @see \tool_trigger\steps\base\base_step::form_definition_extra()
56+
*/
57+
public function form_definition_extra($form, $mform, $customdata) {
58+
}
59+
60+
/**
61+
* {@inheritDoc}
62+
* @see \tool_trigger\steps\base\base_step::get_step_desc()
63+
*/
64+
public static function get_step_desc() {
65+
return get_string('subplugin_lookup_test_step_desc', 'trigger_testplugin');
66+
}
67+
68+
/**
69+
* {@inheritDoc}
70+
* @see \tool_trigger\steps\base\base_step::get_step_name()
71+
*/
72+
public static function get_step_name() {
73+
return get_string('subplugin_lookup_test_step_name', 'trigger_testplugin');
74+
}
75+
76+
/**
77+
* {@inheritDoc}
78+
* @see \tool_trigger\steps\base\base_step::get_privacyfields()
79+
*/
80+
public static function get_privacyfields() {
81+
}
82+
83+
/**
84+
* Get a list of fields this step provides.
85+
*
86+
* @return array $stepfields The fields this step provides.
87+
*/
88+
public static function get_fields() {
89+
return self::$stepfields;
90+
}
91+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Language file for trigger_testplugin.
19+
*
20+
* @package trigger_testplugin
21+
* @copyright 2025 Moodle US
22+
* @author Oscar Nadjar <oscar.nadjar@moodle.com>
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
26+
defined('MOODLE_INTERNAL') || die();
27+
28+
$string['pluginname'] = 'Test Trigger Subplugin';
29+
$string['pluginname_help'] = 'This plugin is used for testing the subplugin functionality of the Tool Trigger plugin.';
30+
$string['subplugin_lookup_test_step_desc'] = 'This is a test subplugin step for the Tool Trigger plugin. It is used to test the functionality of the subplugin system.';
31+
$string['subplugin_lookup_test_step_name'] = 'Test Subplugin Step';

custom/testplugin/version.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Plugin version and other meta-data are defined here.
19+
*
20+
* @package trigger_testplugin
21+
* @copyright 2025 Moodle US
22+
* @author Oscar Nadjar <oscar.nadjar@moodle.com>
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
26+
defined('MOODLE_INTERNAL') || die();
27+
28+
$plugin->component = 'trigger_testplugin';
29+
$plugin->version = 2025042200;
30+
$plugin->requires = 2021051701;
31+
$plugin->supported = [404, 405];
32+
$plugin->maturity = MATURITY_STABLE;
33+
$plugin->release = '1.0.0';

tests/subplugin_step_test.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* "Fail" filter step's unit tests.
19+
*
20+
* @package tool_trigger
21+
* @author Aaron Wells <aaronw@catalyst.net.nz>
22+
* @copyright Catalyst IT 2018
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
26+
namespace tool_trigger;
27+
28+
defined('MOODLE_INTERNAL') || die();
29+
30+
global $CFG;
31+
32+
class subplugin_step_test extends \advanced_testcase {
33+
34+
/**
35+
* Test user.
36+
* @var
37+
*/
38+
protected $user;
39+
40+
/**
41+
* Test event.
42+
* @var
43+
*/
44+
protected $event;
45+
46+
/**
47+
* Create an event to use for testing.
48+
*/
49+
public function setup():void {
50+
// Create a user event.
51+
$this->requestssent = [];
52+
$this->user = \core_user::get_user_by_username('admin');
53+
$this->event = \core\event\user_profile_viewed::create([
54+
'objectid' => $this->user->id,
55+
'relateduserid' => $this->user->id,
56+
'context' => \context_user::instance($this->user->id),
57+
'other' => [
58+
'courseid' => 1,
59+
'courseshortname' => 'short name',
60+
'coursefullname' => 'full name'
61+
]
62+
]);
63+
}
64+
65+
/**
66+
* Basic use-case, with default values for settings. Find the
67+
* user identified at "userid", and add their data with the
68+
* prefix "user_".
69+
*/
70+
public function test_execute_basic() {
71+
$step = new \trigger_testplugin\steps\lookups\subplugin_lookup_test_step(json_encode([]));
72+
73+
list($status, $stepresults) = $step->execute(null, null, $this->event, []);
74+
75+
$this->assertTrue($status);
76+
$this->assertEquals('test', $stepresults['testlookupresult']);
77+
}
78+
}

tests/workflow_manager_test.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ public function test_get_steps_by_type() {
7979
);
8080
}
8181

82+
/**
83+
* Test getting step from custom plugin.
84+
*/
85+
public function test_custom_step_names() {
86+
87+
$stepclasses = ['\trigger_testplugin\steps\lookups\subplugin_lookup_test_step'];
88+
$stepobj = new \tool_trigger\workflow_manager();
89+
$steps = $stepobj->lookup_step_names($stepclasses);
90+
$this->assertEquals(
91+
get_string('subplugin_lookup_test_step_name', 'trigger_testplugin'),
92+
$steps['\trigger_testplugin\steps\lookups\subplugin_lookup_test_step']
93+
);
94+
}
95+
8296
/**
8397
* Test the code for validating the name of a step class and instantiating it.
8498
*

0 commit comments

Comments
 (0)