Fix a bug where ratings were allowed to be set with less than 10 votes. Fixes bug #11294

This helps fix people trying to game the system.
This commit is contained in:
Colin Kuskie 2009-12-08 14:39:24 -08:00
parent 5153d82327
commit d1e6f30493
5 changed files with 253 additions and 94 deletions

View file

@ -17,7 +17,7 @@ use lib "$FindBin::Bin/../../lib";
use WebGUI::Test;
use WebGUI::Session;
use Test::More tests => 26; # increment this value for each test you create
use Test::More tests => 30; # increment this value for each test you create
use Test::Deep;
use JSON;
use WebGUI::Asset::Wobject::Matrix;
@ -65,13 +65,13 @@ cmp_deeply (
# Test add/edit privileges
is ($matrix->canEdit,'0',"Checking canEdit privilege");
is ($matrix->canAddMatrixListing,'0',"Checking canAddMatrixListing privilege");
ok !$matrix->canEdit, "canEdit: as Visitor cannot edit";
ok !$matrix->canAddMatrixListing,"canAddMatrixListing: as Visitor cannot add matrix listing";
$session->user({userId => 3});
is ($matrix->canEdit,'1',"Checking canEdit privilege as Admin");
is ($matrix->canAddMatrixListing,'1',"Checking canAddMatrixListing privilege as Admin");
ok $matrix->canEdit, "canEdit: as Admin can edit";
ok $matrix->canAddMatrixListing, "canAddMatrixListing: as Admin can add matrix listing";
# add a new attribute
@ -87,7 +87,7 @@ my $newAttribute = $matrix->getAttribute($newAttributeId);
my $isValidId = $session->id->valid($newAttributeId);
is($isValidId,1,"editAttributeSave returnes a valid guid");
ok $isValidId, "editAttributeSave returnes a valid guid";
is($newAttribute->{name},'test attribute',"Adding a new attribute, attribute name was set correctly");
is($newAttribute->{fieldType},'MatrixCompare',"Adding a new attribute, undefined fieldType was set correctly to default value");
@ -115,13 +115,13 @@ isa_ok($matrixListing, 'WebGUI::Asset::MatrixListing');
is($matrixListing->getAutoCommitWorkflowId,undef,"The matrix listings getAutoCommitWorkflowId method correctly returns undef, because the auto commit workflow should only be used on adding a new matrix listing.");
#is($matrixListing->hasRated,'0',"The matrix listings hasRated method returns correct value.");
ok( !$matrixListing->hasRated, "hasRated returns false since user hasn't rated yet");
$matrixListing->setRatings({category1=>'1',category2=>'10'});
$matrixListing->setRatings({category1=>'1',category2=>'9'});
ok($matrixListing->hasRated > 0,"Checking hasRated method after rating.");
ok($matrixListing->hasRated, "hasRated returns true after user has rated");
$matrixListing->setRatings({category1=>'2',category2=>'5'});
$matrixListing->setRatings({category1=>'3',category2=>'5'});
$matrixListing->www_click;
@ -177,7 +177,6 @@ cmp_deeply(
$session->user({userId => 3});
my $json = $matrix->www_getCompareFormData('score');
note $session->getId;
my $compareFormData = JSON->new->decode($json);
@ -230,66 +229,218 @@ cmp_deeply(
# Test statistics caching by view method
WebGUI::Cache->new($session,"matrixStatistics_".$matrix->getId)->delete;
$matrix->view;
my $varStatisticsEncoded = WebGUI::Cache->new($session,"matrixStatistics_".$matrix->getId)->get;
my $varStatistics = JSON->new->decode($varStatisticsEncoded);
cmp_deeply(
$varStatistics,
{
alphanumeric_sortButton=>"<span id='sortByName'><button type='button'>Sort by name</button></span><br />",
bestViews_url=>'/'.$matrixListing->get('url'),
bestViews_count=>1,
bestViews_name=>$matrixListing->get('title'),
bestViews_sortButton=>"<span id='sortByViews'><button type='button'>Sort by views</button></span><br />",
bestCompares_url=>'/'.$matrixListing->get('url'),
bestCompares_count=>1,
bestCompares_name=>$matrixListing->get('title'),
bestCompares_sortButton=>"<span id='sortByCompares'><button type='button'>Sort by compares</button></span><br />",
bestClicks_url=>'/'.$matrixListing->get('url'),
bestClicks_count=>1,
bestClicks_name=>$matrixListing->get('title'),
bestClicks_sortButton=>"<span id='sortByClicks'><button type='button'>Sort by clicks</button></span><br />",
last_updated_loop=>[{
url => $matrixListing->getUrl,
name => $matrixListing->get('title'),
lastUpdated => $session->datetime->epochToHuman($matrixListing->get('lastUpdated'),"%z")
}],
lastUpdated_sortButton=>"<span id='sortByUpdated'><button type='button'>Sort by updated</button></span><br />",
best_rating_loop=>[{
url=>'/'.$matrixListing->get('url'),
category=>'category1',
name=>'untitled',
mean=>'1.50',
median=>2,
count=>2,
},
{
url=>'/'.$matrixListing->get('url'),
category=>'category2',
name=>'untitled',
mean=>'7.50',
median=>10,
count=>2,
}],
worst_rating_loop=>[{
url=>'/'.$matrixListing->get('url'),
category=>'category1',
name=>'untitled',
mean=>'1.50',
median=>2,
count=>2,
},
{
url=>'/'.$matrixListing->get('url'),
category=>'category2',
name=>'untitled',
mean=>'7.50',
median=>10,
count=>2,
}],
listingCount=>1,
$varStatistics,
{
alphanumeric_sortButton=>"<span id='sortByName'><button type='button'>Sort by name</button></span><br />",
bestViews_url=>'/'.$matrixListing->get('url'),
bestViews_count=>1,
bestViews_name=>$matrixListing->get('title'),
bestViews_sortButton=>"<span id='sortByViews'><button type='button'>Sort by views</button></span><br />",
bestCompares_url=>'/'.$matrixListing->get('url'),
bestCompares_count=>1,
bestCompares_name=>$matrixListing->get('title'),
bestCompares_sortButton=>"<span id='sortByCompares'><button type='button'>Sort by compares</button></span><br />",
bestClicks_url=>'/'.$matrixListing->get('url'),
bestClicks_count=>1,
bestClicks_name=>$matrixListing->get('title'),
bestClicks_sortButton=>"<span id='sortByClicks'><button type='button'>Sort by clicks</button></span><br />",
last_updated_loop=>[{
url => $matrixListing->getUrl,
name => $matrixListing->get('title'),
lastUpdated => $session->datetime->epochToHuman($matrixListing->get('lastUpdated'),"%z")
}],
lastUpdated_sortButton=>"<span id='sortByUpdated'><button type='button'>Sort by updated</button></span><br />",
best_rating_loop=>[{
url => '/',
category=> 'category1',
name => undef,
mean => 0,
median => 0,
count => 0,
},
'Statistics were cached by view method.'
);
{
url => '/',
category=> 'category2',
name => undef,
mean => 0,
median => 0,
count => 0,
}],
worst_rating_loop=>[{
url => '/',
category=> 'category1',
name => undef,
mean => 0,
median => 0,
count => 0,
},
{
url => '/',
category=> 'category2',
name => undef,
mean => 0,
median => 0,
count => 0,
}],
listingCount=>1,
},
'Statistics were cached by view method.'
);
##Now, add a bunch of ratings. Each one has two
$matrixListing->setRatings({category1=>'1',category2=>'9'});
$matrixListing->setRatings({category1=>'3',category2=>'5'});
$matrixListing->setRatings({category1=>'1',category2=>'9'});
$matrixListing->setRatings({category1=>'3',category2=>'5'});
$matrixListing->setRatings({category1=>'1',category2=>'9'});
$matrixListing->setRatings({category1=>'3',category2=>'5'});
$matrixListing->setRatings({category1=>'1',category2=>'9'});
WebGUI::Cache->new($session,"matrixStatistics_".$matrix->getId)->delete;
$matrix->view;
my $varStatisticsEncoded = WebGUI::Cache->new($session,"matrixStatistics_".$matrix->getId)->get;
my $varStatistics = JSON->new->decode($varStatisticsEncoded);
cmp_deeply(
{
best_rating_loop => $varStatistics->{best_rating_loop},
worst_rating_loop => $varStatistics->{worst_rating_loop},
},
{
best_rating_loop => [{
url => '/',
category=> 'category1',
name => undef,
mean => 0,
median => 0,
count => 0,
},
{
url => '/',
category=> 'category2',
name => undef,
mean => 0,
median => 0,
count => 0,
}],
worst_rating_loop => [{
url => '/',
category=> 'category1',
name => undef,
mean => 0,
median => 0,
count => 0,
},
{
url => '/',
category=> 'category2',
name => undef,
mean => 0,
median => 0,
count => 0,
}],
},
'With only 9 ratings, still no statistics'
);
WebGUI::Cache->new($session,"matrixStatistics_".$matrix->getId)->delete;
$matrixListing->setRatings({category1=>'3'});
$matrix->view;
my $varStatisticsEncoded = WebGUI::Cache->new($session,"matrixStatistics_".$matrix->getId)->get;
my $varStatistics = JSON->new->decode($varStatisticsEncoded);
cmp_deeply(
{
best_rating_loop => $varStatistics->{best_rating_loop},
worst_rating_loop => $varStatistics->{worst_rating_loop},
},
{
best_rating_loop => [{
url => '/'.$matrixListing->get('url'),
category=> 'category1',
name => 'untitled',
mean => 2,
median => 3,
count => 10,
},
{
url => '/',
category=> 'category2',
name => undef,
mean => 0,
median => 0,
count => 0,
}],
worst_rating_loop => [{
url => '/'.$matrixListing->get('url'),
category=> 'category1',
name => 'untitled',
mean => 2,
median => 3,
count => 10,
},
{
url => '/',
category=> 'category2',
name => undef,
mean => 0,
median => 0,
count => 0,
}],
},
'statistics calculated for the category with 10 ratings'
);
WebGUI::Cache->new($session,"matrixStatistics_".$matrix->getId)->delete;
$matrixListing->setRatings({category2=>'5'});
$matrix->view;
my $varStatisticsEncoded = WebGUI::Cache->new($session,"matrixStatistics_".$matrix->getId)->get;
my $varStatistics = JSON->new->decode($varStatisticsEncoded);
cmp_deeply(
{
best_rating_loop => $varStatistics->{best_rating_loop},
worst_rating_loop => $varStatistics->{worst_rating_loop},
},
{
best_rating_loop => [{
url => '/'.$matrixListing->get('url'),
category=> 'category1',
name => 'untitled',
mean => 2,
median => 3,
count => 10,
},
{
url => '/'.$matrixListing->get('url'),
category=> 'category2',
name => 'untitled',
mean => 7,
median => 9,
count => 10,
}],
worst_rating_loop => [{
url => '/'.$matrixListing->get('url'),
category=> 'category1',
name => 'untitled',
mean => 2,
median => 3,
count => 10,
},
{
url => '/'.$matrixListing->get('url'),
category=> 'category2',
name => 'untitled',
mean => 7,
median => 9,
count => 10,
}],
},
'statistics calculated for the category with 10 ratings'
);