weekly release 4.0dev
[moodle.git] / contentbank / tests / content_test.php
CommitLineData
bd4e0a76
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 * Test for content bank contenttype class.
19 *
20 * @package core_contentbank
21 * @category test
22 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace core_contentbank;
27
bd4e0a76
AA
28use stdClass;
29use context_system;
30use contenttype_testable\contenttype as contenttype;
f3126f84 31
bd4e0a76
AA
32/**
33 * Test for content bank contenttype class.
34 *
35 * @package core_contentbank
36 * @category test
37 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 * @coversDefaultClass \core_contentbank\content
40 *
41 */
42class core_contenttype_content_testcase extends \advanced_testcase {
43
f3126f84
SA
44 /**
45 * Setup to ensure that fixtures are loaded.
46 */
47 public static function setupBeforeClass(): void {
48 global $CFG;
49
50 require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
51 require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
52 }
53
bd4e0a76
AA
54 /**
55 * Tests for behaviour of get_name().
56 *
57 * @covers ::get_name
58 */
59 public function test_get_name() {
60 $this->resetAfterTest();
61
62 // Create content.
63 $record = new stdClass();
64 $record->name = 'Test content';
65 $record->configdata = '';
66
67 $contenttype = new contenttype(context_system::instance());
68 $content = $contenttype->create_content($record);
69 $this->assertEquals($record->name, $content->get_name());
70 }
71
3a6ca392
AA
72 /**
73 * Data provider for test_set_name.
74 *
75 * @return array
76 */
77 public function set_name_provider() {
78 return [
79 'Standard name' => ['New name', 'New name'],
80 'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017'],
81 'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle'],
82 'Name with tags' => ['This is <b>bold</b>', 'This is bold'],
83 'Long name' => [str_repeat('a', 100), str_repeat('a', 100)],
84 'Too long name' => [str_repeat('a', 300), str_repeat('a', 255)]
85 ];
86 }
87
88 /**
89 * Tests for 'set_name' behaviour.
90 *
91 * @dataProvider set_name_provider
92 * @param string $newname The name to set
93 * @param string $expected The name result
94 *
95 * @covers ::set_name
96 */
97 public function test_set_name(string $newname, string $expected) {
98 global $DB;
99
100 $this->resetAfterTest();
101 $this->setAdminUser();
102
103 $oldname = "Old name";
104 $context = context_system::instance();
105
106 // Create content.
107 $record = new stdClass();
108 $record->name = $oldname;
109
110 $contenttype = new contenttype($context);
111 $content = $contenttype->create_content($record);
112 $this->assertEquals($oldname, $content->get_name());
113
114 $content->set_name($newname);
115 $this->assertEquals($expected, $content->get_name());
116
117 $record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
118 $this->assertEquals($expected, $record->name);
119 }
120
bd4e0a76
AA
121 /**
122 * Tests for behaviour of get_content_type().
123 *
124 * @covers ::get_content_type
125 */
126 public function test_get_content_type() {
127 $this->resetAfterTest();
128
129 // Create content.
130 $record = new stdClass();
131 $record->name = 'Test content';
132 $record->configdata = '';
133
134 $contenttype = new contenttype(context_system::instance());
135 $content = $contenttype->create_content($record);
136 $this->assertEquals('contenttype_testable', $content->get_content_type());
137 }
138
139 /**
140 * Tests for 'configdata' behaviour.
141 *
142 * @covers ::set_configdata
143 */
144 public function test_configdata_changes() {
145 $this->resetAfterTest();
146
147 $configdata = "{img: 'icon.svg'}";
148
149 // Create content.
150 $record = new stdClass();
151 $record->configdata = $configdata;
152
153 $contenttype = new contenttype(context_system::instance());
154 $content = $contenttype->create_content($record);
155 $this->assertEquals($configdata, $content->get_configdata());
156
157 $configdata = "{alt: 'Name'}";
158 $content->set_configdata($configdata);
159 $this->assertEquals($configdata, $content->get_configdata());
160 }
01003725
AA
161
162 /**
163 * Tests for 'set_contextid' behaviour.
164 *
165 * @covers ::set_contextid
166 */
167 public function test_set_contextid() {
168 $this->resetAfterTest();
169 $this->setAdminUser();
170 $context = context_system::instance();
171 $course = $this->getDataGenerator()->create_course();
172 $newcontext = \context_course::instance($course->id);
173
174 // Add some content to the content bank.
175 $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
176 $contents = $generator->generate_contentbank_data('contenttype_testable', 3, 0, $context);
177 $content = reset($contents);
178
179 $oldcontextid = $content->get_contextid();
180
181 $file = $content->get_file();
182 $this->assertEquals($oldcontextid, $file->get_contextid());
183 $this->assertEquals($context->id, $oldcontextid);
184 $this->assertNotEquals($newcontext->id, $oldcontextid);
185
186 $content->set_contextid($newcontext->id);
187 $file = $content->get_file();
188
189 $this->assertEquals($newcontext->id, $content->get_contextid());
190 $this->assertEquals($newcontext->id, $file->get_contextid());
191 }
bd4e0a76 192}