profile2.test 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Test basic CRUD functionality.
  4. */
  5. class Profile2CRUDTestCase extends DrupalWebTestCase {
  6. public static function getInfo() {
  7. return array(
  8. 'name' => 'Editing profiles',
  9. 'description' => 'Tests basic CRUD and editing of Profile2 profiles.',
  10. 'group' => 'Profile2',
  11. );
  12. }
  13. function setUp() {
  14. parent::setUp('profile2', 'locale');
  15. profile2_type_save(new ProfileType(array(
  16. 'type' => 'test',
  17. 'label' => 'label',
  18. 'weight' => 0
  19. )));
  20. profile2_type_save(new ProfileType(array(
  21. 'type' => 'test2',
  22. 'label' => 'label2',
  23. 'weight' => 2
  24. )));
  25. profile2_load_multiple(FALSE, array(), TRUE);
  26. // Add a field to main type, which is created during module installation.
  27. $field = array(
  28. 'field_name' => 'profile_fullname',
  29. 'type' => 'text',
  30. 'cardinality' => 1,
  31. 'translatable' => FALSE,
  32. );
  33. field_create_field($field);
  34. $instance = array(
  35. 'entity_type' => 'profile2',
  36. 'field_name' => 'profile_fullname',
  37. 'bundle' => 'main',
  38. 'label' => 'Full name',
  39. 'description' => 'Specify your first and last name.',
  40. 'widget' => array(
  41. 'type' => 'text_textfield',
  42. 'weight' => 0,
  43. ),
  44. );
  45. field_create_instance($instance);
  46. }
  47. /**
  48. * Tests CRUD for a profile related to a user and one unrelated to a user.
  49. */
  50. function testCRUD() {
  51. $user1 = $this->drupalCreateUser();
  52. // Create profiles for the user1 and unrelated to a user.
  53. profile2_save(profile2_create(array('type' => 'test', 'uid' => $user1->uid)));
  54. profile2_save(profile2_create(array('type' => 'test2', 'uid' => $user1->uid)));
  55. $profile = profile2_create(array('type' => 'test', 'uid' => NULL));
  56. profile2_save($profile);
  57. $profiles = profile2_load_by_user($user1);
  58. $label = t('@type profile for @user', array('@type' => 'label', '@user' => format_username($user1)));
  59. $label2 = t('@type profile for @user', array('@type' => 'label2', '@user' => format_username($user1)));
  60. $this->assertEqual($profiles['test']->label(), $label, 'Created and loaded profile 1.');
  61. $this->assertEqual($profiles['test2']->label(), $label2, 'Created and loaded profile 2.');
  62. // Test looking up from static cache works also.
  63. $profiles = profile2_load_by_user($user1);
  64. $this->assertEqual($profiles['test']->label, 'label', 'Looked up profiles again.');
  65. $loaded = profile2_load($profile->pid);
  66. $this->assertEqual($loaded->pid, $profile->pid, 'Loaded profile unrelated to a user.');
  67. profile2_delete($profiles['test']);
  68. $profiles2 = profile2_load_by_user($user1);
  69. $this->assertEqual(array_keys($profiles2), array('test2'), 'Profile successfully deleted.');
  70. profile2_save($profiles2['test2']);
  71. $this->assertEqual($profiles['test2']->pid, $profiles2['test2']->pid, 'Profile successfully updated.');
  72. // Delete a profile type.
  73. profile2_type_delete(profile2_get_types('test'));
  74. // Try deleting multiple profiles by deleting all existing profiles.
  75. $pids = array_keys(profile2_load_multiple(FALSE));
  76. profile2_delete_multiple($pids);
  77. }
  78. /**
  79. * Test registration integration.
  80. */
  81. function testRegistrationIntegration() {
  82. // Allow registration by site visitors without administrator approval.
  83. variable_set('user_register', 1);
  84. $edit = array();
  85. $edit['name'] = $name = $this->randomName();
  86. $edit['mail'] = $mail = $edit['name'] . '@example.com';
  87. $edit['profile_main[profile_fullname][und][0][value]'] = $this->randomName();
  88. $this->drupalPost('user/register', $edit, t('Create new account'));
  89. $this->assertText(t('A welcome message with further instructions has been sent to your e-mail address.'), t('User registered successfully.'));
  90. $return = user_load_multiple(array(), array('name' => $name, 'mail' => $mail));
  91. $new_user = reset($return);
  92. $this->assertTrue($new_user->status, t('New account is active after registration.'));
  93. $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.');
  94. }
  95. /**
  96. * Test basic edit and display.
  97. */
  98. function testEditAndDisplay() {
  99. user_role_revoke_permissions(DRUPAL_AUTHENTICATED_RID, array('edit own main profile', 'view own main profile'));
  100. $user1 = $this->drupalCreateUser();
  101. $this->drupalLogin($user1);
  102. // Make sure access is denied to the profile.
  103. $this->drupalGet('user/' . $user1->uid . '/edit/main');
  104. $this->assertText(t('Access denied'), 'Access has been denied.');
  105. // Test creating a profile manually (e.g. by an admin) and ensure the user
  106. // may not see it.
  107. profile2_create(array('type' => 'main', 'uid' => $user1->uid))->save();
  108. $this->drupalGet('user/' . $user1->uid);
  109. $this->assertNoText(t('Main profile'), 'Profile data is not visible to the owner.');
  110. $user2 = $this->drupalCreateUser(array('edit own main profile', 'view own main profile'));
  111. $this->drupalLogin($user2);
  112. // Create profiles for the user2.
  113. $edit['profile_main[profile_fullname][und][0][value]'] = $this->randomName();
  114. $this->drupalPost('user/' . $user2->uid . '/edit/main', $edit, t('Save'));
  115. $this->assertText(t('The changes have been saved.'), 'Profile saved.');
  116. $this->assertEqual(profile2_load_by_user($user2, 'main')->profile_fullname[LANGUAGE_NONE][0]['value'], $edit['profile_main[profile_fullname][und][0][value]'], 'Profile edited.');
  117. $this->drupalGet('user/' . $user2->uid);
  118. $this->assertText(check_plain($edit['profile_main[profile_fullname][und][0][value]']), 'Profile displayed.');
  119. }
  120. /**
  121. * Tests optional access parameters.
  122. */
  123. function testAccess() {
  124. global $user;
  125. $admin_user = $this->drupalCreateUser(array('administer profiles'));
  126. $user2 = $this->drupalCreateUser();
  127. // Create profiles for the admin user.
  128. $profile = profile2_create(array('type' => 'test', 'uid' => $admin_user->uid));
  129. profile2_save($profile);
  130. // Make sure access is denied to the profile.
  131. $this->drupalLogin($user2);
  132. $this->drupalGet('user/' . $admin_user->uid . '/edit/main');
  133. $this->assertText(t('Access denied'), 'Access has been denied.');
  134. // Set the global user to ensure the defaults are respected.
  135. $user = $user2;
  136. // Ensure optional parameters check access for the current logged in user.
  137. $this->assertFalse(profile2_access('edit'), 'No edit access for user 2');
  138. // Ensure optional parameters check access for the admin user.
  139. $this->assertTrue(profile2_access('edit', NULL, $admin_user), 'No edit access for user 1');
  140. }
  141. }