t('Decisions Selections CRUD'), 'description' => t('Create, edit, delete a new selection node with two choices'), 'group' => t('Decisions'), ); } /** * Implementation of setUp(). */ function setUp() { parent::setUp('selection', 'decisions', 'votingapi'); variable_set('decisions_1click', 1); } /** * Submit a closed 1-click decision, then open it, then close it again, * and testing voting as a non administrative user. */ function testSelectionCRUDVote() { $adminUser = $this->drupalCreateUser(array('administer decisions', 'create decisions', 'vote on decisions', 'administer permissions')); $regularUser = $this->drupalCreateUser(array('vote on decisions', 'view decisions')); // Submit a new selection decision. $this->drupalLogin($adminUser); $selection = array(); $title = $this->randomName(4); $node->type = 'decisions_selection'; $type_name = node_get_types('name', $node); $selection['title'] = $title; $choice1 = $this->randomName(4); $choice2 = $this->randomName(4); // Assign 2 labels, set max choices to 1 and set the decision to be closed. $selection["choice[1][label]"] = $choice1; $selection["choice[2][label]"] = $choice2; $selection['settings[active]'] = 0; $selection['settings[date][startdate][date][year]'] = '1900'; $selection['settings[date][noenddate]'] = 1; $selection['settings[maxchoices]'] = 1; $this->drupalPost('node/add/decisions-selection', $selection, t('Save')); $this->assertRaw(t("@type %title has been created.", array('@type' => $type_name, '%title' => $title)), t("@type %title has been created.", array('@type' => $type_name, '%title' => $title))); // We look for "This decision is" because that is the beginning of all // closed/inactive text. $this->assertText(t('This decision is currently closed'), t("Decision closed text is displayed.")); // Open the decision. $nid = db_result(db_query("SELECT nid FROM {node} WHERE type = 'decisions_selection' AND title = '%s'", $title)); $selection['settings[active]'] = 1; $this->drupalPost('node/'. $nid . '/edit', $selection, t('Save')); $this->assertNoRaw(t('This decision is'), t("Decision closed text does is not displayed")); // Verify that the choices appear when the decision is open. $this->assertText($choice1, t("Choice 1 appears for administrators.")); $this->asserttext($choice2, t("Choice 2 appears for administrators.")); // Close the decision. Depending on how results are displayed, the choices // may or may not appear. $selection['settings[active]'] = 0; $this->drupalPost('node/'. $nid . '/edit', $selection, t('Save')); $this->assertRaw(t('This decision is currently closed'), t("Decision closed text is displayed.")); // Reopen the decision to test with non-administrative users. $selection['settings[active]'] = 1; $this->drupalPost('node/'. $nid . '/edit', $selection, t('Save')); // Grant anonymous users permission to view and // vote on decisions. Simpletest for Drupal 6.x // lacks an API function to set permissions. $this->setPermission('anonymous user', array('vote on decisions' => TRUE, 'view decisions' => TRUE, 'access content' => TRUE)); $this->drupalLogout(); // Now, try to vote as a regular (non-admin) user. $this->drupalLogin($regularUser); $this->assert('pass', t("The following tests test voting as a regular (non-administrative) user:")); $this->selectionVoteTest($nid, $choice1, $choice2); $this->drupalLogout(); $this->assert('pass', t("The following tests test voting as an anonymous.")); $this->selectionVoteTest($nid, $choice1, $choice2); } function selectionVoteTest($nid, $choice1, $choice2) { $uid = isset($this->loggedInUser->uid) ? $this->loggedInUser->uid : 0; $this->drupalGet('node/'. $nid); $random = rand(3, 100); // Verify that the choices appear when the decision is open. $this->assertText($choice1, t("Choice 1 appears.")); $this->asserttext($choice2, t("Choice 2 appears.")); // Submit a vote $this->clickLink($choice1); // Verify that the vote confirmaton appears $this->assertText(t("You voted: @tag", array('@tag' => $choice1)), t("Vote confirmation text appears referencing the selected tag.")); // Test decisions_get_vote() and the functions it relies upon. // Verify decisions_get_vote() returns an array with the proper vote tag with the cache fallback on. variable_set('decisions_cache_fallback', 1); $voteFallback = decisions_get_vote($nid, $uid); $this->assertTrue(isset($voteFallback['tag']) && $voteFallback['tag'] == 1, t('decisions_get_vote() correctly returns TRUE with the cache fallback on.')); // Verify decisions_get_vote() returns an array with the proper vote tag with the cache fallback turned off. variable_set('decisions_cache_fallback', 0); $voteFallbackOff = decisions_get_vote($nid, $uid); $this->assertTrue(isset($voteFallbackOff['tag']) && $voteFallbackOff['tag'] == 1, t('decisions_get_vote() correctly returns TRUE with the cache fallback off.')); // Verify decisions_get_vote() returns FALSE with the cache fallback on. variable_set('decisions_cache_fallback', 1); $voteFallbackFalse = decisions_get_vote($random, $uid + $random); $this->assertFalse($voteFallbackFalse, t('decisions_get_vote() correctly returns FALSE with the cache fallback on.')); // Verify decisions_get_vote() returns FALSE with the cache fallback turned off. variable_set('decisions_cache_fallback', 0); $voteFallbackOffFalse = decisions_get_vote($random, $uid + $random); $this->assertFalse($voteFallbackOffFalse, t('decisions_get_vote() correctly returns FALSE with the cache fallback off.')); // Verify that the proper Decisions cache item was created when the user voted. $cache = decisions_get_vote_cache($nid, $uid); $this->assertTrue(isset($cache['tag']) && $cache['tag'] == 1, t("The Decisions vote cache item was created with the proper vote tag.")); // Pass random values to ensure that decisions_get_vote_cache() returns FALSE // when the expected cache item does not exist. $noCache = decisions_get_vote_cache($choice1, 0); $this->assertFalse($noCache, t('decisions_get_vote_cache() correctly returns FALSE.')); $voteDirect = decisions_get_vote_direct($nid, $uid); $this->assertTrue(isset($voteDirect['tag']) && $voteDirect['tag'] == 1, t("decisions_get_vote_direct() returned a vote with the proper tag.")); } /** * Set permission. * * @param string $role * User role to set permissions for. * @param array $permissions * Key-value array of permissions to set. */ function setPermission($role, $permissions) { // Get role id (rid) for specified role. $rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", array('%s' => $role))); if ($rid === FALSE) { $this->fail(t(' [permission] Role "' . $role . '" not found.')); } // Create edit array from permission. $edit = array(); foreach ($permissions as $name => $value) { $edit[$rid . '[' . $name . ']'] = $value; } $this->drupalPost('admin/user/permissions', $edit, t('Save permissions')); $this->assertText(t('The changes have been saved.'), t(' [permission] Saved changes.')); } }