|
| 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 | + * Tests for contacts_job pagination. |
| 19 | + * |
| 20 | + * Regression coverage for the Batalas incident: a page of registrations that are all |
| 21 | + * Cancelled used to leave the paging cursor (lastsourcetimemodified) unchanged while the |
| 22 | + * Arlo response still advertised a "next" page, so contacts_job::run() re-requested the |
| 23 | + * identical page forever, flooding the Arlo API. |
| 24 | + * |
| 25 | + * @package enrol_arlo |
| 26 | + * @category phpunit |
| 27 | + * @copyright 2026 Arlo |
| 28 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 29 | + */ |
| 30 | + |
| 31 | +defined('MOODLE_INTERNAL') || die(); |
| 32 | + |
| 33 | +use enrol_arlo\local\client; |
| 34 | +use enrol_arlo\local\job\contacts_job; |
| 35 | +use enrol_arlo\local\persistent\job_persistent; |
| 36 | +use GuzzleHttp\Handler\MockHandler; |
| 37 | +use GuzzleHttp\HandlerStack; |
| 38 | +use GuzzleHttp\Middleware; |
| 39 | +use GuzzleHttp\Psr7\Response; |
| 40 | + |
| 41 | +class contacts_job_test extends advanced_testcase { |
| 42 | + |
| 43 | + /** @var enrol_arlo_generator $plugingenerator handle to plugin generator. */ |
| 44 | + protected $plugingenerator; |
| 45 | + |
| 46 | + /** @var array captured Guzzle request/response history for the current test. */ |
| 47 | + protected $history = []; |
| 48 | + |
| 49 | + /** |
| 50 | + * Test setup. |
| 51 | + */ |
| 52 | + public function setUp(): void { |
| 53 | + global $CFG; |
| 54 | + parent::setUp(); |
| 55 | + require_once($CFG->dirroot . '/enrol/arlo/lib.php'); |
| 56 | + $this->resetAfterTest(); |
| 57 | + $this->plugingenerator = $this->getDataGenerator()->get_plugin_generator('enrol_arlo'); |
| 58 | + $this->plugingenerator->enable_plugin(); |
| 59 | + $this->plugingenerator->setup_plugin(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Reset the injected HTTP handler so static state does not leak between tests. |
| 64 | + */ |
| 65 | + public function tearDown(): void { |
| 66 | + client::set_test_handler(null); |
| 67 | + $this->history = []; |
| 68 | + parent::tearDown(); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Build a course with an Arlo Event enrolment instance. |
| 73 | + * |
| 74 | + * @return stdClass the enrolment instance record. |
| 75 | + */ |
| 76 | + protected function create_arlo_instance() { |
| 77 | + $course = $this->getDataGenerator()->create_course(); |
| 78 | + $template = $this->plugingenerator->create_event_template(); |
| 79 | + $event = $this->plugingenerator->create_event($template); |
| 80 | + return $this->plugingenerator->create_event_enrolment_instance($course, $event); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Fetch the auto-registered contacts job for an enrolment instance. |
| 85 | + * |
| 86 | + * @param int $instanceid |
| 87 | + * @return job_persistent |
| 88 | + */ |
| 89 | + protected function get_contacts_job($instanceid) { |
| 90 | + return job_persistent::get_record( |
| 91 | + ['area' => 'enrolment', 'type' => 'contacts', 'instanceid' => $instanceid] |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Queue a sequence of fixture files as Arlo XML responses and route the client through them. |
| 97 | + * |
| 98 | + * @param array $fixtures fixture filenames under tests/fixtures/ |
| 99 | + */ |
| 100 | + protected function mock_arlo_responses(array $fixtures) { |
| 101 | + global $CFG; |
| 102 | + $responses = []; |
| 103 | + foreach ($fixtures as $fixture) { |
| 104 | + $xml = file_get_contents($CFG->dirroot . '/enrol/arlo/tests/fixtures/' . $fixture); |
| 105 | + $responses[] = new Response(200, ['Content-Type' => 'application/xml'], $xml); |
| 106 | + } |
| 107 | + $stack = HandlerStack::create(new MockHandler($responses)); |
| 108 | + $this->history = []; |
| 109 | + $stack->push(Middleware::history($this->history)); |
| 110 | + client::set_test_handler($stack); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * A full page of Cancelled registrations must still advance the cursor and stop paging, |
| 115 | + * rather than re-requesting the same page forever. |
| 116 | + */ |
| 117 | + public function test_all_cancelled_page_advances_cursor_and_stops_paging(): void { |
| 118 | + $instance = $this->create_arlo_instance(); |
| 119 | + $job = $this->get_contacts_job($instance->id); |
| 120 | + |
| 121 | + // Sanity: the cursor starts at the epoch default that the flood was stuck on. |
| 122 | + $this->assertSame('1970-01-01T00:00:00Z', $job->get('lastsourcetimemodified')); |
| 123 | + $this->assertEquals(0, $job->get('lastsourceid')); |
| 124 | + |
| 125 | + // Page 1: 3 Cancelled registrations + a "next" link. Page 2: empty (terminates). |
| 126 | + $this->mock_arlo_responses([ |
| 127 | + 'registrations_cancelled_page.xml', |
| 128 | + 'registrations_empty_page.xml', |
| 129 | + ]); |
| 130 | + |
| 131 | + $result = (new contacts_job($job))->run(); |
| 132 | + $this->assertTrue($result); |
| 133 | + |
| 134 | + // Exactly two requests: page 1, then page 2 - NOT an unbounded re-request loop. |
| 135 | + $this->assertCount(2, $this->history, |
| 136 | + 'contacts_job should page once and stop; more requests means the cursor stalled.'); |
| 137 | + |
| 138 | + // The cursor advanced past the cancelled page to the last contact on it. |
| 139 | + $job->read(); |
| 140 | + $this->assertSame('2025-07-17T15:39:56.2075043Z', $job->get('lastsourcetimemodified')); |
| 141 | + $this->assertEquals(2003, $job->get('lastsourceid')); |
| 142 | + |
| 143 | + // The second request used the advanced keyset cursor (incl. the ContactID tiebreaker). |
| 144 | + $secondfilter = urldecode((string) $this->history[1]['request']->getUri()); |
| 145 | + $this->assertStringContainsString("gt datetime('2025-07-17T15:39:56.2075043Z')", $secondfilter); |
| 146 | + $this->assertStringContainsString('Contact/ContactID gt 2003', $secondfilter); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * If the cursor genuinely cannot advance (e.g. contacts with no LastModifiedDateTime) while |
| 151 | + * the API still reports more pages, the safety net must stop paging instead of looping. |
| 152 | + */ |
| 153 | + public function test_paging_halts_when_cursor_cannot_advance(): void { |
| 154 | + $instance = $this->create_arlo_instance(); |
| 155 | + $job = $this->get_contacts_job($instance->id); |
| 156 | + |
| 157 | + // Queue the same non-advancing page twice: with the guard only ONE is ever requested. |
| 158 | + $this->mock_arlo_responses([ |
| 159 | + 'registrations_cancelled_no_modified_page.xml', |
| 160 | + 'registrations_cancelled_no_modified_page.xml', |
| 161 | + ]); |
| 162 | + |
| 163 | + $contactsjob = new contacts_job($job); |
| 164 | + $contactsjob->run(); |
| 165 | + |
| 166 | + // The guard stopped after a single request rather than re-fetching the identical page. |
| 167 | + $this->assertCount(1, $this->history, |
| 168 | + 'A non-advancing page with a next link must not be requested more than once.'); |
| 169 | + |
| 170 | + // Cursor untouched, and the no-progress reason was recorded. |
| 171 | + $job->read(); |
| 172 | + $this->assertSame('1970-01-01T00:00:00Z', $job->get('lastsourcetimemodified')); |
| 173 | + $this->assertEquals(0, $job->get('lastsourceid')); |
| 174 | + $this->assertTrue($contactsjob->has_errors()); |
| 175 | + $this->assertContains(get_string('pagingnoprogress', 'enrol_arlo'), $contactsjob->get_errors()); |
| 176 | + } |
| 177 | +} |
0 commit comments