/[drupal]/drupal/modules/poll/poll.test
ViewVC logotype

Contents of /drupal/modules/poll/poll.test

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.25 - (show annotations) (download) (as text)
Fri Oct 16 23:48:37 2009 UTC (5 weeks, 3 days ago) by webchick
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10
Changes since 1.24: +2 -2 lines
File MIME type: text/x-php
#606640 by eojthebrave and sun: Use proper menu router paths for the block module.
1 <?php
2 // $Id: poll.test,v 1.24 2009/10/11 03:07:19 webchick Exp $
3
4 /**
5 * @file
6 * Tests for the poll module.
7 */
8
9 class PollTestCase extends DrupalWebTestCase {
10
11 /**
12 * Creates a poll.
13 *
14 * @param string $title The title of the poll.
15 * @param array $choices Choices.
16 * @param boolean $test_preview Whether to test if the preview is working or not.
17 * @return integer The nid of the created poll, or FALSE on error.
18 */
19 function pollCreate($title, $choices, $test_preview = TRUE) {
20 $this->assertTrue(TRUE, 'Create a poll');
21
22 $web_user = $this->drupalCreateUser(array('create poll content', 'access content'));
23 $this->drupalLogin($web_user);
24
25 // Get the form first to initialize the state of the internal browser
26 $this->drupalGet('node/add/poll');
27
28 // Prepare a form with two choices
29 list($edit, $index) = $this->_pollGenerateEdit($title, $choices);
30
31 if (count($choices) > 2) {
32 // Re-submit the form while the choices are all in
33 while ($index < count($choices)) {
34 $this->drupalPost(NULL, $edit, t('More choices'));
35 list($edit, $index) = $this->_pollGenerateEdit($title, $choices, $index);
36 }
37 }
38
39 if ($test_preview) {
40 $this->drupalPost(NULL, $edit, t('Preview'));
41 foreach ($choices as $k => $choice_text) {
42 $this->assertRaw($choice_text, t('Choice @choice found was in preview.', array('@choice' => $k)));
43 }
44 list($edit, $index) = $this->_pollGenerateEdit($title, $choices, $index);
45 }
46
47 $this->drupalPost(NULL, $edit, t('Save'));
48 $node = $this->drupalGetNodeByTitle($title);
49 $this->assertText(t('@type @title has been created.', array('@type' => node_type_get_name('poll'), '@title' => $title)), 'Poll has been created.');
50 $this->assertTrue($node->nid, t('Poll has been found in the database.'));
51
52 return isset($node->nid) ? $node->nid : FALSE;
53 }
54
55 function _pollGenerateEdit($title, $choices, $index = 0) {
56 $max_new_choices = $index == 0 ? 2 : 5;
57 $already_submitted_choices = array_slice($choices, 0, $index);
58 $new_choices = array_values(array_slice($choices, $index, $max_new_choices));
59
60 $langcode = FIELD_LANGUAGE_NONE;
61 $edit = array(
62 "title[$langcode][0][value]" => $title
63 );
64 foreach ($already_submitted_choices as $k => $text) {
65 $edit['choice[chid:' . $k . '][chtext]'] = $text;
66 }
67 foreach ($new_choices as $k => $text) {
68 $edit['choice[new:' . $k . '][chtext]'] = $text;
69 }
70 return array($edit, count($already_submitted_choices) + count($new_choices));
71 }
72
73 function _generateChoices($count = 7) {
74 $choices = array();
75 for ($i = 1; $i <= $count; $i++) {
76 $choices[] = $this->randomName();
77 }
78 return $choices;
79 }
80
81 function pollUpdate($nid, $title, $edit) {
82 // Edit the poll node.
83 $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
84 $this->assertText(t('@type @title has been updated.', array('@type' => node_type_get_name('poll'), '@title' => $title)), 'Poll has been updated.');
85 }
86 }
87
88 class PollCreateTestCase extends PollTestCase {
89 public static function getInfo() {
90 return array(
91 'name' => 'Poll create',
92 'description' => 'Adds "more choices", previews and creates a poll.',
93 'group' => 'Poll'
94 );
95 }
96
97 function setUp() {
98 parent::setUp('poll');
99 }
100
101 function testPollCreate() {
102 $title = $this->randomName();
103 $choices = $this->_generateChoices(7);
104 $this->pollCreate($title, $choices, TRUE);
105
106 // Verify poll appears on 'poll' page.
107 $this->drupalGet('poll');
108 $this->assertText($title, 'Poll appears in poll list.');
109 $this->assertText('open', 'Poll is active.');
110
111 // Click on the poll title to go to node page.
112 $this->clickLink($title);
113 $this->assertText('Total votes: 0', 'Link to poll correct.');
114 }
115
116 function testPollClose() {
117 $content_user = $this->drupalCreateUser(array('create poll content', 'edit any poll content', 'access content'));
118 $vote_user = $this->drupalCreateUser(array('cancel own vote', 'inspect all votes', 'vote on polls', 'access content'));
119
120 // Create poll.
121 $title = $this->randomName();
122 $choices = $this->_generateChoices(7);
123 $poll_nid = $this->pollCreate($title, $choices, FALSE);
124
125 $this->drupalLogout();
126 $this->drupalLogin($content_user);
127
128 // Edit the poll node and close the poll.
129 $close_edit = array('active' => 0);
130 $this->pollUpdate($poll_nid, $title, $close_edit);
131
132 // Verify 'Vote' button no longer appears.
133 $this->drupalGet('node/' . $poll_nid);
134 $elements = $this->xpath('//input[@id="edit-vote"]');
135 $this->assertTrue(empty($elements), t("Vote button doesn't appear."));
136
137 // Verify status on 'poll' page is 'closed'.
138 $this->drupalGet('poll');
139 $this->assertText($title, 'Poll appears in poll list.');
140 $this->assertText('closed', 'Poll is closed.');
141
142 // Edit the poll node and re-activate.
143 $open_edit = array('active' => 1);
144 $this->pollUpdate($poll_nid, $title, $open_edit);
145
146 // Vote on the poll.
147 $this->drupalLogout();
148 $this->drupalLogin($vote_user);
149 $vote_edit = array('choice' => '1');
150 $this->drupalPost('node/' . $poll_nid, $vote_edit, t('Vote'));
151 $this->assertText('Your vote was recorded.', 'Your vote was recorded.');
152 $elements = $this->xpath('//input[@value="Cancel your vote"]');
153 $this->assertTrue(isset($elements[0]), t("'Cancel your vote' button appears."));
154
155 // Edit the poll node and close the poll.
156 $this->drupalLogout();
157 $this->drupalLogin($content_user);
158 $close_edit = array('active' => 0);
159 $this->pollUpdate($poll_nid, $title, $close_edit);
160
161 // Verify 'Cancel your vote' button no longer appears.
162 $this->drupalGet('node/' . $poll_nid);
163 $elements = $this->xpath('//input[@value="Cancel your vote"]');
164 $this->assertTrue(empty($elements), t("'Cancel your vote' button no longer appears."));
165 }
166 }
167
168 class PollVoteTestCase extends PollTestCase {
169 public static function getInfo() {
170 return array(
171 'name' => 'Poll vote',
172 'description' => 'Vote on a poll',
173 'group' => 'Poll'
174 );
175 }
176
177 function setUp() {
178 parent::setUp('poll');
179 }
180
181 function tearDown() {
182 parent::tearDown();
183 }
184
185 function testPollVote() {
186 $title = $this->randomName();
187 $choices = $this->_generateChoices(7);
188 $poll_nid = $this->pollCreate($title, $choices, FALSE);
189 $this->drupalLogout();
190
191 $vote_user = $this->drupalCreateUser(array('cancel own vote', 'inspect all votes', 'vote on polls', 'access content'));
192 $restricted_vote_user = $this->drupalCreateUser(array('vote on polls', 'access content'));
193
194 $this->drupalLogin($vote_user);
195
196 // Record a vote for the first choice.
197 $edit = array(
198 'choice' => '1',
199 );
200 $this->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
201 $this->assertText('Your vote was recorded.', 'Your vote was recorded.');
202 $this->assertText('Total votes: 1', 'Vote count updated correctly.');
203 $elements = $this->xpath('//input[@value="Cancel your vote"]');
204 $this->assertTrue(isset($elements[0]), t("'Cancel your vote' button appears."));
205
206 $this->drupalGet("node/$poll_nid/votes");
207 $this->assertText(t('This table lists all the recorded votes for this poll. If anonymous users are allowed to vote, they will be identified by the IP address of the computer they used when they voted.'), 'Vote table text.');
208 $this->assertText($choices[0], 'Vote recorded');
209
210 // Ensure poll listing page has correct number of votes.
211 $this->drupalGet('poll');
212 $this->assertText($title, 'Poll appears in poll list.');
213 $this->assertText('1 vote', 'Poll has 1 vote.');
214
215 // Cancel a vote.
216 $this->drupalPost('node/' . $poll_nid, array(), t('Cancel your vote'));
217 $this->assertText('Your vote was cancelled.', 'Your vote was cancelled.');
218 $this->assertNoText('Cancel your vote', "Cancel vote button doesn't appear.");
219
220 $this->drupalGet("node/$poll_nid/votes");
221 $this->assertNoText($choices[0], 'Vote cancelled');
222
223 // Ensure poll listing page has correct number of votes.
224 $this->drupalGet('poll');
225 $this->assertText($title, 'Poll appears in poll list.');
226 $this->assertText('0 votes', 'Poll has 0 votes.');
227
228 // Log in as a user who can only vote on polls.
229 $this->drupalLogout();
230 $this->drupalLogin($restricted_vote_user);
231
232 // Vote on a poll.
233 $edit = array(
234 'choice' => '1',
235 );
236 $this->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
237 $this->assertText('Your vote was recorded.', 'Your vote was recorded.');
238 $this->assertText('Total votes: 1', 'Vote count updated correctly.');
239 $elements = $this->xpath('//input[@value="Cancel your vote"]');
240 $this->assertTrue(empty($elements), t("'Cancel your vote' button does not appear."));
241 }
242 }
243
244 class PollBlockTestCase extends PollTestCase {
245 public static function getInfo() {
246 return array(
247 'name' => 'Block availability',
248 'description' => 'Check if the most recent poll block is available.',
249 'group' => 'Poll',
250 );
251 }
252
253 function setUp() {
254 parent::setUp('poll');
255
256 // Create and login user
257 $admin_user = $this->drupalCreateUser(array('administer blocks'));
258 $this->drupalLogin($admin_user);
259 }
260
261 function testRecentBlock() {
262 // Set block title to confirm that the interface is available.
263 $this->drupalPost('admin/structure/block/manage/poll/recent/configure', array('title' => $this->randomName(8)), t('Save block'));
264 $this->assertText(t('The block configuration has been saved.'), t('Block configuration set.'));
265
266 // Set the block to a region to confirm block is available.
267 $edit = array();
268 $edit['poll_recent[region]'] = 'footer';
269 $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
270 $this->assertText(t('The block settings have been updated.'), t('Block successfully move to footer region.'));
271
272 // Create a poll which should appear in recent polls block.
273 $title = $this->randomName();
274 $choices = $this->_generateChoices(7);
275 $poll_nid = $this->pollCreate($title, $choices, TRUE);
276
277 // Verify poll appears in a block.
278 // View user page so we're not matching the poll node on front page.
279 $this->drupalGet('user');
280 // If a 'block' view not generated, this title would not appear even though
281 // the choices might.
282 $this->assertText($title, 'Poll appears in block.');
283
284 // Logout and login back in as a user who can vote.
285 $this->drupalLogout();
286 $vote_user = $this->drupalCreateUser(array('cancel own vote', 'inspect all votes', 'vote on polls', 'access content'));
287 $this->drupalLogin($vote_user);
288
289 // Verify we can vote via the block.
290 $edit = array(
291 'choice' => '1',
292 );
293 $this->drupalPost('user/' . $vote_user->uid, $edit, t('Vote'));
294 $this->assertText('Your vote was recorded.', 'Your vote was recorded.');
295 $this->assertText('Total votes: 1', 'Vote count updated correctly.');
296 $this->assertText('Older polls', 'Link to older polls appears.');
297 $this->clickLink('Older polls');
298 $this->assertText('1 vote - open', 'Link to poll listing correct.');
299
300 // Close the poll and verify block doesn't appear.
301 $content_user = $this->drupalCreateUser(array('create poll content', 'edit any poll content', 'access content'));
302 $this->drupalLogout();
303 $this->drupalLogin($content_user);
304 $close_edit = array('active' => 0);
305 $this->pollUpdate($poll_nid, $title, $close_edit);
306 $this->drupalGet('user/' . $content_user->uid);
307 $this->assertNoText($title, 'Poll no longer appears in block.');
308 }
309 }
310
311 /**
312 * Test adding new choices.
313 */
314 class PollJSAddChoice extends DrupalWebTestCase {
315
316 public static function getInfo() {
317 return array(
318 'name' => 'Poll add choice',
319 'description' => 'Submits a POST request for an additional poll choice.',
320 'group' => 'Poll'
321 );
322 }
323
324 function setUp() {
325 parent::setUp('poll');
326 }
327
328 /**
329 * Test adding a new choice.
330 */
331 function testAddChoice() {
332 $web_user = $this->drupalCreateUser(array('create poll content', 'access content'));
333 $this->drupalLogin($web_user);
334 $this->drupalGet('node/add/poll');
335 $langcode = FIELD_LANGUAGE_NONE;
336 $edit = array(
337 "title[$langcode][0][value]" => $this->randomName(),
338 'choice[new:0][chtext]' => $this->randomName(),
339 'choice[new:1][chtext]' => $this->randomName(),
340 );
341
342 // @TODO: the framework should make it possible to submit a form to a
343 // different URL than its action or the current. For now, we can just force
344 // it.
345 $this->additionalCurlOptions[CURLOPT_URL] = url('system/ajax', array('absolute' => TRUE));
346 $this->drupalPost(NULL, $edit, t('More choices'));
347 unset($this->additionalCurlOptions[CURLOPT_URL]);
348
349 // The response is drupal_json_output, so we need to undo some escaping.
350 $commands = json_decode(str_replace(array('\x3c', '\x3e', '\x26'), array("<", ">", "&"), $this->drupalGetContent()));
351
352 // The JSON response will be two AJAX commands. The first is a settings
353 // command and the second is the replace command.
354 $settings = reset($commands);
355 $replace = next($commands);
356
357 $this->assertTrue(is_object($settings), t('The response settings command is an object'));
358 $this->assertTrue(is_object($replace), t('The response replace command is an object'));
359
360 // This replace data is valid HTML so we will can reuse everything we have
361 // for HTML pages.
362 $this->content = $replace->data;
363
364 // Needs to be emptied out so the new content will be parsed.
365 $this->elements = '';
366 $this->assertFieldByName('choice[chid:0][chtext]', $edit['choice[new:0][chtext]'], t('Field !i found', array('!i' => 0)));
367 $this->assertFieldByName('choice[chid:1][chtext]', $edit['choice[new:1][chtext]'], t('Field !i found', array('!i' => 1)));
368 $this->assertFieldByName('choice[new:0][chtext]', '', t('Field !i found', array('!i' => 2)));
369 }
370 }

  ViewVC Help
Powered by ViewVC 1.1.2