From 36e5df06b36013b1a982de921b75a94f8d20bdf6 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Tue, 4 Jun 2013 21:45:15 +0200 Subject: [PATCH] MDL-39723 prevent update_courses() bad habits. All the external functions end calling require_login() that is always in charge of setting the $COURSE global. This is not a problem, but in the case of core_course_external::update_courses() testing, where we are, in the same "request", both setting and getting the $COURSE information and it's clearly outdated, so the test fails. Alternative solution would be to modify the external function to ensure that, after updating a course, $COURSE is also updated with the changes but it does not seem to be necessary for "normal" usage (both UI/WS POVs). --- course/tests/externallib_test.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/course/tests/externallib_test.php b/course/tests/externallib_test.php index 59e0c608a78..5fdbd41a9c8 100644 --- a/course/tests/externallib_test.php +++ b/course/tests/externallib_test.php @@ -671,7 +671,15 @@ class core_course_external_testcase extends externallib_advanced_testcase { * Test update_courses */ public function test_update_courses() { - global $DB, $CFG, $USER; + global $DB, $CFG, $USER, $COURSE; + + // Get current $COURSE to be able to restore it later (defaults to $SITE). We need this + // trick because we are both updating and getting (for testing) course information + // in the same request and core_course_external::update_courses() + // is overwriting $COURSE all over the time with OLD values, so later + // use of get_course() fetches those OLD values instead of the updated ones. + // See MDL-39723 for more info. + $origcourse = clone($COURSE); $this->resetAfterTest(true); @@ -724,6 +732,7 @@ class core_course_external_testcase extends externallib_advanced_testcase { $courses = array($course1, $course2); $updatedcoursewarnings = core_course_external::update_courses($courses); + $COURSE = $origcourse; // Restore $COURSE. Instead of using the OLD one set by the previous line. // Check that right number of courses were created. $this->assertEquals(0, count($updatedcoursewarnings['warnings']));