Skip to content

Commit

Permalink
Merge branch 'MDL-76153' of https://github.com/paulholden/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjona committed Dec 28, 2022
2 parents 2842575 + 27ebde5 commit bb883d9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
12 changes: 12 additions & 0 deletions blog/classes/reportbuilder/datasource/blogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use core_reportbuilder\local\entities\{course, user};
use core_blog\reportbuilder\local\entities\blog;
use core_files\reportbuilder\local\entities\file;
use core_comment\reportbuilder\local\entities\comment;
use core_tag\reportbuilder\local\entities\tag;

/**
Expand Down Expand Up @@ -86,6 +87,12 @@ protected function initialise(): void {
$this->add_entity($courseentity
->add_join("LEFT JOIN {course} {$coursealias} ON {$coursealias}.id = {$postalias}.courseid"));

// Join the comment entity (ensure differing alias from that used by course entity).
$commententity = (new comment())
->set_table_alias('comments', 'bcmt');
$this->add_entity($commententity
->add_join("LEFT JOIN {comments} bcmt ON bcmt.component = 'blog' AND bcmt.itemid = {$postalias}.id"));

// Add report elements from each of the entities we added to the report.
$this->add_all_from_entity($blogentity->get_entity_name());

Expand All @@ -100,6 +107,11 @@ protected function initialise(): void {

$this->add_all_from_entity($userentity->get_entity_name());
$this->add_all_from_entity($courseentity->get_entity_name());

// Add specific comment entity elements.
$this->add_columns_from_entity($commententity->get_entity_name(), ['content', 'timecreated']);
$this->add_filter($commententity->get_filter('timecreated'));
$this->add_condition($commententity->get_filter('timecreated'));
}

/**
Expand Down
25 changes: 25 additions & 0 deletions blog/tests/reportbuilder/datasource/blogs_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

namespace core_blog\reportbuilder\datasource;

use comment;
use context_system;
use context_user;
use core_blog_generator;
use core_collator;
use core_reportbuilder_generator;
Expand All @@ -40,6 +42,14 @@
*/
class blogs_test extends core_reportbuilder_testcase {

/**
* Required test libraries
*/
public static function setUpBeforeClass(): void {
global $CFG;
require_once("{$CFG->dirroot}/comment/lib.php");
}

/**
* Test default datasource
*/
Expand Down Expand Up @@ -90,6 +100,7 @@ public function test_datasource_non_default_columns(): void {
$this->resetAfterTest();

$user = $this->getDataGenerator()->create_user();
$this->setUser($user);

/** @var core_blog_generator $blogsgenerator */
$blogsgenerator = $this->getDataGenerator()->get_plugin_generator('core_blog');
Expand All @@ -107,6 +118,15 @@ public function test_datasource_non_default_columns(): void {
'filename' => 'hello.txt',
], 'hello');

// Add a comment.
$comment = new comment((object) [
'context' => context_user::instance($user->id),
'component' => 'blog',
'area' => 'format_blog',
'itemid' => $blog->id,
]);
$comment->add('Cool');

// Manually update the created/modified date of the blog.
$blog->created = 1654038000;
$blog->lastmodified = $blog->created + HOURSECS;
Expand All @@ -127,6 +147,9 @@ public function test_datasource_non_default_columns(): void {
// File entity.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:size']);

// Comment entity.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:content']);

$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(1, $content);

Expand All @@ -137,6 +160,7 @@ public function test_datasource_non_default_columns(): void {
$timemodified,
$tags,
$filesize,
$comment,
] = array_values($content[0]);

$this->assertStringContainsString('Horses', $body);
Expand All @@ -145,6 +169,7 @@ public function test_datasource_non_default_columns(): void {
$this->assertEquals(userdate($blog->lastmodified), $timemodified);
$this->assertEquals('horse', $tags);
$this->assertEquals("5\xc2\xa0bytes", $filesize);
$this->assertEquals(format_text('Cool'), $comment);
}

/**
Expand Down

0 comments on commit bb883d9

Please sign in to comment.