ReportTest.php 10.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php

//namespace qfq;

//use qfq;

require_once(__DIR__ . '/AbstractDatabaseTest.php');
require_once(__DIR__ . '/../../qfq/report/Report.php');
require_once(__DIR__ . '/../../qfq/store/Store.php');
require_once(__DIR__ . '/../../qfq/Evaluate.php');

/**
 * Created by PhpStorm.
 * User: crose
 * Date: 2/2/16
 * Time: 10:07 PM
 */
class ReportTest extends AbstractDatabaseTest {

    /**
21
     * @var qfq\Evaluate
22
23
24
25
     */
    private $eval = null;

    /**
26
     * @var qfq\Report
27
28
29
30
31
32
     */
    private $report = null;

    /**
     *
     */
33
    public function testReportSingleQuery() {
34

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
        //empty
        $result = $this->report->process("");
        $this->assertEquals('', $result);

        // comment
        $result = $this->report->process('# 10.sql = SELECT "Hello World"');
        $this->assertEquals('', $result);

        // sql
        $result = $this->report->process('10.sql = SELECT "Hello World"');
        $this->assertEquals('Hello World', $result);

        // sql, 2 rows
        $result = $this->report->process('10.sql = SELECT name, firstname FROM Person ORDER BY id LIMIT 2');
        $this->assertEquals('DoeJohnSmithJane', $result);

        // sql, 2 rows, head
        $result = $this->report->process("10.sql = SELECT name, firstname FROM Person ORDER BY id LIMIT 2\n10.head = <table>");
        $this->assertEquals('<table>DoeJohnSmithJane', $result);

        // sql, 2 rows, head, tail
        $result = $this->report->process("10.sql = SELECT name, firstname FROM Person ORDER BY id LIMIT 2\n10.head = <table>\n10.tail = </table>");
        $this->assertEquals('<table>DoeJohnSmithJane</table>', $result);

        // sql, 2 rows, head, tail, rbeg
        $result = $this->report->process("10.sql = SELECT name, firstname FROM Person ORDER BY id LIMIT 2\n10.head = <table>\n10.tail = </table>\n10.rbeg = <tr>");
        $this->assertEquals('<table><tr>DoeJohn<tr>SmithJane</table>', $result);

        // sql, 2 rows, head, tail, rbeg, rend
        $result = $this->report->process("10.sql = SELECT name, firstname FROM Person ORDER BY id LIMIT 2\n10.head = <table>\n10.tail = </table>\n10.rbeg = <tr>\n10.rend = </tr>");
        $this->assertEquals('<table><tr>DoeJohn</tr><tr>SmithJane</tr></table>', $result);

        // sql, 2 rows, head, tail, rbeg, rend, fbeg
        $result = $this->report->process("10.sql = SELECT name, firstname FROM Person ORDER BY id LIMIT 2\n10.head = <table>\n10.tail = </table>\n10.rbeg = <tr>\n10.rend = </tr>\n10.fbeg = <td>");
        $this->assertEquals('<table><tr><td>Doe<td>John</tr><tr><td>Smith<td>Jane</tr></table>', $result);

        // sql, 2 rows, head, tail, rbeg, rend, fbeg, fend
        $result = $this->report->process("10.sql = SELECT name, firstname FROM Person ORDER BY id LIMIT 2\n10.head = <table>\n10.tail = </table>\n10.rbeg = <tr>\n10.rend = </tr>\n10.fbeg = <td>\n10.fend = </td>");
        $this->assertEquals('<table><tr><td>Doe</td><td>John</td></tr><tr><td>Smith</td><td>Jane</td></tr></table>', $result);
74

75
76
77
        // sql, 2 rows, head, tail, rbeg, rend, fbeg, fend, renr
        $result = $this->report->process("10.sql = SELECT name, firstname FROM Person ORDER BY id LIMIT 2\n10.head = <table>\n10.tail = </table>\n10.rbeg = <tr>\n10.rend = ++\n10.fbeg = <td>\n10.fend = </td>\n10.renr = </tr>");
        $this->assertEquals('<table><tr><td>Doe</td><td>John</td>++</tr><tr><td>Smith</td><td>Jane</td>++</tr></table>', $result);
78

79
80
81
82
83
84
85
86
87
88
        // sql, 2 rows, head, tail, rbeg, rend, fbeg, fend, renr, rsep
        $result = $this->report->process("10.sql = SELECT name, firstname FROM Person ORDER BY id LIMIT 2\n10.head = <table>\n10.tail = </table>\n10.rbeg = <tr>\n10.rend = ++\n10.fbeg = <td>\n10.fend = </td>\n10.renr = </tr>\n10.rsep = @");
        $this->assertEquals('<table><tr><td>Doe</td><td>John</td>++</tr>@<tr><td>Smith</td><td>Jane</td>++</tr></table>', $result);

        // sql, 2 rows, head, tail, rbeg, rend, fbeg, fend, renr, rsep, fsep
        $result = $this->report->process("10.sql = SELECT name, firstname FROM Person ORDER BY id LIMIT 2\n10.head = <table>\n10.tail = </table>\n10.rbeg = <tr>\n10.rend = ++\n10.fbeg = <td>\n10.fend = </td>\n10.renr = </tr>\n10.rsep = @\n10.fsep = $$");
        $this->assertEquals('<table><tr><td>Doe</td>$$<td>John</td>++</tr>@<tr><td>Smith</td>$$<td>Jane</td>++</tr></table>', $result);

        // no 'sql'
        $result = $this->report->process("10.tail = </table>\n10.rbeg = <tr>\n10.rend = ++\n10.fbeg = <td>\n10.fend = </td>\n10.renr = </tr>\n10.rsep = @\n10.fsep = $$");
89
        $this->assertEquals('', $result);
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

        // head, tail, rbeg, rend, fbeg, fend, renr, rsep, fsep, sql 2 rows
        $result = $this->report->process("10.head = <table>\n10.tail = </table>\n10.rbeg = <tr>\n10.rend = ++\n10.fbeg = <td>\n10.fend = </td>\n10.renr = </tr>\n10.rsep = @\n10.fsep = $$\n10.sql = SELECT name, firstname FROM Person ORDER BY id LIMIT 2");
        $this->assertEquals('<table><tr><td>Doe</td>$$<td>John</td>++</tr>@<tr><td>Smith</td>$$<td>Jane</td>++</tr></table>', $result);
    }

    /**
     *
     */
    public function testReportMultipleQuery() {

        // nested 2
        $result = $this->report->process("10.sql = SELECT id FROM Person ORDER BY id LIMIT 2\n10.10.sql = SELECT name FROM Person ORDER BY id LIMIT 2\n");
        $this->assertEquals('1DoeSmith2DoeSmith', $result);

        // nested 3
        $result = $this->report->process("10.sql = SELECT id FROM Person ORDER BY id LIMIT 2\n10.10.sql = SELECT name FROM Person ORDER BY id LIMIT 2\n\n10.10.10.sql = SELECT firstname FROM Person ORDER BY id LIMIT 2\n");
        $this->assertEquals('1DoeJohnJaneSmithJohnJane2DoeJohnJaneSmithJohnJane', $result);

        // nested 2, seq
        $result = $this->report->process("10.sql = SELECT id FROM Person ORDER BY id LIMIT 2\n10.10.sql = SELECT name FROM Person ORDER BY id LIMIT 2\n\n10.20.sql = SELECT firstname FROM Person ORDER BY id LIMIT 2\n");
        $this->assertEquals('1DoeSmithJohnJane2DoeSmithJohnJane', $result);
112
113
    }

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    /**
     *
     */
    public function testReportLink() {

        // link
        $result = $this->report->process("10.sql = SELECT 'u:http://www.example.com' AS _link FROM Person ORDER BY id LIMIT 1");
        $this->assertEquals('<a href="http://www.example.com" class="external" >http://www.example.com</a>', $result);

        // link, checked
        $result = $this->report->process("10.sql = SELECT 'u:http://www.example.com|C' AS _link FROM Person ORDER BY id LIMIT 1");
        $this->assertEquals('<a href="http://www.example.com" ><img src="typo3conf/ext/qfq/Resources/Public/icons/checked-green.gif" title="green" > </a>', $result);

        // linck, checked, text
        $result = $this->report->process("10.sql = SELECT 'u:http://www.example.com|C|t:Hello World' AS _link FROM Person ORDER BY id LIMIT 1");
        $this->assertEquals('<a href="http://www.example.com" ><img src="typo3conf/ext/qfq/Resources/Public/icons/checked-green.gif" title="green" > Hello World</a>', $result);

        // link, checked, text, tooltip
        $result = $this->report->process("10.sql = SELECT 'u:http://www.example.com|C|t:Hello World|o:more information' AS _link FROM Person ORDER BY id LIMIT 1");
        $this->assertEquals('<a href="http://www.example.com" title="more information" ><img src="typo3conf/ext/qfq/Resources/Public/icons/checked-green.gif" title="green" > Hello World</a>', $result);
    }

    /**
     *
     */
    public function testReportLinkRenderMode() {

        // r:0, url + text
        $result = $this->report->process("10.sql = SELECT 'u:http://www.example.com|t:Hello World|r:0' AS _link FROM Person ORDER BY id LIMIT 1");
        $this->assertEquals('<a href="http://www.example.com" class="external" >Hello World</a>', $result);

        // r:0, url
        $result = $this->report->process("10.sql = SELECT 'u:http://www.example.com|r:0' AS _link FROM Person ORDER BY id LIMIT 1");
        $this->assertEquals('<a href="http://www.example.com" class="external" >http://www.example.com</a>', $result);

        // r:0, text
        $result = $this->report->process("10.sql = SELECT 'r:0|t:Hello World' AS _link FROM Person ORDER BY id LIMIT 1");
        $this->assertEquals('', $result);

        // r:1, url + text
        $result = $this->report->process("10.sql = SELECT 'u:http://www.example.com|t:Hello World|r:1' AS _link FROM Person ORDER BY id LIMIT 1");
        $this->assertEquals('<a href="http://www.example.com" class="external" >Hello World</a>', $result);

        // r:1, url
        $result = $this->report->process("10.sql = SELECT 'u:http://www.example.com|r:1' AS _link FROM Person ORDER BY id LIMIT 1");
        $this->assertEquals('<a href="http://www.example.com" class="external" >http://www.example.com</a>', $result);

        // r:1, text
        $result = $this->report->process("10.sql = SELECT 'r:1|t:Hello World' AS _link FROM Person ORDER BY id LIMIT 1");
        $this->assertEquals('Hello World', $result);

    }

    /**
     *
     */
    public function testReportMailto() {

        // r:0, mailto
        $result = $this->report->process("10.sql = SELECT 'm:john.doe@example.com' AS _link FROM Person ORDER BY id LIMIT 1");
        $this->assertEquals('<a href="mailto:john.doe@example.com" >mailto:john.doe@example.com</a>', $result);

        $result = $this->report->process("10.sql = SELECT 'm:john.doe@example.com|e:0' AS _link FROM Person ORDER BY id LIMIT 1");
        $this->assertEquals('<a href="mailto:john.doe@example.com" >mailto:john.doe@example.com</a>', $result);

        //TODO: enable encryption check
//        $result = $this->report->process("10.sql = SELECT 'm:john.doe@example.com|e:1' AS _link FROM Person ORDER BY id LIMIT 1");
//        $this->assertEquals('<a href="mailto:john.doe@example.com" >mailto:john.doe@example.com</a>', $result);

    }

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    /**
     *
     */
    public function testReportPageToken() {

        //
//        $result = $this->report->process("10.sql = SELECT 'p:form&r=123&a=hello&type=5&L=3&final=world|t:Person' AS _page FROM Person ORDER BY id LIMIT 1");
//        $this->assertEquals('', $result);


    }

    /**
     *
     */
    public function testReportPageFix() {

        //
//        $result = $this->report->process("10.sql = SELECT 'form|r=123&a=hello&type=5&L=3&final=world|Person' AS _Page FROM Person ORDER BY id LIMIT 1");
//        $this->assertEquals('', $result);


    }

209

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
    /**
     * @throws Exception
     */
    protected function setUp() {

        parent::setUp();
        $GLOBALS["TSFE"] = new FakeTSFEReport();
        $this->eval = new qfq\Evaluate($this->store, $this->db);
        $this->report = new qfq\Report(array(), $this->store->getVar(SYSTEM_SESSION_NAME, STORE_SYSTEM), $this->eval, true);

        $this->executeSQLFile(__DIR__ . '/fixtures/Generic.sql', true);
    }
}

class FakeTSFEReport {
    public $id = 1;
    public $type = 1;
    public $sys_language_uid = 1;
}