t('VotingAPI voting - get vote'), 'desc' => t('Unit testing of votingapi_get_* and related functions.'), 'group' => 'Voting API', ); } /** * Implementation of tearDown(). */ function tearDown() { parent::tearDown(); // Clean up all remaining votes set by the test. if (count($this->_cleanupVotes)) { $content_types = array_unique($this->_cleanupVotes); $placeholder = implode(', ', array_fill(0, count($content_types), "'%s'")); db_query("DELETE FROM {votingapi_vote} WHERE content_type IN ($placeholder)", $content_types); db_query("DELETE FROM {votingapi_cache} WHERE content_type IN ($placeholder)", $content_types); } } /** * Implementation of setUp(). */ function setUp() { parent::setUp(); $this->drupalModuleEnable('votingapi'); } /** * VAGV-01: Get a vote. */ function testVotingAPIGetVote() { global $user; $this->assertTrue(TRUE, t('VAGV-01: Get a vote.')); $content_type = $this->randomName(12, 'VTest_'); $this->_cleanupVotes[] = $content_type; $content_id = mt_rand(0, 0xffff); $value = mt_rand(-0xfffe, 0xffff); $vobj = votingapi_add_vote($content_type, $content_id, $value); $this->assertTrue(TRUE, t('testVotingAPIGetVote(): Fetch result by using vote settings')); $vote = votingapi_get_vote($vobj->content_type, $vobj->content_id, $vobj->value_type, $vobj->tag, $vobj->uid); $this->assertEqual($vote, $vobj, t('testVotingAPIGetVote(): Result equals vote')); $this->assertTrue(TRUE, t('testVotingAPIGetVote(): Fetch result by using vote_id')); $vote = votingapi_get_vote_by_id($vobj->vote_id); $this->assertEqual($vote, $vobj, t('testVotingAPIGetVote(): Result equals vote')); } /** * VAGV-02: Get a votes on a content. */ function testVotingAPIGetContentVotes() { global $user; $this->assertTrue(TRUE, t('VAGV-02: Get a votes on a content.')); $content_type = $this->randomName(12, 'VTest_'); $this->_cleanupVotes[] = $content_type; $content_id = mt_rand(0, 0xffff); $num_votes = mt_rand(3, 20); $num_users = 3; $user_votes = array_fill(0, $num_users, array()); for ($i = 0; $i < $num_votes; $i++) { $value = mt_rand(-0xfffe, 0xffff); $uid = mt_rand(0, $num_users); $tag = $this->randomName(12, 'VTest_'); $vobj = votingapi_add_vote($content_type, $content_id, $value, VOTINGAPI_VALUE_DEFAULT_TYPE, $tag, $uid); $user_votes[$uid][] = $vobj; } $this->assertTrue(TRUE, t('testVotingAPIGetContentVotes(): Fetch all result for the content.')); $results = votingapi_get_content_votes($content_type, $content_id); foreach ($results as $uid => $votes) { foreach ($votes as $vote) { $this->assertTrue(in_array($vote, $user_votes[$uid]), t('testVotingAPIGetContentVotes(): Vote exists in the user set votes.')); } } $this->assertTrue(TRUE, t('testVotingAPIGetContentVotes(): Fetch all result for the content set by the user.')); foreach ($user_votes as $uid => $votes) { $results = votingapi_get_user_votes($content_type, $content_id, $uid); foreach ($results as $vote) { $this->assertTrue(in_array($vote, $votes), t('testVotingAPIGetContentVotes(): Vote exists in the user set votes.')); } } } /** * VAGV-03: Get a results on a content. */ function testVotingAPIGetContentResults() { global $user; $this->assertTrue(TRUE, t('VAGV-03: Get results on a content.')); $content_type = $this->randomName(12, 'VTest_'); $this->_cleanupVotes[] = $content_type; $content_id = mt_rand(0, 0xffff); $num_votes = mt_rand(3, 200); $votes = array(); // Some different types to choose from $types = array('percent', 'points', 'option', 0); for ($i = 0; $i < $num_votes; $i++) { $value = mt_rand(-0xfffe, 0xffff); $tag = $this->randomName(12, 'VTest_'); shuffle($types); // In case the first option is 0, will set a random type. $type = reset($types) ? current($types) : $this->randomName(12, 'VTest_'); $vobj = votingapi_add_vote($content_type, $content_id, $value, $type, $tag); $votes[] = $vobj; } // Calculate all results on the content. votingapi_recalculate_results($content_type, $content_id, TRUE); $this->assertTrue(TRUE, t('@prefix Fetch all results for the content using @name.', array('@prefix' => 'testVotingAPIGetContentResults():', '@name' => 'votingapi_get_voting_results()'))); $results = votingapi_get_voting_results($content_type, $content_id); foreach ($results as $result) { $this->assertEqual($result->content_type, $content_type, t('@prefix Result @name is the same to set @name', array('@prefix' => 'testVotingAPIGetContentResults():', '@name' => 'content_type'))); $this->assertEqual($result->content_id, $content_id, t('@prefix Result @name is the same to set @name', array('@prefix' => 'testVotingAPIGetContentResults():', '@name' => 'content_id'))); $vresult = votingapi_get_voting_result($result->content_type, $result->content_id, $result->value_type, $result->tag, $result->function); $this->assertEqual($result, $vresult, t('@prefix Result is the same as fetched by @name.', array('@prefix' => 'testVotingAPIGetContentResults():', '@name' => 'votingapi_get_voting_result()'))); } } }