Skip to content

Commit

Permalink
MDL-38440 WebService get_contents doesn't return the full text of labels
Browse files Browse the repository at this point in the history
  • Loading branch information
mouneyrac committed Apr 26, 2013
1 parent 5f8a427 commit 487bc1b
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 10 deletions.
6 changes: 4 additions & 2 deletions course/externallib.php
Expand Up @@ -145,8 +145,10 @@ public static function get_course_contents($courseid, $options = array()) {

$modcontext = context_module::instance($cm->id);

if (!empty($cm->showdescription)) {
$module['description'] = $cm->get_content();
if (!empty($cm->showdescription) or $cm->modname == 'label') {
// We want to use the external format. However from reading get_formatted_content(), get_content() format is always FORMAT_HTML.
list($module['description'], $descriptionformat) = external_format_text($cm->get_content(),
FORMAT_HTML, $modcontext->id, $cm->modname, 'intro', $cm->id);
}

//url of the module
Expand Down
42 changes: 34 additions & 8 deletions course/tests/externallib_test.php
Expand Up @@ -568,28 +568,54 @@ public function test_get_course_contents() {
$this->resetAfterTest(true);

$course = self::getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
$forumdescription = 'This is the forum description';
$forum = $this->getDataGenerator()->create_module('forum',
array('course'=>$course->id, 'intro' => $forumdescription),
array('showdescription' => true));
$forumcm = get_coursemodule_from_id('forum', $forum->cmid);
$forumcontext = context_module::instance($forum->cmid);
$data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
$datacontext = context_module::instance($data->cmid);
$datacm = get_coursemodule_from_instance('page', $data->id);
$page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
$pagecontext = context_module::instance($page->cmid);
$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.';
$label = $this->getDataGenerator()->create_module('label', array('course' => $course->id,
'intro' => $labeldescription));
$labelcm = get_coursemodule_from_instance('label', $label->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);

$courses = core_course_external::get_course_contents($course->id, array());
$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.
$courses = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $courses);
$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);
$modinfo = get_fast_modinfo($course);
$testexecuted = 0;
foreach($firstsection['modules'] as $module) {
if ($module['id'] == $forumcm->id and $module['modname'] == 'forum') {
$cm = $modinfo->cms[$forumcm->id];
$formattedtext = format_text($cm->get_content(), FORMAT_HTML,
array('noclean' => true, 'para' => false, 'filter' => false));
$this->assertEquals($formattedtext, $module['description']);
$testexecuted = $testexecuted + 1;
} else if ($module['id'] == $labelcm->id and $module['modname'] == 'label') {
$cm = $modinfo->cms[$labelcm->id];
$formattedtext = format_text($cm->get_content(), FORMAT_HTML,
array('noclean' => true, 'para' => false, 'filter' => false));
$this->assertEquals($formattedtext, $module['description']);
$testexecuted = $testexecuted + 1;
}
}
$this->assertEquals(2, $testexecuted);

// Check that the course has the 3 created modules
$this->assertEquals(3, count($courses[0]['modules']));
// Check that the only return section has the 4 created modules
$this->assertEquals(4, count($firstsection['modules']));
}

/**
Expand Down
72 changes: 72 additions & 0 deletions mod/label/tests/generator/lib.php
@@ -0,0 +1,72 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* mod_label data generator
*
* @package mod_label
* @category test
* @copyright 2013 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();


/**
* Label module data generator class
*
* @package mod_label
* @category test
* @copyright 2013 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_label_generator extends testing_module_generator {

/**
* Create new label module instance
* @param array|stdClass $record
* @param array $options
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once("$CFG->dirroot/mod/label/lib.php");

$this->instancecount++;
$i = $this->instancecount;

$record = (object)(array)$record;
$options = (array)$options;

if (empty($record->course)) {
throw new coding_exception('module generator requires $record->course');
}
if (!isset($record->name)) {
$record->name = get_string('pluginname', 'label').' '.$i;
}
if (!isset($record->intro)) {
$record->intro = 'Test label '.$i;
}
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}

$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = label_add_instance($record, null);
return $this->post_add_instance($id, $record->coursemodule);
}
}
65 changes: 65 additions & 0 deletions mod/label/tests/generator_test.php
@@ -0,0 +1,65 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* PHPUnit label generator tests
*
* @package mod_label
* @category phpunit
* @copyright 2013 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();


/**
* PHPUnit label generator testcase
*
* @package mod_label
* @category phpunit
* @copyright 2013 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_label_generator_testcase extends advanced_testcase {
public function test_generator() {
global $DB;

$this->resetAfterTest(true);

$this->assertEquals(0, $DB->count_records('label'));

$course = $this->getDataGenerator()->create_course();

/** @var mod_label_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('mod_label');
$this->assertInstanceOf('mod_label_generator', $generator);
$this->assertEquals('label', $generator->get_modulename());

$generator->create_instance(array('course'=>$course->id));
$generator->create_instance(array('course'=>$course->id));
$label = $generator->create_instance(array('course'=>$course->id));
$this->assertEquals(3, $DB->count_records('label'));

$cm = get_coursemodule_from_instance('label', $label->id);
$this->assertEquals($label->id, $cm->instance);
$this->assertEquals('label', $cm->modname);
$this->assertEquals($course->id, $cm->course);

$context = context_module::instance($cm->id);
$this->assertEquals($label->cmid, $context->instanceid);
}
}

0 comments on commit 487bc1b

Please sign in to comment.