This commit is contained in:
Sara Arjona 2022-12-28 18:50:28 +01:00
commit bb883d95aa
2 changed files with 37 additions and 0 deletions

View File

@ -23,6 +23,7 @@ use core_reportbuilder\datasource;
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;
/**
@ -86,6 +87,12 @@ class blogs extends datasource {
$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());
@ -100,6 +107,11 @@ class blogs extends datasource {
$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'));
}
/**

View File

@ -18,7 +18,9 @@ declare(strict_types=1);
namespace core_blog\reportbuilder\datasource;
use comment;
use context_system;
use context_user;
use core_blog_generator;
use core_collator;
use core_reportbuilder_generator;
@ -40,6 +42,14 @@ require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
*/
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
*/
@ -90,6 +100,7 @@ class blogs_test extends core_reportbuilder_testcase {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
/** @var core_blog_generator $blogsgenerator */
$blogsgenerator = $this->getDataGenerator()->get_plugin_generator('core_blog');
@ -107,6 +118,15 @@ class blogs_test extends core_reportbuilder_testcase {
'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;
@ -127,6 +147,9 @@ class blogs_test extends core_reportbuilder_testcase {
// 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);
@ -137,6 +160,7 @@ class blogs_test extends core_reportbuilder_testcase {
$timemodified,
$tags,
$filesize,
$comment,
] = array_values($content[0]);
$this->assertStringContainsString('Horses', $body);
@ -145,6 +169,7 @@ class blogs_test extends core_reportbuilder_testcase {
$this->assertEquals(userdate($blog->lastmodified), $timemodified);
$this->assertEquals('horse', $tags);
$this->assertEquals("5\xc2\xa0bytes", $filesize);
$this->assertEquals(format_text('Cool'), $comment);
}
/**