1<?php
2
3/**
4 * @group plugin_csv
5 * @group plugins
6 */
7class syntax_plugin_csv_test extends DokuWikiTest {
8
9    private $delimiters = array(
10        'c' => ',',
11        's' => ';',
12        't' => "\t"
13    );
14
15    private $enclosings = array(
16        'q' => '"',
17        's' => "'",
18    );
19
20    private $escapes = array(
21        'q' => '"',
22        'b' => '\\'
23    );
24
25    function test_files(){
26        // run through all the test files
27        $files = glob(__DIR__.'/csv/*.csv');
28        foreach($files as $file){
29            // load test csv and json files
30            $csv  = file_get_contents($file);
31            $file = basename($file, '.csv');
32            $json = file_get_contents(__DIR__.'/json/'.$file.'.json');
33
34            // get delimiter configs form file name
35            list($delim, $enc, $esc) =  explode('-', $file);
36            $delim = $this->delimiters[$delim];
37            $enc = $this->enclosings[$enc];
38            $esc = $this->escapes[$esc];
39
40            // test
41            $this->assertEquals(json_decode($json, true), $this->csvparse($csv, $delim, $enc, $esc), $file);
42        }
43    }
44
45    /**
46     * Calls the CSV line parser of our plugin and returns the whole array
47     *
48     * @param string $csvdata
49     * @param string $delim
50     * @param string $enc
51     * @param string $esc
52     * @return array
53     */
54    function csvparse($csvdata, $delim, $enc, $esc){
55
56        $data = array();
57
58        while($csvdata != '') {
59            $line = helper_plugin_csv::csv_explode_row($csvdata, $delim, '"', '"');
60            if($line !== false) array_push($data, $line);
61        }
62
63        return $data;
64    }
65
66    /**
67     * check general content loading
68     */
69    function test_content() {
70        $contents = file_get_contents(__DIR__ . '/avengers.csv');
71
72        $opt = helper_plugin_csv::getDefaultOpt();
73
74        $data = helper_plugin_csv::prepareData($contents, $opt);
75        $this->assertSame(174, count($data), 'number of rows');
76        $this->assertSame(21, count($data[0]), 'number of columns');
77    }
78
79    /**
80     * check general content loading
81     */
82    function test_filter() {
83        $contents = file_get_contents(__DIR__ . '/avengers.csv');
84
85        $opt = helper_plugin_csv::getDefaultOpt();
86        $opt['filter'][4] = '^FEMALE$';
87
88        $data = helper_plugin_csv::prepareData($contents, $opt);
89        $this->assertSame(59, count($data), 'number of rows');
90        $this->assertSame(21, count($data[0]), 'number of columns');
91
92        $opt['filter'][1] = '^.*?jessica.*?$';
93        $data = helper_plugin_csv::prepareData($contents, $opt);
94        $this->assertSame(3, count($data), 'number of rows');
95        $this->assertSame(21, count($data[0]), 'number of columns');
96
97        $this->assertEquals('Jessica Jones', $data[2][1]);
98    }
99
100    /**
101     * check the option parsing
102     */
103    function test_options() {
104        $opt = helper_plugin_csv::getDefaultOpt();
105        $this->assertEquals($opt, helper_plugin_csv::parseOptions(''));
106
107        $opt = helper_plugin_csv::parseOptions('foo.csv');
108        $this->assertEquals(':foo.csv', $opt['file']);
109
110        $opt = helper_plugin_csv::parseOptions('file=foo.csv');
111        $this->assertEquals(':foo.csv', $opt['file']);
112
113        $opt = helper_plugin_csv::parseOptions('file="foo.csv"');
114        $this->assertEquals(':foo.csv', $opt['file']);
115
116        $opt = helper_plugin_csv::parseOptions('delim=tab');
117        $this->assertEquals("\t", $opt['delim']);
118
119        $opt = helper_plugin_csv::parseOptions('filter[2]="*t(es)t*"');
120        $this->assertEquals('^.*?t\(es\)t.*?$', $opt['filter'][1]);
121
122        $opt = helper_plugin_csv::parseOptions('filter[2][r]="t(es)t.*"');
123        $this->assertEquals('t(es)t.*', $opt['filter'][1]);
124
125        $opt = helper_plugin_csv::parseOptions('foo="with spaces"');
126        $this->assertEquals('with spaces', $opt['foo']);
127
128        $opt = helper_plugin_csv::parseOptions('output=4,3');
129        $this->assertSame(3, $opt['outc']);
130        $this->assertSame(2, $opt['outr']);
131
132        $opt = helper_plugin_csv::parseOptions('output=4');
133        $this->assertSame(3, $opt['outc']);
134        $this->assertSame(0, $opt['outr']);
135    }
136}
137