Projet

Général

Profil

Paste
Télécharger (6,97 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / votingapi / tests / votingapi.test @ f419e8de

1
<?php
2

    
3
/**
4
 * @file
5
 * Test file for VotingAPI module.
6
 */
7

    
8
class VotingAPITestCase extends DrupalWebTestCase {
9

    
10
  public static function getInfo() {
11
    return array(
12
      'name' => 'Voting API',
13
      'description' => 'Voting API',
14
      'group' => 'Voting API Tests',
15
    );
16
  }
17

    
18
  function setUp() {
19
    parent::setUp('votingapi');
20
    // Create a new page
21
    $node = new stdClass();
22
    $node->title = '';
23
    $node->teaser = t('Teaser text');
24
    $node->body = t('Here is the body of the page');
25
    $node->uid = 1;
26
    $node->type = 'page';
27
    $node->status = 1;
28
    $node->promote = 0;
29
    node_save($node);
30
    variable_set('votingapi_nid1', $node->nid);
31
    $node->title = t('Node @id', array('@id' => $node->nid));
32
    node_save($node);
33
  }
34

    
35
  function tearDown() {
36
    $nid = variable_get('votingapi_nid1', NULL);
37
    if ($nid) {
38
      node_delete($nid);
39
      variable_del('votingapi_nid1');
40
    }
41
    parent::tearDown();
42
  }
43

    
44
  /**
45
   * Ensure that the optional fields are truly optional.
46
   */
47
  function testMinimalAdd() {
48
    $nid = variable_get('votingapi_nid1', NULL);
49
    $value = '85';
50
    // The minimum required fields according to the documentation are
51
    // entity_id and value.
52
    $vote = array(
53
      'entity_id' => $nid,
54
      'value' => $value,
55
    );
56
    try {
57
      $result = votingapi_add_votes($vote);
58
      // Test that the result has its fields set appropriately.
59
      $this->validateVote('testMinimalAdd()', $result, $nid, array($value));
60
      $this->assertTrue(REQUEST_TIME - $result[0]['timestamp'] < 2, t('The timestamp should be less than 2 seconds ago.'));
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(
75
      'entity_id' => $nid,
76
      'value' => $value,
77
      'entity_type' => 'node',
78
    );
79
    try {
80
      $result = votingapi_add_votes($vote);
81
      // Test that the result has its fields set appropriately.
82
      $this->validateVote("testAddVote()", $result, $nid, $value, 'node', $user->uid);
83
      $this->assertTrue(REQUEST_TIME - $result[0]['timestamp'] < 2, t('The timestamp should be less than 2 seconds ago.'));
84
    } catch (Exception $e) {
85
      $this->fail('The votingapi_add_votes threw an error during the "votingapi_add_votes" call.');
86
      return;
87
    }
88
    // Load the vote back in and verify it matches.
89
    $vote_results = votingapi_recalculate_results('node', $nid);
90
    $this->validateVoteCounts('testAddVote()', $vote_results, $nid, array($value));
91
  }
92

    
93
  /**
94
   * Add multiple votes and ensure that the vote calculations are working.
95
   */
96
  function testAddMultipleVotes() {
97
    $users = array();
98
    $users[] = $this->drupalCreateUser();
99
    $users[] = $this->drupalCreateUser();
100
    $users[] = $this->drupalCreateUser();
101
    $nid = variable_get('votingapi_nid1', NULL);
102
    $values = array(72, 13, 27);
103
    $votes = array();
104
    try {
105
      for ($index = 0; $index < count($values); $index++) {
106
        $votes[$index] = array();
107
        $votes[$index]['entity_type'] = 'node';
108
        $votes[$index]['entity_id'] = $nid;
109
        $votes[$index]['uid'] = $users[$index]->uid;
110
        $votes[$index]['value'] = $values[$index];
111
      }
112
      $result = votingapi_add_votes($votes);
113
      // Test that the result has its fields set appropriately.
114
      $this->validateVote("testAddMultipleVotes()", $result, $nid, $values);
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
}