Skip to content

Commit

Permalink
BUGFIX: fix test around poll totals caching (HD#4978)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz authored and Sean Harvey committed Oct 8, 2012
1 parent f7e162e commit cf478a0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
20 changes: 11 additions & 9 deletions code/Poll.php
Expand Up @@ -37,7 +37,7 @@ class Poll extends DataObject implements PermissionProvider {
);

static $default_sort = 'Created DESC';

function getCMSFields() {
Requirements::javascript('polls/javascript/polls.js');

Expand Down Expand Up @@ -118,30 +118,32 @@ function getCMSFields() {
*
* @return int
*/
private $_getTotalVotesCache;

function getTotalVotes($useCache = true) {
static $_cache;
if (!isset($_cache) || !$useCache) {
if (!isset($_getTotalVotesCache) || !$useCache) {
$query = DB::query('SELECT SUM("Votes") As "Total" FROM "PollChoice" WHERE "PollID" = ' . $this->ID);
$res = $query->nextRecord();
$_cache = $res['Total'];
$_getTotalVotesCache = $res['Total'];
}

return $_cache;
return $_getTotalVotesCache;
}

/**
* Find out what is the maximum amount of votes received for one of the options.
* TODO: rewrite as Aggregate, so it uses in-built cache?
*/
private $_getMaxVotesCache;

function getMaxVotes($useCache = true) {
static $_cache;
if (!isset($_cache) || !$useCache) {
if (!isset($_getMaxVotesCache) || !$useCache) {
$query = DB::query('SELECT MAX("Votes") As "Max" FROM "PollChoice" WHERE "PollID" = ' . $this->ID);
$res = $query->nextRecord();
$_cache = $res['Max'];
$_getMaxVotesCache = $res['Max'];
}

return $_cache;
return $_getMaxVotesCache;
}

/**
Expand Down
24 changes: 18 additions & 6 deletions tests/unit/PollTest.php
Expand Up @@ -7,22 +7,25 @@ function testTotalVotes() {
$mobilePoll = $this->ObjFromFixture('Poll', 'mobile-poll');
$this->assertEquals(120 + 80 + 12, $mobilePoll->getTotalVotes());

$mobilePoll = $this->ObjFromFixture('Poll', 'color-poll');
$this->assertEquals(6 + 15 + 30, $mobilePoll->getTotalVotes());
$colorPoll = $this->ObjFromFixture('Poll', 'color-poll');
$this->assertEquals(6 + 15 + 30, $colorPoll->getTotalVotes());
}

function testChartURL() {
$mobilePoll = $this->ObjFromFixture('Poll', 'mobile-poll');
$pollForm = new PollForm(new Controller(), 'PollForm', $mobilePoll);
$chart = $pollForm->getChart();
$this->assertContains(urlencode('iPhone (120)'), $chart);
$this->assertContains(urlencode('Android (80)'), $chart);
$this->assertContains(urlencode('Other (12)'), $chart);
$this->assertContains('iPhone (120)', $chart);
$this->assertContains('Android (80)', $chart);
$this->assertContains('Other (12)', $chart);
}

function testMaxVotes() {
$mobilePoll = $this->ObjFromFixture('Poll', 'mobile-poll');
$this->assertEquals(120, $mobilePoll->getMaxVotes());
$this->assertEquals(120, $mobilePoll->getMaxVotes());

$colorPoll = $this->ObjFromFixture('Poll', 'color-poll');
$this->assertEquals(30, $colorPoll->getMaxVotes());
}

function testVisible() {
Expand All @@ -47,4 +50,13 @@ function testVisible() {
SS_Datetime::set_mock_now('2010-10-12 10:00:00');
$this->assertFalse($mobilePoll->getVisible());
}

function testMarkAsVoted() {
// This cannot be tested this way as it relies on cookies.
/*
$mobilePoll = $this->ObjFromFixture('Poll', 'mobile-poll');
$mobilePoll->markAsVoted();
$this->assertTrue($mobilePoll->isVoted());
*/
}
}

0 comments on commit cf478a0

Please sign in to comment.