function TestVoting() { $POD = new PeoplePod(); $POD->debug(1); $user = $POD->getPerson(); $user->email = '*****@*****.**'; //create user $user->password = '******'; $user->nick = 'test'; $user->save(); $POD->changeActor(array('id' => $user->id)); //log in as user $content = $POD->getContent(); $content->set('headline', 'this is the headline'); $content->set('type', 'this is the type'); $content->save(); for ($i = 0; $i < 3; $i++) { //Test all vote fields initialized to zero $this->assertEqual($content->yes_votes, 0); $this->assertEqual($content->no_votes, 0); $this->assertEqual($content->yes_percent, 0); $this->assertEqual($content->no_percent, 0); ////// begin yes_vote tests ////////// //upvote $content->vote('y'); $this->assertEqual($content->yes_votes, 1); //attempts to upvote twice $content->vote('y'); $this->assertEqual($content->yes_votes, 1); //Test upvote percent $this->assertEqual($content->yes_percent, 100); //vote('n')should return false because it was not unvoted. There cannot be both a yes & no vote on one contentObj // yes_vote should be unaffected $content->vote('n'); $this->assertEqual($content->no_votes, 1); //downvote should not count $this->assertEqual($content->yes_votes, 0); //upvote should remain unchanged $this->assertEqual($content->yes_percent, 0); //upvote % should be unchanged $this->assertEqual($content->no_percent, 100); //upvote % should be unchanged //test unvote $content->unvote(); $this->assertEqual($content->yes_votes, 0); //upvote should clear $this->assertEqual($content->yes_percent, 0); //percent should return to 0 $this->assertEqual($content->no_votes, 0); //no_votes should stil be 0 $this->assertEqual($content->no_percent, 0); //no percent should be 0 ////// begin no_vote tests // same test sequence as above, but using no_votes $content->vote('n'); $this->assertEqual($content->no_votes, 1); //attempt to add two no_votes $content->vote('n'); $this->assertEqual($content->no_votes, 1); $this->assertEqual($content->no_percent, 100); //attempt to add upvote on top of no_vote. Should not be allowed $content->vote('y'); $this->assertEqual($content->yes_votes, 1); $this->assertEqual($content->yes_percent, 100); $this->assertEqual($content->no_percent, 0); //remove vote $content->unvote(); $this->assertEqual($content->yes_votes, 0); $this->assertEqual($content->yes_percent, 0); $this->assertEqual($content->no_votes, 0); $this->assertEqual($content->no_percent, 0); //unvote empty vote set $content->unvote(); $this->assertEqual($content->yes_votes, 0); $this->assertEqual($content->yes_percent, 0); $this->assertEqual($content->no_votes, 0); $this->assertEqual($content->no_percent, 0); } /// content-votes are all set to zero, now users will be added to vote and test %'s $content->vote('y'); //start with one upvote //create a new user $user2 = $POD->getPerson(); $user2->email = '*****@*****.**'; //create user $user2->password = '******'; $user2->nick = 'test2'; $user2->save(); $this->assertTrue($user2->success()); $this->assertNotNull($user2->id); $POD->changeActor(array('id' => $user2->id)); $this->assertTrue($POD->isAuthenticated()); $this->assertEqual($POD->currentUser()->nick, $user2->nick); $content->vote('y'); $this->assertEqual($content->yes_votes, 2); //check to make sure up_votes are incremented $this->assertEqual($content->yes_percent, 100); $content->unvote(); $content->vote('n'); $this->assertEqual($content->yes_percent, 50); $this->assertEqual($content->no_percent, 50); //create yet another user to test %'s with a decimal value $user3 = $POD->getPerson(); $user3->email = '*****@*****.**'; //create user $user3->password = '******'; $user3->nick = 'test3'; $user3->save(); $POD->changeActor(array('id' => $user3->id)); $this->assertTrue($POD->isAuthenticated()); $this->assertEqual($POD->currentUser()->nick, $user3->nick); $content->vote('y'); //there should now be 2 upvotes and 1 down $this->assertEqual($content->yes_votes, 2); $this->assertEqual($content->no_votes, 1); $this->assertEqual($content->yes_percent, 66); $this->assertEqual($content->no_percent, 33); error_log("------------------------------------------------------"); error_log("END VOTE TESTS"); // clean up $content->delete(); $user3->delete(); $POD->changeActor(array('id' => $user2->id)); $user2->delete(); $POD->changeActor(array('id' => $user->id)); $user->delete(); }