diff --git a/tests/generator/lib.php b/tests/generator/lib.php new file mode 100644 index 0000000..1ee3bbb --- /dev/null +++ b/tests/generator/lib.php @@ -0,0 +1,168 @@ +. + +/** + * 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]); + } +} diff --git a/tests/generator_test.php b/tests/generator_test.php new file mode 100644 index 0000000..7c3eb65 --- /dev/null +++ b/tests/generator_test.php @@ -0,0 +1,204 @@ +. + +/** + * 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', + 'foo@bar.com', + ); + $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('foo@bar.com', $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, + 'foo@bar.com', + ); + $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('foo@bar.com', $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, 'someemail@example.edu', 0); + $this->assertEquals($course->id, $alternate->courseid); + $this->assertEquals("someemail@example.edu", $alternate->address); + $this->assertEquals(0, $alternate->valid); + } +} diff --git a/tests/privacy/provider_test.php b/tests/privacy/provider_test.php new file mode 100644 index 0000000..1a14e1d --- /dev/null +++ b/tests/privacy/provider_test.php @@ -0,0 +1,132 @@ +. + +/** + * Privacy provider tests 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 + */ +namespace block_quickmail\privacy; + +use coding_exception; +use context_system; +use context_user; +use core_privacy\local\metadata\collection; +use core_privacy\local\request\approved_contextlist; +use core_privacy\local\request\approved_userlist; +use core_privacy\local\request\userlist; +use core_privacy\local\request\writer; +use core_privacy\tests\provider_testcase; +use dml_exception; + +/** + * Privacy provider test case. + * + * @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\privacy\provider + */ +final class provider_test extends provider_testcase { + /** + * Test getting the context for the user ID related to this plugin. + * @throws dml_exception + */ + public function test_get_contexts_for_userid(): void { + global $DB; + $this->resetAfterTest(); + $generator = $this->getDataGenerator(); + $quickmailgenerator = $this->getDataGenerator()->get_plugin_generator('block_quickmail'); + + // Create four users and a course. + $user1 = $generator->create_user(); + $usercontext1 = context_user::instance($user1->id); + $user2 = $generator->create_user(); + $usercontext2 = context_user::instance($user2->id); + $user3 = $generator->create_user(); + $usercontext3 = context_user::instance($user3->id); + $user4 = $generator->create_user(); + $usercontext4 = context_user::instance($user4->id); + + $course = $generator->create_course(); + + // No contexts returned before user-specific block configurations are applied. + $contextlist1 = provider::get_contexts_for_userid($user1->id); + $this->assertCount(0, $contextlist1); + $contextlist2 = provider::get_contexts_for_userid($user2->id); + $this->assertCount(0, $contextlist2); + + // Create some quickmail data points for all users. + $log1 = $quickmailgenerator->create_quickmail_log($user1, $course, [$user2]); + // Todo: add more test vectors here for the other users [ST 2024/06/07]. + + // Ensure provider only fetches the user's own context. + $contextlist1 = provider::get_contexts_for_userid($user1->id); + $this->assertCount(1, $contextlist1); + $this->assertEquals($usercontext1, $contextlist1->current()); + // Todo: check the other user contexts [ST 2024/06/07]. + } + + /** + * Test getting users in the context ID related to this plugin. + */ + public function test_get_users_in_context(): void { + // Todo: implement. [ST 2024/06/07]. + $this->markTestIncomplete('to be implemented'); + } + + /** + * Test fetching information about user data stored. + */ + public function test_get_metadata(): void { + // Todo: implement. [ST 2024/06/07]. + $this->markTestIncomplete('to be implemented'); + } + + /** + * Test exporting data for an approved contextlist. + */ + public function test_export_user_data(): void { + // Todo: implement. [ST 2024/06/07]. + $this->markTestIncomplete('to be implemented'); + } + + /** + * Test deleting data for all users within an approved contextlist. + */ + public function test_delete_data_for_all_users_in_context(): void { + // Todo: implement. [ST 2024/06/07]. + $this->markTestIncomplete('to be implemented'); + } + + /** + * Test deleting data within an approved contextlist for a user. + */ + public function test_delete_data_for_user(): void { + // Todo: implement. [ST 2024/06/07]. + $this->markTestIncomplete('to be implemented'); + } + + /** + * Test deleting data within a context for an approved userlist. + */ + public function test_delete_data_for_users(): void { + // Todo: implement. [ST 2024/06/07]. + $this->markTestIncomplete('to be implemented'); + } +}