Add new voting system

This commit is contained in:
Alexander Nicolaysen Sørnes
2007-10-09 21:10:29 +02:00
committed by Chris Morgan
parent c95da59dd1
commit 835d82e49b
7 changed files with 283 additions and 7 deletions

View File

@@ -44,6 +44,8 @@ echo "\n";
include_once("test_appData.php");
echo "\n";
include_once("test_testData.php");
echo "\n";
include_once("test_voting.php");
// purge any session messages we generated during the test
purgeSessionMessages();

View File

@@ -100,4 +100,17 @@ function error($sMsg)
echo "$sClass::$sFunction:$sFile:$sLine $sMsg\n";
}
function run_test($sTestName)
{
if(!$sTestName())
{
global $bTestSuccess;
echo "$sTestName() failed!\n";
$bTestSuccess = false;
} else
{
echo "$sTestName() passed.\n";
}
}
?>

72
unit_test/test_voting.php Normal file
View File

@@ -0,0 +1,72 @@
<?php
require_once("path.php");
require_once("test_common.php");
require_once(BASE."include/vote.php");
/* Test to see that voteManager::getVotes() returns the same number of votes as MAX_VOTES */
function test_voteManager_getVotes()
{
$oVoteManager = new voteManager();
$iExpected = MAX_VOTES;
$iReceived = sizeof($oVoteManager->getVotes());
if($iExpected != $iReceived)
{
echo "Expected voteManager::getVotes() to return $iExpected vote objects, got $iReceived.\n";
return FALSE;
}
return TRUE;
}
/* Tests that the votes are saved to the database and that we cannot create more than MAX_VOTES.
Note that a user always has MAX_VOTES even though they're not in the DB, so we use update instead of create */
function test_vote_update()
{
$iUserId = 655000;
for($i = 0; $i < MAX_VOTES+1; $i++)
{
$oVote = new vote();
$oVote->iUserId = $iUserId;
$oVote->iSlotIndex = $i+1;
$oVote->update();
}
$oVoteManager = new voteManager($iUserId);
$aVotes = $oVoteManager->getVotes();
/* First test to see that the legit votes are saved */
for($i = 0; $i < MAX_VOTES; $i++)
{
$iExpected = $i+1;
$iReceived = $aVotes[$i]->iSlotIndex;
if($iExpected != $iReceived)
{
echo "Expected slot index of $iExpected, got $iReceived instead.\n";
return FALSE;
}
}
/* There should only be MAX_VOTES number of votes */
$iExpected = MAX_VOTES;
$iReceived = sizeof($aVotes);
if($iExpected != $iReceived)
{
echo "Expected $iExpected number of votes, got $iReceived.\n";
return FALSE;
}
/* We don't normally delete votes, so we have to do it manually */
query_parameters("DELETE FROM appVotes WHERE userId = '?'", $iUserId);
return TRUE;
}
run_test("test_voteManager_getVotes");
run_test("test_vote_update");
?>