Merge branch 'MDL-67786-master' of git://github.com/aanabit/moodle
[moodle.git] / contentbank / tests / contenttype_test.php
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/>.
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  */
26 namespace core_contentbank;
28 defined('MOODLE_INTERNAL') || die();
30 global $CFG;
31 require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
32 require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
34 use stdClass;
35 use context_system;
36 use 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  */
47 class core_contenttype_contenttype_testcase extends \advanced_testcase {
49     /**
50      * Tests get_contenttype_name result.
51      *
52      * @covers ::get_contenttype_name
53      */
54     public function test_get_contenttype_name() {
55         $this->resetAfterTest();
57         $systemcontext = \context_system::instance();
58         $testable = new contenttype($systemcontext);
60         $this->assertEquals('contenttype_testable', $testable->get_contenttype_name());
61     }
63     /**
64      * Tests get_plugin_name result.
65      *
66      * @covers ::get_plugin_name
67      */
68     public function test_get_plugin_name() {
69         $this->resetAfterTest();
71         $systemcontext = \context_system::instance();
72         $testable = new contenttype($systemcontext);
74         $this->assertEquals('testable', $testable->get_plugin_name());
75     }
77     /**
78      * Tests get_icon result.
79      *
80      * @covers ::get_icon
81      */
82     public function test_get_icon() {
83         $this->resetAfterTest();
85         $systemcontext = \context_system::instance();
86         $testable = new contenttype($systemcontext);
87         $icon = $testable->get_icon('new content');
88         $this->assertContains('archive', $icon);
89     }
91     /**
92      * Tests is_feature_supported behavior .
93      *
94      * @covers ::is_feature_supported
95      */
96     public function test_is_feature_supported() {
97         $this->resetAfterTest();
99         $systemcontext = \context_system::instance();
100         $testable = new contenttype($systemcontext);
102         $this->assertTrue($testable->is_feature_supported(contenttype::CAN_TEST));
103         $this->assertFalse($testable->is_feature_supported(contenttype::CAN_UPLOAD));
104     }
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();
114         $systemcontext = \context_system::instance();
115         $testable = new contenttype($systemcontext);
117         $this->setAdminUser();
118         $this->assertFalse($testable->is_feature_supported(contenttype::CAN_UPLOAD));
119         $this->assertFalse($testable->can_upload());
120     }
122     /**
123      * Test create_content() with empty data.
124      *
125      * @covers ::create_content
126      */
127     public function test_create_empty_content() {
128         $this->resetAfterTest();
130         // Create empty content.
131         $record = new stdClass();
133         $contenttype = new contenttype(context_system::instance());
134         $content = $contenttype->create_content($record);
136         $this->assertEquals('contenttype_testable', $content->get_content_type());
137         $this->assertInstanceOf('\\contenttype_testable\\content', $content);
138     }
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();
148         // Create content.
149         $record = new stdClass();
150         $record->name = 'Test content';
151         $record->configdata = '';
152         $record->contenttype = '';
154         $contenttype = new contenttype(context_system::instance());
155         $content = $contenttype->create_content($record);
157         $this->assertEquals('contenttype_testable', $content->get_content_type());
158         $this->assertInstanceOf('\\contenttype_testable\\content', $content);
159     }