Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 403]

   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  declare(strict_types=1);
  18  
  19  namespace core_badges\reportbuilder\datasource;
  20  
  21  use core_badges_generator;
  22  use core_reportbuilder_generator;
  23  use core_reportbuilder_testcase;
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
  29  
  30  /**
  31   * Unit tests for badges datasource
  32   *
  33   * @package     core_badges
  34   * @covers      \core_badges\reportbuilder\datasource\badges
  35   * @copyright   2022 Paul Holden <paulh@moodle.com>
  36   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class badges_test extends core_reportbuilder_testcase {
  39  
  40      /**
  41       * Load required test libraries
  42       */
  43      public static function setUpBeforeClass(): void {
  44          global $CFG;
  45          require_once("{$CFG->libdir}/badgeslib.php");
  46      }
  47  
  48      /**
  49       * Test datasource
  50       */
  51      public function test_datasource(): void {
  52          $this->resetAfterTest();
  53          $this->setAdminUser();
  54  
  55          // Test users with a badge we can issue them with.
  56          $user1 = $this->getDataGenerator()->create_user(['firstname' => 'Alan', 'lastname' => 'Apple']);
  57          $user2 = $this->getDataGenerator()->create_user(['firstname' => 'Barry', 'lastname' => 'Banana']);
  58  
  59          /** @var core_badges_generator $generator */
  60          $generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
  61  
  62          $sitebadge = $generator->create_badge(['name' => 'Badge 1']);
  63          $sitebadge->issue($user1->id, true);
  64          $sitebadge->issue($user2->id, true);
  65  
  66          // Another badge, in a course, no issues.
  67          $course = $this->getDataGenerator()->create_course();
  68          $coursebadge = $generator->create_badge(['name' => 'Badge 2', 'type' => BADGE_TYPE_COURSE, 'courseid' => $course->id]);
  69  
  70          /** @var core_reportbuilder_generator $generator */
  71          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  72          $report = $generator->create_report(['name' => 'Badges', 'source' => badges::class, 'default' => 0]);
  73  
  74          // Badge course.
  75          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname', 'sortenabled' => 1]);
  76  
  77          // Badge name.
  78          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:name', 'sortenabled' => 1]);
  79  
  80          // User fullname.
  81          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname', 'sortenabled' => 1]);
  82  
  83          $content = $this->get_custom_report_content($report->get('id'));
  84          $this->assertCount(3, $content);
  85  
  86          $this->assertEquals([
  87              ['PHPUnit test site', $sitebadge->name, fullname($user1, true)],
  88              ['PHPUnit test site', $sitebadge->name, fullname($user2, true)],
  89              [$course->fullname, $coursebadge->name, ''],
  90          ], array_map(static function(array $row): array {
  91              return array_values($row);
  92          }, $content));
  93      }
  94  
  95      /**
  96       * Stress test datasource
  97       *
  98       * In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
  99       */
 100      public function test_stress_datasource(): void {
 101          if (!PHPUNIT_LONGTEST) {
 102              $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
 103          }
 104  
 105          $this->resetAfterTest();
 106  
 107          $course = $this->getDataGenerator()->create_course();
 108          $user = $this->getDataGenerator()->create_and_enrol($course);
 109  
 110          /** @var core_badges_generator $generator */
 111          $generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
 112          $badge = $generator->create_badge(['name' => 'Course badge', 'type' => BADGE_TYPE_COURSE, 'courseid' => $course->id]);
 113          $badge->issue($user->id, true);
 114  
 115          $this->datasource_stress_test_columns(badges::class);
 116          $this->datasource_stress_test_columns_aggregation(badges::class);
 117          $this->datasource_stress_test_conditions(badges::class, 'badge:name');
 118      }
 119  }