1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Test file for VotingAPI module.
|
6
|
*/
|
7
|
|
8
|
class VotingAPITestCase extends DrupalWebTestCase {
|
9
|
public static function getInfo() {
|
10
|
return array(
|
11
|
'name' => 'Voting API',
|
12
|
'description' => 'Voting API',
|
13
|
'group' => 'Voting API Tests',
|
14
|
);
|
15
|
}
|
16
|
|
17
|
function setUp() {
|
18
|
parent::setUp('votingapi');
|
19
|
// Create a new page
|
20
|
$node = new stdClass();
|
21
|
$node->title = '';
|
22
|
$node->teaser = t('Teaser text');
|
23
|
$node->body = t('Here is the body of the page');
|
24
|
$node->uid = 1;
|
25
|
$node->type = 'page';
|
26
|
$node->status = 1;
|
27
|
$node->promote = 0;
|
28
|
node_save($node);
|
29
|
variable_set('votingapi_nid1', $node->nid);
|
30
|
$node->title = t('Node @id', array('@id' => $node->nid));
|
31
|
node_save($node);
|
32
|
}
|
33
|
|
34
|
function tearDown() {
|
35
|
$nid = variable_get('votingapi_nid1', NULL);
|
36
|
if ($nid) {
|
37
|
node_delete($nid);
|
38
|
variable_del('votingapi_nid1');
|
39
|
}
|
40
|
parent::tearDown();
|
41
|
}
|
42
|
|
43
|
/**
|
44
|
* Ensure that the optional fields are truly optional.
|
45
|
*/
|
46
|
function testMinimalAdd() {
|
47
|
$nid = variable_get('votingapi_nid1', NULL);
|
48
|
$value = '85';
|
49
|
// The minimum required fields according to the documentation are
|
50
|
// entity_id and value.
|
51
|
$vote = array(
|
52
|
'entity_id' => $nid,
|
53
|
'value' => $value
|
54
|
);
|
55
|
try {
|
56
|
$result = votingapi_add_votes($vote);
|
57
|
// Test that the result has its fields set appropriately.
|
58
|
$this->validateVote('testMinimalAdd()', $result, $nid, array($value));
|
59
|
$this->assertTrue(REQUEST_TIME - $result[0]['timestamp'] < 2, t('The timestamp should be less than 2 seconds ago.'));
|
60
|
}
|
61
|
catch (Exception $e) {
|
62
|
$this->fail(t('Could not add a vote with only entity_id and value.'));
|
63
|
return;
|
64
|
}
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* Add a vote and ensure that the vote was stored properly.
|
69
|
*/
|
70
|
function testAddVote() {
|
71
|
global $user;
|
72
|
$value = '7';
|
73
|
$nid = variable_get('votingapi_nid1', NULL);
|
74
|
$vote = array('entity_id' => $nid,
|
75
|
'value' => $value,
|
76
|
'entity_type' => 'node');
|
77
|
try {
|
78
|
$result = votingapi_add_votes($vote);
|
79
|
// Test that the result has its fields set appropriately.
|
80
|
$this->validateVote("testAddVote()", $result, $nid, $value, 'node', $user->uid);
|
81
|
$this->assertTrue(REQUEST_TIME - $result[0]['timestamp'] < 2, t('The timestamp should be less than 2 seconds ago.'));
|
82
|
}
|
83
|
catch (Exception $e) {
|
84
|
$this->fail('The votingapi_add_votes threw an error during the "votingapi_add_votes" call.');
|
85
|
return;
|
86
|
}
|
87
|
// Load the vote back in and verify it matches.
|
88
|
$vote_results = votingapi_recalculate_results('node', $nid);
|
89
|
$this->validateVoteCounts('testAddVote()', $vote_results, $nid, array($value));
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* Add multiple votes and ensure that the vote calculations are working.
|
94
|
*/
|
95
|
function testAddMultipleVotes() {
|
96
|
$users = array();
|
97
|
$users[] = $this->drupalCreateUser();
|
98
|
$users[] = $this->drupalCreateUser();
|
99
|
$users[] = $this->drupalCreateUser();
|
100
|
$nid = variable_get('votingapi_nid1', NULL);
|
101
|
$values = array(72, 13, 27);
|
102
|
$votes = array();
|
103
|
try {
|
104
|
for ($index = 0; $index < count($values); $index++) {
|
105
|
$votes[$index] = array();
|
106
|
$votes[$index]['entity_type'] = 'node';
|
107
|
$votes[$index]['entity_id'] = $nid;
|
108
|
$votes[$index]['uid'] = $users[$index]->uid;
|
109
|
$votes[$index]['value'] = $values[$index];
|
110
|
}
|
111
|
$result = votingapi_add_votes($votes);
|
112
|
// Test that the result has its fields set appropriately.
|
113
|
$this->validateVote("testAddMultipleVotes()", $result, $nid, $values);
|
114
|
}
|
115
|
catch (Exception $e) {
|
116
|
$this->fail('The votingapi_add_votes threw an error during the "votingapi_add_votes" call.');
|
117
|
return;
|
118
|
}
|
119
|
// Load the vote back in and verify it matches.
|
120
|
$vote_results = votingapi_recalculate_results('node', $nid);
|
121
|
$this->validateVoteCounts('testAddVote()', $vote_results, $nid, $values);
|
122
|
}
|
123
|
|
124
|
function validateVote($prefix, $vote, $entity_id, $value, $entity_type = 'node',
|
125
|
$uid = NULL, $value_type = 'percent', $tag = 'vote', $vote_source = NULL) {
|
126
|
global $user;
|
127
|
if ($vote_source == NULL) {
|
128
|
$vote_source = ip_address();
|
129
|
}
|
130
|
$prefix_array = array('@prefix' => $prefix);
|
131
|
for ($index = 0; $index < count($vote); $index++) {
|
132
|
$this->assertTrue($vote[$index]['entity_id'] == $entity_id, t('@prefix: entity_id should match.', $prefix_array));
|
133
|
$this->assertTrue($vote[$index]['value'] == $value[$index], t('@prefix: value should match.', $prefix_array));
|
134
|
$this->assertTrue($vote[$index]['entity_type'] == $entity_type, t('@prefix: entity_type should match, default = "node".', $prefix_array));
|
135
|
$this->assertTrue($vote[$index]['value_type'] == $value_type, t('@prefix: value_type should match, default= "percent".', $prefix_array));
|
136
|
$this->assertTrue($vote[$index]['tag'] == $tag, t('@prefix: tag should match, default = "vote".', $prefix_array));
|
137
|
$this->assertTrue($vote[$index]['vote_source'] == $vote_source, t('@prefix: vote_source should match, default = ip address.', $prefix_array));
|
138
|
if ($uid != NULL) {
|
139
|
$this->assertTrue($vote[0]['uid'] == $uid, t('@prefix: uid should match.', $prefix_array));
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
|
144
|
function validateVoteCounts($prefix, $votes, $entity_id, $values, $entity_type = 'node',
|
145
|
$value_type = 'percent', $tag = 'vote') {
|
146
|
$count_summary = 0;
|
147
|
$average_summary = 1;
|
148
|
$count = 0.0;
|
149
|
$sum = 0.0;
|
150
|
foreach ($values as $value) {
|
151
|
$sum += $value;
|
152
|
$count++;
|
153
|
}
|
154
|
$average = $sum / $count;
|
155
|
$prefix_array = array('@prefix' => $prefix);
|
156
|
foreach ($votes as $summary) {
|
157
|
if ($summary['function'] == 'count') {
|
158
|
$summary_desc = 'count summary';
|
159
|
$prefix_array['@summary_desc'] = $summary_desc;
|
160
|
$this->assertTrue($summary['value'] == $count,
|
161
|
t('@prefix: (@summary_desc) value should match the number of values.', $prefix_array));
|
162
|
}
|
163
|
elseif ($summary['function'] == 'average') {
|
164
|
$summary_desc = 'average summary';
|
165
|
$prefix_array['@summary_desc'] = $summary_desc;
|
166
|
$this->assertTrue($summary['value'] == $average,
|
167
|
t('@prefix: (@summary_desc) value should match the average of values', $prefix_array));
|
168
|
}
|
169
|
else {
|
170
|
$prefix_array['@summary'] = $summary['function'];
|
171
|
$prefix_array['@summary_desc'] = $summary['function'] . ' summary';
|
172
|
$this->assertFalse(TRUE, t('@prefix: Unknown summary type @summary.', $prefix_array));
|
173
|
}
|
174
|
$this->assertTrue($summary['entity_type'] == $entity_type,
|
175
|
t('@prefix: (@summary_desc) entity_type should match, default = "node"', $prefix_array));
|
176
|
$this->assertTrue($summary['entity_id'] == $entity_id,
|
177
|
t('@prefix: (@summary_desc) entity_id should match', $prefix_array));
|
178
|
$this->assertTrue($summary['value_type'] == $value_type,
|
179
|
t('@prefix: (@summary_desc) value_type should match.', $prefix_array));
|
180
|
$this->assertTrue($summary['tag'] == $tag,
|
181
|
t('@prefix: (@summary_desc) tag should match', $prefix_array));
|
182
|
}
|
183
|
}
|
184
|
}
|