From 42c48b66600f5ca9c4c868b77a1d44c489575a45 Mon Sep 17 00:00:00 2001 From: Ferran Recio Date: Thu, 21 May 2020 18:06:29 +0200 Subject: [PATCH] MDL-68619 mod_h5pactivity: add activity check to get_attempt method --- mod/h5pactivity/classes/local/manager.php | 5 +- mod/h5pactivity/tests/local/manager_test.php | 64 ++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/mod/h5pactivity/classes/local/manager.php b/mod/h5pactivity/classes/local/manager.php index 4c471bf0da6..840de03f405 100644 --- a/mod/h5pactivity/classes/local/manager.php +++ b/mod/h5pactivity/classes/local/manager.php @@ -417,7 +417,10 @@ class manager { */ public function get_attempt(int $attemptid): ?attempt { global $DB; - $record = $DB->get_record('h5pactivity_attempts', ['id' => $attemptid]); + $record = $DB->get_record('h5pactivity_attempts', [ + 'id' => $attemptid, + 'h5pactivityid' => $this->instance->id, + ]); if (!$record) { return null; } diff --git a/mod/h5pactivity/tests/local/manager_test.php b/mod/h5pactivity/tests/local/manager_test.php index ff2e8b6149f..bbbf9eac79f 100644 --- a/mod/h5pactivity/tests/local/manager_test.php +++ b/mod/h5pactivity/tests/local/manager_test.php @@ -738,6 +738,70 @@ class manager_testcase extends \advanced_testcase { ]; } + /** + * Test get_attempt method. + * + * @dataProvider get_attempt_data + * @param string $attemptname the attempt to use + * @param string|null $result the expected attempt ID or null for none + */ + public function test_get_attempt(string $attemptname, ?string $result): void { + + $this->resetAfterTest(); + $this->setAdminUser(); + + $course = $this->getDataGenerator()->create_course(); + + $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]); + $cm = get_coursemodule_from_id('h5pactivity', $activity->cmid, 0, false, MUST_EXIST); + + $otheractivity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]); + $othercm = get_coursemodule_from_id('h5pactivity', $otheractivity->cmid, 0, false, MUST_EXIST); + + $manager = manager::create_from_instance($activity); + + $user = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + $attempts = ['inexistent' => 0]; + + $this->generate_fake_attempts($activity, $user, 1); + $attempt = attempt::last_attempt($user, $cm); + $attempts['current'] = $attempt->get_id(); + + $this->generate_fake_attempts($otheractivity, $user, 1); + $attempt = attempt::last_attempt($user, $othercm); + $attempts['other'] = $attempt->get_id(); + + $attempt = $manager->get_attempt($attempts[$attemptname]); + if ($result === null) { + $this->assertNull($attempt); + } else { + $this->assertEquals($attempts[$attemptname], $attempt->get_id()); + $this->assertEquals($activity->id, $attempt->get_h5pactivityid()); + $this->assertEquals($user->id, $attempt->get_userid()); + $this->assertEquals(4, $attempt->get_attempt()); + } + } + + /** + * Data provider for test_get_attempt. + * + * @return array + */ + public function get_attempt_data(): array { + return [ + 'Get the current activity attempt' => [ + 'current', 'current' + ], + 'Try to get another activity attempt' => [ + 'other', null + ], + 'Try to get an inexistent attempt' => [ + 'inexistent', null + ], + ]; + } + /** * Insert fake attempt data into h5pactiviyt_attempts. * -- 2.43.0