From cfd58aae3efd31adccbb9b6fc67701d02db1dd25 Mon Sep 17 00:00:00 2001 From: Ferran Recio Date: Thu, 14 Jan 2021 16:03:33 +0100 Subject: [PATCH] MDL-68721 mod_h5pactivity: report acceptance tests --- .../tests/behat/display_result_types.feature | 262 ++++++++++++ mod/h5pactivity/tests/coverage.php | 43 ++ .../behat_mod_h5pactivity_generator.php | 52 +++ mod/h5pactivity/tests/generator/lib.php | 394 ++++++++++++++++++ mod/h5pactivity/tests/generator_test.php | 256 ++++++++++++ 5 files changed, 1007 insertions(+) create mode 100644 mod/h5pactivity/tests/behat/display_result_types.feature create mode 100644 mod/h5pactivity/tests/coverage.php create mode 100644 mod/h5pactivity/tests/generator/behat_mod_h5pactivity_generator.php diff --git a/mod/h5pactivity/tests/behat/display_result_types.feature b/mod/h5pactivity/tests/behat/display_result_types.feature new file mode 100644 index 00000000000..e59c72207dd --- /dev/null +++ b/mod/h5pactivity/tests/behat/display_result_types.feature @@ -0,0 +1,262 @@ +@mod @mod_h5pactivity @core_h5p @core_xapi +Feature: Report different types of interactions. + In order to let users to review attempts + As a user + I need to view all valid interactions in the report + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | student1 | Student | 1 | student1@example.com | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "course enrolments" exist: + | user | course | role | + | student1 | C1 | student | + # This test is only about reporting, we don't need to specify any valid H5P file for it. + And the following "activities" exist: + | activity | name | intro | course | idnumber | + | h5pactivity | H5P package | Test H5P description | C1 | h5ppackage | + + Scenario: General success attempt information + Given the following "mod_h5pactivity > attempts" exist: + | user | h5pactivity | attempt | interactiontype | rawscore | maxscore | duration | completion | success | + | student1 | H5P package | 1 | compound | 2 | 2 | 4 | 1 | 1 | + When I log in as "student1" + And I am on "Course 1" course homepage + And I follow "H5P package" + And I follow "View my attempts" + And I follow "View report" + Then I should see "2 out of 2" + And I should see "Pass" + And I should see "4 seconds" + And I should see "This attempt is completed" + + Scenario: General failed attempt statement + Given the following "mod_h5pactivity > attempts" exist: + | user | h5pactivity | attempt | interactiontype | rawscore | maxscore | duration | completion | success | + | student1 | H5P package | 1 | compound | 0 | 2 | 4 | 1 | 0 | + When I log in as "student1" + And I am on "Course 1" course homepage + And I follow "H5P package" + And I follow "View my attempts" + And I follow "View report" + Then I should see "0 out of 2" + And I should see "Fail" + And I should see "4 seconds" + And I should see "This attempt is completed" + + Scenario: View a success choice statement + Given the following "mod_h5pactivity > attempts" exist: + | user | h5pactivity | attempt | interactiontype | rawscore | maxscore | duration | completion | success | + | student1 | H5P package | 1 | choice | 2 | 2 | 1 | 1 | 1 | + | student1 | H5P package | 1 | compound | 2 | 2 | 4 | 1 | 1 | + When I log in as "student1" + And I am on "Course 1" course homepage + And I follow "H5P package" + And I follow "View my attempts" + And I follow "View report" + Then I should see "Select the correct answers" + And "Correct answer" "icon" should exist in the "This is also a correct answer" "table_row" + And I should see "Score: 2 out of 2" + + Scenario: View a failed choice statement + Given the following "mod_h5pactivity > attempts" exist: + | user | h5pactivity | attempt | interactiontype | rawscore | maxscore | duration | completion | success | + | student1 | H5P package | 1 | choice | 0 | 2 | 1 | 1 | 0 | + | student1 | H5P package | 1 | compound | 0 | 2 | 4 | 1 | 0 | + When I log in as "student1" + And I am on "Course 1" course homepage + And I follow "H5P package" + And I follow "View my attempts" + And I follow "View report" + Then I should see "Select the correct answers" + And "Incorrect answer" "icon" should exist in the "Another wrong answer" "table_row" + And I should see "Score: 0 out of 2" + + Scenario: View a success matching statement + Given the following "mod_h5pactivity > attempts" exist: + | user | h5pactivity | attempt | interactiontype | rawscore | maxscore | duration | completion | success | + | student1 | H5P package | 1 | matching | 2 | 2 | 1 | 1 | 1 | + | student1 | H5P package | 1 | compound | 2 | 2 | 4 | 1 | 1 | + When I log in as "student1" + And I am on "Course 1" course homepage + And I follow "H5P package" + And I follow "View my attempts" + And I follow "View report" + Then I should see "Drag and Drop example 1" + And "Your answer is correct" "icon" should exist in the "Drop item A" "table_row" + And I should see "Score: 2 out of 2" + + Scenario: View a failed matching statement + Given the following "mod_h5pactivity > attempts" exist: + | user | h5pactivity | attempt | interactiontype | rawscore | maxscore | duration | completion | success | + | student1 | H5P package | 1 | matching | 0 | 2 | 1 | 1 | 1 | + | student1 | H5P package | 1 | compound | 0 | 2 | 4 | 1 | 1 | + When I log in as "student1" + And I am on "Course 1" course homepage + And I follow "H5P package" + And I follow "View my attempts" + And I follow "View report" + Then I should see "Drag and Drop example 1" + And "Your answer is incorrect" "icon" should exist in the "Drop item A" "table_row" + And I should see "Score: 0 out of 2" + + Scenario: View a success true-false statement + Given the following "mod_h5pactivity > attempts" exist: + | user | h5pactivity | attempt | interactiontype | rawscore | maxscore | duration | completion | success | + | student1 | H5P package | 1 | true-false | 2 | 2 | 1 | 1 | 1 | + | student1 | H5P package | 1 | compound | 2 | 2 | 4 | 1 | 1 | + When I log in as "student1" + And I am on "Course 1" course homepage + And I follow "H5P package" + And I follow "View my attempts" + And I follow "View report" + Then I should see "The correct answer is true" + And "Correct answer" "icon" should exist in the "True" "table_row" + And I should see "Score: 2 out of 2" + + Scenario: View a failed true-false statement + Given the following "mod_h5pactivity > attempts" exist: + | user | h5pactivity | attempt | interactiontype | rawscore | maxscore | duration | completion | success | + | student1 | H5P package | 1 | true-false | 0 | 2 | 1 | 1 | 0 | + | student1 | H5P package | 1 | compound | 0 | 2 | 4 | 1 | 0 | + When I log in as "student1" + And I am on "Course 1" course homepage + And I follow "H5P package" + And I follow "View my attempts" + And I follow "View report" + Then I should see "The correct answer is true" + And "Incorrect answer" "icon" should exist in the "False" "table_row" + And I should see "Score: 0 out of 2" + + Scenario: View a success fill-in statement + Given the following "mod_h5pactivity > attempts" exist: + | user | h5pactivity | attempt | interactiontype | rawscore | maxscore | duration | completion | success | + | student1 | H5P package | 1 | fill-in | 2 | 2 | 1 | 1 | 1 | + | student1 | H5P package | 1 | compound | 2 | 2 | 4 | 1 | 1 | + When I log in as "student1" + And I am on "Course 1" course homepage + And I follow "H5P package" + And I follow "View my attempts" + And I follow "View report" + Then I should see "This an example of missing word text" + And "Your answer is correct" "icon" should exist in the "Gap #1" "table_row" + And I should see "first" in the "Gap #1" "table_row" + And "Your answer is correct" "icon" should exist in the "Gap #2" "table_row" + And I should see "second" in the "Gap #2" "table_row" + And I should see "Score: 2 out of 2" + + Scenario: View a failed fill-in statement + Given the following "mod_h5pactivity > attempts" exist: + | user | h5pactivity | attempt | interactiontype | rawscore | maxscore | duration | completion | success | + | student1 | H5P package | 1 | fill-in | 0 | 2 | 1 | 1 | 0 | + | student1 | H5P package | 1 | compound | 0 | 2 | 4 | 1 | 0 | + When I log in as "student1" + And I am on "Course 1" course homepage + And I follow "H5P package" + And I follow "View my attempts" + And I follow "View report" + Then I should see "This an example of missing word text" + And "Your answer is incorrect" "icon" should exist in the "Gap #1" "table_row" + And I should see "first" in the "Gap #1" "table_row" + And I should see "something" in the "Gap #1" "table_row" + And "Your answer is incorrect" "icon" should exist in the "Gap #2" "table_row" + And I should see "second" in the "Gap #2" "table_row" + And I should see "else" in the "Gap #2" "table_row" + And I should see "Score: 0 out of 2" + + Scenario: View a success long-fill-in statement + Given the following "mod_h5pactivity > attempts" exist: + | user | h5pactivity | attempt | interactiontype | rawscore | maxscore | duration | completion | success | + | student1 | H5P package | 1 | long-fill-in | 2 | 2 | 1 | 1 | 1 | + | student1 | H5P package | 1 | compound | 2 | 2 | 4 | 1 | 1 | + When I log in as "student1" + And I am on "Course 1" course homepage + And I follow "H5P package" + And I follow "View my attempts" + And I follow "View report" + Then I should see "Please describe the novel The Hobbit" + And I should see "The Hobbit is book" + # Fill-in does not have a partial scope indicador, we only check the general one. + And I should see "2 out of 2" + + Scenario: View a failed long-fill-in statement + Given the following "mod_h5pactivity > attempts" exist: + | user | h5pactivity | attempt | interactiontype | rawscore | maxscore | duration | completion | success | + | student1 | H5P package | 1 | long-fill-in | 0 | 2 | 1 | 1 | 0 | + | student1 | H5P package | 1 | compound | 0 | 2 | 4 | 1 | 0 | + When I log in as "student1" + And I am on "Course 1" course homepage + And I follow "H5P package" + And I follow "View my attempts" + And I follow "View report" + Then I should see "Please describe the novel The Hobbit" + And I should see "Who cares?" + # Fill-in does not have a partial scope indicador, we only check the general one. + And I should see "0 out of 2" + + # The current H5P implementation does not send a complete sequencing interaction statement + # we check only the warning and the final result. + Scenario: View a success sequencing statement + Given the following "mod_h5pactivity > attempts" exist: + | user | h5pactivity | attempt | interactiontype | rawscore | maxscore | duration | completion | success | + | student1 | H5P package | 1 | sequencing | 2 | 2 | 1 | 1 | 1 | + | student1 | H5P package | 1 | compound | 2 | 2 | 4 | 1 | 1 | + When I log in as "student1" + And I am on "Course 1" course homepage + And I follow "H5P package" + And I follow "View my attempts" + And I follow "View report" + Then I should see "This interaction (sequencing) does not provide tracking information" + # Sequencing does not have a partial scope indicador, we only check the general one. + And I should see "2 out of 2" + + # The current H5P implementation does not send a complete sequencing interaction statement + # we check only the warning and the final result. + Scenario: View a failed sequencing statement + Given the following "mod_h5pactivity > attempts" exist: + | user | h5pactivity | attempt | interactiontype | rawscore | maxscore | duration | completion | success | + | student1 | H5P package | 1 | sequencing | 2 | 2 | 1 | 1 | 0 | + | student1 | H5P package | 1 | compound | 0 | 2 | 4 | 1 | 0 | + When I log in as "student1" + And I am on "Course 1" course homepage + And I follow "H5P package" + And I follow "View my attempts" + And I follow "View report" + Then I should see "This interaction (sequencing) does not provide tracking information" + # Sequencing does not have a partial scope indicador, we only check the general one. + And I should see "0 out of 2" + + # The current H5P implementation does not send a complete sequencing interaction statement + # we check only the warning and the final result. + Scenario: View a success other statement + Given the following "mod_h5pactivity > attempts" exist: + | user | h5pactivity | attempt | interactiontype | rawscore | maxscore | duration | completion | success | + | student1 | H5P package | 1 | other | 2 | 2 | 1 | 1 | 1 | + | student1 | H5P package | 1 | compound | 2 | 2 | 4 | 1 | 1 | + When I log in as "student1" + And I am on "Course 1" course homepage + And I follow "H5P package" + And I follow "View my attempts" + And I follow "View report" + Then I should see "This interaction (other) does not provide tracking information" + # Other does not have a partial scope indicador, we only check the general one. + And I should see "2 out of 2" + + # The current H5P implementation does not send a complete sequencing interaction statement + # we check only the warning and the final result. + Scenario: View a failed other statement + Given the following "mod_h5pactivity > attempts" exist: + | user | h5pactivity | attempt | interactiontype | rawscore | maxscore | duration | completion | success | + | student1 | H5P package | 1 | other | 2 | 2 | 1 | 1 | 0 | + | student1 | H5P package | 1 | compound | 0 | 2 | 4 | 1 | 0 | + When I log in as "student1" + And I am on "Course 1" course homepage + And I follow "H5P package" + And I follow "View my attempts" + And I follow "View report" + Then I should see "This interaction (other) does not provide tracking information" + # Other does not have a partial scope indicador, we only check the general one. + And I should see "0 out of 2" diff --git a/mod/h5pactivity/tests/coverage.php b/mod/h5pactivity/tests/coverage.php new file mode 100644 index 00000000000..935436b1233 --- /dev/null +++ b/mod/h5pactivity/tests/coverage.php @@ -0,0 +1,43 @@ +. + +/** + * Coverage information for the mod_h5pactivity. + * + * @package mod_h5pactivity + * @category test + * @copyright 2021 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +return new class extends phpunit_coverage_info { + /** @var array The list of folders relative to the plugin root to whitelist in coverage generation. */ + protected $whitelistfolders = [ + 'classes', + 'tests/generator', + ]; + + /** @var array The list of files relative to the plugin root to whitelist in coverage generation. */ + protected $whitelistfiles = []; + + /** @var array The list of folders relative to the plugin root to excludelist in coverage generation. */ + protected $excludelistfolders = []; + + /** @var array The list of files relative to the plugin root to excludelist in coverage generation. */ + protected $excludelistfiles = []; +}; diff --git a/mod/h5pactivity/tests/generator/behat_mod_h5pactivity_generator.php b/mod/h5pactivity/tests/generator/behat_mod_h5pactivity_generator.php new file mode 100644 index 00000000000..25dd32cfce2 --- /dev/null +++ b/mod/h5pactivity/tests/generator/behat_mod_h5pactivity_generator.php @@ -0,0 +1,52 @@ +. + +/** + * H5P-related steps definitions. + * + * @package mod_h5pactivity + * @category test + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_mod_h5pactivity_generator extends behat_generator_base { + + protected function get_creatable_entities(): array { + return [ + 'attempts' => [ + 'singular' => 'attempt', + 'datagenerator' => 'attempt', + 'required' => ['h5pactivity', 'user'], + 'switchids' => ['h5pactivity' => 'h5pactivityid', 'user' => 'userid'], + ], + ]; + } + + /** + * Look up the id of a h5p from its name. + * + * @param string $h5pname the activity name, for example 'Test h5p'. + * @return int corresponding id + */ + protected function get_h5pactivity_id(string $h5pname): int { + global $DB; + + if (!$id = $DB->get_field('h5pactivity', 'id', ['name' => $h5pname])) { + throw new Exception('There is no h5p activity with name "' . $h5pname); + } + return $id; + } +} diff --git a/mod/h5pactivity/tests/generator/lib.php b/mod/h5pactivity/tests/generator/lib.php index c5516326bbb..be255f42832 100644 --- a/mod/h5pactivity/tests/generator/lib.php +++ b/mod/h5pactivity/tests/generator/lib.php @@ -180,4 +180,398 @@ class mod_h5pactivity_generator extends testing_module_generator { return $attempt; } + + /** + * Create a H5P attempt. + * + * This method is user by behat generator. + * + * @param array $data the attempts data array + */ + public function create_attempt(array $data): void { + + if (!isset($data['h5pactivityid'])) { + throw new coding_exception('Must specify h5pactivityid when creating a H5P attempt.'); + } + + if (!isset($data['userid'])) { + throw new coding_exception('Must specify userid when creating a H5P attempt.'); + } + + // Defaults. + $data['attempt'] = $data['attempt'] ?? 1; + $data['rawscore'] = $data['rawscore'] ?? 0; + $data['maxscore'] = $data['maxscore'] ?? 0; + $data['duration'] = $data['duration'] ?? 0; + $data['completion'] = $data['completion'] ?? 1; + $data['success'] = $data['success'] ?? 0; + + $data['attemptid'] = $this->get_attempt_object($data); + + // Check interaction type and create a valid record for it. + $data['interactiontype'] = $data['interactiontype'] ?? 'compound'; + $method = 'get_attempt_result_' . str_replace('-', '', $data['interactiontype']); + if (!method_exists($this, $method)) { + throw new Exception("Cannot create a {$data['interactiontype']} interaction statement"); + } + + $this->insert_statement($data, $this->$method($data)); + } + + /** + * Get or create an H5P attempt using the data array. + * + * @param array $attemptinfo the generator provided data + * @return int the attempt id + */ + private function get_attempt_object($attemptinfo): int { + global $DB; + $result = $DB->get_record('h5pactivity_attempts', [ + 'userid' => $attemptinfo['userid'], + 'h5pactivityid' => $attemptinfo['h5pactivityid'], + 'attempt' => $attemptinfo['attempt'], + ]); + if ($result) { + return $result->id; + } + return $this->new_user_attempt($attemptinfo); + } + + /** + * Creates a user attempt. + * + * @param array $attemptinfo the current attempt information. + * @return int the h5pactivity_attempt ID + */ + private function new_user_attempt(array $attemptinfo): int { + global $DB; + $record = (object)[ + 'h5pactivityid' => $attemptinfo['h5pactivityid'], + 'userid' => $attemptinfo['userid'], + 'timecreated' => time(), + 'timemodified' => time(), + 'attempt' => $attemptinfo['attempt'], + 'rawscore' => $attemptinfo['rawscore'], + 'maxscore' => $attemptinfo['maxscore'], + 'duration' => $attemptinfo['duration'], + 'completion' => $attemptinfo['completion'], + 'success' => $attemptinfo['success'], + ]; + if (empty($record->maxscore)) { + $record->scaled = 0; + } else { + $record->scaled = $record->rawscore / $record->maxscore; + } + return $DB->insert_record('h5pactivity_attempts', $record); + } + + /** + * Insert a new statement into an attempt. + * + * If the interaction type is "compound" it will also update the attempt general result. + * + * @param array $attemptinfo the current attempt information + * @param array $statement the statement tracking information + * @return int the h5pactivity_attempt_result ID + */ + private function insert_statement(array $attemptinfo, array $statement): int { + global $DB; + $record = $statement + [ + 'attemptid' => $attemptinfo['attemptid'], + 'interactiontype' => $attemptinfo['interactiontype'] ?? 'compound', + 'timecreated' => time(), + 'rawscore' => $attemptinfo['rawscore'], + 'maxscore' => $attemptinfo['maxscore'], + 'duration' => $attemptinfo['duration'], + 'completion' => $attemptinfo['completion'], + 'success' => $attemptinfo['success'], + ]; + $result = $DB->insert_record('h5pactivity_attempts_results', $record); + if ($record['interactiontype'] == 'compound') { + $attempt = (object)[ + 'id' => $attemptinfo['attemptid'], + 'rawscore' => $record['rawscore'], + 'maxscore' => $record['maxscore'], + 'duration' => $record['duration'], + 'completion' => $record['completion'], + 'success' => $record['success'], + ]; + $DB->update_record('h5pactivity_attempts', $attempt); + } + return $result; + } + + /** + * Generates a valid compound tracking result. + * + * @param array $attemptinfo the current attempt information. + * @return array with the required statement data + */ + private function get_attempt_result_compound(array $attemptinfo): array { + $additionals = (object)[ + "extensions" => (object)[ + "http://h5p.org/x-api/h5p-local-content-id" => 1, + ], + "contextExtensions" => (object)[], + ]; + + return [ + 'subcontent' => '', + 'description' => '', + 'correctpattern' => '', + 'response' => '', + 'additionals' => json_encode($additionals), + ]; + } + + /** + * Generates a valid choice tracking result. + * + * @param array $attemptinfo the current attempt information. + * @return array with the required statement data + */ + private function get_attempt_result_choice(array $attemptinfo): array { + + $response = ($attemptinfo['rawscore']) ? '1[,]0' : '2[,]3'; + + $additionals = (object)[ + "choices" => [ + (object)[ + "id" => "3", + "description" => (object)[ + "en-US" => "Another wrong answer\n", + ], + ], + (object)[ + "id" => "2", + "description" => (object)[ + "en-US" => "Wrong answer\n", + ], + ], + (object)[ + "id" => "1", + "description" => (object)[ + "en-US" => "This is also a correct answer\n", + ], + ], + (object)[ + "id" => "0", + "description" => (object)[ + "en-US" => "This is a correct answer\n", + ], + ], + ], + "extensions" => (object)[ + "http://h5p.org/x-api/h5p-local-content-id" => 1, + "http://h5p.org/x-api/h5p-subContentId" => "4367a919-ec47-43c9-b521-c22d9c0c0d8d", + ], + "contextExtensions" => (object)[], + ]; + + return [ + 'subcontent' => microtime(), + 'description' => 'Select the correct answers', + 'correctpattern' => '["1[,]0"]', + 'response' => $response, + 'additionals' => json_encode($additionals), + ]; + } + + /** + * Generates a valid matching tracking result. + * + * @param array $attemptinfo the current attempt information. + * @return array with the required statement data + */ + private function get_attempt_result_matching(array $attemptinfo): array { + + $response = ($attemptinfo['rawscore']) ? '0[.]0[,]1[.]1' : '1[.]0[,]0[.]1'; + + $additionals = (object)[ + "source" => [ + (object)[ + "id" => "0", + "description" => (object)[ + "en-US" => "Drop item A\n", + ], + ], + (object)[ + "id" => "1", + "description" => (object)[ + "en-US" => "Drop item B\n", + ], + ], + ], + "target" => [ + (object)[ + "id" => "0", + "description" => (object)[ + "en-US" => "Drop zone A\n", + ], + ], + (object)[ + "id" => "1", + "description" => (object)[ + "en-US" => "Drop zone B\n", + ], + ], + ], + "extensions" => [ + "http://h5p.org/x-api/h5p-local-content-id" => 1, + "http://h5p.org/x-api/h5p-subContentId" => "682f1c74-c819-4e9d-8c36-12d9dc5fcdbc", + ], + "contextExtensions" => (object)[], + ]; + + return [ + 'subcontent' => microtime(), + 'description' => 'Drag and Drop example 1', + 'correctpattern' => '["0[.]0[,]1[.]1"]', + 'response' => $response, + 'additionals' => json_encode($additionals), + ]; + } + + /** + * Generates a valid fill-in tracking result. + * + * @param array $attemptinfo the current attempt information. + * @return array with the required statement data + */ + private function get_attempt_result_fillin(array $attemptinfo): array { + + $response = ($attemptinfo['rawscore']) ? 'first[,]second' : 'something[,]else'; + + $additionals = (object)[ + "extensions" => (object)[ + "http://h5p.org/x-api/h5p-local-content-id" => 1, + "http://h5p.org/x-api/h5p-subContentId" => "1a3febd5-7edc-4336-8112-12756b945b62", + "https://h5p.org/x-api/case-sensitivity" => true, + "https://h5p.org/x-api/alternatives" => [ + ["first"], + ["second"], + ], + ], + "contextExtensions" => (object)[ + "https://h5p.org/x-api/h5p-reporting-version" => "1.1.0", + ], + ]; + + return [ + 'subcontent' => microtime(), + 'description' => '

This an example of missing word text.

+ +

The first answer if "first": the first answer is __________.

+ +

The second is second is "second": the secons answer is __________

', + 'correctpattern' => '["{case_matters=true}first[,]second"]', + 'response' => $response, + 'additionals' => json_encode($additionals), + ]; + } + + /** + * Generates a valid true-false tracking result. + * + * @param array $attemptinfo the current attempt information. + * @return array with the required statement data + */ + private function get_attempt_result_truefalse(array $attemptinfo): array { + + $response = ($attemptinfo['rawscore']) ? 'true' : 'false'; + + $additionals = (object)[ + "extensions" => (object)[ + "http://h5p.org/x-api/h5p-local-content-id" => 1, + "http://h5p.org/x-api/h5p-subContentId" => "5de9fb1e-aa03-4c9a-8cf0-3870b3f012ca", + ], + "contextExtensions" => (object)[], + ]; + + return [ + 'subcontent' => microtime(), + 'description' => 'The correct answer is true.', + 'correctpattern' => '["true"]', + 'response' => $response, + 'additionals' => json_encode($additionals), + ]; + } + + /** + * Generates a valid long-fill-in tracking result. + * + * @param array $attemptinfo the current attempt information. + * @return array with the required statement data + */ + private function get_attempt_result_longfillin(array $attemptinfo): array { + + $response = ($attemptinfo['rawscore']) ? 'The Hobbit is book' : 'Who cares?'; + + $additionals = (object)[ + "extensions" => (object)[ + "http://h5p.org/x-api/h5p-local-content-id" => 1, + "http://h5p.org/x-api/h5p-subContentId" => "5de9fb1e-aa03-4c9a-8cf0-3870b3f012ca", + ], + "contextExtensions" => (object)[], + ]; + + return [ + 'subcontent' => microtime(), + 'description' => '

Please describe the novel The Hobbit', + 'correctpattern' => '', + 'response' => $response, + 'additionals' => json_encode($additionals), + ]; + } + + /** + * Generates a valid sequencing tracking result. + * + * @param array $attemptinfo the current attempt information. + * @return array with the required statement data + */ + private function get_attempt_result_sequencing(array $attemptinfo): array { + + $response = ($attemptinfo['rawscore']) ? 'true' : 'false'; + + $additionals = (object)[ + "extensions" => (object)[ + "http://h5p.org/x-api/h5p-local-content-id" => 1, + "http://h5p.org/x-api/h5p-subContentId" => "5de9fb1e-aa03-4c9a-8cf0-3870b3f012ca", + ], + "contextExtensions" => (object)[], + ]; + + return [ + 'subcontent' => microtime(), + 'description' => 'The correct answer is true.', + 'correctpattern' => '["{case_matters=true}first[,]second"]', + 'response' => $response, + 'additionals' => json_encode($additionals), + ]; + } + + /** + * Generates a valid other tracking result. + * + * @param array $attemptinfo the current attempt information. + * @return array with the required statement data + */ + private function get_attempt_result_other(array $attemptinfo): array { + + $additionals = (object)[ + "extensions" => (object)[ + "http://h5p.org/x-api/h5p-local-content-id" => 1, + ], + "contextExtensions" => (object)[], + ]; + + return [ + 'subcontent' => microtime(), + 'description' => '', + 'correctpattern' => '', + 'response' => '', + 'additionals' => json_encode($additionals), + ]; + } } diff --git a/mod/h5pactivity/tests/generator_test.php b/mod/h5pactivity/tests/generator_test.php index 0fd5cab3e5e..2c3f1971925 100644 --- a/mod/h5pactivity/tests/generator_test.php +++ b/mod/h5pactivity/tests/generator_test.php @@ -109,4 +109,260 @@ class mod_h5pactivity_generator_testcase extends advanced_testcase { $this->expectException(coding_exception::class); $activity = $this->getDataGenerator()->create_module('h5pactivity', $params); } + + /** + * Test to create H5P attempts + * + * @dataProvider create_attempt_data + * + * @param array $tracks the attempt tracks objects + * @param int $attempts the final registered attempts + * @param int $results the final registered attempts results + * @param bool $exception if an exception is expected + * + */ + public function test_create_attempt(array $tracks, int $attempts, int $results, bool $exception) { + global $DB; + $this->resetAfterTest(); + $this->setAdminUser(); + + $course = $this->getDataGenerator()->create_course(); + $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]); + $user = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + $this->assertEquals(0, $DB->count_records('h5pactivity_attempts')); + $this->assertEquals(0, $DB->count_records('h5pactivity_attempts_results')); + + if ($exception) { + $this->expectException(Exception::class); + } + + foreach ($tracks as $track) { + $attemptinfo = [ + 'userid' => $user->id, + 'h5pactivityid' => $activity->id, + 'attempt' => $track['attempt'], + 'interactiontype' => $track['interactiontype'], + 'rawscore' => $track['rawscore'], + 'maxscore' => $track['maxscore'], + 'duration' => $track['duration'], + 'completion' => $track['completion'], + 'success' => $track['success'], + ]; + + $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity'); + $generator->create_attempt($attemptinfo); + + $this->assert_attempt_matches_info($attemptinfo); + } + + $this->assertEquals($attempts, $DB->count_records('h5pactivity_attempts')); + $this->assertEquals($results, $DB->count_records('h5pactivity_attempts_results')); + } + + /** + * Data provider for create attempt test. + * + * @return array + */ + public function create_attempt_data(): array { + return [ + 'Compound statement' => [ + [ + [ + 'interactiontype' => 'compound', 'attempt' => 1, 'rawscore' => 2, + 'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0 + ], + ], 1, 1, false, + ], + 'Choice statement' => [ + [ + [ + 'interactiontype' => 'choice', 'attempt' => 1, 'rawscore' => 2, + 'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0 + ], + ], 1, 1, false, + ], + 'Matching statement' => [ + [ + [ + 'interactiontype' => 'matching', 'attempt' => 1, 'rawscore' => 2, + 'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0 + ], + ], 1, 1, false, + ], + 'Fill-in statement' => [ + [ + [ + 'interactiontype' => 'fill-in', 'attempt' => 1, 'rawscore' => 2, + 'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0 + ], + ], 1, 1, false, + ], + 'True-false statement' => [ + [ + [ + 'interactiontype' => 'true-false', 'attempt' => 1, 'rawscore' => 2, + 'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0 + ], + ], 1, 1, false, + ], + 'Long-fill-in statement' => [ + [ + [ + 'interactiontype' => 'long-fill-in', 'attempt' => 1, 'rawscore' => 2, + 'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0 + ], + ], 1, 1, false, + ], + 'Sequencing statement' => [ + [ + [ + 'interactiontype' => 'sequencing', 'attempt' => 1, 'rawscore' => 2, + 'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0 + ], + ], 1, 1, false, + ], + 'Other statement' => [ + [ + [ + 'interactiontype' => 'other', 'attempt' => 1, 'rawscore' => 2, + 'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0 + ], + ], 1, 1, false, + ], + 'Other statement' => [ + [ + [ + 'interactiontype' => 'other', 'attempt' => 1, 'rawscore' => 2, + 'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0 + ], + ], 1, 1, false, + ], + 'No graded statement' => [ + [ + [ + 'interactiontype' => 'other', 'attempt' => 1, 'rawscore' => 0, + 'maxscore' => 0, 'duration' => 1, 'completion' => 1, 'success' => 0 + ], + ], 1, 1, false, + ], + 'Invalid statement type' => [ + [ + [ + 'interactiontype' => 'no-valid-statement-type', 'attempt' => 1, 'rawscore' => 2, + 'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0 + ], + ], 0, 0, true, + ], + 'Adding a second statement to attempt' => [ + [ + [ + 'interactiontype' => 'true-false', 'attempt' => 1, 'rawscore' => 2, + 'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0 + ], + [ + 'interactiontype' => 'compound', 'attempt' => 1, 'rawscore' => 3, + 'maxscore' => 3, 'duration' => 2, 'completion' => 1, 'success' => 0 + ], + ], 1, 2, false, + ], + 'Creating two attempts' => [ + [ + [ + 'interactiontype' => 'compound', 'attempt' => 1, 'rawscore' => 2, + 'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0 + ], + [ + 'interactiontype' => 'compound', 'attempt' => 2, 'rawscore' => 3, + 'maxscore' => 3, 'duration' => 1, 'completion' => 1, 'success' => 0 + ], + ], 2, 2, false, + ], + ]; + } + + /** + * Insert track into attempt, creating the attempt if necessary. + * + * @param array $attemptinfo the attempt track information + */ + private function assert_attempt_matches_info($attemptinfo): void { + global $DB; + + $attempt = $DB->get_record('h5pactivity_attempts', [ + 'userid' => $attemptinfo['userid'], + 'h5pactivityid' => $attemptinfo['h5pactivityid'], + 'attempt' => $attemptinfo['attempt'], + ]); + $this->assertEquals($attemptinfo['rawscore'], $attempt->rawscore); + $this->assertEquals($attemptinfo['maxscore'], $attempt->maxscore); + $this->assertEquals($attemptinfo['duration'], $attempt->duration); + $this->assertEquals($attemptinfo['completion'], $attempt->completion); + $this->assertEquals($attemptinfo['success'], $attempt->success); + + $track = $DB->get_record('h5pactivity_attempts_results', [ + 'attemptid' => $attempt->id, + 'interactiontype' => $attemptinfo['interactiontype'], + ]); + $this->assertEquals($attemptinfo['rawscore'], $track->rawscore); + $this->assertEquals($attemptinfo['maxscore'], $track->maxscore); + $this->assertEquals($attemptinfo['duration'], $track->duration); + $this->assertEquals($attemptinfo['completion'], $track->completion); + $this->assertEquals($attemptinfo['success'], $track->success); + } + + /** + * Test exceptions when creating an invalid attempt. + * + * @dataProvider create_attempt_exceptions_data + * + * @param bool $validmod if the activity id is provided + * @param bool $validuser if the user id is provided + */ + public function test_create_attempt_exceptions(bool $validmod, bool $validuser) { + global $DB; + $this->resetAfterTest(); + $this->setAdminUser(); + + $course = $this->getDataGenerator()->create_course(); + $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]); + $user = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + $this->expectException(coding_exception::class); + + $attemptinfo = [ + 'attempt' => 1, + 'interactiontype' => 'compound', + 'rawscore' => 2, + 'maxscore' => 1, + 'duration' => 1, + 'completion' => 1, + 'success' => 0, + ]; + + if ($validmod) { + $attemptinfo['h5pactivityid'] = $activity->id; + } + + if ($validuser) { + $attemptinfo['userid'] = $user->id; + } + + $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity'); + $generator->create_attempt($attemptinfo); + } + + /** + * Data provider for data request creation tests. + * + * @return array + */ + public function create_attempt_exceptions_data(): array { + return [ + 'Invalid user' => [true, false], + 'Invalid activity' => [false, true], + 'Invalid user and activity' => [false, false], + ]; + } }