From: Andy S. <an...@us...> - 2005-09-07 10:32:26
|
Update of /cvsroot/gallery/gallery2/modules/imageblock/test/phpunit In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30404/test/phpunit Modified Files: ImageBlockHandlerTest.class Log Message: FIX: core/ItemMove.inc's GalleryEntity::save event was handled incorrectly, it would even create an error. In the case where an item already was in the disabledFlag map, it tried to insert another row anyway. Added a series of unit tests to cover most of the GalleryEnttity::save events for this event handler Index: ImageBlockHandlerTest.class =================================================================== RCS file: /cvsroot/gallery/gallery2/modules/imageblock/test/phpunit/ImageBlockHandlerTest.class,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- ImageBlockHandlerTest.class 23 Aug 2005 03:49:44 -0000 1.7 +++ ImageBlockHandlerTest.class 7 Sep 2005 10:32:18 -0000 1.8 @@ -46,17 +46,16 @@ GalleryCoreApi::relativeRequireOnce('modules/imageblock/classes/ImageBlockDisabledMap.class'); GalleryCoreApi::relativeRequireOnce('modules/imageblock/classes/ImageBlockHelper.class'); - /* Enable event (normally disabled for unit tests) */ - $this->_registerTestEventListener('GalleryEntity::save', new ImageBlockHelper()); - $this->_registerTestEventListener('GalleryEntity::delete', new ImageBlockHelper()); - - list ($ret, $this->_handlerAlbum) = $this->_createRandomAlbum($this->_getRootId()); + list ($ret, $this->_topAlbum, $this->_topItems) = + $this->_createSubAlbumWithItems($this->_getRootId()); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } - $this->_markForCleanup($this->_handlerAlbum); - $ret = ImageBlockDisabledMap::addMapEntry(array('itemId' => $this->_handlerAlbum->getId())); + $this->_markForCleanup($this->_topAlbum); + + list ($ret, $this->_subAlbum, $this->_subItems) = + $this->_createSubAlbumWithItems($this->_topAlbum->getId()); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } @@ -66,16 +65,44 @@ * Delete the album that was used for testing */ function tearDown() { - $ret = ImageBlockDisabledMap::removeMapEntry(array('itemId' => $this->_handlerAlbum->getId())); + $ret = ImageBlockHelper::setDisabledFlag($this->_topAlbum, true, false); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } parent::tearDown(); } + + function _createSubAlbumWithItems($parentId) { + /* Create a new album and image, set thumbnail and highlight */ + list ($ret, $album) = $this->_createRandomAlbum($parentId); + if ($ret->isError()) { + return array($ret->wrap(__FILE__, __LINE__), null, null); + } + $items = array(); + for ($i = 0; $i < 5; $i++) { + list($ret, $items[$i]) = $this->_createRandomDataItem($album->getId()); + if ($ret->isError()) { + return array($ret->wrap(__FILE__, __LINE__), null, null); + } + } + + return array(GalleryStatus::success(), $album, $items); + } + function testImageBlockDisabledMapHandler() { - list ($ret, $this->_handlerItem) = $this->_createRandomDataItem($this->_handlerAlbum->getId()); + /* 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 = ImageBlockDisabledMap::addMapEntry(array('itemId' => $this->_topAlbum->getId())); + if ($ret->isError()) { + return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); + } + + list ($ret, $this->_handlerItem) = $this->_createRandomDataItem($this->_topAlbum->getId()); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } @@ -99,64 +126,349 @@ } $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); - function testImageBlockDisabledMapHandlerItemMove() { - list ($ret, $moveAlbum) = $this->_createRandomAlbum($this->_getRootId()); + /* 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->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } + + /* Verification: Is it in the disabled list? */ + $this->_verifyDisabledFlagForItems(array($item), true); + } - list ($ret, $this->_moveItem) = $this->_createRandomDataItem($moveAlbum->getId()); + /* 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->isError()) { - $this->_markForCleanup($moveAlbum); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } - $moveItemId = $this->_moveItem->getId(); + + /* Verification: Is it in the disabled list? */ + $this->_verifyDisabledFlagForItems(array($item), false); + } - list($ret, $disabledFlag) = ImageBlockHelper::getDisabledFlag($moveItemId); + function testImageBlockDisabledMapHandlerMoveDisabledAlbumToDisabledAlbum() { + /* Move an album with disabledFlag to another album with disabledFlag */ + list ($ret, $albumItem, $this->_subItems2) = + $this->_createSubAlbumWithItems($this->_topAlbum->getId()); if ($ret->isError()) { - $this->_markForCleanup($moveAlbum); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } - $this->assert(!$disabledFlag, 'Item incorrectly marked as disabled'); + + /* Add the album to the disabled map */ + $this->_setAndAssertDisabledFlag($albumItem, true); - /* Lock target, self, and source albums */ - list ($ret, $lockIds[]) = GalleryCoreApi::acquireWriteLock( - array($this->_handlerAlbum->getId(), $moveAlbum->getId(), $this->_getRootId())); + /* 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->isError()) { - $this->_markForCleanup($moveAlbum); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } + + /* Make sure the album is not in the disabled map */ + $this->_setAndAssertDisabledFlag($albumItem, false); - /* Move from root to handlerAlbum */ - $ret = $moveAlbum->move($this->_handlerAlbum->getId()); + /* 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->isError()) { - $this->_markForCleanup($moveAlbum); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } + + /* Make sure the album is in the disabled map */ + $this->_setAndAssertDisabledFlag($albumItem, true); - $ret = $moveAlbum->save(); + /* 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->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } + + /* Make sure the album is not in the disabled map */ + $this->_setAndAssertDisabledFlag($albumItem, false); - $ret = GalleryCoreApi::releaseLocks($lockIds); + /* 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->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } + + /* Verification: Is it in the disabled list? */ + $this->_verifyDisabledFlagForItems(array($albumItem), true); + } - list($ret, $disabledFlag) = ImageBlockHelper::getDisabledFlag($moveAlbum->getId()); + /* 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->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } - $this->assert($disabledFlag, 'Album not marked as disabled after move'); + + /* Verification: Is it in the disabled list? */ + $this->_verifyDisabledFlagForItems(array($albumItem), false); + } - list($ret, $disabledFlag) = ImageBlockHelper::getDisabledFlag($moveItemId); + /* + * Move an item into a new album + * + * @param GalleryItem object, item to move + * @param GalleryAlbumItem object, the old / current parent album + * @param GalleryAlbumItem object, 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->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } - $this->assert($disabledFlag, 'SubItem not disabled after move'); + list ($ret, $lockIds[]) = GalleryCoreApi::acquireWriteLock($item->getId()); + if ($ret->isError()) { + return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); + } + $ret = $item->move($newParent->getId()); + if ($ret->isError()) { + return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); + } + $ret = $item->save(); + if ($ret->isError()) { + /* + * 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 $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); + } + $ret = GalleryCoreApi::releaseLocks($lockIds); + if ($ret->isError()) { + return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); + } + } + + /* 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->isError()) { + return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); + } + $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->isError()) { + return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); + } + /* Assert that the item is in the disable map, or not */ + list ($ret, $disabledFlag) = ImageBlockHelper::getDisabledFlag($item->getId()); + if ($ret->isError()) { + return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); + } + $this->assertEquals($value, !empty($disabledFlag), + 'item disabledFlag map assertion failed'); } } ?> |