MDL-49242 webservices: Units tests for core_course_get_contents
This commit is contained in:
@@ -597,20 +597,19 @@ class core_course_externallib_testcase extends externallib_advanced_testcase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_course_contents
|
||||
* Create a course with contents
|
||||
* @return array A list with the course object and course modules objects
|
||||
*/
|
||||
public function test_get_course_contents() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
private function prepare_get_course_contents_test() {
|
||||
$course = self::getDataGenerator()->create_course();
|
||||
$forumdescription = 'This is the forum description';
|
||||
$forum = $this->getDataGenerator()->create_module('forum',
|
||||
array('course'=>$course->id, 'intro' => $forumdescription),
|
||||
array('course' => $course->id, 'intro' => $forumdescription),
|
||||
array('showdescription' => true));
|
||||
$forumcm = get_coursemodule_from_id('forum', $forum->cmid);
|
||||
$data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
|
||||
$data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
|
||||
$datacm = get_coursemodule_from_instance('page', $data->id);
|
||||
$page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
|
||||
$page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
|
||||
$pagecm = get_coursemodule_from_instance('page', $page->id);
|
||||
$labeldescription = 'This is a very long label to test if more than 50 characters are returned.
|
||||
So bla bla bla bla <b>bold bold bold</b> bla bla bla bla.';
|
||||
@@ -618,23 +617,36 @@ class core_course_externallib_testcase extends externallib_advanced_testcase {
|
||||
'intro' => $labeldescription));
|
||||
$labelcm = get_coursemodule_from_instance('label', $label->id);
|
||||
$url = $this->getDataGenerator()->create_module('url', array('course' => $course->id,
|
||||
'name' => 'URL: % & $ ../'));
|
||||
'name' => 'URL: % & $ ../', 'section' => 2));
|
||||
$urlcm = get_coursemodule_from_instance('url', $url->id);
|
||||
|
||||
// Set the required capabilities by the external function.
|
||||
$context = context_course::instance($course->id);
|
||||
$roleid = $this->assignUserCapability('moodle/course:view', $context->id);
|
||||
$this->assignUserCapability('moodle/course:update', $context->id, $roleid);
|
||||
|
||||
$sections = core_course_external::get_course_contents($course->id, array());
|
||||
return array($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_course_contents
|
||||
*/
|
||||
public function test_get_course_contents() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
|
||||
|
||||
$sections = core_course_external::get_course_contents($course->id, array());
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
|
||||
|
||||
// Check that forum and label descriptions are correctly returned.
|
||||
$firstsection = array_pop($sections);
|
||||
$firstsection = array_shift($sections);
|
||||
$lastsection = array_pop($sections);
|
||||
|
||||
$modinfo = get_fast_modinfo($course);
|
||||
$testexecuted = 0;
|
||||
foreach($firstsection['modules'] as $module) {
|
||||
foreach ($firstsection['modules'] as $module) {
|
||||
if ($module['id'] == $forumcm->id and $module['modname'] == 'forum') {
|
||||
$cm = $modinfo->cms[$forumcm->id];
|
||||
$formattedtext = format_text($cm->content, FORMAT_HTML,
|
||||
@@ -653,8 +665,165 @@ class core_course_externallib_testcase extends externallib_advanced_testcase {
|
||||
}
|
||||
$this->assertEquals(2, $testexecuted);
|
||||
|
||||
// Check that the only return section has the 5 created modules
|
||||
$this->assertEquals(5, count($firstsection['modules']));
|
||||
// Check that the only return section has the 5 created modules.
|
||||
$this->assertCount(4, $firstsection['modules']);
|
||||
$this->assertCount(1, $lastsection['modules']);
|
||||
|
||||
try {
|
||||
$sections = core_course_external::get_course_contents($course->id,
|
||||
array(array("name" => "invalid", "value" => 1)));
|
||||
$this->fail('Exception expected due to invalid option.');
|
||||
} catch (moodle_exception $e) {
|
||||
$this->assertEquals('errorinvalidparam', $e->errorcode);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test get_course_contents excluding modules
|
||||
*/
|
||||
public function test_get_course_contents_excluding_modules() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
|
||||
|
||||
// Test exclude modules.
|
||||
$sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludemodules", "value" => 1)));
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
|
||||
|
||||
$firstsection = array_shift($sections);
|
||||
$lastsection = array_pop($sections);
|
||||
|
||||
$this->assertEmpty($firstsection['modules']);
|
||||
$this->assertEmpty($lastsection['modules']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_course_contents excluding contents
|
||||
*/
|
||||
public function test_get_course_contents_excluding_contents() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
|
||||
|
||||
// Test exclude modules.
|
||||
$sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludecontents", "value" => 1)));
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
|
||||
|
||||
foreach ($sections as $section) {
|
||||
foreach ($section['modules'] as $module) {
|
||||
// Only resources return contents.
|
||||
if (isset($module['contents'])) {
|
||||
$this->assertEmpty($module['contents']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_course_contents filtering by section number
|
||||
*/
|
||||
public function test_get_course_contents_section_number() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
|
||||
|
||||
// Test exclude modules.
|
||||
$sections = core_course_external::get_course_contents($course->id, array(array("name" => "sectionnumber", "value" => 0)));
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
|
||||
|
||||
$this->assertCount(1, $sections);
|
||||
$this->assertCount(4, $sections[0]['modules']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_course_contents filtering by cmid
|
||||
*/
|
||||
public function test_get_course_contents_cmid() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
|
||||
|
||||
// Test exclude modules.
|
||||
$sections = core_course_external::get_course_contents($course->id, array(array("name" => "cmid", "value" => $forumcm->id)));
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
|
||||
|
||||
$this->assertCount(2, $sections);
|
||||
$this->assertCount(1, $sections[0]['modules']);
|
||||
$this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test get_course_contents filtering by cmid and section
|
||||
*/
|
||||
public function test_get_course_contents_section_cmid() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
|
||||
|
||||
// Test exclude modules.
|
||||
$sections = core_course_external::get_course_contents($course->id, array(
|
||||
array("name" => "cmid", "value" => $forumcm->id),
|
||||
array("name" => "sectionnumber", "value" => 0)
|
||||
));
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
|
||||
|
||||
$this->assertCount(1, $sections);
|
||||
$this->assertCount(1, $sections[0]['modules']);
|
||||
$this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_course_contents filtering by modname
|
||||
*/
|
||||
public function test_get_course_contents_modname() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
|
||||
|
||||
// Test exclude modules.
|
||||
$sections = core_course_external::get_course_contents($course->id, array(array("name" => "modname", "value" => "forum")));
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
|
||||
|
||||
$this->assertCount(2, $sections);
|
||||
$this->assertCount(1, $sections[0]['modules']);
|
||||
$this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_course_contents filtering by modname
|
||||
*/
|
||||
public function test_get_course_contents_modid() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
|
||||
|
||||
// Test exclude modules.
|
||||
$sections = core_course_external::get_course_contents($course->id, array(
|
||||
array("name" => "modname", "value" => "page"),
|
||||
array("name" => "modid", "value" => $pagecm->instance),
|
||||
));
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
|
||||
|
||||
$this->assertCount(2, $sections);
|
||||
$this->assertCount(1, $sections[0]['modules']);
|
||||
$this->assertEquals("page", $sections[0]['modules'][0]["modname"]);
|
||||
$this->assertEquals($pagecm->instance, $sections[0]['modules'][0]["instance"]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user