|
19 | 19 | require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); |
20 | 20 |
|
21 | 21 | use Behat\Mink\Exception\DriverException; |
| 22 | +use Behat\Mink\Exception\ExpectationException; |
22 | 23 | use Moodle\BehatExtension\Exception\SkippedException; |
23 | 24 |
|
24 | 25 | /** |
@@ -480,6 +481,106 @@ protected function get_mobile_url_scheme() { |
480 | 481 | return !empty($mobilesettings->forcedurlscheme) ? $mobilesettings->forcedurlscheme : 'moodlemobile'; |
481 | 482 | } |
482 | 483 |
|
| 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 | + |
483 | 584 | /** |
484 | 585 | * Get a coursemodule from an activity name or idnumber with course. |
485 | 586 | * |
|
0 commit comments