/tests/VoteTest.php

https://github.com/omigeot-ccpo/SemanticScuttle-SSO · PHP · 536 lines · 266 code · 111 blank · 159 comment · 0 complexity · 9bcdc00baeb8454cfc4940938193d3a1 MD5 · raw file

  1. <?php
  2. /**
  3. * SemanticScuttle - your social bookmark manager.
  4. *
  5. * PHP version 5.
  6. *
  7. * @category Bookmarking
  8. * @package SemanticScuttle
  9. * @author Christian Weiske <cweiske@cweiske.de>
  10. * @license GPL http://www.gnu.org/licenses/gpl.html
  11. * @link http://sourceforge.net/projects/semanticscuttle
  12. */
  13. /**
  14. * Unit tests for the SemanticScuttle voting system.
  15. *
  16. * @category Bookmarking
  17. * @package SemanticScuttle
  18. * @author Christian Weiske <cweiske@cweiske.de>
  19. * @license GPL http://www.gnu.org/licenses/gpl.html
  20. * @link http://sourceforge.net/projects/semanticscuttle
  21. */
  22. class VoteTest extends TestBase
  23. {
  24. /**
  25. * Vote service instance to test.
  26. *
  27. * @var SemanticScuttle_Service_Vote
  28. */
  29. protected $vs = null;
  30. /**
  31. * Bookmark service instance.
  32. *
  33. * @var SemanticScuttle_Service_Bookmark
  34. */
  35. protected $bs = null;
  36. public function setUp()
  37. {
  38. $GLOBALS['enableVoting'] = true;
  39. //FIXME: create true new instance
  40. $this->vs = SemanticScuttle_Service_Factory::get('Vote');
  41. $this->vs->deleteAll();
  42. $this->bs = SemanticScuttle_Service_Factory::get('Bookmark');
  43. }
  44. /**
  45. * Test getVoting() when no votes have been cast.
  46. *
  47. * @return void
  48. */
  49. public function testGetVotingZero()
  50. {
  51. $bid = $this->addBookmark();
  52. $this->assertEquals(0, $this->vs->getVoting($bid));
  53. $bm = $this->bs->getBookmark($bid);
  54. $this->assertEquals(0, $bm['bVoting']);
  55. $this->assertEquals(0, $bm['bVotes']);
  56. }
  57. /**
  58. * Test getVoting() when one positive vote has been cast.
  59. *
  60. * @return void
  61. */
  62. public function testGetVotingOne()
  63. {
  64. $bid = $this->addBookmark();
  65. $this->vs->vote($bid, 1, 1);
  66. $this->assertEquals(1, $this->vs->getVoting($bid));
  67. $bm = $this->bs->getBookmark($bid);
  68. $this->assertEquals(1, $bm['bVoting']);
  69. $this->assertEquals(1, $bm['bVotes']);
  70. }
  71. /**
  72. * Test getVoting() when one nevative vote has been cast.
  73. *
  74. * @return void
  75. */
  76. public function testGetVotingMinusOne()
  77. {
  78. $bid = $this->addBookmark();
  79. $this->vs->vote($bid, 1, -1);
  80. $this->assertEquals(-1, $this->vs->getVoting($bid));
  81. $bm = $this->bs->getBookmark($bid);
  82. $this->assertEquals(-1, $bm['bVoting']);
  83. $this->assertEquals(1, $bm['bVotes']);
  84. }
  85. /**
  86. * Test getVoting() when several votes have been cast.
  87. *
  88. * @return void
  89. */
  90. public function testGetVotingSum()
  91. {
  92. $bid = $this->addBookmark();
  93. $this->vs->vote($bid, 1, 1);
  94. $this->vs->vote($bid, 2, -1);
  95. $this->vs->vote($bid, 3, 1);
  96. $this->vs->vote($bid, 4, 1);
  97. $this->assertEquals(2, $this->vs->getVoting($bid));
  98. $bm = $this->bs->getBookmark($bid);
  99. $this->assertEquals(2, $bm['bVoting']);
  100. $this->assertEquals(4, $bm['bVotes']);
  101. }
  102. /**
  103. * Test getVotes() when no vote has been cast.
  104. *
  105. * @return void
  106. */
  107. public function testGetVotesZero()
  108. {
  109. $bid = $this->addBookmark();
  110. $this->assertEquals(0, $this->vs->getVotes($bid));
  111. $bm = $this->bs->getBookmark($bid);
  112. $this->assertEquals(0, $bm['bVoting']);
  113. $this->assertEquals(0, $bm['bVotes']);
  114. }
  115. /**
  116. * Test getVotes() when one vote has been cast.
  117. *
  118. * @return void
  119. */
  120. public function testGetVotesOne()
  121. {
  122. $bid = $this->addBookmark();
  123. $this->vs->vote($bid, 1, 1);
  124. $this->assertEquals(1, $this->vs->getVotes($bid));
  125. $bm = $this->bs->getBookmark($bid);
  126. $this->assertEquals(1, $bm['bVoting']);
  127. $this->assertEquals(1, $bm['bVotes']);
  128. }
  129. /**
  130. * Test getVotes() when several votes have been cast.
  131. *
  132. * @return void
  133. */
  134. public function testGetVotesMultiple()
  135. {
  136. $bid = $this->addBookmark();
  137. $this->vs->vote($bid, 1, 1);
  138. $this->vs->vote($bid, 2, -1);
  139. $this->vs->vote($bid, 3, 1);
  140. $this->vs->vote($bid, 4, 1);
  141. $this->assertEquals(4, $this->vs->getVotes($bid));
  142. $bm = $this->bs->getBookmark($bid);
  143. $this->assertEquals(2, $bm['bVoting']);
  144. $this->assertEquals(4, $bm['bVotes']);
  145. }
  146. /**
  147. * Test hasVoted() when a no vote has been cast
  148. *
  149. * @return void
  150. */
  151. public function testHasVotedFalse()
  152. {
  153. $uid = 1;
  154. $bid = $this->addBookmark();
  155. $this->assertFalse($this->vs->hasVoted($bid, $uid));
  156. }
  157. /**
  158. * Test hasVoted() when a vote has been cast
  159. *
  160. * @return void
  161. */
  162. public function testHasVotedTrue()
  163. {
  164. $uid = 1;
  165. $bid = $this->addBookmark();
  166. $this->vs->vote($bid, $uid, 1);
  167. $this->assertTrue($this->vs->hasVoted($bid, $uid));
  168. }
  169. /**
  170. * Test hasVoted() when a vote has been cast for other bookmarks.
  171. * Also verify that the bookmark voting did not change for
  172. * the other bookmarks.
  173. *
  174. * @return void
  175. */
  176. public function testHasVotedFalseOthers()
  177. {
  178. $uid = 1;
  179. $bid = $this->addBookmark();
  180. $bid2 = $this->addBookmark();
  181. $bid3 = $this->addBookmark();
  182. $this->vs->vote($bid, $uid, 1);
  183. $this->vs->vote($bid3, $uid, 1);
  184. $this->assertFalse($this->vs->hasVoted($bid2, $uid));
  185. $bm2 = $this->bs->getBookmark($bid2);
  186. $this->assertEquals(0, $bm2['bVoting']);
  187. $this->assertEquals(0, $bm2['bVotes']);
  188. }
  189. /**
  190. * Test getVote() when no vote has been cast.
  191. *
  192. * @return void
  193. */
  194. public function testGetVoteNone()
  195. {
  196. $uid = 1;
  197. $bid = $this->addBookmark();
  198. $this->assertNull($this->vs->getVote($bid, $uid));
  199. }
  200. /**
  201. * Test getVote() when a positive vote has been cast.
  202. *
  203. * @return void
  204. */
  205. public function testGetVoteOne()
  206. {
  207. $uid = 1;
  208. $bid = $this->addBookmark();
  209. $this->vs->vote($bid, $uid, 1);
  210. $this->assertEquals(1, $this->vs->getVote($bid, $uid));
  211. }
  212. /**
  213. * Test getVote() when a negavitve vote has been cast.
  214. *
  215. * @return void
  216. */
  217. public function testGetVoteMinusOne()
  218. {
  219. $uid = 1;
  220. $bid = $this->addBookmark();
  221. $this->vs->vote($bid, $uid, -1);
  222. $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
  223. }
  224. /**
  225. * Test vote() when voting is deactivated
  226. *
  227. * @return void
  228. */
  229. public function testVoteVotingDeactivated()
  230. {
  231. $GLOBALS['enableVoting'] = false;
  232. $uid = 1;
  233. $bid = $this->addBookmark();
  234. $this->assertFalse($this->vs->vote($bid, $uid, 1));
  235. }
  236. /**
  237. * Test vote() with wrong vote parameter
  238. *
  239. * @return void
  240. */
  241. public function testVoteWrongVoteParam()
  242. {
  243. $uid = 1;
  244. $bid = $this->addBookmark();
  245. $this->assertFalse($this->vs->vote($bid, $uid, 2));
  246. $this->assertFalse($this->vs->vote($bid, $uid, 0));
  247. $this->assertFalse($this->vs->vote($bid, $uid, 1.5));
  248. $this->assertFalse($this->vs->vote($bid, $uid, -1.1));
  249. $this->assertFalse($this->vs->vote($bid, $uid, 'yes'));
  250. $this->assertFalse($this->vs->vote($bid, $uid, 'no'));
  251. }
  252. /**
  253. * Test vote() when the user already has voted
  254. *
  255. * @return void
  256. */
  257. public function testVoteHasVoted()
  258. {
  259. $uid = 1;
  260. $bid = $this->addBookmark();
  261. $this->assertTrue($this->vs->vote($bid, $uid, 1));
  262. $this->assertTrue($this->vs->vote($bid, $uid, 1));
  263. $bm = $this->bs->getBookmark($bid);
  264. $this->assertEquals(1, $bm['bVoting']);
  265. $this->assertEquals(1, $bm['bVotes']);
  266. $bid = $this->addBookmark();
  267. $this->assertTrue($this->vs->vote($bid, $uid, -1));
  268. $this->assertTrue($this->vs->vote($bid, $uid, 1));
  269. $bm = $this->bs->getBookmark($bid);
  270. $this->assertEquals(1, $bm['bVoting']);
  271. $this->assertEquals(1, $bm['bVotes']);
  272. }
  273. /**
  274. * Test vote() with positive vote
  275. *
  276. * @return void
  277. */
  278. public function testVotePositive()
  279. {
  280. $uid = 1;
  281. $bid = $this->addBookmark();
  282. $this->assertTrue($this->vs->vote($bid, $uid, 1));
  283. $this->assertEquals(1, $this->vs->getVote($bid, $uid));
  284. $bm = $this->bs->getBookmark($bid);
  285. $this->assertEquals(1, $bm['bVoting']);
  286. $this->assertEquals(1, $bm['bVotes']);
  287. }
  288. /**
  289. * Test vote() with negative vote
  290. *
  291. * @return void
  292. */
  293. public function testVoteNegative()
  294. {
  295. $uid = 1;
  296. $bid = $this->addBookmark();
  297. $this->assertTrue($this->vs->vote($bid, $uid, -1));
  298. $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
  299. $bm = $this->bs->getBookmark($bid);
  300. $this->assertEquals(-1, $bm['bVoting']);
  301. $this->assertEquals(1, $bm['bVotes']);
  302. }
  303. /**
  304. * Verify that changing the vote from positive to negative
  305. * works.
  306. *
  307. * @return void
  308. */
  309. public function testVoteChangePosNeg()
  310. {
  311. $uid = 1;
  312. $bid = $this->addBookmark();
  313. $this->assertTrue($this->vs->vote($bid, $uid, 1));
  314. $this->assertEquals(1, $this->vs->getVote($bid, $uid));
  315. $this->assertEquals(1, $this->vs->getVotes($bid));
  316. $b = $this->bs->getBookmark($bid);
  317. $this->assertEquals(1, $b['bVoting']);
  318. $this->assertEquals(1, $b['bVotes']);
  319. //change vote
  320. $this->assertTrue($this->vs->vote($bid, $uid, -1));
  321. $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
  322. $this->assertEquals(1, $this->vs->getVotes($bid));
  323. $b = $this->bs->getBookmark($bid);
  324. $this->assertEquals(-1, $b['bVoting']);
  325. $this->assertEquals(1, $b['bVotes']);
  326. }
  327. /**
  328. * Verify that changing the vote from negative to positive
  329. * works.
  330. *
  331. * @return void
  332. */
  333. public function testVoteChangeNegPos()
  334. {
  335. $uid = 1;
  336. $bid = $this->addBookmark();
  337. $this->assertTrue($this->vs->vote($bid, $uid, -1));
  338. $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
  339. $this->assertEquals(1, $this->vs->getVotes($bid));
  340. $b = $this->bs->getBookmark($bid);
  341. $this->assertEquals(-1, $b['bVoting']);
  342. $this->assertEquals(1, $b['bVotes']);
  343. //change vote
  344. $this->assertTrue($this->vs->vote($bid, $uid, 1));
  345. $this->assertEquals(1, $this->vs->getVote($bid, $uid));
  346. $this->assertEquals(1, $this->vs->getVotes($bid));
  347. $b = $this->bs->getBookmark($bid);
  348. $this->assertEquals(1, $b['bVoting']);
  349. $this->assertEquals(1, $b['bVotes']);
  350. }
  351. /**
  352. * Verify that changing the vote from postitive to positive
  353. * has no strange effects
  354. *
  355. * @return void
  356. */
  357. public function testVoteChangePosPos()
  358. {
  359. $uid = 1;
  360. $bid = $this->addBookmark();
  361. $this->assertTrue($this->vs->vote($bid, $uid, 1));
  362. $this->assertEquals(1, $this->vs->getVote($bid, $uid));
  363. $this->assertEquals(1, $this->vs->getVotes($bid));
  364. $b = $this->bs->getBookmark($bid);
  365. $this->assertEquals(1, $b['bVoting']);
  366. $this->assertEquals(1, $b['bVotes']);
  367. //change vote
  368. $this->assertTrue($this->vs->vote($bid, $uid, 1));
  369. $this->assertEquals(1, $this->vs->getVote($bid, $uid));
  370. $this->assertEquals(1, $this->vs->getVotes($bid));
  371. $b = $this->bs->getBookmark($bid);
  372. $this->assertEquals(1, $b['bVoting']);
  373. $this->assertEquals(1, $b['bVotes']);
  374. }
  375. /**
  376. * Verify that changing the vote from negative to negative
  377. * has no strange effects
  378. *
  379. * @return void
  380. */
  381. public function testVoteChangeNegNeg()
  382. {
  383. $uid = 1;
  384. $bid = $this->addBookmark();
  385. $this->assertTrue($this->vs->vote($bid, $uid, -1));
  386. $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
  387. $this->assertEquals(1, $this->vs->getVotes($bid));
  388. $b = $this->bs->getBookmark($bid);
  389. $this->assertEquals(-1, $b['bVoting']);
  390. $this->assertEquals(1, $b['bVotes']);
  391. //change vote to same value
  392. $this->assertTrue($this->vs->vote($bid, $uid, -1));
  393. $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
  394. $this->assertEquals(1, $this->vs->getVotes($bid));
  395. $b = $this->bs->getBookmark($bid);
  396. $this->assertEquals(-1, $b['bVoting']);
  397. $this->assertEquals(1, $b['bVotes']);
  398. }
  399. /**
  400. * Test that rewriting votings does work
  401. *
  402. * @return void
  403. */
  404. public function testRewriteVotings()
  405. {
  406. $uid = 1;
  407. $bid = $this->addBookmark();
  408. $this->assertTrue($this->vs->vote($bid, $uid, 1));
  409. $bm = $this->bs->getBookmark($bid);
  410. $this->assertEquals(1, $bm['bVoting']);
  411. $this->assertEquals(1, $bm['bVotes']);
  412. $this->vs->deleteAll();
  413. //we assume that $vs->deleteAll() does *not* reset
  414. //voting in bookmarks table
  415. $bm = $this->bs->getBookmark($bid);
  416. $this->assertEquals(1, $bm['bVoting']);
  417. $this->assertEquals(1, $bm['bVotes']);
  418. $this->vs->rewriteVotings();
  419. $bm = $this->bs->getBookmark($bid);
  420. //now it should be reset to 0
  421. $this->assertEquals(0, $bm['bVoting']);
  422. $this->assertEquals(0, $bm['bVotes']);
  423. }
  424. }//class VoteTest extends TestBase
  425. ?>