t('VotingAPI voting - add vote'), 'desc' => t('Unit testing of votingapi_add_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'); } /** * VAAV-01: Add a vote with defaults. */ function testVotingAPIAddVoteDefaults() { global $user; $this->assertTrue(TRUE, t('VAAV-01: Add a vote with defaults.')); $content_type = $this->randomName(12, 'VTest_'); $this->_cleanupVotes[] = $content_type; $content_id = mt_rand(0, 0xffff); $value = mt_rand(-0xfffe, 0xffff); $vote = votingapi_add_vote($content_type, $content_id, $value); $this->assertVotingAPIVoteAdded($content_type, $content_id, $value, VOTINGAPI_VALUE_DEFAULT_TYPE, VOTINGAPI_VALUE_DEFAULT_TAG, $user->uid, $vote); } /** * VAAV-02: Add a vote with custom values. */ function testVotingAPIAddVoteCustom() { global $user; $this->assertTrue(TRUE, t('VAAV-02: Add a vote with custom values.')); $content_type = $this->randomName(mt_rand(1, 50), 'VTest_'); $this->_cleanupVotes[] = $content_type; $content_id = mt_rand(0, 0xffff); $value = mt_rand(-0xfffe, 0xffff); $value_type = $this->randomName(mt_rand(1, 50), 'VTest_'); $tag = $this->randomName(mt_rand(1, 50), 'VTest_'); // VotingAPI should NOT return a valid item, in case some fields cannot be pushed into database. if ($vote = votingapi_add_vote($content_type, $content_id, $value, $value_type, $tag)) { $this->assertVotingAPIVoteAdded($content_type, $content_id, $value, $value_type, $tag, $user->uid, $vote); } } function assertVotingAPIVoteAdded($content_type, $content_id, $value, $value_type, $tag, $uid, $vote) { $vote_id = $vote->vote_id; $this->assertTrue(is_object($vote), t('Vote is an object.')); $this->assertEqual($vote->value, $value, t('Vote @name is @value.', array('@name' => 'value', '@value' => $value))); $this->assertEqual($vote->tag, $tag, t('Vote @name is @value.', array('@name' => 'tag', '@value' => $tag))); $this->assertEqual($vote->value_type, $value_type, t('Vote @name is @value.', array('@name' => 'value_type', '@value' => $value_type))); $this->assertEqual($vote->content_id, $content_id, t('Vote @name is the same.', array('@name' => 'content_id'))); $this->assertEqual($vote->content_type, $content_type, t('Vote @name is @value.', array('@name' => 'content_type', '@value' => $content_type))); $this->assertEqual($vote->uid, $uid, t('Vote @name is the same.', array('@name' => 'uid'))); $this->assertTrue(isset($vote->vote_id) && is_numeric($vote->vote_id), t('Result @name is set and numeric.', array('@name' => 'vote_id'))); $this->_cleanupVotes[] = $vote->content_type; $this->assertTrue(TRUE, t('Fetching previous vote with the same settings as set by the vote.')); $vote = votingapi_get_vote($content_type, $content_id, $value_type, $tag, $uid); $this->assertTrue(is_object($vote), t('Result is an object.')); $this->assertEqual($vote->value, $value, t('Result @name is @value.', array('@name' => 'value', '@value' => $value))); $this->assertEqual($vote->tag, $tag, t('Result @name is @value.', array('@name' => 'tag', '@value' => $tag))); $this->assertEqual($vote->value_type, $value_type, t('Result @name is @value.', array('@name' => 'value_type', '@value' => $value_type))); $this->assertEqual($vote->content_id, $content_id, t('Result @name is the same.', array('@name' => 'content_id'))); $this->assertEqual($vote->content_type, $content_type, t('Result @name is @value.', array('@name' => 'content_type', '@value' => $content_type))); $this->assertEqual($vote->uid, $uid, t('Result @name is the same.', array('@name' => 'uid'))); $this->assertEqual($vote->vote_id, $vote_id, t('Result @name is is the same.', array('@name' => 'vote_id'))); $this->_cleanupVotes[] = $vote->content_type; $this->assertTrue(TRUE, t('Fetching previous vote using vote id.')); $vote = votingapi_get_vote_by_id($vote_id); $this->assertTrue(is_object($vote), t('Result is an object.')); $this->assertEqual($vote->value, $value, t('Result @name is @value.', array('@name' => 'value', '@value' => $value))); $this->assertEqual($vote->tag, $tag, t('Result @name is @value.', array('@name' => 'tag', '@value' => $tag))); $this->assertEqual($vote->value_type, $value_type, t('Result @name is @value.', array('@name' => 'value_type', '@value' => $value_type))); $this->assertEqual($vote->content_id, $content_id, t('Result @name is the same.', array('@name' => 'content_id'))); $this->assertEqual($vote->content_type, $content_type, t('Result @name is @value.', array('@name' => 'content_type', '@value' => $content_type))); $this->assertEqual($vote->uid, $uid, t('Result @name is the same.', array('@name' => 'uid'))); $this->assertEqual($vote->vote_id, $vote_id, t('Result @name is is the same.', array('@name' => 'vote_id'))); $this->_cleanupVotes[] = $vote->content_type; } }