MDL-68483 contentbank: improve search API
[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
27namespace core_contentbank;
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
36use core_contentbank\external\delete_content;
37use core_contentbank\external\external;
38use core_contentbank\external\rename_content;
39use external_api;
40
41/**
42 * Core content bank external functions tests.
43 *
44 * @package core_contentbank
45 * @copyright 2020 Sara Arjona <sara@moodle.com>
46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47 * @coversDefaultClass \core_contentbank\external
48 */
49class rename_content_testcase extends \externallib_advanced_testcase {
50
51 /**
52 * Data provider for test_rename_content.
53 *
54 * @return array
55 */
56 public function rename_content_provider() {
57 return [
58 'Standard name' => ['New name', 'New name'],
59 'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017'],
60 'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle'],
61 'Name with tags' => ['This is <b>bold</b>', 'This is bold'],
62 'Long name' => [str_repeat('a', 100), str_repeat('a', 100)],
63 'Too long name' => [str_repeat('a', 300), str_repeat('a', 255)]
64 ];
65 }
66
67 /**
68 * Test the behaviour of rename_content() for users with permission.
69 *
70 * @dataProvider rename_content_provider
71 * @param string $newname The name to set
72 * @param string $expected The name result
73 *
74 * @covers ::execute
75 */
76 public function test_rename_content_with_permission(string $newname, string $expected) {
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);
97 $this->assertTrue($result['result']);
98 $record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
99 $this->assertNotEquals($oldname, $record->name);
100 $this->assertEquals($expected, $record->name);
101
102 // Call the WS using an unexisting contentid and check an error is thrown.
103 $this->expectException(\invalid_response_exception::class);
104 $result = rename_content::execute_returns($content->get_id() + 1, $oldname);
105 $result = external_api::clean_returnvalue(rename_content::execute_returns(), $result);
106 $this->assertFalse($result['result']);
107 }
108
109 /**
110 * Test the behaviour of rename_content() for users with permission.
111 *
112 * @covers ::execute
113 */
114 public function test_rename_content_without_permission() {
115 global $DB;
116 $this->resetAfterTest();
117
118 // Create users.
119 $course = $this->getDataGenerator()->create_course();
120 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
121 $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
122
123 // Add some content to the content bank as teacher.
124 $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
125 $contents = $generator->generate_contentbank_data('contenttype_testable', 1, $teacher->id);
126 $content = array_shift($contents);
127
128 $oldname = $content->get_name();
129 $newname = 'New name';
130
131 // Call the WS and check the content has not been renamed by the student.
132 $this->setUser($student);
133 $result = rename_content::execute($content->get_id(), $newname);
134 $result = external_api::clean_returnvalue(rename_content::execute_returns(), $result);
135 $this->assertFalse($result['result']);
136 $record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
137 $this->assertEquals($oldname, $record->name);
138 $this->assertNotEquals($newname, $record->name);
139 }
140}