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_completion;
  18  
  19  /**
  20   * PHPUnit data generator testcase
  21   *
  22   * @package     core_completion
  23   * @category    test
  24   * @copyright   2023 Amaia Anabitarte <amaia@moodle.com>
  25   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   * @coversDefaultClass \core_completion_generator
  27   */
  28  class generator_test extends \advanced_testcase {
  29  
  30      /**
  31       * Test create_default_completion.
  32       *
  33       * @dataProvider create_default_completion_provider
  34       *
  35       * @param int|null|string $course The course to add the default activities conditions to.
  36       * @param int|null|string $module The module to add the default activities conditions to.
  37       * @param bool $exception Whether an exception is expected or not.
  38       * @param int $count The number of default activity completions to be created.
  39       * @param int $completion The value for completion setting.
  40       *
  41       * @covers ::create_default_completion
  42       */
  43      public function test_create_default_completion($course, $module, bool $exception, int $count, int $completion = 0) {
  44          global $DB;
  45  
  46          $this->resetAfterTest(true);
  47          $generator = $this->getDataGenerator()->get_plugin_generator('core_completion');
  48  
  49          $record = [
  50              'course' => $course,
  51              'module' => $module,
  52              'completion' => $completion,
  53          ];
  54          $result = (object) array_merge([
  55              'completion' => 0,
  56              'completionview' => 0,
  57              'completionusegrade' => 0,
  58              'completionpassgrade' => 0,
  59              'completionexpected' => 0,
  60              'customrules' => '',
  61          ], $record);
  62  
  63          if ($exception) {
  64              $this->expectException('moodle_exception');
  65          }
  66          $defaultcompletion = $generator->create_default_completion($record);
  67  
  68          if (!$exception) {
  69              foreach ($result as $key => $value) {
  70                  $this->assertEquals($defaultcompletion->{$key}, $value);
  71              }
  72          }
  73          $this->assertEquals(
  74              $count,
  75              $DB->count_records('course_completion_defaults', ['course' => $course, 'module' => $module])
  76          );
  77      }
  78  
  79      /**
  80       * Data provider for test_create_default_completion().
  81       * @return array[]
  82       */
  83      public function create_default_completion_provider(): array {
  84          global $SITE;
  85  
  86          return [
  87              'Null course' => [
  88                  'course' => null,
  89                  'module' => null,
  90                  'exception' => true,
  91                  'count' => 0,
  92              ],
  93              'Empty course' => [
  94                  'course' => '',
  95                  'module' => null,
  96                  'exception' => true,
  97                  'count' => 0,
  98              ],
  99              'Invalid course' => [
 100                  'course' => 0,
 101                  'module' => null,
 102                  'exception' => true,
 103                  'count' => 0,
 104              ],
 105              'Null module' => [
 106                  'course' => $SITE->id,
 107                  'module' => null,
 108                  'exception' => true,
 109                  'count' => 0,
 110              ],
 111              'Empty module' => [
 112                  'course' => $SITE->id,
 113                  'module' => null,
 114                  'exception' => true,
 115                  'count' => 0,
 116              ],
 117              'Invalid module' => [
 118                  'course' => $SITE->id,
 119                  'module' => 0,
 120                  'exception' => true,
 121                  'count' => 0,
 122              ],
 123              'Default activity completion: NONE' => [
 124                  'course' => $SITE->id,
 125                  'module' => 1,
 126                  'exception' => false,
 127                  'count' => 1,
 128              ],
 129              'Default activity completion: AUTOMATIC' => [
 130                  'course' => $SITE->id,
 131                  'module' => 1,
 132                  'exception' => false,
 133                  'count' => 1,
 134                  'completion' => 2,
 135              ],
 136          ];
 137      }
 138  }