FormActionTest.php 12.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
185
186
187
188
189
190
191
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/15/16
 * Time: 8:24 AM
 */

namespace qfq;

require_once(__DIR__ . '/AbstractDatabaseTest.php');
require_once(__DIR__ . '/../../qfq/form/FormAction.php');

class FormActionTest extends \AbstractDatabaseTest {

    public function testBeforeLoad() {
        $formSpec[F_TABLE_NAME] = 'Person';
        $formAction = new FormAction($formSpec, $this->db, true);

        // 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);
    }

    /**
     * Expect 0 recrod, but get 1
     * @expectedException \qfq\UserFormException
     **/
    public function testBeforeLoadException1() {
        $formSpec[F_TABLE_NAME] = 'Person';
        $formAction = new FormAction($formSpec, $this->db, true);

        $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
     **/
    public function testBeforeLoadException0() {
        $formSpec[F_TABLE_NAME] = 'Person';
        $formAction = new FormAction($formSpec, $this->db, true);

        $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
     **/
    public function testBeforeLoadException2() {
        $formSpec[F_TABLE_NAME] = 'Person';
        $formAction = new FormAction($formSpec, $this->db, true);

        $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
     **/
    public function testBeforeLoadException3() {
        $formSpec[F_TABLE_NAME] = 'Person';
        $formAction = new FormAction($formSpec, $this->db, 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);
    }

    /**
     * Necessary FE is empty > don't process check
     **/
    public function testBeforeLoadException4() {
        $formSpec[F_TABLE_NAME] = 'Person';
        $formAction = new FormAction($formSpec, $this->db, 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';

        // 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
     **/
    public function testBeforeLoadException5() {
        $formSpec[F_TABLE_NAME] = 'Person';
        $formAction = new FormAction($formSpec, $this->db, true);

        $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
     **/
    public function testBeforeLoad2() {
        $formSpec[F_TABLE_NAME] = 'Person';
        $formAction = new FormAction($formSpec, $this->db, true);

        $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
     **/
    public function testBeforeLoadException6() {
        $formSpec[F_TABLE_NAME] = 'Person';
        $formAction = new FormAction($formSpec, $this->db, true);

        $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
     **/
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
    public function testInsert() {

        $formSpec[F_TABLE_NAME] = 'Person';
        $formAction = new FormAction($formSpec, $this->db, true);

        $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';
        $result = $this->db->sql('TRUNCATE Address');
        $formAction->elements(2, [$feSpecAction], FE_TYPE_AFTER_SAVE);

        $result = $this->db->sql('SELECT id, city, personId FROM Address', ROW_IMPLODE_ALL);
        $this->assertEquals('1Downtown2', $result);


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

        $result = $this->db->sql('SELECT id, city, personId FROM Address', ROW_IMPLODE_ALL);
        $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] = '';

        $result = $this->db->sql('TRUNCATE Address');
        $formAction->elements(2, [$feSpecAction], FE_TYPE_AFTER_SAVE);

        // get the new slave record
        $result = $this->db->sql('SELECT id, city, personId FROM Address', ROW_IMPLODE_ALL);
        $this->assertEquals('1Downtown2', $result);
        // get the updated id in the master record
        $result = $this->db->sql('SELECT id, name, adrId FROM Person WHERE id=2', ROW_IMPLODE_ALL);
        $this->assertEquals('2Smith1', $result);
    }

    /**
     * Process UPDATE
     **/
    public function testUpdate() {

        $formSpec[F_TABLE_NAME] = 'Person';
        $formAction = new FormAction($formSpec, $this->db, true);

        $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}} }} ";
254
//        $feSpecAction[FE_SQL_DELETE] = "{{ DELETE FROM Address WHERE personId={{r}} AND id=0 }} ";
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288

        $result = $this->db->sql("TRUNCATE Address");
        $result = $this->db->sql("INSERT INTO Address (city, personId) VALUES ('Downtown1', 1)");
        $result = $this->db->sql("INSERT INTO Address (city, personId) VALUES ('Downtown2', 1)");
        $result = $this->db->sql("INSERT INTO Address (city, personId) VALUES ('Downtown3', $masterId)");

        $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);

        $result = $this->db->sql("SELECT id, city, personId FROM Address WHERE personId=$masterId", ROW_IMPLODE_ALL);
        $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);

        $result = $this->db->sql("SELECT id, city, personId FROM Address WHERE personId=$masterId", ROW_IMPLODE_ALL);
        $this->assertEquals('3Uptown' . $masterId, $result);


        // slaveId: column in master record
        $this->db->sql("UPDATE Person SET adrId=3 WHERE id=$masterId", ROW_IMPLODE_ALL);
//        $this->store->setVar('adrId', '3', STORE_RECORD);
        $feSpecAction[FE_NAME] = 'adrId';
        $feSpecAction[FE_SLAVE_ID] = '';

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

        $result = $this->db->sql("SELECT id, city, personId FROM Address WHERE personId=$masterId", ROW_IMPLODE_ALL);
        $this->assertEquals('3Uptown' . $masterId, $result);
289

290
        $result = $this->db->sql("SELECT id, name, adrId FROM Person WHERE id=$masterId", ROW_IMPLODE_ALL);
291
        $this->assertEquals('2Smith0', $result);
292
293
294
    }


295
296
297
298
299
300
301
302
303
304
305
306

    protected function setUp() {

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

        $this->store->setVar('form', 'TestFormName', STORE_TYPO3);
        $this->executeSQLFile(__DIR__ . '/fixtures/Generic.sql', true);

    }

}