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
@@ -0,0 +1,95 @@
<?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/>.
namespace message_email;
/**
* Class for testing the event observers.
*
* @package message_email
* @category test
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class event_observers_test extends \advanced_testcase {
/**
* Test the message viewed event observer.
*/
public function test_message_viewed_observer(): void {
global $DB;
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
$this->resetAfterTest();
// Create the test data.
$course = $this->getDataGenerator()->create_course();
$user1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
$user2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
groups_add_member($group1->id, $user1->id);
groups_add_member($group1->id, $user2->id);
$conversation = \core_message\api::create_conversation(
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
[$user1->id, $user2->id],
'Group 1', \core_message\api::MESSAGE_CONVERSATION_ENABLED,
'core_group',
'groups',
$group1->id,
\context_course::instance($course->id)->id
);
$message = new \core\message\message();
$message->courseid = 1;
$message->component = 'moodle';
$message->name = 'instantmessage';
$message->userfrom = $user1;
$message->convid = $conversation->id;
$message->subject = 'message subject';
$message->fullmessage = 'message body';
$message->fullmessageformat = FORMAT_MARKDOWN;
$message->fullmessagehtml = '<p>message body</p>';
$message->smallmessage = 'small message';
$message->notification = '0';
// Send the message twice.
$messageid1 = message_send($message);
$messageid2 = message_send($message);
// Check there are now 2 messages pending to be sent in the digest.
$this->assertEquals(2, $DB->count_records('message_email_messages'));
// Mark one of the messages as read.
$message1 = $DB->get_record('messages', ['id' => $messageid1]);
\core_message\api::mark_message_as_read($user2->id, $message1);
$emailmessage = $DB->get_records('message_email_messages');
// Check there is now only 1 message pending to be sent in the digest and it is the correct message.
$this->assertEquals(1, count($emailmessage));
$emailmessage = reset($emailmessage);
$this->assertEquals($user2->id, $emailmessage->useridto);
$this->assertEquals($conversation->id, $emailmessage->conversationid);
$this->assertEquals($messageid2, $emailmessage->messageid);
}
}
@@ -0,0 +1,92 @@
<?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/>.
namespace message_email\privacy;
use context_system;
use core_message_external;
use core_privacy\local\request\writer;
use core_privacy\tests\provider_testcase;
/**
* Unit tests for message\output\email\classes\privacy\provider.php
*
* @package message_email
* @covers \message_email\privacy\provider
* @copyright 2018 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider_test extends provider_testcase {
/**
* Basic setup for these tests.
*/
public function setUp(): void {
$this->resetAfterTest(true);
}
/**
* Test returning metadata.
*/
public function test_get_metadata(): void {
$collection = new \core_privacy\local\metadata\collection('message_email');
$collection = \message_email\privacy\provider::get_metadata($collection);
$this->assertNotEmpty($collection);
}
/**
* Test getting the context for the user ID related to this plugin.
*/
public function test_get_contexts_for_userid(): void {
$user = $this->getDataGenerator()->create_user();
$contextlist = \message_email\privacy\provider::get_contexts_for_userid($user->id);
$this->assertEmpty($contextlist);
}
/**
* Test exporting user preferences
*/
public function test_export_user_preferences(): void {
global $CFG;
require_once("{$CFG->dirroot}/message/externallib.php");
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
// Submit configuration form, which adds the preferences..
core_message_external::message_processor_config_form($user->id, 'email', [
[
'name' => 'email_email',
'value' => 'alternate@example.com',
],
]);
// Switch to admin user (so we can validate preferences of the correct user are being exported).
$this->setAdminUser();
provider::export_user_preferences($user->id);
$writer = writer::with_context(context_system::instance());
$this->assertTrue($writer->has_any_data());
$preferences = $writer->get_user_preferences('message_email');
$this->assertNotEmpty($preferences->email);
$this->assertEquals('alternate@example.com', $preferences->email->value);
$this->assertEquals(get_string('privacy:preference:email', 'message_email'), $preferences->email->description);
}
}
@@ -0,0 +1,137 @@
<?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/>.
namespace message_email\task;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
/**
* Class for testing the send email task.
*
* @package message_email
* @category test
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class send_email_test extends \advanced_testcase {
/**
* Test sending email task.
*/
public function test_sending_email_task(): void {
global $DB, $SITE;
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
$this->resetAfterTest();
// Create a course.
$course = $this->getDataGenerator()->create_course();
$user1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
$user2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Create two groups in the course.
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
groups_add_member($group1->id, $user1->id);
groups_add_member($group2->id, $user1->id);
groups_add_member($group1->id, $user2->id);
groups_add_member($group2->id, $user2->id);
$conversation1 = \core_message\api::create_conversation(
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
[$user1->id, $user2->id],
'Group 1', \core_message\api::MESSAGE_CONVERSATION_ENABLED,
'core_group',
'groups',
$group1->id,
\context_course::instance($course->id)->id
);
$conversation2 = \core_message\api::create_conversation(
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
[$user1->id, $user2->id],
'Group 2',
\core_message\api::MESSAGE_CONVERSATION_ENABLED,
'core_group',
'groups',
$group2->id,
\context_course::instance($course->id)->id
);
// Go through each conversation.
if ($conversations = $DB->get_records('message_conversations')) {
foreach ($conversations as $conversation) {
$conversationid = $conversation->id;
// Let's send 5 messages.
for ($i = 1; $i <= 5; $i++) {
$message = new \core\message\message();
$message->courseid = 1;
$message->component = 'moodle';
$message->name = 'instantmessage';
$message->userfrom = $user1;
$message->convid = $conversationid;
$message->subject = 'message subject';
$message->fullmessage = 'message body';
$message->fullmessageformat = FORMAT_MARKDOWN;
$message->fullmessagehtml = '<p>message body</p>';
$message->smallmessage = 'small message';
$message->notification = '0';
message_send($message);
}
}
}
$this->assertEquals(10, $DB->count_records('message_email_messages'));
// Only 1 email is sent as the messages are included in it at a digest.
$sink = $this->redirectEmails();
$task = new send_email_task();
$task->execute();
$this->assertEquals(1, $sink->count());
// Confirm it contains the correct data.
$emails = $sink->get_messages();
$email = reset($emails);
$sitename = format_string($SITE->fullname);
$this->assertSame(get_string('messagedigestemailsubject', 'message_email', $sitename), $email->subject);
$this->assertSame($user2->email, $email->to);
$this->assertNotEmpty($email->header);
$emailbody = quoted_printable_decode($email->body);
$this->assertStringContainsString('Group 1', $emailbody);
$this->assertStringContainsString('Group 2', $emailbody);
// 5 unread messages per conversation, this will be listed twice.
$this->assertMatchesRegularExpression("/<span\b[^>]*>5<\/span> <span\b[^>]*>Unread message\w+/", $emailbody);
// Confirm table was emptied after task was run.
$this->assertEquals(0, $DB->count_records('message_email_messages'));
// Confirm running it again does not send another.
$sink = $this->redirectEmails();
$task = new send_email_task();
$task->execute();
$this->assertEquals(0, $sink->count());
}
}