Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 
19
use csv_export_writer;
20
use csv_import_reader;
21
 
22
defined('MOODLE_INTERNAL') || die();
23
 
24
global $CFG;
25
require_once($CFG->dirroot . '/lib/csvlib.class.php');
26
 
27
/**
28
 * Tests csv import and export functions.
29
 *
30
 * @package    core
31
 * @category   test
32
 * @copyright  2012 Adrian Greeve
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
1441 ariadna 35
final class csvclass_test extends \advanced_testcase {
1 efrain 36
 
37
    protected $testdata = array();
38
    protected $teststring = '';
39
    protected $teststring2 = '';
40
    protected $teststring3 = '';
41
    protected $teststring4 = '';
42
 
43
    protected function setUp(): void {
1441 ariadna 44
        parent::setUp();
1 efrain 45
 
46
        $this->resetAfterTest();
47
 
48
        $csvdata = array();
49
        $csvdata[0][] = 'fullname';
50
        $csvdata[0][] = 'description of things';
51
        $csvdata[0][] = 'beer';
52
        $csvdata[1][] = 'William B Stacey';
53
        $csvdata[1][] = '<p>A field that contains "double quotes"</p>';
54
        $csvdata[1][] = 'Asahi';
55
        $csvdata[2][] = 'Phillip Jenkins';
56
        $csvdata[2][] = '<p>This field has </p>
57
<p>Multiple lines</p>
58
<p>and also contains "double quotes"</p>';
59
        $csvdata[2][] = 'Yebisu';
60
        $this->testdata = $csvdata;
61
 
62
        // Please note that each line needs a carriage return.
63
        $this->teststring = 'fullname,"description of things",beer
64
"William B Stacey","<p>A field that contains ""double quotes""</p>",Asahi
65
"Phillip Jenkins","<p>This field has </p>
66
<p>Multiple lines</p>
67
<p>and also contains ""double quotes""</p>",Yebisu
68
';
69
 
70
        $this->teststring2 = 'fullname,"description of things",beer
71
"Fred Flint","<p>Find the stone inside the box</p>",Asahi,"A fourth column"
72
"Sarah Smith","<p>How are the people next door?</p>,Yebisu,"Forget the next"
73
';
74
 
75
        $this->teststring4 = 'fullname,"description of things",beer
76
"Douglas Dirk","<p>I am fine, thankyou.</p>",Becks
77
 
78
"Addelyn Francis","<p>Thanks for the cake</p>",Becks
79
"Josh Frankson","<p>Everything is fine</p>",Asahi
80
 
81
 
82
"Heath Forscyth","<p>We are going to make you lose your mind</p>",Fosters
83
';
84
    }
85
 
11 efrain 86
    public function test_csv_functions(): void {
1 efrain 87
        global $CFG;
88
        $csvexport = new csv_export_writer();
89
        $csvexport->set_filename('unittest');
90
        foreach ($this->testdata as $data) {
91
            $csvexport->add_data($data);
92
        }
93
        $csvoutput = $csvexport->print_csv_data(true);
94
        $this->assertSame($csvoutput, $this->teststring);
95
 
96
        $test_data = csv_export_writer::print_array($this->testdata, 'comma', '"', true);
97
        $this->assertSame($test_data, $this->teststring);
98
 
99
        // Testing that the content is imported correctly.
100
        $iid = csv_import_reader::get_new_iid('lib');
101
        $csvimport = new csv_import_reader($iid, 'lib');
102
        $contentcount = $csvimport->load_csv_content($this->teststring, 'utf-8', 'comma');
103
        $csvimport->init();
104
        $dataset = array();
105
        $dataset[] = $csvimport->get_columns();
106
        while ($record = $csvimport->next()) {
107
            $dataset[] = $record;
108
        }
109
        $csvimport->cleanup();
110
        $csvimport->close();
111
        $this->assertSame($dataset, $this->testdata);
112
 
113
        // Testing for the wrong count of columns.
114
        $errortext = get_string('csvweirdcolumns', 'error');
115
        $iid = csv_import_reader::get_new_iid('lib');
116
        $csvimport = new csv_import_reader($iid, 'lib');
117
        $contentcount = $csvimport->load_csv_content($this->teststring2, 'utf-8', 'comma');
118
        $importerror = $csvimport->get_error();
119
        $csvimport->cleanup();
120
        $csvimport->close();
121
        $this->assertSame($importerror, $errortext);
122
 
123
        // Testing for empty content.
124
        $errortext = get_string('csvemptyfile', 'error');
125
 
126
        $iid = csv_import_reader::get_new_iid('lib');
127
        $csvimport = new csv_import_reader($iid, 'lib');
128
        $contentcount = $csvimport->load_csv_content($this->teststring3, 'utf-8', 'comma');
129
        $importerror = $csvimport->get_error();
130
        $csvimport->cleanup();
131
        $csvimport->close();
132
        $this->assertSame($importerror, $errortext);
133
 
134
        // Testing for a tab separated file.
135
        // The tab separated file has a trailing tab and extra blank lines at the end of the file.
136
        $filename = $CFG->dirroot . '/lib/tests/fixtures/tabfile.csv';
137
        $fp = fopen($filename, 'r');
138
        $tabdata = fread($fp, filesize($filename));
139
        fclose($fp);
140
        $iid = csv_import_reader::get_new_iid('tab');
141
        $csvimport = new csv_import_reader($iid, 'tab');
142
        $contentcount = $csvimport->load_csv_content($tabdata, 'utf-8', 'tab');
143
        // This should import four rows including the headings.
144
        $this->assertEquals($contentcount, 4);
145
 
146
        // Testing for empty lines.
147
        $iid = csv_import_reader::get_new_iid('blanklines');
148
        $csvimport = new csv_import_reader($iid, 'blanklines');
149
        $contentcount = $csvimport->load_csv_content($this->teststring4, 'utf-8', 'comma');
150
        // Five lines including the headings should be imported.
151
        $this->assertEquals($contentcount, 5);
152
    }
153
}