Differences Between: [Versions 310 and 311] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
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 mod_label; 18 19 use externallib_advanced_testcase; 20 use mod_label_external; 21 22 defined('MOODLE_INTERNAL') || die(); 23 24 global $CFG; 25 26 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 27 28 /** 29 * External mod_label functions unit tests 30 * 31 * @package mod_label 32 * @category external 33 * @copyright 2017 Juan Leyva <juan@moodle.com> 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 * @since Moodle 3.3 36 */ 37 class externallib_test extends externallib_advanced_testcase { 38 39 /** 40 * Test test_mod_label_get_labels_by_courses 41 */ 42 public function test_mod_label_get_labels_by_courses() { 43 global $DB; 44 45 $this->resetAfterTest(true); 46 47 $course1 = self::getDataGenerator()->create_course(); 48 $course2 = self::getDataGenerator()->create_course(); 49 50 $student = self::getDataGenerator()->create_user(); 51 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 52 $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id); 53 54 // First label. 55 $record = new \stdClass(); 56 $record->course = $course1->id; 57 $label1 = self::getDataGenerator()->create_module('label', $record); 58 59 // Second label. 60 $record = new \stdClass(); 61 $record->course = $course2->id; 62 $label2 = self::getDataGenerator()->create_module('label', $record); 63 64 // Execute real Moodle enrolment as we'll call unenrol() method on the instance later. 65 $enrol = enrol_get_plugin('manual'); 66 $enrolinstances = enrol_get_instances($course2->id, true); 67 foreach ($enrolinstances as $courseenrolinstance) { 68 if ($courseenrolinstance->enrol == "manual") { 69 $instance2 = $courseenrolinstance; 70 break; 71 } 72 } 73 $enrol->enrol_user($instance2, $student->id, $studentrole->id); 74 75 self::setUser($student); 76 77 $returndescription = mod_label_external::get_labels_by_courses_returns(); 78 79 // Create what we expect to be returned when querying the two courses. 80 $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'timemodified', 81 'section', 'visible', 'groupmode', 'groupingid'); 82 83 // Add expected coursemodule and data. 84 $label1->coursemodule = $label1->cmid; 85 $label1->introformat = 1; 86 $label1->section = 0; 87 $label1->visible = true; 88 $label1->groupmode = 0; 89 $label1->groupingid = 0; 90 $label1->introfiles = []; 91 92 $label2->coursemodule = $label2->cmid; 93 $label2->introformat = 1; 94 $label2->section = 0; 95 $label2->visible = true; 96 $label2->groupmode = 0; 97 $label2->groupingid = 0; 98 $label2->introfiles = []; 99 100 foreach ($expectedfields as $field) { 101 $expected1[$field] = $label1->{$field}; 102 $expected2[$field] = $label2->{$field}; 103 } 104 105 $expectedlabels = array($expected2, $expected1); 106 107 // Call the external function passing course ids. 108 $result = mod_label_external::get_labels_by_courses(array($course2->id, $course1->id)); 109 $result = \external_api::clean_returnvalue($returndescription, $result); 110 111 $this->assertEquals($expectedlabels, $result['labels']); 112 $this->assertCount(0, $result['warnings']); 113 114 // Call the external function without passing course id. 115 $result = mod_label_external::get_labels_by_courses(); 116 $result = \external_api::clean_returnvalue($returndescription, $result); 117 $this->assertEquals($expectedlabels, $result['labels']); 118 $this->assertCount(0, $result['warnings']); 119 120 // Add a file to the intro. 121 $filename = "file.txt"; 122 $filerecordinline = array( 123 'contextid' => \context_module::instance($label2->cmid)->id, 124 'component' => 'mod_label', 125 'filearea' => 'intro', 126 'itemid' => 0, 127 'filepath' => '/', 128 'filename' => $filename, 129 ); 130 $fs = get_file_storage(); 131 $timepost = time(); 132 $fs->create_file_from_string($filerecordinline, 'image contents (not really)'); 133 134 $result = mod_label_external::get_labels_by_courses(array($course2->id, $course1->id)); 135 $result = \external_api::clean_returnvalue($returndescription, $result); 136 137 $this->assertCount(1, $result['labels'][0]['introfiles']); 138 $this->assertEquals($filename, $result['labels'][0]['introfiles'][0]['filename']); 139 140 // Unenrol user from second course. 141 $enrol->unenrol_user($instance2, $student->id); 142 array_shift($expectedlabels); 143 144 // Call the external function without passing course id. 145 $result = mod_label_external::get_labels_by_courses(); 146 $result = \external_api::clean_returnvalue($returndescription, $result); 147 $this->assertEquals($expectedlabels, $result['labels']); 148 149 // Call for the second course we unenrolled the user from, expected warning. 150 $result = mod_label_external::get_labels_by_courses(array($course2->id)); 151 $this->assertCount(1, $result['warnings']); 152 $this->assertEquals('1', $result['warnings'][0]['warningcode']); 153 $this->assertEquals($course2->id, $result['warnings'][0]['itemid']); 154 } 155 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body