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/**
18 * Unit tests for grade/lib.php.
19 *
20 * @package   core_grades
21 * @category  test
22 * @copyright 2016 Jun Pataleta
23 * @license   http://www.gnu.org/copyleft/gpl.html GNU Public License
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->dirroot . '/grade/lib.php');
30
31/**
32 * Unit tests for grade/lib.php.
33 *
34 * @package   core_grades
35 * @category  test
36 * @copyright 2016 Jun Pataleta <jun@moodle.com>
37 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class core_grade_lib_test extends advanced_testcase {
40
41    /**
42     * Test can_output_item.
43     */
44    public function test_can_output_item() {
45        $this->resetAfterTest();
46
47        $generator = $this->getDataGenerator();
48
49        // Course level grade category.
50        $course = $generator->create_course();
51        // Grade tree looks something like:
52        // - Test course    (Rendered).
53        $gradetree = grade_category::fetch_course_tree($course->id);
54        $this->assertTrue(grade_tree::can_output_item($gradetree));
55
56        // Add a grade category with default settings.
57        $generator->create_grade_category(array('courseid' => $course->id));
58        // Grade tree now looks something like:
59        // - Test course n        (Rendered).
60        // -- Grade category n    (Rendered).
61        $gradetree = grade_category::fetch_course_tree($course->id);
62        $this->assertNotEmpty($gradetree['children']);
63        foreach ($gradetree['children'] as $child) {
64            $this->assertTrue(grade_tree::can_output_item($child));
65        }
66
67        // Add a grade category with grade type = None.
68        $nototalcategory = 'No total category';
69        $nototalparams = [
70            'courseid' => $course->id,
71            'fullname' => $nototalcategory,
72            'aggregation' => GRADE_AGGREGATE_WEIGHTED_MEAN
73        ];
74        $nototal = $generator->create_grade_category($nototalparams);
75        $catnototal = grade_category::fetch(array('id' => $nototal->id));
76        // Set the grade type of the grade item associated to the grade category.
77        $catitemnototal = $catnototal->load_grade_item();
78        $catitemnototal->gradetype = GRADE_TYPE_NONE;
79        $catitemnototal->update();
80
81        // Grade tree looks something like:
82        // - Test course n        (Rendered).
83        // -- Grade category n    (Rendered).
84        // -- No total category   (Not rendered).
85        $gradetree = grade_category::fetch_course_tree($course->id);
86        foreach ($gradetree['children'] as $child) {
87            if ($child['object']->fullname == $nototalcategory) {
88                $this->assertFalse(grade_tree::can_output_item($child));
89            } else {
90                $this->assertTrue(grade_tree::can_output_item($child));
91            }
92        }
93
94        // Add another grade category with default settings under 'No total category'.
95        $normalinnototalparams = [
96            'courseid' => $course->id,
97            'fullname' => 'Normal category in no total category',
98            'parent' => $nototal->id
99        ];
100        $generator->create_grade_category($normalinnototalparams);
101
102        // Grade tree looks something like:
103        // - Test course n                           (Rendered).
104        // -- Grade category n                       (Rendered).
105        // -- No total category                      (Rendered).
106        // --- Normal category in no total category  (Rendered).
107        $gradetree = grade_category::fetch_course_tree($course->id);
108        foreach ($gradetree['children'] as $child) {
109            // All children are now visible.
110            $this->assertTrue(grade_tree::can_output_item($child));
111            if (!empty($child['children'])) {
112                foreach ($child['children'] as $grandchild) {
113                    $this->assertTrue(grade_tree::can_output_item($grandchild));
114                }
115            }
116        }
117
118        // Add a grade category with grade type = None.
119        $nototalcategory2 = 'No total category 2';
120        $nototal2params = [
121            'courseid' => $course->id,
122            'fullname' => $nototalcategory2,
123            'aggregation' => GRADE_AGGREGATE_WEIGHTED_MEAN
124        ];
125        $nototal2 = $generator->create_grade_category($nototal2params);
126        $catnototal2 = grade_category::fetch(array('id' => $nototal2->id));
127        // Set the grade type of the grade item associated to the grade category.
128        $catitemnototal2 = $catnototal2->load_grade_item();
129        $catitemnototal2->gradetype = GRADE_TYPE_NONE;
130        $catitemnototal2->update();
131
132        // Add a category with no total under 'No total category'.
133        $nototalinnototalcategory = 'Category with no total in no total category';
134        $nototalinnototalparams = [
135            'courseid' => $course->id,
136            'fullname' => $nototalinnototalcategory,
137            'aggregation' => GRADE_AGGREGATE_WEIGHTED_MEAN,
138            'parent' => $nototal2->id
139        ];
140        $nototalinnototal = $generator->create_grade_category($nototalinnototalparams);
141        $catnototalinnototal = grade_category::fetch(array('id' => $nototalinnototal->id));
142        // Set the grade type of the grade item associated to the grade category.
143        $catitemnototalinnototal = $catnototalinnototal->load_grade_item();
144        $catitemnototalinnototal->gradetype = GRADE_TYPE_NONE;
145        $catitemnototalinnototal->update();
146
147        // Grade tree looks something like:
148        // - Test course n                                    (Rendered).
149        // -- Grade category n                                (Rendered).
150        // -- No total category                               (Rendered).
151        // --- Normal category in no total category           (Rendered).
152        // -- No total category 2                             (Not rendered).
153        // --- Category with no total in no total category    (Not rendered).
154        $gradetree = grade_category::fetch_course_tree($course->id);
155        foreach ($gradetree['children'] as $child) {
156            if ($child['object']->fullname == $nototalcategory2) {
157                $this->assertFalse(grade_tree::can_output_item($child));
158            } else {
159                $this->assertTrue(grade_tree::can_output_item($child));
160            }
161            if (!empty($child['children'])) {
162                foreach ($child['children'] as $grandchild) {
163                    if ($grandchild['object']->fullname == $nototalinnototalcategory) {
164                        $this->assertFalse(grade_tree::can_output_item($grandchild));
165                    } else {
166                        $this->assertTrue(grade_tree::can_output_item($grandchild));
167                    }
168                }
169            }
170        }
171    }
172}
173