View source
<?php
class Profile2CRUDTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Editing profiles',
'description' => 'Tests basic CRUD and editing of Profile2 profiles.',
'group' => 'Profile2',
);
}
function setUp() {
parent::setUp('profile2', 'locale');
profile2_type_save(new ProfileType(array(
'type' => 'test',
'label' => 'label',
'weight' => 0,
)));
profile2_type_save(new ProfileType(array(
'type' => 'test2',
'label' => 'label2',
'weight' => 2,
)));
profile2_load_multiple(FALSE, array(), TRUE);
$field = array(
'field_name' => 'profile_fullname',
'type' => 'text',
'cardinality' => 1,
'translatable' => FALSE,
);
field_create_field($field);
$instance = array(
'entity_type' => 'profile2',
'field_name' => 'profile_fullname',
'bundle' => 'main',
'label' => 'Full name',
'description' => 'Specify your first and last name.',
'widget' => array(
'type' => 'text_textfield',
'weight' => 0,
),
);
field_create_instance($instance);
}
function testCRUD() {
$user1 = $this
->drupalCreateUser();
profile2_save(profile2_create(array(
'type' => 'test',
'uid' => $user1->uid,
)));
profile2_save(profile2_create(array(
'type' => 'test2',
'uid' => $user1->uid,
)));
$profile = profile2_create(array(
'type' => 'test',
'uid' => NULL,
));
profile2_save($profile);
$profiles = profile2_load_by_user($user1);
$label = t('@type profile for @user', array(
'@type' => 'label',
'@user' => format_username($user1),
));
$label2 = t('@type profile for @user', array(
'@type' => 'label2',
'@user' => format_username($user1),
));
$this
->assertEqual($profiles['test']
->label(), $label, 'Created and loaded profile 1.');
$this
->assertEqual($profiles['test2']
->label(), $label2, 'Created and loaded profile 2.');
$profiles = profile2_load_by_user($user1);
$this
->assertEqual($profiles['test']->label, 'label', 'Looked up profiles again.');
$loaded = profile2_load($profile->pid);
$this
->assertEqual($loaded->pid, $profile->pid, 'Loaded profile unrelated to a user.');
profile2_delete($profiles['test']);
$profiles2 = profile2_load_by_user($user1);
$this
->assertEqual(array_keys($profiles2), array(
'test2',
), 'Profile successfully deleted.');
profile2_save($profiles2['test2']);
$this
->assertEqual($profiles['test2']->pid, $profiles2['test2']->pid, 'Profile successfully updated.');
profile2_type_delete(profile2_get_types('test'));
$pids = array_keys(profile2_load_multiple(FALSE));
profile2_delete_multiple($pids);
}
function testRegistrationIntegration() {
variable_set('user_register', 1);
$edit = array();
$edit['name'] = $name = $this
->randomName();
$edit['mail'] = $mail = $edit['name'] . '@example.com';
$edit['profile_main[profile_fullname][und][0][value]'] = $this
->randomName();
$this
->drupalPost('user/register', $edit, t('Create new account'));
$this
->assertText(t('A welcome message with further instructions has been sent to your e-mail address.'), t('User registered successfully.'));
$return = user_load_multiple(array(), array(
'name' => $name,
'mail' => $mail,
));
$new_user = reset($return);
$this
->assertTrue($new_user->status, t('New account is active after registration.'));
$this
->assertEqual(profile2_load_by_user($new_user, 'main')->profile_fullname[LANGUAGE_NONE][0]['value'], $edit['profile_main[profile_fullname][und][0][value]'], 'Profile created.');
}
function testEditAndDisplay() {
user_role_revoke_permissions(DRUPAL_AUTHENTICATED_RID, array(
'edit own main profile',
'view own main profile',
));
$user1 = $this
->drupalCreateUser();
$this
->drupalLogin($user1);
$this
->drupalGet('user/' . $user1->uid . '/edit/main');
$this
->assertText(t('Access denied'), 'Access has been denied.');
profile2_create(array(
'type' => 'main',
'uid' => $user1->uid,
))
->save();
$this
->drupalGet('user/' . $user1->uid);
$this
->assertNoText(t('Main profile'), 'Profile data is not visible to the owner.');
$user2 = $this
->drupalCreateUser(array(
'edit own main profile',
'view own main profile',
));
$this
->drupalLogin($user2);
$edit['profile_main[profile_fullname][und][0][value]'] = $this
->randomName();
$this
->drupalPost('user/' . $user2->uid . '/edit/main', $edit, t('Save'));
$this
->assertText(t('The changes have been saved.'), 'Profile saved.');
$this
->assertEqual(profile2_load_by_user($user2, 'main')->profile_fullname[LANGUAGE_NONE][0]['value'], $edit['profile_main[profile_fullname][und][0][value]'], 'Profile edited.');
$this
->drupalGet('user/' . $user2->uid);
$this
->assertText(check_plain($edit['profile_main[profile_fullname][und][0][value]']), 'Profile displayed.');
}
function testAccess() {
global $user;
$admin_user = $this
->drupalCreateUser(array(
'administer profiles',
));
$user2 = $this
->drupalCreateUser();
$profile = profile2_create(array(
'type' => 'test',
'uid' => $admin_user->uid,
));
profile2_save($profile);
$this
->drupalLogin($user2);
$this
->drupalGet('user/' . $admin_user->uid . '/edit/main');
$this
->assertText(t('Access denied'), 'Access has been denied.');
$user = $user2;
$this
->assertFalse(profile2_access('edit'), 'No edit access for user 2');
$this
->assertTrue(profile2_access('edit', NULL, $admin_user), 'No edit access for user 1');
}
}