3
* Gallery - a web based photo album viewer and editor
4
* Copyright (C) 2000-2007 Bharat Mediratta
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.
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.
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.
22
* Test ItemAttribute functionality.
23
* @package GalleryCore
25
* @author Bharat Mediratta <bharat@menalto.com>
26
* @version $Revision: 15513 $
28
class ItemAttributesTest extends GalleryTestCase {
30
function ItemAttributesTest($methodName) {
31
$this->GalleryTestCase($methodName);
38
$this->_saveSession = $gallery->_session;
40
/* Clear response headers */
41
$headers =& GalleryUtilities::_getResponseHeaders();
44
list ($ret, $this->_album) = $this->_createRandomAlbum(
45
$this->_getRootId(), array('orderBy' => 'id', 'orderDirection' => ORDER_ASCENDING));
47
return $this->failWithStatus($ret);
49
$this->_markForCleanup($this->_album);
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.
55
$ret = GalleryCoreApi::removeItemPermissions($this->_album->getId());
57
return $this->failWithStatus($ret);
59
list ($ret, $everybodyGroupId) =
60
GalleryCoreApi::getPluginParameter('module', 'core', 'id.everybodyGroup');
62
return $this->failWithStatus($ret);
64
$ret = GalleryCoreApi::addGroupPermission(
65
$this->_album->getId(), $everybodyGroupId, 'core.view', true);
67
return $this->failWithStatus($ret);
69
/* Verify the permissions */
70
list ($ret, $compressedPerms) =
71
GalleryCoreApi::fetchAllPermissionsForItem($this->_album->getId(), true);
73
return $this->failWithStatus($ret);
75
$this->assertEquals(array(array('groupId' => $everybodyGroupId,
76
'permission' => 'core.view')), $compressedPerms,
77
'Failed to define initial permissions');
79
/* Create additional test items */
80
for ($i = 0; $i < 3; $i++) {
81
list ($ret, $this->_items[$i]) =
82
$this->_createRandomAlbum($this->_album->getId());
84
return $this->failWithStatus($ret);
91
$gallery->_session = $this->_saveSession;
97
* Update the view count and verify it.
99
function testSetItemViewCount() {
100
$ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 999);
102
return $this->failWithStatus($ret);
105
list ($ret, $viewCount) =
106
GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
108
return $this->failWithStatus($ret);
111
$this->assertEquals(999, $viewCount);
115
* Update the view count and verify it.
117
function testIncrementItemViewCount() {
119
$gallery->_phpVm = new ItemAttributesTestPhpVm();
120
$gallery->_session = new ItemAttributesTestSession();
122
$ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 0);
124
return $this->failWithStatus($ret);
127
$ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId(), 3);
129
return $this->failWithStatus($ret);
132
list ($ret, $viewCount) =
133
GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
135
return $this->failWithStatus($ret);
138
$this->assertEquals(3, $viewCount);
139
$this->assertEquals(array(), GalleryUtilities::_getResponseHeaders(),
140
'No headers should be sent for logged in users');
144
* Verify that we don't increment the view count until a day after the last increment within
147
function testDontIncrementItemViewCountTooOften() {
149
$gallery->_phpVm = new ItemAttributesTestPhpVm();
150
$gallery->_session = new ItemAttributesTestSession();
152
$ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 0);
154
return $this->failWithStatus($ret);
157
for ($i = 0; $i < 8; $i++) {
158
$ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId(), 1);
160
return $this->failWithStatus($ret);
164
list ($ret, $viewCount) =
165
GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
167
return $this->failWithStatus($ret);
169
$this->assertEquals(1, $viewCount);
173
* Verify that we don't increment the view count until a day after the last increment within
176
function testIncrementItemViewCountThreshold() {
178
$gallery->_phpVm = new ItemAttributesTestPhpVm();
179
$gallery->_session = new ItemAttributesTestSession();
181
$session =& $gallery->_session;
183
$ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 0);
185
return $this->failWithStatus($ret);
188
$ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId(), 1);
190
return $this->failWithStatus($ret);
193
/* Backdate our last viewed time by *almost* one day */
194
$lastViewed =& $session->get('core.lastViewed');
195
$lastViewed[$this->_items[0]->getId()] -= 86398;
197
$ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId(), 1);
199
return $this->failWithStatus($ret);
202
list ($ret, $viewCount) =
203
GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
205
return $this->failWithStatus($ret);
208
/* No change; a full day has not elapsed */
209
$this->assertEquals(1, $viewCount);
211
/* Backdate it further over the day line */
212
$lastViewed =& $session->get('core.lastViewed');
213
$lastViewed[$this->_items[0]->getId()] -= 4;
215
$ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId(), 1);
217
return $this->failWithStatus($ret);
220
list ($ret, $viewCount) =
221
GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
223
return $this->failWithStatus($ret);
227
$this->assertEquals(2, $viewCount);
230
function testIncrementItemViewCountGuestNoSessionIfModifiedNotSetIsSearchEngine() {
232
$gallery->_phpVm = new ItemAttributesTestPhpVm(false, array(), time());
233
$gallery->_session = new ItemAttributesTestSession(false, true);
235
$this->_becomeGuestUser();
236
unset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
238
$ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 0);
240
return $this->failWithStatus($ret);
243
$ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId());
245
return $this->failWithStatus($ret);
248
list ($ret, $viewCount) =
249
GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
251
return $this->failWithStatus($ret);
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());
258
function testIncrementItemViewCountGuestNoSessionIfModifiedNotSetIsNotSearchEngine() {
261
$gallery->_phpVm = new ItemAttributesTestPhpVm(false, array(), $now);
262
$gallery->_session = new ItemAttributesTestSession(false, false);
264
$this->_becomeGuestUser();
265
unset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
267
$ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 0);
269
return $this->failWithStatus($ret);
272
$ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId());
274
return $this->failWithStatus($ret);
277
list ($ret, $viewCount) =
278
GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
280
return $this->failWithStatus($ret);
282
$this->assertEquals(1, $viewCount, 'First attempt, not search engine -> increment');
284
array('last-modified' => 'Last-Modified: ' . GalleryUtilities::getHttpDate($now),
285
'expires' => 'Expires: ' . GalleryUtilities::getHttpDate($now - 3600 * 24 * 7)),
286
GalleryUtilities::_getResponseHeaders());
289
function testIncrementItemViewCountGuestIfModifiedIsSet() {
291
$gallery->_phpVm = new ItemAttributesTestPhpVm();
292
$gallery->_session = new ItemAttributesTestSession(false);
294
$this->_becomeGuestUser();
295
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = 'something';
297
$ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 0);
299
return $this->failWithStatus($ret);
302
$ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId());
304
return $this->failWithStatus($ret);
307
list ($ret, $viewCount) =
308
GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
310
return $this->failWithStatus($ret);
312
$this->assertEquals(0, $viewCount);
315
function testIncrementItemViewCountGuestIfModifiedIsSet2() {
317
$gallery->_phpVm = new ItemAttributesTestPhpVm(true,
318
array('If-Modified-Since' => 'something'));
319
$gallery->_session = new ItemAttributesTestSession(false);
321
$this->_becomeGuestUser();
322
unset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
324
$ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 0);
326
return $this->failWithStatus($ret);
329
$ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId());
331
return $this->failWithStatus($ret);
334
list ($ret, $viewCount) =
335
GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
337
return $this->failWithStatus($ret);
339
$this->assertEquals(0, $viewCount);
342
function testIncrementItemViewCountGuestIfModifiedIsSetNotGuest() {
344
$gallery->_phpVm = new ItemAttributesTestPhpVm(true,
345
array('If-Modified-Since' => 'something'));
346
$gallery->_session = new ItemAttributesTestSession();
348
$asdminUserId = $gallery->getActiveUserId();
349
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = 'foobar';
351
$ret = GalleryCoreApi::setItemViewCount($this->_items[0]->getId(), 0);
353
return $this->failWithStatus($ret);
356
$this->assertEquals($asdminUserId, $gallery->getActiveUserId(), 'admin user id');
358
$ret = GalleryCoreApi::incrementItemViewCount($this->_items[0]->getId());
360
return $this->failWithStatus($ret);
363
list ($ret, $viewCount) =
364
GalleryCoreApi::fetchItemViewCount($this->_items[0]->getId());
366
return $this->failWithStatus($ret);
368
$this->assertEquals(1, $viewCount, 'View count');
372
* Update the view count and verify it
374
function testSetOrderWeight() {
375
$ret = GalleryCoreApi::setItemOrderWeight($this->_items[0]->getId(), 123);
377
return $this->failWithStatus($ret);
380
list ($ret, $orderWeight) =
381
GalleryCoreApi::fetchItemOrderWeight($this->_items[0]->getId());
383
return $this->failWithStatus($ret);
386
$this->assertEquals(123, $orderWeight);
390
* Set the weights to something sequential then verify that we can find the next
391
* higher or lower weight.
393
function testFetchNextItemWeight() {
394
for ($i = 0; $i < sizeof($this->_items); $i++) {
395
$ret = GalleryCoreApi::setItemOrderWeight($this->_items[$i]->getId(), $i);
397
return $this->failWithStatus($ret);
401
list ($ret, $higher) =
402
GalleryCoreApi::fetchNextItemWeight($this->_items[1]->getId(), HIGHER_WEIGHT);
404
return $this->failWithStatus($ret);
406
$this->assertEquals($higher, 2);
408
list ($ret, $lower) =
409
GalleryCoreApi::fetchNextItemWeight($this->_items[1]->getId(), LOWER_WEIGHT);
411
return $this->failWithStatus($ret);
413
$this->assertEquals($lower, 0);
417
* Fetch the min/max child weight.
419
function textFetchExtremeChildWeight() {
420
$this->assert(false);
422
for ($i = 0; $i < sizeof($this->_items); $i++) {
423
$ret = GalleryCoreApi::setItemOrderWeight($this->_items[$i]->getId(), 100+$i);
425
return $this->failWithStatus($ret);
430
GalleryCoreApi::fetchMaxChildWeight($this->_album->getId(), HIGHER_WEIGHT);
432
return $this->failWithStatus($ret);
434
$this->assertEquals($max, 102);
437
GalleryCoreApi::fetchMaxChildWeight($this->_album->getId(), LOWER_WEIGHT);
439
return $this->failWithStatus($ret);
441
$this->assertEquals($max, 100);
445
* Update the view count and verify it.
447
function testRebalanceOrderWeights() {
448
for ($i = 0; $i < sizeof($this->_items); $i++) {
449
$ret = GalleryCoreApi::setItemOrderWeight($this->_items[$i]->getId(), $i);
451
return $this->failWithStatus($ret);
454
$ret = GalleryCoreApi::rebalanceChildOrderWeights($this->_album->getId(), 100);
456
return $this->failWithStatus($ret);
459
$count = sizeof($this->_items);
460
for ($i = 0; $i < $count; $i++) {
461
list ($ret, $weight) =
462
GalleryCoreApi::fetchItemOrderWeight($this->_items[$i]->getId());
464
return $this->failWithStatus($ret);
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()));
472
function testFetchParentSequence() {
475
list ($ret, $album) = $this->_createRandomAlbum($this->_items[1]->getId());
477
return $this->failWithStatus($ret);
480
list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($album->getId());
481
$this->assertEquals(array($this->_getRootId(),
482
$this->_album->getId(),
483
$this->_items[1]->getId()),
485
'album parent sequence');
487
list ($ret, $item) = $this->_createRandomDataItem($album->getId());
489
return $this->failWithStatus($ret);
492
list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($item->getId());
494
return $this->failWithStatus($ret);
496
$this->assertEquals(array($this->_getRootId(),
497
$this->_album->getId(),
498
$this->_items[1]->getId(),
501
'item parent sequence');
503
/* Set virtual root */
504
$gallery->setConfig('breadcrumbRootId', $this->_items[1]->getId());
506
list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($item->getId(), true);
508
return $this->failWithStatus($ret);
510
$this->assertEquals(array($this->_items[1]->getId(),
513
'virtual root parent sequence');
515
list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($item->getId());
517
return $this->failWithStatus($ret);
519
$this->assertEquals(array($this->_getRootId(),
520
$this->_album->getId(),
521
$this->_items[1]->getId(),
524
'virtual root, forced full parent sequence');
526
/* Used to have a bug in item creation with breadcrumbRootId set */
527
list ($ret, $item) = $this->_createRandomDataItem($album->getId());
529
return $this->failWithStatus($ret);
531
$gallery->setConfig('breadcrumbRootId', null);
533
list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($item->getId());
535
return $this->failWithStatus($ret);
537
$this->assertEquals(array($this->_getRootId(),
538
$this->_album->getId(),
539
$this->_items[1]->getId(),
542
'item parent sequence, item created in multiroot');
545
function testFetchItemizedDescendentCounts() {
546
list ($ret, $album) = $this->_createRandomAlbum($this->_items[1]->getId());
548
return $this->failWithStatus($ret);
551
for ($i = 0; $i < 3; $i++) {
552
list ($ret, $item[$i]) = $this->_createRandomDataItem($album->getId());
554
return $this->failWithStatus($ret);
558
/* Drop perms for one item and one album */
559
$ret = GalleryCoreApi::removeItemPermissions($item[0]->getId());
561
return $this->failWithStatus($ret);
563
$ret = GalleryCoreApi::removeItemPermissions($this->_items[0]->getId());
565
return $this->failWithStatus($ret);
568
/* Verify our counts */
569
list ($ret, $counts) =
570
GalleryCoreApi::fetchItemizedDescendentCounts(array($this->_album->getId(),
573
return $this->failWithStatus($ret);
576
$this->assertEquals(array($this->_album->getId() => array('GalleryAlbumItem' => 3,
577
'GalleryDataItem' => 2),
578
$album->getId() => array('GalleryAlbumItem' => 0,
579
'GalleryDataItem' => 2)),
583
function testFetchDescendentCounts() {
584
list ($ret, $album) = $this->_createRandomAlbum($this->_items[1]->getId());
586
return $this->failWithStatus($ret);
589
for ($i = 0; $i < 3; $i++) {
590
list ($ret, $item[$i]) = $this->_createRandomDataItem($album->getId());
592
return $this->failWithStatus($ret);
596
/* Drop perms for one item and one album */
597
$ret = GalleryCoreApi::removeItemPermissions($item[0]->getId());
599
return $this->failWithStatus($ret);
601
$ret = GalleryCoreApi::removeItemPermissions($this->_items[0]->getId());
603
return $this->failWithStatus($ret);
606
/* Verify our counts */
607
list ($ret, $counts) = GalleryCoreApi::fetchDescendentCounts(
608
array($this->_album->getId(), $album->getId()));
610
return $this->failWithStatus($ret);
613
$this->assertEquals(array($this->_album->getId() => 5,
614
$album->getId() => 2),
618
function testFetchDescendentCountsSessionPermission() {
619
/* Session based permissions should not apply when calculating cached descendent count */
621
$session =& $gallery->getSession();
622
$this->_saveSessionPerms = $session->get(GALLERY_PERMISSION_SESSION_KEY);
624
$itemId = $this->_items[1]->getId();
625
$ret = GalleryCoreApi::removeItemPermissions($itemId);
627
return $this->failWithStatus($ret);
629
$ret = GalleryCoreApi::addEntityPermission($itemId, $itemId, 'core.all');
631
return $this->failWithStatus($ret);
633
$session->put(GALLERY_PERMISSION_SESSION_KEY, array($itemId));
635
list ($ret, $counts) =
636
GalleryCoreApi::fetchDescendentCounts(array($this->_album->getId()));
638
$this->failWithStatus($ret);
641
$this->assertEquals(array($this->_album->getId() => 2), $counts);
643
if (isset($this->_saveSessionPerms)) {
644
$session->put(GALLERY_PERMISSION_SESSION_KEY, $this->_saveSessionPerms);
646
$session->remove(GALLERY_PERMISSION_SESSION_KEY);
651
function testFetchDescendentCountsDifferentUser() {
654
list ($ret, $album) = $this->_createRandomAlbum($this->_items[1]->getId());
656
return $this->failWithStatus($ret);
659
/* Create a new target user */
660
list ($ret, $user) = $this->_createRandomUser();
662
return $this->failWithStatus($ret);
664
$this->_markForCleanup($user);
666
for ($i = 0; $i < 3; $i++) {
667
list ($ret, $item[$i]) = $this->_createRandomDataItem($album->getId());
669
return $this->failWithStatus($ret);
673
/* Drop perms for one item and one album */
674
$ret = GalleryCoreApi::removeItemPermissions($item[0]->getId());
676
return $this->failWithStatus($ret);
678
$ret = GalleryCoreApi::removeItemPermissions($this->_items[0]->getId());
680
return $this->failWithStatus($ret);
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);
687
return $this->failWithStatus($ret);
689
$ret = GalleryCoreApi::addUserPermission(
690
$this->_items[0]->getId(), $gallery->getActiveUserId(), 'core.view', false);
692
return $this->failWithStatus($ret);
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()),
700
return $this->failWithStatus($ret);
703
$this->assertEquals(array($this->_album->getId() => 5,
704
$album->getId() => 2),
709
class ItemAttributesTestSession {
713
function ItemAttributesTestSession($isPersistent=true, $isSearchEngineSession=false) {
714
$this->_creationTime = time();
715
$this->_isPersistent = $isPersistent;
716
$this->_isSearchEngineSession = $isSearchEngineSession;
719
function put($key, $value) {
720
$this->_data[$key] = $value;
723
function getCreationTime() {
724
return $this->_creationTime;
727
function exists($key) {
728
if (!$this->_isPersistent) {
732
return isset($this->_data[$key]);
735
function &get($key) {
736
return $this->_data[$key];
739
function getUserId() {
740
return $this->_userId;
743
function setUserId($userId) {
744
$this->_userId = $userId;
747
function isPersistent() {
748
return $this->_isPersistent;
751
function isSearchEngineSession() {
752
return $this->_isSearchEngineSession;
756
class ItemAttributesTestPhpVm extends GalleryPhpVm {
758
function ItemAttributesTestPhpVm($getAllHeadersExists=false, $headers=array(), $time=null) {
759
$this->_getAllHeadersExists = $getAllHeadersExists;
760
$this->_headers = $headers;
761
$this->_time = $time;
764
function getallheaders() {
765
return $this->_headers;
768
function function_exists($functionName) {
769
if ($functionName == 'getallheaders') {
770
return $this->_getAllHeadersExists;
773
return parent::function_exists($functionName);
776
function header($header, $replace=null) {
777
/* Avoid modifying actual header information */
780
function headers_sent() {
781
return GalleryUtilities::_getResponseHeaders() != array();
785
if (isset($this->_time)) {