summaryrefslogtreecommitdiff
path: root/tests/VoteTest.php
blob: 9d6d904921319b05a5bfcc18a9452982e4eb0379 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
/**
 * SemanticScuttle - your social bookmark manager.
 *
 * PHP version 5.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */

require_once 'prepare.php';

if (!defined('PHPUnit_MAIN_METHOD')) {
    define('PHPUnit_MAIN_METHOD', 'VoteTest::main');
}

/**
 * Unit tests for the SemanticScuttle voting system.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */
class VoteTest extends TestBase
{
    /**
     * Vote service instance to test.
     *
     * @var SemanticScuttle_Service_Vote
     */
    protected $vs = null;



    /**
     * Used to run this test class standalone
     *
     * @return void
     */
    public static function main()
    {
        require_once 'PHPUnit/TextUI/TestRunner.php';
        PHPUnit_TextUI_TestRunner::run(
            new PHPUnit_Framework_TestSuite('VoteTest')
        );
    }



    public function setUp()
    {
        $GLOBALS['enableVoting'] = true;
        //FIXME: create true new instance
        $this->vs = SemanticScuttle_Service_Factory::get('Vote');
        $this->vs->deleteAll();

        $this->bs = SemanticScuttle_Service_Factory::get('Bookmark');
    }



    /**
     * Test getVoting() when no votes have been cast.
     *
     * @return void
     */
    public function testGetVotingZero()
    {
        $bid = $this->addBookmark();
        $this->assertEquals(0, $this->vs->getVoting($bid));
    }



    /**
     * Test getVoting() when one positive vote has been cast.
     *
     * @return void
     */
    public function testGetVotingOne()
    {
        $bid = $this->addBookmark();
        $this->vs->vote($bid, 1, 1);
        $this->assertEquals(1, $this->vs->getVoting($bid));
    }



    /**
     * Test getVoting() when one nevative vote has been cast.
     *
     * @return void
     */
    public function testGetVotingMinusOne()
    {
        $bid = $this->addBookmark();
        $this->vs->vote($bid, 1, -1);
        $this->assertEquals(-1, $this->vs->getVoting($bid));
    }



    /**
     * Test getVoting() when several votes have been cast.
     *
     * @return void
     */
    public function testGetVotingSum()
    {
        $bid = $this->addBookmark();
        $this->vs->vote($bid, 1, 1);
        $this->vs->vote($bid, 2, -1);
        $this->vs->vote($bid, 3, 1);
        $this->vs->vote($bid, 4, 1);
        $this->assertEquals(2, $this->vs->getVoting($bid));
    }



    /**
     * Test getVotes() when no vote has been cast.
     *
     * @return void
     */
    public function testGetVotesZero()
    {
        $bid = $this->addBookmark();
        $this->assertEquals(0, $this->vs->getVotes($bid));
    }



    /**
     * Test getVotes() when one vote has been cast.
     *
     * @return void
     */
    public function testGetVotesOne()
    {
        $bid = $this->addBookmark();
        $this->vs->vote($bid, 1, 1);
        $this->assertEquals(1, $this->vs->getVotes($bid));
    }



    /**
     * Test getVotes() when several votes have been cast.
     *
     * @return void
     */
    public function testGetVotesMultiple()
    {
        $bid = $this->addBookmark();
        $this->vs->vote($bid, 1, 1);
        $this->vs->vote($bid, 2, -1);
        $this->vs->vote($bid, 3, 1);
        $this->vs->vote($bid, 4, 1);
        $this->assertEquals(4, $this->vs->getVotes($bid));
    }



    /**
     * Test hasVoted() when a no vote has been cast
     *
     * @return void
     */
    public function testHasVotedFalse()
    {
        $uid = 1;
        $bid = $this->addBookmark();
        $this->assertFalse($this->vs->hasVoted($bid, $uid));
    }



    /**
     * Test hasVoted() when a vote has been cast
     *
     * @return void
     */
    public function testHasVotedTrue()
    {
        $uid = 1;
        $bid = $this->addBookmark();
        $this->vs->vote($bid, $uid, 1);
        $this->assertTrue($this->vs->hasVoted($bid, $uid));
    }



    /**
     * Test hasVoted() when a vote has been cast for other bookmarks.
     * Also verify that the bookmark voting did not change for
     * the other bookmarks.
     *
     * @return void
     */
    public function testHasVotedFalseOthers()
    {
        $uid = 1;
        $bid = $this->addBookmark();
        $bid2 = $this->addBookmark();
        $bid3 = $this->addBookmark();

        $this->vs->vote($bid, $uid, 1);
        $this->vs->vote($bid3, $uid, 1);

        $this->assertFalse($this->vs->hasVoted($bid2, $uid));

        $bm2 = $this->bs->getBookmark($bid2);
        $this->assertEquals(0, $bm2['bVoting']);
    }



    /**
     * Test getVote() when no vote has been cast.
     *
     * @return void
     */
    public function testGetVoteNone()
    {
        $uid = 1;
        $bid = $this->addBookmark();
        $this->assertNull($this->vs->getVote($bid, $uid));
    }



    /**
     * Test getVote() when a positive vote has been cast.
     *
     * @return void
     */
    public function testGetVoteOne()
    {
        $uid = 1;
        $bid = $this->addBookmark();
        $this->vs->vote($bid, $uid, 1);
        $this->assertEquals(1, $this->vs->getVote($bid, $uid));
    }



    /**
     * Test getVote() when a negavitve vote has been cast.
     *
     * @return void
     */
    public function testGetVoteMinusOne()
    {
        $uid = 1;
        $bid = $this->addBookmark();
        $this->vs->vote($bid, $uid, -1);
        $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
    }



    /**
     * Test vote() with wrong vote parameter
     *
     * @return void
     */
    public function testVoteWrongVoteParam()
    {
        $uid = 1;
        $bid = $this->addBookmark();
        $this->assertFalse($this->vs->vote($bid, $uid, 2));
        $this->assertFalse($this->vs->vote($bid, $uid, 0));
        $this->assertFalse($this->vs->vote($bid, $uid, 1.5));
        $this->assertFalse($this->vs->vote($bid, $uid, -1.1));
        $this->assertFalse($this->vs->vote($bid, $uid, 'yes'));
        $this->assertFalse($this->vs->vote($bid, $uid, 'no'));
    }



    /**
     * Test vote() when the user already has voted
     *
     * @return void
     */
    public function testVoteHasVoted()
    {
        $uid = 1;
        $bid = $this->addBookmark();
        $this->assertTrue($this->vs->vote($bid, $uid, 1));
        $this->assertFalse($this->vs->vote($bid, $uid, 1));

        $bid = $this->addBookmark();
        $this->assertTrue($this->vs->vote($bid, $uid, -1));
        $this->assertFalse($this->vs->vote($bid, $uid, 1));
    }



    /**
     * Test vote() with positive vote
     *
     * @return void
     */
    public function testVotePositive()
    {
        $uid = 1;
        $bid = $this->addBookmark();
        $this->assertTrue($this->vs->vote($bid, $uid, 1));
        $this->assertEquals(1, $this->vs->getVote($bid, $uid));
    }



    /**
     * Test vote() with negative vote
     *
     * @return void
     */
    public function testVoteNegative()
    {
        $uid = 1;
        $bid = $this->addBookmark();
        $this->assertTrue($this->vs->vote($bid, $uid, -1));
        $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
    }



    /**
     * Test that rewriting votings does work
     *
     * @return void
     */
    public function testRewriteVotings()
    {
        $uid = 1;
        $bid = $this->addBookmark();
        $this->assertTrue($this->vs->vote($bid, $uid, 1));

        $bm = $this->bs->getBookmark($bid);
        $this->assertEquals(1, $bm['bVoting']);

        $this->vs->deleteAll();
        //we assume that $vs->deleteAll() does *not* reset
        //voting in bookmarks table
        $bm = $this->bs->getBookmark($bid);
        $this->assertEquals(1, $bm['bVoting']);

        $this->vs->rewriteVotings();
        $bm = $this->bs->getBookmark($bid);
        //now it should be reset to 0
        $this->assertEquals(0, $bm['bVoting']);
    }

}//class VoteTest extends TestBase


if (PHPUnit_MAIN_METHOD == 'VoteTest::main') {
    VoteTest::main();
}
?>