Skip to content

Commit

Permalink
adds test coverage for privacy provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
stopfstedt committed Jun 10, 2024
1 parent c696dbc commit 50a8aec
Show file tree
Hide file tree
Showing 3 changed files with 504 additions and 0 deletions.
168 changes: 168 additions & 0 deletions tests/generator/lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?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 generator for block_quickmail.
*
* @package block_quickmail
* @copyright The Regents of the University of California
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Quickmail block data generator class.
*
* @package block_quickmail
* @copyright The Regents of the University of California
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_quickmail_generator extends testing_block_generator {

/**
* Creates and returns a quickmail log (aka "sent message") record with the given data points.
*
* @param stdClass $user The message sender.
* @param stdClass $course The course that this message was sent in.
* @param array $recipients A list of user records that are recipients of this message.
* @param stdClass|null $alternateemail An alternate email to be used as sender.
* @param string|null $subject The message subject.
* @param string|null $message The message contents.
* @param string|null $attachment A comma-separated list of attachment names.
* @param int|null $format The message format.
* @param int|null $timesent The timestamp of when the message was sent.
* @param string|null $faileduserids A comma-separated list of user IDs where message delivery failed.
* @param string|null $additionalemails A comma-separated list of emails that this message was sent to.
* @return stdClass The log record.
* @throws dml_exception
*/
public function create_quickmail_log(
stdClass $user,
stdClass $course,
array $recipients,
?stdClass $alternateemail = null,
?string $subject = null,
?string $message = null,
?string $attachment = null,
?int $format = null,
?int $timesent = null,
?string $faileduserids = null,
?string $additionalemails = null,
): stdClass {
global $DB;

$record = [];
$record['courseid'] = $course->id;
$record['userid'] = $user->id;
$record['alternateid'] = $alternateemail?->id;
$recipientids = [];
foreach ($recipients as $recipient) {
$recipientids[] = $recipient->id;
}
$record['mailto'] = implode(',', $recipientids);
$record['subject'] = $subject ?? 'test subject';
$record['message'] = $message ?? 'test message';
$record['attachment'] = $attachment ?? '';
$record['format'] = $format ?? 0;
$record['time'] = $timesent ?? time();
$record['failuserids'] = $faileduserids ?? '';
$record['additional_emails'] = $additionalemails ?? '';

$id = $DB->insert_record('block_quickmail_log', $record);
return $DB->get_record('block_quickmail_log', ['id' => $id]);
}

/**
* Creates a quickmail draft (aka "message draft") record with the given data points.
*
* @param stdClass $user The message sender.
* @param stdClass $course The course that this message will be sent in.
* @param array $recipients A list of user records that will be recipients of this message.
* @param stdClass|null $alternateemail An alternate email to be used as sender.
* @param string|null $subject The message subject.
* @param string|null $message The message contents.
* @param string|null $attachment A comma-separated list of attachment names.
* @param int|null $format The message format.
* @param int|null $timecreated The timestamp of when the draft was created.
* @param string|null $additionalemails A comma-separated list of emails that this message will be sent to.
* @return stdClass The message draft record.
* @throws dml_exception
*/
public function create_quickmail_draft(
stdClass $user,
stdClass $course,
array $recipients,
?stdClass $alternateemail = null,
?string $subject = null,
?string $message = null,
?string $attachment = null,
?int $format = null,
?int $timecreated = null,
?string $additionalemails = null,
): stdClass {
global $DB;

$record = [];
$record['courseid'] = $course->id;
$record['userid'] = $user->id;
$record['alternateid'] = $alternateemail?->id;
$recipientids = [];
foreach ($recipients as $recipient) {
$recipientids[] = $recipient->id;
}
$record['mailto'] = implode(',', $recipientids);
$record['subject'] = $subject ?? 'test subject';
$record['message'] = $message ?? 'test message';
$record['attachment'] = $attachment ?? '';
$record['format'] = $format ?? 0;
$record['time'] = $timecreated ?? time();
$record['additional_emails'] = $additionalemails ?? '';

$id = $DB->insert_record('block_quickmail_drafts', $record);
return $DB->get_record('block_quickmail_drafts', ['id' => $id]);

}

/**
* Creates a quickmail email signature record with the given data points.
* @param stdClass $user The signature owner.
* @param stdClass $course The course that this message will be sent in.
* @return stdClass The draft record.
*/
public function create_quickmail_signature(stdClass $user, stdClass $course): stdClass {
// Todo: Implement. [ST 2024/06/07].
return new stdClass();
}

/**
* Creates a quickmail alternate email sender record for the given course.
*
* @param stdClass $course The course.
* @param ?string $email The alternate email address.
* @param ?int $valid Set to 1 to indicate a valid record, 0 for invalid.
* @return stdClass The alternate email record.
* @throws dml_exception
*/
public function create_quickmail_alternate(stdClass $course, ?string $email = null, ?int $valid = null): stdClass {
global $DB;

$record = [];
$record['courseid'] = $course->id;
$record['address'] = $email ?? "course$course->id@example.edu";
$record['valid'] = $valid ?? 1;
$id = $DB->insert_record('block_quickmail_alternate', $record);
return $DB->get_record('block_quickmail_alternate', ['id' => $id]);
}
}
204 changes: 204 additions & 0 deletions tests/generator_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?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/>.

/**
* Test coverage for Quickmail block data generator.
*
* @package block_quickmail
* @copyright The Regents of the University of California
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace block_quickmail;

use advanced_testcase;

/**
* Quickmail block data generator testcase.
*
* @package block_quickmail
* @copyright The Regents of the University of California
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \block_quickmail_generator
*/
final class generator_test extends advanced_testcase {
/**
* Tests quickmail log record generator.
*/
public function test_create_quickmail_log(): void {
global $DB;

$this->resetAfterTest();
$generator = $this->getDataGenerator();
$plugingenerator = $generator->get_plugin_generator('block_quickmail');

$this->assertEquals(0, $DB->count_records('block_quickmail_log'));

$user = $generator->create_user();
$user2 = $generator->create_user();
$user3 = $generator->create_user();
$course = $generator->create_course();

// Create quickmail log record with required properties only.
$log = $plugingenerator->create_quickmail_log($user, $course, []);

// Confirm that record count in the DB ticked up.
$this->assertEquals(1, $DB->count_records('block_quickmail_log'));

// Check log record props.
$this->assertEquals($course->id, $log->courseid);
$this->assertEquals($user->id, $log->userid);
$this->assertNull($log->alternateid);
$this->assertEquals('', $log->mailto);
$this->assertEquals('test subject', $log->subject);
$this->assertEquals('test message', $log->message);
$this->assertEquals('', $log->attachment);
$this->assertEquals(0, $log->format);
$this->assertLessThanOrEqual(time(), $log->time);
$this->assertGreaterThan(0, $log->time);
$this->assertEquals('', $log->failuserids);
$this->assertEquals('', $log->additional_emails);

// Create another log record with overridden defaults.
$alternate = $plugingenerator->create_quickmail_alternate($course);
$timesent = time() + 10000;

$log = $plugingenerator->create_quickmail_log(
$user,
$course,
[$user2, $user3],
$alternate,
'lorem',
'ipsum',
'test.png',
2,
$timesent,
'1,2,3',
'[email protected]',
);
$this->assertEquals(2, $DB->count_records('block_quickmail_log'));

$this->assertEquals($course->id, $log->courseid);
$this->assertEquals($user->id, $log->userid);
$this->assertEquals($alternate->id, $log->alternateid);
$this->assertEquals("$user2->id,$user3->id", $log->mailto);
$this->assertEquals('lorem', $log->subject);
$this->assertEquals('ipsum', $log->message);
$this->assertEquals('test.png', $log->attachment);
$this->assertEquals(2, $log->format);
$this->assertEquals($timesent, $log->time);
$this->assertEquals('1,2,3', $log->failuserids);
$this->assertEquals('[email protected]', $log->additional_emails);
}

/**
* Tests quickmail draft record generator.
*/
public function test_create_quickmail_draft(): void {
global $DB;

$this->resetAfterTest();
$generator = $this->getDataGenerator();
$plugingenerator = $generator->get_plugin_generator('block_quickmail');

$this->assertEquals(0, $DB->count_records('block_quickmail_drafts'));

$user = $generator->create_user();
$user2 = $generator->create_user();
$user3 = $generator->create_user();
$course = $generator->create_course();

// Create quickmail draft record with required properties only.
$draft = $plugingenerator->create_quickmail_draft($user, $course, []);

// Confirm that record count in the DB ticked up.
$this->assertEquals(1, $DB->count_records('block_quickmail_drafts'));

// Check draft record props.
$this->assertEquals($course->id, $draft->courseid);
$this->assertEquals($user->id, $draft->userid);
$this->assertNull($draft->alternateid);
$this->assertEquals('', $draft->mailto);
$this->assertEquals('test subject', $draft->subject);
$this->assertEquals('test message', $draft->message);
$this->assertEquals('', $draft->attachment);
$this->assertEquals(0, $draft->format);
$this->assertLessThanOrEqual(time(), $draft->time);
$this->assertGreaterThan(0, $draft->time);
$this->assertEquals('', $draft->additional_emails);

// Create another draft record with overridden defaults.
$alternate = $plugingenerator->create_quickmail_alternate($course);
$timecreated = time() + 10000;

$draft = $plugingenerator->create_quickmail_draft(
$user,
$course,
[$user2, $user3],
$alternate,
'lorem',
'ipsum',
'test.png',
2,
$timecreated,
'[email protected]',
);
$this->assertEquals(2, $DB->count_records('block_quickmail_drafts'));

$this->assertEquals($course->id, $draft->courseid);
$this->assertEquals($user->id, $draft->userid);
$this->assertEquals($alternate->id, $draft->alternateid);
$this->assertEquals("$user2->id,$user3->id", $draft->mailto);
$this->assertEquals('lorem', $draft->subject);
$this->assertEquals('ipsum', $draft->message);
$this->assertEquals('test.png', $draft->attachment);
$this->assertEquals(2, $draft->format);
$this->assertEquals($timecreated, $draft->time);
$this->assertEquals('[email protected]', $draft->additional_emails);
}

/**
* Tests quickmail alternate email record record generator.
*/
public function test_create_quickmail_alternate(): void {
global $DB;

$this->resetAfterTest();
$generator = $this->getDataGenerator();
$plugingenerator = $generator->get_plugin_generator('block_quickmail');

$this->assertEquals(0, $DB->count_records('block_quickmail_alternate'));

$course = $generator->create_course();

// Create quickmail alternate email record with required properties only.
$alternate = $plugingenerator->create_quickmail_alternate($course);

// Confirm that record count in the DB ticked up.
$this->assertEquals(1, $DB->count_records('block_quickmail_alternate'));

// Check log record props.
$this->assertEquals($course->id, $alternate->courseid);
$this->assertEquals("course$course->id@example.edu", $alternate->address);
$this->assertEquals(1, $alternate->valid);

// Create another alt email record with overridden defaults.
$alternate = $plugingenerator->create_quickmail_alternate($course, '[email protected]', 0);
$this->assertEquals($course->id, $alternate->courseid);
$this->assertEquals("[email protected]", $alternate->address);
$this->assertEquals(0, $alternate->valid);
}
}
Loading

0 comments on commit 50a8aec

Please sign in to comment.