/** * Test update_inplace_editable() */ public function test_update_module_name_inplace() { global $CFG, $DB, $PAGE; require_once $CFG->dirroot . '/lib/external/externallib.php'; $this->setUser($this->getDataGenerator()->create_user()); $this->resetAfterTest(true); $course = $this->getDataGenerator()->create_course(); $forum = self::getDataGenerator()->create_module('forum', array('course' => $course->id, 'name' => 'forum name')); // Call service for core_course component without necessary permissions. try { core_external::update_inplace_editable('core_course', 'activityname', $forum->cmid, 'New forum name'); $this->fail('Exception expected'); } catch (moodle_exception $e) { $this->assertEquals('Course or activity not accessible. (Not enrolled)', $e->getMessage()); } // Change to admin user and make sure that cm name can be updated using web service update_inplace_editable(). $this->setAdminUser(); $res = core_external::update_inplace_editable('core_course', 'activityname', $forum->cmid, 'New forum name'); $res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res); $this->assertEquals('New forum name', $res['value']); $this->assertEquals('New forum name', $DB->get_field('forum', 'name', array('id' => $forum->id))); }
/** * Test update_inplace_editable() */ public function test_update_inplace_editable() { $this->resetAfterTest(true); // Call service for component that does not have inplace_editable callback. try { core_external::update_inplace_editable('tool_log', 'itemtype', 1, 'newvalue'); $this->fail('Exception expected'); } catch (moodle_exception $e) { $this->assertEquals('Error calling update processor', $e->getMessage()); } // This is a very basic test for the return value of the external function. // More detailed test for tag updating can be found in core_tag component. $this->setAdminUser(); $tag = $this->getDataGenerator()->create_tag(); $res = core_external::update_inplace_editable('core_tag', 'tagname', $tag->id, 'new tag name'); $res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res); $this->assertEquals('new tag name', $res['value']); }
/** * Test web service updating section name */ public function test_update_inplace_editable() { global $CFG, $DB, $PAGE; require_once $CFG->dirroot . '/lib/external/externallib.php'; $this->resetAfterTest(); $user = $this->getDataGenerator()->create_user(); $this->setUser($user); $course = $this->getDataGenerator()->create_course(array('numsections' => 5, 'format' => 'weeks'), array('createsections' => true)); $section = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 2)); // Call webservice without necessary permissions. try { core_external::update_inplace_editable('format_weeks', 'sectionname', $section->id, 'New section name'); $this->fail('Exception expected'); } catch (moodle_exception $e) { $this->assertEquals('Course or activity not accessible. (Not enrolled)', $e->getMessage()); } // Change to teacher and make sure that section name can be updated using web service update_inplace_editable(). $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $teacherrole->id); $res = core_external::update_inplace_editable('format_weeks', 'sectionname', $section->id, 'New section name'); $res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res); $this->assertEquals('New section name', $res['value']); $this->assertEquals('New section name', $DB->get_field('course_sections', 'name', array('id' => $section->id))); }
/** * Test update_inplace_editable() */ public function test_update_inplace_editable() { global $CFG, $DB, $PAGE; require_once $CFG->dirroot . '/lib/external/externallib.php'; $this->resetAfterTest(true); $tag = $this->getDataGenerator()->create_tag(); $this->setUser($this->getDataGenerator()->create_user()); // Call service for core_tag component without necessary permissions. try { core_external::update_inplace_editable('core_tag', 'tagname', $tag->id, 'new tag name'); $this->fail('Exception expected'); } catch (moodle_exception $e) { $this->assertEquals('Sorry, but you do not currently have permissions to do that (Manage all tags)', $e->getMessage()); } // Change to admin user and make sure that tag name can be updated using web service update_inplace_editable(). $this->setAdminUser(); $res = core_external::update_inplace_editable('core_tag', 'tagname', $tag->id, 'New tag name'); $res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res); $this->assertEquals('New tag name', $res['value']); $this->assertEquals('New tag name', $DB->get_field('tag', 'rawname', array('id' => $tag->id))); // Call callback core_tag_inplace_editable() directly. $tmpl = component_callback('core_tag', 'inplace_editable', array('tagname', $tag->id, 'Rename me again')); $this->assertInstanceOf('core\\output\\inplace_editable', $tmpl); $res = $tmpl->export_for_template($PAGE->get_renderer('core')); $this->assertEquals('Rename me again', $res['value']); $this->assertEquals('Rename me again', $DB->get_field('tag', 'rawname', array('id' => $tag->id))); }