first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
@core @core_blog @_file_upload @javascript
Feature: Blog entries can be added, modified and deleted
In order to modify or delete a blog entry
As a user
I need to be able to add a blog entry
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| testuser | Test | User | moodle@example.com |
And I am on the "testuser" "user > profile" page logged in as testuser
And I follow "Blog entries"
And I follow "Add a new entry"
And I should see "Blogs: Add a new entry"
And I set the following fields to these values:
| Entry title | Entry 1 |
| Blog entry body | Entry 1 content |
| Attachment | lib/tests/fixtures/gd-logo.png |
And I press "Save changes"
Scenario: Modify a blog entry
When I click on "Edit" "link"
And I set the following fields to these values:
| Entry title | Blog entry 1 |
And I press "Save changes"
Then I should see "Blog entry 1"
Scenario: Delete a blog entry
When I click on "Delete" "link"
And I press "Continue"
Then I should not see "Entry 1"
And I should see "Add a new entry"
+34
View File
@@ -0,0 +1,34 @@
@core @core_blog
Feature: Blogs can be set to be only visible by the author.
In order to make blogs personal only
As a user
I need to set the blog level to Users can only see their own blogs.
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| testuser | Test | User | moodle@example.com |
| testuser2 | Test2 | User2 | moodle2@example.com |
And the following "courses" exist:
| fullname | shortname |
| Course 1 | C1 |
And the following "course enrolments" exist:
| user | course | role |
| testuser | C1 | student |
| testuser2 | C1 | student |
And I log in as "admin"
And I am on site homepage
And I navigate to "Appearance > Blog" in site administration
And I set the following fields to these values:
| Blog visibility | Users can only see their own blog |
And I press "Save changes"
Scenario: A student can not see another student's blog entries.
Given I am on the "Course 1" course page logged in as testuser
And I navigate to course participants
When I follow "Test2 User2"
And I should see "Miscellaneous"
Then I should not see "Blog entries"
And I follow "Profile" in the user menu
And I follow "Blog entries"
And I should see "User blog: Test User"
+70
View File
@@ -0,0 +1,70 @@
@core @core_blog @javascript
Feature: Comment on a blog entry
In order to respond to a blog post
As a user
I need to be able to comment on a blog entry
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| testuser | Test | User | moodle@example.com |
| testuser2 | Test2 | User2 | moodle2@example.com |
And the following "core_blog > entries" exist:
| subject | body | user |
| Blog post from user 1 | User 1 blog post content | testuser |
And I log in as "admin"
And I am on site homepage
And I turn editing mode on
And the following config values are set as admin:
| unaddableblocks | | theme_boost|
# TODO MDL-57120 "Site blogs" link not accessible without navigation block.
And I add the "Navigation" block if not present
And I configure the "Navigation" block
And I set the following fields to these values:
| Page contexts | Display throughout the entire site |
And I press "Save changes"
And I log out
Scenario: Commenting on my own blog entry
Given I log in as "testuser"
And I click on "Site pages" "list_item" in the "Navigation" "block"
And I click on "Site blogs" "link" in the "Navigation" "block"
And I follow "Blog post from user 1"
And I should see "User 1 blog post content"
And I follow "Comments (0)"
When I set the field "content" to "$My own >nasty< \"string\"!"
And I follow "Save comment"
Then I should see "$My own >nasty< \"string\"!"
And I set the field "content" to "Another $Nasty <string?>"
And I follow "Save comment"
And I should see "Comments (2)" in the ".comment-link" "css_element"
Scenario: Deleting my own comment
Given I log in as "testuser"
And I click on "Site pages" "list_item" in the "Navigation" "block"
And I click on "Site blogs" "link" in the "Navigation" "block"
And I follow "Blog post from user 1"
And I should see "User 1 blog post content"
And I follow "Comments (0)"
And I set the field "content" to "$My own >nasty< \"string\"!"
And I follow "Save comment"
When I click on ".comment-delete a" "css_element"
# Waiting for the animation to finish.
And I wait "4" seconds
Then I should not see "$My own >nasty< \"string\"!"
And I follow "Blog post from user 1"
And I click on ".comment-link" "css_element"
And I should not see "$My own >nasty< \"string\"!"
And I should see "Comments (0)" in the ".comment-link" "css_element"
Scenario: Commenting on someone's blog post
Given I am on site homepage
And I log in as "testuser2"
And I am on site homepage
And I click on "Site pages" "list_item" in the "Navigation" "block"
And I click on "Site blogs" "link" in the "Navigation" "block"
And I follow "Blog post from user 1"
When I follow "Comments (0)"
And I set the field "content" to "$My own >nasty< \"string\"!"
And I follow "Save comment"
Then I should see "$My own >nasty< \"string\"!"
+45
View File
@@ -0,0 +1,45 @@
@core @core_blog
Feature: Delete a blog entry
In order to manage my blog entries
As a user
I need to be able to delete entries I no longer wish to appear
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| testuser | Test | User | moodle@example.com |
And the following "core_blog > entries" exist:
| subject | body | user |
| Blog post one | User 1 blog post content | testuser |
| Blog post two | User 1 blog post content | testuser |
And I log in as "admin"
And I am on site homepage
And I turn editing mode on
And the following config values are set as admin:
| unaddableblocks | | theme_boost|
# TODO MDL-57120 "Site blogs" link not accessible without navigation block.
And I add the "Navigation" block if not present
And I configure the "Navigation" block
And I set the following fields to these values:
| Page contexts | Display throughout the entire site |
And I press "Save changes"
And I log out
And I log in as "testuser"
And I am on site homepage
And I click on "Site blogs" "link" in the "Navigation" "block"
Scenario: Delete blog post results in post deleted
Given I follow "Blog post one"
And I follow "Delete"
And I should see "Delete the blog entry 'Blog post one'?"
When I press "Continue"
Then I should not see "Blog post one"
And I should see "Blog post two"
Scenario: Delete confirmation screen works and allows cancel
Given I follow "Blog post one"
When I follow "Delete"
Then I should see "Delete the blog entry 'Blog post one'?"
And I press "Cancel"
And I should see "Blog post one"
And I should see "Blog post two"
+563
View File
@@ -0,0 +1,563 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Events tests.
*
* @package core_blog
* @category test
* @copyright 2016 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_blog\event;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/blog/locallib.php');
require_once($CFG->dirroot . '/blog/lib.php');
/**
* Unit tests for the blog events.
*
* @copyright 2016 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class events_test extends \advanced_testcase {
/** @var $courseid */
private $courseid;
/** @var $cmid */
private $cmid;
/** @var $groupid */
private $groupid;
/** @var $userid */
private $userid;
/** @var $tagid */
private $tagid;
/** @var $postid */
private $postid;
/**
* Setup the tests.
*/
protected function setUp(): void {
global $DB;
parent::setUp();
$this->resetAfterTest();
// Create default course.
$course = $this->getDataGenerator()->create_course(array('category' => 1, 'shortname' => 'ANON'));
$this->assertNotEmpty($course);
$page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
$this->assertNotEmpty($page);
// Create default group.
$group = new \stdClass();
$group->courseid = $course->id;
$group->name = 'ANON';
$group->id = $DB->insert_record('groups', $group);
// Create default user.
$user = $this->getDataGenerator()->create_user(array(
'username' => 'testuser',
'firstname' => 'Jimmy',
'lastname' => 'Kinnon'
));
// Create default tag.
$tag = $this->getDataGenerator()->create_tag(array('userid' => $user->id,
'rawname' => 'Testtagname', 'isstandard' => 1));
// Create default post.
$post = new \stdClass();
$post->userid = $user->id;
$post->groupid = $group->id;
$post->content = 'test post content text';
$post->module = 'blog';
$post->id = $DB->insert_record('post', $post);
// Grab important ids.
$this->courseid = $course->id;
$this->cmid = $page->cmid;
$this->groupid = $group->id;
$this->userid = $user->id;
$this->tagid = $tag->id;
$this->postid = $post->id;
}
/**
* Test various blog related events.
*/
public function test_blog_entry_created_event(): void {
global $USER;
$this->setAdminUser();
$this->resetAfterTest();
// Create a blog entry for another user as Admin.
$sink = $this->redirectEvents();
$blog = new \blog_entry();
$blog->subject = "Subject of blog";
$blog->userid = $this->userid;
$states = \blog_entry::get_applicable_publish_states();
$blog->publishstate = reset($states);
$blog->add();
$events = $sink->get_events();
$sink->close();
$event = reset($events);
$sitecontext = \context_system::instance();
// Validate event data.
$this->assertInstanceOf('\core\event\blog_entry_created', $event);
$url = new \moodle_url('/blog/index.php', array('entryid' => $event->objectid));
$this->assertEquals($url, $event->get_url());
$this->assertEquals($sitecontext->id, $event->contextid);
$this->assertEquals($blog->id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($this->userid, $event->relateduserid);
$this->assertEquals("post", $event->objecttable);
$this->assertEventContextNotUsed($event);
}
/**
* Tests for event blog_entry_updated.
*/
public function test_blog_entry_updated_event(): void {
global $USER;
$this->setAdminUser();
$this->resetAfterTest();
$sitecontext = \context_system::instance();
// Edit a blog entry as Admin.
$blog = new \blog_entry($this->postid);
$sink = $this->redirectEvents();
$blog->summary_editor = array('text' => 'Something', 'format' => FORMAT_MOODLE);
$blog->edit(array(), null, array(), array());
$events = $sink->get_events();
$event = array_pop($events);
$sink->close();
// Validate event data.
$this->assertInstanceOf('\core\event\blog_entry_updated', $event);
$url = new \moodle_url('/blog/index.php', array('entryid' => $event->objectid));
$this->assertEquals($url, $event->get_url());
$this->assertEquals($sitecontext->id, $event->contextid);
$this->assertEquals($blog->id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($this->userid, $event->relateduserid);
$this->assertEquals("post", $event->objecttable);
$this->assertEventContextNotUsed($event);
}
/**
* Tests for event blog_entry_deleted.
*/
public function test_blog_entry_deleted_event(): void {
global $USER, $DB;
$this->setAdminUser();
$this->resetAfterTest();
$sitecontext = \context_system::instance();
// Delete a user blog entry as Admin.
$blog = new \blog_entry($this->postid);
$sink = $this->redirectEvents();
$record = $DB->get_record('post', array('id' => $blog->id));
$blog->delete();
$events = $sink->get_events();
$event = array_pop($events);
$sink->close();
// Validate event data.
$this->assertInstanceOf('\core\event\blog_entry_deleted', $event);
$this->assertEquals(null, $event->get_url());
$this->assertEquals($sitecontext->id, $event->contextid);
$this->assertEquals($blog->id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($this->userid, $event->relateduserid);
$this->assertEquals("post", $event->objecttable);
$this->assertEquals($record, $event->get_record_snapshot("post", $blog->id));
$this->assertEventContextNotUsed($event);
}
/**
* Tests for event blog_association_deleted.
*/
public function test_blog_association_deleted_event(): void {
global $USER;
$this->setAdminUser();
$this->resetAfterTest();
$sitecontext = \context_system::instance();
$coursecontext = \context_course::instance($this->courseid);
$contextmodule = \context_module::instance($this->cmid);
// Add blog associations with a course.
$blog = new \blog_entry($this->postid);
$blog->add_association($coursecontext->id);
$sink = $this->redirectEvents();
$blog->remove_associations();
$events = $sink->get_events();
$event = reset($events);
$sink->close();
// Validate event data.
$this->assertInstanceOf('\core\event\blog_association_deleted', $event);
$this->assertEquals($sitecontext->id, $event->contextid);
$this->assertEquals($blog->id, $event->other['blogid']);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($this->userid, $event->relateduserid);
$this->assertEquals('blog_association', $event->objecttable);
// Add blog associations with a module.
$blog = new \blog_entry($this->postid);
$blog->add_association($contextmodule->id);
$sink = $this->redirectEvents();
$blog->remove_associations();
$events = $sink->get_events();
$event = reset($events);
$sink->close();
// Validate event data.
$this->assertEquals($blog->id, $event->other['blogid']);
$this->assertEquals($USER->id, $event->userid);
$this->assertEventContextNotUsed($event);
}
/**
* Tests for event blog_association_created.
*/
public function test_blog_association_created_event(): void {
global $USER;
$this->setAdminUser();
$this->resetAfterTest();
$sitecontext = \context_system::instance();
$coursecontext = \context_course::instance($this->courseid);
$contextmodule = \context_module::instance($this->cmid);
// Add blog associations with a course.
$blog = new \blog_entry($this->postid);
$sink = $this->redirectEvents();
$blog->add_association($coursecontext->id);
$events = $sink->get_events();
$event = reset($events);
$sink->close();
// Validate event data.
$this->assertInstanceOf('\core\event\blog_association_created', $event);
$this->assertEquals($sitecontext->id, $event->contextid);
$url = new \moodle_url('/blog/index.php', array('entryid' => $event->other['blogid']));
$this->assertEquals($url, $event->get_url());
$this->assertEquals($blog->id, $event->other['blogid']);
$this->assertEquals($this->courseid, $event->other['associateid']);
$this->assertEquals('course', $event->other['associatetype']);
$this->assertEquals($blog->subject, $event->other['subject']);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($this->userid, $event->relateduserid);
$this->assertEquals('blog_association', $event->objecttable);
// Add blog associations with a module.
$blog = new \blog_entry($this->postid);
$sink = $this->redirectEvents();
$blog->add_association($contextmodule->id);
$events = $sink->get_events();
$event = reset($events);
$sink->close();
// Validate event data.
$this->assertEquals($blog->id, $event->other['blogid']);
$this->assertEquals($this->cmid, $event->other['associateid']);
$this->assertEquals('coursemodule', $event->other['associatetype']);
$this->assertEventContextNotUsed($event);
}
/**
* Tests for event blog_association_created validations.
*/
public function test_blog_association_created_event_validations(): void {
$this->resetAfterTest();
// Make sure associatetype validations work.
try {
\core\event\blog_association_created::create(array(
'contextid' => 1,
'objectid' => 3,
'relateduserid' => 2,
'other' => array('associateid' => 2 , 'blogid' => 3, 'subject' => 'blog subject')));
} catch (\coding_exception $e) {
$this->assertStringContainsString('The \'associatetype\' value must be set in other and be a valid type.', $e->getMessage());
}
try {
\core\event\blog_association_created::create(array(
'contextid' => 1,
'objectid' => 3,
'relateduserid' => 2,
'other' => array('associateid' => 2 , 'blogid' => 3, 'associatetype' => 'random', 'subject' => 'blog subject')));
} catch (\coding_exception $e) {
$this->assertStringContainsString('The \'associatetype\' value must be set in other and be a valid type.', $e->getMessage());
}
// Make sure associateid validations work.
try {
\core\event\blog_association_created::create(array(
'contextid' => 1,
'objectid' => 3,
'relateduserid' => 2,
'other' => array('blogid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject')));
} catch (\coding_exception $e) {
$this->assertStringContainsString('The \'associateid\' value must be set in other.', $e->getMessage());
}
// Make sure blogid validations work.
try {
\core\event\blog_association_created::create(array(
'contextid' => 1,
'objectid' => 3,
'relateduserid' => 2,
'other' => array('associateid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject')));
} catch (\coding_exception $e) {
$this->assertStringContainsString('The \'blogid\' value must be set in other.', $e->getMessage());
}
// Make sure blogid validations work.
try {
\core\event\blog_association_created::create(array(
'contextid' => 1,
'objectid' => 3,
'relateduserid' => 2,
'other' => array('blogid' => 3, 'associateid' => 3, 'associatetype' => 'course')));
} catch (\coding_exception $e) {
$this->assertStringContainsString('The \'subject\' value must be set in other.', $e->getMessage());
}
}
/**
* Tests for event blog_entries_viewed.
*/
public function test_blog_entries_viewed_event(): void {
$this->setAdminUser();
$other = array('entryid' => $this->postid, 'tagid' => $this->tagid, 'userid' => $this->userid, 'modid' => $this->cmid,
'groupid' => $this->groupid, 'courseid' => $this->courseid, 'search' => 'search', 'fromstart' => 2);
// Trigger event.
$sink = $this->redirectEvents();
$eventparams = array('other' => $other);
$eventinst = \core\event\blog_entries_viewed::create($eventparams);
$eventinst->trigger();
$events = $sink->get_events();
$event = reset($events);
$sink->close();
// Validate event data.
$url = new \moodle_url('/blog/index.php', $other);
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);
}
/**
* Test comment_created event.
*/
public function test_blog_comment_created_event(): void {
global $USER, $CFG;
$this->setAdminUser();
require_once($CFG->dirroot . '/comment/lib.php');
$context = \context_user::instance($USER->id);
$cmt = new \stdClass();
$cmt->context = $context;
$cmt->courseid = $this->courseid;
$cmt->area = 'format_blog';
$cmt->itemid = $this->postid;
$cmt->showcount = 1;
$cmt->component = 'blog';
$manager = new \comment($cmt);
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$manager->add("New comment");
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\core\event\blog_comment_created', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEquals($this->postid, $event->other['itemid']);
$url = new \moodle_url('/blog/index.php', array('entryid' => $this->postid));
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);
}
/**
* Test comment_deleted event.
*/
public function test_blog_comment_deleted_event(): void {
global $USER, $CFG;
$this->setAdminUser();
require_once($CFG->dirroot . '/comment/lib.php');
$context = \context_user::instance($USER->id);
$cmt = new \stdClass();
$cmt->context = $context;
$cmt->courseid = $this->courseid;
$cmt->area = 'format_blog';
$cmt->itemid = $this->postid;
$cmt->showcount = 1;
$cmt->component = 'blog';
$manager = new \comment($cmt);
$newcomment = $manager->add("New comment");
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$manager->delete($newcomment->id);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\core\event\blog_comment_deleted', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEquals($this->postid, $event->other['itemid']);
$url = new \moodle_url('/blog/index.php', array('entryid' => $this->postid));
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);
}
/**
* Test external blog added event.
*
* There is no external API for this, so the unit test will simply
* create and trigger the event and ensure data is returned as expected.
*/
public function test_external_blog_added_event(): void {
// Trigger an event: external blog added.
$eventparams = array(
'context' => $context = \context_system::instance(),
'objectid' => 1001,
'other' => array('url' => 'http://moodle.org')
);
$event = \core\event\blog_external_added::create($eventparams);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\blog_external_added', $event);
$this->assertEquals(1001, $event->objectid);
$this->assertEquals('http://moodle.org', $event->other['url']);
$this->assertDebuggingNotCalled();
}
/**
* Test external blog updated event.
*
* There is no external API for this, so the unit test will simply
* create and trigger the event and ensure data is returned as expected.
*/
public function test_external_blog_updated_event(): void {
// Trigger an event: external blog updated.
$eventparams = array(
'context' => $context = \context_system::instance(),
'objectid' => 1001,
'other' => array('url' => 'http://moodle.org')
);
$event = \core\event\blog_external_updated::create($eventparams);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\blog_external_updated', $event);
$this->assertEquals(1001, $event->objectid);
$this->assertEquals('http://moodle.org', $event->other['url']);
$this->assertDebuggingNotCalled();
}
/**
* Test external blog removed event.
*
* There is no external API for this, so the unit test will simply
* create and trigger the event and ensure data is returned as expected.
*/
public function test_external_blog_removed_event(): void {
// Trigger an event: external blog removed.
$eventparams = array(
'context' => $context = \context_system::instance(),
'objectid' => 1001,
);
$event = \core\event\blog_external_removed::create($eventparams);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\blog_external_removed', $event);
$this->assertEquals(1001, $event->objectid);
$this->assertDebuggingNotCalled();
}
/**
* Test external blogs viewed event.
*
* There is no external API for this, so the unit test will simply
* create and trigger the event and ensure data is returned as expected.
*/
public function test_external_blogs_viewed_event(): void {
// Trigger an event: external blogs viewed.
$eventparams = array(
'context' => $context = \context_system::instance(),
);
$event = \core\event\blog_external_viewed::create($eventparams);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\blog_external_viewed', $event);
$this->assertDebuggingNotCalled();
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,51 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Behat data generator for core_blog.
*
* @package core_blog
* @category test
* @copyright 2022 Noel De Martin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Behat data generator for core_blog.
*
* @package core_blog
* @category test
* @copyright 2022 Noel De Martin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_core_blog_generator extends behat_generator_base {
/**
* Get a list of the entities that can be created.
*
* @return array entity name => information about how to generate.
*/
protected function get_creatable_entities(): array {
return [
'entries' => [
'singular' => 'entry',
'datagenerator' => 'entry',
'required' => ['subject', 'body'],
'switchids' => ['user' => 'userid'],
],
];
}
}
+55
View File
@@ -0,0 +1,55 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Generator for blog area.
*
* @package core_blog
* @category test
* @copyright 2022 Noel De Martin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/blog/locallib.php');
/**
* Blog module test data generator class
*
* @package core_blog
* @category test
* @copyright 2022 Noel De Martin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_blog_generator extends component_generator_base {
/**
* Create a blog entry
*
* @param array $data Entry data.
* @return blog_entry Entry instance.
*/
public function create_entry(array $data = []): blog_entry {
$data['publishstate'] = $data['publishstate'] ?? 'site';
$data['summary'] = $data['summary'] ?? $data['body'];
$entry = new blog_entry(null, $data);
$entry->add();
return $entry;
}
}
+285
View File
@@ -0,0 +1,285 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for blog
*
* @package core_blog
* @category phpunit
* @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_blog;
use blog_listing;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/blog/locallib.php');
require_once($CFG->dirroot . '/blog/lib.php');
/**
* Test functions that rely on the DB tables
*/
class lib_test extends \advanced_testcase {
private $courseid;
private $cmid;
private $groupid;
private $userid;
private $tagid;
private $postid;
protected function setUp(): void {
global $DB;
parent::setUp();
$this->resetAfterTest();
// Create default course.
$course = $this->getDataGenerator()->create_course(array('category' => 1, 'shortname' => 'ANON'));
$this->assertNotEmpty($course);
$page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
$this->assertNotEmpty($page);
// Create default group.
$group = new \stdClass();
$group->courseid = $course->id;
$group->name = 'ANON';
$group->id = $DB->insert_record('groups', $group);
// Create default user.
$user = $this->getDataGenerator()->create_user(array(
'username' => 'testuser',
'firstname' => 'Jimmy',
'lastname' => 'Kinnon'
));
// Create default tag.
$tag = $this->getDataGenerator()->create_tag(array('userid' => $user->id,
'rawname' => 'Testtagname', 'isstandard' => 1));
// Create default post.
$post = new \stdClass();
$post->userid = $user->id;
$post->groupid = $group->id;
$post->content = 'test post content text';
$post->module = 'blog';
$post->id = $DB->insert_record('post', $post);
// Grab important ids.
$this->courseid = $course->id;
$this->cmid = $page->cmid;
$this->groupid = $group->id;
$this->userid = $user->id;
$this->tagid = $tag->id;
$this->postid = $post->id;
}
public function test_overrides(): void {
global $SITE;
// Try all the filters at once: Only the entry filter is active.
$filters = array('site' => $SITE->id, 'course' => $this->courseid, 'module' => $this->cmid,
'group' => $this->groupid, 'user' => $this->userid, 'tag' => $this->tagid, 'entry' => $this->postid);
$bloglisting = new blog_listing($filters);
$this->assertFalse(array_key_exists('site', $bloglisting->filters));
$this->assertFalse(array_key_exists('course', $bloglisting->filters));
$this->assertFalse(array_key_exists('module', $bloglisting->filters));
$this->assertFalse(array_key_exists('group', $bloglisting->filters));
$this->assertFalse(array_key_exists('user', $bloglisting->filters));
$this->assertFalse(array_key_exists('tag', $bloglisting->filters));
$this->assertTrue(array_key_exists('entry', $bloglisting->filters));
// Again, but without the entry filter: This time, the tag, user and module filters are active.
$filters = array('site' => $SITE->id, 'course' => $this->courseid, 'module' => $this->cmid,
'group' => $this->groupid, 'user' => $this->userid, 'tag' => $this->postid);
$bloglisting = new blog_listing($filters);
$this->assertFalse(array_key_exists('site', $bloglisting->filters));
$this->assertFalse(array_key_exists('course', $bloglisting->filters));
$this->assertFalse(array_key_exists('group', $bloglisting->filters));
$this->assertTrue(array_key_exists('module', $bloglisting->filters));
$this->assertTrue(array_key_exists('user', $bloglisting->filters));
$this->assertTrue(array_key_exists('tag', $bloglisting->filters));
// We should get the same result by removing the 3 inactive filters: site, course and group.
$filters = array('module' => $this->cmid, 'user' => $this->userid, 'tag' => $this->tagid);
$bloglisting = new blog_listing($filters);
$this->assertFalse(array_key_exists('site', $bloglisting->filters));
$this->assertFalse(array_key_exists('course', $bloglisting->filters));
$this->assertFalse(array_key_exists('group', $bloglisting->filters));
$this->assertTrue(array_key_exists('module', $bloglisting->filters));
$this->assertTrue(array_key_exists('user', $bloglisting->filters));
$this->assertTrue(array_key_exists('tag', $bloglisting->filters));
}
// The following series of 'test_blog..' functions correspond to the blog_get_headers() function within blog/lib.php.
// Some cases are omitted due to the optional_param variables used.
public function test_blog_get_headers_case_1(): void {
global $CFG, $PAGE, $OUTPUT;
$blogheaders = blog_get_headers();
$this->assertEquals($blogheaders['heading'], get_string('siteblogheading', 'blog'));
}
public function test_blog_get_headers_case_6(): void {
global $CFG, $PAGE, $OUTPUT;
$blogheaders = blog_get_headers($this->courseid, null, $this->userid);
$this->assertNotEquals($blogheaders['heading'], '');
}
public function test_blog_get_headers_case_7(): void {
global $CFG, $PAGE, $OUTPUT;
$blogheaders = blog_get_headers(null, $this->groupid);
$this->assertNotEquals($blogheaders['heading'], '');
}
public function test_blog_get_headers_case_10(): void {
global $CFG, $PAGE, $OUTPUT;
$blogheaders = blog_get_headers($this->courseid);
$this->assertNotEquals($blogheaders['heading'], '');
}
/**
* Tests the core_blog_myprofile_navigation() function.
*/
public function test_core_blog_myprofile_navigation(): void {
global $USER;
// Set up the test.
$tree = new \core_user\output\myprofile\tree();
$this->setAdminUser();
$iscurrentuser = true;
$course = null;
// Enable blogs.
set_config('enableblogs', true);
// Check the node tree is correct.
core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
$reflector = new \ReflectionObject($tree);
$nodes = $reflector->getProperty('nodes');
$this->assertArrayHasKey('blogs', $nodes->getValue($tree));
}
/**
* Tests the core_blog_myprofile_navigation() function as a guest.
*/
public function test_core_blog_myprofile_navigation_as_guest(): void {
global $USER;
// Set up the test.
$tree = new \core_user\output\myprofile\tree();
$iscurrentuser = false;
$course = null;
// Set user as guest.
$this->setGuestUser();
// Check the node tree is correct.
core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
$reflector = new \ReflectionObject($tree);
$nodes = $reflector->getProperty('nodes');
$this->assertArrayNotHasKey('blogs', $nodes->getValue($tree));
}
/**
* Tests the core_blog_myprofile_navigation() function when blogs are disabled.
*/
public function test_core_blog_myprofile_navigation_blogs_disabled(): void {
global $USER;
// Set up the test.
$tree = new \core_user\output\myprofile\tree();
$this->setAdminUser();
$iscurrentuser = false;
$course = null;
// Disable blogs.
set_config('enableblogs', false);
// Check the node tree is correct.
core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
$reflector = new \ReflectionObject($tree);
$nodes = $reflector->getProperty('nodes');
$this->assertArrayNotHasKey('blogs', $nodes->getValue($tree));
}
public function test_blog_get_listing_course(): void {
$this->setAdminUser();
$coursecontext = \context_course::instance($this->courseid);
$anothercourse = $this->getDataGenerator()->create_course();
// Add blog associations with a course.
$blog = new \blog_entry($this->postid);
$blog->add_association($coursecontext->id);
// There is one entry associated with a course.
$bloglisting = new blog_listing(array('course' => $this->courseid));
$this->assertCount(1, $bloglisting->get_entries());
// There is no entry associated with a wrong course.
$bloglisting = new blog_listing(array('course' => $anothercourse->id));
$this->assertCount(0, $bloglisting->get_entries());
// There is no entry associated with a module.
$bloglisting = new blog_listing(array('module' => $this->cmid));
$this->assertCount(0, $bloglisting->get_entries());
// There is one entry associated with a site (id is ignored).
$bloglisting = new blog_listing(array('site' => 12345));
$this->assertCount(1, $bloglisting->get_entries());
// There is one entry associated with course context.
$bloglisting = new blog_listing(array('context' => $coursecontext->id));
$this->assertCount(1, $bloglisting->get_entries());
}
public function test_blog_get_listing_module(): void {
$this->setAdminUser();
$coursecontext = \context_course::instance($this->courseid);
$contextmodule = \context_module::instance($this->cmid);
$anothermodule = $this->getDataGenerator()->create_module('page', array('course' => $this->courseid));
// Add blog associations with a course.
$blog = new \blog_entry($this->postid);
$blog->add_association($contextmodule->id);
// There is no entry associated with a course.
$bloglisting = new blog_listing(array('course' => $this->courseid));
$this->assertCount(0, $bloglisting->get_entries());
// There is one entry associated with a module.
$bloglisting = new blog_listing(array('module' => $this->cmid));
$this->assertCount(1, $bloglisting->get_entries());
// There is no entry associated with a wrong module.
$bloglisting = new blog_listing(array('module' => $anothermodule->cmid));
$this->assertCount(0, $bloglisting->get_entries());
// There is one entry associated with a site (id is ignored).
$bloglisting = new blog_listing(array('site' => 12345));
$this->assertCount(1, $bloglisting->get_entries());
// There is one entry associated with course context (module is a subcontext of a course).
$bloglisting = new blog_listing(array('context' => $coursecontext->id));
$this->assertCount(1, $bloglisting->get_entries());
}
}
+933
View File
@@ -0,0 +1,933 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Data provider tests.
*
* @package core_blog
* @category test
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_blog\privacy;
defined('MOODLE_INTERNAL') || die();
global $CFG;
use core_privacy\tests\provider_testcase;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\transform;
use core_privacy\local\request\writer;
use core_blog\privacy\provider;
require_once($CFG->dirroot . '/blog/locallib.php');
require_once($CFG->dirroot . '/comment/lib.php');
/**
* Data provider testcase class.
*
* @package core_blog
* @category test
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider_test extends provider_testcase {
public function setUp(): void {
$this->resetAfterTest();
}
public function test_get_contexts_for_userid(): void {
$dg = $this->getDataGenerator();
$c1 = $dg->create_course();
$c2 = $dg->create_course();
$c3 = $dg->create_course();
$cm1a = $dg->create_module('page', ['course' => $c1]);
$cm1b = $dg->create_module('page', ['course' => $c1]);
$cm2a = $dg->create_module('page', ['course' => $c2]);
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$u1ctx = \context_user::instance($u1->id);
// Blog share a table with notes, so throw data in there and make sure it doesn't get reported.
$dg->get_plugin_generator('core_notes')->create_instance(['userid' => $u1->id, 'courseid' => $c3->id]);
$this->assertEmpty(provider::get_contexts_for_userid($u1->id)->get_contextids());
$this->assertEmpty(provider::get_contexts_for_userid($u2->id)->get_contextids());
// Gradually create blog posts for user 1. First system one.
$this->create_post(['userid' => $u1->id]);
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(1, $contextids);
$this->assertEquals($u1ctx->id, $contextids[0]);
$this->assertEmpty(provider::get_contexts_for_userid($u2->id)->get_contextids());
// Create a blog post associated with c1.
$post = $this->create_post(['userid' => $u1->id, 'courseid' => $c1->id]);
$entry = new \blog_entry($post->id);
$entry->add_association(\context_course::instance($c1->id)->id);
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(2, $contextids);
$this->assertTrue(in_array($u1ctx->id, $contextids));
$this->assertTrue(in_array(\context_course::instance($c1->id)->id, $contextids));
$this->assertEmpty(provider::get_contexts_for_userid($u2->id)->get_contextids());
// Create a blog post associated with cm2a.
$post = $this->create_post(['userid' => $u1->id, 'courseid' => $c2->id]);
$entry = new \blog_entry($post->id);
$entry->add_association(\context_module::instance($cm2a->cmid)->id);
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(3, $contextids);
$this->assertTrue(in_array($u1ctx->id, $contextids));
$this->assertTrue(in_array(\context_course::instance($c1->id)->id, $contextids));
$this->assertTrue(in_array(\context_module::instance($cm2a->cmid)->id, $contextids));
$this->assertEmpty(provider::get_contexts_for_userid($u2->id)->get_contextids());
// User 2 comments on u1's post.
$comment = $this->get_comment_object($u1ctx, $post->id);
$this->setUser($u2);
$comment->add('Hello, it\'s me!');
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(3, $contextids);
$this->assertTrue(in_array($u1ctx->id, $contextids));
$this->assertTrue(in_array(\context_course::instance($c1->id)->id, $contextids));
$this->assertTrue(in_array(\context_module::instance($cm2a->cmid)->id, $contextids));
$contextids = provider::get_contexts_for_userid($u2->id)->get_contextids();
$this->assertCount(1, $contextids);
$this->assertTrue(in_array($u1ctx->id, $contextids));
}
public function test_get_contexts_for_userid_with_one_associated_post_only(): void {
$dg = $this->getDataGenerator();
$c1 = $dg->create_course();
$u1 = $dg->create_user();
$u1ctx = \context_user::instance($u1->id);
$this->assertEmpty(provider::get_contexts_for_userid($u1->id)->get_contextids());
// Create a blog post associated with c1. It should always return both the course and user context.
$post = $this->create_post(['userid' => $u1->id, 'courseid' => $c1->id]);
$entry = new \blog_entry($post->id);
$entry->add_association(\context_course::instance($c1->id)->id);
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(2, $contextids);
$this->assertTrue(in_array($u1ctx->id, $contextids));
$this->assertTrue(in_array(\context_course::instance($c1->id)->id, $contextids));
}
/**
* Test that user IDs are returned for a specificed course or module context.
*/
public function test_get_users_in_context_course_and_module(): void {
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$c1ctx = \context_course::instance($course->id);
$post = $this->create_post(['userid' => $user1->id, 'courseid' => $course->id]);
$entry = new \blog_entry($post->id);
$entry->add_association($c1ctx->id);
// Add a comment from user 2.
$comment = $this->get_comment_object(\context_user::instance($user1->id), $entry->id);
$this->setUser($user2);
$comment->add('Nice blog post');
$userlist = new \core_privacy\local\request\userlist($c1ctx, 'core_blog');
provider::get_users_in_context($userlist);
$userids = $userlist->get_userids();
$this->assertCount(2, $userids);
// Add an association for a module.
$cm1a = $this->getDataGenerator()->create_module('page', ['course' => $course]);
$cm1ctx = \context_module::instance($cm1a->cmid);
$post2 = $this->create_post(['userid' => $user2->id, 'courseid' => $course->id]);
$entry2 = new \blog_entry($post2->id);
$entry2->add_association($cm1ctx->id);
$userlist = new \core_privacy\local\request\userlist($cm1ctx, 'core_blog');
provider::get_users_in_context($userlist);
$userids = $userlist->get_userids();
$this->assertCount(1, $userids);
}
/**
* Test that user IDs are returned for a specificed user context.
*/
public function test_get_users_in_context_user_context(): void {
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$u1ctx = \context_user::instance($user1->id);
$post = $this->create_post(['userid' => $user1->id]);
$entry = new \blog_entry($post->id);
// Add a comment from user 2.
$comment = $this->get_comment_object($u1ctx, $entry->id);
$this->setUser($user2);
$comment->add('Another nice blog post');
$userlist = new \core_privacy\local\request\userlist($u1ctx, 'core_blog');
provider::get_users_in_context($userlist);
$userids = $userlist->get_userids();
$this->assertCount(2, $userids);
}
/**
* Test that user IDs are returned for a specificed user context for an external blog.
*/
public function test_get_users_in_context_external_blog(): void {
$user1 = $this->getDataGenerator()->create_user();
$u1ctx = \context_user::instance($user1->id);
$extu1 = $this->create_external_blog(['userid' => $user1->id]);
$userlist = new \core_privacy\local\request\userlist($u1ctx, 'core_blog');
provider::get_users_in_context($userlist);
$userids = $userlist->get_userids();
$this->assertCount(1, $userids);
}
public function test_delete_data_for_user(): void {
global $DB;
$dg = $this->getDataGenerator();
$c1 = $dg->create_course();
$c2 = $dg->create_course();
$cm1a = $dg->create_module('page', ['course' => $c1]);
$cm1b = $dg->create_module('page', ['course' => $c1]);
$cm2a = $dg->create_module('page', ['course' => $c2]);
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$u3 = $dg->create_user();
$c1ctx = \context_course::instance($c1->id);
$c2ctx = \context_course::instance($c2->id);
$cm1actx = \context_module::instance($cm1a->cmid);
$cm1bctx = \context_module::instance($cm1b->cmid);
$cm2actx = \context_module::instance($cm2a->cmid);
$u1ctx = \context_user::instance($u1->id);
$u2ctx = \context_user::instance($u2->id);
// Blog share a table with notes, so throw data in there and make sure it doesn't get deleted.
$this->assertFalse($DB->record_exists('post', ['courseid' => $c1->id, 'userid' => $u1->id, 'module' => 'notes']));
$dg->get_plugin_generator('core_notes')->create_instance(['userid' => $u1->id, 'courseid' => $c1->id]);
$this->assertTrue($DB->record_exists('post', ['courseid' => $c1->id, 'userid' => $u1->id, 'module' => 'notes']));
// Create two external blogs.
$extu1 = $this->create_external_blog(['userid' => $u1->id]);
$extu2 = $this->create_external_blog(['userid' => $u2->id]);
// Create a set of posts.
$entry = new \blog_entry($this->create_post(['userid' => $u1->id])->id);
$commentedon = $entry;
$entry = new \blog_entry($this->create_post(['userid' => $u2->id])->id);
// Two course associations for u1.
$entry = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c1->id])->id);
$entry->add_association($c1ctx->id);
$entry = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c1->id])->id);
$entry->add_association($c1ctx->id);
// Two module associations with cm1a, and 1 with cm1b for u1.
$entry = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c1->id])->id);
$entry->add_association($cm1actx->id);
$entry = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c1->id])->id);
$entry->add_association($cm1actx->id);
$entry = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c1->id])->id);
$entry->add_association($cm1bctx->id);
// One association for u2 in c1, cm1a and cm2a.
$entry = new \blog_entry($this->create_post(['userid' => $u2->id, 'courseid' => $c1->id])->id);
$entry->add_association($c1ctx->id);
$entry = new \blog_entry($this->create_post(['userid' => $u2->id, 'courseid' => $c1->id])->id);
$entry->add_association($cm1actx->id);
$entry = new \blog_entry($this->create_post(['userid' => $u2->id, 'courseid' => $c2->id])->id);
$entry->add_association($cm2actx->id);
// One association for u1 in c2 and cm2a.
$entry = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c2->id])->id);
$entry->add_association($c2ctx->id);
$entry = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c2->id])->id);
$entry->add_association($cm2actx->id);
// Add comments.
$comment = $this->get_comment_object($u1ctx, $commentedon->id);
$this->setUser($u1);
$comment->add('Hello, it\'s me!');
$comment->add('I was wondering...');
$this->setUser($u2);
$comment->add('If after all these years');
$this->setUser($u3);
$comment->add('You\'d like to meet');
// Assert current setup.
$this->assertCount(6, provider::get_contexts_for_userid($u1->id)->get_contextids());
$this->assertCount(9, $DB->get_records('post', ['userid' => $u1->id]));
$this->assertCount(5, provider::get_contexts_for_userid($u2->id)->get_contextids());
$this->assertCount(4, $DB->get_records('post', ['userid' => $u2->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u1->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u2->id]));
$this->assertCount(2, $DB->get_records('comments', ['userid' => $u1->id]));
$this->assertCount(1, $DB->get_records('comments', ['userid' => $u2->id]));
$this->assertCount(1, $DB->get_records('comments', ['userid' => $u3->id]));
// Delete for u1 in cm1a.
$appctxs = new approved_contextlist($u1, 'core_blog', [$cm1actx->id]);
provider::delete_data_for_user($appctxs);
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(5, $contextids);
$this->assertFalse(in_array($cm1actx->id, $contextids));
$this->assertCount(9, $DB->get_records('post', ['userid' => $u1->id]));
$this->assertCount(5, provider::get_contexts_for_userid($u2->id)->get_contextids());
$this->assertCount(4, $DB->get_records('post', ['userid' => $u2->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u1->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u2->id]));
$this->assertTrue($DB->record_exists('post', ['courseid' => $c1->id, 'userid' => $u1->id, 'module' => 'notes']));
// Delete for u1 in c1.
$appctxs = new approved_contextlist($u1, 'core_blog', [$c1ctx->id]);
provider::delete_data_for_user($appctxs);
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(4, $contextids);
$this->assertFalse(in_array($c1ctx->id, $contextids));
$this->assertCount(9, $DB->get_records('post', ['userid' => $u1->id]));
$this->assertCount(5, provider::get_contexts_for_userid($u2->id)->get_contextids());
$this->assertCount(4, $DB->get_records('post', ['userid' => $u2->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u1->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u2->id]));
$this->assertTrue($DB->record_exists('post', ['courseid' => $c1->id, 'userid' => $u1->id, 'module' => 'notes']));
// Delete for u1 in c2.
$appctxs = new approved_contextlist($u1, 'core_blog', [$c2ctx->id]);
provider::delete_data_for_user($appctxs);
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(3, $contextids);
$this->assertFalse(in_array($c2ctx->id, $contextids));
$this->assertCount(9, $DB->get_records('post', ['userid' => $u1->id]));
$this->assertCount(5, provider::get_contexts_for_userid($u2->id)->get_contextids());
$this->assertCount(4, $DB->get_records('post', ['userid' => $u2->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u1->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u2->id]));
$this->assertTrue($DB->record_exists('post', ['courseid' => $c1->id, 'userid' => $u1->id, 'module' => 'notes']));
// Delete for u1 in another user's context, shouldn't do anything.
provider::delete_data_for_user(new approved_contextlist($u1, 'core_blog', [$u2ctx->id]));
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(3, $contextids);
$this->assertFalse(in_array($c2ctx->id, $contextids));
$this->assertCount(9, $DB->get_records('post', ['userid' => $u1->id]));
$this->assertCount(5, provider::get_contexts_for_userid($u2->id)->get_contextids());
$this->assertCount(4, $DB->get_records('post', ['userid' => $u2->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u1->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u2->id]));
$this->assertTrue($DB->record_exists('post', ['courseid' => $c1->id, 'userid' => $u1->id, 'module' => 'notes']));
$this->assertCount(2, $DB->get_records('comments', ['userid' => $u1->id]));
$this->assertCount(1, $DB->get_records('comments', ['userid' => $u2->id]));
// Delete for u2 in u1 context.
provider::delete_data_for_user(new approved_contextlist($u2, 'core_blog', [$u1ctx->id]));
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(3, $contextids);
$this->assertFalse(in_array($c2ctx->id, $contextids));
$this->assertCount(9, $DB->get_records('post', ['userid' => $u1->id]));
$this->assertCount(4, provider::get_contexts_for_userid($u2->id)->get_contextids());
$this->assertCount(4, $DB->get_records('post', ['userid' => $u2->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u1->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u2->id]));
$this->assertTrue($DB->record_exists('post', ['courseid' => $c1->id, 'userid' => $u1->id, 'module' => 'notes']));
$this->assertCount(2, $DB->get_records('comments', ['userid' => $u1->id]));
$this->assertCount(0, $DB->get_records('comments', ['userid' => $u2->id]));
$this->assertCount(1, $DB->get_records('comments', ['userid' => $u3->id]));
// Delete for u1 in their context.
$appctxs = new approved_contextlist($u1, 'core_blog', [$u1ctx->id]);
provider::delete_data_for_user($appctxs);
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(0, $contextids);
$this->assertCount(1, $DB->get_records('post', ['userid' => $u1->id]));
$this->assertCount(4, provider::get_contexts_for_userid($u2->id)->get_contextids());
$this->assertCount(4, $DB->get_records('post', ['userid' => $u2->id]));
$this->assertCount(0, $DB->get_records('blog_external', ['userid' => $u1->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u2->id]));
$this->assertCount(0, $DB->get_records('comments', ['userid' => $u1->id]));
$this->assertCount(0, $DB->get_records('comments', ['userid' => $u2->id]));
$this->assertCount(0, $DB->get_records('comments', ['userid' => $u3->id]));
$this->assertTrue($DB->record_exists('post', ['courseid' => $c1->id, 'userid' => $u1->id, 'module' => 'notes']));
}
/**
* Test provider delete_data_for_user with a context that contains no entries
*
* @return void
*/
public function test_delete_data_for_user_empty_context(): void {
global $DB;
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$context = \context_course::instance($course->id);
// Create a blog entry for user, associated with course.
$entry = new \blog_entry($this->create_post(['userid' => $user->id, 'courseid' => $course->id])->id);
$entry->add_association($context->id);
// Generate list of contexts for user.
$contexts = provider::get_contexts_for_userid($user->id);
$this->assertContainsEquals($context->id, $contexts->get_contextids());
// Now delete the blog entry.
$entry->delete();
// Try to delete user data using contexts obtained prior to entry deletion.
$contextlist = new approved_contextlist($user, 'core_blog', $contexts->get_contextids());
provider::delete_data_for_user($contextlist);
// Sanity check to ensure blog_associations is really empty.
$this->assertEmpty($DB->get_records('blog_association', ['contextid' => $context->id]));
}
public function test_delete_data_for_all_users_in_context(): void {
global $DB;
$dg = $this->getDataGenerator();
$c1 = $dg->create_course();
$c2 = $dg->create_course();
$cm1a = $dg->create_module('page', ['course' => $c1]);
$cm1b = $dg->create_module('page', ['course' => $c1]);
$cm2a = $dg->create_module('page', ['course' => $c2]);
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$c1ctx = \context_course::instance($c1->id);
$c2ctx = \context_course::instance($c2->id);
$cm1actx = \context_module::instance($cm1a->cmid);
$cm1bctx = \context_module::instance($cm1b->cmid);
$cm2actx = \context_module::instance($cm2a->cmid);
$u1ctx = \context_user::instance($u1->id);
// Create two external blogs.
$extu1 = $this->create_external_blog(['userid' => $u1->id]);
$extu2 = $this->create_external_blog(['userid' => $u2->id]);
// Create a set of posts.
$entry = new \blog_entry($this->create_post(['userid' => $u1->id])->id);
$entry = new \blog_entry($this->create_post(['userid' => $u2->id])->id);
// Course associations for u1 and u2.
$entry = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c1->id])->id);
$entry->add_association($c1ctx->id);
$entry = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c1->id])->id);
$entry->add_association($c1ctx->id);
$entry = new \blog_entry($this->create_post(['userid' => $u2->id, 'courseid' => $c1->id])->id);
$entry->add_association($c1ctx->id);
// Module associations for u1 and u2.
$entry = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c1->id])->id);
$entry->add_association($cm1actx->id);
$entry = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c1->id])->id);
$entry->add_association($cm1actx->id);
$entry = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c1->id])->id);
$entry->add_association($cm1bctx->id);
$entry = new \blog_entry($this->create_post(['userid' => $u2->id, 'courseid' => $c1->id])->id);
$entry->add_association($cm1actx->id);
// Foreign associations for u1, u2.
$entry = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c2->id])->id);
$entry->add_association($c2ctx->id);
$entry = new \blog_entry($this->create_post(['userid' => $u2->id, 'courseid' => $c2->id])->id);
$entry->add_association($c2ctx->id);
$entry = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $cm2a->id])->id);
$entry->add_association($cm2actx->id);
// Validate what we've got.
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(8, $DB->get_records('post', ['userid' => $u1->id]));
$this->assertCount(6, $contextids);
$this->assertTrue(in_array($c1ctx->id, $contextids));
$this->assertTrue(in_array($c2ctx->id, $contextids));
$this->assertTrue(in_array($cm1actx->id, $contextids));
$this->assertTrue(in_array($cm1bctx->id, $contextids));
$this->assertTrue(in_array($cm2actx->id, $contextids));
$this->assertTrue(in_array($u1ctx->id, $contextids));
$contextids = provider::get_contexts_for_userid($u2->id)->get_contextids();
$this->assertCount(4, $DB->get_records('post', ['userid' => $u2->id]));
$this->assertCount(4, $contextids);
$this->assertTrue(in_array($c1ctx->id, $contextids));
$this->assertTrue(in_array($c2ctx->id, $contextids));
$this->assertTrue(in_array($cm1actx->id, $contextids));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u1->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u2->id]));
// Delete cm1a context.
provider::delete_data_for_all_users_in_context($cm1actx);
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(8, $DB->get_records('post', ['userid' => $u1->id]));
$this->assertCount(5, $contextids);
$this->assertTrue(in_array($c1ctx->id, $contextids));
$this->assertTrue(in_array($c2ctx->id, $contextids));
$this->assertFalse(in_array($cm1actx->id, $contextids));
$this->assertTrue(in_array($cm1bctx->id, $contextids));
$this->assertTrue(in_array($cm2actx->id, $contextids));
$this->assertTrue(in_array($u1ctx->id, $contextids));
$contextids = provider::get_contexts_for_userid($u2->id)->get_contextids();
$this->assertCount(4, $DB->get_records('post', ['userid' => $u2->id]));
$this->assertCount(3, $contextids);
$this->assertTrue(in_array($c1ctx->id, $contextids));
$this->assertTrue(in_array($c2ctx->id, $contextids));
$this->assertFalse(in_array($cm1actx->id, $contextids));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u1->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u2->id]));
// Delete c1 context.
provider::delete_data_for_all_users_in_context($c1ctx);
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(8, $DB->get_records('post', ['userid' => $u1->id]));
$this->assertCount(4, $contextids);
$this->assertFalse(in_array($c1ctx->id, $contextids));
$this->assertTrue(in_array($c2ctx->id, $contextids));
$this->assertFalse(in_array($cm1actx->id, $contextids));
$this->assertTrue(in_array($cm1bctx->id, $contextids));
$this->assertTrue(in_array($cm2actx->id, $contextids));
$this->assertTrue(in_array($u1ctx->id, $contextids));
$contextids = provider::get_contexts_for_userid($u2->id)->get_contextids();
$this->assertCount(4, $DB->get_records('post', ['userid' => $u2->id]));
$this->assertCount(2, $contextids);
$this->assertFalse(in_array($c1ctx->id, $contextids));
$this->assertTrue(in_array($c2ctx->id, $contextids));
$this->assertFalse(in_array($cm1actx->id, $contextids));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u1->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u2->id]));
// Delete u1 context.
provider::delete_data_for_all_users_in_context($u1ctx);
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(0, $DB->get_records('post', ['userid' => $u1->id]));
$this->assertCount(0, $contextids);
$this->assertFalse(in_array($c1ctx->id, $contextids));
$this->assertFalse(in_array($c2ctx->id, $contextids));
$this->assertFalse(in_array($cm1actx->id, $contextids));
$this->assertFalse(in_array($cm1bctx->id, $contextids));
$this->assertFalse(in_array($cm2actx->id, $contextids));
$this->assertFalse(in_array($u1ctx->id, $contextids));
$contextids = provider::get_contexts_for_userid($u2->id)->get_contextids();
$this->assertCount(4, $DB->get_records('post', ['userid' => $u2->id]));
$this->assertCount(2, $contextids);
$this->assertFalse(in_array($c1ctx->id, $contextids));
$this->assertTrue(in_array($c2ctx->id, $contextids));
$this->assertFalse(in_array($cm1actx->id, $contextids));
$this->assertCount(0, $DB->get_records('blog_external', ['userid' => $u1->id]));
$this->assertCount(1, $DB->get_records('blog_external', ['userid' => $u2->id]));
}
public function test_export_data_for_user(): void {
global $DB;
$dg = $this->getDataGenerator();
$c1 = $dg->create_course();
$cm1a = $dg->create_module('page', ['course' => $c1]);
$cm1b = $dg->create_module('page', ['course' => $c1]);
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$c1ctx = \context_course::instance($c1->id);
$cm1actx = \context_module::instance($cm1a->cmid);
$cm1bctx = \context_module::instance($cm1b->cmid);
$u1ctx = \context_user::instance($u1->id);
$u2ctx = \context_user::instance($u2->id);
// System entries.
$e1 = new \blog_entry($this->create_post(['userid' => $u1->id, 'subject' => 'Hello world!',
'publishstate' => 'public'])->id);
$e2 = new \blog_entry($this->create_post(['userid' => $u1->id, 'subject' => 'Hi planet!',
'publishstate' => 'draft'])->id);
$e3 = new \blog_entry($this->create_post(['userid' => $u2->id, 'subject' => 'Ignore me'])->id);
// Create a blog entry associated with contexts.
$e4 = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c1->id, 'subject' => 'Course assoc'])->id);
$e4->add_association($c1ctx->id);
$e4b = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c1->id, 'subject' => 'Course assoc 2'])->id);
$e4b->add_association($c1ctx->id);
$e5 = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c1->id, 'subject' => 'Module assoc',
'publishstate' => 'public'])->id);
$e5->add_association($cm1actx->id);
$e5b = new \blog_entry($this->create_post(['userid' => $u1->id, 'courseid' => $c1->id, 'subject' => 'C/CM assoc'])->id);
$e5b->add_association($c1ctx->id);
$e5b->add_association($cm1actx->id);
$e6 = new \blog_entry($this->create_post(['userid' => $u2->id, 'courseid' => $c1->id, 'subject' => 'Module assoc'])->id);
$e6->add_association($cm1actx->id);
// External blogs.
$ex1 = $this->create_external_blog(['userid' => $u1->id, 'url' => 'https://moodle.org', 'name' => 'Moodle RSS']);
$ex2 = $this->create_external_blog(['userid' => $u1->id, 'url' => 'https://example.com', 'name' => 'Example']);
$ex3 = $this->create_external_blog(['userid' => $u2->id, 'url' => 'https://example.com', 'name' => 'Ignore me']);
// Attach tags.
\core_tag_tag::set_item_tags('core', 'post', $e1->id, $u1ctx, ['Beer', 'Golf']);
\core_tag_tag::set_item_tags('core', 'blog_external', $ex1->id, $u1ctx, ['Car', 'Golf']);
\core_tag_tag::set_item_tags('core', 'post', $e3->id, $u2ctx, ['ITG']);
\core_tag_tag::set_item_tags('core', 'blog_external', $ex3->id, $u2ctx, ['DDR']);
\core_tag_tag::set_item_tags('core', 'dontfindme', $e1->id, $u1ctx, ['Lone tag']);
// Attach comments.
$comment = $this->get_comment_object($u1ctx, $e1->id);
$this->setUser($u1);
$comment->add('Hello, it\'s me!');
$this->setUser($u2);
$comment->add('I was wondering if after');
$this->setUser($u1);
$comment = $this->get_comment_object($u2ctx, $e3->id);
$comment->add('All these years');
// Blog share a table with notes, so throw some data in there, it should not be exported.
$note = $dg->get_plugin_generator('core_notes')->create_instance(['userid' => $u1->id, 'courseid' => $c1->id,
'subject' => 'ABC']);
// Validate module associations.
$contextlist = new approved_contextlist($u1, 'core_blog', [$cm1actx->id]);
provider::export_user_data($contextlist);
$writer = writer::with_context($cm1actx);
$assocs = $writer->get_data([get_string('privacy:path:blogassociations', 'core_blog')]);
$this->assertCount(2, $assocs->associations);
$this->assertTrue(in_array('Module assoc', $assocs->associations));
$this->assertTrue(in_array('C/CM assoc', $assocs->associations));
// Validate course associations.
$contextlist = new approved_contextlist($u1, 'core_blog', [$c1ctx->id]);
provider::export_user_data($contextlist);
$writer = writer::with_context($c1ctx);
$assocs = $writer->get_data([get_string('privacy:path:blogassociations', 'core_blog')]);
$this->assertCount(3, $assocs->associations);
$this->assertTrue(in_array('Course assoc', $assocs->associations));
$this->assertTrue(in_array('Course assoc 2', $assocs->associations));
$this->assertTrue(in_array('C/CM assoc', $assocs->associations));
// Confirm we're not exporting for another user.
$contextlist = new approved_contextlist($u2, 'core_blog', [$u2ctx->id]);
$writer = writer::with_context($u1ctx);
$this->assertFalse($writer->has_any_data());
// Now export user context for u2.
$this->setUser($u2);
$contextlist = new approved_contextlist($u2, 'core_blog', [$u1ctx->id]);
provider::export_user_data($contextlist);
$writer = writer::with_context($u1ctx);
$data = $writer->get_data([get_string('blog', 'core_blog'), get_string('externalblogs', 'core_blog'),
$ex1->name . " ({$ex1->id})"]);
$this->assertEmpty($data);
$data = $writer->get_data([get_string('blog', 'core_blog'), get_string('blogentries', 'core_blog'),
$e2->subject . " ({$e2->id})"]);
$this->assertEmpty($data);
$data = $writer->get_data([get_string('blog', 'core_blog'), get_string('blogentries', 'core_blog'),
$e1->subject . " ({$e1->id})"]);
$this->assertEmpty($data);
$data = $writer->get_data([get_string('blog', 'core_blog'), get_string('blogentries', 'core_blog'),
$e1->subject . " ({$e1->id})", get_string('commentsubcontext', 'core_comment')]);
$this->assertNotEmpty($data);
$this->assertCount(1, $data->comments);
$comment = array_shift($data->comments);
$this->assertEquals('I was wondering if after', strip_tags($comment->content));
// Now export user context data.
$this->setUser($u1);
$contextlist = new approved_contextlist($u1, 'core_blog', [$u1ctx->id]);
writer::reset();
provider::export_user_data($contextlist);
$writer = writer::with_context($u1ctx);
// Check external blogs.
$externals = [$ex1, $ex2];
foreach ($externals as $ex) {
$data = $writer->get_data([get_string('blog', 'core_blog'), get_string('externalblogs', 'core_blog'),
$ex->name . " ({$ex->id})"]);
$this->assertNotEmpty($data);
$this->assertEquals($data->name, $ex->name);
$this->assertEquals($data->description, $ex->description);
$this->assertEquals($data->url, $ex->url);
$this->assertEquals($data->filtertags, $ex->filtertags);
$this->assertEquals($data->modified, transform::datetime($ex->timemodified));
$this->assertEquals($data->lastfetched, transform::datetime($ex->timefetched));
}
// Check entries.
$entries = [$e1, $e2, $e4, $e4b, $e5, $e5b];
$associations = [
$e1->id => null,
$e2->id => null,
$e4->id => $c1ctx->get_context_name(),
$e4b->id => $c1ctx->get_context_name(),
$e5->id => $cm1actx->get_context_name(),
$e5b->id => [$c1ctx->get_context_name(), $cm1actx->get_context_name()],
];
foreach ($entries as $e) {
$path = [get_string('blog', 'core_blog'), get_string('blogentries', 'core_blog'), $e->subject . " ({$e->id})"];
$data = $writer->get_data($path);
$this->assertNotEmpty($data);
$this->assertEquals($data->subject, $e->subject);
$this->assertEquals($data->summary, $e->summary);
$this->assertEquals($data->publishstate, provider::transform_publishstate($e->publishstate));
$this->assertEquals($data->created, transform::datetime($e->created));
$this->assertEquals($data->lastmodified, transform::datetime($e->lastmodified));
// We attached comments and tags to this entry.
$commentpath = array_merge($path, [get_string('commentsubcontext', 'core_comment')]);
if ($e->id == $e1->id) {
$tagdata = $writer->get_related_data($path, 'tags');
$this->assertEqualsCanonicalizing(['Beer', 'Golf'], $tagdata);
$comments = $writer->get_data($commentpath);
$this->assertCount(2, $comments->comments);
$c0 = strip_tags($comments->comments[0]->content);
$c1 = strip_tags($comments->comments[1]->content);
$expectedcomments = [
'Hello, it\'s me!',
'I was wondering if after',
];
$this->assertNotFalse(array_search($c0, $expectedcomments));
$this->assertNotFalse(array_search($c1, $expectedcomments));
$this->assertNotEquals($c0, $c1);
} else {
$tagdata = $writer->get_related_data($path, 'tags');
$this->assertEmpty($tagdata);
$comments = $writer->get_data($commentpath);
$this->assertEmpty($comments);
}
if (isset($associations[$e->id])) {
$assocs = $associations[$e->id];
if (is_array($assocs)) {
$this->assertCount(count($assocs), $data->associations);
foreach ($assocs as $v) {
$this->assertTrue(in_array($v, $data->associations));
}
} else {
$this->assertCount(1, $data->associations);
$this->assertTrue(in_array($assocs, $data->associations));
}
}
}
// The note was not exported.
$path = [get_string('blog', 'core_blog'), get_string('blogentries', 'core_blog'), "ABC ($note->id)"];
$this->assertEmpty($writer->get_data($path));
}
/**
* Test that deleting of blog information in a user context works as desired.
*/
public function test_delete_data_for_users_user_context(): void {
global $DB;
$u1 = $this->getDataGenerator()->create_user();
$u2 = $this->getDataGenerator()->create_user();
$u3 = $this->getDataGenerator()->create_user();
$u4 = $this->getDataGenerator()->create_user();
$u5 = $this->getDataGenerator()->create_user();
$u1ctx = \context_user::instance($u1->id);
$post = $this->create_post(['userid' => $u1->id]);
$entry = new \blog_entry($post->id);
$comment = $this->get_comment_object($u1ctx, $entry->id);
$this->setUser($u1);
$comment->add('Hello, I created the blog');
$this->setUser($u2);
$comment->add('User 2 making a comment.');
$this->setUser($u3);
$comment->add('User 3 here.');
$this->setUser($u4);
$comment->add('User 4 is nice.');
$this->setUser($u5);
$comment->add('User 5 for the win.');
// This will only delete the comments made by user 4 and 5.
$this->assertCount(5, $DB->get_records('comments', ['contextid' => $u1ctx->id]));
$userlist = new \core_privacy\local\request\approved_userlist($u1ctx, 'core_blog', [$u4->id, $u5->id]);
provider::delete_data_for_users($userlist);
$this->assertCount(3, $DB->get_records('comments', ['contextid' => $u1ctx->id]));
$this->assertCount(1, $DB->get_records('post', ['userid' => $u1->id]));
// As the owner of the post is here everything will be deleted.
$userlist = new \core_privacy\local\request\approved_userlist($u1ctx, 'core_blog', [$u1->id, $u2->id]);
provider::delete_data_for_users($userlist);
$this->assertEmpty($DB->get_records('comments', ['contextid' => $u1ctx->id]));
$this->assertEmpty($DB->get_records('post', ['userid' => $u1->id]));
}
/**
* Test that deleting of an external blog in a user context works as desired.
*/
public function test_delete_data_for_users_external_blog(): void {
global $DB;
$u1 = $this->getDataGenerator()->create_user();
$u2 = $this->getDataGenerator()->create_user();
$u1ctx = \context_user::instance($u1->id);
$u2ctx = \context_user::instance($u2->id);
$post = $this->create_external_blog(['userid' => $u1->id, 'url' => 'https://moodle.org', 'name' => 'Moodle RSS']);
$post2 = $this->create_external_blog(['userid' => $u2->id, 'url' => 'https://moodle.com', 'name' => 'Some other thing']);
// Check that we have two external blogs created.
$this->assertCount(2, $DB->get_records('blog_external'));
// This will only delete the external blog for user 1.
$userlist = new \core_privacy\local\request\approved_userlist($u1ctx, 'core_blog', [$u1->id, $u2->id]);
provider::delete_data_for_users($userlist);
$this->assertCount(1, $DB->get_records('blog_external'));
}
public function test_delete_data_for_users_course_and_module_context(): void {
global $DB;
$u1 = $this->getDataGenerator()->create_user();
$u2 = $this->getDataGenerator()->create_user();
$u3 = $this->getDataGenerator()->create_user();
$u4 = $this->getDataGenerator()->create_user();
$u5 = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$module = $this->getDataGenerator()->create_module('page', ['course' => $course]);
$u1ctx = \context_user::instance($u1->id);
$u3ctx = \context_user::instance($u3->id);
$c1ctx = \context_course::instance($course->id);
$cm1ctx = \context_module::instance($module->cmid);
// Blog with course association.
$post1 = $this->create_post(['userid' => $u1->id, 'courseid' => $course->id]);
$entry1 = new \blog_entry($post1->id);
$entry1->add_association($c1ctx->id);
// Blog with module association.
$post2 = $this->create_post(['userid' => $u3->id, 'courseid' => $course->id]);
$entry2 = new \blog_entry($post2->id);
$entry2->add_association($cm1ctx->id);
$comment = $this->get_comment_object($u1ctx, $entry1->id);
$this->setUser($u1);
$comment->add('Hello, I created the blog');
$this->setUser($u2);
$comment->add('comment on first course blog');
$this->setUser($u4);
$comment->add('user 4 on course blog');
$comment = $this->get_comment_object($u3ctx, $entry2->id);
$this->setUser($u3);
$comment->add('Hello, I created the module blog');
$this->setUser($u2);
$comment->add('I am commenting on both');
$this->setUser($u5);
$comment->add('User 5 for modules');
$this->assertCount(6, $DB->get_records('comments', ['component' => 'blog']));
$this->assertCount(2, $DB->get_records('post', ['courseid' => $course->id]));
$this->assertCount(2, $DB->get_records('blog_association'));
// When using the course or module context we are only removing the blog associations and the comments.
$userlist = new \core_privacy\local\request\approved_userlist($c1ctx, 'core_blog', [$u2->id, $u1->id, $u5->id]);
provider::delete_data_for_users($userlist);
// Only one of the blog_associations should be removed. Everything else should be as before.
$this->assertCount(6, $DB->get_records('comments', ['component' => 'blog']));
$this->assertCount(2, $DB->get_records('post', ['courseid' => $course->id]));
$this->assertCount(1, $DB->get_records('blog_association'));
$userlist = new \core_privacy\local\request\approved_userlist($cm1ctx, 'core_blog', [$u2->id, $u1->id, $u3->id]);
provider::delete_data_for_users($userlist);
// Now we've removed the other association.
$this->assertCount(6, $DB->get_records('comments', ['component' => 'blog']));
$this->assertCount(2, $DB->get_records('post', ['courseid' => $course->id]));
$this->assertEmpty($DB->get_records('blog_association'));
}
/**
* Create a blog post.
*
* @param array $params The params.
* @return stdClass
*/
protected function create_post(array $params) {
global $DB;
$post = new \stdClass();
$post->module = 'blog';
$post->courseid = 0;
$post->subject = 'the test post';
$post->summary = 'test post summary text';
$post->summaryformat = FORMAT_PLAIN;
$post->publishstate = 'site';
$post->created = time() - HOURSECS;
$post->lastmodified = time();
foreach ($params as $key => $value) {
$post->{$key} = $value;
}
$post->id = $DB->insert_record('post', $post);
return $post;
}
/**
* Create an extenral blog.
*
* @param array $params The params.
* @return stdClass
*/
protected function create_external_blog(array $params) {
global $DB;
$post = new \stdClass();
$post->name = 'test external';
$post->description = 'the description';
$post->url = 'http://example.com';
$post->filtertags = 'a, c, b';
$post->timefetched = time() - HOURSECS;
$post->timemodified = time();
foreach ($params as $key => $value) {
$post->{$key} = $value;
}
$post->id = $DB->insert_record('blog_external', $post);
return $post;
}
/**
* Get the comment area.
*
* @param context $context The context.
* @param int $itemid The item ID.
* @param string $component The component.
* @param string $area The area.
* @return comment
*/
protected function get_comment_object(\context $context, $itemid) {
$args = new \stdClass();
$args->context = $context;
$args->course = get_course(SITEID);
$args->area = 'format_blog';
$args->itemid = $itemid;
$args->component = 'blog';
$comment = new \comment($args);
$comment->set_post_permission(true);
return $comment;
}
}
@@ -0,0 +1,301 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_blog\reportbuilder\datasource;
use context_system;
use context_user;
use core_blog_generator;
use core_comment_generator;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{boolean_select, date, select, text};
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for blogs datasource
*
* @package core_blog
* @covers \core_blog\reportbuilder\datasource\blogs
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class blogs_test extends core_reportbuilder_testcase {
/**
* Test default datasource
*/
public function test_datasource_default(): void {
$this->resetAfterTest();
/** @var core_blog_generator $blogsgenerator */
$blogsgenerator = $this->getDataGenerator()->get_plugin_generator('core_blog');
// Our first user will create a course blog.
$course = $this->getDataGenerator()->create_course();
$userone = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe']);
$courseblog = $blogsgenerator->create_entry(['publishstate' => 'site', 'userid' => $userone->id,
'subject' => 'Course', 'summary' => 'Course summary', 'courseid' => $course->id]);
// Our second user will create a personal and site blog.
$usertwo = $this->getDataGenerator()->create_user(['firstname' => 'Amy']);
$personalblog = $blogsgenerator->create_entry(['publishstate' => 'draft', 'userid' => $usertwo->id,
'subject' => 'Personal', 'summary' => 'Personal summary']);
$this->waitForSecond(); // For consistent ordering we need distinct time for second user blogs.
$siteblog = $blogsgenerator->create_entry(['publishstate' => 'public', 'userid' => $usertwo->id,
'subject' => 'Site', 'summary' => 'Site summary']);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Blogs', 'source' => blogs::class, 'default' => 1]);
$content = $this->get_custom_report_content($report->get('id'));
// Default columns are user, course, title, time created. Sorted by user and time created.
$this->assertEquals([
[fullname($usertwo), '', $personalblog->subject, userdate($personalblog->created)],
[fullname($usertwo), '', $siteblog->subject, userdate($siteblog->created)],
[fullname($userone), $course->fullname, $courseblog->subject, userdate($courseblog->created)],
], array_map('array_values', $content));
}
/**
* Test datasource columns that aren't added by default
*/
public function test_datasource_non_default_columns(): void {
global $DB;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
/** @var core_blog_generator $blogsgenerator */
$blogsgenerator = $this->getDataGenerator()->get_plugin_generator('core_blog');
$blog = $blogsgenerator->create_entry(['publishstate' => 'draft', 'userid' => $user->id, 'subject' => 'My blog',
'summary' => 'Horses', 'tags' => ['horse']]);
// Add an attachment.
$blog->attachment = 1;
get_file_storage()->create_file_from_string([
'contextid' => context_system::instance()->id,
'component' => 'blog',
'filearea' => 'attachment',
'itemid' => $blog->id,
'filepath' => '/',
'filename' => 'hello.txt',
], 'hello');
/** @var core_comment_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
$generator->create_comment([
'context' => context_user::instance($user->id),
'component' => 'blog',
'area' => 'format_blog',
'itemid' => $blog->id,
'content' => 'Cool',
]);
// Manually update the created/modified date of the blog.
$blog->created = 1654038000;
$blog->lastmodified = $blog->created + HOURSECS;
$DB->update_record('post', $blog);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Blogs', 'source' => blogs::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'blog:titlewithlink']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'blog:body']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'blog:attachment']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'blog:publishstate']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'blog:timemodified']);
// Tag entity (course/user presence already checked by default columns).
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'tag:name']);
// File entity.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:size']);
// Comment entity.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:content']);
// Commenter entity.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'commenter:fullname']);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(1, $content);
[
$link,
$body,
$attachment,
$publishstate,
$timemodified,
$tags,
$filesize,
$comment,
$commenter,
] = array_values($content[0]);
$this->assertEquals("<a href=\"https://www.example.com/moodle/blog/index.php?entryid={$blog->id}\">{$blog->subject}</a>",
$link);
$this->assertStringContainsString('Horses', $body);
$this->assertStringContainsString('hello.txt', $attachment);
$this->assertEquals('Draft', $publishstate);
$this->assertEquals(userdate($blog->lastmodified), $timemodified);
$this->assertEquals('horse', $tags);
$this->assertEquals("5\xc2\xa0bytes", $filesize);
$this->assertEquals(format_text('Cool'), $comment);
$this->assertEquals(fullname($user), $commenter);
}
/**
* Data provider for {@see test_datasource_filters}
*
* @return array[]
*/
public function datasource_filters_provider(): array {
return [
'Filter title' => ['subject', 'Cool', 'blog:title', [
'blog:title_operator' => text::CONTAINS,
'blog:title_value' => 'Cool',
], true],
'Filter title (no match)' => ['subject', 'Cool', 'blog:title', [
'blog:title_operator' => text::CONTAINS,
'blog:title_value' => 'Beans',
], false],
'Filter body' => ['summary', 'Awesome', 'blog:body', [
'blog:body_operator' => select::EQUAL_TO,
'blog:body_value' => 'Awesome',
], true],
'Filter body (no match)' => ['summary', 'Awesome', 'blog:body', [
'blog:body_operator' => select::EQUAL_TO,
'blog:body_value' => 'Beans',
], false],
'Filter attachment' => ['attachment', 1, 'blog:attachment', [
'blog:attachment_operator' => boolean_select::CHECKED,
], true],
'Filter attachment (no match)' => ['attachment', 1, 'blog:attachment', [
'blog:attachment_operator' => boolean_select::NOT_CHECKED,
], false],
'Filter publish state' => ['publishstate', 'site', 'blog:publishstate', [
'blog:publishstate_operator' => select::EQUAL_TO,
'blog:publishstate_value' => 'site',
], true],
'Filter publish state (no match)' => ['publishstate', 'site', 'blog:publishstate', [
'blog:publishstate_operator' => select::EQUAL_TO,
'blog:publishstate_value' => 'draft',
], false],
'Filter time created' => ['created', 1654038000, 'blog:timecreated', [
'blog:timecreated_operator' => date::DATE_RANGE,
'blog:timecreated_from' => 1622502000,
], true],
'Filter time created (no match)' => ['created', 1654038000, 'blog:timecreated', [
'blog:timecreated_operator' => date::DATE_RANGE,
'blog:timecreated_to' => 1622502000,
], false],
'Filter time modified' => ['lastmodified', 1654038000, 'blog:timemodified', [
'blog:timemodified_operator' => date::DATE_RANGE,
'blog:timemodified_from' => 1622502000,
], true],
'Filter time modified (no match)' => ['lastmodified', 1654038000, 'blog:timemodified', [
'blog:timemodified_operator' => date::DATE_RANGE,
'blog:timemodified_to' => 1622502000,
], false],
];
}
/**
* Test datasource filters
*
* @param string $field
* @param mixed $value
* @param string $filtername
* @param array $filtervalues
* @param bool $expectmatch
*
* @dataProvider datasource_filters_provider
*/
public function test_datasource_filters(
string $field,
$value,
string $filtername,
array $filtervalues,
bool $expectmatch
): void {
global $DB;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
/** @var core_blog_generator $blogsgenerator */
$blogsgenerator = $this->getDataGenerator()->get_plugin_generator('core_blog');
// Create default blog, then manually override one of it's properties to use for filtering.
$blog = $blogsgenerator->create_entry(['userid' => $user->id, 'subject' => 'My blog', 'summary' => 'Horses']);
$DB->set_field('post', $field, $value, ['id' => $blog->id]);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Create report containing single user column, and given filter.
$report = $generator->create_report(['name' => 'Blogs', 'source' => blogs::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
// Add filter, set it's values.
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
$content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
if ($expectmatch) {
$this->assertCount(1, $content);
$this->assertEquals(fullname($user), reset($content[0]));
} else {
$this->assertEmpty($content);
}
}
/**
* Stress test datasource
*
* In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
*/
public function test_stress_datasource(): void {
if (!PHPUNIT_LONGTEST) {
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
}
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
/** @var core_blog_generator $blogsgenerator */
$blogsgenerator = $this->getDataGenerator()->get_plugin_generator('core_blog');
$blogsgenerator->create_entry(['userid' => $user->id, 'subject' => 'My blog', 'summary' => 'Horses']);
$this->datasource_stress_test_columns(blogs::class);
$this->datasource_stress_test_columns_aggregation(blogs::class);
$this->datasource_stress_test_conditions(blogs::class, 'blog:title');
}
}