Ejemplo n.º 1
0
Archivo: add.php Proyecto: legende91/ez
 $email = $comment->attribute('email');
 $changeNotification = false;
 if ($notification === true) {
     // email is enabled in setting
     if (!is_null($email)) {
         $changeNotification = true;
     } else {
         //email is disabled in setting but user logged in
         if (is_null($email) && !$user->isAnonymous()) {
             $changeNotification = true;
             $email = $user->attribute('email');
             $comment->setAttribute('email', $email);
         }
     }
 }
 $commentManager = ezcomCommentManager::instance();
 $commentManager->tpl = $tpl;
 $existingNotification = false;
 $addingResult = false;
 if ($changeNotification) {
     $existingNotification = ezcomSubscription::exists($contentObjectId, $languageId, 'ezcomcomment', $email);
     if (!$existingNotification) {
         $addingResult = $commentManager->addComment($comment, $user, null, true);
     } else {
         $addingResult = $commentManager->addComment($comment, $user);
     }
 } else {
     $addingResult = $commentManager->addComment($comment, $user);
 }
 if ($addingResult !== true) {
     $tpl->setVariable('error_message', $addingResult);
 /**
  * create an instance of ezcomCommentManager
  * @return ezcomCommentManager
  */
 public static function instance()
 {
     if (!isset(self::$instance)) {
         $ini = eZINI::instance('ezcomments.ini');
         $className = $ini->variable('ManagerClasses', 'CommentManagerClass');
         self::$instance = new $className();
     }
     return self::$instance;
 }
 /**
  * Test the addcomment method in ezcomComment
  */
 public function testAddComment()
 {
     //1. test adding a comment without notification
     $time = time();
     $contentObjectID = 209;
     $languageID = 2;
     $comment = ezcomComment::create();
     $comment->setAttribute('name', 'xc');
     $comment->setAttribute('email', '*****@*****.**');
     $comment->setAttribute('text', 'This is a test comment:)');
     $comment->setAttribute('contentobject_id', $contentObjectID);
     $comment->setAttribute('language_id', $languageID);
     $comment->setAttribute('created', $time);
     $comment->setAttribute('modified', $time);
     $user = eZUser::currentUser();
     $commentManager = ezcomCommentManager::instance();
     //1.1 without subscription
     $result = $commentManager->addComment($comment, $user);
     $this->assertSame(true, $result);
     //1.2 with subscription
     //add subscriber
     $time = $time + 1;
     $comment = ezcomComment::create();
     $comment->setAttribute('name', 'xc');
     $comment->setAttribute('email', '*****@*****.**');
     $comment->setAttribute('text', 'This is a test comment:)');
     $comment->setAttribute('contentobject_id', $contentObjectID);
     $comment->setAttribute('language_id', $languageID);
     $comment->setAttribute('created', $time);
     $comment->setAttribute('modified', $time);
     $subscriber = ezcomSubscriber::create();
     $subscriber->setAttribute('user_id', $user->attribute('contentobject_id'));
     $subscriber->setAttribute('email', $comment->attribute('email'));
     $subscriber->store();
     //add subscription
     $subscription = ezcomSubscription::create();
     $subscription->setAttribute('subscriber_id', $subscriber->attribute('id'));
     $subscription->setAttribute('content_id', $contentObjectID);
     $subscription->setAttribute('language_id', $languageID);
     $subscription->setAttribute('user_id', $user->attribute('contentobject_id'));
     $subscription->setAttribute('subscription_type', 'ezcomcomment');
     $subscription->setAttribute('subscription_time', $time);
     $subscription->store();
     //add comment
     $result = $commentManager->addComment($comment, $user);
     $this->assertSame(true, $result);
     //verify the notification
     $notifications = ezcomNotification::fetchNotificationList(1, 1, 0, array('id' => 'desc'));
     $this->assertEquals($contentObjectID, $notifications[0]->attribute('contentobject_id'));
     $this->assertEquals($comment->attribute('id'), $notifications[0]->attribute('comment_id'));
     $this->assertEquals($languageID, $notifications[0]->attribute('language_id'));
     //2. test adding a comment with notification
     $time2 = time() + 3;
     $contentObjectID = 210;
     $languageID = 2;
     $comment2 = ezcomComment::create();
     $comment2->setAttribute('name', 'chen');
     $comment2->setAttribute('email', '*****@*****.**');
     $comment2->setAttribute('text', 'notified comment');
     $comment2->setAttribute('contentobject_id', $contentObjectID);
     $comment2->setAttribute('language_id', $languageID);
     $comment2->setAttribute('created', $time2);
     $comment2->setAttribute('modified', $time2);
     $user2 = eZUser::currentUser();
     //2.1 if there is no subscription
     $result2 = $commentManager->addComment($comment2, $user2, $time2);
     $this->assertSame(true, $result2);
     $notifications = ezcomNotification::fetchNotificationList(1, 1, 0, array('id' => 'desc'));
     $this->assertNotEquals($notifications[0]->attribute('comment_id'), $comment2->attribute('id'));
     //assert that there is no new notification
     //2.2 if there is already subscription
     $comment2 = ezcomComment::create();
     $comment2->setAttribute('name', 'chen');
     $comment2->setAttribute('email', '*****@*****.**');
     $comment2->setAttribute('text', 'notified comment');
     $comment2->setAttribute('contentobject_id', $contentObjectID);
     $comment2->setAttribute('language_id', $languageID);
     $time2 = $time2 + 1;
     //add subscriber
     $subscriber = ezcomSubscriber::create();
     $subscriber->setAttribute('user_id', $user2->attribute('contentobject_id'));
     $subscriber->setAttribute('email', $comment2->attribute('email'));
     $subscriber->store();
     //add subscription
     $subscription = ezcomSubscription::create();
     $subscription->setAttribute('subscriber_id', $subscriber->attribute('id'));
     $subscription->setAttribute('content_id', $contentObjectID);
     $subscription->setAttribute('language_id', $languageID);
     $subscription->setAttribute('user_id', $user2->attribute('contentobject_id'));
     $subscription->setAttribute('subscription_type', 'ezcomcomment');
     $subscription->setAttribute('subscription_time', $time2);
     $subscription->store();
     //add comment
     $comment2->setAttribute('created', $time2);
     $comment2->setAttribute('modified', $time2);
     $result = $commentManager->addComment($comment2, $user2, $time2);
     $this->assertSame(true, $result);
     //vertify the notification
     $notifications = ezcomNotification::fetchNotificationList(1, 1, 0, array('id' => 'desc'));
     $this->assertEquals($contentObjectID, $notifications[0]->attribute('contentobject_id'));
     $this->assertEquals($comment2->attribute('id'), $notifications[0]->attribute('comment_id'));
     $this->assertEquals($languageID, $notifications[0]->attribute('language_id'));
 }