Merge branch 'MDL-51664-master' of git://github.com/FMCorz/moodle
authorDan Poltawski <dan@moodle.com>
Tue, 1 Dec 2015 20:14:59 +0000 (20:14 +0000)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Thu, 3 Dec 2015 10:16:30 +0000 (11:16 +0100)
1  2 
lib/externallib.php
lib/tests/externallib_test.php
mod/chat/classes/external.php
mod/choice/classes/external.php
mod/forum/externallib.php
mod/scorm/classes/external.php

Simple merge
@@@ -265,34 -265,66 +265,95 @@@ class core_externallib_testcase extend
          test_exernal_api::get_context_wrapper(array('roleid' => 3, 'userid' => $USER->id, 'instanceid' => $course->id));
      }
  
 +    public function all_external_info_provider() {
 +        global $DB;
 +
 +        // We are testing here that all the external function descriptions can be generated without
 +        // producing warnings. E.g. misusing optional params will generate a debugging message which
 +        // will fail this test.
 +        $functions = $DB->get_records('external_functions', array(), 'name');
 +        $return = array();
 +        foreach ($functions as $f) {
 +            $return[$f->name] = array($f);
 +        }
 +        return $return;
 +    }
 +
 +    /**
 +     * @dataProvider all_external_info_provider
 +     */
 +    public function test_all_external_info($f) {
 +        $desc = external_function_info($f);
 +        $this->assertNotEmpty($desc->name);
 +        $this->assertNotEmpty($desc->classname);
 +        $this->assertNotEmpty($desc->methodname);
 +        $this->assertEquals($desc->component, clean_param($desc->component, PARAM_COMPONENT));
 +        $this->assertInstanceOf('external_function_parameters', $desc->parameters_desc);
 +        if ($desc->returns_desc != null) {
 +            $this->assertInstanceOf('external_description', $desc->returns_desc);
 +        }
 +    }
++
+     public function test_validate_courses() {
+         $this->resetAfterTest(true);
+         $c1 = $this->getDataGenerator()->create_course();
+         $c2 = $this->getDataGenerator()->create_course();
+         $c3 = $this->getDataGenerator()->create_course();
+         $u1 = $this->getDataGenerator()->create_user();
+         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
+         $courseids = array($c1->id, $c2->id, $c3->id);
+         $this->setAdminUser();
+         list($courses, $warnings) = external_util::validate_courses($courseids);
+         $this->assertEmpty($warnings);
+         $this->assertCount(3, $courses);
+         $this->assertArrayHasKey($c1->id, $courses);
+         $this->assertArrayHasKey($c2->id, $courses);
+         $this->assertArrayHasKey($c3->id, $courses);
+         $this->assertEquals($c1->id, $courses[$c1->id]->id);
+         $this->assertEquals($c2->id, $courses[$c2->id]->id);
+         $this->assertEquals($c3->id, $courses[$c3->id]->id);
+         $this->setUser($u1);
+         list($courses, $warnings) = external_util::validate_courses($courseids);
+         $this->assertCount(2, $warnings);
+         $this->assertEquals($c2->id, $warnings[0]['itemid']);
+         $this->assertEquals($c3->id, $warnings[1]['itemid']);
+         $this->assertCount(1, $courses);
+         $this->assertArrayHasKey($c1->id, $courses);
+         $this->assertArrayNotHasKey($c2->id, $courses);
+         $this->assertArrayNotHasKey($c3->id, $courses);
+         $this->assertEquals($c1->id, $courses[$c1->id]->id);
+     }
+     /**
+      * Validate courses can re-use an array of prefetched courses.
+      */
+     public function test_validate_courses_prefetch() {
+         $this->resetAfterTest(true);
+         $c1 = $this->getDataGenerator()->create_course();
+         $c2 = $this->getDataGenerator()->create_course();
+         $c3 = $this->getDataGenerator()->create_course();
+         $c4 = $this->getDataGenerator()->create_course();
+         $u1 = $this->getDataGenerator()->create_user();
+         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
+         $this->getDataGenerator()->enrol_user($u1->id, $c2->id);
+         $courseids = array($c1->id, $c2->id, $c3->id);
+         $courses = array($c2->id => $c2, $c3->id => $c3, $c4->id => $c4);
+         $this->setUser($u1);
+         list($courses, $warnings) = external_util::validate_courses($courseids, $courses);
+         $this->assertCount(2, $courses);
+         $this->assertCount(1, $warnings);
+         $this->assertArrayHasKey($c1->id, $courses);
+         $this->assertSame($c2, $courses[$c2->id]);
+         $this->assertArrayNotHasKey($c3->id, $courses);
+         // The extra course passed is not returned.
+         $this->assertArrayNotHasKey($c4->id, $courses);
+     }
  }
  
  /*
Simple merge
Simple merge
Simple merge
Simple merge