Skip to content

Commit

Permalink
MDL-39723 prevent update_courses() bad habits.
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
stronk7 committed Jun 4, 2013
1 parent 79452da commit a182f88
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion course/tests/externallib_test.php
Expand Up @@ -671,7 +671,15 @@ public function test_duplicate_course() {
* 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);

Expand Down Expand Up @@ -724,6 +732,7 @@ public function test_update_courses() {
$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']));
Expand Down

0 comments on commit a182f88

Please sign in to comment.