weekly release 3.1dev
[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
8e355853
MG
30require_once($CFG->libdir . '/externallib.php');
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32
33class 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 'tagtype' => 'default',
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 'official' => 1,
56 );
ef4c23cc
MG
57 $gettag = array(
58 'id' => $tag->id,
59 );
8e355853 60
ef4c23cc 61 // User without any caps can not change anything about a tag but can request [partial] tag data.
8e355853
MG
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);
ef4c23cc
MG
65 $this->assertEquals($tag->id, $result['warnings'][0]['item']);
66 $this->assertEquals('nothingtoupdate', $result['warnings'][0]['warningcode']);
8e355853 67 $this->assertEquals($originaltag['rawname'], $DB->get_field('tag', 'rawname',
ef4c23cc 68 array('id' => $tag->id)));
8e355853 69 $this->assertEquals($originaltag['description'], $DB->get_field('tag', 'description',
ef4c23cc
MG
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]);
8e355853
MG
81
82 // User with editing only capability can change description but not the tag name.
83 $roleid = $this->assignUserCapability('moodle/tag:edit', $context->id);
84 $result = core_tag_external::update_tags(array($updatetag));
85 $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result);
86 $this->assertEmpty($result['warnings']);
ef4c23cc
MG
87
88 $result = core_tag_external::get_tags(array($gettag));
89 $result = external_api::clean_returnvalue(core_tag_external::get_tags_returns(), $result);
8e355853
MG
90 $this->assertEquals($updatetag['id'], $result['tags'][0]['id']);
91 $this->assertEquals($updatetag['description'], $result['tags'][0]['description']);
92 $this->assertEquals($originaltag['rawname'], $result['tags'][0]['rawname']);
ef4c23cc 93 $this->assertArrayNotHasKey('flag', $result['tags'][0]); // 'Flag' is not available unless 'moodle/tag:manage' cap exists.
8e355853
MG
94 $this->assertEquals(0, $result['tags'][0]['official']);
95 $this->assertEquals($originaltag['rawname'], $DB->get_field('tag', 'rawname',
96 array('id' => $tag->id)));
97 $this->assertEquals($updatetag['description'], $DB->get_field('tag', 'description',
98 array('id' => $tag->id)));
99
100 // User with editing and manage cap can also change the tag name,
101 // make it official and reset flag.
102 assign_capability('moodle/tag:manage', CAP_ALLOW, $roleid, $context->id);
103 $context->mark_dirty();
104 $this->assertTrue(has_capability('moodle/tag:manage', $context));
105 $result = core_tag_external::update_tags(array($updatetag));
106 $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result);
107 $this->assertEmpty($result['warnings']);
ef4c23cc
MG
108
109 $result = core_tag_external::get_tags(array($gettag));
110 $result = external_api::clean_returnvalue(core_tag_external::get_tags_returns(), $result);
8e355853
MG
111 $this->assertEquals($updatetag['id'], $result['tags'][0]['id']);
112 $this->assertEquals($updatetag['rawname'], $result['tags'][0]['rawname']);
113 $this->assertEquals(core_text::strtolower($updatetag['rawname']), $result['tags'][0]['name']);
114 $this->assertEquals($updatetag['flag'], $result['tags'][0]['flag']);
115 $this->assertEquals($updatetag['official'], $result['tags'][0]['official']);
116 $this->assertEquals($updatetag['rawname'], $DB->get_field('tag', 'rawname',
117 array('id' => $tag->id)));
118 $this->assertEquals('official', $DB->get_field('tag', 'tagtype',
119 array('id' => $tag->id)));
120
ef4c23cc 121 // Updating and getting non-existing tag.
8e355853
MG
122 $nonexistingtag = array(
123 'id' => 123,
124 'description' => 'test'
125 );
ef4c23cc
MG
126 $getnonexistingtag = array(
127 'id' => 123,
128 );
8e355853
MG
129 $result = core_tag_external::update_tags(array($nonexistingtag));
130 $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result);
ef4c23cc
MG
131 $this->assertEquals(123, $result['warnings'][0]['item']);
132 $this->assertEquals('tagnotfound', $result['warnings'][0]['warningcode']);
133
134 $result = core_tag_external::get_tags(array($getnonexistingtag));
135 $result = external_api::clean_returnvalue(core_tag_external::get_tags_returns(), $result);
8e355853
MG
136 $this->assertEmpty($result['tags']);
137 $this->assertEquals(123, $result['warnings'][0]['item']);
138 $this->assertEquals('tagnotfound', $result['warnings'][0]['warningcode']);
139
140 // Attempt to update a tag to the name that is reserved.
141 $anothertag = $this->getDataGenerator()->create_tag(array('rawname' => 'Mytag'));
142 $updatetag2 = array('id' => $tag->id, 'rawname' => 'MYTAG');
143 $result = core_tag_external::update_tags(array($updatetag2));
144 $result = external_api::clean_returnvalue(core_tag_external::update_tags_returns(), $result);
8e355853
MG
145 $this->assertEquals($tag->id, $result['warnings'][0]['item']);
146 $this->assertEquals('namesalreadybeeingused', $result['warnings'][0]['warningcode']);
147 }
148}