| 1 | efrain | 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 | namespace core;
 | 
        
           |  |  | 18 |   | 
        
           | 11 | efrain | 19 | use coding_exception;
 | 
        
           | 1 | efrain | 20 | use context_system;
 | 
        
           |  |  | 21 | use core_component;
 | 
        
           |  |  | 22 |   | 
        
           |  |  | 23 | /**
 | 
        
           | 11 | efrain | 24 |  * Tests for the dataformat plugins
 | 
        
           | 1 | efrain | 25 |  *
 | 
        
           |  |  | 26 |  * @package    core
 | 
        
           |  |  | 27 |  * @covers     \core\dataformat
 | 
        
           |  |  | 28 |  * @copyright  2020 Paul Holden <paulh@moodle.com>
 | 
        
           |  |  | 29 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 30 |  */
 | 
        
           | 11 | efrain | 31 | final class dataformat_test extends \advanced_testcase {
 | 
        
           | 1 | efrain | 32 |   | 
        
           |  |  | 33 |     /**
 | 
        
           | 11 | efrain | 34 |      * Test getting writer instance for given dataformat
 | 
        
           |  |  | 35 |      */
 | 
        
           |  |  | 36 |     public function test_get_format_instance(): void {
 | 
        
           |  |  | 37 |         $instance = dataformat::get_format_instance('pdf');
 | 
        
           |  |  | 38 |         $this->assertInstanceOf(\dataformat_pdf\writer::class, $instance);
 | 
        
           |  |  | 39 |     }
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 |     /**
 | 
        
           |  |  | 42 |      * Test getting writer instance for invalid dataformat
 | 
        
           |  |  | 43 |      */
 | 
        
           |  |  | 44 |     public function test_get_format_instance_invalid(): void {
 | 
        
           |  |  | 45 |         $this->expectException(coding_exception::class);
 | 
        
           |  |  | 46 |         $this->expectExceptionMessage('Invalid dataformat (weird)');
 | 
        
           |  |  | 47 |         dataformat::get_format_instance('weird');
 | 
        
           |  |  | 48 |     }
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |     /**
 | 
        
           | 1 | efrain | 51 |      * Data provider to return array of dataformat types
 | 
        
           |  |  | 52 |      *
 | 
        
           |  |  | 53 |      * @return array
 | 
        
           |  |  | 54 |      */
 | 
        
           | 11 | efrain | 55 |     public static function write_data_provider(): array {
 | 
        
           | 1 | efrain | 56 |         $data = [];
 | 
        
           |  |  | 57 |   | 
        
           |  |  | 58 |         $dataformats = core_component::get_plugin_list('dataformat');
 | 
        
           |  |  | 59 |         foreach ($dataformats as $dataformat => $unused) {
 | 
        
           |  |  | 60 |             $data[] = [$dataformat];
 | 
        
           |  |  | 61 |         }
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 |         return $data;
 | 
        
           |  |  | 64 |     }
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 |     /**
 | 
        
           |  |  | 67 |      * Test writing dataformat export to local file
 | 
        
           |  |  | 68 |      *
 | 
        
           |  |  | 69 |      * @param string $dataformat
 | 
        
           |  |  | 70 |      * @return void
 | 
        
           |  |  | 71 |      *
 | 
        
           |  |  | 72 |      * @dataProvider write_data_provider
 | 
        
           |  |  | 73 |      */
 | 
        
           |  |  | 74 |     public function test_write_data(string $dataformat): void {
 | 
        
           |  |  | 75 |         $columns = ['fruit', 'colour', 'animal'];
 | 
        
           |  |  | 76 |         $rows = [
 | 
        
           |  |  | 77 |             ['banana', 'yellow', 'monkey'],
 | 
        
           |  |  | 78 |             ['apple', 'red', 'wolf'],
 | 
        
           |  |  | 79 |             ['melon', 'green', 'aardvark'],
 | 
        
           |  |  | 80 |         ];
 | 
        
           |  |  | 81 |   | 
        
           |  |  | 82 |         // Export to file. Assert that the exported file exists and is non-zero in size.
 | 
        
           |  |  | 83 |         $exportfile = dataformat::write_data('My export', $dataformat, $columns, $rows);
 | 
        
           |  |  | 84 |         $this->assertFileExists($exportfile);
 | 
        
           |  |  | 85 |         $this->assertGreaterThan(0, filesize($exportfile));
 | 
        
           |  |  | 86 |     }
 | 
        
           |  |  | 87 |   | 
        
           |  |  | 88 |     /**
 | 
        
           |  |  | 89 |      * Test writing dataformat export to filearea
 | 
        
           |  |  | 90 |      *
 | 
        
           |  |  | 91 |      * @param string $dataformat
 | 
        
           |  |  | 92 |      * @return void
 | 
        
           |  |  | 93 |      *
 | 
        
           |  |  | 94 |      * @dataProvider write_data_provider
 | 
        
           |  |  | 95 |      */
 | 
        
           |  |  | 96 |     public function test_write_data_to_filearea(string $dataformat): void {
 | 
        
           |  |  | 97 |         $this->resetAfterTest();
 | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 |         $columns = ['fruit', 'colour', 'animal'];
 | 
        
           |  |  | 100 |         $rows = [
 | 
        
           |  |  | 101 |             ['banana', 'yellow', 'monkey'],
 | 
        
           |  |  | 102 |             ['apple', 'red', 'wolf'],
 | 
        
           |  |  | 103 |             ['melon', 'green', 'aardvark'],
 | 
        
           |  |  | 104 |         ];
 | 
        
           |  |  | 105 |   | 
        
           |  |  | 106 |         // Export to filearea. Assert that the the file exists in file storage and matches the original file record.
 | 
        
           |  |  | 107 |         $filerecord = [
 | 
        
           |  |  | 108 |             'contextid' => context_system::instance()->id,
 | 
        
           |  |  | 109 |             'component' => 'core_dataformat',
 | 
        
           |  |  | 110 |             'filearea' => 'test',
 | 
        
           |  |  | 111 |             'itemid' => 0,
 | 
        
           |  |  | 112 |             'filepath' => '/',
 | 
        
           |  |  | 113 |             'filename' => 'My export',
 | 
        
           |  |  | 114 |         ];
 | 
        
           |  |  | 115 |   | 
        
           |  |  | 116 |         $file = dataformat::write_data_to_filearea($filerecord, $dataformat, $columns, $rows);
 | 
        
           |  |  | 117 |         $this->assertEquals($filerecord['contextid'], $file->get_contextid());
 | 
        
           |  |  | 118 |         $this->assertEquals($filerecord['component'], $file->get_component());
 | 
        
           |  |  | 119 |         $this->assertEquals($filerecord['filearea'], $file->get_filearea());
 | 
        
           |  |  | 120 |         $this->assertEquals($filerecord['itemid'], $file->get_itemid());
 | 
        
           |  |  | 121 |         $this->assertEquals($filerecord['filepath'], $file->get_filepath());
 | 
        
           |  |  | 122 |         $this->assertStringStartsWith($filerecord['filename'], $file->get_filename());
 | 
        
           |  |  | 123 |         $this->assertGreaterThan(0, $file->get_filesize());
 | 
        
           |  |  | 124 |     }
 | 
        
           |  |  | 125 | }
 |