-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprofile2.test
133 lines (114 loc) · 5.05 KB
/
profile2.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
// $Id$
/**
* Test basic CRUD functionality.
*/
class ProfileCRUDTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Editing profiles',
'description' => 'Tests basic CRUD and editing of 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);
// Add a field to main type, which is created during module installation.
$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);
}
/**
* Tests CRUD for a profile related to a user and one unrelated to a user.
*/
function testCRUD() {
$user1 = $this->drupalCreateUser();
// Create profiles for the user1 and unrelated to a user.
profile2_save(profile_create(array('type' => 'test', 'uid' => $user1->uid)));
profile2_save(profile_create(array('type' => 'test2', 'uid' => $user1->uid)));
$profile = profile_create(array('type' => 'test', 'uid' => NULL));
profile2_save($profile);
$profiles = profile2_load_by_user($user1);
$this->assertEqual($profiles['test']->label(), 'label', 'Created and loaded profile 1.');
$this->assertEqual($profiles['test2']->label(), 'label2', 'Created and loaded profile 2.');
// Test looking up from static cache works also.
$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.');
// Delete a profile type.
profile2_type_delete(profile2_get_types('test'));
// Try deleting multiple profiles by deleting all existing profiles.
$pids = array_keys(profile2_load_multiple(FALSE));
profile2_delete_multiple($pids);
}
/**
* Test registration integration.
*/
function testRegistrationIntegration() {
// Allow registration by site visitors without administrator approval.
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.');
}
/**
* Test basic edit and display.
*/
function testEditAndDisplay() {
user_role_revoke_permissions(DRUPAL_AUTHENTICATED_RID, array('edit own main profile', 'view own main profile'));
$user1 = $this->drupalCreateUser();
$this->drupalLogin($user1);
// Make sure access is denied to the profile.
$this->drupalGet('user/' . $user1->uid . '/edit/main');
$this->assertText(t('Access denied'), 'Access has been denied.');
$user2 = $this->drupalCreateUser(array('edit own main profile', 'view own main profile'));
$this->drupalLogin($user2);
// Create profiles for the user1.
$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.');
}
}