MDL-50919 tags: new UI for managing tags
[moodle.git] / tag / tests / external_test.php
CommitLineData
8e355853
MG
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
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30require_once($CFG->dirroot . '/tag/lib.php');
31require_once($CFG->libdir . '/externallib.php');
32require_once($CFG->dirroot . '/webservice/tests/helpers.php');
33
34class core_tag_external_testcase extends externallib_advanced_testcase {
35 /**
36 * Test update_categories
37 */
38 public function test_update_tags() {
39 global $DB;
40 $this->resetAfterTest();
41 $context = context_system::instance();
42
43 $originaltag = array(
44 'tagtype' => 'default',
45 'flag' => 1,
46 'rawname' => 'test',
47 'description' => 'desc'
48 );
49 $tag = $this->getDataGenerator()->create_tag($originaltag);
50
51 $updatetag = array(
52 'id' => $tag->id,
53 'description' => 'Trying to change tag description',
54 'rawname' => 'Trying to change tag name',
55 'flag' => 0,
56 'official' => 1,
57 );
58
59 // User without any caps can not change anything about a tag.
60 $this->setUser($this->getDataGenerator()->create_user());
61 $result = core_tag_external::update_tags(array($updatetag));
62 $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result);
63 $this->assertEmpty($result['warnings']);
64 $this->assertEmpty($result['tags']);
65 $this->assertEquals($originaltag['rawname'], $DB->get_field('tag', 'rawname',
66 array('id' => $tag->id)));
67 $this->assertEquals($originaltag['description'], $DB->get_field('tag', 'description',
68 array('id' => $tag->id)));
69
70 // User with editing only capability can change description but not the tag name.
71 $roleid = $this->assignUserCapability('moodle/tag:edit', $context->id);
72 $result = core_tag_external::update_tags(array($updatetag));
73 $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result);
74 $this->assertEmpty($result['warnings']);
75 $this->assertEquals($updatetag['id'], $result['tags'][0]['id']);
76 $this->assertEquals($updatetag['description'], $result['tags'][0]['description']);
77 $this->assertEquals($originaltag['rawname'], $result['tags'][0]['rawname']);
78 $this->assertEquals($originaltag['flag'], $result['tags'][0]['flag']);
79 $this->assertEquals(0, $result['tags'][0]['official']);
80 $this->assertEquals($originaltag['rawname'], $DB->get_field('tag', 'rawname',
81 array('id' => $tag->id)));
82 $this->assertEquals($updatetag['description'], $DB->get_field('tag', 'description',
83 array('id' => $tag->id)));
84
85 // User with editing and manage cap can also change the tag name,
86 // make it official and reset flag.
87 assign_capability('moodle/tag:manage', CAP_ALLOW, $roleid, $context->id);
88 $context->mark_dirty();
89 $this->assertTrue(has_capability('moodle/tag:manage', $context));
90 $result = core_tag_external::update_tags(array($updatetag));
91 $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result);
92 $this->assertEmpty($result['warnings']);
93 $this->assertEquals($updatetag['id'], $result['tags'][0]['id']);
94 $this->assertEquals($updatetag['rawname'], $result['tags'][0]['rawname']);
95 $this->assertEquals(core_text::strtolower($updatetag['rawname']), $result['tags'][0]['name']);
96 $this->assertEquals($updatetag['flag'], $result['tags'][0]['flag']);
97 $this->assertEquals($updatetag['official'], $result['tags'][0]['official']);
98 $this->assertEquals($updatetag['rawname'], $DB->get_field('tag', 'rawname',
99 array('id' => $tag->id)));
100 $this->assertEquals('official', $DB->get_field('tag', 'tagtype',
101 array('id' => $tag->id)));
102
103 // Updating non-existing tag.
104 $nonexistingtag = array(
105 'id' => 123,
106 'description' => 'test'
107 );
108 $result = core_tag_external::update_tags(array($nonexistingtag));
109 $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result);
110 $this->assertEmpty($result['tags']);
111 $this->assertEquals(123, $result['warnings'][0]['item']);
112 $this->assertEquals('tagnotfound', $result['warnings'][0]['warningcode']);
113
114 // Attempt to update a tag to the name that is reserved.
115 $anothertag = $this->getDataGenerator()->create_tag(array('rawname' => 'Mytag'));
116 $updatetag2 = array('id' => $tag->id, 'rawname' => 'MYTAG');
117 $result = core_tag_external::update_tags(array($updatetag2));
118 $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result);
119 $this->assertEmpty($result['tags']);
120 $this->assertEquals($tag->id, $result['warnings'][0]['item']);
121 $this->assertEquals('namesalreadybeeingused', $result['warnings'][0]['warningcode']);
122 }
123}