You are here

function ProfileModuleTestSelection::testProfileSingle in SimpleTest 5

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

File

tests/profile_module.test, line 635

Class

ProfileModuleTestSelection

Code

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);
}