Merge branch 'MDL-69089-master' of git://github.com/aanabit/moodle
[moodle.git] / contentbank / tests / external / rename_content_test.php
CommitLineData
3a6ca392
AA
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 * Core content bank external functions tests.
19 *
20 * @package core_contentbank
21 * @category external
22 * @copyright 2020 Sara Arjona <sara@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since Moodle 3.9
25 */
26
f3126f84 27namespace core_contentbank\external;
3a6ca392
AA
28
29defined('MOODLE_INTERNAL') || die();
30
31global $CFG;
32require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
33require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
34require_once($CFG->dirroot . '/webservice/tests/helpers.php');
35
3a6ca392
AA
36use external_api;
37
38/**
39 * Core content bank external functions tests.
40 *
41 * @package core_contentbank
42 * @copyright 2020 Sara Arjona <sara@moodle.com>
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 * @coversDefaultClass \core_contentbank\external
45 */
46class rename_content_testcase extends \externallib_advanced_testcase {
47
48 /**
49 * Data provider for test_rename_content.
50 *
51 * @return array
52 */
53 public function rename_content_provider() {
54 return [
dfd8f6b5
AA
55 'Standard name' => ['New name', 'New name', true],
56 'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017', true],
57 'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle', true],
58 'Name with tags' => ['This is <b>bold</b>', 'This is bold', true],
59 'Long name' => [str_repeat('a', 100), str_repeat('a', 100), true],
60 'Too long name' => [str_repeat('a', 300), str_repeat('a', 255), true],
61 'Empty name' => ['', 'Test content ', false],
62 'Blanks only' => [' ', 'Test content ', false],
3a6ca392
AA
63 ];
64 }
65
66 /**
67 * Test the behaviour of rename_content() for users with permission.
68 *
69 * @dataProvider rename_content_provider
70 * @param string $newname The name to set
dfd8f6b5
AA
71 * @param string $expectedname The name result
72 * @param bool $expectedresult The bolean result expected when renaming
3a6ca392
AA
73 *
74 * @covers ::execute
75 */
dfd8f6b5 76 public function test_rename_content_with_permission(string $newname, string $expectedname, bool $expectedresult) {
3a6ca392
AA
77 global $DB;
78 $this->resetAfterTest();
79
80 // Create users.
81 $roleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher']);
82 $teacher = $this->getDataGenerator()->create_user();
83
84 $this->getDataGenerator()->role_assign($roleid, $teacher->id);
85 $this->setUser($teacher);
86
87 // Add some content to the content bank as teacher.
88 $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
89 $contents = $generator->generate_contentbank_data('contenttype_testable', 1, $teacher->id);
90 $content = array_shift($contents);
91
92 $oldname = $content->get_name();
93
94 // Call the WS and check the content is renamed as expected.
95 $result = rename_content::execute($content->get_id(), $newname);
96 $result = external_api::clean_returnvalue(rename_content::execute_returns(), $result);
dfd8f6b5 97 $this->assertEquals($expectedresult, $result['result']);
3a6ca392 98 $record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
dfd8f6b5 99 $this->assertEquals($expectedname, $record->name);
3a6ca392
AA
100
101 // Call the WS using an unexisting contentid and check an error is thrown.
102 $this->expectException(\invalid_response_exception::class);
103 $result = rename_content::execute_returns($content->get_id() + 1, $oldname);
104 $result = external_api::clean_returnvalue(rename_content::execute_returns(), $result);
105 $this->assertFalse($result['result']);
106 }
107
108 /**
109 * Test the behaviour of rename_content() for users with permission.
110 *
111 * @covers ::execute
112 */
113 public function test_rename_content_without_permission() {
114 global $DB;
115 $this->resetAfterTest();
116
117 // Create users.
118 $course = $this->getDataGenerator()->create_course();
119 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
120 $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
121
122 // Add some content to the content bank as teacher.
123 $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
124 $contents = $generator->generate_contentbank_data('contenttype_testable', 1, $teacher->id);
125 $content = array_shift($contents);
126
127 $oldname = $content->get_name();
128 $newname = 'New name';
129
130 // Call the WS and check the content has not been renamed by the student.
131 $this->setUser($student);
132 $result = rename_content::execute($content->get_id(), $newname);
133 $result = external_api::clean_returnvalue(rename_content::execute_returns(), $result);
134 $this->assertFalse($result['result']);
135 $record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
136 $this->assertEquals($oldname, $record->name);
137 $this->assertNotEquals($newname, $record->name);
138 }
139}