You are here

class ProfileModuleTestSelection in SimpleTest 5

Same name and namespace in other branches
  1. 6 tests/profile_module.test \ProfileModuleTestSelection

Hierarchy

Expanded class hierarchy of ProfileModuleTestSelection

File

tests/profile_module.test, line 589

View source
class ProfileModuleTestSelection extends DrupalTestCase {
  function get_info() {
    $modules = module_list();
    return array(
      'name' => 'Test Selection field',
      'desc' => "Testing profile module with add/edit/delete new fields into profile page",
      'group' => 'Profile Module',
    );
  }
  function _rolesApi($op, $edit) {
    if ($op == 'delete') {
      $id = $edit['rid'];
      db_query('DELETE FROM {role} WHERE rid = %d', $id);
      db_query('DELETE FROM {permission} WHERE rid = %d', $id);

      // Update the users who have this role set:
      $result = db_query('SELECT DISTINCT(ur1.uid) FROM {users_roles} ur1 LEFT JOIN {users_roles} ur2 ON ur2.uid = ur1.uid WHERE ur1.rid = %d AND ur2.rid != ur1.rid', $id);
      $uid = array();
      while ($u = db_fetch_object($result)) {
        $uid[] = $u->uid;
      }
      if ($uid) {
        db_query('DELETE FROM {users_roles} WHERE rid = %d AND uid IN (%s)', $id, implode(', ', $uid));
      }

      // Users with only the deleted role are put back in the authenticated users pool.
      db_query('UPDATE {users_roles} SET rid = %d WHERE rid = %d', DRUPAL_AUTHENTICATED_RID, $id);
    }
    else {
      if ($op == 'add') {
        if (isset($edit['name'])) {
          db_query("INSERT INTO {role} (name) VALUES ('%s')", $edit['name']);
          $result = db_query("SELECT rid FROM {role} WHERE name = '%s'", $edit['name']);
          $rid = db_result($result);
          db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, $edit['perm']);
          return $rid;
        }
        else {
          return 0;
        }
      }
    }
  }
  function testProfileSingle() {
    $this
      ->drupalModuleEnable('profile');

    // create test user
    $edit['name'] = 'Profile ' . $this
      ->randomName(5);
    $edit['perm'] = 'access content, administer users, administer site configuration, access administration pages, access configuration pages, access user profiles';
    $rid = $this
      ->_rolesApi('add', $edit);
    $name = $this
      ->randomName();
    $pass = $this
      ->randomName();
    $mail = "{$name}@example.com";
    unset($edit);
    $edit['roles'] = array(
      $rid => $rid,
    );
    $user = user_save('', array(
      'name' => $name,
      'pass' => $pass,
      'init' => $mail,
      'mail' => $mail,
      'roles' => $edit['roles'],
      'status' => 1,
    ));

    //log in
    $edit = array(
      'name' => $name,
      'pass' => $pass,
    );
    $this
      ->drupalPostRequest('user', $edit, 'Log in', 0);

    //wartosci
    $my_category = 'Simpletest';

    //single line textfield
    $title = "single_" . $this
      ->randomName(10);
    $form_name = 'profile_' . $title;
    $explanation = $this
      ->randomName(50);
    $options = "";
    for ($i = 0; $i < 3; $i++) {
      $options .= $this
        ->randomName(8) . "\n";
    }
    $edit = array(
      'category' => $my_category,
      'title' => $title,
      'name' => $form_name,
      'explanation' => $explanation,
      'options' => $options,
    );
    $this
      ->drupalPostRequest("admin/user/profile/add/selection", $edit, 'Save field', 0);
    $fid = db_result(db_query('SELECT fid FROM {profile_fields} WHERE title = "%s"', $title));
    $single_field = array(
      'title' => $title,
      'form_name' => $form_name,
      'explanation' => $explanation,
    );

    // checking simple fields
    $this->_browser
      ->get(url("user/" . $user->uid . "/edit/{$my_category}"));

    // checking name
    $this
      ->assertWantedText($title, "Checking title for " . $title);

    // checking explanation
    $this
      ->assertWantedText($explanation, "Checking explanation for " . $title);

    // can we choose something which doesn't come from the list ?
    $this
      ->assertFalse($this
      ->setField('edit[' . $form_name . ']', $this
      ->randomName(10)));

    // or can we choose each of our options
    $op_tab = explode("\n", $options, 3);
    foreach ($op_tab as $option) {
      $this
        ->assertTrue($this
        ->setField($form_name, $option));
    }

    // ok, now let put some data
    unset($edit);
    $edit = array();
    $checking = array();
    $element = rand(0, 2);
    $key = $form_name;
    $edit[$key] = rtrim($op_tab[$element]);
    $this
      ->drupalPostRequest("user/" . $user->uid . "/edit/{$my_category}", $edit, 'Submit', 0);
    $this->_browser
      ->get(url("user/" . $user->uid));

    // checking profile page
    $this
      ->assertWantedText($edit[$form_name], "Checking " . $edit[$form_name]);
    $this
      ->assertWantedText($title, "Checking {$title}");

    // update field
    $new_title = $this
      ->randomName(20);
    $this
      ->drupalPostRequest("admin/user/profile/edit/{$fid}", array(
      'title' => $new_title,
    ), 'Save field', 0);
    $this->_browser
      ->get(url("admin/user/profile"));
    $this
      ->assertWantedText($new_title, "Checking updated field");

    // deleting field
    $this
      ->drupalPostRequest("admin/user/profile/delete/{$fid}", array(), 'Delete', 0);
    $this->_browser
      ->get(url("admin/user/profile"));
    $this
      ->assertNoUnwantedText($new_title, "Checking deleted field {$title}");

    // delete test user and roles
    if ($user->uid > 0) {
      db_query('DELETE FROM {users} WHERE uid =%d', $user->uid);
      db_query('DELETE FROM {users_roles} WHERE uid = %d', $user->uid);
      module_invoke_all('user', 'delete', '', $user);
    }

    //delete roles
    $edit['rid'] = $rid;
    $this
      ->_rolesApi('delete', $edit);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalTestCase::$_cleanupModules property
DrupalTestCase::$_cleanupRoles property
DrupalTestCase::$_cleanupUsers property
DrupalTestCase::$_cleanupVariables property
DrupalTestCase::$_content property
DrupalTestCase::assertCopy function Will trigger a pass if both parameters refer to different objects. Fail otherwise.
DrupalTestCase::assertEqual function Will trigger a pass if the two parameters have the same value only. Otherwise a fail.
DrupalTestCase::assertError function Confirms that an error has occurred and optionally that the error text matches exactly.
DrupalTestCase::assertErrorPattern function Confirms that an error has occurred and that the error text matches a Perl regular expression.
DrupalTestCase::assertIdentical function Will trigger a pass if the two parameters have the same value and same type. Otherwise a fail.
DrupalTestCase::assertIsA function Type and class test. Will pass if class matches the type name or is a subclass or if not an object, but the type is correct.
DrupalTestCase::assertNoErrors function Confirms that no errors have occurred so far in the test method.
DrupalTestCase::assertNotA function Type and class mismatch test. Will pass if class name or underling type does not match the one specified.
DrupalTestCase::assertNotEqual function Will trigger a pass if the two parameters have a different value. Otherwise a fail.
DrupalTestCase::assertNotIdentical function Will trigger a pass if the two parameters have the different value or different type.
DrupalTestCase::assertNotNull function Will be true if the value is set.
DrupalTestCase::assertNoUnwantedPattern function Will trigger a pass if the Perl regex pattern is not present in subject. Fail if found.
DrupalTestCase::assertNoUnwantedRaw function Will trigger a pass if the raw text is NOT found on the loaded page Fail otherwise.
DrupalTestCase::assertNull function Will be true if the value is null.
DrupalTestCase::assertReference function Will trigger a pass if both parameters refer to the same object. Fail otherwise.
DrupalTestCase::assertWantedPattern function Will trigger a pass if the Perl regex pattern is found in the subject. Fail otherwise.
DrupalTestCase::assertWantedRaw function Will trigger a pass if the raw text is found on the loaded page Fail otherwise.
DrupalTestCase::clickLink function Follows a link by name. Will click the first link found with this link text by default, or a later one if an index is given. Match is case insensitive with normalised space. Does make assertations if the click was sucessful or not and it does…
DrupalTestCase::drupalCheckAuth function @abstract Checks to see if we need to send a http-auth header to authenticate when browsing a site.
DrupalTestCase::drupalCreateRolePerm function Create a role / perm combination specified by permissions
DrupalTestCase::drupalCreateUserRolePerm function Creates a user / role / permissions combination specified by permissions
DrupalTestCase::drupalGet function @abstract Brokder for the get function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::drupalGetContent function @TODO: needs documentation
DrupalTestCase::drupalLoginUser function Logs in a user with the internal browser
DrupalTestCase::drupalModuleDisable function Disables a drupal module
DrupalTestCase::drupalModuleEnable function Enables a drupal module
DrupalTestCase::drupalPostRequest function Do a post request on a drupal page. It will be done as usual post request with SimpleBrowser
DrupalTestCase::drupalRawPost function @abstract Broker for the post function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::DrupalTestCase function
DrupalTestCase::drupalVariableSet function Set a druapl variable and keep track of the changes for tearDown()
DrupalTestCase::randomName function Generates a random string, to be used as name or whatever
DrupalTestCase::run function Just some info for the reporter
DrupalTestCase::tearDown function tearDown implementation, setting back switched modules etc 1
ProfileModuleTestSelection::get_info function
ProfileModuleTestSelection::testProfileSingle function
ProfileModuleTestSelection::_rolesApi function