2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Exporter class tests.
20 * @package core_competency
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
31 * @package core_competency
32 * @copyright 2015 Damyon Wiese
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class core_competency_exporter_testcase extends advanced_testcase {
37 protected $validrelated = null;
38 protected $invalidrelated = null;
39 protected $validdata = null;
40 protected $invaliddata = null;
42 public function setUp() {
44 $this->validrelated = array('simplestdClass' => $s, 'arrayofstdClass' => array($s, $s));
45 $this->invalidrelated = array('simplestdClass' => 'a string', 'arrayofstdClass' => 5);
47 $this->validdata = array('stringA' => 'A string', 'stringAformat' => FORMAT_HTML, 'intB' => 4);
49 $this->invaliddata = array('stringA' => 'A string');
52 public function test_get_read_structure() {
53 $structure = core_competency_testable_exporter::get_read_structure();
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']);
63 public function test_get_create_structure() {
64 $structure = core_competency_testable_exporter::get_create_structure();
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);
74 public function test_get_update_structure() {
75 $structure = core_competency_testable_exporter::get_update_structure();
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);
86 * @expectedException coding_exception
88 public function test_invalid_data() {
90 $exporter = new core_competency_testable_exporter($this->invaliddata, $this->validrelated);
91 $output = $PAGE->get_renderer('tool_lp');
93 $result = $exporter->export($output);
97 * @expectedException coding_exception
99 public function test_invalid_related() {
101 $exporter = new core_competency_testable_exporter($this->validdata, $this->invalidrelated);
102 $output = $PAGE->get_renderer('tool_lp');
104 $result = $exporter->export($output);
107 public function test_valid_data_and_related() {
109 $exporter = new core_competency_testable_exporter($this->validdata, $this->validrelated);
111 $output = $PAGE->get_renderer('tool_lp');
113 $result = $exporter->export($output);
118 * Example persistent class.
120 * @package core_competency
121 * @copyright 2015 Frédéric Massart - FMCorz.net
122 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
124 class core_competency_testable_exporter extends \core_competency\external\exporter {
126 protected static function define_related() {
127 // We cache the context so it does not need to be retrieved from the course.
128 return array('simplestdClass' => 'stdClass', 'arrayofstdClass' => 'stdClass[]');
131 protected function get_other_values(renderer_base $output) {
133 'otherstring' => 'An other string',
134 'otherstrings' => array('String a', 'String b')
138 public static function define_properties() {
143 'stringAformat' => array(
152 public static function define_other_properties() {
154 'otherstring' => array(
155 'type' => PARAM_TEXT,
157 'otherstrings' => array(
158 'type' => PARAM_TEXT,