Skip to content

Commit

Permalink
Merge branch 'MDL-48197-master' of git://github.com/andrewnicols/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Nov 24, 2014
2 parents 406712e + fc05c1b commit 4cbf997
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 34 deletions.
3 changes: 2 additions & 1 deletion auth/ldap/tests/plugin_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,9 @@ public function test_ldap_user_signup() {
$auth = get_auth_plugin('ldap');

$sink = $this->redirectEvents();
$mailsink = $this->redirectEmails();
$auth->user_signup((object)$user, false);
$this->assertDebuggingCalled('Not sending email due to $CFG->noemailever config setting');
$this->assertEquals(1, $mailsink->count());
$events = $sink->get_events();
$sink->close();

Expand Down
2 changes: 0 additions & 2 deletions badges/tests/badgeslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ protected function setUp() {
global $DB, $CFG;
$this->resetAfterTest(true);

unset_config('noemailever');

$CFG->enablecompletion = true;

$user = $this->getDataGenerator()->create_user();
Expand Down
4 changes: 0 additions & 4 deletions course/tests/courserequest_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ public function test_approve_request() {
$this->resetAfterTest(true);
$this->preventResetByRollback();

unset_config('noemailever');

$defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
set_config('enablecourserequests', 1);
set_config('requestcategoryselection', 0);
Expand Down Expand Up @@ -138,8 +136,6 @@ public function test_reject_request() {
$this->resetAfterTest(true);
$this->preventResetByRollback();

unset_config('noemailever');

$this->setAdminUser();
set_config('enablecourserequests', 1);
set_config('requestcategoryselection', 0);
Expand Down
2 changes: 0 additions & 2 deletions lib/phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,6 @@
ini_set('display_errors', '1');
ini_set('log_errors', '1');

$CFG->noemailever = true; // better not mail anybody from tests, override temporarily if necessary

// some ugly hacks
$CFG->themerev = 1;
$CFG->jsrev = 1;
Expand Down
15 changes: 10 additions & 5 deletions lib/phpunit/classes/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,15 @@ public static function reset_all_data($detectchanges = false) {
// Stop any message redirection.
phpunit_util::stop_message_redirection();

// Stop any message redirection.
phpunit_util::stop_phpmailer_redirection();

// Stop any message redirection.
phpunit_util::stop_event_redirection();

// Start a new email redirection.
// This will clear any existing phpmailer redirection.
// We redirect all phpmailer output to this message sink which is
// called instead of phpmailer actually sending the message.
phpunit_util::start_phpmailer_redirection();

// We used to call gc_collect_cycles here to ensure desctructors were called between tests.
// This accounted for 25% of the total time running phpunit - so we removed it.

Expand Down Expand Up @@ -678,9 +681,11 @@ public static function message_sent($message) {
*/
public static function start_phpmailer_redirection() {
if (self::$phpmailersink) {
self::stop_phpmailer_redirection();
// If an existing mailer sink is active, just clear it.
self::$phpmailersink->clear();
} else {
self::$phpmailersink = new phpunit_phpmailer_sink();
}
self::$phpmailersink = new phpunit_phpmailer_sink();
return self::$phpmailersink;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/phpunit/tests/advanced_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ public function test_message_redirection() {
$message2->notification = 0;

// There should be debugging message without redirection.
$mailsink = $this->redirectEmails();
$mailsink->close();
message_send($message1);
$this->assertDebuggingCalled(null, null, 'message_send() must print debug message that messaging is disabled in phpunit tests.');

Expand Down
19 changes: 11 additions & 8 deletions lib/tests/messagelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class core_messagelib_testcase extends advanced_testcase {
public function test_message_provider_disabled() {
$this->resetAfterTest();
$this->preventResetByRollback();
unset_config('noemailever');

// Disable instantmessage provider.
$disableprovidersetting = 'moodle_instantmessage_disable';
Expand Down Expand Up @@ -382,8 +381,6 @@ public function test_send_message() {
$this->preventResetByRollback();
$this->resetAfterTest();

unset_config('noemailever');

$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();

Expand Down Expand Up @@ -717,7 +714,6 @@ public function test_rollback() {

$this->resetAfterTest();
$this->preventResetByRollback();
set_config('noemailever', 1);

$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
Expand All @@ -734,24 +730,30 @@ public function test_rollback() {
$message->smallmessage = 'small message';
$message->notification = '0';

$mailsink = $this->redirectEmails();

// Sending outside of a transaction is fine.
message_send($message);
$this->assertDebuggingCalled('Not sending email due to $CFG->noemailever config setting');
$this->assertEquals(1, $mailsink->count());

$transaction1 = $DB->start_delegated_transaction();

$mailsink->clear();
message_send($message);
$this->assertDebuggingNotCalled();
$this->assertEquals(0, $mailsink->count());

$transaction2 = $DB->start_delegated_transaction();

$mailsink->clear();
message_send($message);
$this->assertDebuggingNotCalled();
$this->assertEquals(0, $mailsink->count());

try {
$transaction2->rollback(new Exception('x'));
$this->fail('Expecting exception');
} catch (Exception $e) {}
$this->assertDebuggingNotCalled();
$this->assertEquals(0, $mailsink->count());

$this->assertTrue($DB->is_transaction_started());

Expand All @@ -760,11 +762,12 @@ public function test_rollback() {
$this->fail('Expecting exception');
} catch (Exception $e) {}
$this->assertDebuggingNotCalled();
$this->assertEquals(0, $mailsink->count());

$this->assertFalse($DB->is_transaction_started());

message_send($message);
$this->assertDebuggingCalled('Not sending email due to $CFG->noemailever config setting');
$this->assertEquals(1, $mailsink->count());
}

public function test_forced_rollback() {
Expand Down
8 changes: 5 additions & 3 deletions lib/tests/moodlelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2607,6 +2607,11 @@ public function test_email_to_user() {
$subject2 = 'subject 2';
$messagetext2 = 'message text 2';

// Close the default email sink.
$sink = $this->redirectEmails();
$sink->close();

$CFG->noemailever = true;
$this->assertNotEmpty($CFG->noemailever);
email_to_user($user1, $user2, $subject, $messagetext);
$this->assertDebuggingCalled('Not sending email due to $CFG->noemailever config setting');
Expand Down Expand Up @@ -2660,9 +2665,6 @@ public function test_setnew_password_and_mail() {

$user = $this->getDataGenerator()->create_user();

// Set config to allow email_to_user() to be called.
$CFG->noemailever = false;

// Update user password.
$sink = $this->redirectEvents();
$sink2 = $this->redirectEmails(); // Make sure we are redirecting emails.
Expand Down
3 changes: 3 additions & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ information provided here is intended especially for developers.
=== 2.9 ===

* \core\event\course_viewed 'other' argument renamed from coursesectionid to coursesectionnumber as it contains the section number.
* PHPUnit: PHPMailer Sink is now started for all tests and is setup within the phpunit wrapper for advanced tests.
Catching debugging messages when sending mail will no longer work. Use $sink = $this->redirectEmails(); and then check
the message in the sink instead.

=== 2.8 ===

Expand Down
3 changes: 0 additions & 3 deletions message/output/email/message_output_email.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ class message_output_email extends message_output {
function send_message($eventdata) {
global $CFG;

// Ignore $CFG->noemailever here because we want to test this code,
// the message sending fails later in email_to_user().

// skip any messaging suspended and deleted users
if ($eventdata->userto->auth === 'nologin' or $eventdata->userto->suspended or $eventdata->userto->deleted) {
return true;
Expand Down
3 changes: 0 additions & 3 deletions mod/forum/tests/mail_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ public function setUp() {
// ensure that messages are sent out.
$CFG->maxeditingtime = -1;

// Ensure that we don't prevent e-mail as this will cause unit test failures.
$CFG->noemailever = false;

$this->helper = $helper;
}

Expand Down
3 changes: 0 additions & 3 deletions mod/forum/tests/maildigest_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ public function setUp() {
// messages are sent out.
$CFG->maxeditingtime = 1;

// Ensure that we don't prevent e-mail as this will cause unit test failures.
$CFG->noemailever = false;

// We must clear the subscription caches. This has to be done both before each test, and after in case of other
// tests using these functions.
\mod_forum\subscriptions::reset_forum_cache();
Expand Down

0 comments on commit 4cbf997

Please sign in to comment.