profile2.test 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. $this->assertEqual($profiles['test']->label(), 'label', 'Created and loaded profile 1.');
  59. $this->assertEqual($profiles['test2']->label(), 'label2', 'Created and loaded profile 2.');
  60. // Test looking up from static cache works also.
  61. $profiles = profile2_load_by_user($user1);
  62. $this->assertEqual($profiles['test']->label, 'label', 'Looked up profiles again.');
  63. $loaded = profile2_load($profile->pid);
  64. $this->assertEqual($loaded->pid, $profile->pid, 'Loaded profile unrelated to a user.');
  65. profile2_delete($profiles['test']);
  66. $profiles2 = profile2_load_by_user($user1);
  67. $this->assertEqual(array_keys($profiles2), array('test2'), 'Profile successfully deleted.');
  68. profile2_save($profiles2['test2']);
  69. $this->assertEqual($profiles['test2']->pid, $profiles2['test2']->pid, 'Profile successfully updated.');
  70. // Delete a profile type.
  71. profile2_type_delete(profile2_get_types('test'));
  72. // Try deleting multiple profiles by deleting all existing profiles.
  73. $pids = array_keys(profile2_load_multiple(FALSE));
  74. profile2_delete_multiple($pids);
  75. }
  76. /**
  77. * Test registration integration.
  78. */
  79. function testRegistrationIntegration() {
  80. // Allow registration by site visitors without administrator approval.
  81. variable_set('user_register', 1);
  82. $edit = array();
  83. $edit['name'] = $name = $this->randomName();
  84. $edit['mail'] = $mail = $edit['name'] . '@example.com';
  85. $edit['profile_main[profile_fullname][und][0][value]'] = $this->randomName();
  86. $this->drupalPost('user/register', $edit, t('Create new account'));
  87. $this->assertText(t('A welcome message with further instructions has been sent to your e-mail address.'), t('User registered successfully.'));
  88. $return = user_load_multiple(array(), array('name' => $name, 'mail' => $mail));
  89. $new_user = reset($return);
  90. $this->assertTrue($new_user->status, t('New account is active after registration.'));
  91. $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.');
  92. }
  93. /**
  94. * Test basic edit and display.
  95. */
  96. function testEditAndDisplay() {
  97. user_role_revoke_permissions(DRUPAL_AUTHENTICATED_RID, array('edit own main profile', 'view own main profile'));
  98. $user1 = $this->drupalCreateUser();
  99. $this->drupalLogin($user1);
  100. // Make sure access is denied to the profile.
  101. $this->drupalGet('user/' . $user1->uid . '/edit/main');
  102. $this->assertText(t('Access denied'), 'Access has been denied.');
  103. // Test creating a profile manually (e.g. by an admin) and ensure the user
  104. // may not see it.
  105. profile2_create(array('type' => 'main', 'uid' => $user1->uid))->save();
  106. $this->drupalGet('user/' . $user1->uid);
  107. $this->assertNoText(t('Main profile'), 'Profile data is not visible to the owner.');
  108. $user2 = $this->drupalCreateUser(array('edit own main profile', 'view own main profile'));
  109. $this->drupalLogin($user2);
  110. // Create profiles for the user2.
  111. $edit['profile_main[profile_fullname][und][0][value]'] = $this->randomName();
  112. $this->drupalPost('user/' . $user2->uid . '/edit/main', $edit, t('Save'));
  113. $this->assertText(t('The changes have been saved.'), 'Profile saved.');
  114. $this->assertEqual(profile2_load_by_user($user2, 'main')->profile_fullname[LANGUAGE_NONE][0]['value'], $edit['profile_main[profile_fullname][und][0][value]'], 'Profile edited.');
  115. $this->drupalGet('user/' . $user2->uid);
  116. $this->assertText(check_plain($edit['profile_main[profile_fullname][und][0][value]']), 'Profile displayed.');
  117. }
  118. }