Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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_grades\external\get_gradeitems as get_gradeitems;
  20  use core_external\external_api;
  21  use grade_item;
  22  
  23  defined('MOODLE_INTERNAL') || die;
  24  
  25  global $CFG;
  26  
  27  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  28  
  29  /**
  30   * Unit tests for the core_grades\external\get_gradeitems.
  31   *
  32   * @package    core_grades
  33   * @category   external
  34   * @copyright  2023 Mathew May <Mathew.solutions>
  35   * @covers     \core_grades\external\get_gradeitems
  36   */
  37  class get_gradeitems_test extends \externallib_advanced_testcase {
  38      public function test_execute(): void {
  39          $this->resetAfterTest();
  40          $this->setAdminUser();
  41          $course = $this->getDataGenerator()->create_course();
  42          $this->getDataGenerator()->create_module('forum', ['course' => $course->id]);
  43          $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course->id]);
  44          $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
  45  
  46          $result = get_gradeitems::execute($course->id);
  47          $result = external_api::clean_returnvalue(get_gradeitems::execute_returns(), $result);
  48          $allgradeitems = grade_item::fetch_all(['courseid' => $course->id]);
  49          $gradeitems = array_filter($allgradeitems, function($item) {
  50              $item->itemname = $item->get_name();
  51              $item->category = $item->get_parent_category()->get_name();
  52              return $item->gradetype != GRADE_TYPE_NONE && !$item->is_category_item() && !$item->is_course_item();
  53          });
  54          // Move back from grade items into an array of arrays.
  55          $mapped = array_map(function($item) {
  56              return [
  57                  'id' => $item->id,
  58                  'itemname' => $item->itemname,
  59                  'category' => $item->category
  60              ];
  61          }, array_values($gradeitems));
  62          $this->assertEquals($mapped, $result['gradeItems']);
  63      }
  64  }