MDL-68483 contentbank: improve search API
[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
28defined('MOODLE_INTERNAL') || die();
29
30global $CFG;
31require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
32require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
33
34use stdClass;
35use context_system;
36use contenttype_testable\contenttype as contenttype;
37/**
38 * Test for content bank contenttype class.
39 *
40 * @package core_contentbank
41 * @category test
42 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 * @coversDefaultClass \core_contentbank\content
45 *
46 */
47class core_contenttype_content_testcase extends \advanced_testcase {
48
49 /**
50 * Tests for behaviour of get_name().
51 *
52 * @covers ::get_name
53 */
54 public function test_get_name() {
55 $this->resetAfterTest();
56
57 // Create content.
58 $record = new stdClass();
59 $record->name = 'Test content';
60 $record->configdata = '';
61
62 $contenttype = new contenttype(context_system::instance());
63 $content = $contenttype->create_content($record);
64 $this->assertEquals($record->name, $content->get_name());
65 }
66
3a6ca392
AA
67 /**
68 * Data provider for test_set_name.
69 *
70 * @return array
71 */
72 public function set_name_provider() {
73 return [
74 'Standard name' => ['New name', 'New name'],
75 'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017'],
76 'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle'],
77 'Name with tags' => ['This is <b>bold</b>', 'This is bold'],
78 'Long name' => [str_repeat('a', 100), str_repeat('a', 100)],
79 'Too long name' => [str_repeat('a', 300), str_repeat('a', 255)]
80 ];
81 }
82
83 /**
84 * Tests for 'set_name' behaviour.
85 *
86 * @dataProvider set_name_provider
87 * @param string $newname The name to set
88 * @param string $expected The name result
89 *
90 * @covers ::set_name
91 */
92 public function test_set_name(string $newname, string $expected) {
93 global $DB;
94
95 $this->resetAfterTest();
96 $this->setAdminUser();
97
98 $oldname = "Old name";
99 $context = context_system::instance();
100
101 // Create content.
102 $record = new stdClass();
103 $record->name = $oldname;
104
105 $contenttype = new contenttype($context);
106 $content = $contenttype->create_content($record);
107 $this->assertEquals($oldname, $content->get_name());
108
109 $content->set_name($newname);
110 $this->assertEquals($expected, $content->get_name());
111
112 $record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
113 $this->assertEquals($expected, $record->name);
114 }
115
bd4e0a76
AA
116 /**
117 * Tests for behaviour of get_content_type().
118 *
119 * @covers ::get_content_type
120 */
121 public function test_get_content_type() {
122 $this->resetAfterTest();
123
124 // Create content.
125 $record = new stdClass();
126 $record->name = 'Test content';
127 $record->configdata = '';
128
129 $contenttype = new contenttype(context_system::instance());
130 $content = $contenttype->create_content($record);
131 $this->assertEquals('contenttype_testable', $content->get_content_type());
132 }
133
134 /**
135 * Tests for 'configdata' behaviour.
136 *
137 * @covers ::set_configdata
138 */
139 public function test_configdata_changes() {
140 $this->resetAfterTest();
141
142 $configdata = "{img: 'icon.svg'}";
143
144 // Create content.
145 $record = new stdClass();
146 $record->configdata = $configdata;
147
148 $contenttype = new contenttype(context_system::instance());
149 $content = $contenttype->create_content($record);
150 $this->assertEquals($configdata, $content->get_configdata());
151
152 $configdata = "{alt: 'Name'}";
153 $content->set_configdata($configdata);
154 $this->assertEquals($configdata, $content->get_configdata());
155 }
156}