MDL-67790 core_contentbank: Behat contentbank content generator
[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
67 /**
68 * Tests for behaviour of get_content_type().
69 *
70 * @covers ::get_content_type
71 */
72 public function test_get_content_type() {
73 $this->resetAfterTest();
74
75 // Create content.
76 $record = new stdClass();
77 $record->name = 'Test content';
78 $record->configdata = '';
79
80 $contenttype = new contenttype(context_system::instance());
81 $content = $contenttype->create_content($record);
82 $this->assertEquals('contenttype_testable', $content->get_content_type());
83 }
84
85 /**
86 * Tests for 'configdata' behaviour.
87 *
88 * @covers ::set_configdata
89 */
90 public function test_configdata_changes() {
91 $this->resetAfterTest();
92
93 $configdata = "{img: 'icon.svg'}";
94
95 // Create content.
96 $record = new stdClass();
97 $record->configdata = $configdata;
98
99 $contenttype = new contenttype(context_system::instance());
100 $content = $contenttype->create_content($record);
101 $this->assertEquals($configdata, $content->get_configdata());
102
103 $configdata = "{alt: 'Name'}";
104 $content->set_configdata($configdata);
105 $this->assertEquals($configdata, $content->get_configdata());
106 }
107}