Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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