Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

   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 mod_forum\grades\gradeitems.
  19   *
  20   * @package   mod_forum
  21   * @category  test
  22   * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU Public License
  24   */
  25  
  26  declare(strict_types = 1);
  27  
  28  namespace tests\mod_forum\grades;
  29  
  30  use advanced_testcase;
  31  use core_grades\component_gradeitems;
  32  use coding_exception;
  33  
  34  /**
  35   * Unit tests for mod_forum\grades\gradeitems.
  36   *
  37   * @package   mod_forum
  38   * @category  test
  39   * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
  40   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class gradeitems_test extends advanced_testcase {
  43  
  44      /**
  45       * Ensure that the mappings are present and correct.
  46       */
  47      public function test_get_itemname_mapping_for_component(): void {
  48          $mappings = component_gradeitems::get_itemname_mapping_for_component('mod_forum');
  49          $this->assertIsArray($mappings);
  50          $this->assertCount(2, $mappings);
  51          $expected = [0 => 'rating', 1 => 'forum'];
  52          // Verify each expected element exists and its value matches.
  53          foreach ($expected as $key => $value) {
  54              $this->assertArrayHasKey($key, $mappings);
  55              $this->assertSame($value, $mappings[$key]);
  56          }
  57      }
  58  
  59      /**
  60       * Ensure that the advanced grading only applies to the relevant items.
  61       */
  62      public function test_get_advancedgrading_itemnames_for_component(): void {
  63          $mappings = component_gradeitems::get_advancedgrading_itemnames_for_component('mod_forum');
  64          $this->assertIsArray($mappings);
  65          $this->assertCount(1, $mappings);
  66          $this->assertContains('forum', $mappings);
  67          $this->assertNotContains('rating', $mappings);
  68      }
  69  
  70      /**
  71       * Ensure that the correct items are identified by is_advancedgrading_itemname.
  72       *
  73       * @dataProvider is_advancedgrading_itemname_provider
  74       * @param string $itemname
  75       * @param bool $expected
  76       */
  77      public function test_is_advancedgrading_itemname(string $itemname, bool $expected): void {
  78          $this->assertEquals(
  79              $expected,
  80              component_gradeitems::is_advancedgrading_itemname('mod_forum', $itemname)
  81          );
  82      }
  83  
  84      /**
  85       * Data provider for tests of is_advancedgrading_itemname.
  86       *
  87       * @return array
  88       */
  89      public function is_advancedgrading_itemname_provider(): array {
  90          return [
  91              'rating is not advanced' => [
  92                  'rating',
  93                  false,
  94              ],
  95              'Whole forum grading is advanced' => [
  96                  'forum',
  97                  true,
  98              ],
  99          ];
 100      }
 101  }