votingapi.test
6.98 KB
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
<?php
/**
* @file
* Test file for VotingAPI module.
*/
class VotingAPITestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Voting API',
'description' => 'Voting API',
'group' => 'Voting API Tests',
);
}
function setUp() {
parent::setUp('votingapi');
// Create a new page
$node = new stdClass();
$node->title = '';
$node->teaser = t('Teaser text');
$node->body = t('Here is the body of the page');
$node->uid = 1;
$node->type = 'page';
$node->status = 1;
$node->promote = 0;
node_save($node);
variable_set('votingapi_nid1', $node->nid);
$node->title = t('Node @id', array('@id' => $node->nid));
node_save($node);
}
function tearDown() {
$nid = variable_get('votingapi_nid1', NULL);
if ($nid) {
node_delete($nid);
variable_del('votingapi_nid1');
}
parent::tearDown();
}
/**
* Ensure that the optional fields are truly optional.
*/
function testMinimalAdd() {
$nid = variable_get('votingapi_nid1', NULL);
$value = '85';
// The minimum required fields according to the documentation are
// entity_id and value.
$vote = array(
'entity_id' => $nid,
'value' => $value
);
try {
$result = votingapi_add_votes($vote);
// Test that the result has its fields set appropriately.
$this->validateVote('testMinimalAdd()', $result, $nid, array($value));
$this->assertTrue(REQUEST_TIME - $result[0]['timestamp'] < 2, t('The timestamp should be less than 2 seconds ago.'));
}
catch (Exception $e) {
$this->fail(t('Could not add a vote with only entity_id and value.'));
return;
}
}
/**
* Add a vote and ensure that the vote was stored properly.
*/
function testAddVote() {
global $user;
$value = '7';
$nid = variable_get('votingapi_nid1', NULL);
$vote = array('entity_id' => $nid,
'value' => $value,
'entity_type' => 'node');
try {
$result = votingapi_add_votes($vote);
// Test that the result has its fields set appropriately.
$this->validateVote("testAddVote()", $result, $nid, $value, 'node', $user->uid);
$this->assertTrue(REQUEST_TIME - $result[0]['timestamp'] < 2, t('The timestamp should be less than 2 seconds ago.'));
}
catch (Exception $e) {
$this->fail('The votingapi_add_votes threw an error during the "votingapi_add_votes" call.');
return;
}
// Load the vote back in and verify it matches.
$vote_results = votingapi_recalculate_results('node', $nid);
$this->validateVoteCounts('testAddVote()', $vote_results, $nid, array($value));
}
/**
* Add multiple votes and ensure that the vote calculations are working.
*/
function testAddMultipleVotes() {
$users = array();
$users[] = $this->drupalCreateUser();
$users[] = $this->drupalCreateUser();
$users[] = $this->drupalCreateUser();
$nid = variable_get('votingapi_nid1', NULL);
$values = array(72, 13, 27);
$votes = array();
try {
for ($index = 0; $index < count($values); $index++) {
$votes[$index] = array();
$votes[$index]['entity_type'] = 'node';
$votes[$index]['entity_id'] = $nid;
$votes[$index]['uid'] = $users[$index]->uid;
$votes[$index]['value'] = $values[$index];
}
$result = votingapi_add_votes($votes);
// Test that the result has its fields set appropriately.
$this->validateVote("testAddMultipleVotes()", $result, $nid, $values);
}
catch (Exception $e) {
$this->fail('The votingapi_add_votes threw an error during the "votingapi_add_votes" call.');
return;
}
// Load the vote back in and verify it matches.
$vote_results = votingapi_recalculate_results('node', $nid);
$this->validateVoteCounts('testAddVote()', $vote_results, $nid, $values);
}
function validateVote($prefix, $vote, $entity_id, $value, $entity_type = 'node',
$uid = NULL, $value_type = 'percent', $tag = 'vote', $vote_source = NULL) {
global $user;
if ($vote_source == NULL) {
$vote_source = ip_address();
}
$prefix_array = array('@prefix' => $prefix);
for ($index = 0; $index < count($vote); $index++) {
$this->assertTrue($vote[$index]['entity_id'] == $entity_id, t('@prefix: entity_id should match.', $prefix_array));
$this->assertTrue($vote[$index]['value'] == $value[$index], t('@prefix: value should match.', $prefix_array));
$this->assertTrue($vote[$index]['entity_type'] == $entity_type, t('@prefix: entity_type should match, default = "node".', $prefix_array));
$this->assertTrue($vote[$index]['value_type'] == $value_type, t('@prefix: value_type should match, default= "percent".', $prefix_array));
$this->assertTrue($vote[$index]['tag'] == $tag, t('@prefix: tag should match, default = "vote".', $prefix_array));
$this->assertTrue($vote[$index]['vote_source'] == $vote_source, t('@prefix: vote_source should match, default = ip address.', $prefix_array));
if ($uid != NULL) {
$this->assertTrue($vote[0]['uid'] == $uid, t('@prefix: uid should match.', $prefix_array));
}
}
}
function validateVoteCounts($prefix, $votes, $entity_id, $values, $entity_type = 'node',
$value_type = 'percent', $tag = 'vote') {
$count_summary = 0;
$average_summary = 1;
$count = 0.0;
$sum = 0.0;
foreach ($values as $value) {
$sum += $value;
$count++;
}
$average = $sum / $count;
$prefix_array = array('@prefix' => $prefix);
foreach ($votes as $summary) {
if ($summary['function'] == 'count') {
$summary_desc = 'count summary';
$prefix_array['@summary_desc'] = $summary_desc;
$this->assertTrue($summary['value'] == $count,
t('@prefix: (@summary_desc) value should match the number of values.', $prefix_array));
}
elseif ($summary['function'] == 'average') {
$summary_desc = 'average summary';
$prefix_array['@summary_desc'] = $summary_desc;
$this->assertTrue($summary['value'] == $average,
t('@prefix: (@summary_desc) value should match the average of values', $prefix_array));
}
else {
$prefix_array['@summary'] = $summary['function'];
$prefix_array['@summary_desc'] = $summary['function'] . ' summary';
$this->assertFalse(TRUE, t('@prefix: Unknown summary type @summary.', $prefix_array));
}
$this->assertTrue($summary['entity_type'] == $entity_type,
t('@prefix: (@summary_desc) entity_type should match, default = "node"', $prefix_array));
$this->assertTrue($summary['entity_id'] == $entity_id,
t('@prefix: (@summary_desc) entity_id should match', $prefix_array));
$this->assertTrue($summary['value_type'] == $value_type,
t('@prefix: (@summary_desc) value_type should match.', $prefix_array));
$this->assertTrue($summary['tag'] == $tag,
t('@prefix: (@summary_desc) tag should match', $prefix_array));
}
}
}