MDL-67795 contentbank: add generator
[moodle.git] / contentbank / tests / contenttype_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\contenttype
45 *
46 */
47class core_contenttype_contenttype_testcase extends \advanced_testcase {
48
49 /**
50 * Tests get_contenttype_name result.
51 *
52 * @covers ::get_contenttype_name
53 */
54 public function test_get_contenttype_name() {
55 $this->resetAfterTest();
56
57 $systemcontext = \context_system::instance();
58 $testable = new contenttype($systemcontext);
59
60 $this->assertEquals('contenttype_testable', $testable->get_contenttype_name());
61 }
62
63 /**
64 * Tests get_plugin_name result.
65 *
66 * @covers ::get_plugin_name
67 */
68 public function test_get_plugin_name() {
69 $this->resetAfterTest();
70
71 $systemcontext = \context_system::instance();
72 $testable = new contenttype($systemcontext);
73
74 $this->assertEquals('testable', $testable->get_plugin_name());
75 }
76
77 /**
78 * Tests get_icon result.
79 *
80 * @covers ::get_icon
81 */
82 public function test_get_icon() {
83 $this->resetAfterTest();
84
85 $systemcontext = \context_system::instance();
86 $testable = new contenttype($systemcontext);
87 $icon = $testable->get_icon('new content');
88 $this->assertContains('archive', $icon);
89 }
90
91 /**
92 * Tests is_feature_supported behavior .
93 *
94 * @covers ::is_feature_supported
95 */
96 public function test_is_feature_supported() {
97 $this->resetAfterTest();
98
99 $systemcontext = \context_system::instance();
100 $testable = new contenttype($systemcontext);
101
102 $this->assertTrue($testable->is_feature_supported(contenttype::CAN_TEST));
103 $this->assertFalse($testable->is_feature_supported(contenttype::CAN_UPLOAD));
104 }
105
106 /**
107 * Tests can_upload behavior with no implemented upload feature.
108 *
109 * @covers ::can_upload
110 */
111 public function test_no_upload_feature_supported() {
112 $this->resetAfterTest();
113
114 $systemcontext = \context_system::instance();
115 $testable = new contenttype($systemcontext);
116
117 $this->setAdminUser();
118 $this->assertFalse($testable->is_feature_supported(contenttype::CAN_UPLOAD));
119 $this->assertFalse($testable->can_upload());
120 }
121
122 /**
123 * Test create_content() with empty data.
124 *
125 * @covers ::create_content
126 */
127 public function test_create_empty_content() {
128 $this->resetAfterTest();
129
130 // Create empty content.
131 $record = new stdClass();
132
133 $contenttype = new contenttype(context_system::instance());
134 $content = $contenttype->create_content($record);
135
136 $this->assertEquals('contenttype_testable', $content->get_content_type());
137 $this->assertInstanceOf('\\contenttype_testable\\content', $content);
138 }
139
140 /**
141 * Tests for behaviour of create_content() with data.
142 *
143 * @covers ::create_content
144 */
145 public function test_create_content() {
146 $this->resetAfterTest();
147
148 // Create content.
149 $record = new stdClass();
150 $record->name = 'Test content';
151 $record->configdata = '';
152 $record->contenttype = '';
153
154 $contenttype = new contenttype(context_system::instance());
155 $content = $contenttype->create_content($record);
156
157 $this->assertEquals('contenttype_testable', $content->get_content_type());
158 $this->assertInstanceOf('\\contenttype_testable\\content', $content);
159 }
160}