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_grades\external; 18 19 use core_external\external_api; 20 21 defined('MOODLE_INTERNAL') || die; 22 23 global $CFG; 24 25 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 26 27 /** 28 * Unit tests for core_grades\external\get_grade_tree. 29 * 30 * @package core_grades 31 * @category external 32 * @copyright 2023 Mihail Geshoski <mihail@moodle.com> 33 * @covers \core_grades\external\get_grade_tree 34 */ 35 class get_grade_tree_test extends \externallib_advanced_testcase { 36 37 /** 38 * Test the return value of the external function. 39 * 40 * @covers ::execute 41 * @return void 42 */ 43 public function test_execute(): void { 44 $this->resetAfterTest(); 45 $this->setAdminUser(); 46 $course = $this->getDataGenerator()->create_course(['fullname' => 'Course']); 47 $coursegradecategory = \grade_category::fetch_course_category($course->id); 48 // Create a grade item 'Grade item' and grade category 'Category 1' within the course grade category. 49 $gradeitem = $this->getDataGenerator()->create_grade_item( 50 ['courseid' => $course->id, 'itemname' => 'Grade item']); 51 $gradecategory1 = $this->getDataGenerator()->create_grade_category( 52 ['courseid' => $course->id, 'fullname' => 'Category 1']); 53 // Create a grade item 'Grade item 1' and grade category 'Category 2' within 'Category 1'. 54 $gradeitem1 = $this->getDataGenerator()->create_grade_item( 55 ['courseid' => $course->id, 'itemname' => 'Grade item 1', 'categoryid' => $gradecategory1->id]); 56 $gradecategory2 = $this->getDataGenerator()->create_grade_category( 57 ['courseid' => $course->id, 'fullname' => 'Category 2', 'parent' => $gradecategory1->id]); 58 // Create a grade item 'Grade item 2' and grade category 'Category 3' (with no children) within 'Category 2'. 59 $gradeitem2 = $this->getDataGenerator()->create_grade_item( 60 ['courseid' => $course->id, 'itemname' => 'Grade item 2', 'categoryid' => $gradecategory2->id]); 61 $gradecategory3 = $this->getDataGenerator()->create_grade_category( 62 ['courseid' => $course->id, 'fullname' => 'Category 3', 'parent' => $gradecategory2->id]); 63 64 $result = get_grade_tree::execute($course->id); 65 $result = external_api::clean_returnvalue(get_grade_tree::execute_returns(), $result); 66 67 $expected = json_encode([ 68 'id' => $coursegradecategory->id, 69 'name' => 'Course', 70 'iscategory' => true, 71 'haschildcategories' => true, 72 'children' => [ 73 [ 74 'id' => $gradeitem->id, 75 'name' => 'Grade item', 76 'iscategory' => false, 77 'children' => null 78 ], 79 [ 80 'id' => $gradecategory1->id, 81 'name' => 'Category 1', 82 'iscategory' => true, 83 'haschildcategories' => true, 84 'children' => [ 85 [ 86 'id' => $gradeitem1->id, 87 'name' => 'Grade item 1', 88 'iscategory' => false, 89 'children' => null 90 ], 91 [ 92 'id' => $gradecategory2->id, 93 'name' => 'Category 2', 94 'iscategory' => true, 95 'haschildcategories' => true, 96 'children' => [ 97 [ 98 'id' => $gradeitem2->id, 99 'name' => 'Grade item 2', 100 'iscategory' => false, 101 'children' => null 102 ], 103 [ 104 'id' => $gradecategory3->id, 105 'name' => 'Category 3', 106 'iscategory' => true, 107 'haschildcategories' => false, 108 'children' => null 109 ] 110 ] 111 ] 112 ] 113 ] 114 ] 115 ]); 116 117 $this->assertEquals($expected, $result); 118 } 119 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body