Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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