You are here

function Profile2CRUDTestCase::testCRUD in Profile 2 7

Same name and namespace in other branches
  1. 7.2 profile2.test \Profile2CRUDTestCase::testCRUD()

Tests CRUD for a profile related to a user and one unrelated to a user.

File

./profile2.test, line 56

Class

Profile2CRUDTestCase
Test basic CRUD functionality.

Code

function testCRUD() {
  $user1 = $this
    ->drupalCreateUser();

  // Create profiles for the user1 and unrelated to a user.
  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.');

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