/
Base.php
588 lines (531 loc) · 22.4 KB
/
Base.php
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
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
254
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
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
338
339
340
341
342
343
344
345
346
347
348
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
375
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
<?php
/**
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @license http://www.horde.org/licenses/gpl GPLv2
* @category Horde
* @package Horde_ActiveSync
* @subpackage UnitTests
*/
class Horde_ActiveSync_StateTest_Base extends Horde_Test_Case
{
protected static $state;
protected static $logger;
protected function _testGetDeviceInfo()
{
// First with no existing deivce.
$this->assertEquals(false, (boolean)self::$state->deviceExists('dev123', 'mike'));
// Can't use setExpectedException here since it stops the rest
// of the method from running when it's thrown.
try {
self::$state->loadDeviceInfo('dev123', 'mike');
$this->fail('Did not raise expected Horde_ActiveSync_Exception.');
} catch (Horde_ActiveSync_Exception $e) {
}
// Add the device, then retreive it.
$deviceInfo = new Horde_ActiveSync_Device(self::$state);
$deviceInfo->rwstatus = 0;
$deviceInfo->deviceType = 'Test Device';
$deviceInfo->userAgent = 'Horde Tests';
$deviceInfo->id = 'dev123';
$deviceInfo->user = 'mike';
$deviceInfo->policykey = 456;
$deviceInfo->supported = array();
$deviceInfo->save();
$this->assertEquals(true, (boolean)self::$state->deviceExists('dev123', 'mike'));
$di = self::$state->loadDeviceInfo('dev123', 'mike');
$this->assertEquals($deviceInfo, $di);
}
/**
* @return [type] [description]
*/
protected function _testListDevices()
{
$devices = self::$state->listDevices();
$this->assertCount(1, $devices);
$deviceInfo = new Horde_ActiveSync_Device(self::$state);
$deviceInfo->rwstatus = 0;
$deviceInfo->deviceType = 'Test Device';
$deviceInfo->userAgent = 'Horde Tests';
$deviceInfo->id = 'dev123';
$deviceInfo->user = 'ashley';
$deviceInfo->policykey = 123;
$deviceInfo->supported = array();
$deviceInfo->save();
$devices = self::$state->listDevices();
$this->assertCount(2, $devices);
}
protected function _testPolicyKeys()
{
$device = self::$state->loadDeviceInfo('dev123', 'mike');
$this->assertEquals(456, $device->policykey);
self::$state->setPolicyKey('dev123', 789);
// Make sure it took without affected other data
// (need to load a different device first, to clear
// the local copy of the data)
$device = self::$state->loadDeviceInfo('dev123', 'ashley');
$this->assertEquals(123, $device->policykey);
$device = self::$state->loadDeviceInfo('dev123', 'mike');
$this->assertEquals(789, $device->policykey);
self::$state->resetAllPolicyKeys();
$device = self::$state->loadDeviceInfo('dev123', 'ashley');
$this->assertEquals(0, $device->policykey);
$device = self::$state->loadDeviceInfo('dev123', 'mike');
$this->assertEquals(0, $device->policykey);
}
protected function _testDuplicatePIMAddition()
{
// @TODO. Need to implement getChanges/saveState tests.
}
/**
* Not much testing here yet, just run through a save and check for
* fatals.
*
*/
protected function _loadStateTest()
{
$collection = array(
'folderid' => '@Contacts@',
'class' => Horde_ActiveSync::CLASS_CONTACTS);
self::$state->loadState($collection, 0, Horde_ActiveSync::REQUEST_TYPE_SYNC, 'abcdef');
self::$state->setNewSyncKey('{51941e99-0b9c-41f8-b678-1532c0a8015f}1');
self::$state->save();
}
protected function _testCacheInitialState()
{
$cache = new Horde_ActiveSync_SyncCache(self::$state, 'dev123', 'mike', self::$logger->getLogger());
$this->assertEquals(array(), $cache->getCollections());
$this->assertEquals(array(), $cache->getCollections(true));
$this->assertEquals(0, $cache->countCollections());
$this->assertEquals(false, $cache->collectionExists('@Contacts@'));
$this->assertEquals(false, $cache->collectionIsPingable('@Contacts@'));
$this->assertEquals(false, $cache->collectionIsPingable('@Contacts@'));
$this->assertEquals(array(), $cache->getFolders());
$this->assertEquals(false, $cache->getFolder('@Contacts@'));
}
protected function _testCacheFolders()
{
$log = new Horde_Test_Log();
$cache = new Horde_ActiveSync_SyncCache(self::$state, 'dev123', 'mike', self::$logger->getLogger());
// First Fixture
$folder = new Horde_ActiveSync_Message_Folder((array('logger' => $log->getLogger(), 'protocolversion' => Horde_ActiveSync::VERSION_TWELVEONE)));
$folder->type = Horde_ActiveSync::FOLDER_TYPE_CONTACT;
$folder->serverid = '@Contacts@';
$folder->_serverid = '@Contacts@';
$cache->updateFolder($folder);
// Second fixture
$folder = new Horde_ActiveSync_Message_Folder((array('logger' => $log->getLogger(), 'protocolversion' => Horde_ActiveSync::VERSION_TWELVEONE)));
$folder->type = Horde_ActiveSync::FOLDER_TYPE_INBOX;
$folder->serverid = '519422f1-4c5c-4547-946a-1701c0a8015f';
$folder->_serverid = 'INBOX';
$cache->updateFolder($folder);
$expected = array(
'@Contacts@' => array(
'class' => 'Contacts',
'serverid' => '@Contacts@',
),
'519422f1-4c5c-4547-946a-1701c0a8015f' => array(
'class' => 'Email',
'serverid' => 'INBOX'
)
);
$this->assertEquals($expected, $cache->getFolders());
$expected = array(
'class' => 'Email',
'serverid' => 'INBOX'
);
$this->assertEquals($expected, $cache->getFolder('519422f1-4c5c-4547-946a-1701c0a8015f'));
$cache->save();
}
protected function _testCacheDataRestrictFields()
{
$cache_data = self::$state->getSyncCache('dev123', 'mike', array('folders'));
$this->assertCount(1, $cache_data);
list($key, $value) = each($cache_data);
$this->assertEquals('folders', $key);
}
protected function _testCacheFoldersPersistence()
{
$cache = new Horde_ActiveSync_SyncCache(self::$state, 'dev123', 'mike', self::$logger->getLogger());
$expected = array(
'@Contacts@' => array(
'class' => 'Contacts',
'serverid' => '@Contacts@',
),
'519422f1-4c5c-4547-946a-1701c0a8015f' => array(
'class' => 'Email',
'serverid' => 'INBOX'
)
);
$this->assertEquals($expected, $cache->getFolders());
$expected = array(
'class' => 'Email',
'serverid' => 'INBOX'
);
$this->assertEquals($expected, $cache->getFolder('519422f1-4c5c-4547-946a-1701c0a8015f'));
}
protected function _testCacheCollections()
{
$collections = array(
'519422f1-4c5c-4547-946a-1701c0a8015f' => array(
'class' => 'Email',
'windowsize' => 5,
'truncation' => 0,
'mimesupport' => 0,
'mimetruncation' => 8,
'conflict' => 1,
'bodyprefs' => array(
'wanted' => 2,
2 => array(
'type' => 2,
'truncationsize' => 200000)
),
'deletesasmoves' => 1,
'filtertype' => 5,
'id' => '519422f1-4c5c-4547-946a-1701c0a8015f',
'serverid' => 'INBOX'),
'@Contacts@' => array(
'class' => 'Contacts',
'windowsize' => 4,
'truncation' => 0,
'mimesupport' => 0,
'mimetruncation' => 8,
'conflict' => 1,
'bodyprefs' => array(
'wanted' => 1,
1 => array(
'type' => 1,
'truncationsize' => 200000)
),
'deletesasmoves' => 1,
'id' => '@Contacts@',
'serverid' => '@Contacts@')
);
$cache = new Horde_ActiveSync_SyncCache(self::$state, 'dev123', 'mike', self::$logger->getLogger());
foreach ($collections as $collection) {
$cache->addCollection($collection);
}
// Mangle the fixture to match what we expect.
$collections['519422f1-4c5c-4547-946a-1701c0a8015f']['rtftruncation'] = null;
$collections['@Contacts@']['rtftruncation'] = null;
$collections['@Contacts@']['filtertype'] = null;
$this->assertEquals(2, $cache->countCollections());
$this->assertEquals($collections, $cache->getCollections(false));
$this->assertEquals(array(), $cache->getCollections(true));
$this->assertEquals(true, $cache->collectionExists('@Contacts@'));
$this->assertEquals(true, $cache->collectionExists('519422f1-4c5c-4547-946a-1701c0a8015f'));
$this->assertEquals(false, $cache->collectionExists('foo'));
$this->assertEquals(false, $cache->collectionIsPingable('@Contacts@'));
$cache->setPingableCollection('@Contacts@');
$this->assertEquals(true, $cache->collectionIsPingable('@Contacts@'));
$cache->removePingableCollection('@Contacts@');
$this->assertEquals(false, $cache->collectionIsPingable('@Contacts@'));
$cache->updateCollection(
array('id' => '519422f1-4c5c-4547-946a-1701c0a8015f', 'newsynckey' => '{51941e99-0b9c-41f8-b678-1532c0a8015f}2'),
array('newsynckey' => true));
$cache->save();
// Now we should have a lastsynckey
$this->assertEquals(1, count($cache->getCollections()));
// And still have 2 if we don't need a synckey
$this->assertEquals(2, count($cache->getCollections(false)));
}
protected function _testLoadCollectionsFromCache()
{
$collections = $this->getCollectionHandler();
$collections->loadCollectionsFromCache();
$this->assertEquals(2, $collections->collectionCount());
}
protected function _testCollectionsFromCache()
{
$cache = new Horde_ActiveSync_SyncCache(self::$state, 'dev123', 'mike', self::$logger->getLogger());
$collections = array('519422f1-4c5c-4547-946a-1701c0a8015f' => array('id' => '519422f1-4c5c-4547-946a-1701c0a8015f'));
$expected = array('519422f1-4c5c-4547-946a-1701c0a8015f' => array(
'class' => 'Email',
'windowsize' => 5,
'truncation' => 0,
'mimesupport' => 0,
'mimetruncation' => 8,
'bodyprefs' => array(
'wanted' => 2,
2 => array(
'type' => 2,
'truncationsize' => 200000)
),
'filtertype' => 5,
'id' => '519422f1-4c5c-4547-946a-1701c0a8015f',
'serverid' => 'INBOX'));
$cache->validateCollectionsFromCache($collections);
$this->assertEquals($expected, $collections);
}
protected function _testCacheRefreshCollections()
{
$cache = new Horde_ActiveSync_SyncCache(self::$state, 'dev123', 'mike', self::$logger->getLogger());
$newcache = new Horde_ActiveSync_SyncCache(self::$state, 'dev123', 'mike', self::$logger->getLogger());
$newcache->updateCollection(
array('id' => '519422f1-4c5c-4547-946a-1701c0a8015f', 'newsynckey' => '{51941e99-0b9c-41f8-b678-1532c0a8015f}3'),
array('newsynckey' => true));
sleep(1);
$newcache->save();
$cache->refreshCollections();
$collection = $cache->getCollections();
$this->assertEquals($collection['519422f1-4c5c-4547-946a-1701c0a8015f']['lastsynckey'], '{51941e99-0b9c-41f8-b678-1532c0a8015f}3');
// Test timestamp
$this->assertEquals(false, $cache->validateCache());
}
protected function _testValidateAfterUpdateTimestamp()
{
$cache = new Horde_ActiveSync_SyncCache(self::$state, 'dev123', 'mike', self::$logger->getLogger());
$this->assertEquals(true, $cache->validateCache());
$cache->updateTimestamp();
$this->assertEquals(true, $cache->validateCache());
}
/**
*
*/
protected function _testCacheUniqueness()
{
$cache = new Horde_ActiveSync_SyncCache(self::$state, 'dev123', 'bob', self::$logger->getLogger());
$this->assertEquals(array(), $cache->getFolders());
$cache = new Horde_ActiveSync_SyncCache(self::$state, 'dev456', 'mike', self::$logger->getLogger());
$this->assertEquals(array(), $cache->getFolders());
}
protected function _testGetStateWithNoState()
{
self::$state->loadDeviceInfo('dev123');
self::$state->loadState(array(), 0, Horde_ActiveSync::REQUEST_TYPE_FOLDERSYNC);
}
protected function _testCollectionHandler()
{
$collections = $this->getCollectionHandler();
// Initial state
$this->assertEquals(0, $collections->collectionCount());
// No syncable collections either, even though we have a synccache, none
// of the collections have a 'synckey' and have not been loaded into
// collection handler.
$this->assertEquals(false, $collections->haveSyncableCollections(Horde_ActiveSync::VERSION_TWOFIVE));
$this->assertEquals(false, $collections->haveSyncableCollections(Horde_ActiveSync::VERSION_TWELVEONE));
$this->assertEquals(2, $collections->cachedCollectionCount());
// Now load the collections
$collections->loadCollectionsFromCache();
$this->assertEquals(2, $collections->collectionCount());
$this->assertEquals(true, $collections->haveSyncableCollections(Horde_ActiveSync::VERSION_TWOFIVE));
$this->assertEquals(true, $collections->haveSyncableCollections(Horde_ActiveSync::VERSION_TWELVEONE));
}
/**
* Tests the setup for a PARTIAL sync request.
*/
protected function _testPartialSyncWithChangedCollections()
{
$collections = $this->getCollectionHandler();
$collections->loadCollectionsFromCache();
// Now import a collection that IS different (which is the only reason
// to have imported colletions with PARTIAL).
$col = array(
'id' => '519422f1-4c5c-4547-946a-1701c0a8015f',
'windowsize' => 5,
'truncation' => 0,
'mimesupport' => 0,
'mimetruncation' => 8,
'conflict' => 1,
'bodyprefs' => array(
'wanted' => 2,
2 => array(
'type' => 2,
'truncationsize' => 100000)
),
'synckey' => '{51941e99-0b9c-41f8-b678-1532c0a8015f}3',
'deletesasmoves' => 1,
'filtertype' => 5,
);
$collections->addCollection($col);
$this->assertEquals(2, $collections->collectionCount());
$this->assertEquals(true, $collections->initPartialSync());
}
/**
* Tests the setup for a PARTIAL sync request.
*/
protected function _testPartialSyncWithUnchangedCollections()
{
// Pretend the heartbeat was not sent by the client.
$cache = new Horde_ActiveSync_SyncCache(self::$state, 'dev123', 'mike', self::$logger->getLogger());
$cache->hbinterval = false;
$cache->wait = false;
$cache->save();
$collections = $this->getCollectionHandler();
$collections->loadCollectionsFromCache();
// Should return false because we haven't loaded (from incoming xml) any
// collections yet, so no collections in handler are syncable.
$this->assertEquals(false, $collections->initPartialSync());
// Pretent to read a new collection in from xml.
// This one is identical to what we already have, so this should also
// fail.
$col = array(
'id' => '519422f1-4c5c-4547-946a-1701c0a8015f',
'windowsize' => 5,
'truncation' => 0,
'mimesupport' => 0,
'mimetruncation' => 8,
'conflict' => 1,
'bodyprefs' => array(
'wanted' => 2,
2 => array(
'type' => 2,
'truncationsize' => 200000)
),
'synckey' => '{517541cc-b188-478d-9e1a-fa49c0a8015f}3',
'deletesasmoves' => 1,
'filtertype' => 5,
);
$collections->addCollection($col);
$this->assertEquals(false, $collections->initPartialSync());
// Change the filtertype to simulate a new filtertype request from client.
// This should now return true.
$col['filtertype'] = 6;
$collections->addCollection($col);
$this->assertEquals(true, $collections->initPartialSync());
}
protected function _testPartialSyncWithOnlyChangedHbInterval()
{
$this->markTestSkipped('No idea why the cache does not load the collections here.');
$collections = $this->getCollectionHandler();
$collections->loadCollectionsFromCache();
$collections->setHeartbeat(array('hbinterval' => 1));
$result = $collections->initPartialSync();
$this->assertEquals(true, $result);
}
protected function _testEmptyResponse()
{
$cache = new Horde_ActiveSync_SyncCache(self::$state, 'dev123', 'mike', self::$logger->getLogger());
// Reset these from other tests.
$cache->hbinterval = 100;
$cache->save();
$collections = $this->getCollectionHandler();
$this->assertEquals(true, $collections->canSendEmptyResponse());
$collections->importedChanges = true;
$this->assertEquals(false, $collections->canSendEmptyResponse());
}
/**
* Tests initiating a partial sync where 1 collection was passed from
* client and 2 others had to be loaded from cache.
*/
protected function _testMissingCollections()
{
// Need to prime the cache with a synckey for contacts so we have
// another one to load for the test.
$col = array('id' => '@Contacts@', 'newsynckey' => '{517541cc-b188-478d-aaaa-fa49c0a8015f}35');
$cache = new Horde_ActiveSync_SyncCache(self::$state, 'dev123', 'mike', self::$logger->getLogger());
$cache->updateCollection($col, array('newsynckey' => true));
$cache->save();
$collections = $this->getCollectionHandler();
$col = array(
'id' => '519422f1-4c5c-4547-946a-1701c0a8015f',
'windowsize' => 5,
'truncation' => 0,
'mimesupport' => 0,
'mimetruncation' => 8,
'conflict' => 1,
'bodyprefs' => array(
'wanted' => 2,
2 => array(
'type' => 2,
'truncationsize' => 300000)
),
'synckey' => '{517541cc-b188-478d-9e1a-fa49c0a8015f}3',
'deletesasmoves' => 1,
'filtertype' => 5,
);
$collections->addCollection($col);
$collections->initPartialSync();
$this->assertEquals(1, $collections->collectionCount());
$collections->getMissingCollectionsFromCache();
$this->assertEquals(2, $collections->collectionCount());
}
/**
* Test detecting a change in a collection's filtertype.
*/
protected function _testChangingFilterType()
{
$collections = $this->getCollectionHandler();
$col = array(
'id' => '519422f1-4c5c-4547-946a-1701c0a8015f',
'windowsize' => 5,
'truncation' => 0,
'mimesupport' => 0,
'mimetruncation' => 8,
'conflict' => 1,
'bodyprefs' => array(
'wanted' => 2,
2 => array(
'type' => 2,
'truncationsize' => 200000)
),
'synckey' => '{517541cc-b188-478d-9e1a-fa49c0a8015f}96',
'deletesasmoves' => 1,
'filtertype' => 4,
);
$collections->addCollection($col);
$this->assertEquals(false, $collections->checkFilterType($col['id'], $col['filtertype']));
}
protected function _testGettingImapId()
{
$collections = $this->getCollectionHandler();
$this->assertEquals('INBOX', $collections->getBackendIdForFolderUid('519422f1-4c5c-4547-946a-1701c0a8015f'));
$this->assertEquals('@Contacts@', $collections->getBackendIdForFolderUid('@Contacts@'));
}
protected function _testHierarchy()
{
self::$state->setBackend($this->getMockDriver());
$collections = $this->getCollectionHandler(true);
$seen = $collections->initHierarchySync(0);
$this->assertEquals(array(), $seen);
$expected = array(
array(
'type' => 'change',
'flags' => 'NewMessage',
'id' => '@Tasks@',
'serverid' => '@Tasks@'
),
array(
'type' => 'change',
'flags' => 'NewMessage',
'id' => '@Notes@',
'serverid' => '@Notes@'
),
array(
'type' => 'change',
'flags' => 'NewMessage',
'id' => '@Contacts@',
'serverid' => '@Contacts@'
),
array(
'type' => 'change',
'flags' => 'NewMessage',
'id' => '@Calendar@',
'serverid' => '@Calendar@'
)
);
$changes = $collections->getHierarchyChanges();
$this->assertEquals($expected, $changes);
}
public function getMockDriver()
{
$connector = new Horde_ActiveSync_Driver_MockConnector();
$driver = new Horde_ActiveSync_Driver_Mock(array(
'connector' => $connector,
'auth' => false,
'imap' => false,
'state' => self::$state));
return $driver;
}
public function getCollectionHandler($addDevice = false)
{
$as = $this->getMockSkipConstructor('Horde_ActiveSync');
$as->logger = self::$logger->getLogger();
$as->state = self::$state;
if ($addDevice) {
$as->device = self::$state->loadDeviceInfo('dev123', 'mike');
}
$cache = new Horde_ActiveSync_SyncCache(self::$state, 'dev123', 'mike', self::$logger->getLogger());
$collections = new Horde_ActiveSync_Collections($cache, $as);
return $collections;
}
}