MDL-68619 mod_h5pactivity: add activity check to get_attempt method
authorFerran Recio <ferran@moodle.com>
Thu, 21 May 2020 16:06:29 +0000 (18:06 +0200)
committerFerran Recio <ferran@moodle.com>
Thu, 21 May 2020 16:37:54 +0000 (18:37 +0200)
mod/h5pactivity/classes/local/manager.php
mod/h5pactivity/tests/local/manager_test.php

index 4c471bf..840de03 100644 (file)
@@ -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;
         }
index ff2e8b6..bbbf9ea 100644 (file)
@@ -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.
      *