1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7class Rating_AggregationTest extends TikiTestCase 8{ 9 protected $ratingDefaultOptions; 10 protected $ratingAllowMultipleVotes; 11 12 function setUp() 13 { 14 global $user, $testhelpers, $prefs; 15 16 $user = null; 17 18 $tikilib = $this->createMock('TikiLib'); 19 $tikilib->expects($this->any())->method('get_ip_address')->will($this->returnValue('127.0.0.1')); 20 21 $testableTikiLib = new TestableTikiLib; 22 $testableTikiLib->overrideLibs(['tiki' => $tikilib]); 23 24 parent::setUp(); 25 TikiDb::get()->query('DELETE FROM `tiki_user_votings` WHERE `id` LIKE ?', ['test.%']); 26 27 $testhelpers = new TestHelpers(); 28 29 $ratinglib = TikiLib::lib('rating'); 30 31 $this->ratingDefaultOptions = $prefs['rating_default_options']; 32 $prefs['rating_default_options'] = '0,1,2,3,4,5'; 33 $this->ratingAllowMultipleVotes = $prefs['rating_allow_multi_votes']; 34 $prefs['rating_allow_multi_votes'] = 'y'; 35 } 36 37 function tearDown() 38 { 39 global $testhelpers, $user, $prefs; 40 $user = null; 41 parent::tearDown(); 42 TikiDb::get()->query('DELETE FROM `tiki_user_votings` WHERE `id` LIKE ?', ['test.%']); 43 44 $testhelpers->reset_all(); 45 $prefs['rating_default_options'] = $this->ratingDefaultOptions; 46 $prefs['rating_allow_multi_votes'] = $this->ratingAllowMultipleVotes; 47 } 48 49 function testGetGlobalSum() 50 { 51 $lib = new RatingLib; 52 $lib->record_user_vote('abc', 'test', 111, 4, time() - 3000); 53 $lib->record_user_vote('abc', 'test', 111, 2, time() - 2000); 54 $lib->record_user_vote('abc', 'test', 112, 3, time() - 1000); 55 $lib->record_anonymous_vote('deadbeef01234567', 'test', 111, 3, time() - 1000); 56 57 $this->assertEquals(9.0, $lib->collect('test', 111, 'sum')); 58 } 59 60 function testGetGlobalSumSingleVote() 61 { 62 global $prefs; 63 $prefs['rating_allow_multi_votes'] = ''; 64 65 $lib = new RatingLib; 66 $lib->record_user_vote('abc', 'test', 111, 4, time() - 3000); // overridden 67 $lib->record_user_vote('abc', 'test', 111, 2, time() - 2000); 68 $lib->record_user_vote('abc', 'test', 112, 3, time() - 1000); 69 $lib->record_anonymous_vote('deadbeef01234567', 'test', 111, 3, time() - 1000); 70 71 $this->assertEquals(5.0, $lib->collect('test', 111, 'sum')); 72 } 73 74 function testSumWithNoData() 75 { 76 $lib = new RatingLib; 77 78 $this->assertEquals(0.0, $lib->collect('test', 111, 'sum')); 79 } 80 81 function testAverageWithNoData() 82 { 83 $lib = new RatingLib; 84 85 $this->assertEquals(0.0, $lib->collect('test', 111, 'avg')); 86 } 87 88 function testGetGlobalAverage() 89 { 90 $lib = new RatingLib; 91 $lib->record_user_vote('abc', 'test', 111, 5, time() - 3000); 92 $lib->record_user_vote('abc', 'test', 111, 2, time() - 2000); 93 $lib->record_user_vote('abc', 'test', 112, 3, time() - 1000); 94 $lib->record_anonymous_vote('deadbeef01234567', 'test', 111, 3, time() - 1000); 95 96 $this->assertEquals(10 / 3, $lib->collect('test', 111, 'avg'), '', 1 / 1000); 97 } 98 99 function testGetGlobalAverageSingleVote() 100 { 101 global $prefs; 102 $prefs['rating_allow_multi_votes'] = ''; 103 104 $lib = new RatingLib; 105 $lib->record_user_vote('abc', 'test', 111, 5, time() - 3000); // overridden 106 $lib->record_user_vote('abc', 'test', 111, 2, time() - 2000); 107 $lib->record_user_vote('abc', 'test', 112, 3, time() - 1000); 108 $lib->record_anonymous_vote('deadbeef01234567', 'test', 111, 3, time() - 1000); 109 110 $this->assertEquals(5 / 2, $lib->collect('test', 111, 'avg'), '', 1 / 1000); 111 } 112 113 function testBadAggregateFunction() 114 { 115 $lib = new RatingLib; 116 $lib->record_user_vote('abc', 'test', 111, 5, time() - 3000); 117 $lib->record_user_vote('abc', 'test', 111, 2, time() - 2000); 118 $lib->record_user_vote('abc', 'test', 112, 3, time() - 1000); 119 $lib->record_anonymous_vote('deadbeef01234567', 'test', 111, 3, time() - 1000); 120 121 $this->assertFalse($lib->collect('test', 111, 'foobar')); 122 } 123 124 function testTimeRangeLimiter() 125 { 126 $lib = new RatingLib; 127 $lib->record_user_vote('abc', 'test', 111, 5, time() - 3000); 128 $lib->record_user_vote('abc', 'test', 111, 2, time() - 2000); 129 $lib->record_user_vote('abc', 'test', 112, 3, time() - 1000); 130 $lib->record_anonymous_vote('deadbeef01234567', 'test', 111, 3, time() - 1000); 131 132 $this->assertEquals(5.0, $lib->collect('test', 111, 'sum', ['range' => 2500])); 133 } 134 135 function testIgnoreAnonymous() 136 { 137 $lib = new RatingLib; 138 $lib->record_user_vote('abc', 'test', 111, 5, time() - 3000); 139 $lib->record_user_vote('abc', 'test', 111, 2, time() - 2000); 140 $lib->record_user_vote('abc', 'test', 111, 3, time() - 1000); 141 $lib->record_anonymous_vote('deadbeef01234567', 'test', 111, 3, time() - 1000); 142 143 $this->assertEquals(10.0, $lib->collect('test', 111, 'sum', ['ignore' => 'anonymous'])); 144 } 145 146 function testIgnoreAnonymousSingleVote() 147 { 148 global $prefs; 149 $prefs['rating_allow_multi_votes'] = ''; 150 151 $lib = new RatingLib; 152 $lib->record_user_vote('abc', 'test', 111, 5, time() - 3000); // overridden 153 $lib->record_user_vote('abc', 'test', 111, 2, time() - 2000); // overridden 154 $lib->record_user_vote('abc', 'test', 111, 3, time() - 1000); 155 $lib->record_anonymous_vote('deadbeef01234567', 'test', 111, 3, time() - 1000); 156 157 $this->assertEquals(3.0, $lib->collect('test', 111, 'sum', ['ignore' => 'anonymous'])); 158 } 159 160 function testKeepLatest() 161 { 162 $lib = new RatingLib; 163 $lib->record_user_vote('abc', 'test', 111, 5, time() - 3000); 164 $lib->record_user_vote('abc', 'test', 111, 2, time() - 2000); 165 $lib->record_user_vote('abc', 'test', 111, 3, time() - 1500); 166 $lib->record_anonymous_vote('deadbeef01234567', 'test', 111, 3, time() - 1000); 167 168 $this->assertEquals(6.0, $lib->collect('test', 111, 'sum', ['keep' => 'latest'])); 169 170 $this->assertEquals(3.0, $lib->collect('test', 111, 'sum', ['keep' => 'latest', 'range' => 1200])); 171 172 $this->assertEquals(0.0, $lib->collect('test', 111, 'sum', ['keep' => 'latest', 'range' => 1200, 'ignore' => 'anonymous'])); 173 } 174 175 function testKeepOldest() 176 { 177 $lib = new RatingLib; 178 $lib->record_user_vote('abc', 'test', 111, 5, time() - 3000); 179 $lib->record_user_vote('abc', 'test', 111, 2, time() - 2000); 180 $lib->record_user_vote('abc', 'test', 111, 3, time() - 1000); 181 $lib->record_anonymous_vote('deadbeef01234567', 'test', 111, 3, time() - 1000); 182 183 $this->assertEquals(8.0, $lib->collect('test', 111, 'sum', ['keep' => 'oldest'])); 184 185 $this->assertEquals(5.0, $lib->collect('test', 111, 'sum', ['keep' => 'oldest', 'range' => 2500])); 186 187 $this->assertEquals(2.0, $lib->collect('test', 111, 'sum', ['keep' => 'oldest', 'range' => 2500, 'ignore' => 'anonymous'])); 188 } 189 190 function testKeepOldestSingleVote() 191 { 192 global $prefs; 193 $prefs['rating_allow_multi_votes'] = ''; 194 195 $lib = new RatingLib; 196 $lib->record_user_vote('abc', 'test', 111, 5, time() - 3000); // overridden 197 $lib->record_user_vote('abc', 'test', 111, 2, time() - 2000); // overridden 198 $lib->record_user_vote('abc', 'test', 111, 3, time() - 1000); 199 $lib->record_anonymous_vote('deadbeef01234567', 'test', 111, 3, time() - 1000); 200 201 $this->assertEquals(6.0, $lib->collect('test', 111, 'sum', ['keep' => 'oldest'])); 202 203 $this->assertEquals(6.0, $lib->collect('test', 111, 'sum', ['keep' => 'oldest', 'range' => 2500])); 204 205 $this->assertEquals(3.0, $lib->collect('test', 111, 'sum', ['keep' => 'oldest', 'range' => 2500, 'ignore' => 'anonymous'])); 206 } 207 208 function testConsiderPerPeriod() 209 { 210 $lib = new RatingLib; 211 $lib->record_user_vote('abc', 'test', 111, 5, time() - 3000); // kept 212 $lib->record_user_vote('abc', 'test', 111, 2, time() - 2000); // kept 213 $lib->record_user_vote('abc', 'test', 111, 3, time() - 1000); 214 $lib->record_anonymous_vote('deadbeef01234567', 'test', 111, 3, time() - 1000); // kept 215 216 $this->assertEquals(10.0, $lib->collect('test', 111, 'sum', ['keep' => 'oldest', 'revote' => 2500])); 217 218 $this->assertEquals( 219 10 / 3, 220 $lib->collect('test', 111, 'avg', ['keep' => 'oldest', 'revote' => 2500]), 221 '', 222 1 / 1000 223 ); 224 } 225 226 function testConsiderPerPeriodSingleVote() 227 { 228 global $prefs; 229 $prefs['rating_allow_multi_votes'] = ''; 230 231 $lib = new RatingLib; 232 $lib->record_user_vote('abc', 'test', 111, 5, time() - 3000); // overridden 233 $lib->record_user_vote('abc', 'test', 111, 2, time() - 2000); // overridden 234 $lib->record_user_vote('abc', 'test', 111, 3, time() - 1000); //kept 235 $lib->record_anonymous_vote('deadbeef01234567', 'test', 111, 3, time() - 1000); // kept 236 237 $this->assertEquals(6.0, $lib->collect('test', 111, 'sum', ['keep' => 'oldest', 'revote' => 2500])); 238 239 $this->assertEquals( 240 6 / 2, 241 $lib->collect('test', 111, 'avg', ['keep' => 'oldest', 'revote' => 2500]), 242 '', 243 1 / 1000 244 ); 245 } 246} 247