Skip to content

Commit 8b0c671

Browse files
authored
Merge pull request ncstate-delta#687 from armccoy/enhancement/custom_entity_generators
tests: add generator for Behat tests
2 parents 0fce067 + 8231195 commit 8b0c671

6 files changed

Lines changed: 209 additions & 4 deletions

File tree

classes/webservice.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,15 @@ private function make_call($path, $data = [], $method = 'get') {
314314
if ($header['x-ratelimit-remaining'] == 0 && !empty($retryafter)) {
315315
set_config('retry-after', $retryafter, 'zoom');
316316
throw new api_limit_exception($response->message, $response->code, $retryafter);
317-
} else if (!(defined('PHPUNIT_TEST') && PHPUNIT_TEST)) {
317+
} else if (!((defined('PHPUNIT_TEST') && PHPUNIT_TEST) || (defined('BEHAT_TEST') && BEHAT_TEST))) {
318318
// When running CLI we might want to know how many calls remaining.
319319
debugging('x-ratelimit-remaining = ' . $header['x-ratelimit-remaining']);
320320
}
321321
}
322322

323323
debugging('Received 429 response, sleeping ' . strval($timediff) .
324324
' seconds until next retry. Current retry: ' . $this->makecallretries);
325-
if ($timediff > 0 && !(defined('PHPUNIT_TEST') && PHPUNIT_TEST)) {
325+
if ($timediff > 0 && !((defined('PHPUNIT_TEST') && PHPUNIT_TEST) || (defined('BEHAT_TEST') && BEHAT_TEST))) {
326326
sleep($timediff);
327327
}
328328
return $this->make_call($path, $data, $method);

lib.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function zoom_add_instance(stdClass $zoom, ?mod_zoom_mod_form $mform = null) {
7171
global $CFG, $DB;
7272
require_once($CFG->dirroot . '/mod/zoom/locallib.php');
7373

74-
if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) {
74+
if ((defined('PHPUNIT_TEST') && PHPUNIT_TEST) || (defined('BEHAT_TEST') && BEHAT_TEST)) {
7575
$zoom->id = $DB->insert_record('zoom', $zoom);
7676
zoom_grade_item_update($zoom);
7777
zoom_calendar_item_update($zoom);

tests/behat/behat_mod_zoom.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
require_once(__DIR__ . '/../../../../lib/behat/behat_base.php');
18+
use Behat\Behat\Context\Context;
19+
20+
/**
21+
* Behat steps for mod_zoom.
22+
*
23+
* @package mod_zoom
24+
* @copyright 2025 Alan McCoy
25+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26+
*/
27+
class behat_mod_zoom extends behat_base implements Context {
28+
/**
29+
* Convert page names to URLs for steps like 'When I am on the "[page name]" page'.
30+
*
31+
* Recognised page names are:
32+
* | None so far! | |
33+
*
34+
* @param string $page name of the page, with the component name removed e.g. 'Admin notification'.
35+
* @return moodle_url the corresponding URL.
36+
* @throws Exception with a meaningful error message if the specified page cannot be found.
37+
*/
38+
protected function resolve_page_url(string $page): moodle_url {
39+
switch (strtolower($page)) {
40+
default:
41+
throw new Exception("Unrecognized page type '{$page}'.");
42+
}
43+
}
44+
45+
/**
46+
* Convert page names to URLs for steps like 'When I am on the "[identifier]" "[page type]" page'.
47+
*
48+
* Recognised page names are:
49+
* | pagetype | name meaning | description |
50+
* | View | Zoom meeting name | The Zoom meeting activity page |
51+
*
52+
* @param string $type identifies which type of page this is, e.g. 'mod_zoom > View'.
53+
* @param string $identifier identifies the particular page, e.g. 'Test Meeting'.
54+
* @return moodle_url the corresponding URL.
55+
* @throws Exception with a meaningful error message if the specified page cannot be found.
56+
*/
57+
protected function resolve_page_instance_url(string $type, string $identifier): moodle_url {
58+
global $DB;
59+
60+
switch (strtolower($type)) {
61+
case 'view':
62+
return new moodle_url('/mod/zoom/view.php', [
63+
'id' => $this->get_cm_by_meeting_name($identifier)->id,
64+
]);
65+
case 'edit':
66+
return new moodle_url('/course/modedit.php', [
67+
'update' => $this->get_cm_by_meeting_name($identifier)->id,
68+
'return' => 0,
69+
]);
70+
default:
71+
throw new Exception('Unrecognized zoom page type: ' . $type);
72+
}
73+
}
74+
75+
/**
76+
* Get a Zoom meeting by name.
77+
*
78+
* @param string $name Zoom meeting name.
79+
* @return stdClass the corresponding DB row.
80+
*/
81+
protected function get_meeting_by_name(string $name): stdClass {
82+
global $DB;
83+
return $DB->get_record('zoom', ['name' => $name]);
84+
}
85+
86+
/**
87+
* Get a Zoom meeting cmid from the meeting name.
88+
*
89+
* @param string $name Zoom meeting name.
90+
* @return stdClass cm from get_coursemodule_from_instance.
91+
*/
92+
protected function get_cm_by_meeting_name(string $name): stdClass {
93+
$meeting = $this->get_meeting_by_name($name);
94+
return get_coursemodule_from_instance('zoom', $meeting->id, $meeting->course);
95+
}
96+
}

tests/behat/view_meeting.feature

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@mod @mod_zoom
2+
Feature: View a meeting
3+
4+
Background:
5+
Given the following "users" exist:
6+
| username | firstname | lastname | email |
7+
| teacher1 | Terry1 | Teacher1 | teacher1@example.com |
8+
| student1 | Sam1 | Student1 | student1@example.com |
9+
And the following "courses" exist:
10+
| fullname | shortname | format |
11+
| Course 1 | C1 | topics |
12+
And the following "course enrolments" exist:
13+
| user | course | role |
14+
| teacher1 | C1 | editingteacher |
15+
| student1 | C1 | student |
16+
And the following "activity" exists:
17+
| activity | zoom |
18+
| course | C1 |
19+
| idnumber | 00001 |
20+
| name | Meeting 1 |
21+
| intro | Test meeting description |
22+
| section | 1 |
23+
| grade | 100 |
24+
25+
@javascript
26+
Scenario: As a student, I should be able to view a Zoom meeting's details
27+
When I am on the "Meeting 1" "mod_zoom > View" page logged in as "student1"
28+
Then I should see "Start Time"
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
* Behat data generator for mod_zoom
19+
*
20+
* @package mod_zoom
21+
* @copyright 2025 Alan McCoy
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
class behat_mod_zoom_generator extends behat_generator_base {
25+
/**
26+
* Get a list of entities that can be created.
27+
*
28+
* @return array
29+
*/
30+
protected function get_creatable_entities(): array {
31+
return [
32+
'meetings' => [
33+
'singular' => 'meeting',
34+
'datagenerator' => 'meeting',
35+
'required' => ['name'],
36+
],
37+
];
38+
}
39+
40+
41+
/**
42+
* Look up the id of a Zoom meeting from its name.
43+
*
44+
* @param string $zoomname The Zoom activity name, for example 'Test meeting'.
45+
* @return int corresponding id.
46+
* @throws Exception
47+
*/
48+
protected function get_zoom_id(string $zoomname): int {
49+
global $DB;
50+
51+
if (!$id = $DB->get_field('zoom', 'id', ['name' => $zoomname])) {
52+
throw new Exception('There is no Zoom activity with name "' . $zoomname . '" does not exist');
53+
}
54+
return $id;
55+
}
56+
57+
/**
58+
* Get the activity id from its name
59+
*
60+
* @param string $activityname
61+
* @return int
62+
* @throws Exception
63+
*/
64+
protected function get_activity_id(string $activityname): int {
65+
global $DB;
66+
67+
$sql = <<<EOF
68+
SELECT cm.instance
69+
FROM {course_modules} cm
70+
JOIN {modules} m ON m.id = cm.module
71+
JOIN {zoom} z ON z.id = cm.instance
72+
WHERE cm.idnumber = :idnumber OR z.name = :name
73+
EOF;
74+
$id = $DB->get_field_sql($sql, ['idnumber' => $activityname, 'name' => $activityname]);
75+
if (empty($id)) {
76+
throw new Exception("There is no Zoom meeting with name '{$activityname}' does not exist");
77+
}
78+
79+
return $id;
80+
}
81+
}

tests/generator/lib.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function create_instance($record = null, ?array $options = null) {
4545
'meetingcode' => '',
4646
'webinar' => 0,
4747
'option_host_video' => 0,
48-
'option_audio' => 0,
48+
'option_audio' => ZOOM_AUDIO_BOTH,
4949
'recurring' => 0,
5050
'option_participants_video' => 0,
5151
'option_jbh' => 0,

0 commit comments

Comments
 (0)