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 namespace core\moodlenet; 18 19 use core\moodlenet\share_recorder; 20 21 /** 22 * Test coverage for moodlenet share recorder. 23 * 24 * @package core 25 * @copyright 2023 David Woloszyn <david.woloszyn@moodle.com> 26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 * @coversDefaultClass \core\moodlenet\share_recorder 28 */ 29 class share_recorder_test extends \advanced_testcase { 30 31 /** 32 * Test inserting and updating an activity share progress to MoodleNet. 33 * 34 * @covers ::insert_share_progress 35 * @covers ::update_share_progress 36 */ 37 public function test_activity_share_progress(): void { 38 global $DB, $USER; 39 $this->resetAfterTest(); 40 41 $courseid = 10; 42 $cmid = 20; 43 $resourceurl = 'https://moodlenet.test/files/testresource.mbz'; 44 45 // Insert the activity share progress and test the returned id. 46 $shareid = share_recorder::insert_share_progress(share_recorder::TYPE_ACTIVITY, $USER->id, $courseid, $cmid); 47 $this->assertNotNull($shareid); 48 49 // Test we have set the fields correctly. 50 $record = $DB->get_record('moodlenet_share_progress', ['id' => $shareid]); 51 $this->assertEquals(share_recorder::TYPE_ACTIVITY, $record->type); 52 $this->assertEquals(share_recorder::STATUS_IN_PROGRESS, $record->status); 53 $this->assertEquals($courseid, $record->courseid); 54 $this->assertEquals($cmid, $record->cmid); 55 $this->assertTimeCurrent($record->timecreated); 56 $this->assertEquals($USER->id, $record->userid); 57 58 // Update the record with the returned data from MoodleNet. 59 share_recorder::update_share_progress($shareid, share_recorder::STATUS_SENT, $resourceurl); 60 61 // Test we have set the fields correctly. 62 $record = $DB->get_record('moodlenet_share_progress', ['id' => $shareid]); 63 $this->assertEquals($resourceurl, $record->resourceurl); 64 $this->assertEquals(share_recorder::STATUS_SENT, $record->status); 65 } 66 67 /** 68 * Test inserting and updating a course share progress to MoodleNet. 69 * We will also force an error status and test that too. 70 * 71 * @covers ::insert_share_progress 72 * @covers ::update_share_progress 73 */ 74 public function test_course_share_progress(): void { 75 global $DB, $USER; 76 $this->resetAfterTest(); 77 78 $courseid = 10; 79 80 // Insert the course share progress and test the returned id. 81 $shareid = share_recorder::insert_share_progress(share_recorder::TYPE_COURSE, $USER->id, $courseid); 82 $this->assertNotNull($shareid); 83 84 // Test we have set the fields correctly (we expect cmid to be null for course shares). 85 $record = $DB->get_record('moodlenet_share_progress', ['id' => $shareid]); 86 $this->assertEquals(share_recorder::TYPE_COURSE, $record->type); 87 $this->assertEquals(share_recorder::STATUS_IN_PROGRESS, $record->status); 88 $this->assertEquals($courseid, $record->courseid); 89 $this->assertNull($record->cmid); 90 $this->assertTimeCurrent($record->timecreated); 91 $this->assertEquals($USER->id, $record->userid); 92 93 // Update the record, but let's test with an error status. 94 share_recorder::update_share_progress($shareid, share_recorder::STATUS_ERROR); 95 96 // Test we have set the field correctly. 97 $record = $DB->get_record('moodlenet_share_progress', ['id' => $shareid]); 98 $this->assertEquals(share_recorder::STATUS_ERROR, $record->status); 99 } 100 101 /** 102 * Tests the share type is one of the allowed values. 103 * 104 * @covers ::get_allowed_share_types 105 */ 106 public function test_invalid_share_type(): void { 107 global $USER; 108 $this->resetAfterTest(); 109 110 $courseid = 10; 111 $invalidsharetype = 99; 112 113 $this->expectException(\moodle_exception::class); 114 share_recorder::insert_share_progress($invalidsharetype, $USER->id, $courseid); 115 } 116 117 /** 118 * Tests the share status is one of the allowed values. 119 * 120 * @covers ::get_allowed_share_statuses 121 */ 122 public function test_invalid_share_status(): void { 123 global $USER; 124 $this->resetAfterTest(); 125 126 $courseid = 10; 127 $invalidsharestatus = 66; 128 129 $recordid = share_recorder::insert_share_progress(share_recorder::TYPE_COURSE, $USER->id, $courseid); 130 131 $this->expectException(\moodle_exception::class); 132 share_recorder::update_share_progress($recordid, $invalidsharestatus); 133 } 134 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body