Skip to content

Commit a82f4c5

Browse files
[auto-generated] Update plugin files
Check out the commits that caused these changes: moodlehq/moodleapp@2750898...55c083d
1 parent f77f115 commit a82f4c5

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

tests/behat/behat_app.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,40 @@ public function the_app_should_have_opened_url(bool $not, string $urlpattern, ?s
968968
});
969969
}
970970

971+
/**
972+
* Check that an event has been logged.
973+
*
974+
* @Then /^the following events should( not)? have been logged for (".+"|the system) in the app:$/
975+
*/
976+
public function the_event_should_have_been_logged(bool $not, string $username, TableNode $data) {
977+
$userid = $this->get_event_userid($username);
978+
979+
foreach ($data->getColumnsHash() as $event) {
980+
$eventname = $event['name'];
981+
$logs = $this->get_event_logs($userid, $event);
982+
983+
if (!$not && empty($logs)) {
984+
throw new ExpectationException("Logs for event '$eventname' not found", $this->getSession()->getDriver());
985+
}
986+
987+
if ($not && !empty($logs) && empty($event['other'])) {
988+
throw new ExpectationException("Logs for event '$eventname' found, but shouldn't have", $this->getSession()->getDriver());
989+
}
990+
991+
if (!empty($event['other'])) {
992+
$log = $this->find_event_log_with_other($logs, json_decode($event['other'], true));
993+
994+
if (!$not && is_null($log)) {
995+
throw new ExpectationException("Other data for event '$eventname' does not match", $this->getSession()->getDriver());
996+
}
997+
998+
if ($not && !is_null($log)) {
999+
throw new ExpectationException("Logs for event '$eventname' found, but shouldn't have", $this->getSession()->getDriver());
1000+
}
1001+
}
1002+
}
1003+
}
1004+
9711005
/**
9721006
* Switches to a newly-opened browser tab.
9731007
*

tests/behat/behat_app_helper.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
require_once(__DIR__ . '/../../../../lib/behat/behat_base.php');
2020

2121
use Behat\Mink\Exception\DriverException;
22+
use Behat\Mink\Exception\ExpectationException;
2223
use Moodle\BehatExtension\Exception\SkippedException;
2324

2425
/**
@@ -480,6 +481,106 @@ protected function get_mobile_url_scheme() {
480481
return !empty($mobilesettings->forcedurlscheme) ? $mobilesettings->forcedurlscheme : 'moodlemobile';
481482
}
482483

484+
/**
485+
* Get user id corresponding to the given username in event logs.
486+
*
487+
* @param string $username User name, or "the system" to refer to a non-user actor such as the system, the cli, or a cron job.
488+
* @return int Event user id.
489+
*/
490+
protected function get_event_userid(string $username): int {
491+
global $DB;
492+
493+
if ($username === 'the system') {
494+
return \core\event\base::USER_OTHER;
495+
}
496+
497+
if (str_starts_with($username, '"')) {
498+
$username = substr($username, 1, -1);
499+
}
500+
501+
$user = $DB->get_record('user', compact('username'));
502+
503+
if (is_null($user)) {
504+
throw new ExpectationException("'$username' user not found", $this->getSession()->getDriver());
505+
}
506+
507+
return $user->id;
508+
}
509+
510+
/**
511+
* Given event logs matching the given restrictions.
512+
*
513+
* @param array $event Event restrictions.
514+
* @return array Event logs.
515+
*/
516+
protected function get_event_logs(int $userid, array $event): array {
517+
global $DB;
518+
519+
$filters = [
520+
'origin' => 'ws',
521+
'eventname' => $event['name'],
522+
'userid' => $userid,
523+
'courseid' => empty($event['course']) ? 0 : $this->get_course_id($event['course']),
524+
];
525+
526+
if (!empty($event['relateduser'])) {
527+
$relateduser = $DB->get_record('user', ['username' => $event['relateduser']]);
528+
529+
$filters['relateduserid'] = $relateduser->id;
530+
}
531+
532+
if (!empty($event['activity'])) {
533+
$cm = $this->get_cm_by_activity_name_and_course($event['activity'], $event['activityname'], $event['course']);
534+
535+
$filters['contextinstanceid'] = $cm->id;
536+
}
537+
538+
if (!empty($event['object'])) {
539+
$namecolumns = [
540+
'book_chapters' => 'title',
541+
'glossary_entries' => 'concept',
542+
'lesson_pages' => 'title',
543+
'notifications' => 'subject',
544+
];
545+
546+
$field = $namecolumns[$event['object']] ?? 'shortname';
547+
$object = $DB->get_record_select(
548+
$event['object'],
549+
$DB->sql_compare_text($field) . ' = ' . $DB->sql_compare_text('?'),
550+
[$event['objectname']]
551+
);
552+
553+
$filters['objectid'] = $object->id;
554+
}
555+
556+
return $DB->get_records('logstore_standard_log', $filters);
557+
}
558+
559+
/**
560+
* Find a log matching the given other data.
561+
*
562+
* @param array $logs Event logs.
563+
* @param array $other Other data.
564+
* @return object Log matching the given other data, or null otherwise.
565+
*/
566+
protected function find_event_log_with_other(array $logs, array $other): ?object {
567+
foreach ($logs as $log) {
568+
$logother = json_decode($log->other, true);
569+
570+
if (empty($logother)) {
571+
continue;
572+
}
573+
574+
if (!empty(array_diff_assoc($other, array_intersect_assoc($other, $logother)))) {
575+
continue;
576+
}
577+
578+
return $log;
579+
}
580+
581+
return null;
582+
}
583+
483584
/**
484585
* Get a coursemodule from an activity name or idnumber with course.
485586
*

version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

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

9-
$plugin->version = 2024031200;
9+
$plugin->version = 2024041100;
1010
$plugin->requires = 2016052300;
1111
$plugin->maturity = MATURITY_STABLE;
1212
$plugin->release = '4.4.0';

0 commit comments

Comments
 (0)