[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 3 // This file is part of Moodle - http://moodle.org/ 4 // 5 // Moodle is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // Moodle is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 17 18 /** 19 * The module forums external functions unit tests 20 * 21 * @package mod_forum 22 * @category external 23 * @copyright 2013 Andrew Nicols 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 global $CFG; 30 31 class mod_forum_maildigest_testcase extends advanced_testcase { 32 33 /** 34 * Keep track of the message and mail sinks that we set up for each 35 * test. 36 * 37 * @var stdClass $helper 38 */ 39 protected $helper; 40 41 /** 42 * Set up message and mail sinks, and set up other requirements for the 43 * cron to be tested here. 44 */ 45 public function setUp() { 46 global $CFG; 47 48 $this->helper = new stdClass(); 49 50 // Messaging is not compatible with transactions... 51 $this->preventResetByRollback(); 52 53 // Catch all messages 54 $this->helper->messagesink = $this->redirectMessages(); 55 $this->helper->mailsink = $this->redirectEmails(); 56 57 // Confirm that we have an empty message sink so far. 58 $messages = $this->helper->messagesink->get_messages(); 59 $this->assertEquals(0, count($messages)); 60 61 $messages = $this->helper->mailsink->get_messages(); 62 $this->assertEquals(0, count($messages)); 63 64 // Tell Moodle that we've not sent any digest messages out recently. 65 $CFG->digestmailtimelast = 0; 66 67 // And set the digest sending time to a negative number - this has 68 // the effect of making it 11pm the previous day. 69 $CFG->digestmailtime = -1; 70 71 // Forcibly reduce the maxeditingtime to a one second to ensure that 72 // messages are sent out. 73 $CFG->maxeditingtime = 1; 74 75 // Ensure that we don't prevent e-mail as this will cause unit test failures. 76 $CFG->noemailever = false; 77 78 // We must clear the subscription caches. This has to be done both before each test, and after in case of other 79 // tests using these functions. 80 \mod_forum\subscriptions::reset_forum_cache(); 81 \mod_forum\subscriptions::reset_discussion_cache(); 82 } 83 84 /** 85 * Clear the message sinks set up in this test. 86 */ 87 public function tearDown() { 88 $this->helper->messagesink->clear(); 89 $this->helper->messagesink->close(); 90 91 $this->helper->mailsink->clear(); 92 $this->helper->mailsink->close(); 93 } 94 95 /** 96 * Setup a user, course, and forums. 97 * 98 * @return stdClass containing the list of forums, courses, forumids, 99 * and the user enrolled in them. 100 */ 101 protected function helper_setup_user_in_course() { 102 global $DB; 103 104 $return = new stdClass(); 105 $return->courses = new stdClass(); 106 $return->forums = new stdClass(); 107 $return->forumids = array(); 108 109 // Create a user. 110 $user = $this->getDataGenerator()->create_user(); 111 $return->user = $user; 112 113 // Create courses to add the modules. 114 $return->courses->course1 = $this->getDataGenerator()->create_course(); 115 116 // Create forums. 117 $record = new stdClass(); 118 $record->course = $return->courses->course1->id; 119 $record->forcesubscribe = 1; 120 121 $return->forums->forum1 = $this->getDataGenerator()->create_module('forum', $record); 122 $return->forumsids[] = $return->forums->forum1->id; 123 124 $return->forums->forum2 = $this->getDataGenerator()->create_module('forum', $record); 125 $return->forumsids[] = $return->forums->forum2->id; 126 127 // Check the forum was correctly created. 128 list ($test, $params) = $DB->get_in_or_equal($return->forumsids); 129 130 // Enrol the user in the courses. 131 // DataGenerator->enrol_user automatically sets a role for the user 132 $this->getDataGenerator()->enrol_user($return->user->id, $return->courses->course1->id); 133 134 return $return; 135 } 136 137 /** 138 * Helper to falsify all forum post records for a digest run. 139 */ 140 protected function helper_force_digest_mail_times() { 141 global $CFG, $DB; 142 // Fake all of the post editing times because digests aren't sent until 143 // the start of an hour where the modification time on the message is before 144 // the start of that hour 145 $digesttime = usergetmidnight(time(), $CFG->timezone) + ($CFG->digestmailtime * 3600) - (60 * 60); 146 $DB->set_field('forum_posts', 'modified', $digesttime, array('mailed' => 0)); 147 $DB->set_field('forum_posts', 'created', $digesttime, array('mailed' => 0)); 148 } 149 150 /** 151 * Run the forum cron, and check that the specified post was sent the 152 * specified number of times. 153 * 154 * @param integer $expected The number of times that the post should have been sent 155 * @return array An array of the messages caught by the message sink 156 */ 157 protected function helper_run_cron_check_count($expected, $messagecount, $mailcount) { 158 if ($expected === 0) { 159 $this->expectOutputRegex('/(Email digests successfully sent to .* users.){0}/'); 160 } else { 161 $this->expectOutputRegex("/Email digests successfully sent to {$expected} users/"); 162 } 163 forum_cron(); 164 165 // Now check the results in the message sink. 166 $messages = $this->helper->messagesink->get_messages(); 167 // There should be the expected number of messages. 168 $this->assertEquals($messagecount, count($messages)); 169 170 // Now check the results in the mail sink. 171 $messages = $this->helper->mailsink->get_messages(); 172 // There should be the expected number of messages. 173 $this->assertEquals($mailcount, count($messages)); 174 175 return $messages; 176 } 177 178 public function test_set_maildigest() { 179 global $DB; 180 181 $this->resetAfterTest(true); 182 183 $helper = $this->helper_setup_user_in_course(); 184 $user = $helper->user; 185 $course1 = $helper->courses->course1; 186 $forum1 = $helper->forums->forum1; 187 188 // Set to the user. 189 self::setUser($helper->user); 190 191 // Confirm that there is no current value. 192 $currentsetting = $DB->get_record('forum_digests', array( 193 'forum' => $forum1->id, 194 'userid' => $user->id, 195 )); 196 $this->assertFalse($currentsetting); 197 198 // Test with each of the valid values: 199 // 0, 1, and 2 are valid values. 200 forum_set_user_maildigest($forum1, 0, $user); 201 $currentsetting = $DB->get_record('forum_digests', array( 202 'forum' => $forum1->id, 203 'userid' => $user->id, 204 )); 205 $this->assertEquals($currentsetting->maildigest, 0); 206 207 forum_set_user_maildigest($forum1, 1, $user); 208 $currentsetting = $DB->get_record('forum_digests', array( 209 'forum' => $forum1->id, 210 'userid' => $user->id, 211 )); 212 $this->assertEquals($currentsetting->maildigest, 1); 213 214 forum_set_user_maildigest($forum1, 2, $user); 215 $currentsetting = $DB->get_record('forum_digests', array( 216 'forum' => $forum1->id, 217 'userid' => $user->id, 218 )); 219 $this->assertEquals($currentsetting->maildigest, 2); 220 221 // And the default value - this should delete the record again 222 forum_set_user_maildigest($forum1, -1, $user); 223 $currentsetting = $DB->get_record('forum_digests', array( 224 'forum' => $forum1->id, 225 'userid' => $user->id, 226 )); 227 $this->assertFalse($currentsetting); 228 229 // Try with an invalid value. 230 $this->setExpectedException('moodle_exception'); 231 forum_set_user_maildigest($forum1, 42, $user); 232 } 233 234 public function test_get_user_digest_options_default() { 235 global $USER, $DB; 236 237 $this->resetAfterTest(true); 238 239 // Set up a basic user enrolled in a course. 240 $helper = $this->helper_setup_user_in_course(); 241 $user = $helper->user; 242 $course1 = $helper->courses->course1; 243 $forum1 = $helper->forums->forum1; 244 245 // Set to the user. 246 self::setUser($helper->user); 247 248 // We test against these options. 249 $digestoptions = array( 250 '0' => get_string('emaildigestoffshort', 'mod_forum'), 251 '1' => get_string('emaildigestcompleteshort', 'mod_forum'), 252 '2' => get_string('emaildigestsubjectsshort', 'mod_forum'), 253 ); 254 255 // The default settings is 0. 256 $this->assertEquals(0, $user->maildigest); 257 $options = forum_get_user_digest_options(); 258 $this->assertEquals($options[-1], get_string('emaildigestdefault', 'mod_forum', $digestoptions[0])); 259 260 // Update the setting to 1. 261 $USER->maildigest = 1; 262 $this->assertEquals(1, $USER->maildigest); 263 $options = forum_get_user_digest_options(); 264 $this->assertEquals($options[-1], get_string('emaildigestdefault', 'mod_forum', $digestoptions[1])); 265 266 // Update the setting to 2. 267 $USER->maildigest = 2; 268 $this->assertEquals(2, $USER->maildigest); 269 $options = forum_get_user_digest_options(); 270 $this->assertEquals($options[-1], get_string('emaildigestdefault', 'mod_forum', $digestoptions[2])); 271 } 272 273 public function test_get_user_digest_options_sorting() { 274 global $USER, $DB; 275 276 $this->resetAfterTest(true); 277 278 // Set up a basic user enrolled in a course. 279 $helper = $this->helper_setup_user_in_course(); 280 $user = $helper->user; 281 $course1 = $helper->courses->course1; 282 $forum1 = $helper->forums->forum1; 283 284 // Set to the user. 285 self::setUser($helper->user); 286 287 // Retrieve the list of applicable options. 288 $options = forum_get_user_digest_options(); 289 290 // The default option must always be at the top of the list. 291 $lastoption = -2; 292 foreach ($options as $value => $description) { 293 $this->assertGreaterThan($lastoption, $value); 294 $lastoption = $value; 295 } 296 } 297 298 public function test_cron_no_posts() { 299 global $DB; 300 301 $this->resetAfterTest(true); 302 303 $this->helper_force_digest_mail_times(); 304 305 // Initially the forum cron should generate no messages as we've made no posts. 306 $this->helper_run_cron_check_count(0, 0, 0); 307 } 308 309 /** 310 * Sends several notifications to one user as: 311 * * single messages based on a user profile setting. 312 */ 313 public function test_cron_profile_single_mails() { 314 global $DB; 315 316 $this->resetAfterTest(true); 317 318 // Set up a basic user enrolled in a course. 319 $userhelper = $this->helper_setup_user_in_course(); 320 $user = $userhelper->user; 321 $course1 = $userhelper->courses->course1; 322 $forum1 = $userhelper->forums->forum1; 323 $forum2 = $userhelper->forums->forum2; 324 325 // Add some discussions to the forums. 326 $record = new stdClass(); 327 $record->course = $course1->id; 328 $record->userid = $user->id; 329 $record->mailnow = 1; 330 331 // Add 5 discussions to forum 1. 332 $record->forum = $forum1->id; 333 for ($i = 0; $i < 5; $i++) { 334 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 335 } 336 337 // Add 5 discussions to forum 2. 338 $record->forum = $forum2->id; 339 for ($i = 0; $i < 5; $i++) { 340 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 341 } 342 343 // Ensure that the creation times mean that the messages will be sent. 344 $this->helper_force_digest_mail_times(); 345 346 // Set the tested user's default maildigest setting. 347 $DB->set_field('user', 'maildigest', 0, array('id' => $user->id)); 348 349 // Set the maildigest preference for forum1 to default. 350 forum_set_user_maildigest($forum1, -1, $user); 351 352 // Set the maildigest preference for forum2 to default. 353 forum_set_user_maildigest($forum2, -1, $user); 354 355 // No digests mails should be sent, but 10 forum mails will be sent. 356 $this->helper_run_cron_check_count(0, 10, 0); 357 } 358 359 /** 360 * Sends several notifications to one user as: 361 * * daily digests coming from the user profile setting. 362 */ 363 public function test_cron_profile_digest_email() { 364 global $DB, $CFG; 365 366 $this->resetAfterTest(true); 367 368 // Set up a basic user enrolled in a course. 369 $userhelper = $this->helper_setup_user_in_course(); 370 $user = $userhelper->user; 371 $course1 = $userhelper->courses->course1; 372 $forum1 = $userhelper->forums->forum1; 373 $forum2 = $userhelper->forums->forum2; 374 375 // Add a discussion to the forums. 376 $record = new stdClass(); 377 $record->course = $course1->id; 378 $record->userid = $user->id; 379 $record->mailnow = 1; 380 381 // Add 5 discussions to forum 1. 382 $record->forum = $forum1->id; 383 for ($i = 0; $i < 5; $i++) { 384 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 385 } 386 387 // Add 5 discussions to forum 2. 388 $record->forum = $forum2->id; 389 for ($i = 0; $i < 5; $i++) { 390 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 391 } 392 393 // Ensure that the creation times mean that the messages will be sent. 394 $this->helper_force_digest_mail_times(); 395 396 // Set the tested user's default maildigest setting. 397 $DB->set_field('user', 'maildigest', 1, array('id' => $user->id)); 398 399 // Set the maildigest preference for forum1 to default. 400 forum_set_user_maildigest($forum1, -1, $user); 401 402 // Set the maildigest preference for forum2 to default. 403 forum_set_user_maildigest($forum2, -1, $user); 404 405 // One digest mail should be sent, with no notifications, and one e-mail. 406 $this->helper_run_cron_check_count(1, 0, 1); 407 } 408 409 /** 410 * Sends several notifications to one user as: 411 * * daily digests coming from the per-forum setting; and 412 * * single e-mails from the profile setting. 413 */ 414 public function test_cron_mixed_email_1() { 415 global $DB, $CFG; 416 417 $this->resetAfterTest(true); 418 419 // Set up a basic user enrolled in a course. 420 $userhelper = $this->helper_setup_user_in_course(); 421 $user = $userhelper->user; 422 $course1 = $userhelper->courses->course1; 423 $forum1 = $userhelper->forums->forum1; 424 $forum2 = $userhelper->forums->forum2; 425 426 // Add a discussion to the forums. 427 $record = new stdClass(); 428 $record->course = $course1->id; 429 $record->userid = $user->id; 430 $record->mailnow = 1; 431 432 // Add 5 discussions to forum 1. 433 $record->forum = $forum1->id; 434 for ($i = 0; $i < 5; $i++) { 435 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 436 } 437 438 // Add 5 discussions to forum 2. 439 $record->forum = $forum2->id; 440 for ($i = 0; $i < 5; $i++) { 441 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 442 } 443 444 // Ensure that the creation times mean that the messages will be sent. 445 $this->helper_force_digest_mail_times(); 446 447 // Set the tested user's default maildigest setting. 448 $DB->set_field('user', 'maildigest', 0, array('id' => $user->id)); 449 450 // Set the maildigest preference for forum1 to digest. 451 forum_set_user_maildigest($forum1, 1, $user); 452 453 // Set the maildigest preference for forum2 to default (single). 454 forum_set_user_maildigest($forum2, -1, $user); 455 456 // One digest e-mail should be sent, and five individual notifications. 457 $this->helper_run_cron_check_count(1, 5, 1); 458 } 459 460 /** 461 * Sends several notifications to one user as: 462 * * single e-mails from the per-forum setting; and 463 * * daily digests coming from the per-user setting. 464 */ 465 public function test_cron_mixed_email_2() { 466 global $DB, $CFG; 467 468 $this->resetAfterTest(true); 469 470 // Set up a basic user enrolled in a course. 471 $userhelper = $this->helper_setup_user_in_course(); 472 $user = $userhelper->user; 473 $course1 = $userhelper->courses->course1; 474 $forum1 = $userhelper->forums->forum1; 475 $forum2 = $userhelper->forums->forum2; 476 477 // Add a discussion to the forums. 478 $record = new stdClass(); 479 $record->course = $course1->id; 480 $record->userid = $user->id; 481 $record->mailnow = 1; 482 483 // Add 5 discussions to forum 1. 484 $record->forum = $forum1->id; 485 for ($i = 0; $i < 5; $i++) { 486 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 487 } 488 489 // Add 5 discussions to forum 2. 490 $record->forum = $forum2->id; 491 for ($i = 0; $i < 5; $i++) { 492 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 493 } 494 495 // Ensure that the creation times mean that the messages will be sent. 496 $this->helper_force_digest_mail_times(); 497 498 // Set the tested user's default maildigest setting. 499 $DB->set_field('user', 'maildigest', 1, array('id' => $user->id)); 500 501 // Set the maildigest preference for forum1 to digest. 502 forum_set_user_maildigest($forum1, -1, $user); 503 504 // Set the maildigest preference for forum2 to single. 505 forum_set_user_maildigest($forum2, 0, $user); 506 507 // One digest e-mail should be sent, and five individual notifications. 508 $this->helper_run_cron_check_count(1, 5, 1); 509 } 510 511 /** 512 * Sends several notifications to one user as: 513 * * daily digests coming from the per-forum setting. 514 */ 515 public function test_cron_forum_digest_email() { 516 global $DB, $CFG; 517 518 $this->resetAfterTest(true); 519 520 // Set up a basic user enrolled in a course. 521 $userhelper = $this->helper_setup_user_in_course(); 522 $user = $userhelper->user; 523 $course1 = $userhelper->courses->course1; 524 $forum1 = $userhelper->forums->forum1; 525 $forum2 = $userhelper->forums->forum2; 526 527 // Add a discussion to the forums. 528 $record = new stdClass(); 529 $record->course = $course1->id; 530 $record->userid = $user->id; 531 $record->mailnow = 1; 532 533 // Add 5 discussions to forum 1. 534 $record->forum = $forum1->id; 535 for ($i = 0; $i < 5; $i++) { 536 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 537 } 538 539 // Add 5 discussions to forum 2. 540 $record->forum = $forum2->id; 541 for ($i = 0; $i < 5; $i++) { 542 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 543 } 544 545 // Ensure that the creation times mean that the messages will be sent. 546 $this->helper_force_digest_mail_times(); 547 548 // Set the tested user's default maildigest setting. 549 $DB->set_field('user', 'maildigest', 0, array('id' => $user->id)); 550 551 // Set the maildigest preference for forum1 to digest (complete). 552 forum_set_user_maildigest($forum1, 1, $user); 553 554 // Set the maildigest preference for forum2 to digest (short). 555 forum_set_user_maildigest($forum2, 2, $user); 556 557 // One digest e-mail should be sent, and no individual notifications. 558 $this->helper_run_cron_check_count(1, 0, 1); 559 } 560 561 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |