~ubuntu-branches/ubuntu/maverick/gallery2/maverick

« back to all changes in this revision

Viewing changes to modules/core/test/phpunit/ItemAttributesTest.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2007-09-10 20:22:19 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070910202219-0jsuntvqge4ade6b
Tags: 2.2.3-2
Add Slovak translation of Debconf templates.  (Thanks to 
Ivan Masá.  Closes: #441671)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Gallery - a web based photo album viewer and editor
4
 
 * Copyright (C) 2000-2007 Bharat Mediratta
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or (at
9
 
 * your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful, but
12
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
 * General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
19
 
 */
20
 
 
21
 
/**
22
 
 * Test ItemAttribute functionality.
23
 
 * @package GalleryCore
24
 
 * @subpackage PHPUnit
25
 
 * @author Bharat Mediratta <bharat@menalto.com>
26
 
 * @version $Revision: 15513 $
27
 
 */
28
 
class ItemAttributesTest extends GalleryTestCase {
29
 
 
30
 
    function ItemAttributesTest($methodName) {
31
 
        $this->GalleryTestCase($methodName);
32
 
    }
33
 
 
34
 
    function setUp() {
35
 
        global $gallery;
36
 
        parent::setUp();
37
 
 
38
 
        $this->_saveSession = $gallery->_session;
39
 
 
40
 
        /* Clear response headers */
41
 
        $headers =& GalleryUtilities::_getResponseHeaders();
42
 
        $headers = array();
43
 
 
44
 
        list ($ret, $this->_album) = $this->_createRandomAlbum(
45
 
            $this->_getRootId(), array('orderBy' => 'id', 'orderDirection' => ORDER_ASCENDING));
46
 
        if ($ret) {
47
 
            return $this->failWithStatus($ret);
48
 
        }
49
 
        $this->_markForCleanup($this->_album);
50
 
 
51
 
        /*
52
 
         * The album inherits the permissions from the root album.  Thus, normalize the permissions
53
 
         * for the tests here.  Set it to core.view permission for everybody.
54
 
         */
55
 
        $ret = GalleryCoreApi::removeItemPermissions($this->_album->getId());
56
 
        if ($ret) {
57
 
            return $this->failWithStatus($ret);
58
 
        }
59
 
        list ($ret, $everybodyGroupId) =
60
 
            GalleryCoreApi::getPluginParameter('module', 'core', 'id.everybodyGroup');
61
 
        if ($ret) {
62
 
            return $this->failWithStatus($ret);
63
 
        }
64
 
        $ret = GalleryCoreApi::addGroupPermission(
65
 
            $this->_album->getId(), $everybodyGroupId, 'core.view', true);
66
 
        if ($ret) {
67
 
            return $this->failWithStatus($ret);
68
 
        }
69
 
        /* Verify the permissions */
70
 
        list ($ret, $compressedPerms) =
71
 
            GalleryCoreApi::fetchAllPermissionsForItem($this->_album->getId(), true);
72
 
        if ($ret) {
73
 
            return $this->failWithStatus($ret);
74
 
        }
75
 
        $this->assertEquals(array(array('groupId' => $everybodyGroupId,
76
 
                                        'permission' => 'core.view')), $compressedPerms,
77
 
                            'Failed to define initial permissions');
78
 
 
79
 
        /* Create additional test items */
80
 
        for ($i = 0; $i < 3; $i++) {
81
 
            list ($ret, $this->_items[$i]) =
82
 
                $this->_createRandomAlbum($this->_album->getId());
83
 
            if ($ret) {
84
 
                return $this->failWithStatus($ret);
85
 
            }
86
 
        }
87
 
    }
88
 
 
89
 
    function tearDown() {
90
 
        global $gallery;
91
 
        $gallery->_session = $this->_saveSession;
92
 
 
93
 
        parent::tearDown();
94
 
    }
95
 
 
96
 
    /**
97
 
     * Update the view count and verify it.
98
 
     */
99
 
    function testSetItemViewCount() {
100
 
        $ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 999);
101
 
        if ($ret) {
102
 
            return $this->failWithStatus($ret);
103
 
        }
104
 
 
105
 
        list ($ret, $viewCount) =
106
 
            GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
107
 
        if ($ret) {
108
 
            return $this->failWithStatus($ret);
109
 
        }
110
 
 
111
 
        $this->assertEquals(999, $viewCount);
112
 
    }
113
 
 
114
 
    /**
115
 
     * Update the view count and verify it.
116
 
     */
117
 
    function testIncrementItemViewCount() {
118
 
        global $gallery;
119
 
        $gallery->_phpVm = new ItemAttributesTestPhpVm();
120
 
        $gallery->_session = new ItemAttributesTestSession();
121
 
 
122
 
        $ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 0);
123
 
        if ($ret) {
124
 
            return $this->failWithStatus($ret);
125
 
        }
126
 
 
127
 
        $ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId(), 3);
128
 
        if ($ret) {
129
 
            return $this->failWithStatus($ret);
130
 
        }
131
 
 
132
 
        list ($ret, $viewCount) =
133
 
            GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
134
 
        if ($ret) {
135
 
            return $this->failWithStatus($ret);
136
 
        }
137
 
 
138
 
        $this->assertEquals(3, $viewCount);
139
 
        $this->assertEquals(array(), GalleryUtilities::_getResponseHeaders(),
140
 
                      'No headers should be sent for logged in users');
141
 
    }
142
 
 
143
 
    /**
144
 
     * Verify that we don't increment the view count until a day after the last increment within
145
 
     * the same session.
146
 
     */
147
 
    function testDontIncrementItemViewCountTooOften() {
148
 
        global $gallery;
149
 
        $gallery->_phpVm = new ItemAttributesTestPhpVm();
150
 
        $gallery->_session = new ItemAttributesTestSession();
151
 
 
152
 
        $ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 0);
153
 
        if ($ret) {
154
 
            return $this->failWithStatus($ret);
155
 
        }
156
 
 
157
 
        for ($i = 0; $i < 8; $i++) {
158
 
            $ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId(), 1);
159
 
            if ($ret) {
160
 
                return $this->failWithStatus($ret);
161
 
            }
162
 
        }
163
 
 
164
 
        list ($ret, $viewCount) =
165
 
            GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
166
 
        if ($ret) {
167
 
            return $this->failWithStatus($ret);
168
 
        }
169
 
        $this->assertEquals(1, $viewCount);
170
 
    }
171
 
 
172
 
    /**
173
 
     * Verify that we don't increment the view count until a day after the last increment within
174
 
     * the same session.
175
 
     */
176
 
    function testIncrementItemViewCountThreshold() {
177
 
        global $gallery;
178
 
        $gallery->_phpVm = new ItemAttributesTestPhpVm();
179
 
        $gallery->_session = new ItemAttributesTestSession();
180
 
 
181
 
        $session =& $gallery->_session;
182
 
 
183
 
        $ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 0);
184
 
        if ($ret) {
185
 
            return $this->failWithStatus($ret);
186
 
        }
187
 
 
188
 
        $ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId(), 1);
189
 
        if ($ret) {
190
 
            return $this->failWithStatus($ret);
191
 
        }
192
 
 
193
 
        /* Backdate our last viewed time by *almost* one day */
194
 
        $lastViewed =& $session->get('core.lastViewed');
195
 
        $lastViewed[$this->_items[0]->getId()] -= 86398;
196
 
 
197
 
        $ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId(), 1);
198
 
        if ($ret) {
199
 
            return $this->failWithStatus($ret);
200
 
        }
201
 
 
202
 
        list ($ret, $viewCount) =
203
 
            GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
204
 
        if ($ret) {
205
 
            return $this->failWithStatus($ret);
206
 
        }
207
 
 
208
 
        /* No change; a full day has not elapsed */
209
 
        $this->assertEquals(1, $viewCount);
210
 
 
211
 
        /* Backdate it further over the day line */
212
 
        $lastViewed =& $session->get('core.lastViewed');
213
 
        $lastViewed[$this->_items[0]->getId()] -= 4;
214
 
 
215
 
        $ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId(), 1);
216
 
        if ($ret) {
217
 
            return $this->failWithStatus($ret);
218
 
        }
219
 
 
220
 
        list ($ret, $viewCount) =
221
 
            GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
222
 
        if ($ret) {
223
 
            return $this->failWithStatus($ret);
224
 
        }
225
 
 
226
 
        /* Now it changes */
227
 
        $this->assertEquals(2, $viewCount);
228
 
    }
229
 
 
230
 
    function testIncrementItemViewCountGuestNoSessionIfModifiedNotSetIsSearchEngine() {
231
 
        global $gallery;
232
 
        $gallery->_phpVm = new ItemAttributesTestPhpVm(false, array(), time());
233
 
        $gallery->_session = new ItemAttributesTestSession(false, true);
234
 
 
235
 
        $this->_becomeGuestUser();
236
 
        unset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
237
 
 
238
 
        $ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 0);
239
 
        if ($ret) {
240
 
            return $this->failWithStatus($ret);
241
 
        }
242
 
 
243
 
        $ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId());
244
 
        if ($ret) {
245
 
            return $this->failWithStatus($ret);
246
 
        }
247
 
 
248
 
        list ($ret, $viewCount) =
249
 
            GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
250
 
        if ($ret) {
251
 
            return $this->failWithStatus($ret);
252
 
        }
253
 
        $this->assertEquals(0, $viewCount, 'first attempt, search engine -> do not increment');
254
 
        /* It only sets the header if it actually increments */
255
 
        $this->assertEquals(array(), GalleryUtilities::_getResponseHeaders());
256
 
    }
257
 
 
258
 
    function testIncrementItemViewCountGuestNoSessionIfModifiedNotSetIsNotSearchEngine() {
259
 
        global $gallery;
260
 
        $now = time();
261
 
        $gallery->_phpVm = new ItemAttributesTestPhpVm(false, array(), $now);
262
 
        $gallery->_session = new ItemAttributesTestSession(false, false);
263
 
 
264
 
        $this->_becomeGuestUser();
265
 
        unset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
266
 
 
267
 
        $ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 0);
268
 
        if ($ret) {
269
 
            return $this->failWithStatus($ret);
270
 
        }
271
 
 
272
 
        $ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId());
273
 
        if ($ret) {
274
 
            return $this->failWithStatus($ret);
275
 
        }
276
 
 
277
 
        list ($ret, $viewCount) =
278
 
            GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
279
 
        if ($ret) {
280
 
            return $this->failWithStatus($ret);
281
 
        }
282
 
        $this->assertEquals(1, $viewCount, 'First attempt, not search engine -> increment');
283
 
        $this->assertEquals(
284
 
            array('last-modified' => 'Last-Modified: ' . GalleryUtilities::getHttpDate($now),
285
 
                  'expires' => 'Expires: ' . GalleryUtilities::getHttpDate($now - 3600 * 24 * 7)),
286
 
            GalleryUtilities::_getResponseHeaders());
287
 
    }
288
 
 
289
 
    function testIncrementItemViewCountGuestIfModifiedIsSet() {
290
 
        global $gallery;
291
 
        $gallery->_phpVm = new ItemAttributesTestPhpVm();
292
 
        $gallery->_session = new ItemAttributesTestSession(false);
293
 
 
294
 
        $this->_becomeGuestUser();
295
 
        $_SERVER['HTTP_IF_MODIFIED_SINCE'] = 'something';
296
 
 
297
 
        $ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 0);
298
 
        if ($ret) {
299
 
            return $this->failWithStatus($ret);
300
 
        }
301
 
 
302
 
        $ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId());
303
 
        if ($ret) {
304
 
            return $this->failWithStatus($ret);
305
 
        }
306
 
 
307
 
        list ($ret, $viewCount) =
308
 
            GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
309
 
        if ($ret) {
310
 
            return $this->failWithStatus($ret);
311
 
        }
312
 
        $this->assertEquals(0, $viewCount);
313
 
    }
314
 
 
315
 
    function testIncrementItemViewCountGuestIfModifiedIsSet2() {
316
 
        global $gallery;
317
 
        $gallery->_phpVm = new ItemAttributesTestPhpVm(true,
318
 
            array('If-Modified-Since' => 'something'));
319
 
        $gallery->_session = new ItemAttributesTestSession(false);
320
 
 
321
 
        $this->_becomeGuestUser();
322
 
        unset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
323
 
 
324
 
        $ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 0);
325
 
        if ($ret) {
326
 
            return $this->failWithStatus($ret);
327
 
        }
328
 
 
329
 
        $ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId());
330
 
        if ($ret) {
331
 
            return $this->failWithStatus($ret);
332
 
        }
333
 
 
334
 
        list ($ret, $viewCount) =
335
 
            GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
336
 
        if ($ret) {
337
 
            return $this->failWithStatus($ret);
338
 
        }
339
 
        $this->assertEquals(0, $viewCount);
340
 
    }
341
 
 
342
 
    function testIncrementItemViewCountGuestIfModifiedIsSetNotGuest() {
343
 
        global $gallery;
344
 
        $gallery->_phpVm = new ItemAttributesTestPhpVm(true,
345
 
            array('If-Modified-Since' => 'something'));
346
 
        $gallery->_session = new ItemAttributesTestSession();
347
 
 
348
 
        $asdminUserId = $gallery->getActiveUserId();
349
 
        $_SERVER['HTTP_IF_MODIFIED_SINCE'] = 'foobar';
350
 
 
351
 
        $ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 0);
352
 
        if ($ret) {
353
 
            return $this->failWithStatus($ret);
354
 
        }
355
 
 
356
 
        $this->assertEquals($asdminUserId, $gallery->getActiveUserId(), 'admin user id');
357
 
 
358
 
        $ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId());
359
 
        if ($ret) {
360
 
            return $this->failWithStatus($ret);
361
 
        }
362
 
 
363
 
        list ($ret, $viewCount) =
364
 
            GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
365
 
        if ($ret) {
366
 
            return $this->failWithStatus($ret);
367
 
        }
368
 
        $this->assertEquals(1, $viewCount, 'View count');
369
 
    }
370
 
 
371
 
    /**
372
 
     * Update the view count and verify it
373
 
     */
374
 
    function testSetOrderWeight() {
375
 
        $ret = GalleryCoreApi::setItemOrderWeight($this->_items[0]->getId(), 123);
376
 
        if ($ret) {
377
 
            return $this->failWithStatus($ret);
378
 
        }
379
 
 
380
 
        list ($ret, $orderWeight) =
381
 
            GalleryCoreApi::fetchItemOrderWeight($this->_items[0]->getId());
382
 
        if ($ret) {
383
 
            return $this->failWithStatus($ret);
384
 
        }
385
 
 
386
 
        $this->assertEquals(123, $orderWeight);
387
 
    }
388
 
 
389
 
    /**
390
 
     * Set the weights to something sequential then verify that we can find the next
391
 
     * higher or lower weight.
392
 
     */
393
 
    function testFetchNextItemWeight() {
394
 
        for ($i = 0; $i < sizeof($this->_items); $i++) {
395
 
            $ret = GalleryCoreApi::setItemOrderWeight($this->_items[$i]->getId(), $i);
396
 
            if ($ret) {
397
 
                return $this->failWithStatus($ret);
398
 
            }
399
 
        }
400
 
 
401
 
        list ($ret, $higher) =
402
 
            GalleryCoreApi::fetchNextItemWeight($this->_items[1]->getId(), HIGHER_WEIGHT);
403
 
        if ($ret) {
404
 
            return $this->failWithStatus($ret);
405
 
        }
406
 
        $this->assertEquals($higher, 2);
407
 
 
408
 
        list ($ret, $lower) =
409
 
            GalleryCoreApi::fetchNextItemWeight($this->_items[1]->getId(), LOWER_WEIGHT);
410
 
        if ($ret) {
411
 
            return $this->failWithStatus($ret);
412
 
        }
413
 
        $this->assertEquals($lower, 0);
414
 
    }
415
 
 
416
 
    /**
417
 
     * Fetch the min/max child weight.
418
 
     */
419
 
    function textFetchExtremeChildWeight() {
420
 
        $this->assert(false);
421
 
 
422
 
        for ($i = 0; $i < sizeof($this->_items); $i++) {
423
 
            $ret = GalleryCoreApi::setItemOrderWeight($this->_items[$i]->getId(), 100+$i);
424
 
            if ($ret) {
425
 
                return $this->failWithStatus($ret);
426
 
            }
427
 
        }
428
 
 
429
 
        list ($ret, $max) =
430
 
            GalleryCoreApi::fetchMaxChildWeight($this->_album->getId(), HIGHER_WEIGHT);
431
 
        if ($ret) {
432
 
            return $this->failWithStatus($ret);
433
 
        }
434
 
        $this->assertEquals($max, 102);
435
 
 
436
 
        list ($ret, $min) =
437
 
            GalleryCoreApi::fetchMaxChildWeight($this->_album->getId(), LOWER_WEIGHT);
438
 
        if ($ret) {
439
 
            return $this->failWithStatus($ret);
440
 
        }
441
 
        $this->assertEquals($max, 100);
442
 
    }
443
 
 
444
 
    /**
445
 
     * Update the view count and verify it.
446
 
     */
447
 
    function testRebalanceOrderWeights() {
448
 
        for ($i = 0; $i < sizeof($this->_items); $i++) {
449
 
            $ret = GalleryCoreApi::setItemOrderWeight($this->_items[$i]->getId(), $i);
450
 
            if ($ret) {
451
 
                return $this->failWithStatus($ret);
452
 
            }
453
 
        }
454
 
        $ret = GalleryCoreApi::rebalanceChildOrderWeights($this->_album->getId(), 100);
455
 
        if ($ret) {
456
 
            return $this->failWithStatus($ret);
457
 
        }
458
 
 
459
 
        $count = sizeof($this->_items);
460
 
        for ($i = 0; $i < $count; $i++) {
461
 
            list ($ret, $weight) =
462
 
                GalleryCoreApi::fetchItemOrderWeight($this->_items[$i]->getId());
463
 
            if ($ret) {
464
 
                return $this->failWithStatus($ret);
465
 
            }
466
 
            $this->assertEquals(($i+1)*100, $weight,
467
 
                                sprintf('item %d of %d (id: %d) is not balanced correctly',
468
 
                                        $i, $count, $this->_items[$i]->getId()));
469
 
        }
470
 
    }
471
 
 
472
 
    function testFetchParentSequence() {
473
 
        global $gallery;
474
 
 
475
 
        list ($ret, $album) = $this->_createRandomAlbum($this->_items[1]->getId());
476
 
        if ($ret) {
477
 
            return $this->failWithStatus($ret);
478
 
        }
479
 
 
480
 
        list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($album->getId());
481
 
        $this->assertEquals(array($this->_getRootId(),
482
 
                                  $this->_album->getId(),
483
 
                                  $this->_items[1]->getId()),
484
 
                            $parentSequence,
485
 
                            'album parent sequence');
486
 
 
487
 
        list ($ret, $item) = $this->_createRandomDataItem($album->getId());
488
 
        if ($ret) {
489
 
            return $this->failWithStatus($ret);
490
 
        }
491
 
 
492
 
        list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($item->getId());
493
 
        if ($ret) {
494
 
            return $this->failWithStatus($ret);
495
 
        }
496
 
        $this->assertEquals(array($this->_getRootId(),
497
 
                                  $this->_album->getId(),
498
 
                                  $this->_items[1]->getId(),
499
 
                                  $album->getId()),
500
 
                            $parentSequence,
501
 
                            'item parent sequence');
502
 
 
503
 
        /* Set virtual root */
504
 
        $gallery->setConfig('breadcrumbRootId', $this->_items[1]->getId());
505
 
 
506
 
        list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($item->getId(), true);
507
 
        if ($ret) {
508
 
            return $this->failWithStatus($ret);
509
 
        }
510
 
        $this->assertEquals(array($this->_items[1]->getId(),
511
 
                                  $album->getId()),
512
 
                            $parentSequence,
513
 
                            'virtual root parent sequence');
514
 
 
515
 
        list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($item->getId());
516
 
        if ($ret) {
517
 
            return $this->failWithStatus($ret);
518
 
        }
519
 
        $this->assertEquals(array($this->_getRootId(),
520
 
                                  $this->_album->getId(),
521
 
                                  $this->_items[1]->getId(),
522
 
                                  $album->getId()),
523
 
                            $parentSequence,
524
 
                            'virtual root, forced full parent sequence');
525
 
 
526
 
        /* Used to have a bug in item creation with breadcrumbRootId set */
527
 
        list ($ret, $item) = $this->_createRandomDataItem($album->getId());
528
 
        if ($ret) {
529
 
            return $this->failWithStatus($ret);
530
 
        }
531
 
        $gallery->setConfig('breadcrumbRootId', null);
532
 
 
533
 
        list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($item->getId());
534
 
        if ($ret) {
535
 
            return $this->failWithStatus($ret);
536
 
        }
537
 
        $this->assertEquals(array($this->_getRootId(),
538
 
                                  $this->_album->getId(),
539
 
                                  $this->_items[1]->getId(),
540
 
                                  $album->getId()),
541
 
                            $parentSequence,
542
 
                            'item parent sequence, item created in multiroot');
543
 
    }
544
 
 
545
 
    function testFetchItemizedDescendentCounts() {
546
 
        list ($ret, $album) = $this->_createRandomAlbum($this->_items[1]->getId());
547
 
        if ($ret) {
548
 
            return $this->failWithStatus($ret);
549
 
        }
550
 
 
551
 
        for ($i = 0; $i < 3; $i++) {
552
 
            list ($ret, $item[$i]) = $this->_createRandomDataItem($album->getId());
553
 
            if ($ret) {
554
 
                return $this->failWithStatus($ret);
555
 
            }
556
 
        }
557
 
 
558
 
        /* Drop perms for one item and one album */
559
 
        $ret = GalleryCoreApi::removeItemPermissions($item[0]->getId());
560
 
        if ($ret) {
561
 
            return $this->failWithStatus($ret);
562
 
        }
563
 
        $ret = GalleryCoreApi::removeItemPermissions($this->_items[0]->getId());
564
 
        if ($ret) {
565
 
            return $this->failWithStatus($ret);
566
 
        }
567
 
 
568
 
        /* Verify our counts */
569
 
        list ($ret, $counts) =
570
 
            GalleryCoreApi::fetchItemizedDescendentCounts(array($this->_album->getId(),
571
 
                                                                  $album->getId()));
572
 
        if ($ret) {
573
 
            return $this->failWithStatus($ret);
574
 
        }
575
 
 
576
 
        $this->assertEquals(array($this->_album->getId() => array('GalleryAlbumItem' => 3,
577
 
                                                                       'GalleryDataItem' => 2),
578
 
                                  $album->getId() => array('GalleryAlbumItem' => 0,
579
 
                                                           'GalleryDataItem' => 2)),
580
 
                            $counts);
581
 
    }
582
 
 
583
 
    function testFetchDescendentCounts() {
584
 
        list ($ret, $album) = $this->_createRandomAlbum($this->_items[1]->getId());
585
 
        if ($ret) {
586
 
            return $this->failWithStatus($ret);
587
 
        }
588
 
 
589
 
        for ($i = 0; $i < 3; $i++) {
590
 
            list ($ret, $item[$i]) = $this->_createRandomDataItem($album->getId());
591
 
            if ($ret) {
592
 
                return $this->failWithStatus($ret);
593
 
            }
594
 
        }
595
 
 
596
 
        /* Drop perms for one item and one album */
597
 
        $ret = GalleryCoreApi::removeItemPermissions($item[0]->getId());
598
 
        if ($ret) {
599
 
            return $this->failWithStatus($ret);
600
 
        }
601
 
        $ret = GalleryCoreApi::removeItemPermissions($this->_items[0]->getId());
602
 
        if ($ret) {
603
 
            return $this->failWithStatus($ret);
604
 
        }
605
 
 
606
 
        /* Verify our counts */
607
 
        list ($ret, $counts) = GalleryCoreApi::fetchDescendentCounts(
608
 
            array($this->_album->getId(), $album->getId()));
609
 
        if ($ret) {
610
 
            return $this->failWithStatus($ret);
611
 
        }
612
 
 
613
 
        $this->assertEquals(array($this->_album->getId() => 5,
614
 
                                  $album->getId() => 2),
615
 
                            $counts);
616
 
    }
617
 
 
618
 
    function testFetchDescendentCountsSessionPermission() {
619
 
        /* Session based permissions should not apply when calculating cached descendent count */
620
 
        global $gallery;
621
 
        $session =& $gallery->getSession();
622
 
        $this->_saveSessionPerms = $session->get(GALLERY_PERMISSION_SESSION_KEY);
623
 
 
624
 
        $itemId = $this->_items[1]->getId();
625
 
        $ret = GalleryCoreApi::removeItemPermissions($itemId);
626
 
        if ($ret) {
627
 
            return $this->failWithStatus($ret);
628
 
        }
629
 
        $ret = GalleryCoreApi::addEntityPermission($itemId, $itemId, 'core.all');
630
 
        if ($ret) {
631
 
            return $this->failWithStatus($ret);
632
 
        }
633
 
        $session->put(GALLERY_PERMISSION_SESSION_KEY, array($itemId));
634
 
 
635
 
        list ($ret, $counts) =
636
 
            GalleryCoreApi::fetchDescendentCounts(array($this->_album->getId()));
637
 
        if ($ret) {
638
 
            $this->failWithStatus($ret);
639
 
        }
640
 
 
641
 
        $this->assertEquals(array($this->_album->getId() => 2), $counts);
642
 
 
643
 
        if (isset($this->_saveSessionPerms)) {
644
 
            $session->put(GALLERY_PERMISSION_SESSION_KEY, $this->_saveSessionPerms);
645
 
        } else {
646
 
            $session->remove(GALLERY_PERMISSION_SESSION_KEY);
647
 
        }
648
 
 
649
 
    }
650
 
 
651
 
    function testFetchDescendentCountsDifferentUser() {
652
 
        global $gallery;
653
 
 
654
 
        list ($ret, $album) = $this->_createRandomAlbum($this->_items[1]->getId());
655
 
        if ($ret) {
656
 
            return $this->failWithStatus($ret);
657
 
        }
658
 
 
659
 
        /* Create a new target user */
660
 
        list ($ret, $user) = $this->_createRandomUser();
661
 
        if ($ret) {
662
 
            return $this->failWithStatus($ret);
663
 
        }
664
 
        $this->_markForCleanup($user);
665
 
 
666
 
        for ($i = 0; $i < 3; $i++) {
667
 
            list ($ret, $item[$i]) = $this->_createRandomDataItem($album->getId());
668
 
            if ($ret) {
669
 
                return $this->failWithStatus($ret);
670
 
            }
671
 
        }
672
 
 
673
 
        /* Drop perms for one item and one album */
674
 
        $ret = GalleryCoreApi::removeItemPermissions($item[0]->getId());
675
 
        if ($ret) {
676
 
            return $this->failWithStatus($ret);
677
 
        }
678
 
        $ret = GalleryCoreApi::removeItemPermissions($this->_items[0]->getId());
679
 
        if ($ret) {
680
 
            return $this->failWithStatus($ret);
681
 
        }
682
 
 
683
 
        /* Add perms back for the active user, but not our target user */
684
 
        $ret = GalleryCoreApi::addUserPermission(
685
 
            $item[0]->getId(), $gallery->getActiveUserId(), 'core.view', false);
686
 
        if ($ret) {
687
 
            return $this->failWithStatus($ret);
688
 
        }
689
 
        $ret = GalleryCoreApi::addUserPermission(
690
 
            $this->_items[0]->getId(), $gallery->getActiveUserId(), 'core.view', false);
691
 
        if ($ret) {
692
 
            return $this->failWithStatus($ret);
693
 
        }
694
 
 
695
 
        /* Verify that the target user can't see the item (even though the active user can) */
696
 
        list ($ret, $counts) = GalleryCoreApi::fetchDescendentCounts(
697
 
            array($this->_album->getId(), $album->getId()),
698
 
            $user->getId());
699
 
        if ($ret) {
700
 
            return $this->failWithStatus($ret);
701
 
        }
702
 
 
703
 
        $this->assertEquals(array($this->_album->getId() => 5,
704
 
                                  $album->getId() => 2),
705
 
                            $counts);
706
 
    }
707
 
}
708
 
 
709
 
class ItemAttributesTestSession {
710
 
    var $_userId;
711
 
    var $_creationTime;
712
 
 
713
 
    function ItemAttributesTestSession($isPersistent=true, $isSearchEngineSession=false) {
714
 
        $this->_creationTime = time();
715
 
        $this->_isPersistent = $isPersistent;
716
 
        $this->_isSearchEngineSession = $isSearchEngineSession;
717
 
    }
718
 
 
719
 
    function put($key, $value) {
720
 
        $this->_data[$key] = $value;
721
 
    }
722
 
 
723
 
    function getCreationTime() {
724
 
        return $this->_creationTime;
725
 
    }
726
 
 
727
 
    function exists($key) {
728
 
        if (!$this->_isPersistent) {
729
 
            return false;
730
 
        }
731
 
 
732
 
        return isset($this->_data[$key]);
733
 
    }
734
 
 
735
 
    function &get($key) {
736
 
        return $this->_data[$key];
737
 
    }
738
 
 
739
 
    function getUserId() {
740
 
        return $this->_userId;
741
 
    }
742
 
 
743
 
    function setUserId($userId) {
744
 
        $this->_userId = $userId;
745
 
    }
746
 
 
747
 
    function isPersistent() {
748
 
        return $this->_isPersistent;
749
 
    }
750
 
 
751
 
    function isSearchEngineSession() {
752
 
        return $this->_isSearchEngineSession;
753
 
    }
754
 
}
755
 
 
756
 
class ItemAttributesTestPhpVm extends GalleryPhpVm {
757
 
 
758
 
    function ItemAttributesTestPhpVm($getAllHeadersExists=false, $headers=array(), $time=null) {
759
 
        $this->_getAllHeadersExists = $getAllHeadersExists;
760
 
        $this->_headers = $headers;
761
 
        $this->_time = $time;
762
 
    }
763
 
 
764
 
    function getallheaders() {
765
 
        return $this->_headers;
766
 
    }
767
 
 
768
 
    function function_exists($functionName) {
769
 
        if ($functionName == 'getallheaders') {
770
 
            return $this->_getAllHeadersExists;
771
 
        }
772
 
 
773
 
        return parent::function_exists($functionName);
774
 
    }
775
 
 
776
 
    function header($header, $replace=null) {
777
 
        /* Avoid modifying actual header information */
778
 
    }
779
 
 
780
 
    function headers_sent() {
781
 
        return GalleryUtilities::_getResponseHeaders() != array();
782
 
    }
783
 
 
784
 
    function time() {
785
 
        if (isset($this->_time)) {
786
 
            return $this->_time;
787
 
        }
788
 
 
789
 
        return time();
790
 
    }
791
 
}
792
 
?>