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_reportbuilder\local\report;
  20  
  21  use advanced_testcase;
  22  use context_system;
  23  use core_reportbuilder\system_report_available;
  24  use core_reportbuilder\system_report_factory;
  25  
  26  /**
  27   * Unit tests for report base class
  28   *
  29   * @package     core_reportbuilder
  30   * @covers      \core_reportbuilder\local\report\base
  31   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class base_test extends advanced_testcase {
  35  
  36      /**
  37       * Load required class
  38       */
  39      public static function setUpBeforeClass(): void {
  40          global $CFG;
  41          require_once("{$CFG->dirroot}/reportbuilder/tests/fixtures/system_report_available.php");
  42      }
  43  
  44      /**
  45       * Test for add_base_condition_simple
  46       */
  47      public function test_add_base_condition_simple(): void {
  48          $this->resetAfterTest();
  49  
  50          $systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
  51          $systemreport->add_base_condition_simple('username', 'admin');
  52          [$where, $params] = $systemreport->get_base_condition();
  53          $this->assertStringMatchesFormat('username = :%a', $where);
  54          $this->assertEqualsCanonicalizing(['admin'], $params);
  55      }
  56  
  57      /**
  58       * Test for add_base_condition_simple null
  59       */
  60      public function test_add_base_condition_simple_null(): void {
  61          $this->resetAfterTest();
  62  
  63          $systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
  64          $systemreport->add_base_condition_simple('username', null);
  65          [$where, $params] = $systemreport->get_base_condition();
  66          $this->assertEquals('username IS NULL', $where);
  67          $this->assertEmpty($params);
  68      }
  69  
  70      /**
  71       * Test for get_filter_instances
  72       */
  73      public function test_get_filter_instances(): void {
  74          $this->resetAfterTest();
  75  
  76          $systemreport = system_report_factory::create(system_report_available::class, context_system::instance(),
  77              '', '', 0, ['withfilters' => true]);
  78          $filters = $systemreport->get_filter_instances();
  79          $this->assertCount(1, $filters);
  80          $this->assertInstanceOf(\core_reportbuilder\local\filters\text::class, reset($filters));
  81      }
  82  
  83      /**
  84       * Test for set_downloadable
  85       */
  86      public function test_set_downloadable(): void {
  87          $this->resetAfterTest();
  88  
  89          $systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
  90          $systemreport->set_downloadable(true, 'testfilename');
  91          $this->assertTrue($systemreport->is_downloadable());
  92          $this->assertEquals('testfilename', $systemreport->get_downloadfilename());
  93  
  94          $systemreport->set_downloadable(false, 'anothertestfilename');
  95          $this->assertFalse($systemreport->is_downloadable());
  96          $this->assertEquals('anothertestfilename', $systemreport->get_downloadfilename());
  97      }
  98  
  99      /**
 100       * Test for get_context
 101       */
 102      public function test_get_context(): void {
 103          $this->resetAfterTest();
 104  
 105          $systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
 106          $this->assertEquals(context_system::instance(), $systemreport->get_context());
 107  
 108          $course = $this->getDataGenerator()->create_course();
 109          $contextcourse = \context_course::instance($course->id);
 110          $systemreport2 = system_report_factory::create(system_report_available::class, $contextcourse);
 111          $this->assertEquals($contextcourse, $systemreport2->get_context());
 112      }
 113  
 114      /**
 115       * Test for get_column
 116       */
 117      public function test_get_column(): void {
 118          $this->resetAfterTest();
 119  
 120          $systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
 121          $column = $systemreport->get_column('user:username');
 122          $this->assertInstanceOf(column::class, $column);
 123  
 124          $column = $systemreport->get_column('user:nonexistingcolumn');
 125          $this->assertNull($column);
 126      }
 127  
 128      /**
 129       * Test for get_filter
 130       */
 131      public function test_get_filter(): void {
 132          $this->resetAfterTest();
 133  
 134          $systemreport = system_report_factory::create(system_report_available::class, context_system::instance(),
 135              '', '', 0, ['withfilters' => true]);
 136          $filter = $systemreport->get_filter('user:username');
 137          $this->assertInstanceOf(filter::class, $filter);
 138  
 139          $filter = $systemreport->get_filter('user:nonexistingfilter');
 140          $this->assertNull($filter);
 141      }
 142  
 143      /**
 144       * Test for get_report_persistent
 145       */
 146      public function test_get_report_persistent(): void {
 147          $this->resetAfterTest();
 148  
 149          $systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
 150          $persistent = $systemreport->get_report_persistent();
 151          $this->assertEquals(system_report_available::class, $persistent->get('source'));
 152      }
 153  }