[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Unit tests for WS in tags 19 * 20 * @package core_tag 21 * @category test 22 * @copyright 2015 Marina Glancy 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 30 require_once($CFG->libdir . '/externallib.php'); 31 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 32 33 class core_tag_external_testcase extends externallib_advanced_testcase { 34 /** 35 * Test update_categories 36 */ 37 public function test_update_tags() { 38 global $DB; 39 $this->resetAfterTest(); 40 $context = context_system::instance(); 41 42 $originaltag = array( 43 'isstandard' => 0, 44 'flag' => 1, 45 'rawname' => 'test', 46 'description' => 'desc' 47 ); 48 $tag = $this->getDataGenerator()->create_tag($originaltag); 49 50 $updatetag = array( 51 'id' => $tag->id, 52 'description' => 'Trying to change tag description', 53 'rawname' => 'Trying to change tag name', 54 'flag' => 0, 55 'isstandard' => 1, 56 ); 57 $gettag = array( 58 'id' => $tag->id, 59 ); 60 61 // User without any caps can not change anything about a tag but can request [partial] tag data. 62 $this->setUser($this->getDataGenerator()->create_user()); 63 $result = core_tag_external::update_tags(array($updatetag)); 64 $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result); 65 $this->assertEquals($tag->id, $result['warnings'][0]['item']); 66 $this->assertEquals('nothingtoupdate', $result['warnings'][0]['warningcode']); 67 $this->assertEquals($originaltag['rawname'], $DB->get_field('tag', 'rawname', 68 array('id' => $tag->id))); 69 $this->assertEquals($originaltag['description'], $DB->get_field('tag', 'description', 70 array('id' => $tag->id))); 71 72 $result = core_tag_external::get_tags(array($gettag)); 73 $result = external_api::clean_returnvalue(core_tag_external::get_tags_returns(), $result); 74 $this->assertEquals($originaltag['rawname'], $result['tags'][0]['rawname']); 75 $this->assertEquals($originaltag['description'], $result['tags'][0]['description']); 76 $this->assertNotEmpty($result['tags'][0]['viewurl']); 77 $this->assertArrayNotHasKey('changetypeurl', $result['tags'][0]); 78 $this->assertArrayNotHasKey('changeflagurl', $result['tags'][0]); 79 $this->assertArrayNotHasKey('flag', $result['tags'][0]); 80 $this->assertArrayNotHasKey('official', $result['tags'][0]); 81 $this->assertArrayNotHasKey('isstandard', $result['tags'][0]); 82 83 // User with editing only capability can change description but not the tag name. 84 $roleid = $this->assignUserCapability('moodle/tag:edit', $context->id); 85 $result = core_tag_external::update_tags(array($updatetag)); 86 $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result); 87 $this->assertEmpty($result['warnings']); 88 89 $result = core_tag_external::get_tags(array($gettag)); 90 $result = external_api::clean_returnvalue(core_tag_external::get_tags_returns(), $result); 91 $this->assertEquals($updatetag['id'], $result['tags'][0]['id']); 92 $this->assertEquals($updatetag['description'], $result['tags'][0]['description']); 93 $this->assertEquals($originaltag['rawname'], $result['tags'][0]['rawname']); 94 $this->assertArrayNotHasKey('flag', $result['tags'][0]); // 'Flag' is not available unless 'moodle/tag:manage' cap exists. 95 $this->assertEquals(0, $result['tags'][0]['official']); 96 $this->assertEquals(0, $result['tags'][0]['isstandard']); 97 $this->assertEquals($originaltag['rawname'], $DB->get_field('tag', 'rawname', 98 array('id' => $tag->id))); 99 $this->assertEquals($updatetag['description'], $DB->get_field('tag', 'description', 100 array('id' => $tag->id))); 101 102 // User with editing and manage cap can also change the tag name, 103 // make it standard and reset flag. 104 assign_capability('moodle/tag:manage', CAP_ALLOW, $roleid, $context->id); 105 $context->mark_dirty(); 106 $this->assertTrue(has_capability('moodle/tag:manage', $context)); 107 $result = core_tag_external::update_tags(array($updatetag)); 108 $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result); 109 $this->assertEmpty($result['warnings']); 110 111 $result = core_tag_external::get_tags(array($gettag)); 112 $result = external_api::clean_returnvalue(core_tag_external::get_tags_returns(), $result); 113 $this->assertEquals($updatetag['id'], $result['tags'][0]['id']); 114 $this->assertEquals($updatetag['rawname'], $result['tags'][0]['rawname']); 115 $this->assertEquals(core_text::strtolower($updatetag['rawname']), $result['tags'][0]['name']); 116 $this->assertEquals($updatetag['flag'], $result['tags'][0]['flag']); 117 $this->assertEquals($updatetag['isstandard'], $result['tags'][0]['official']); 118 $this->assertEquals($updatetag['isstandard'], $result['tags'][0]['isstandard']); 119 $this->assertEquals($updatetag['rawname'], $DB->get_field('tag', 'rawname', 120 array('id' => $tag->id))); 121 $this->assertEquals(1, $DB->get_field('tag', 'isstandard', array('id' => $tag->id))); 122 123 // Updating and getting non-existing tag. 124 $nonexistingtag = array( 125 'id' => 123, 126 'description' => 'test' 127 ); 128 $getnonexistingtag = array( 129 'id' => 123, 130 ); 131 $result = core_tag_external::update_tags(array($nonexistingtag)); 132 $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result); 133 $this->assertEquals(123, $result['warnings'][0]['item']); 134 $this->assertEquals('tagnotfound', $result['warnings'][0]['warningcode']); 135 136 $result = core_tag_external::get_tags(array($getnonexistingtag)); 137 $result = external_api::clean_returnvalue(core_tag_external::get_tags_returns(), $result); 138 $this->assertEmpty($result['tags']); 139 $this->assertEquals(123, $result['warnings'][0]['item']); 140 $this->assertEquals('tagnotfound', $result['warnings'][0]['warningcode']); 141 142 // Attempt to update a tag to the name that is reserved. 143 $anothertag = $this->getDataGenerator()->create_tag(array('rawname' => 'Mytag')); 144 $updatetag2 = array('id' => $tag->id, 'rawname' => 'MYTAG'); 145 $result = core_tag_external::update_tags(array($updatetag2)); 146 $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result); 147 $this->assertEquals($tag->id, $result['warnings'][0]['item']); 148 $this->assertEquals('namesalreadybeeingused', $result['warnings'][0]['warningcode']); 149 } 150 151 /** 152 * Test update_inplace_editable() 153 */ 154 public function test_update_inplace_editable() { 155 global $CFG, $DB, $PAGE; 156 require_once($CFG->dirroot . '/lib/external/externallib.php'); 157 158 $this->resetAfterTest(true); 159 $tag = $this->getDataGenerator()->create_tag(); 160 $this->setUser($this->getDataGenerator()->create_user()); 161 162 // Call service for core_tag component without necessary permissions. 163 try { 164 core_external::update_inplace_editable('core_tag', 'tagname', $tag->id, 'new tag name'); 165 $this->fail('Exception expected'); 166 } catch (moodle_exception $e) { 167 $this->assertEquals('Sorry, but you do not currently have permissions to do that (Manage all tags)', 168 $e->getMessage()); 169 } 170 171 // Change to admin user and make sure that tag name can be updated using web service update_inplace_editable(). 172 $this->setAdminUser(); 173 $res = core_external::update_inplace_editable('core_tag', 'tagname', $tag->id, 'New tag name'); 174 $res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res); 175 $this->assertEquals('New tag name', $res['value']); 176 $this->assertEquals('New tag name', $DB->get_field('tag', 'rawname', array('id' => $tag->id))); 177 178 // Call callback core_tag_inplace_editable() directly. 179 $tmpl = component_callback('core_tag', 'inplace_editable', array('tagname', $tag->id, 'Rename me again')); 180 $this->assertInstanceOf('core\output\inplace_editable', $tmpl); 181 $res = $tmpl->export_for_template($PAGE->get_renderer('core')); 182 $this->assertEquals('Rename me again', $res['value']); 183 $this->assertEquals('Rename me again', $DB->get_field('tag', 'rawname', array('id' => $tag->id))); 184 } 185 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |