t('VotingAPI voting - set vote'), 'desc' => t('Unit testing of votingapi_set_vote 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'); } /** * VASV-01A: Set a numeric vote with getting calculated results back. */ function testVotingAPISetVoteNumeric() { $this->assertTrue(TRUE, t('VASV-01A: Testing voting with a number with getting calculated results back.')); $content_type = $this->randomName(12, 'VTest_'); $this->_cleanupVotes[] = $content_type; $content_id = mt_rand(0, 0xffff); $value = mt_rand(-0xfffe, 0xffff); $results = votingapi_set_vote($content_type, $content_id, $value); $this->assertVotingAPIResults('testVotingAPISetVoteNumeric():', $content_type, $content_id, $results); } /** * VASV-01B: Set a numeric vote. */ function testVotingAPISetVoteNumericResults() { $this->assertTrue(TRUE, t('VASV-01B: Testing voting with a number.')); $content_type = $this->randomName(12, 'VTest_'); $this->_cleanupVotes[] = $content_type; $content_id = mt_rand(0, 0xffff); $value = mt_rand(-0xfffe, 0xffff); $vote = votingapi_set_vote($content_type, $content_id, $value, NULL, TRUE); $this->assertTrue(is_object($vote), t('@prefix Vote returned an object.', array('@prefix' => 'testVotingAPISetVoteNumericResults():'))); $this->assertEqual($vote->value, $value, t('@prefix Vote @name is the same.', array('@name' => 'value', '@prefix' => 'testVotingAPISetVoteNumericResults():'))); $this->assertEqual($vote->tag, VOTINGAPI_VALUE_DEFAULT_TAG, t('@prefix Vote @name is @value.', array('@name' => 'tag', '@value' => VOTINGAPI_VALUE_DEFAULT_TAG, '@prefix' => 'testVotingAPISetVoteNumericResults():'))); $this->assertEqual($vote->value_type, VOTINGAPI_VALUE_DEFAULT_TYPE, t('@prefix Vote @name is @value.', array('@name' => 'value_type', '@value' => VOTINGAPI_VALUE_DEFAULT_TYPE, '@prefix' => 'testVotingAPISetVoteNumericResults():'))); } /** * VASV-02A: Set a number of different numeric votes with same user. */ function testVotingAPISetVoteNumericVotes() { $this->assertTrue(TRUE, t('VASV-02A: Set a number of different numeric votes from same user.')); $content_type = $this->randomName(12, 'VTest_'); $this->_cleanupVotes[] = $content_type; $content_id = mt_rand(0, 0xffff); $num_votes = mt_rand(2, 20); for ($i = 0; $i < $num_votes; $i++) { $value = mt_rand(-0xfffe, 0xffff); votingapi_set_vote($content_type, $content_id, $value); } $results = votingapi_get_content_votes($content_type, $content_id); $this->assertEqual(count($results), 1, t('@prefix Number of votes is 1.', array('@prefix' => 'testVotingAPISetVoteNumericVotes():'))); foreach ($results as $votes) { $this->assertTrue(is_array($votes), t('@prefix Votes for a user is an array.', array('@prefix' => 'testVotingAPISetVoteNumericVotes():'))); foreach($votes as $vote) { $this->assertTrue(is_object($vote), t('@prefix Result returned an object.', array('@prefix' => 'testVotingAPISetVoteNumericVotes():'))); $this->assertEqual($vote->content_id, $content_id, t('@prefix Result @name is the same.', array('@name' => 'content_id', '@prefix' => 'testVotingAPISetVoteNumericVotes():'))); $this->assertEqual($vote->content_type, $content_type, t('@prefix Result @name is the same.', array('@name' => 'content_type', '@prefix' => 'testVotingAPISetVoteNumericVotes():'))); $this->assertEqual($vote->value, $value, t('@prefix Result @name is the same as the last set @name.', array('@name' => 'value', '@prefix' => 'testVotingAPISetVoteNumericVotes():'))); $this->assertEqual($vote->tag, VOTINGAPI_VALUE_DEFAULT_TAG, t('@prefix Result @name is set.', array('@name' => 'tag', '@prefix' => 'testVotingAPISetVoteNumericVotes():'))); $this->assertEqual($vote->value_type, VOTINGAPI_VALUE_DEFAULT_TYPE, t('@prefix Result @name is set.', array('@name' => 'value_type', '@prefix' => 'testVotingAPISetVoteNumericVotes():'))); } } } /** * VASV-02B: Set a number of different numeric votes with same user. */ function testVotingAPISetVoteNumericVotesUsers() { $this->assertTrue(TRUE, t('VASV-02B: Set a number of different numeric votes from different user.')); $content_type = $this->randomName(12, 'VTest_'); $this->_cleanupVotes[] = $content_type; $content_id = mt_rand(0, 0xffff); $num_votes = mt_rand(2, 20); $values = array(); $uids = array(); for ($i = 0; $i < $num_votes; $i++) { $value = mt_rand(-0xfffe, 0xffff); $uid = mt_rand(0, 0xffffff); $uids[$uid] = $value; votingapi_set_vote($content_type, $content_id, $value, $uid); } $results = votingapi_get_content_votes($content_type, $content_id); $this->assertEqual(count($results), count($uids), t('@prefix Number of votes is the same number as votes made.', array('@prefix' => 'testVotingAPISetVoteNumericVotesUsers():'))); foreach ($results as $uid => $votes) { $this->assertTrue(is_array($votes), t('@prefix Votes from a user is an array.', array('@prefix' => 'testVotingAPISetVoteNumericVotesUsers():'))); foreach($votes as $vote) { $this->assertTrue(is_object($vote), t('@prefix Result returned an object.', array('@prefix' => 'testVotingAPISetVoteNumericVotesUsers():'))); $this->assertEqual($vote->content_id, $content_id, t('@prefix Result @name is the same.', array('@name' => 'content_id', '@prefix' => 'testVotingAPISetVoteNumericVotesUsers():'))); $this->assertEqual($vote->content_type, $content_type, t('@prefix Result @name is the same.', array('@name' => 'content_type', '@prefix' => 'testVotingAPISetVoteNumericVotesUsers():'))); $this->assertEqual($vote->value, $uids[$uid], t('@prefix Result @name is the same as set.', array('@name' => 'value', '@prefix' => 'testVotingAPISetVoteNumericVotesUsers():'))); $this->assertEqual($vote->tag, VOTINGAPI_VALUE_DEFAULT_TAG, t('@prefix Result @name is set.', array('@name' => 'tag', '@prefix' => 'testVotingAPISetVoteNumericVotesUsers():'))); $this->assertEqual($vote->value_type, VOTINGAPI_VALUE_DEFAULT_TYPE, t('@prefix Result @name is set.', array('@name' => 'value_type', '@prefix' => 'testVotingAPISetVoteNumericVotesUsers():'))); } } } /** * VASV-03A: Set a number of different votes with same user different tags. */ function testVotingAPISetVoteVotesArray() { $this->assertTrue(TRUE, t('VASV-03A: Set a number of different votes with same user different tags; with votes in an array.')); $content_type = $this->randomName(12, 'VTest_'); $this->_cleanupVotes[] = $content_type; $content_id = mt_rand(0, 0xffff); $num_votes = mt_rand(2, 20); $values = array(); $tags = array(); $votes = array(); for ($i = 0; $i < $num_votes; $i++) { $vote = new stdClass(); $vote->value = mt_rand(-0xfffe, 0xffff); $vote->tag = $this->randomName(50); $vote->value_type = VOTINGAPI_VALUE_DEFAULT_TYPE; $votes[$vote->tag] = $vote; } votingapi_set_vote($content_type, $content_id, $votes); $results = votingapi_get_content_votes($content_type, $content_id); $this->assertEqual(count(reset($results)), count($votes), t('@prefix Number of votes is the same number as votes made.', array('@prefix' => 'testVotingAPISetVoteVotesArray():'))); foreach ($results as $uid => $result) { $this->assertTrue(is_array($result), t('@prefix Votes from a user is an array.', array('@prefix' => 'testVotingAPISetVoteVotesArray():'))); foreach($result as $vote) { $this->assertTrue(is_object($vote), t('@prefix Result returned an object.', array('@prefix' => 'testVotingAPISetVoteVotesArray():'))); $this->assertEqual($vote->content_id, $content_id, t('@prefix Result @name is the same.', array('@name' => 'content_id', '@prefix' => 'testVotingAPISetVoteVotesArray():'))); $this->assertEqual($vote->content_type, $content_type, t('@prefix Result @name is the same.', array('@name' => 'content_type', '@prefix' => 'testVotingAPISetVoteVotesArray():'))); $this->assertTrue(array_key_exists($vote->tag, $votes), t('@prefix Result @name do exists in set votes.', array('@name' => 'tag', '@prefix' => 'testVotingAPISetVoteVotesArray():'))); $this->assertEqual($vote->value, $votes[$vote->tag]->value, t('@prefix Result @name is the same as set.', array('@name' => 'value', '@prefix' => 'testVotingAPISetVoteVotesArray():'))); $this->assertEqual($vote->value_type, $votes[$vote->tag]->value_type, t('@prefix Result @name is the same as set.', array('@name' => 'value_type', '@prefix' => 'testVotingAPISetVoteVotesArray():'))); } } } /** * VASV-03B: Set a number of different votes with same user different tags. */ function testVotingAPISetVoteVotes() { $this->assertTrue(TRUE, t('VASV-03B: Set a number of different numeric votes with same user different tags; with votes as an object')); $content_type = $this->randomName(12, 'VTest_'); $this->_cleanupVotes[] = $content_type; $content_id = mt_rand(0, 0xffff); $num_votes = mt_rand(2, 20); $values = array(); $tags = array(); $votes = array(); for ($i = 0; $i < $num_votes; $i++) { $vote = new stdClass(); $vote->value = mt_rand(-0xfffe, 0xffff); $vote->tag = $this->randomName(50); $vote->value_type = VOTINGAPI_VALUE_DEFAULT_TYPE; $votes[$vote->tag] = $vote; votingapi_set_vote($content_type, $content_id, $vote); } $results = votingapi_get_content_votes($content_type, $content_id); $this->assertEqual(count(reset($results)), count($votes), t('@prefix Number of votes is the same number as votes made.', array('@prefix' => 'testVotingAPISetVoteVotes():'))); foreach ($results as $uid => $result) { $this->assertTrue(is_array($result), t('@prefix Votes from a user is an array.', array('@prefix' => 'testVotingAPISetVoteVotes():'))); foreach($result as $vote) { $this->assertTrue(is_object($vote), t('@prefix Result returned an object.', array('@prefix' => 'testVotingAPISetVoteVotes():'))); $this->assertEqual($vote->content_id, $content_id, t('@prefix Result @name is the same.', array('@name' => 'content_id', '@prefix' => 'testVotingAPISetVoteVotes():'))); $this->assertEqual($vote->content_type, $content_type, t('@prefix Result @name is the same.', array('@name' => 'content_type', '@prefix' => 'testVotingAPISetVoteVotes():'))); $this->assertTrue(array_key_exists($vote->tag, $votes), t('@prefix Result @name do exists in set votes.', array('@name' => 'tag', '@prefix' => 'testVotingAPISetVoteVotes():'))); $this->assertEqual($vote->value, $votes[$vote->tag]->value, t('@prefix Result @name is the same as set.', array('@name' => 'value', '@prefix' => 'testVotingAPISetVoteVotes():'))); $this->assertEqual($vote->value_type, $votes[$vote->tag]->value_type, t('@prefix Result @name is the same as set.', array('@name' => 'value_type', '@prefix' => 'testVotingAPISetVoteVotes():'))); } } } /** * VASV-04: Set a number of different numeric votes with getting calculated results back. */ function testVotingAPISetVoteNumericVotesResults() { $this->assertTrue(TRUE, t('VASV-03: Set a number of different numeric votes with getting calculated results back.')); $content_type = $this->randomName(12, 'VTest_'); $this->_cleanupVotes[] = $content_type; $content_id = mt_rand(0, 0xffff); $num_votes = mt_rand(1, 20); for ($i = 0; $i < $num_votes; $i++) { $value = mt_rand(-0xfffe, 0xffff); votingapi_set_vote($content_type, $content_id, $value); } $results = votingapi_get_voting_results($content_type, $content_id); $this->assertVotingAPIResults('testVotingAPISetVoteNumericVotesResults():', $content_type, $content_id, $results); } function assertVotingAPIResults($prefix, $content_type, $content_id, $results) { $this->assertTrue(is_array($results), t('@prefix Results are array of results.', array('@prefix' => $prefix))); foreach ($results as $vote) { $this->assertTrue(is_object($vote), t('@prefix Result returned an object.', array('@prefix' => $prefix))); $this->assertEqual($vote->content_id, $content_id, t('@prefix Result @name is the same.', array('@name' => 'content_id', '@prefix' => $prefix))); $this->assertEqual($vote->content_type, $content_type, t('Result @name is the same.', array('@name' => 'content_type', '@prefix' => $prefix))); $this->assertTrue(isset($vote->value), t('@prefix Result @name is set.', array('@name' => 'value', '@prefix' => $prefix))); $this->assertTrue(isset($vote->tag), t('@prefix Result @name is set.', array('@name' => 'tag', '@prefix' => $prefix))); $this->assertTrue(isset($vote->function), t('@prefix Result @name is set.', array('@name' => 'function', '@prefix' => $prefix))); $this->assertTrue(isset($vote->value_type), t('@prefix Result @name is set.', array('@name' => 'value_type', '@prefix' => $prefix))); $this->assertTrue(isset($vote->timestamp), t('@prefix Result @name is set.', array('@name' => 'timestamp', '@prefix' => $prefix))); $this->assertTrue(isset($vote->vote_cache_id), t('@prefix Result @name is set.', array('@name' => 'vote_cache_id', '@prefix' => $prefix))); } } }