<?php

	lt_include( PLOG_CLASS_PATH."class/test/helpers/lifetypetestcase.class.php" );
	lt_include( PLOG_CLASS_PATH."class/test/helpers/testtools.class.php" );	
	lt_include( PLOG_CLASS_PATH."class/dao/bloginfo.class.php" );
	lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
	lt_include( PLOG_CLASS_PATH."class/dao/article.class.php" );
	lt_include( PLOG_CLASS_PATH."class/dao/commentscommon.class.php" );
	lt_include( PLOG_CLASS_PATH."class/dao/usercomment.class.php" );
	lt_include( PLOG_CLASS_PATH."class/dao/trackbacks.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/articles.class.php" );
	lt_include( PLOG_CLASS_PATH."class/dao/articlecomments.class.php" );
	lt_include( PLOG_CLASS_PATH."class/data/timestamp.class.php" );
	lt_include( PLOG_CLASS_PATH."class/template/cachecontrol.class.php" );

	/**
	 * \ingroup Test
	 *
	 * Test cases for the CommentsCommon class
	 */
	class CommentsCommon_Test extends LifeTypeTestCase
	{

        function testGetNumPostComments()
		{

            // create a user
            $users = new Users();
            $randomName = md5(time());
            $user = new UserInfo($randomName, "blah", "test@not-a-real-address.net", "", "Full Name");
            $this->assertTrue($users->addUser($user), "Couldn't add test user");
            
            // create a blog
			$blogs = new Blogs();
			$randomName = md5(time());
			$blog = new BlogInfo( $randomName, $user->getId(), "About blog random blog", new BlogSettings());
			$this->assertTrue($blogs->addBlog( $blog ), "Couldn't add test blog");

            // add an article
            $articles = new Articles();
			$article = new Article(
				"dummy topic",
				"dummy text",
				Array( 1 ),   // a dummy category (I don't need a category here)
				$user->getId(), // our user we created above
				$blog->getId(),  // our blog we created above
				POST_STATUS_PUBLISHED,  // published status
				0   // not read yet
                );
            $this->assertTrue($articles->addArticle($article), "Couldn't add test article");

            // Add comments
            $timestamp = new Timestamp();
            $comment1 = new UserComment($article->getId(),
                                        $article->getBlogId(),
                                        0, // dummy parent
                                        "dummy topic",
                                        "dummy text",
                                        $timestamp->getTimestamp());
            
            $timestamp = new Timestamp();
            $comment2 = new UserComment($article->getId(),
                                        $article->getBlogId(),
                                        0, // dummy parent
                                        "dummy topic 2",
                                        "dummy text 2",
                                        $timestamp->getTimestamp());

            $timestamp = new Timestamp();
            $comment3 = new UserComment($article->getId(),
                                        $article->getBlogId(),
                                        0, // dummy parent
                                        "dummy topic 2",
                                        "spam",
                                        $timestamp->getTimestamp(),
                                        "username",
                                        "",
                                        "",
                                        "0.0.0.0",
                                        0,
                                        COMMENT_STATUS_SPAM);

                // add trackbacks
            $timestamp = new Timestamp();
            $trackback1 = new Trackback("fake url",
                                        "this is a title",
                                        $article->getId(),
                                        $article->getBlogId(),
                                        "excerpt from my blog",
                                        "my blog name",
                                        $timestamp->getTimestamp(),
                                        "0.0.0.0");

            $timestamp = new Timestamp();
            $trackback2 = new Trackback("fake url 2",
                                        "this is a title",
                                        $article->getId(),
                                        $article->getBlogId(),
                                        "excerpt from my blog",
                                        "my blog name",
                                        $timestamp->getTimestamp(),
                                        "0.0.0.0",
                                        0,
                                        COMMENT_STATUS_SPAM);
            $timestamp = new Timestamp();
            $trackback3 = new Trackback("fake url 3",
                                        "this is a title",
                                        $article->getId(),
                                        $article->getBlogId(),
                                        "excerpt from my blog",
                                        "my blog name",
                                        $timestamp->getTimestamp(),
                                        "0.0.0.0",
                                        0,
                                        COMMENT_STATUS_SPAM);
            $timestamp = new Timestamp();
            $trackback4 = new Trackback("fake url 4",
                                        "this is a title",
                                        $article->getId(),
                                        $article->getBlogId(),
                                        "excerpt from my blog",
                                        "my blog name",
                                        $timestamp->getTimestamp(),
                                        "0.0.0.0",
                                        0,
                                        COMMENT_STATUS_SPAM);


            $comments = new CommentsCommon();
            $this->assertTrue($comments->addComment($comment1), "Couldn't add test comment 1");
            $this->assertTrue($comments->addComment($comment2), "Couldn't add test comment 2");
            $this->assertTrue($comments->addComment($comment3), "Couldn't add test comment 3");
            $this->assertTrue($comments->addComment($trackback1), "Couldn't add test trackback 1");
            $this->assertTrue($comments->addComment($trackback2), "Couldn't add test trackback 2");
            $this->assertTrue($comments->addComment($trackback3), "Couldn't add test trackback 3");
            $this->assertTrue($comments->addComment($trackback4), "Couldn't add test trackback 4");

            $num = $comments->getNumPostComments($article->getId(), COMMENT_STATUS_ALL, COMMENT_TYPE_ANY);
            $this->assertTrue($num == 7, "Wrong number of comments/trackbacks (all) ". $num);
            $num = $comments->getNumPostComments($article->getId(), COMMENT_STATUS_NONSPAM, COMMENT_TYPE_ANY);
            $this->assertTrue($num == 3, "Wrong number of comments/trackbacks (nonspam) ". $num);
            $num = $comments->getNumPostComments($article->getId(), COMMENT_STATUS_SPAM, COMMENT_TYPE_ANY);
            $this->assertTrue($num == 4, "Wrong number of comments/trackbacks (spam) ". $num);

            $num = $comments->getNumPostComments($article->getId(), COMMENT_STATUS_ALL, COMMENT_TYPE_COMMENT);
            $this->assertTrue($num == 3, "Wrong number of comments (all) ". $num);
            $num = $comments->getNumPostComments($article->getId(), COMMENT_STATUS_NONSPAM, COMMENT_TYPE_COMMENT);
            $this->assertTrue($num == 2, "Wrong number of comments (nonspam) ". $num);
            $num = $comments->getNumPostComments($article->getId(), COMMENT_STATUS_SPAM, COMMENT_TYPE_COMMENT);
            $this->assertTrue($num == 1, "Wrong number of comments (spam) ". $num);

            $num = $comments->getNumPostComments($article->getId(), COMMENT_STATUS_ALL, COMMENT_TYPE_TRACKBACK);
            $this->assertTrue($num == 4, "Wrong number of trackbacks (all) ". $num);
            $num = $comments->getNumPostComments($article->getId(), COMMENT_STATUS_NONSPAM, COMMENT_TYPE_TRACKBACK);
            $this->assertTrue($num == 1, "Wrong number of trackbacks (nonspam) ". $num);
            $num = $comments->getNumPostComments($article->getId(), COMMENT_STATUS_SPAM, COMMENT_TYPE_TRACKBACK);
            $this->assertTrue($num == 3, "Wrong number of trackbacks (spam) ". $num);

            
			// delete the temporary blogs
			$blogs->deleteBlog($blog->getId());
            $users->deleteUser($user->getId());
		}
		
		/**
		 * Test case for mantis bug http://bugs.lifetype.net/view.php?id=1144
		 *
		 * Dates with time offset being saved to the database
		 */
		function testUpdateCommentWithTimeOffsets()
		{
			// create the scenario
			$user = TestTools::createUser();
			$blog = TestTools::createBlog( $user->getId());
			$cat  = TestTools::createArticleCategory( $blog->getId());
			$article = TestTools::createArticle( $blog->getId(), $user->getId(), Array( $cat->getId()));
			
			// update the time offset settings for the blog
			$blog->setValue( "time_offset", "+3" );
			$blogs = new Blogs();
			$blogs->updateBlog( $blog );
			
			// create the comment and save it to the database
			$comment = new UserComment( $article->getId(), $blog->getId(), 0, "test comment", "test comment body" );
			$comments = new ArticleComments();
			$comments->addComment( $comment );
			
			// now get the time
			$origTime = $comment->getTimestamp();
			
			// update the comment and reload the comment
			$comments->updateComment( $comment );
			$comment2 = $comments->getComment( $comment->getId());
			$newTime = $comment2->getTimestamp();
			
			// check that dates are the same
			$this->assertEquals( $origTime->getTimestamp(), $newTime->getTimestamp(), "Comment times are not the same!" );
			
			// destroy the test data
			TestTools::deleteDaoTestData( Array( $user, $blog, $cat, $article, $comment, $comment2 ));
		}
	}
?>


