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 402] [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_comment\reportbuilder\datasource;
  20  
  21  use comment;
  22  use context_course;
  23  use core_reportbuilder_generator;
  24  use core_reportbuilder_testcase;
  25  use core_reportbuilder\local\filters\{date, text};
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
  31  
  32  /**
  33   * Unit tests for comments datasource
  34   *
  35   * @package     core_comment
  36   * @covers      \core_comment\reportbuilder\datasource\comments
  37   * @copyright   2022 Paul Holden <paulh@moodle.com>
  38   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class comments_test extends core_reportbuilder_testcase {
  41  
  42      /**
  43       * Require test libraries
  44       */
  45      public static function setUpBeforeClass(): void {
  46          global $CFG;
  47          require_once("{$CFG->dirroot}/comment/lib.php");
  48      }
  49  
  50      /**
  51       * Test default datasource
  52       */
  53      public function test_datasource_default(): void {
  54          $this->resetAfterTest();
  55          $this->setAdminUser();
  56  
  57          $course = $this->getDataGenerator()->create_course();
  58          $coursecontext = context_course::instance($course->id);
  59  
  60          $comment = new comment((object) [
  61              'context' => $coursecontext,
  62              'component' => 'block_comments',
  63              'area' => 'page_comments',
  64          ]);
  65          $comment->add('Cool');
  66  
  67          /** @var core_reportbuilder_generator $generator */
  68          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
  69          $report = $generator->create_report(['name' => 'Blogs', 'source' => comments::class, 'default' => 1]);
  70  
  71          $content = $this->get_custom_report_content($report->get('id'));
  72          $this->assertCount(1, $content);
  73  
  74          // Default columns are context, content, user, time created.
  75          [$contextname, $content, $userfullname, $timecreated] = array_values($content[0]);
  76  
  77          $this->assertEquals($coursecontext->get_context_name(), $contextname);
  78          $this->assertEquals(format_text('Cool'), $content);
  79          $this->assertEquals(fullname(get_admin()), $userfullname);
  80          $this->assertNotEmpty($timecreated);
  81      }
  82  
  83      /**
  84       * Test datasource columns that aren't added by default
  85       */
  86      public function test_datasource_non_default_columns(): void {
  87          $this->resetAfterTest();
  88          $this->setAdminUser();
  89  
  90          $course = $this->getDataGenerator()->create_course();
  91          $courseurl = course_get_url($course);
  92          $coursecontext = context_course::instance($course->id);
  93  
  94          $comment = new comment((object) [
  95              'context' => $coursecontext,
  96              'component' => 'block_comments',
  97              'area' => 'page_comments',
  98          ]);
  99          $comment->add('Cool');
 100  
 101          /** @var core_reportbuilder_generator $generator */
 102          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 103          $report = $generator->create_report(['name' => 'Blogs', 'source' => comments::class, 'default' => 0]);
 104  
 105          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:contexturl']);
 106          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:component']);
 107          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:area']);
 108          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:itemid']);
 109  
 110          $content = $this->get_custom_report_content($report->get('id'));
 111          $this->assertCount(1, $content);
 112  
 113          $this->assertEquals([
 114              "<a href=\"{$courseurl}\">{$coursecontext->get_context_name()}</a>",
 115              'block_comments',
 116              'page_comments',
 117              0,
 118          ], array_values($content[0]));
 119      }
 120  
 121      /**
 122       * Data provider for {@see test_datasource_filters}
 123       *
 124       * @return array[]
 125       */
 126      public function datasource_filters_provider(): array {
 127          return [
 128              // Comment.
 129              'Filter content' => ['comment:content', [
 130                  'comment:content_operator' => text::CONTAINS,
 131                  'comment:content_value' => 'Cool',
 132              ], true],
 133              'Filter content (no match)' => ['comment:content', [
 134                  'comment:content_operator' => text::IS_EQUAL_TO,
 135                  'comment:content_value' => 'Beans',
 136              ], false],
 137              'Filter time created' => ['comment:timecreated', [
 138                  'comment:timecreated_operator' => date::DATE_RANGE,
 139                  'comment:timecreated_from' => 1622502000,
 140              ], true],
 141              'Filter time created (no match)' => ['comment:timecreated', [
 142                  'comment:timecreated_operator' => date::DATE_RANGE,
 143                  'comment:timecreated_to' => 1622502000,
 144              ], false],
 145  
 146              // User (just to check the join).
 147              'Filter user' => ['user:username', [
 148                  'user:username_operator' => text::IS_EQUAL_TO,
 149                  'user:username_value' => 'admin',
 150              ], true],
 151              'Filter user (no match)' => ['user:username', [
 152                  'user:username_operator' => text::IS_EQUAL_TO,
 153                  'user:username_value' => 'lionel',
 154              ], false],
 155          ];
 156      }
 157  
 158      /**
 159       * Test datasource filters
 160       *
 161       * @param string $filtername
 162       * @param array $filtervalues
 163       * @param bool $expectmatch
 164       *
 165       * @dataProvider datasource_filters_provider
 166       */
 167      public function test_datasource_filters(
 168          string $filtername,
 169          array $filtervalues,
 170          bool $expectmatch
 171      ): void {
 172          $this->resetAfterTest();
 173          $this->setAdminUser();
 174  
 175          $course = $this->getDataGenerator()->create_course();
 176          $coursecontext = context_course::instance($course->id);
 177  
 178          $comment = new comment((object) [
 179              'context' => $coursecontext,
 180              'component' => 'block_comments',
 181              'area' => 'page_comments',
 182          ]);
 183          $comment->add('Cool');
 184  
 185          /** @var core_reportbuilder_generator $generator */
 186          $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 187  
 188          // Create report containing single column, and given filter.
 189          $report = $generator->create_report(['name' => 'Tasks', 'source' => comments::class, 'default' => 0]);
 190          $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:component']);
 191  
 192          // Add filter, set it's values.
 193          $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
 194          $content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
 195  
 196          if ($expectmatch) {
 197              $this->assertCount(1, $content);
 198              $this->assertEquals('block_comments', reset($content[0]));
 199          } else {
 200              $this->assertEmpty($content);
 201          }
 202      }
 203  
 204      /**
 205       * Stress test datasource
 206       *
 207       * In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
 208       */
 209      public function test_stress_datasource(): void {
 210          if (!PHPUNIT_LONGTEST) {
 211              $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
 212          }
 213  
 214          $this->resetAfterTest();
 215          $this->setAdminUser();
 216  
 217          $course = $this->getDataGenerator()->create_course();
 218          $coursecontext = context_course::instance($course->id);
 219  
 220          $comment = new comment((object) [
 221              'context' => $coursecontext,
 222              'component' => 'block_comments',
 223              'area' => 'page_comments',
 224          ]);
 225          $comment->add('Cool');
 226  
 227          $this->datasource_stress_test_columns(comments::class);
 228          $this->datasource_stress_test_columns_aggregation(comments::class);
 229          $this->datasource_stress_test_conditions(comments::class, 'comment:component');
 230      }
 231  }