<?php

	lt_include( PLOG_CLASS_PATH."class/test/helpers/lifetypetestcase.class.php" );
	lt_include( PLOG_CLASS_PATH."class/dao/article.class.php" );
	lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
	lt_include( PLOG_CLASS_PATH."class/dao/bloginfo.class.php" );	
	lt_include( PLOG_CLASS_PATH."class/dao/users.class.php" );
	lt_include( PLOG_CLASS_PATH."class/dao/userinfo.class.php" );	
	lt_include( PLOG_CLASS_PATH."class/dao/articlecategories.class.php" );
	lt_include( PLOG_CLASS_PATH."class/dao/articlecategory.class.php" );
	lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" );
	lt_include( PLOG_CLASS_PATH."class/dao/userstatus.class.php" );		
	lt_include( PLOG_CLASS_PATH."class/summary/dao/summarystats.class.php" );

	/**
	 * Unit test cases for the SummaryStats class
	 */
	class SummaryStats_Test extends LifeTypeTestCase
	{
		/**
		 * dummy blog we'll be using during the tests
		 */
		var $blog;
		
		/**
		 * dummy blog owner
		 */
		var $owner;
		
		/**
		 * dummy category
		 */
		var $cat;
		
		/**
		 * URL pointing to this server's xmlrpc.php
		 */
		
		function setUp()
		{
			// create the blog owner
			$this->owner = new UserInfo( md5(time()),   // name
			                             "password",   // password
			                             "whatever@whatever.com",  // email address
			             				 "",    // about
			                             "" );
			$users = new Users();
			if( !$users->addUser( $this->owner )) {
				throw( new Exception( "Error adding test user" ));
				die();
			}
			
			// create the test blog
			$blogs = new Blogs();
			$this->blog = null;
			$this->blog = new BlogInfo( "test blog",
			                            $this->owner->getId(),
			                            "",
			                            new BlogSettings());
			if( !$blogs->addBlog( $this->blog )) {
				throw( new Exception( "Error adding test blog!" ));
				die();
			}
			
			// add a default category
			$this->cat = new ArticleCategory( "General", 
			                            "",
										$this->blog->getId(),
										true );
			$cats = new ArticleCategories();
			if( !$cats->addArticleCategory( $this->cat )) {
				throw(  new Exception( "Error adding test category!" ));
				die();
			}
		}
		
		function tearDown()
		{
			$users = new Users();
			$users->deleteUser( $this->owner->getId());
			
			$blogs = new Blogs();
			$blogs->deleteBlog( $this->blog->getId());
			
			$cats = new ArticleCategories();
			$cats->deleteCategory( $this->cat->getId(), $this->blog->getId());
		}		
		
		/**
		 * Test case for SummaryStats::getRecentArticles and mantis case 1052 
		 * (http://bugs.lifetype.net/view.php?id=1052)
		 */
		function testGetRecentArticlesIgnoreFuturePosts()
		{
			// create a new post first
			$article = new Article(
				"topic",
				"text",
				Array( $this->cat->getId()),
				$this->owner->getId(),
				$this->blog->getId(),
				POST_STATUS_PUBLISHED,
				0
				);
			// set the date within 5 minutes
			//$t = Timestamp::getTimestampWithOffset( new Timestamp(), 2 );
			$t = new Timestamp();
			$t->addSeconds( 5 * 60 );
			$article->setDateObject( $t );

			// save the article and check
			$articles = new Articles();
			$this->assertTrue( $articles->addArticle( $article ), "Unable to add a new test article" );

			// load the list of recent posts and check that the one we've just added, which
			// has a date in the future, isn't there
			$stats = new SummaryStats();
			$posts = $stats->getRecentArticles();
			$i = 0;
			$found = false;				
			while( $i < count( $posts ) && !$found ) {
				$found = ($posts[$i]->getId() == $article->getId());
				$i++;
			}
			
			$this->assertFalse( $found, "A post with date in the future was returned by getRecentPosts" );
			
			$this->assertTrue( $articles->deleteArticle( $article->getId(), $this->owner->getId(), $this->blog->getId()));
		}
	}
?>