[pLog-svn] r4190 - in plog/branches/lifetype-1.1.2/class: dao test/tests/dao

oscar at devel.lifetype.net oscar at devel.lifetype.net
Thu Oct 26 21:54:53 GMT 2006


Author: oscar
Date: 2006-10-26 21:54:52 +0000 (Thu, 26 Oct 2006)
New Revision: 4190

Added:
   plog/branches/lifetype-1.1.2/class/test/tests/dao/articlecategories_test.class.php
Modified:
   plog/branches/lifetype-1.1.2/class/dao/articlecategories.class.php
   plog/branches/lifetype-1.1.2/class/dao/articles.class.php
Log:
fixes and test case for issue http://bugs.lifetype.net/view.php?id=1100 -- category counters getting out of sync


Modified: plog/branches/lifetype-1.1.2/class/dao/articlecategories.class.php
===================================================================
--- plog/branches/lifetype-1.1.2/class/dao/articlecategories.class.php	2006-10-26 21:53:57 UTC (rev 4189)
+++ plog/branches/lifetype-1.1.2/class/dao/articlecategories.class.php	2006-10-26 21:54:52 UTC (rev 4190)
@@ -293,6 +293,18 @@
          */
         function updateCategory( $category )
         {
+			// set the counter fields
+			$category->setNumArticles( $this->getNumItems( 
+				$this->getPrefix()."article_categories_link", 
+				"category_id = '".Db::qstr($category->getId())."'",
+				"category_id"
+			));
+			// number of published articles
+			$category->setNumPublishedArticles( $this->getNumItems (
+					$this->getPrefix()."article_categories_link acl, ".$this->getPrefix()."articles a",
+					"acl.category_id = '".Db::qstr($category->getId())."' and acl.article_id = a.id and a.status = ".POST_STATUS_PUBLISHED,
+					"category_id"
+			));
         	if( $result = $this->update( $category )) {
 				$this->_cache->removeData( $category->getBlogId(), CACHE_ARTICLE_CATEGORIES_BLOG );
         		$this->_cache->setData( $category->getId(), CACHE_ARTICLE_CATEGORIES, $category );

Modified: plog/branches/lifetype-1.1.2/class/dao/articles.class.php
===================================================================
--- plog/branches/lifetype-1.1.2/class/dao/articles.class.php	2006-10-26 21:53:57 UTC (rev 4189)
+++ plog/branches/lifetype-1.1.2/class/dao/articles.class.php	2006-10-26 21:54:52 UTC (rev 4190)
@@ -666,11 +666,11 @@
 
                	$category = $articleCategories->getCategory( $categoryId );                
                 if( $article->getStatus() == POST_STATUS_PUBLISHED ) {
-                	$category->setNumPublishedArticles( $category->getNumPublishedArticles() +1 );
+                	//$category->setNumPublishedArticles( $category->getNumPublishedArticles() +1 );
 					include_once( PLOG_CLASS_PATH."class/data/timestamp.class.php" );
 					$category->setLastModification( new Timestamp());
                 }
-				$category->setNumArticles( $category->getNumAllArticles() + 1 );
+				//$category->setNumArticles( $category->getNumAllArticles() + 1 );
 				$articleCategories->updateCategory( $category );
             }
 
@@ -695,10 +695,6 @@
             	// updated the category counters
             	$articleCategories = new ArticleCategories();
             	foreach( $article->getCategories() as $category ) {
-					$category->setNumArticles( $category->getNumArticles( POST_STATUS_ALL ) - 1 );
-					if( $article->getStatus() == POST_STATUS_PUBLISHED ) {
-						$category->setNumPublishedArticles( $category->getNumPublishedArticles() - 1 );
-					}
 					$articleCategories->updateCategory( $category );
             	}
 				// clean the cache that contains the links

Added: plog/branches/lifetype-1.1.2/class/test/tests/dao/articlecategories_test.class.php
===================================================================
--- plog/branches/lifetype-1.1.2/class/test/tests/dao/articlecategories_test.class.php	2006-10-26 21:53:57 UTC (rev 4189)
+++ plog/branches/lifetype-1.1.2/class/test/tests/dao/articlecategories_test.class.php	2006-10-26 21:54:52 UTC (rev 4190)
@@ -0,0 +1,54 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/test/helpers/lifetypetestcase.class.php" );
+	include_once( PLOG_CLASS_PATH."class/test/helpers/testtools.class.php" );	
+	include_once( PLOG_CLASS_PATH."class/dao/articlecategory.class.php" );
+	include_once( PLOG_CLASS_PATH."class/dao/articlecategories.class.php" );
+
+	/**
+	 * \ingroup Test
+	 *
+	 * Test cases for the ArticleCategories class
+	 */
+	class ArticleCategories_Test extends LifeTypeTestCase
+	{
+		
+		/**
+		 * Regression test for Mantis case http://bugs.lifetype.net/view.php?id=1100 -- category
+		 * counters getting out of sync.
+		 */
+		function testArticleCategoriesCounters()
+		{
+			// create the test data
+			$data = TestTools::createBlogScenario( Array( "num_articles" => 5, "num_categories" => 3 ));
+			
+			// add an article to one of the categories
+			$article = TestTools::createArticle( $data["blog"]->getId(), $data["user"]->getId(), Array( $data["categories"][0]->getId()), POST_STATUS_DRAFT );
+			$this->assertTrue( $article, "Error creating test article!" );
+			
+			// check that the counter has been updated
+			$articleCategories = new ArticleCategories();
+			$updatedCat = $articleCategories->getCategory( $data["categories"][0]->getId());
+			$this->assertTrue( $updatedCat->getNumArticles( POST_STATUS_ALL ) == ($data["categories"][0]->getNumArticles() + 1 ), "Article category counters are not valid!" );
+			
+			// now let's mess up the database a little
+			$db =& Db::getDb();
+			$db->Execute( "UPDATE ".Db::getPrefix().
+			              "articles_categories SET num_published_articles = 234234, num_articles = 2342 ".
+			              "WHERE id = ".$data["categories"][1]->getId());
+			// trigger a cache reset
+			$articleCategories->_cache->removeData( $data["categories"][1]->getId(), CACHE_ARTICLE_CATEGORIES );
+			// update the category, relaod it and check that the counters are correct again
+			$tmp = $data["categories"][1];
+			$articleCategories->updateCategory( $tmp );
+			$updatedCat2 = $articleCategories->getCategory( $data["categories"][1]->getId());
+			$this->assertTrue( $updatedCat2->getNumArticles( POST_STATUS_ALL ) == $data["categories"][1]->getNumArticles( POST_STATUS_ALL ), "Article category counters do not match!" );
+			$this->assertTrue( $updatedCat2->getNumPublishedArticles() == $data["categories"][1]->getNumPublishedArticles(), "Article category counters do not match!" );
+
+			// delete the test data
+			//TestTools::deleteDaoTestData( Array( $data["blog"], $data["user"] ));
+			//TestTools::deleteDaoTestData( $data["categories"] );
+			//TestTools::deleteDaoTestData( $data["articles"] );
+		}
+	}
+?>
\ No newline at end of file



More information about the pLog-svn mailing list