Sindbad~EG File Manager

Current Path : /var/www/web3/modules/imageblock/test/phpunit/
Upload File :
Current File : /var/www/web3/modules/imageblock/test/phpunit/ImageBlockHandlerTest.class

<?php
/*
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2008 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */

/**
 * Test Image Block functionality
 * @package ImageBlock
 * @subpackage PHPUnit
 * @author Jay Rossiter <cryptographite@users.sf.net>
 * @version $Revision: 17580 $
 */
class ImageBlockHandlerTest extends GalleryTestCase {

    function ImageBlockHandlerTest($methodName) {
	$this->GalleryTestCase($methodName);
    }

    function setUp() {
	global $gallery;
	$ret = parent::setUp();
	if ($ret) {
	   return $ret;
	}
	GalleryCoreApi::requireOnce('modules/imageblock/classes/ImageBlockHelper.class');

	list ($ret, $this->_topAlbum, $this->_topItems) =
	    $this->_createSubAlbumWithItems($this->_getRootId());
	if ($ret) {
	    return $ret;
	}

	$this->_markForCleanup($this->_topAlbum);

	list ($ret, $this->_subAlbum, $this->_subItems) =
	    $this->_createSubAlbumWithItems($this->_topAlbum->getId());
	if ($ret) {
	    return $ret;
	}
    }

    function tearDown() {
	$ret = ImageBlockHelper::setDisabledFlag($this->_topAlbum, true, false);
	if ($ret) {
	    return $ret;
	}

	$ret = parent::tearDown();
	if ($ret) {
	   $this->failWithStatus($ret);
	}
    }

    function _createSubAlbumWithItems($parentId) {
	/* Create a new album and image, set thumbnail and highlight */
	list ($ret, $album) = $this->_createRandomAlbum($parentId);
	if ($ret) {
	    return array($ret, null, null);
	}

	$items = array();
	for ($i = 0; $i < 5; $i++) {
	    list ($ret, $items[$i]) = $this->_createRandomDataItem($album->getId());
	    if ($ret) {
		return array($ret, null, null);
	    }
	}

	return array(null, $album, $items);
    }

    function testImageBlockDisabledMapHandler() {
	/* Enable event (normally disabled for unit tests) */
	$this->_registerTestEventListener('GalleryEntity::save', new ImageBlockHelper());
	$this->_registerTestEventListener('GalleryEntity::delete', new ImageBlockHelper());

	/* Add top album to disabledMap */
	$ret = GalleryCoreApi::addMapEntry(
	    'ImageBlockDisabledMap', array('itemId' => $this->_topAlbum->getId()));
	if ($ret) {
	    return $ret;
	}

	list ($ret, $this->_handlerItem) = $this->_createRandomDataItem($this->_topAlbum->getId());
	if ($ret) {
	    return $ret;
	}

	$handlerItemId = $this->_handlerItem->getId();

	list ($ret, $disabledFlag) = ImageBlockHelper::getDisabledFlag($handlerItemId);
	if ($ret) {
	    return $ret;
	}
	$this->assert($disabledFlag, 'Add eventHandler failed');

	$ret = GalleryCoreApi::deleteEntityById($this->_handlerItem->getId(), 'GalleryDataItem');
	if ($ret) {
	    return $ret;
	}

	list ($ret, $disabledFlag) = ImageBlockHelper::getDisabledFlag($handlerItemId);
	if ($ret) {
	    return $ret;
	}
	$this->assert(!$disabledFlag, 'Delete eventHandler failed');
    }

    function testImageBlockDisabledMapHandlerMoveDisabledItemToDisabledAlbum() {
	/* Move a data item from one album with disabledFlag to another album with disabledFlag */
	$item = $this->_topItems[0];

	/* Add the item / it's parent album to the disabled map */
	$this->_setAndAssertDisabledFlag($this->_topAlbum, true);

	/* Add new parent to disabled map  */
	$this->_setAndAssertDisabledFlag($this->_subAlbum, true);

	/* Make sure the event handler is registered */
	$listener = new ImageBlockHelper();
	$this->_registerTestEventListener('GalleryEntity::save', $listener);

	/* Execute the test */
	$this->_moveItem($item, $this->_topAlbum, $this->_subAlbum);

	/* Verification: Is it in the disabled list? */
	$this->_verifyDisabledFlagForItems(array($item), true);
    }

    function testImageBlockDisabledMapHandlerMoveItemToDisabledAlbum() {
	/*
	 * Move a data item from one album without disabledFlag
	 * to another album with disabledFlag
	 */
	$item = $this->_topItems[0];

	/* Make sure the item / it's parent album are not in disabledFlag map */
	$this->_setAndAssertDisabledFlag($this->_topAlbum, false);

	/* Add new parent to disabled map  */
	$this->_setAndAssertDisabledFlag($this->_subAlbum, true);

	/* Make sure the event handler is registered */
	$listener = new ImageBlockHelper();
	$this->_registerTestEventListener('GalleryEntity::save', $listener);

	/* Execute the test */
	$this->_moveItem($item, $this->_topAlbum, $this->_subAlbum);

	/* Verification: Is it in the disabled list? */
	$this->_verifyDisabledFlagForItems(array($item), true);
    }

    function testImageBlockDisabledMapHandlerMoveDisabledItemToAlbum() {
	/*
	 * Move a data item from one album with disabledFlag
	 * to another album without disabledFlag
	 */
	$item = $this->_topItems[0];

	/* Make sure the item / it's parent album are in disabledFlag map */
	$this->_setAndAssertDisabledFlag($this->_topAlbum, true);

	/* Make sure it's new parent album is not in the disabledFlag map  */
	$this->_setAndAssertDisabledFlag($this->_subAlbum, false);

	/* Make sure the event handler is registered */
	$listener = new ImageBlockHelper();
	$this->_registerTestEventListener('GalleryEntity::save', $listener);

	/* Execute the test */
	$this->_moveItem($item, $this->_topAlbum, $this->_subAlbum);

	/* Verification: Is it in the disabled list? */
	$this->_verifyDisabledFlagForItems(array($item), false);
    }

    function testImageBlockDisabledMapHandlerMoveItemToAlbum() {
	/*
	 * Move a data item from one album with disabledFlag
	 * to another album without disabledFlag
	 */
	$item = $this->_topItems[0];

	/* Make sure the item / it's parent album are not in disabledFlag map */
	$this->_setAndAssertDisabledFlag($this->_topAlbum, false);

	/* Make sure it's new parent album is not in the disabledFlag map  */
	$this->_setAndAssertDisabledFlag($this->_subAlbum, false);

	/* Make sure the event handler is registered */
	$listener = new ImageBlockHelper();
	$this->_registerTestEventListener('GalleryEntity::save', $listener);

	/* Execute the test */
	$this->_moveItem($item, $this->_topAlbum, $this->_subAlbum);

	/* Verification: Is it in the disabled list? */
	$this->_verifyDisabledFlagForItems(array($item), false);
    }

    /* Create a new data item in an album with disabledFlag */
    function testImageBlockDisabledMapHandlerNewItemToDisabledAlbum() {
	/* Make sure it's new parent album is in the disabledFlag map  */
	$this->_setAndAssertDisabledFlag($this->_subAlbum, true);

	/* Make sure the event handler is registered */
	$listener = new ImageBlockHelper();
	$this->_registerTestEventListener('GalleryEntity::save', $listener);

	/* Execute the test */
	list ($ret, $item) = $this->_createRandomDataItem($this->_subAlbum->getId());
	if ($ret) {
	    return $ret;
	}

	/* Verification: Is it in the disabled list? */
	$this->_verifyDisabledFlagForItems(array($item), true);
    }

    /* Create a new data item in an album without disabledFlag */
    function testImageBlockDisabledMapHandlerNewItemToAlbum() {
	/* Make sure it's new parent album is in the disabledFlag map  */
	$this->_setAndAssertDisabledFlag($this->_subAlbum, false);

	/* Make sure the event handler is registered */
	$listener = new ImageBlockHelper();
	$this->_registerTestEventListener('GalleryEntity::save', $listener);

	/* Execute the test */
	list ($ret, $item) = $this->_createRandomDataItem($this->_subAlbum->getId());
	if ($ret) {
	    return $ret;
	}

	/* Verification: Is it in the disabled list? */
	$this->_verifyDisabledFlagForItems(array($item), false);
    }

    function testImageBlockDisabledMapHandlerMoveDisabledAlbumToDisabledAlbum() {
	/* Move an album with disabledFlag to another album with disabledFlag */
	list ($ret, $albumItem, $this->_subItems2) =
	    $this->_createSubAlbumWithItems($this->_topAlbum->getId());
	if ($ret) {
	    return $ret;
	}

	/* Add the album to the disabled map */
	$this->_setAndAssertDisabledFlag($albumItem, true);

	/* Add new parent to disabled map  */
	$this->_setAndAssertDisabledFlag($this->_subAlbum, true);

	/* Make sure the event handler is registered */
	$listener = new ImageBlockHelper();
	$this->_registerTestEventListener('GalleryEntity::save', $listener);

	/* Execute the test */
	$this->_moveItem($albumItem, $this->_topAlbum, $this->_subAlbum);

	/* Verification: Is it in the disabled list? */
	$checkItems = $this->_subItems2;
	$checkItems[] = $albumItem;
	$this->_verifyDisabledFlagForItems($checkItems, true);
    }

    function testImageBlockDisabledMapHandlerMoveAlbumToDisabledAlbum() {
	/* Move an album without disabledFlag to another album with disabledFlag */
	list ($ret, $albumItem, $this->_subItems2) =
	    $this->_createSubAlbumWithItems($this->_topAlbum->getId());
	if ($ret) {
	    return $ret;
	}

	/* Make sure the album is not in the disabled map */
	$this->_setAndAssertDisabledFlag($albumItem, false);

	/* Add new parent to disabled map  */
	$this->_setAndAssertDisabledFlag($this->_subAlbum, true);

	/* Make sure the event handler is registered */
	$listener = new ImageBlockHelper();
	$this->_registerTestEventListener('GalleryEntity::save', $listener);

	/* Execute the test */
	$this->_moveItem($albumItem, $this->_topAlbum, $this->_subAlbum);

	/* Verification: Is it in the disabled list? */
	$checkItems = $this->_subItems2;
	$checkItems[] = $albumItem;
	$this->_verifyDisabledFlagForItems($checkItems, false);
    }

    function testImageBlockDisabledMapHandlerMoveDisabledAlbumToAlbum() {
	/* Move an album with disabledFlag to another album without disabledFlag */
	list ($ret, $albumItem, $this->_subItems2) =
	    $this->_createSubAlbumWithItems($this->_topAlbum->getId());
	if ($ret) {
	    return $ret;
	}

	/* Make sure the album is in the disabled map */
	$this->_setAndAssertDisabledFlag($albumItem, true);

	/* Make sure the new parent album is not in the disabledFlag map  */
	$this->_setAndAssertDisabledFlag($this->_subAlbum, false);

	/* Make sure the event handler is registered */
	$listener = new ImageBlockHelper();
	$this->_registerTestEventListener('GalleryEntity::save', $listener);

	/* Execute the test */
	$this->_moveItem($albumItem, $this->_topAlbum, $this->_subAlbum);

	/* Verification: Is it in the disabled list? */
	$checkItems = $this->_subItems2;
	$checkItems[] = $albumItem;
	$this->_verifyDisabledFlagForItems($checkItems, true);
    }

    function testImageBlockDisabledMapHandlerMoveAlbumToAlbum() {
	/* Move an album without disabledFlag to another album without disabledFlag */
	list ($ret, $albumItem, $this->_subItems2) =
	    $this->_createSubAlbumWithItems($this->_topAlbum->getId());
	if ($ret) {
	    return $ret;
	}

	/* Make sure the album is not in the disabled map */
	$this->_setAndAssertDisabledFlag($albumItem, false);

	/* Make sure the new parent album is not in the disabledFlag map  */
	$this->_setAndAssertDisabledFlag($this->_subAlbum, false);

	/* Make sure the event handler is registered */
	$listener = new ImageBlockHelper();
	$this->_registerTestEventListener('GalleryEntity::save', $listener);

	/* Execute the test */
	$this->_moveItem($albumItem, $this->_topAlbum, $this->_subAlbum);

	/* Verification: Is it in the disabled list? */
	$checkItems = $this->_subItems2;
	$checkItems[] = $albumItem;
	$this->_verifyDisabledFlagForItems($checkItems, false);
    }

    /* Create a new album in another album with disabledFlag */
    function testImageBlockDisabledMapHandlerNewAlbumToDisabledAlbum() {
	/* Make sure the new parent album is in the disabledFlag map  */
	$this->_setAndAssertDisabledFlag($this->_subAlbum, true);

	/* Make sure the event handler is registered */
	$listener = new ImageBlockHelper();
	$this->_registerTestEventListener('GalleryEntity::save', $listener);

	/* Execute the test */
	list ($ret, $albumItem) = $this->_createRandomAlbum($this->_subAlbum->getId());
	if ($ret) {
	    return $ret;
	}

	/* Verification: Is it in the disabled list? */
	$this->_verifyDisabledFlagForItems(array($albumItem), true);
    }

    /* Create a new album in another album with disabledFlag */
    function testImageBlockDisabledMapHandlerNewAlbumToAlbum() {
	/* Make sure the new parent album is not in the disabledFlag map  */
	$this->_setAndAssertDisabledFlag($this->_subAlbum, false);

	/* Make sure the event handler is registered */
	$listener = new ImageBlockHelper();
	$this->_registerTestEventListener('GalleryEntity::save', $listener);

	/* Execute the test */
	list ($ret, $albumItem) = $this->_createRandomAlbum($this->_subAlbum->getId());
	if ($ret) {
	    return $ret;
	}

	/* Verification: Is it in the disabled list? */
	$this->_verifyDisabledFlagForItems(array($albumItem), false);
    }

    /* The entity::save event handler should ignore non GalleryItem entities*/
    function testImageBlockSaveEventHandlerShouldIgnoreNonGalleryItemEntities() {
	/* Make sure the event handler is registered */
	$listener = new ImageBlockHelper();
	$this->_registerTestEventListener('GalleryEntity::save', $listener);

	/* Just make sure the test runs without errors */

	/* Execute the test */
	list ($ret, $user) = $this->_createRandomUser();
	if ($ret) {
	    return $ret;
	}
	$this->_markForCleanup($user);

	$user->setFullName('new name to trigger new save');
	list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($user->getId());
	if ($ret) {
	    return $ret;
	}
	$ret = $user->save();
	if ($ret) {
	    return $ret;
	}
	$ret = GalleryCoreApi::releaseLocks($lockId);
	if ($ret) {
	    return $ret;
	}
    }

    /*
     * Move an item into a new album
     *
     * @param GalleryItem $item item to move
     * @param GalleryAlbumItem $oldParent the old / current parent album
     * @param GalleryAlbumItem $newParent the new parent album
     */
    function _moveItem(&$item, &$oldParent, &$newParent) {
	$lockIds = array();

	/* Acquire the locks */
	list ($ret, $lockIds[]) =
	    GalleryCoreApi::acquireReadLock(array($newParent->getId(),
						  $oldParent->getId()));
	if ($ret) {
	    return $ret;
	}
	list ($ret, $lockIds[]) = GalleryCoreApi::acquireWriteLock($item->getId());
	if ($ret) {
	    return $ret;
	}
	$ret = $item->move($newParent->getId());
	if ($ret) {
	    return $ret;
	}
	$ret = $item->save();
	if ($ret) {
	    /*
	     * The imageblock event handler shouldn't have attempted to add the item to the
	     * disabled list if it already was in the list, else we'd see the result here
	     * as an error
	     */
	    return $ret;
	}
	$ret = GalleryCoreApi::releaseLocks($lockIds);
	if ($ret) {
	    return $ret;
	}
    }

    /* Verify the value of the disabledFlag for a list of items */
    function _verifyDisabledFlagForItems($items, $expectedValue) {
	foreach ($items as $item) {
	    list ($ret, $disabledFlag) = ImageBlockHelper::getDisabledFlag($item->getId());
	    if ($ret) {
		return $ret;
	    }
	    $this->assertEquals($expectedValue, !empty($disabledFlag),
				'item is / is not in disabledFlag map');
	}
    }

    /* Sets the disabledFlag for an item and verifies its new value */
    function _setAndAssertDisabledFlag($item, $value) {
	$ret = ImageBlockHelper::setDisabledFlag($item, false, $value);
	if ($ret) {
	    return $ret;
	}
	/* Assert that the item is in the disable map, or not */
	list ($ret, $disabledFlag) = ImageBlockHelper::getDisabledFlag($item->getId());
	if ($ret) {
	    return $ret;
	}
	$this->assertEquals($value, !empty($disabledFlag),
			    'item disabledFlag map assertion failed');
    }
}
?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists