Merge branch 'MDL-72965-master' of https://github.com/peterRd/moodle
[moodle.git] / lib / tests / dataformat_test.php
blobebd10b0910a44e728ab007c555f00e5ce0bbf402
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.
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 * Tests for the dataformat plugins
20 * @package core
21 * @copyright 2020 Paul Holden <paulh@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core;
27 use context_system;
28 use core_component;
30 /**
31 * Dataformat tests
33 * @package core
34 * @covers \core\dataformat
35 * @copyright 2020 Paul Holden <paulh@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class dataformat_test extends \advanced_testcase {
40 /**
41 * Data provider to return array of dataformat types
43 * @return array
45 public function write_data_provider(): array {
46 $data = [];
48 $dataformats = core_component::get_plugin_list('dataformat');
49 foreach ($dataformats as $dataformat => $unused) {
50 $data[] = [$dataformat];
53 return $data;
56 /**
57 * Test writing dataformat export to local file
59 * @param string $dataformat
60 * @return void
62 * @dataProvider write_data_provider
64 public function test_write_data(string $dataformat): void {
65 $columns = ['fruit', 'colour', 'animal'];
66 $rows = [
67 ['banana', 'yellow', 'monkey'],
68 ['apple', 'red', 'wolf'],
69 ['melon', 'green', 'aardvark'],
72 // Export to file. Assert that the exported file exists and is non-zero in size.
73 $exportfile = dataformat::write_data('My export', $dataformat, $columns, $rows);
74 $this->assertFileExists($exportfile);
75 $this->assertGreaterThan(0, filesize($exportfile));
78 /**
79 * Test writing dataformat export to filearea
81 * @param string $dataformat
82 * @return void
84 * @dataProvider write_data_provider
86 public function test_write_data_to_filearea(string $dataformat): void {
87 $this->resetAfterTest();
89 $columns = ['fruit', 'colour', 'animal'];
90 $rows = [
91 ['banana', 'yellow', 'monkey'],
92 ['apple', 'red', 'wolf'],
93 ['melon', 'green', 'aardvark'],
96 // Export to filearea. Assert that the the file exists in file storage and matches the original file record.
97 $filerecord = [
98 'contextid' => context_system::instance()->id,
99 'component' => 'core_dataformat',
100 'filearea' => 'test',
101 'itemid' => 0,
102 'filepath' => '/',
103 'filename' => 'My export',
106 $file = dataformat::write_data_to_filearea($filerecord, $dataformat, $columns, $rows);
107 $this->assertEquals($filerecord['contextid'], $file->get_contextid());
108 $this->assertEquals($filerecord['component'], $file->get_component());
109 $this->assertEquals($filerecord['filearea'], $file->get_filearea());
110 $this->assertEquals($filerecord['itemid'], $file->get_itemid());
111 $this->assertEquals($filerecord['filepath'], $file->get_filepath());
112 $this->assertStringStartsWith($filerecord['filename'], $file->get_filename());
113 $this->assertGreaterThan(0, $file->get_filesize());