. /** * Unit tests for WS in tags * * @package core_tag * @category test * @copyright 2015 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/tag/lib.php'); require_once($CFG->libdir . '/externallib.php'); require_once($CFG->dirroot . '/webservice/tests/helpers.php'); class core_tag_external_testcase extends externallib_advanced_testcase { /** * Test update_categories */ public function test_update_tags() { global $DB; $this->resetAfterTest(); $context = context_system::instance(); $originaltag = array( 'tagtype' => 'default', 'flag' => 1, 'rawname' => 'test', 'description' => 'desc' ); $tag = $this->getDataGenerator()->create_tag($originaltag); $updatetag = array( 'id' => $tag->id, 'description' => 'Trying to change tag description', 'rawname' => 'Trying to change tag name', 'flag' => 0, 'official' => 1, ); $gettag = array( 'id' => $tag->id, ); // User without any caps can not change anything about a tag but can request [partial] tag data. $this->setUser($this->getDataGenerator()->create_user()); $result = core_tag_external::update_tags(array($updatetag)); $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result); $this->assertEquals($tag->id, $result['warnings'][0]['item']); $this->assertEquals('nothingtoupdate', $result['warnings'][0]['warningcode']); $this->assertEquals($originaltag['rawname'], $DB->get_field('tag', 'rawname', array('id' => $tag->id))); $this->assertEquals($originaltag['description'], $DB->get_field('tag', 'description', array('id' => $tag->id))); $result = core_tag_external::get_tags(array($gettag)); $result = external_api::clean_returnvalue(core_tag_external::get_tags_returns(), $result); $this->assertEquals($originaltag['rawname'], $result['tags'][0]['rawname']); $this->assertEquals($originaltag['description'], $result['tags'][0]['description']); $this->assertNotEmpty($result['tags'][0]['viewurl']); $this->assertArrayNotHasKey('changetypeurl', $result['tags'][0]); $this->assertArrayNotHasKey('changeflagurl', $result['tags'][0]); $this->assertArrayNotHasKey('flag', $result['tags'][0]); $this->assertArrayNotHasKey('official', $result['tags'][0]); // User with editing only capability can change description but not the tag name. $roleid = $this->assignUserCapability('moodle/tag:edit', $context->id); $result = core_tag_external::update_tags(array($updatetag)); $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result); $this->assertEmpty($result['warnings']); $result = core_tag_external::get_tags(array($gettag)); $result = external_api::clean_returnvalue(core_tag_external::get_tags_returns(), $result); $this->assertEquals($updatetag['id'], $result['tags'][0]['id']); $this->assertEquals($updatetag['description'], $result['tags'][0]['description']); $this->assertEquals($originaltag['rawname'], $result['tags'][0]['rawname']); $this->assertArrayNotHasKey('flag', $result['tags'][0]); // 'Flag' is not available unless 'moodle/tag:manage' cap exists. $this->assertEquals(0, $result['tags'][0]['official']); $this->assertEquals($originaltag['rawname'], $DB->get_field('tag', 'rawname', array('id' => $tag->id))); $this->assertEquals($updatetag['description'], $DB->get_field('tag', 'description', array('id' => $tag->id))); // User with editing and manage cap can also change the tag name, // make it official and reset flag. assign_capability('moodle/tag:manage', CAP_ALLOW, $roleid, $context->id); $context->mark_dirty(); $this->assertTrue(has_capability('moodle/tag:manage', $context)); $result = core_tag_external::update_tags(array($updatetag)); $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result); $this->assertEmpty($result['warnings']); $result = core_tag_external::get_tags(array($gettag)); $result = external_api::clean_returnvalue(core_tag_external::get_tags_returns(), $result); $this->assertEquals($updatetag['id'], $result['tags'][0]['id']); $this->assertEquals($updatetag['rawname'], $result['tags'][0]['rawname']); $this->assertEquals(core_text::strtolower($updatetag['rawname']), $result['tags'][0]['name']); $this->assertEquals($updatetag['flag'], $result['tags'][0]['flag']); $this->assertEquals($updatetag['official'], $result['tags'][0]['official']); $this->assertEquals($updatetag['rawname'], $DB->get_field('tag', 'rawname', array('id' => $tag->id))); $this->assertEquals('official', $DB->get_field('tag', 'tagtype', array('id' => $tag->id))); // Updating and getting non-existing tag. $nonexistingtag = array( 'id' => 123, 'description' => 'test' ); $getnonexistingtag = array( 'id' => 123, ); $result = core_tag_external::update_tags(array($nonexistingtag)); $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result); $this->assertEquals(123, $result['warnings'][0]['item']); $this->assertEquals('tagnotfound', $result['warnings'][0]['warningcode']); $result = core_tag_external::get_tags(array($getnonexistingtag)); $result = external_api::clean_returnvalue(core_tag_external::get_tags_returns(), $result); $this->assertEmpty($result['tags']); $this->assertEquals(123, $result['warnings'][0]['item']); $this->assertEquals('tagnotfound', $result['warnings'][0]['warningcode']); // Attempt to update a tag to the name that is reserved. $anothertag = $this->getDataGenerator()->create_tag(array('rawname' => 'Mytag')); $updatetag2 = array('id' => $tag->id, 'rawname' => 'MYTAG'); $result = core_tag_external::update_tags(array($updatetag2)); $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result); $this->assertEquals($tag->id, $result['warnings'][0]['item']); $this->assertEquals('namesalreadybeeingused', $result['warnings'][0]['warningcode']); } }