MDL-53700 competency: Migrating tests to core
[moodle.git] / competency / tests / exporter_test.php
CommitLineData
c80630da
DW
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 * Exporter class tests.
19 *
e0b9ba28 20 * @package core_competency
c80630da
DW
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26global $CFG;
27
28/**
29 * Exporter testcase.
30 *
e0b9ba28 31 * @package core_competency
c80630da
DW
32 * @copyright 2015 Damyon Wiese
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
e0b9ba28 35class core_competency_exporter_testcase extends advanced_testcase {
c80630da
DW
36
37 protected $validrelated = null;
38 protected $invalidrelated = null;
39 protected $validdata = null;
40 protected $invaliddata = null;
41
42 public function setUp() {
43 $s = new stdClass();
44 $this->validrelated = array('simplestdClass' => $s, 'arrayofstdClass' => array($s, $s));
45 $this->invalidrelated = array('simplestdClass' => 'a string', 'arrayofstdClass' => 5);
46
47 $this->validdata = array('stringA' => 'A string', 'stringAformat' => FORMAT_HTML, 'intB' => 4);
48
49 $this->invaliddata = array('stringA' => 'A string');
50 }
51
52 public function test_get_read_structure() {
e0b9ba28 53 $structure = core_competency_testable_exporter::get_read_structure();
c80630da
DW
54
55 $this->assertInstanceOf('external_single_structure', $structure);
56 $this->assertInstanceOf('external_value', $structure->keys['stringA']);
57 $this->assertInstanceOf('external_format_value', $structure->keys['stringAformat']);
58 $this->assertInstanceOf('external_value', $structure->keys['intB']);
59 $this->assertInstanceOf('external_value', $structure->keys['otherstring']);
60 $this->assertInstanceOf('external_multiple_structure', $structure->keys['otherstrings']);
61 }
62
63 public function test_get_create_structure() {
e0b9ba28 64 $structure = core_competency_testable_exporter::get_create_structure();
c80630da
DW
65
66 $this->assertInstanceOf('external_single_structure', $structure);
67 $this->assertInstanceOf('external_value', $structure->keys['stringA']);
68 $this->assertInstanceOf('external_format_value', $structure->keys['stringAformat']);
69 $this->assertInstanceOf('external_value', $structure->keys['intB']);
70 $this->assertArrayNotHasKey('otherstring', $structure->keys);
71 $this->assertArrayNotHasKey('otherstrings', $structure->keys);
72 }
73
74 public function test_get_update_structure() {
e0b9ba28 75 $structure = core_competency_testable_exporter::get_update_structure();
c80630da
DW
76
77 $this->assertInstanceOf('external_single_structure', $structure);
78 $this->assertInstanceOf('external_value', $structure->keys['stringA']);
79 $this->assertInstanceOf('external_format_value', $structure->keys['stringAformat']);
80 $this->assertInstanceOf('external_value', $structure->keys['intB']);
81 $this->assertArrayNotHasKey('otherstring', $structure->keys);
82 $this->assertArrayNotHasKey('otherstrings', $structure->keys);
83 }
84
85 public function test_invalid_data() {
86 global $PAGE;
87 $this->setExpectedException('coding_exception');
e0b9ba28 88 $exporter = new core_competency_testable_exporter($this->invaliddata, $this->validrelated);
c80630da
DW
89 $output = $PAGE->get_renderer('tool_lp');
90
91 $result = $exporter->export($output);
92 }
93
94 public function test_invalid_related() {
95 global $PAGE;
96 $this->setExpectedException('coding_exception');
e0b9ba28 97 $exporter = new core_competency_testable_exporter($this->validdata, $this->invalidrelated);
c80630da
DW
98 $output = $PAGE->get_renderer('tool_lp');
99
100 $result = $exporter->export($output);
101 }
102
103 public function test_valid_data_and_related() {
104 global $PAGE;
e0b9ba28 105 $exporter = new core_competency_testable_exporter($this->validdata, $this->validrelated);
c80630da
DW
106
107 $output = $PAGE->get_renderer('tool_lp');
108
109 $result = $exporter->export($output);
110 }
111}
112
113/**
114 * Example persistent class.
115 *
e0b9ba28 116 * @package core_competency
c80630da
DW
117 * @copyright 2015 Frédéric Massart - FMCorz.net
118 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
119 */
e0b9ba28 120class core_competency_testable_exporter extends \core_competency\external\exporter {
c80630da
DW
121
122 protected static function define_related() {
123 // We cache the context so it does not need to be retrieved from the course.
124 return array('simplestdClass' => 'stdClass', 'arrayofstdClass' => 'stdClass[]');
125 }
126
127 protected function get_other_values(renderer_base $output) {
128 return array(
129 'otherstring' => 'An other string',
130 'otherstrings' => array('String a', 'String b')
131 );
132 }
133
134 public static function define_properties() {
135 return array(
136 'stringA' => array(
d323d6d3 137 'type' => PARAM_RAW,
c80630da
DW
138 ),
139 'stringAformat' => array(
140 'type' => PARAM_INT,
141 ),
142 'intB' => array(
143 'type' => PARAM_INT,
144 )
145 );
146 }
147
148 public static function define_other_properties() {
149 return array(
150 'otherstring' => array(
151 'type' => PARAM_TEXT,
152 ),
153 'otherstrings' => array(
154 'type' => PARAM_TEXT,
155 'multiple' => true
156 )
157 );
158 }
159
160
161}