| 1 | efrain | 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 |  */
 | 
        
           | 1441 | ariadna | 35 | final class get_grade_tree_test extends \externallib_advanced_testcase {
 | 
        
           | 1 | efrain | 36 |   | 
        
           |  |  | 37 |     /**
 | 
        
           |  |  | 38 |      * Test the return value of the external function.
 | 
        
           |  |  | 39 |      *
 | 
        
           |  |  | 40 |      * @return void
 | 
        
           |  |  | 41 |      */
 | 
        
           |  |  | 42 |     public function test_execute(): void {
 | 
        
           |  |  | 43 |         $this->resetAfterTest();
 | 
        
           |  |  | 44 |         $this->setAdminUser();
 | 
        
           |  |  | 45 |         $course = $this->getDataGenerator()->create_course(['fullname' => 'Course']);
 | 
        
           |  |  | 46 |         $coursegradecategory = \grade_category::fetch_course_category($course->id);
 | 
        
           |  |  | 47 |         // Create a grade item 'Grade item' and grade category 'Category 1' within the course grade category.
 | 
        
           |  |  | 48 |         $gradeitem = $this->getDataGenerator()->create_grade_item(
 | 
        
           |  |  | 49 |             ['courseid' => $course->id, 'itemname' => 'Grade item']);
 | 
        
           |  |  | 50 |         $gradecategory1 = $this->getDataGenerator()->create_grade_category(
 | 
        
           |  |  | 51 |             ['courseid' => $course->id, 'fullname' => 'Category 1']);
 | 
        
           |  |  | 52 |         // Create a grade item 'Grade item 1' and grade category 'Category 2' within 'Category 1'.
 | 
        
           |  |  | 53 |         $gradeitem1 = $this->getDataGenerator()->create_grade_item(
 | 
        
           |  |  | 54 |             ['courseid' => $course->id, 'itemname' => 'Grade item 1', 'categoryid' => $gradecategory1->id]);
 | 
        
           |  |  | 55 |         $gradecategory2 = $this->getDataGenerator()->create_grade_category(
 | 
        
           |  |  | 56 |             ['courseid' => $course->id, 'fullname' => 'Category 2', 'parent' => $gradecategory1->id]);
 | 
        
           |  |  | 57 |         // Create a grade item 'Grade item 2' and grade category 'Category 3' (with no children) within 'Category 2'.
 | 
        
           |  |  | 58 |         $gradeitem2 = $this->getDataGenerator()->create_grade_item(
 | 
        
           |  |  | 59 |             ['courseid' => $course->id, 'itemname' => 'Grade item 2', 'categoryid' => $gradecategory2->id]);
 | 
        
           |  |  | 60 |         $gradecategory3 = $this->getDataGenerator()->create_grade_category(
 | 
        
           |  |  | 61 |             ['courseid' => $course->id, 'fullname' => 'Category 3', 'parent' => $gradecategory2->id]);
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 |         $result = get_grade_tree::execute($course->id);
 | 
        
           |  |  | 64 |         $result = external_api::clean_returnvalue(get_grade_tree::execute_returns(), $result);
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 |         $expected = json_encode([
 | 
        
           |  |  | 67 |             'id' => $coursegradecategory->id,
 | 
        
           |  |  | 68 |             'name' => 'Course',
 | 
        
           |  |  | 69 |             'iscategory' => true,
 | 
        
           |  |  | 70 |             'haschildcategories' => true,
 | 
        
           |  |  | 71 |             'children' => [
 | 
        
           |  |  | 72 |                 [
 | 
        
           |  |  | 73 |                     'id' => $gradeitem->id,
 | 
        
           |  |  | 74 |                     'name' => 'Grade item',
 | 
        
           |  |  | 75 |                     'iscategory' => false,
 | 
        
           |  |  | 76 |                     'children' => null
 | 
        
           |  |  | 77 |                 ],
 | 
        
           |  |  | 78 |                 [
 | 
        
           |  |  | 79 |                     'id' => $gradecategory1->id,
 | 
        
           |  |  | 80 |                     'name' => 'Category 1',
 | 
        
           |  |  | 81 |                     'iscategory' => true,
 | 
        
           |  |  | 82 |                     'haschildcategories' => true,
 | 
        
           |  |  | 83 |                     'children' => [
 | 
        
           |  |  | 84 |                         [
 | 
        
           |  |  | 85 |                             'id' => $gradeitem1->id,
 | 
        
           |  |  | 86 |                             'name' => 'Grade item 1',
 | 
        
           |  |  | 87 |                             'iscategory' => false,
 | 
        
           |  |  | 88 |                             'children' => null
 | 
        
           |  |  | 89 |                         ],
 | 
        
           |  |  | 90 |                         [
 | 
        
           |  |  | 91 |                             'id' => $gradecategory2->id,
 | 
        
           |  |  | 92 |                             'name' => 'Category 2',
 | 
        
           |  |  | 93 |                             'iscategory' => true,
 | 
        
           |  |  | 94 |                             'haschildcategories' => true,
 | 
        
           |  |  | 95 |                             'children' => [
 | 
        
           |  |  | 96 |                                 [
 | 
        
           |  |  | 97 |                                     'id' => $gradeitem2->id,
 | 
        
           |  |  | 98 |                                     'name' => 'Grade item 2',
 | 
        
           |  |  | 99 |                                     'iscategory' => false,
 | 
        
           |  |  | 100 |                                     'children' => null
 | 
        
           |  |  | 101 |                                 ],
 | 
        
           |  |  | 102 |                                 [
 | 
        
           |  |  | 103 |                                     'id' => $gradecategory3->id,
 | 
        
           |  |  | 104 |                                     'name' => 'Category 3',
 | 
        
           |  |  | 105 |                                     'iscategory' => true,
 | 
        
           |  |  | 106 |                                     'haschildcategories' => false,
 | 
        
           |  |  | 107 |                                     'children' => null
 | 
        
           |  |  | 108 |                                 ]
 | 
        
           |  |  | 109 |                             ]
 | 
        
           |  |  | 110 |                         ]
 | 
        
           |  |  | 111 |                     ]
 | 
        
           |  |  | 112 |                 ]
 | 
        
           |  |  | 113 |             ]
 | 
        
           |  |  | 114 |         ]);
 | 
        
           |  |  | 115 |   | 
        
           |  |  | 116 |         $this->assertEquals($expected, $result);
 | 
        
           |  |  | 117 |     }
 | 
        
           |  |  | 118 | }
 |