From 6733fb3ea3b6100f8dddd735b9b5f29d37daa8e2 Mon Sep 17 00:00:00 2001 From: TamaroWalter Date: Fri, 14 Jun 2024 19:56:46 +0200 Subject: [PATCH] WIP: php mess cleaning --- classes/manager/mail_manager.php | 1 - locallib.php | 4 +- tests/dailymail_test.php | 4 - tests/ratings_test.php | 60 +-- tests/subscriptions_test.php | 747 +++++++++++-------------------- tests/userstats_test.php | 1 - 6 files changed, 287 insertions(+), 530 deletions(-) diff --git a/classes/manager/mail_manager.php b/classes/manager/mail_manager.php index 0ee2f1fbb5..04dbba65e3 100644 --- a/classes/manager/mail_manager.php +++ b/classes/manager/mail_manager.php @@ -86,7 +86,6 @@ public static function moodleoverflow_send_mails(): bool { $moodleoverflows = []; $courses = []; $coursemodules = []; - $subscribedusers = []; // Posts older than x days will not be mailed. // This will avoid problems with the cron not ran for a long time. diff --git a/locallib.php b/locallib.php index 69102f1c6d..299399afe7 100644 --- a/locallib.php +++ b/locallib.php @@ -772,7 +772,7 @@ function moodleoverflow_user_can_see_discussion($moodleoverflow, $discussion, $c } // Retrieve the coursemodule. - if (!$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $moodleoverflow->course)) { + if (!get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $moodleoverflow->course)) { throw new moodle_exception('invalidcoursemodule'); } @@ -1566,7 +1566,7 @@ function moodleoverflow_print_posts_nested($course, &$cm, $moodleoverflow, $disc * @return array */ function get_attachments($post, $cm) { - global $CFG, $OUTPUT; + global $OUTPUT; $attachments = []; if (empty($post->attachment)) { diff --git a/tests/dailymail_test.php b/tests/dailymail_test.php index 5a2f2329b5..f28b52b88e 100644 --- a/tests/dailymail_test.php +++ b/tests/dailymail_test.php @@ -224,10 +224,6 @@ public function test_content_of_mail_delivery(): void { $linktodiscussion = 'discussion[0]->id; - // Assemble text. - $text = 'Course: ' . $linktocourse . ' -> ' . $linktoforum . ', Topic: ' - . $linktodiscussion . ' has ' . $messagecount . ' unread posts.'; - $this->assertStringContainsString($linktocourse, $message); $this->assertStringContainsString($linktoforum, $message); $this->assertStringContainsString($linktodiscussion, $message); diff --git a/tests/ratings_test.php b/tests/ratings_test.php index 1ebf68fcf9..479b133674 100644 --- a/tests/ratings_test.php +++ b/tests/ratings_test.php @@ -61,9 +61,6 @@ final class ratings_test extends \advanced_testcase { /** @var stdClass answer from user 2 */ private $answer6; - /** @var \mod_moodleoverflow_generator $generator */ - private $generator; - /** * Test setUp. */ @@ -77,8 +74,8 @@ public function setUp(): void { */ public function tearDown(): void { // Clear all caches. - \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache(); - \mod_moodleoverflow\subscriptions::reset_discussion_cache(); + subscriptions::reset_moodleoverflow_cache(); + subscriptions::reset_discussion_cache(); } // Begin of test functions. @@ -163,40 +160,32 @@ public function test_answersorting_twogroups(): void { $this->set_ratingpreferences(0); // Test case 1: helpful and solved post, only solved posts. - $group1 = 'sh'; - $group2 = 's'; - $this->process_groups($group1, $group2); + $this->process_groups('sh', 's'); // Test case 2: helpful and solved post, only helpful posts. - $group2 = 'h'; - $this->process_groups($group1, $group2); + $this->process_groups('sh', 'h'); // Test case 3: helpful and solved post, not-marked posts. - $group2 = 'o'; - $this->process_groups($group1, $group2); + $this->process_groups('sh', 'o'); // Test case 4: only solved posts and only helpful posts with ratingpreferences = 0. - $group1 = 's'; - $group2 = 'h'; $this->set_ratingpreferences(0); $rightorder = [$this->post, $this->answer6, $this->answer5, $this->answer4, $this->answer2, $this->answer1, $this->answer3]; - $this->process_groups($group1, $group2, $rightorder); + $this->process_groups('s', 'h', $rightorder); // Test case 5: only solved posts and only helpful posts with ratingpreferences = 1. $this->set_ratingpreferences(1); - $this->process_groups($group1, $group2); + $this->process_groups('s', 'h'); // Test case 6: only solved posts and not-marked posts. - $group2 = 'o'; - $this->create_twogroups($group1, $group2); + $this->create_twogroups('s', 'o'); $posts = [$this->post, $this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6]; $rightorder = [$this->post, $this->answer2, $this->answer1, $this->answer3, $this->answer6, $this->answer5, $this->answer4]; $result = $this->postsorderequal(ratings::moodleoverflow_sort_answers_by_ratings($posts), $rightorder); $this->assertEquals(1, $result); // Test case 6: only helpful posts and not-marked posts. - $group1 = 'h'; - $this->process_groups($group1, $group2); + $this->process_groups('h', 'o'); } /** @@ -213,8 +202,7 @@ public function test_answersorting_onegroup(): void { $this->set_ratingpreferences(0); // Test case 1: only solved and helpful posts. - $group = 'sh'; - $this->create_onegroup($group); + $this->create_onegroup('sh'); $posts = [$this->post, $this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6]; $rightorder = [$this->post, $this->answer4, $this->answer6, $this->answer3, $this->answer1, $this->answer2, $this->answer5]; $result = $this->postsorderequal(ratings::moodleoverflow_sort_answers_by_ratings($posts), $rightorder); @@ -227,8 +215,7 @@ public function test_answersorting_onegroup(): void { $this->assertEquals(1, $result); // Test case 2: only solvedposts. - $group = 's'; - $this->create_onegroup($group); + $this->create_onegroup('s'); $rightorder = [$this->post, $this->answer4, $this->answer6, $this->answer3, $this->answer1, $this->answer2, $this->answer5]; $result = $this->postsorderequal(ratings::moodleoverflow_sort_answers_by_ratings($posts), $rightorder); $this->assertEquals(1, $result); @@ -240,8 +227,7 @@ public function test_answersorting_onegroup(): void { $this->assertEquals(1, $result); // Test case 3: only helpful posts. - $group = 'h'; - $this->create_onegroup($group); + $this->create_onegroup('h'); $rightorder = [$this->post, $this->answer4, $this->answer6, $this->answer3, $this->answer1, $this->answer2, $this->answer5]; $result = $this->postsorderequal(ratings::moodleoverflow_sort_answers_by_ratings($posts), $rightorder); $this->assertEquals(1, $result); @@ -253,8 +239,7 @@ public function test_answersorting_onegroup(): void { $this->assertEquals(1, $result); // Test case 4: only not marked posts. - $group = 'o'; - $this->create_onegroup($group); + $this->create_onegroup('o'); $rightorder = [$this->post, $this->answer4, $this->answer6, $this->answer3, $this->answer1, $this->answer2, $this->answer5]; $result = $this->postsorderequal(ratings::moodleoverflow_sort_answers_by_ratings($posts), $rightorder); $this->assertEquals(1, $result); @@ -292,15 +277,15 @@ private function helper_course_set_up() { $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student'); // Create a discussion, a parent post and six answers. - $this->generator = $this->getDataGenerator()->get_plugin_generator('mod_moodleoverflow'); - $discussion = $this->generator->post_to_forum($moodleoverflow, $teacher); + $generator = $this->getDataGenerator()->get_plugin_generator('mod_moodleoverflow'); + $discussion = $generator->post_to_forum($moodleoverflow, $teacher); $this->post = $DB->get_record('moodleoverflow_posts', ['id' => $discussion[0]->firstpost], '*'); - $this->answer1 = $this->generator->reply_to_post($discussion[1], $user1, true); - $this->answer2 = $this->generator->reply_to_post($discussion[1], $user1, true); - $this->answer3 = $this->generator->reply_to_post($discussion[1], $user1, true); - $this->answer4 = $this->generator->reply_to_post($discussion[1], $user2, true); - $this->answer5 = $this->generator->reply_to_post($discussion[1], $user2, true); - $this->answer6 = $this->generator->reply_to_post($discussion[1], $user2, true); + $this->answer1 = $generator->reply_to_post($discussion[1], $user1, true); + $this->answer2 = $generator->reply_to_post($discussion[1], $user1, true); + $this->answer3 = $generator->reply_to_post($discussion[1], $user1, true); + $this->answer4 = $generator->reply_to_post($discussion[1], $user2, true); + $this->answer5 = $generator->reply_to_post($discussion[1], $user2, true); + $this->answer6 = $generator->reply_to_post($discussion[1], $user2, true); } @@ -315,7 +300,8 @@ private function postsorderequal($sortedposts, $rightorder) { if (count($sortedposts) != count($rightorder)) { return 0; } - for ($i = 0; $i < count($sortedposts); $i++) { + $numberofposts = count($sortedposts); + for ($i = 0; $i < $numberofposts; $i++) { // Get the current elements. $sortedpost = current($sortedposts); $post = current($rightorder); diff --git a/tests/subscriptions_test.php b/tests/subscriptions_test.php index 7fe9eae71d..4b02a0854c 100644 --- a/tests/subscriptions_test.php +++ b/tests/subscriptions_test.php @@ -45,8 +45,8 @@ final class subscriptions_test extends advanced_testcase { */ public function setUp(): void { // Clear all caches. - \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache(); - \mod_moodleoverflow\subscriptions::reset_discussion_cache(); + subscriptions::reset_moodleoverflow_cache(); + subscriptions::reset_discussion_cache(); } /** @@ -54,8 +54,8 @@ public function setUp(): void { */ public function tearDown(): void { // Clear all caches. - \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache(); - \mod_moodleoverflow\subscriptions::reset_discussion_cache(); + subscriptions::reset_moodleoverflow_cache(); + subscriptions::reset_discussion_cache(); } /** @@ -129,34 +129,34 @@ public function test_subscription_modes(): void { $this->setUser($user); // Test the forced subscription. - \mod_moodleoverflow\subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_FORCESUBSCRIBE); + subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_FORCESUBSCRIBE); $moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $moodleoverflow->id]); $this->assertEquals(MOODLEOVERFLOW_FORCESUBSCRIBE, - \mod_moodleoverflow\subscriptions::get_subscription_mode($moodleoverflow)); - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_forcesubscribed($moodleoverflow)); - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, $modulecontext)); - $this->assertFalse(\mod_moodleoverflow\subscriptions::subscription_disabled($moodleoverflow)); + subscriptions::get_subscription_mode($moodleoverflow)); + $this->assertTrue(subscriptions::is_forcesubscribed($moodleoverflow)); + $this->assertFalse(subscriptions::is_subscribable($moodleoverflow, $modulecontext)); + $this->assertFalse(subscriptions::subscription_disabled($moodleoverflow)); // Test the disallowed subscription. - \mod_moodleoverflow\subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_DISALLOWSUBSCRIBE); + subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_DISALLOWSUBSCRIBE); $moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $moodleoverflow->id]); - $this->assertTrue(\mod_moodleoverflow\subscriptions::subscription_disabled($moodleoverflow)); - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, $modulecontext)); - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_forcesubscribed($moodleoverflow)); + $this->assertTrue(subscriptions::subscription_disabled($moodleoverflow)); + $this->assertFalse(subscriptions::is_subscribable($moodleoverflow, $modulecontext)); + $this->assertFalse(subscriptions::is_forcesubscribed($moodleoverflow)); // Test the initial subscription. - \mod_moodleoverflow\subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_INITIALSUBSCRIBE); + subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_INITIALSUBSCRIBE); $moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $moodleoverflow->id]); - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, $modulecontext)); - $this->assertFalse(\mod_moodleoverflow\subscriptions::subscription_disabled($moodleoverflow)); - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_forcesubscribed($moodleoverflow)); + $this->assertTrue(subscriptions::is_subscribable($moodleoverflow, $modulecontext)); + $this->assertFalse(subscriptions::subscription_disabled($moodleoverflow)); + $this->assertFalse(subscriptions::is_forcesubscribed($moodleoverflow)); // Test the choose subscription. - \mod_moodleoverflow\subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_CHOOSESUBSCRIBE); + subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_CHOOSESUBSCRIBE); $moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $moodleoverflow->id]); - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, $modulecontext)); - $this->assertFalse(\mod_moodleoverflow\subscriptions::subscription_disabled($moodleoverflow)); - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_forcesubscribed($moodleoverflow)); + $this->assertTrue(subscriptions::is_subscribable($moodleoverflow, $modulecontext)); + $this->assertFalse(subscriptions::subscription_disabled($moodleoverflow)); + $this->assertFalse(subscriptions::is_forcesubscribed($moodleoverflow)); } /** @@ -182,7 +182,7 @@ public function test_unsubscribable_moodleoverflows(): void { $this->setUser($user); // Without any subscriptions, there should be nothing returned. - $result = \mod_moodleoverflow\subscriptions::get_unsubscribable_moodleoverflows(); + $result = subscriptions::get_unsubscribable_moodleoverflows(); $this->assertEquals(0, count($result)); // Create the moodleoverflows. @@ -196,15 +196,15 @@ public function test_unsubscribable_moodleoverflows(): void { $this->getDataGenerator()->create_module('moodleoverflow', $options); // At present the user is only subscribed to the initial moodleoverflow. - $result = \mod_moodleoverflow\subscriptions::get_unsubscribable_moodleoverflows(); + $result = subscriptions::get_unsubscribable_moodleoverflows(); $this->assertEquals(1, count($result)); // Ensure that the user is enrolled in all of the moodleoverflows execpt force subscribe. - \mod_moodleoverflow\subscriptions::subscribe_user($user->id, $disallow, $modulecontext); - \mod_moodleoverflow\subscriptions::subscribe_user($user->id, $choose, $modulecontext); + subscriptions::subscribe_user($user->id, $disallow, $modulecontext); + subscriptions::subscribe_user($user->id, $choose, $modulecontext); // At present the user is subscribed to all three moodleoverflows. - $result = \mod_moodleoverflow\subscriptions::get_unsubscribable_moodleoverflows(); + $result = subscriptions::get_unsubscribable_moodleoverflows(); $this->assertEquals(3, count($result)); } @@ -217,16 +217,12 @@ public function test_moodleoverflow_toggle_as_other(): void { // Reset the database after testing. $this->resetAfterTest(true); - // Create a course with a moodleoverflow. + // Create a course with a moodleoverflow. Get the module context and enroll a user in the course as a student. $course = $this->getDataGenerator()->create_course(); $options = ['course' => $course->id]; $moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options); - - // Get the module context. $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id); $modulecontext = \context_module::instance($cm->id); - - // Create a user enrolled in the course as a student. list ($author) = $this->helper_create_users($course, 1); // Post a discussion to the moodleoverflow. @@ -236,149 +232,83 @@ public function test_moodleoverflow_toggle_as_other(): void { $discussion->moodleoverflow = $moodleoverflow->id; // Check that the user is currently not subscribed to the moodleoverflow. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, - $modulecontext)); + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); // Check that the user is unsubscribed from the discussion too. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, - $modulecontext, $discussion->id)); + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, $discussion->id)); + + // Declare the options for the subscription tables. + $discussoptions = ['userid' => $author->id, 'discussion' => $discussion->id]; + $subscriptionoptions = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; // Check thast we have no records in either on the subscription tables. - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(0, $count); - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(0, $count); + $this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(0, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); - // Subscribing to the moodleoverflow should create a record in the subscription table, - // but the moodleoverflow discussion subscriptions table. - \mod_moodleoverflow\subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext); - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(1, $count); - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(0, $count); + // Subscribing to the moodleoverflow should add a record to the moodleoverflow subscription table. + subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext); + $this->assertEquals(1, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(0, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); - // Unsubscribing should remove the record from the moodleoverflow subscription table. - // Do not modify the moodleoverflow discussion subscriptions table. - \mod_moodleoverflow\subscriptions::unsubscribe_user($author->id, $moodleoverflow, $modulecontext); - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(0, $count); - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(0, $count); + // Unsubscribing should remove the record from the moodleoverflow subscription table. The discussion table stays the same. + subscriptions::unsubscribe_user($author->id, $moodleoverflow, $modulecontext); + $this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(0, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); - // Enroling the user in the discussion should add one record to the - // moodleoverflow discussion table without modifying the form subscription. - \mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext); - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(0, $count); - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(1, $count); + // Subscribing to the discussion should only add a record to the discussion subscription table. + subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext); + $this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); - // Unsubscribing should remove the record from the moodleoverflow subscriptions - // table and not modify the moodleoverflow discussion subscription table. - \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(0, $count); - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(0, $count); + // Unsubscribing should remove the record from the moodleoverflow subscriptions and modify the discussion subscriptions. + subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); + $this->assertEquals(0, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); + $this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); // Resubscribe to the discussion so that we can check the effect of moodleoverflow-level subscriptions. - \mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext); - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(1, $count); - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(0, $count); + subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext); + $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); + $this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); - // Subscribing to the moodleoverflow should have no effect on the moodleoverflow discussion - // subscription table if the user did not request the change himself. - \mod_moodleoverflow\subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext); - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(1, $count); - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(1, $count); + // Subscribing to the moodleoverflow should have no effect on the discussion subscription. + subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext); + $this->assertEquals(1, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); - // Unsubbing from the moodleoverflow should have no effect on the moodleoverflow - // discussion subscription table if the user did not request the change themself. - \mod_moodleoverflow\subscriptions::unsubscribe_user($author->id, $moodleoverflow, $modulecontext); - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(0, $count); - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(1, $count); + // Unsubbing from the moodleoverflow should have no effect on the discussion subscription. + subscriptions::unsubscribe_user($author->id, $moodleoverflow, $modulecontext); + $this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); - // Subscribing to the moodleoverflow should remove the per-discussion - // subscription preference if the user requested the change themself. - \mod_moodleoverflow\subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext, true); - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(1, $count); - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(0, $count); + // Subscribing to the moodleoverflow should remove the per-discussion subscription preference if the user wanted the change. + subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext, true); + $this->assertEquals(1, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(0, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); // Now unsubscribe from the current discussion whilst being subscribed to the moodleoverflow as a whole. - \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(1, $count); - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(1, $count); + subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); + $this->assertEquals(1, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); - // Unsubscribing from the moodleoverflow should remove the per-discussion - // subscription preference if the user requested the change himself. - \mod_moodleoverflow\subscriptions::unsubscribe_user($author->id, $moodleoverflow, $modulecontext, true); - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(0, $count); - $count = $DB->count_records('moodleoverflow_discuss_subs', [ - 'userid' => $author->id, - 'discussion' => $discussion->id, - ]); - $this->assertEquals(0, $count); + // Unsubscribing from the moodleoverflow should remove per-discussion subscription preference if the user wanted the change. + subscriptions::unsubscribe_user($author->id, $moodleoverflow, $modulecontext, true); + $this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(0, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); // Subscribe to the discussion. - \mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext); - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(0, $count); - $count = $DB->count_records('moodleoverflow_discuss_subs', [ - 'userid' => $author->id, - 'discussion' => $discussion->id, - ]); - $this->assertEquals(1, $count); + subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext); + $this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); // Subscribe to the moodleoverflow without removing the discussion preferences. - \mod_moodleoverflow\subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext); - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(1, $count); - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(1, $count); + subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext); + $this->assertEquals(1, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); // Unsubscribe from the discussion should result in a change. - \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(1, $count); - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(1, $count); + subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); + $this->assertEquals(1, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); } /** @@ -397,14 +327,14 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_unsub list($author) = $this->helper_create_users($course, 1); // Check that the user is currently not subscribed to the moodleoverflow. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $moodleoverflow)); + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $moodleoverflow)); // Post a discussion to the moodleoverflow. list($discussion, $post) = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); unset($post); // Check that the user is unsubscribed from the discussion too. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $moodleoverflow, $discussion)); } @@ -429,21 +359,21 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_subsc // Enrol the user in the moodleoverflow. // If a subscription was added, we get the record ID. - $this->assertIsInt(\mod_moodleoverflow\subscriptions::subscribe_user($author->id, + $this->assertIsInt(subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext)); // If we already have a subscription when subscribing the user, we get a boolean (true). - $this->assertTrue(\mod_moodleoverflow\subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext)); + $this->assertTrue(subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext)); // Check that the user is currently subscribed to the moodleoverflow. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); // Post a discussion to the moodleoverflow. list($discussion, $post) = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); unset($post); // Check that the user is subscribed to the discussion too. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, + $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, $discussion)); } @@ -467,7 +397,7 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_unsub list($author) = $this->helper_create_users($course, 1); // Check that the user is currently not subscribed to the moodleoverflow. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $moodleoverflow)); + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $moodleoverflow)); // Post a discussion to the moodleoverflow. $discussion = new \stdClass(); @@ -476,18 +406,18 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_unsub $discussion->moodleoverflow = $moodleoverflow->id; // Attempting to unsubscribe from the discussion should not make a change. - $this->assertFalse(\mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, + $this->assertFalse(subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext)); // Then subscribe them to the discussion. - $this->assertTrue(\mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id, + $this->assertTrue(subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext)); // Check that the user is still unsubscribed from the moodleoverflow. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $moodleoverflow)); + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $moodleoverflow)); // But subscribed to the discussion. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, + $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, $discussion->id)); } @@ -511,10 +441,10 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_subsc list($author) = $this->helper_create_users($course, 2); // Enrol the student in the moodleoverflow. - \mod_moodleoverflow\subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext); + subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext); // Check that the user is currently subscribed to the moodleoverflow. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); // Post a discussion to the moodleoverflow. $discussion = new \stdClass(); @@ -523,13 +453,13 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_subsc $discussion->moodleoverflow = $moodleoverflow->id; // Then unsubscribe them from the discussion. - \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); + subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); // Check that the user is still subscribed to the moodleoverflow. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); // But unsubscribed from the discussion. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, $discussion->id)); } @@ -538,23 +468,17 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_subsc */ public function test_moodleoverflow_discussion_toggle_moodleoverflow_subscribed(): void { global $DB; - // Reset the database after testing. - $this->resetAfterTest(true); + $this->resetAfterTest(); - // Create a course with a moodleoverflow. + // Create a course with a moodleoverflow and enroll two users in the course as students and in the moodleoverflow. $course = $this->getDataGenerator()->create_course(); $options = ['course' => $course->id, 'forcesubscribe' => MOODLEOVERFLOW_CHOOSESUBSCRIBE]; $moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options); - $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id); $modulecontext = \context_module::instance($cm->id); - - // Create two users enrolled in the course as students. list($author) = $this->helper_create_users($course, 2); - - // Enrol the student in the moodleoverflow. - \mod_moodleoverflow\subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext); + subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext); // Post a discussion to the moodleoverflow. $discussion = new \stdClass(); @@ -562,152 +486,82 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_subscribed( unset($post); $discussion->moodleoverflow = $moodleoverflow->id; - // Check that the user is currently subscribed to the moodleoverflow. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); - - // Check that the user is initially subscribed to that discussion. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, - $discussion->id)); + // Check that the user is currently subscribed to the moodleoverflow and initially subscribed to that discussion. + $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, $discussion->id)); // An attempt to subscribe again should result in a falsey return to indicate that no change was made. - $this->assertFalse(\mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id, - $discussion, $modulecontext)); + $this->assertFalse(subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext)); - // And there should be no discussion subscriptions (and one moodleoverflow subscription). - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(0, $count); - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(1, $count); + // Declare the options for the subscription tables. + $discussoptions = ['userid' => $author->id, 'discussion' => $discussion->id]; + $subscriptionoptions = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - // Then unsubscribe them from the discussion. - \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); - - // Check that the user is still subscribed to the moodleoverflow. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + // And there should be no discussion subscriptions (and one moodleoverflow subscription). + $this->assertEquals(0, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); + $this->assertEquals(1, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); - // An attempt to unsubscribe again should result in a falsey return to indicate that no change was made. - $this->assertFalse(\mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, - $discussion, $modulecontext)); + // Unsubscribe them from the discussion while still subscribe the moodleoverflow. Attempt to unsubscribe again should fail. + subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); + $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + $this->assertFalse(subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext)); // And there should be a discussion subscriptions (and one moodleoverflow subscription). - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(1, $count); - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(1, $count); - - // But unsubscribed from the discussion. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, - $discussion->id)); - - // There should be a record in the discussion subscription tracking table. - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(1, $count); - - // And one in the moodleoverflow subscription tracking table. - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(1, $count); - - // Now subscribe the user again to the discussion. - \mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext); - - // Check that the user is still subscribed to the moodleoverflow. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); - - // And is subscribed to the discussion again. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, - $discussion->id)); - - // And one in the moodleoverflow subscription tracking table. - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(1, $count); - - // There should be no record in the discussion subscription tracking table. - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(0, $count); - - // And unsubscribe again. - \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); - - // Check that the user is still subscribed to the moodleoverflow. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); + $this->assertEquals(1, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); // But unsubscribed from the discussion. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, - $discussion->id)); + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, $discussion->id)); - // And one in the moodleoverflow subscription tracking table. - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(1, $count); + // There should be one record in the discussion and moodleoverflow subscription tracking table. + $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); + $this->assertEquals(1, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); - // There should be a record in the discussion subscription tracking table. - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(1, $count); + // Now subscribe the user again to the discussion. Now discussion and moodleoverflow should be subscribed. + subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext); + $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, $discussion->id)); - // And subscribe the user again to the discussion. - \mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext); + // And one in the moodleoverflow subscription and no record in the discussion tracking table. + $this->assertEquals(1, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(0, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); - // Check that the user is still subscribed to the moodleoverflow. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + // And unsubscribe again. Only moodleoverflow should be subscribed. Discussion should be unsubscribed. + subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); + $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, $discussion->id)); - // And is subscribed to the discussion again. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, - $discussion->id)); - - // There should be no record in the discussion subscription tracking table. - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(0, $count); + // There should be one record in the moodleoverflow and discussion subscription tracking table. + $this->assertEquals(1, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); - // And one in the forum subscription tracking table. - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(1, $count); + // And subscribe the user again to the discussion. Both should be subscribed. + subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext); + $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, $discussion->id)); - // And unsubscribe again. - \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); + // There should be no record in the discussion and one in the moodleoverflow subscription tracking table. + $this->assertEquals(0, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); + $this->assertEquals(1, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); - // But unsubscribed from the discussion. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, - $discussion->id)); + // And unsubscribe again from the discussion. Only moodleoverflow should be subscribed. + subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); + $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, $discussion->id)); - // Check that the user is still subscribed to the moodleoverflow. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); - - // There should be a record in the discussion subscription tracking table. - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(1, $count); - - // And one in the moodleoverflow subscription tracking table. - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(1, $count); + // There should be a record in the discussion and one in the moodleoverflow subscription tracking table. + $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); + $this->assertEquals(1, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); // Now unsubscribe the user from the moodleoverflow. - $this->assertTrue(\mod_moodleoverflow\subscriptions::unsubscribe_user($author->id, $moodleoverflow, $modulecontext, - true)); + $this->assertTrue(subscriptions::unsubscribe_user($author->id, $moodleoverflow, $modulecontext, true)); // This removes both the moodleoverflow, and the moodleoverflow records. - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(0, $count); - $options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; - $count = $DB->count_records('moodleoverflow_subscriptions', $options); - $this->assertEquals(0, $count); + $this->assertEquals(0, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); + $this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); // And should have reset the discussion cache value. - $result = \mod_moodleoverflow\subscriptions::fetch_discussion_subscription($moodleoverflow->id, $author->id); + $result = subscriptions::fetch_discussion_subscription($moodleoverflow->id, $author->id); $this->assertIsArray($result); $this->assertFalse(isset($result[$discussion->id])); } @@ -721,20 +575,16 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_unsubscribe // Reset the database after testing. $this->resetAfterTest(true); - // Create a course, with a moodleoverflow. + // Create a course, with a moodleoverflow and get the module context. Create two users enrolled inthe course as students. $course = $this->getDataGenerator()->create_course(); $options = ['course' => $course->id, 'forcesubscribe' => MOODLEOVERFLOW_CHOOSESUBSCRIBE]; $moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options); - - // Get the module context. $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id); $modulecontext = \context_module::instance($cm->id); - - // Create two users enrolled in the course as students. list($author) = $this->helper_create_users($course, 2); // Check that the user is currently unsubscribed to the moodleoverflow. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); // Post a discussion to the moodleoverflow. $discussion = new \stdClass(); @@ -742,54 +592,47 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_unsubscribe unset($post); $discussion->moodleoverflow = $moodleoverflow->id; + // Declare the options for the subscription tables. + $discussoptions = ['userid' => $author->id, 'discussion' => $discussion->id]; + // Check that the user is initially unsubscribed to that discussion. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, - $discussion->id)); + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, $discussion->id)); // Then subscribe them to the discussion. - $this->assertTrue(\mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id, - $discussion, $modulecontext)); + $this->assertTrue(subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext)); // An attempt to subscribe again should result in a falsey return to indicate that no change was made. - $this->assertFalse(\mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id, - $discussion, $modulecontext)); + $this->assertFalse(subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext)); // Check that the user is still unsubscribed from the moodleoverflow. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); // But subscribed to the discussion. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, - $discussion->id)); + $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, $discussion->id)); // There should be a record in the discussion subscription tracking table. - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(1, $count); + $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); // Now unsubscribe the user again from the discussion. - \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); + subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); // Check that the user is still unsubscribed from the moodleoverflow. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); // And is unsubscribed from the discussion again. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, - $discussion->id)); + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, $discussion->id)); // There should be no record in the discussion subscription tracking table. - $options = ['userid' => $author->id, 'discussion' => $discussion->id]; - $count = $DB->count_records('moodleoverflow_discuss_subs', $options); - $this->assertEquals(0, $count); + $this->assertEquals(0, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); // And subscribe the user again to the discussion. - \mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext); + subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext); // And is subscribed to the discussion again. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, - $discussion->id)); + $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, $discussion->id)); // Check that the user is still unsubscribed from the moodleoverflow. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); // There should be a record in the discussion subscription tracking table. $options = ['userid' => $author->id, 'discussion' => $discussion->id]; @@ -797,14 +640,13 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_unsubscribe $this->assertEquals(1, $count); // And unsubscribe again. - \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); + subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext); // But unsubscribed from the discussion. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, - $discussion->id)); + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, $discussion->id)); // Check that the user is still unsubscribed from the moodleoverflow. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); + $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); // There should be no record in the discussion subscription tracking table. $options = ['userid' => $author->id, 'discussion' => $discussion->id]; @@ -822,37 +664,34 @@ public function test_fetch_subscribed_users_subscriptions(): void { // Reset the database after testing. $this->resetAfterTest(true); - // Create a course, with a moodleoverflow. where users are initially subscribed. + // Create a course, with a moodleoverflow. where users are initially subscribed and get the module context. $course = $this->getDataGenerator()->create_course(); $options = ['course' => $course->id, 'forcesubscribe' => MOODLEOVERFLOW_INITIALSUBSCRIBE]; $moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options); - - // Get the module context. $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id); $modulecontext = \context_module::instance($cm->id); - // Create some user enrolled in the course as a student. + // Create five user enrolled in the course as a student. $usercount = 5; $users = $this->helper_create_users($course, $usercount); // All users should be subscribed. - $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); + $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); $this->assertEquals($usercount, count($subscribers)); // Subscribe the guest user too to the moodleoverflow - they should never be returned by this function. $this->getDataGenerator()->enrol_user($CFG->siteguest, $course->id); - $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); + $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); $this->assertEquals($usercount, count($subscribers)); // Unsubscribe 2 users. - $unsubscribedcount = 2; - for ($i = 0; $i < $unsubscribedcount; $i++) { - \mod_moodleoverflow\subscriptions::unsubscribe_user($users[$i]->id, $moodleoverflow, $modulecontext); + for ($i = 0; $i < 2; $i++) { + subscriptions::unsubscribe_user($users[$i]->id, $moodleoverflow, $modulecontext); } // The subscription count should now take into account those users who have been unsubscribed. - $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); - $this->assertEquals($usercount - $unsubscribedcount, count($subscribers)); + $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); + $this->assertEquals($usercount - 2, count($subscribers)); } /** @@ -877,7 +716,7 @@ public function test_fetch_subscribed_users_forced(): void { $this->helper_create_users($course, $usercount); // All users should be subscribed. - $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); + $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); $this->assertEquals($usercount, count($subscribers)); } @@ -910,21 +749,19 @@ public function test_fetch_subscribed_users_discussion_subscriptions(): void { $discussion->moodleoverflow = $moodleoverflow->id; // All users should be subscribed. - $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); + $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); $this->assertEquals($usercount, count($subscribers)); - $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null, - true); + $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null, true); $this->assertEquals($usercount, count($subscribers)); - \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($users[0]->id, $discussion, $modulecontext); + subscriptions::unsubscribe_user_from_discussion($users[0]->id, $discussion, $modulecontext); // All users should be subscribed. - $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); + $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); $this->assertEquals($usercount, count($subscribers)); // All users should be subscribed. - $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null, - true); + $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null, true); $this->assertEquals($usercount, count($subscribers)); // Manually insert an extra subscription for one of the users. @@ -936,35 +773,27 @@ public function test_fetch_subscribed_users_discussion_subscriptions(): void { $DB->insert_record('moodleoverflow_discuss_subs', $record); // The discussion count should not have changed. - $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); + $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); $this->assertEquals($usercount, count($subscribers)); - $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null, - true); + $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null, true); $this->assertEquals($usercount, count($subscribers)); // Unsubscribe 2 users. - $unsubscribedcount = 2; - for ($i = 0; $i < $unsubscribedcount; $i++) { - \mod_moodleoverflow\subscriptions::unsubscribe_user($users[$i]->id, $moodleoverflow, $modulecontext); - } + subscriptions::unsubscribe_user($users[0]->id, $moodleoverflow, $modulecontext); + subscriptions::unsubscribe_user($users[1]->id, $moodleoverflow, $modulecontext); // The subscription count should now take into account those users who have been unsubscribed. - $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); - $this->assertEquals($usercount - $unsubscribedcount, count($subscribers)); - $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null, - true); - $this->assertEquals($usercount - $unsubscribedcount, count($subscribers)); + $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); + $this->assertEquals($usercount - 2, count($subscribers)); + $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null, true); + $this->assertEquals($usercount - 2, count($subscribers)); // Now subscribe one of those users back to the discussion. - $subedusers = 1; - for ($i = 0; $i < $subedusers; $i++) { - \mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($users[$i]->id, $discussion, $modulecontext); - } - $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); - $this->assertEquals($usercount - $unsubscribedcount, count($subscribers)); - $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null, - true); - $this->assertEquals($usercount - $unsubscribedcount + $subedusers, count($subscribers)); + subscriptions::subscribe_user_to_discussion($users[0]->id, $discussion, $modulecontext); + $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext); + $this->assertEquals($usercount - 2, count($subscribers)); + $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null, true); + $this->assertEquals($usercount - 1, count($subscribers)); } /** @@ -972,7 +801,6 @@ public function test_fetch_subscribed_users_discussion_subscriptions(): void { */ public function test_force_subscribed_to_moodleoverflow(): void { global $DB; - // Reset database after testing. $this->resetAfterTest(true); @@ -992,7 +820,7 @@ public function test_force_subscribed_to_moodleoverflow(): void { $this->getDataGenerator()->enrol_user($user->id, $course->id, $roleids['student']); // Check that the user is currently subscribed to the moodleoverflow. - $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($user->id, $moodleoverflow, $context)); + $this->assertTrue(subscriptions::is_subscribed($user->id, $moodleoverflow, $context)); assign_capability('mod/moodleoverflow:allowforcesubscribe', CAP_PROHIBIT, $roleids['student'], $context); @@ -1018,17 +846,17 @@ public function test_subscription_cache_prefill(): void { $users = $this->helper_create_users($course, 20); // Reset the subscription cache. - \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache(); + subscriptions::reset_moodleoverflow_cache(); // Filling the subscription cache should only use a single query, except for Postgres, which delegates actual reading // to Cursors, thus tripling the amount of queries. We intend to test the cache, though, so no worries. - $this->assertNull(\mod_moodleoverflow\subscriptions::fill_subscription_cache($moodleoverflow->id)); + $this->assertNull(subscriptions::fill_subscription_cache($moodleoverflow->id)); $postfillcount = $DB->perf_get_reads(); // Now fetch some subscriptions from that moodleoverflow - these should use // the cache and not perform additional queries. foreach ($users as $user) { - $this->assertTrue(\mod_moodleoverflow\subscriptions::fetch_subscription_cache($moodleoverflow->id, $user->id)); + $this->assertTrue(subscriptions::fetch_subscription_cache($moodleoverflow->id, $user->id)); } $finalcount = $DB->perf_get_reads(); $this->assertEquals($finalcount, $postfillcount); @@ -1053,14 +881,14 @@ public function test_subscription_cache_fill(): void { $users = $this->helper_create_users($course, 20); // Reset the subscription cache. - \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache(); + subscriptions::reset_moodleoverflow_cache(); // Filling the subscription cache should only use a single query. $startcount = $DB->perf_get_reads(); // Fetch some subscriptions from that moodleoverflow - these should not use the cache and will perform additional queries. foreach ($users as $user) { - $this->assertTrue(\mod_moodleoverflow\subscriptions::fetch_subscription_cache($moodleoverflow->id, $user->id)); + $this->assertTrue(subscriptions::fetch_subscription_cache($moodleoverflow->id, $user->id)); } $finalcount = $DB->perf_get_reads(); $this->assertEquals(20, $finalcount - $startcount); @@ -1080,34 +908,35 @@ public function test_discussion_subscription_cache_fill_for_course(): void { // Create the moodleoverflows. $options = ['course' => $course->id, 'forcesubscribe' => MOODLEOVERFLOW_DISALLOWSUBSCRIBE]; - $disallowmoodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options); + $disallowobject = $this->getDataGenerator()->create_module('moodleoverflow', $options); $options = ['course' => $course->id, 'forcesubscribe' => MOODLEOVERFLOW_CHOOSESUBSCRIBE]; - $choosemoodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options); + $chooseobject = $this->getDataGenerator()->create_module('moodleoverflow', $options); $options = ['course' => $course->id, 'forcesubscribe' => MOODLEOVERFLOW_INITIALSUBSCRIBE]; - $initialmoodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options); + $initialobject = $this->getDataGenerator()->create_module('moodleoverflow', $options); // Create some users and keep a reference to the first user. $users = $this->helper_create_users($course, 20); $user = reset($users); // Reset the subscription caches. - \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache(); + subscriptions::reset_moodleoverflow_cache(); - $result = \mod_moodleoverflow\subscriptions::fill_subscription_cache_for_course($course->id, $user->id); + $result = subscriptions::fill_subscription_cache_for_course($course->id, $user->id); $this->assertNull($result); $postfillcount = $DB->perf_get_reads(); - $this->assertFalse(\mod_moodleoverflow\subscriptions::fetch_subscription_cache($disallowmoodleoverflow->id, $user->id)); - $this->assertFalse(\mod_moodleoverflow\subscriptions::fetch_subscription_cache($choosemoodleoverflow->id, $user->id)); - $this->assertTrue(\mod_moodleoverflow\subscriptions::fetch_subscription_cache($initialmoodleoverflow->id, $user->id)); + $this->assertFalse(subscriptions::fetch_subscription_cache($disallowobject->id, $user->id)); + $this->assertFalse(subscriptions::fetch_subscription_cache($chooseobject->id, $user->id)); + $this->assertTrue(subscriptions::fetch_subscription_cache($initialobject->id, $user->id)); $finalcount = $DB->perf_get_reads(); $this->assertEquals(0, $finalcount - $postfillcount); // Test for all users. foreach ($users as $user) { - $result = \mod_moodleoverflow\subscriptions::fill_subscription_cache_for_course($course->id, $user->id); - $this->assertFalse(\mod_moodleoverflow\subscriptions::fetch_subscription_cache($disallowmoodleoverflow->id, $user->id)); - $this->assertFalse(\mod_moodleoverflow\subscriptions::fetch_subscription_cache($choosemoodleoverflow->id, $user->id)); - $this->assertTrue(\mod_moodleoverflow\subscriptions::fetch_subscription_cache($initialmoodleoverflow->id, $user->id)); + $result = subscriptions::fill_subscription_cache_for_course($course->id, $user->id); + $this->assertNull($result); + $this->assertFalse(subscriptions::fetch_subscription_cache($disallowobject->id, $user->id)); + $this->assertFalse(subscriptions::fetch_subscription_cache($chooseobject->id, $user->id)); + $this->assertTrue(subscriptions::fetch_subscription_cache($initialobject->id, $user->id)); } $finalcount = $DB->perf_get_reads(); $reads = $finalcount - $postfillcount; @@ -1162,24 +991,24 @@ public function test_discussion_subscription_cache_prefill(): void { if ($usercount % 2) { continue; } - \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion, $modulecontext); + subscriptions::unsubscribe_user_from_discussion($user->id, $data, $modulecontext); $usercount++; } $moodleoverflowcount++; } // Reset the subscription caches. - \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache(); - \mod_moodleoverflow\subscriptions::reset_discussion_cache(); + subscriptions::reset_moodleoverflow_cache(); + subscriptions::reset_discussion_cache(); // Filling the discussion subscription cache should only use a single query. - $this->assertNull(\mod_moodleoverflow\subscriptions::fill_discussion_subscription_cache($moodleoverflow->id)); + $this->assertNull(subscriptions::fill_discussion_subscription_cache($moodleoverflow->id)); $postfillcount = $DB->perf_get_reads(); // Now fetch some subscriptions from that moodleoverflow - these should use // the cache and not perform additional queries. foreach ($users as $user) { - $result = \mod_moodleoverflow\subscriptions::fetch_discussion_subscription($moodleoverflow->id, $user->id); + $result = subscriptions::fetch_discussion_subscription($moodleoverflow->id, $user->id); $this->assertIsArray($result); } $finalcount = $DB->perf_get_reads(); @@ -1229,22 +1058,22 @@ public function test_discussion_subscription_cache_fill(): void { if ($usercount % 2) { continue; } - \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion, $modulecontext); + subscriptions::unsubscribe_user_from_discussion($user->id, $data, $modulecontext); $usercount++; } $moodleoverflowcount++; } // Reset the subscription caches. - \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache(); - \mod_moodleoverflow\subscriptions::reset_discussion_cache(); + subscriptions::reset_moodleoverflow_cache(); + subscriptions::reset_discussion_cache(); $startcount = $DB->perf_get_reads(); // Now fetch some subscriptions from that moodleoverflow - these should use // the cache and not perform additional queries. foreach ($users as $user) { - $result = \mod_moodleoverflow\subscriptions::fetch_discussion_subscription($moodleoverflow->id, $user->id); + $result = subscriptions::fetch_discussion_subscription($moodleoverflow->id, $user->id); $this->assertIsArray($result); } $finalcount = $DB->perf_get_reads(); @@ -1268,16 +1097,12 @@ public function test_moodleoverflow_subscribe_toggle_as_other_repeat_subscriptio // Reset the database after testing. $this->resetAfterTest(true); - // Create a course, with a moodleoverflow. + // Create a course, with a moodleoverflow and get the module context. Create a user enrolled in the course as a student. $course = $this->getDataGenerator()->create_course(); $options = ['course' => $course->id, 'forcesubscribe' => MOODLEOVERFLOW_CHOOSESUBSCRIBE]; $moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options); - - // Get the module context. $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id); $modulecontext = \context_module::instance($cm->id); - - // Create a user enrolled in the course as a student. list($user) = $this->helper_create_users($course, 1); // Post a discussion to the moodleoverflow. @@ -1286,73 +1111,52 @@ public function test_moodleoverflow_subscribe_toggle_as_other_repeat_subscriptio unset($post); $discussion->moodleoverflow = $moodleoverflow->id; + // Declare the options for the subscription tables. + $discussoptions = ['userid' => $author->id, 'discussion' => $discussion->id]; + $subscriptionoptions = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id]; + // Confirm that the user is currently not subscribed to the moodleoverflow. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($user->id, $moodleoverflow, $modulecontext)); + $this->assertFalse(subscriptions::is_subscribed($user->id, $moodleoverflow, $modulecontext)); // Confirm that the user is unsubscribed from the discussion too. - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($user->id, $moodleoverflow, $modulecontext, - $discussion->id)); + $this->assertFalse(subscriptions::is_subscribed($user->id, $moodleoverflow, $modulecontext, $discussion->id)); // Confirm that we have no records in either of the subscription tables. - $this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', [ - 'userid' => $user->id, - 'moodleoverflow' => $moodleoverflow->id, - ])); - $this->assertEquals(0, $DB->count_records('moodleoverflow_discuss_subs', [ - 'userid' => $user->id, - 'discussion' => $discussion->id, - ])); + $this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(0, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); // Subscribing to the moodleoverflow should create a record in the subscriptions table, // but not the moodleoverflow discussion subscriptions table. - \mod_moodleoverflow\subscriptions::subscribe_user($user->id, $moodleoverflow, $modulecontext); - $this->assertEquals(1, $DB->count_records('moodleoverflow_subscriptions', [ - 'userid' => $user->id, - 'moodleoverflow' => $moodleoverflow->id, - ])); - $this->assertEquals(0, $DB->count_records('moodleoverflow_discuss_subs', [ - 'userid' => $user->id, - 'discussion' => $discussion->id, - ])); + subscriptions::subscribe_user($user->id, $moodleoverflow, $modulecontext); + $this->assertEquals(1, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(0, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); // Now unsubscribe from the discussion. This should return true. $uid = $user->id; - $this->assertTrue(\mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($uid, $discussion, $modulecontext)); + $this->assertTrue(subscriptions::unsubscribe_user_from_discussion($uid, $discussion, $modulecontext)); // Attempting to unsubscribe again should return false because no change was made. - $this->assertFalse(\mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($uid, $discussion, $modulecontext)); + $this->assertFalse(subscriptions::unsubscribe_user_from_discussion($uid, $discussion, $modulecontext)); // Subscribing to the discussion again should return truthfully as the subscription preference was removed. - $this->assertTrue(\mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($user->id, $discussion, $modulecontext)); + $this->assertTrue(subscriptions::subscribe_user_to_discussion($user->id, $discussion, $modulecontext)); // Attempting to subscribe again should return false because no change was made. - $this->assertFalse(\mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($user->id, $discussion, $modulecontext)); + $this->assertFalse(subscriptions::subscribe_user_to_discussion($user->id, $discussion, $modulecontext)); // Now unsubscribe from the discussion. This should return true once more. - $this->assertTrue(\mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($uid, $discussion, $modulecontext)); + $this->assertTrue(subscriptions::unsubscribe_user_from_discussion($uid, $discussion, $modulecontext)); // And unsubscribing from the moodleoverflow but not as a request from the user should maintain their preference. - \mod_moodleoverflow\subscriptions::unsubscribe_user($user->id, $moodleoverflow, $modulecontext); + subscriptions::unsubscribe_user($user->id, $moodleoverflow, $modulecontext); - $this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', [ - 'userid' => $user->id, - 'moodleoverflow' => $moodleoverflow->id, - ])); - $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', [ - 'userid' => $user->id, - 'discussion' => $discussion->id, - ])); + $this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); // Subscribing to the discussion should return truthfully because a change was made. - $this->assertTrue(\mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($user->id, $discussion, $modulecontext)); - $this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', [ - 'userid' => $user->id, - 'moodleoverflow' => $moodleoverflow->id, - ])); - $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', [ - 'userid' => $user->id, - 'discussion' => $discussion->id, - ])); + $this->assertTrue(subscriptions::subscribe_user_to_discussion($user->id, $discussion, $modulecontext)); + $this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', $subscriptionoptions)); + $this->assertEquals(1, $DB->count_records('moodleoverflow_discuss_subs', $discussoptions)); } /** @@ -1361,20 +1165,8 @@ public function test_moodleoverflow_subscribe_toggle_as_other_repeat_subscriptio * @return array */ public static function is_subscribable_moodleoverflows() { - return [ - [ - 'forcesubscribe' => MOODLEOVERFLOW_DISALLOWSUBSCRIBE, - ], - [ - 'forcesubscribe' => MOODLEOVERFLOW_CHOOSESUBSCRIBE, - ], - [ - 'forcesubscribe' => MOODLEOVERFLOW_INITIALSUBSCRIBE, - ], - [ - 'forcesubscribe' => MOODLEOVERFLOW_FORCESUBSCRIBE, - ], - ]; + return [['forcesubscribe' => MOODLEOVERFLOW_DISALLOWSUBSCRIBE], ['forcesubscribe' => MOODLEOVERFLOW_CHOOSESUBSCRIBE], + ['forcesubscribe' => MOODLEOVERFLOW_INITIALSUBSCRIBE], ['forcesubscribe' => MOODLEOVERFLOW_FORCESUBSCRIBE], ]; } /** @@ -1387,7 +1179,6 @@ public static function is_subscribable_provider(): array { foreach (self::is_subscribable_moodleoverflows() as $moodleoverflow) { $data[] = [$moodleoverflow]; } - return $data; } @@ -1407,8 +1198,7 @@ public function test_is_subscribable_logged_out($options): void { $options['course'] = $course->id; $moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options); - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, - \context_module::instance($moodleoverflow->cmid))); + $this->assertFalse(subscriptions::is_subscribable($moodleoverflow, \context_module::instance($moodleoverflow->cmid))); } /** @@ -1432,8 +1222,7 @@ public function test_is_subscribable_is_guest($options): void { $options['course'] = $course->id; $moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options); - $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, - \context_module::instance($moodleoverflow->cmid))); + $this->assertFalse(subscriptions::is_subscribable($moodleoverflow, \context_module::instance($moodleoverflow->cmid))); } /** @@ -1442,22 +1231,10 @@ public function test_is_subscribable_is_guest($options): void { */ public static function is_subscribable_loggedin_provider(): array { return [ - [ - ['forcesubscribe' => MOODLEOVERFLOW_DISALLOWSUBSCRIBE], - false, - ], - [ - ['forcesubscribe' => MOODLEOVERFLOW_CHOOSESUBSCRIBE], - true, - ], - [ - ['forcesubscribe' => MOODLEOVERFLOW_INITIALSUBSCRIBE], - true, - ], - [ - ['forcesubscribe' => MOODLEOVERFLOW_FORCESUBSCRIBE], - false, - ], + [['forcesubscribe' => MOODLEOVERFLOW_DISALLOWSUBSCRIBE], false], + [['forcesubscribe' => MOODLEOVERFLOW_CHOOSESUBSCRIBE], true], + [['forcesubscribe' => MOODLEOVERFLOW_INITIALSUBSCRIBE], true], + [['forcesubscribe' => MOODLEOVERFLOW_FORCESUBSCRIBE], false], ]; } @@ -1484,7 +1261,7 @@ public function test_is_subscribable_loggedin($options, $expect): void { $this->getDataGenerator()->enrol_user($user->id, $course->id); $this->setUser($user); - $this->assertEquals($expect, \mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, - \context_module::instance($moodleoverflow->cmid))); + $this->assertEquals($expect, subscriptions::is_subscribable($moodleoverflow, + \context_module::instance($moodleoverflow->cmid))); } } diff --git a/tests/userstats_test.php b/tests/userstats_test.php index 080a11dd95..037b1db1f6 100644 --- a/tests/userstats_test.php +++ b/tests/userstats_test.php @@ -230,7 +230,6 @@ public function test_total_anonymous(): void { // Get the current userstats to compare later. $olduserstats = $this->create_statstable(); - $oldupvotesuser1 = $this->get_specific_userstats($olduserstats, $this->user1, 'receivedupvotes'); $oldactivityuser1 = $this->get_specific_userstats($olduserstats, $this->user1, 'forumactivity'); $oldupvotesuser2 = $this->get_specific_userstats($olduserstats, $this->user2, 'receivedupvotes');