FormActionTest.php 17.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/15/16
 * Time: 8:24 AM
 */

namespace qfq;

11
require_once(__DIR__ . '/../database/AbstractDatabaseTest.php');
12

13
14
15
16
/**
 * Class FormActionTest
 * @package qfq
 */
17
class FormActionTest extends AbstractDatabaseTest {
18

Carsten  Rose's avatar
Carsten Rose committed
19
20
21
22
23
24
25
26
27
28
    /**
     * @throws CodeException
     * @throws DbException
     * @throws DownloadException
     * @throws UserFormException
     * @throws UserReportException
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
29
30
    public function testBeforeLoad() {
        $formSpec[F_TABLE_NAME] = 'Person';
31
        $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true);
32
33
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

        // Nothing to do: should not throw an exception
        $formAction->elements(0, array(), '');
        $formAction->elements(0, array(), FE_TYPE_BEFORE_LOAD . ',' . FE_TYPE_AFTER_LOAD);

        $feSpecAction[FE_NAME] = '';
        $feSpecAction[FE_TYPE] = FE_TYPE_BEFORE_LOAD;
        $feSpecAction[FE_MESSAGE_FAIL] = 'error';
        $formAction->elements(0, [$feSpecAction], FE_TYPE_BEFORE_LOAD);

        // Fire sqlValidate with one record, expect 1 record
        $feSpecAction[FE_TYPE] = FE_TYPE_BEFORE_LOAD;
        $feSpecAction[FE_SQL_VALIDATE] = '{{!SELECT id FROM Person LIMIT 1}}';
        $feSpecAction[FE_EXPECT_RECORDS] = '1';
        $formAction->elements(0, [$feSpecAction], FE_TYPE_BEFORE_LOAD);

        // Fire sqlValidate with one record, expect 0-2 records
        $feSpecAction[FE_EXPECT_RECORDS] = '0,1,2';
        $formAction->elements(0, [$feSpecAction], FE_TYPE_BEFORE_LOAD);

        // Fire sqlValidate with one record, expect 0-2 records
        $feSpecAction[FE_TYPE] = FE_TYPE_BEFORE_LOAD;
        $feSpecAction[FE_EXPECT_RECORDS] = '0,1,2';
        $formAction->elements(0, [$feSpecAction], FE_TYPE_BEFORE_LOAD);

        // Check with more classes
        $feSpecAction[FE_TYPE] = FE_TYPE_AFTER_LOAD;
        $feSpecAction[FE_EXPECT_RECORDS] = '0,1,2';
        $formAction->elements(0, [$feSpecAction], FE_TYPE_BEFORE_LOAD . ',' . FE_TYPE_AFTER_LOAD);
Carsten  Rose's avatar
Carsten Rose committed
61
62
63

        # Fake to suppress phpUnit about missing test.
        $this->assertEquals('', '');
64
65
66
67
68
    }

    /**
     * Expect 0 recrod, but get 1
     * @expectedException \qfq\UserFormException
Carsten  Rose's avatar
Carsten Rose committed
69
70
71
72
73
74
75
76
77
78
     *
     * @throws CodeException
     * @throws DbException
     * @throws DownloadException
     * @throws UserFormException
     * @throws UserReportException
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
79
80
    public function testBeforeLoadException1() {
        $formSpec[F_TABLE_NAME] = 'Person';
81
        $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true);
82
83
84
85
86
87
88
89
90
91
92
93

        $feSpecAction[FE_NAME] = '';
        $feSpecAction[FE_TYPE] = FE_TYPE_BEFORE_LOAD;
        $feSpecAction[FE_MESSAGE_FAIL] = 'error';
        $feSpecAction[FE_SQL_VALIDATE] = '{{!SELECT id FROM Person LIMIT 1}}';
        $feSpecAction[FE_EXPECT_RECORDS] = '0';
        $formAction->elements(0, [$feSpecAction], FE_TYPE_BEFORE_LOAD);
    }

    /**
     * Expect 1 recrod, but get 0
     * @expectedException \qfq\UserFormException
Carsten  Rose's avatar
Carsten Rose committed
94
95
96
97
98
99
100
101
102
103
     *
     * @throws CodeException
     * @throws DbException
     * @throws DownloadException
     * @throws UserFormException
     * @throws UserReportException
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
104
105
    public function testBeforeLoadException0() {
        $formSpec[F_TABLE_NAME] = 'Person';
106
        $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true);
107
108
109
110
111
112
113
114
115
116
117
118

        $feSpecAction[FE_NAME] = '';
        $feSpecAction[FE_TYPE] = FE_TYPE_BEFORE_LOAD;
        $feSpecAction[FE_MESSAGE_FAIL] = 'error';
        $feSpecAction[FE_SQL_VALIDATE] = '{{!SELECT id FROM Person LIMIT 0}}';
        $feSpecAction[FE_EXPECT_RECORDS] = '1';
        $formAction->elements(0, [$feSpecAction], FE_TYPE_BEFORE_LOAD);
    }

    /**
     * Expect '0,1', but get 2 records
     * @expectedException \qfq\UserFormException
Carsten  Rose's avatar
Carsten Rose committed
119
120
121
122
123
124
125
126
127
128
     *
     * @throws CodeException
     * @throws DbException
     * @throws DownloadException
     * @throws UserFormException
     * @throws UserReportException
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
129
130
    public function testBeforeLoadException2() {
        $formSpec[F_TABLE_NAME] = 'Person';
131
        $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true);
132
133
134
135
136
137
138
139
140
141
142
143

        $feSpecAction[FE_NAME] = '';
        $feSpecAction[FE_TYPE] = FE_TYPE_BEFORE_LOAD;
        $feSpecAction[FE_MESSAGE_FAIL] = 'error';
        $feSpecAction[FE_SQL_VALIDATE] = '{{!SELECT id FROM Person LIMIT 2}}';
        $feSpecAction[FE_EXPECT_RECORDS] = '0,1';
        $formAction->elements(0, [$feSpecAction], FE_TYPE_BEFORE_LOAD);
    }

    /**
     * Expect '0,1', but get 2 records
     * @expectedException \qfq\UserFormException
Carsten  Rose's avatar
Carsten Rose committed
144
145
146
147
148
149
150
151
152
153
     *
     * @throws CodeException
     * @throws DbException
     * @throws DownloadException
     * @throws UserFormException
     * @throws UserReportException
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
154
155
    public function testBeforeLoadException3() {
        $formSpec[F_TABLE_NAME] = 'Person';
156
        $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true);
157
158
159
160
161
162
163
164
165
166
167

        $feSpecAction[FE_NAME] = '';
        $feSpecAction[FE_TYPE] = FE_TYPE_AFTER_UPDATE;
        $feSpecAction[FE_MESSAGE_FAIL] = 'error';
        $feSpecAction[FE_SQL_VALIDATE] = '{{!SELECT id FROM Person LIMIT 2}}';
        $feSpecAction[FE_EXPECT_RECORDS] = '0,1';
        $formAction->elements(0, [$feSpecAction], FE_TYPE_BEFORE_LOAD . ',' . FE_TYPE_AFTER_LOAD . ',' . FE_TYPE_AFTER_UPDATE . ',' . FE_TYPE_BEFORE_SAVE);
    }

    /**
     * Necessary FE is empty > don't process check
Carsten  Rose's avatar
Carsten Rose committed
168
169
170
171
172
173
174
175
176
177
     *
     * @throws CodeException
     * @throws DbException
     * @throws DownloadException
     * @throws UserFormException
     * @throws UserReportException
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
178
179
    public function testBeforeLoadException4() {
        $formSpec[F_TABLE_NAME] = 'Person';
180
        $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true);
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

        $feSpecAction[FE_NAME] = '';
        $feSpecAction[FE_TYPE] = FE_TYPE_AFTER_UPDATE;
        $feSpecAction[FE_MESSAGE_FAIL] = 'error';
        $feSpecAction[FE_SQL_VALIDATE] = '{{!SELECT id FROM Person LIMIT 2}}';
        $feSpecAction[FE_EXPECT_RECORDS] = '0,1';

        // one FE in list
        $this->store->setVar('street', 'Street', STORE_FORM, true);
        $this->store->setVar('city', '', STORE_FORM, true);
        $feSpecAction[FE_REQUIRED_LIST] = 'city';
        $formAction->elements(0, [$feSpecAction], FE_TYPE_BEFORE_LOAD . ',' . FE_TYPE_AFTER_LOAD . ',' . FE_TYPE_AFTER_UPDATE . ',' . FE_TYPE_BEFORE_SAVE);

        // three FE in list. one is set, one not, one dont exist
        $this->store->setVar('city', '', STORE_FORM, true);
        $feSpecAction[FE_REQUIRED_LIST] = 'street,city,downtown';
        $formAction->elements(0, [$feSpecAction], FE_TYPE_BEFORE_LOAD . ',' . FE_TYPE_AFTER_LOAD . ',' . FE_TYPE_AFTER_UPDATE . ',' . FE_TYPE_BEFORE_SAVE);
    }

    /**
     * Expect '0,1', but get 2 records
     * @expectedException \qfq\UserFormException
Carsten  Rose's avatar
Carsten Rose committed
203
204
205
206
207
208
209
210
211
212
     *
     * @throws CodeException
     * @throws DbException
     * @throws DownloadException
     * @throws UserFormException
     * @throws UserReportException
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
213
214
    public function testBeforeLoadException5() {
        $formSpec[F_TABLE_NAME] = 'Person';
215
        $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true);
216
217
218
219
220
221
222
223
224
225
226
227

        $this->store->setVar('city', 'New York', STORE_FORM, true);
        $feSpecAction[FE_NAME] = '';
        $feSpecAction[FE_TYPE] = FE_TYPE_AFTER_UPDATE;
        $feSpecAction[FE_MESSAGE_FAIL] = 'error';
        $feSpecAction[FE_SQL_VALIDATE] = '{{!SELECT id FROM Person LIMIT 2}}';
        $feSpecAction[FE_EXPECT_RECORDS] = '0,1';
        $formAction->elements(0, [$feSpecAction], FE_TYPE_BEFORE_LOAD . ',' . FE_TYPE_AFTER_LOAD . ',' . FE_TYPE_AFTER_UPDATE . ',' . FE_TYPE_BEFORE_SAVE);
    }

    /**
     * Do check for 2 action records
Carsten  Rose's avatar
Carsten Rose committed
228
229
230
231
232
233
234
235
236
237
     *
     * @throws CodeException
     * @throws DbException
     * @throws DownloadException
     * @throws UserFormException
     * @throws UserReportException
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
238
239
    public function testBeforeLoad2() {
        $formSpec[F_TABLE_NAME] = 'Person';
240
        $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true);
241
242
243
244
245
246
247
248
249
250
251
252

        $feSpecAction[FE_NAME] = '';
        $feSpecAction[FE_TYPE] = FE_TYPE_AFTER_LOAD;
        $feSpecAction[FE_MESSAGE_FAIL] = 'error';
        $feSpecAction[FE_SQL_VALIDATE] = '{{!SELECT id FROM Person LIMIT 2}}';
        $feSpecAction[FE_EXPECT_RECORDS] = '2';
        $formAction->elements(0, [$feSpecAction, $feSpecAction], FE_TYPE_BEFORE_LOAD . ',' . FE_TYPE_AFTER_LOAD . ',' . FE_TYPE_AFTER_UPDATE . ',' . FE_TYPE_BEFORE_SAVE);
    }

    /**
     * Do check for 2 action records, fail on second.
     * @expectedException \qfq\UserFormException
Carsten  Rose's avatar
Carsten Rose committed
253
254
255
256
257
258
259
260
261
262
     *
     * @throws CodeException
     * @throws DbException
     * @throws DownloadException
     * @throws UserFormException
     * @throws UserReportException
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
263
264
    public function testBeforeLoadException6() {
        $formSpec[F_TABLE_NAME] = 'Person';
265
        $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true);
266
267
268
269
270
271
272
273
274
275
276
277
278

        $feSpecAction[FE_NAME] = '';
        $feSpecAction[FE_TYPE] = FE_TYPE_AFTER_LOAD;
        $feSpecAction[FE_MESSAGE_FAIL] = 'error';
        $feSpecAction[FE_SQL_VALIDATE] = '{{!SELECT id FROM Person LIMIT 2}}';
        $feSpecAction[FE_EXPECT_RECORDS] = '2';
        $feSpecAction2 = $feSpecAction;
        $feSpecAction2[FE_EXPECT_RECORDS] = '0';
        $formAction->elements(0, [$feSpecAction, $feSpecAction2], FE_TYPE_BEFORE_LOAD . ',' . FE_TYPE_AFTER_LOAD . ',' . FE_TYPE_AFTER_UPDATE . ',' . FE_TYPE_BEFORE_SAVE);
    }

    /**
     * Process INSERT
Carsten  Rose's avatar
Carsten Rose committed
279
280
281
282
283
284
285
286
287
288
     *
     * @throws CodeException
     * @throws DbException
     * @throws DownloadException
     * @throws UserFormException
     * @throws UserReportException
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
289
290
291
    public function testInsert() {

        $formSpec[F_TABLE_NAME] = 'Person';
292
        $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true);
293
294
295
296
297
298
299
300
301
302
303
304

        $feSpecAction[FE_NAME] = '';
        $feSpecAction[FE_TYPE] = FE_TYPE_AFTER_SAVE;
        $feSpecAction[FE_SQL_INSERT] = '{{ INSERT INTO Address (city, personId) VALUES ("Downtown", {{r}}) }} ';
        $feSpecAction[FE_SQL_UPDATE] = '{{ UPDATE Address SET city="invalid" WHERE id={{r}} }} ';
        $feSpecAction[FE_SQL_DELETE] = '{{ DELETE FROM Address WHERE personId={{r}} AND id=0 }} ';

        $this->store->setVar('r', '2', STORE_SIP, true);


        // slaveId: 0
        $feSpecAction[FE_SLAVE_ID] = '0';
305
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql('TRUNCATE Address');
306
307
        $formAction->elements(2, [$feSpecAction], FE_TYPE_AFTER_SAVE);

308
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id, city, personId FROM Address', ROW_IMPLODE_ALL);
309
310
311
312
313
        $this->assertEquals('1Downtown2', $result);


        // slaveId: SELECT ... >> ''
        $feSpecAction[FE_SLAVE_ID] = '{{SELECT id FROM Address WHERE personId={{r}} ORDER BY id LIMIT 1}}';
314
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql('TRUNCATE Address');
315
316
        $formAction->elements(2, [$feSpecAction], FE_TYPE_AFTER_SAVE);

317
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id, city, personId FROM Address', ROW_IMPLODE_ALL);
318
319
320
321
322
323
324
325
        $this->assertEquals('1Downtown2', $result);


        // slaveId: slaveId through column in master record & update Master record with slaveId
        $this->store->setVar('adrId', '0', STORE_RECORD);
        $feSpecAction[FE_NAME] = 'adrId';
        $feSpecAction[FE_SLAVE_ID] = '';

326
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql('TRUNCATE Address');
327
328
329
        $formAction->elements(2, [$feSpecAction], FE_TYPE_AFTER_SAVE);

        // get the new slave record
330
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id, city, personId FROM Address', ROW_IMPLODE_ALL);
331
332
        $this->assertEquals('1Downtown2', $result);
        // get the updated id in the master record
333
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id, name, adrId FROM Person WHERE id=2', ROW_IMPLODE_ALL);
334
335
336
337
338
        $this->assertEquals('2Smith1', $result);
    }

    /**
     * Process UPDATE
Carsten  Rose's avatar
Carsten Rose committed
339
340
341
342
343
344
345
346
347
348
     *
     * @throws CodeException
     * @throws DbException
     * @throws DownloadException
     * @throws UserFormException
     * @throws UserReportException
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
349
350
351
    public function testUpdate() {

        $formSpec[F_TABLE_NAME] = 'Person';
352
        $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true);
353
354
355
356
357
358
359
360

        $masterId = 2;

        $feSpecAction[FE_NAME] = '';
        $feSpecAction[FE_TYPE] = FE_TYPE_AFTER_SAVE;
        $feSpecAction[FE_SQL_INSERT] = "{{ INSERT INTO Address (city, personId) VALUES ('invalid', {{r}}) }} ";
        $feSpecAction[FE_SQL_UPDATE] = "{{ UPDATE Address SET city='Uptown' WHERE id={{slaveId:V}} }} ";

361
362
363
364
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql("TRUNCATE Address");
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql("INSERT INTO Address (city, personId) VALUES ('Downtown1', 1)");
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql("INSERT INTO Address (city, personId) VALUES ('Downtown2', 1)");
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql("INSERT INTO Address (city, personId) VALUES ('Downtown3', $masterId)");
365
366
367
368
369
370
371

        $this->store->setVar('r', "$masterId", STORE_SIP, true);

        // slaveId: 1 - hard coded
        $feSpecAction[FE_SLAVE_ID] = '3';
        $formAction->elements($masterId, [$feSpecAction], FE_TYPE_AFTER_SAVE);

372
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id, city, personId FROM Address WHERE personId=$masterId", ROW_IMPLODE_ALL);
373
374
375
376
377
378
379
        $this->assertEquals('3Uptown' . $masterId, $result);


        // slaveId: SELECT ... >> ''
        $feSpecAction[FE_SLAVE_ID] = "{{SELECT id FROM Address WHERE personId={{r}} ORDER BY id LIMIT 1}}";
        $formAction->elements($masterId, [$feSpecAction], FE_TYPE_AFTER_SAVE);

380
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id, city, personId FROM Address WHERE personId=$masterId", ROW_IMPLODE_ALL);
381
382
383
384
        $this->assertEquals('3Uptown' . $masterId, $result);


        // slaveId: column in master record
385
        $this->dbArray[DB_INDEX_DEFAULT]->sql("UPDATE Person SET adrId=3 WHERE id=$masterId", ROW_IMPLODE_ALL);
386
387
388
389
390
391
//        $this->store->setVar('adrId', '3', STORE_RECORD);
        $feSpecAction[FE_NAME] = 'adrId';
        $feSpecAction[FE_SLAVE_ID] = '';

        $formAction->elements($masterId, [$feSpecAction], FE_TYPE_AFTER_SAVE);

392
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id, city, personId FROM Address WHERE personId=$masterId", ROW_IMPLODE_ALL);
393
        $this->assertEquals('3Uptown' . $masterId, $result);
394

395
        // Check updated primary record: person.adrId
396
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id, name, adrId FROM Person WHERE id=$masterId", ROW_IMPLODE_ALL);
397
398
399
400
401
402
403
        $this->assertEquals('2Smith3', $result);


        // Same situation as above, with sqlDelete.
        $feSpecAction[FE_SQL_DELETE] = "{{ DELETE FROM Address WHERE id={{slaveId}} }} ";
        $formAction->elements($masterId, [$feSpecAction], FE_TYPE_AFTER_SAVE);

404
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id, city, personId FROM Address WHERE personId=$masterId", ROW_IMPLODE_ALL);
405
406
407
        $this->assertEquals('', $result);

        // Check updated primary record: person.adrId
408
        $result = $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id, name, adrId FROM Person WHERE id=$masterId", ROW_IMPLODE_ALL);
409
        $this->assertEquals('2Smith0', $result);
410
411
    }

Carsten  Rose's avatar
Carsten Rose committed
412
413
414
415
416
417
    /**
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     * @throws UserReportException
     */
418
419
420
421
422
423
    protected function setUp() {

        $this->store = Store::getInstance('form=TestFormName', true);
        parent::setUp();

        $this->store->setVar('form', 'TestFormName', STORE_TYPO3);
424
        $this->executeSQLFile(__DIR__ . '/../database/fixtures/Generic.sql', true);
425
426
427
428

    }

}