DirtyTest.php 35.2 KB
Newer Older
1
2
3
4
5
6
7
8
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 2/2/16
 * Time: 8:47 AM
 */

9
10
11
//namespace qfq;
//
//use PHPUnit\Framework\TestCase;
12
13
14
15
16
//use qfq;

require_once(__DIR__ . '/AbstractDatabaseTest.php');
require_once(__DIR__ . '/../../qfq/form/Dirty.php');
require_once(__DIR__ . '/../../qfq/store/Sip.php');
17
18
require_once(__DIR__ . '/../../qfq/store/Session.php');
require_once(__DIR__ . '/../../qfq/database/Database.php');
19

20
21
22
23
24
/*
 * Open to check
 * - FORM_DELETE
 */

25
26
27
/*
 * 1. DirtyMode: none
 * ===============
28
29
30
 * r=0, Alice lock, Alice releasefirst simple unit tests for recdord locking.-k
 * r>0, Alice lock, Alice release
 * Bob lock, Alice lock
31
32
33
 *
 * 2. DirtyMode: advisory
 * ===================
34
35
36
37
38
39
40
 * r=0, Alice lock, Alice release
 * r>0, Alice lock, Alice release
 * Alice lock, Alice timeout, Alice release
 * Alice release
 * Alice lock, Record modified by someone else, Alice release
 * Bob lock, Alice lock
 * Bob lock, Bob timeout, Alice lock
41
 * Alice lock 1, Alice lock 2, Bob lock 3, Bob release 3, Alice release 2, Alice release 1
42
 * Alice lock 1 Form A, Bob lock 1 Form B
43
44
45
 *
 * 3. DirtyMode: exclusive
 * ====================
46
47
48
49
50
51
52
 * r=0, Alice lock, Alice release
 * r>0, Alice lock, Alice release
 * Alice lock, Alice timeout, Alice release
 * Alice release
 * Alice lock, Record modified by someone else, Alice release
 * Bob lock, Alice lock
 * Bob lock, Bob timeout, Alice lock
53
 * Alice lock 1, Alice lock 2, Bob lock 3, Bob release 3, Alice release 2, Alice release 1
54
 * Alice lock 1 Form A, Bob lock 1 Form B
55
56
57
58
 *
 * 4. Mix Mode
 * ===========
 *
59
60
61
62
63
64
65
 * For all: Alice lock 1 Form A, Bob lock 1 Form B
 *   Form A: none, Form B: exclusive
 *   Form A: none, Form B: advisory
 *   Form A: advisory, Form B: none
 *   Form A: advisory, Form B: exclusive
 *   Form A: exclusive, Form B: none
 *   Form A: exclusive, Form B: advisory
66
67
 *
 *
68
69
 * 5. Special
 * ==========
70
 *
71
72
 * Compute expire time
 * Lock non existing record
73
74
 */

75
76
77
/**
 * Class DirtyTest
 */
78
79
80
class DirtyTest extends \AbstractDatabaseTest {

    /*
81
     * @var \qfq\Sip
82
83
84
85
     */
    protected $sip = null;

    /**
86
     * @var \qfq\Database instantiated class
87
     */
88
    protected $dbArray = null;
89

90
91
92
93
94
    /**********************************************************************
     * 1. DirtyMode: none
     */


95
    /**
96
97
98
99
     * r=0, Alice lock, Alice release
     */
    public function testNoneR0AliceLockRelease() {

100
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=0&form=lockNone", RETURN_SIP);
101
102
103

        // Alice lock
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
104
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
105
        $dirty = new \qfq\Dirty();
106
107
108
109

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

110
        $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id FROM Dirty", ROW_EXPECT_0);
111
112
113

        // Alice release
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
114
        $dirty = new \qfq\Dirty();
115
116
117
118

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

119
        $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id FROM Dirty", ROW_EXPECT_0);
120
121
    }

122
    /**
123
124
125
126
     * r=1, Alice lock, Alice release
     */
    public function testNoneR1AliceLockRelease() {

127
128
129
130
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockNone", RETURN_SIP);

        // Alice lock
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
131
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
132
133
134
135
136
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

137
        $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id FROM Dirty", ROW_EXPECT_0);
138
139
140

        // Alice release
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
141
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
142
143
144
145
146
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

147
        $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id FROM Dirty", ROW_EXPECT_0);
148
149
    }

150
151
152
153
154
155
156
157
158
159
160
161
162
163
    /**
     *  Alice lock, Record modified by someone else, Alice release
     */
    public function testNoneAliceLockBobLock() {

        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockNone", RETURN_SIP);

        // Alice lock - but change cookie later to Bob
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        // move lock to another owner (Alice fake becomes Bob) - but there is no lockrecord!
164
        $this->dbArray[DB_INDEX_DEFAULT]->sql("UPDATE Dirty SET qfqUserSessionCookie='SessionCookieBob' WHERE id=1", ROW_REGULAR);
165
166
167
168
169
170
171
172
173

        // Alice lock again
        $result = $dirty->process();

        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];

        $this->assertEquals($expected, $result);
    }

174
175
176
177
    /**********************************************************************
     * 1. DirtyMode: advisory
     */

178
    /**
179
180
181
182
183
184
185
186
     * r=0, Alice lock, Alice release
     */
    public function testAdvisoryR0AliceLockRelease() {

        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=0&form=lockAdvisory", RETURN_SIP);

        // Alice lock
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
187
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
188
189
190
191
192
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

193
        $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id FROM Dirty", ROW_EXPECT_0);
194
195
196
197
198
199
200
201

        // Alice release
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

202
        $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id FROM Dirty", ROW_EXPECT_0);
203
204
    }

205
    /**
206
207
208
209
210
211
212
213
     * r=1, Alice lock, Alice release
     */
    public function testAdvisoryR1AliceLockRelease() {

        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);

        // Alice lock
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
214
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
215
216
217
218
219
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

220
        $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id FROM Dirty", ROW_EXPECT_1);
221
222
223

        // Alice release
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
224
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
225
226
227
228
229
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

230
        $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id FROM Dirty", ROW_EXPECT_0);
231
232
    }

233
    /**
234
235
236
237
238
239
240
241
     *  * Alice lock, Alice timeout, Alice release
     */
    public function testAdvisoryAliceLockTimeoutRelease() {

        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);

        // Alice lock
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
242
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
243
244
245
246
247
248
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

        // Timeout
249
        $rc = $this->dbArray[DB_INDEX_DEFAULT]->sql("UPDATE Dirty SET expire='2001-01-01 00:00:00'", ROW_REGULAR);
250
251
252
253
        $this->assertEquals(1, $rc, 'Expect that exactly one record has been updated');

        // Alice release
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
254
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
255
256
257
258
259
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

260
        $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id FROM Dirty", ROW_EXPECT_0);
261
262
    }

263
264
265
266
267
268
269
270
271
272
273
274
    /**
     *  Alice lock, Record modified by someone else, Alice release
     */
    public function testAdvisoryAliceLockModifiedRelease() {

        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);

        // Alice lock
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
        $dirty = new \qfq\Dirty();

275
        $this->dbArray[DB_INDEX_DEFAULT]->sql("UPDATE Person SET modified='2001-01-01 13:14:15' WHERE id=1", ROW_REGULAR);
276
277
278
279
        $result = $dirty->process();
        $this->assertEquals($expected, $result);

        // change modified
280
        $this->dbArray[DB_INDEX_DEFAULT]->sql("UPDATE Person SET modified='2002-02-02 16:17:18' WHERE id=1", ROW_REGULAR);
281
282
283
284
285
286
287

        // Alice release
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

Carsten  Rose's avatar
Carsten Rose committed
288
289
290
        //TODO: this should be enabled again.
//        $expected = [API_STATUS => API_ANSWER_STATUS_CONFLICT, API_MESSAGE => 'The record has been modified in the meantime. Please reload the form, edit and save again.'];
//        $this->assertEquals($expected, $result);
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
    }

    /**
     *  Bob lock, Alice lock
     */
    public function testAdvisoryBobLockAliceLock() {

        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);

        // Alice lock - but change cookie later to Bob
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        // move lock to another owner (Alice fake becomes Bob)
307
        $this->dbArray[DB_INDEX_DEFAULT]->sql("UPDATE Dirty SET qfqUserSessionCookie='SessionCookieBob' WHERE id=1", ROW_REGULAR);
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337

        // Alice lock again
        $result = $dirty->process();

        $msg = 'The record has already';
        $expected = [API_STATUS => API_ANSWER_STATUS_CONFLICT_ALLOW_FORCE, API_MESSAGE => $msg];

        // cut IP, User and Timestamp
        $result[API_MESSAGE] = substr($result[API_MESSAGE], 0, strlen($msg));

        $this->assertEquals($expected, $result);
    }


    /**
     *  * Bob lock, Bob timeout, Alice lock
     */
    public function testAdvisoryBobLockTimeoutAliceLock() {

        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);

        // Alice fake (becomes Bob) lock
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

        // Set timeout to expired, Change Alice to Bob
338
        $rc = $this->dbArray[DB_INDEX_DEFAULT]->sql("UPDATE Dirty SET expire='2001-01-01 00:00:00', qfqUserSessionCookie='SessionCookieBob'", ROW_REGULAR);
339
340
341
342
343
344
345
346
347
348
        $this->assertEquals(1, $rc, 'Expect that exactly one record has been updated');

        // Alice lock
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);
    }

349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
    /**
     *   * Alice lock 1, Alice lock 2, Bob lock 3, Bob release 3, Alice release 2, Alice release 1
     */
    public function testAdvisoryAliceLock1AliceLock2BobLock3BobRelease3AliceRelease2AliceRelease1() {


        // Alice lock 1
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

        // Alice lock 2
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=2&form=lockAdvisory", RETURN_SIP);
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

        // Bob becomes logged in user
        $_COOKIE[SESSION_NAME] = 'SessionCookieBob';

        // Create third Person record
375
        $rc = $this->dbArray[DB_INDEX_DEFAULT]->sql("INSERT INTO Person (`name`, `firstName`) VALUES ('no', 'name')", ROW_REGULAR);
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437

        // Bob lock 3
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=3&form=lockAdvisory", RETURN_SIP);
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

        // RELEASE

        // Bob Release 3
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
        $this->assertEquals($expected, $result);

        $session = \qfq\Session::getInstance(true);
        $session::clearAll();

        // Alice Release 2
        $_COOKIE[SESSION_NAME] = 'SessionCookieAlice';
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;

        // with phpunit, we only have one sip at a time
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=2&form=lockExclusive", RETURN_SIP);
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
        $this->assertEquals($expected, $result);

        // Alice Release 1
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
        // with phpunit, we only have one sip at a time
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
        $this->assertEquals($expected, $result);
    }

    /**
     *  Alice lock 1 Form A, Bob lock 1 Form B
     */
    public function testAdvisoryAliceFormALock1BobFormBLock1() {

        // Create clean environment
        $this->clean('SessionCookieAlice');

        // Alice lock 1 Form A
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

Carsten  Rose's avatar
Carsten Rose committed
438
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
        $this->assertEquals($expected, $result);

        // Create clean environment
        $this->clean('SessionCookieBob');

        // Bob lock 1 Form B
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory2", RETURN_SIP);
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        $msg = 'The record has already';
        $expected = [API_STATUS => API_ANSWER_STATUS_CONFLICT_ALLOW_FORCE, API_MESSAGE => $msg];

        // cut IP, User and Timestamp
        $result[API_MESSAGE] = substr($result[API_MESSAGE], 0, strlen($msg));

        $this->assertEquals($expected, $result);
    }

460
461
462
463
    /**********************************************************************
     * 3. DirtyMode: exclusive
     */

464
    /**
465
466
467
468
469
470
471
472
     * r=0, Alice lock, Alice release
     */
    public function testExclusiveR0AliceLockRelease() {

        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=0&form=lockExclusive", RETURN_SIP);

        // Alice lock
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
473
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
474
475
476
477
478
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

479
        $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id FROM Dirty", ROW_EXPECT_0);
480
481
482
483
484
485
486
487

        // Alice release
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

488
        $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id FROM Dirty", ROW_EXPECT_0);
489
490
    }

491
    /**
492
493
494
495
     * r=1, Alice lock, Alice release
     */
    public function testExclusiveR1AliceLockRelease() {

496
497
498
        // Create clean environment
        $this->clean('SessionCookieAlice');

499
500
501
502
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);

        // Alice lock
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
503
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
504
        $dirty = new \qfq\Dirty();
505
506
507
508

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

509
        $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id FROM Dirty", ROW_EXPECT_1);
510
511
512

        // Alice release
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
513
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
514
        $dirty = new \qfq\Dirty();
515
516
517
518

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

519
        $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id FROM Dirty", ROW_EXPECT_0);
520
521
    }

522
    /**
523
524
525
     *  * Alice lock, Alice timeout, Alice release
     */
    public function testExclusiveAliceLockTimeoutRelease() {
526

527
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
528

529
530
        // Alice lock
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
531
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
532
533
534
535
536
537
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

        // Timeout
538
        $rc = $this->dbArray[DB_INDEX_DEFAULT]->sql("UPDATE Dirty SET expire='2001-01-01 00:00:00'", ROW_REGULAR);
539
540
541
542
        $this->assertEquals(1, $rc, 'Expect that exactly one record has been updated');

        // Alice release
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
543
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
544
545
546
547
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);
548

549
        $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT id FROM Dirty", ROW_EXPECT_0);
550
551
    }

552
553
554
    /**
     * @expectedException \qfq\UserFormException
     */
555
556
557
558
559
560
561
562
563
564
//    public function testExclusiveAliceReleaseException() {
//
//        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
//
//        // Alice release
//        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
//        $dirty = new \qfq\Dirty();
//
//        $dirty->process();
//    }
565
566

    /**
567
     *  Alice lock, Record modified by someone else, Alice release
568
     */
569
    public function testExclusiveAliceLockModifiedRelease() {
570

571
572
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);

573
574
        // Alice lock
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
575
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
576
577
        $dirty = new \qfq\Dirty();

578
        $this->dbArray[DB_INDEX_DEFAULT]->sql("UPDATE Person SET modified='2001-01-01 13:14:15' WHERE id=1", ROW_REGULAR);
579
580
581
        $result = $dirty->process();
        $this->assertEquals($expected, $result);

582
        // change modified
583
        $this->dbArray[DB_INDEX_DEFAULT]->sql("UPDATE Person SET modified='2002-02-02 16:17:18' WHERE id=1", ROW_REGULAR);
584

585
586
587
588
        // Alice release
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
        $dirty = new \qfq\Dirty();

589
        $result = $dirty->process();
590

Carsten  Rose's avatar
Carsten Rose committed
591
592
593
        //TODO: this should be enabled again.
//        $expected = [API_STATUS => API_ANSWER_STATUS_CONFLICT, API_MESSAGE => 'The record has been modified in the meantime. Please reload the form, edit and save again.'];
//        $this->assertEquals($expected, $result);
594
595
    }

596
597
    /**
     *  Bob lock, Alice lock
598
     */
599
    public function testExclusiveBobLockAliceLock() {
600
601
602

        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);

603
604
605
606
607
608
609
        // Alice lock - but change cookie later to Bob
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        // move lock to another owner (Alice fake becomes Bob)
610
        $this->dbArray[DB_INDEX_DEFAULT]->sql("UPDATE Dirty SET qfqUserSessionCookie='SessionCookieBob' WHERE id=1", ROW_REGULAR);
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633

        // Alice lock again
        $result = $dirty->process();

        $msg = 'The record has already';
        $expected = [API_STATUS => API_ANSWER_STATUS_CONFLICT, API_MESSAGE => $msg];

        // cut IP, User and Timestamp
        $result[API_MESSAGE] = substr($result[API_MESSAGE], 0, strlen($msg));

        $this->assertEquals($expected, $result);
    }

    /**
     *  * Bob lock, Bob timeout, Alice lock
     */
    public function testExclusiveBobLockTimeoutAliceLock() {

        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);

        // Alice fake (becomes Bob) lock
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
634
635
        $dirty = new \qfq\Dirty();

636
637
638
639
        $result = $dirty->process();
        $this->assertEquals($expected, $result);

        // Set timeout to expired, Change Alice to Bob
640
        $rc = $this->dbArray[DB_INDEX_DEFAULT]->sql("UPDATE Dirty SET expire='2001-01-01 00:00:00', qfqUserSessionCookie='SessionCookieBob'", ROW_REGULAR);
641
642
643
644
645
        $this->assertEquals(1, $rc, 'Expect that exactly one record has been updated');

        // Alice lock
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
        $dirty = new \qfq\Dirty();
646

647
648
        $result = $dirty->process();
        $this->assertEquals($expected, $result);
649
650
    }

651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
    /**
     *   * Alice lock 1, Alice lock 2, Bob lock 3, Bob release 3, Alice release 2, Alice release 1
     */
    public function testExclusiveAliceLock1AliceLock2BobLock3BobRelease3AliceRelease2AliceRelease1() {


        // Alice lock 1
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

        // Alice lock 2
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=2&form=lockExclusive", RETURN_SIP);
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

        // Bob becomes logged in user
        $_COOKIE[SESSION_NAME] = 'SessionCookieBob';

        // Create third Person record
677
        $rc = $this->dbArray[DB_INDEX_DEFAULT]->sql("INSERT INTO Person (`name`, `firstName`) VALUES ('no', 'name')", ROW_REGULAR);
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739

        // Bob lock 3
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=3&form=lockExclusive", RETURN_SIP);
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();
        $this->assertEquals($expected, $result);

        // RELEASE

        // Bob Release 3
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
        $this->assertEquals($expected, $result);

        $session = \qfq\Session::getInstance(true);
        $session::clearAll();

        // Alice Release 2
        $_COOKIE[SESSION_NAME] = 'SessionCookieAlice';
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;

        // with phpunit, we only have one sip at a time
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=2&form=lockExclusive", RETURN_SIP);
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
        $this->assertEquals($expected, $result);

        // Alice Release 1
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
        // with phpunit, we only have one sip at a time
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
        $this->assertEquals($expected, $result);
    }

    /**
     *  Alice lock 1 Form A, Bob lock 1 Form B
     */
    public function testExclusiveAliceFormALock1BobFormBLock1() {

        // Create clean environment
        $this->clean('SessionCookieAlice');

        // Alice lock 1 Form A
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

Carsten  Rose's avatar
Carsten Rose committed
740
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
        $this->assertEquals($expected, $result);

        // Create clean environment
        $this->clean('SessionCookieBob');

        // Bob lock 1 Form B
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive2", RETURN_SIP);
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        $msg = 'The record has already';
        $expected = [API_STATUS => API_ANSWER_STATUS_CONFLICT, API_MESSAGE => $msg];

        // cut IP, User and Timestamp
        $result[API_MESSAGE] = substr($result[API_MESSAGE], 0, strlen($msg));

        $this->assertEquals($expected, $result);
    }

    /**********************************************************************
     * 4. Mix Mode
     */

    /**
     * Form A: none, Form B: exclusive
     * Alice lock 1 Form A, Bob lock 1 Form B
     */
    public function testAliceLock1NoneBobLock1Exclusive() {

        // Create clean environment
        $this->clean('SessionCookieAlice');

        // Alice lock 1 Form A
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockNone", RETURN_SIP);
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

Carsten  Rose's avatar
Carsten Rose committed
782
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
        $this->assertEquals($expected, $result);

        // Create clean environment
        $this->clean('SessionCookieBob');

        // Bob lock 1 Form B
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
        $this->assertEquals($expected, $result);
    }

    /**
     * Form A: none, Form B: advisory
     * Alice lock 1 Form A, Bob lock 1 Form B
     */
    public function testAliceLock1NoneBobLock1Advisory() {

        // Create clean environment
        $this->clean('SessionCookieAlice');

        // Alice lock 1 Form A
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockNone", RETURN_SIP);
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

Carsten  Rose's avatar
Carsten Rose committed
814
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
        $this->assertEquals($expected, $result);

        // Create clean environment
        $this->clean('SessionCookieBob');

        // Bob lock 1 Form B
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
        $this->assertEquals($expected, $result);
    }

    /**
     * Form A: advisory, Form B: none
     * Alice lock 1 Form A, Bob lock 1 Form B
     */
    public function testAliceLock1AdvisoryBobLock1None() {

        // Create clean environment
        $this->clean('SessionCookieAlice');

        // Alice lock 1 Form A
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

Carsten  Rose's avatar
Carsten Rose committed
846
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
        $this->assertEquals($expected, $result);

        // Create clean environment
        $this->clean('SessionCookieBob');

        // Bob lock 1 Form B
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockNone", RETURN_SIP);
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        $msg = 'The record has already';
        $expected = [API_STATUS => API_ANSWER_STATUS_CONFLICT_ALLOW_FORCE, API_MESSAGE => $msg];

        // cut IP, User and Timestamp
        $result[API_MESSAGE] = substr($result[API_MESSAGE], 0, strlen($msg));

        $this->assertEquals($expected, $result);
    }

    /**
     * Form A: advisory, Form B: Exclusive
     * Alice lock 1 Form A, Bob lock 1 Form B
     */
    public function testAliceLock1AdvisoryBobLock1Exclusive() {

        // Create clean environment
        $this->clean('SessionCookieAlice');

        // Alice lock 1 Form A
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

Carsten  Rose's avatar
Carsten Rose committed
883
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
        $this->assertEquals($expected, $result);

        // Create clean environment
        $this->clean('SessionCookieBob');

        // Bob lock 1 Form B
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        $msg = 'The record has already';
        $expected = [API_STATUS => API_ANSWER_STATUS_CONFLICT, API_MESSAGE => $msg];

        // cut IP, User and Timestamp
        $result[API_MESSAGE] = substr($result[API_MESSAGE], 0, strlen($msg));

        $this->assertEquals($expected, $result);
    }

    /**
     * Form A: exclusive, Form B: none
     * Alice lock 1 Form A, Bob lock 1 Form B
     */
    public function testAliceLock1ExclusiveBobLock1None() {

        // Create clean environment
        $this->clean('SessionCookieAlice');

        // Alice lock 1 Form A
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

Carsten  Rose's avatar
Carsten Rose committed
920
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
        $this->assertEquals($expected, $result);

        // Create clean environment
        $this->clean('SessionCookieBob');

        // Bob lock 1 Form B
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockNone", RETURN_SIP);
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        $msg = 'The record has already';
        $expected = [API_STATUS => API_ANSWER_STATUS_CONFLICT, API_MESSAGE => $msg];

        // cut IP, User and Timestamp
        $result[API_MESSAGE] = substr($result[API_MESSAGE], 0, strlen($msg));

        $this->assertEquals($expected, $result);
    }

    /**
     * Form A: exclusive, Form B: Advisory
     * Alice lock 1 Form A, Bob lock 1 Form B
     */
    public function testAliceLock1ExclusiveBobLock1Advisory() {

        // Create clean environment
        $this->clean('SessionCookieAlice');

        // Alice lock 1 Form A
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

Carsten  Rose's avatar
Carsten Rose committed
957
        $expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
        $this->assertEquals($expected, $result);

        // Create clean environment
        $this->clean('SessionCookieBob');

        // Bob lock 1 Form B
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        $msg = 'The record has already';
        $expected = [API_STATUS => API_ANSWER_STATUS_CONFLICT, API_MESSAGE => $msg];

        // cut IP, User and Timestamp
        $result[API_MESSAGE] = substr($result[API_MESSAGE], 0, strlen($msg));

        $this->assertEquals($expected, $result);
    }

978
    /****************************************************
979
     * 5. Special
980
981
     */

982
    /**
983
984
985
986
     *  Compute expire time
     */
    public function testExpireTime() {

987
        $this->dbArray[DB_INDEX_DEFAULT]->sql("DELETE FROM Dirty", ROW_REGULAR);
988
        $_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
989

990
991
992
993
994
995
996
        // Alice lock
        $_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
        $dirty = new \qfq\Dirty();

        $result = $dirty->process();

        // Check that the expire time is correctly computed
997
        $row = $this->dbArray[DB_INDEX_DEFAULT]->sql("SELECT expire FROM Dirty", ROW_EXPECT_1);
998
999
        $secs = abs(strtotime($row[DIRTY_EXPIRE]) - strtotime(date('Y-m-d H:i:s')) - 900);
        $this->assertLessThan(2, $secs);
1000
    }