You are here

profile2.test in Profile 2 7

Same filename and directory in other branches
  1. 7.2 profile2.test

File

profile2.test
View source
<?php

/**
 * Test basic CRUD functionality.
 */
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);

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

  /**
   * 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.');

    // Test creating a profile manually (e.g. by an admin) and ensure the user
    // may not see it.
    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);

    // Create profiles for the 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.');
  }

  /**
   * Tests optional access parameters.
   */
  function testAccess() {
    global $user;
    $admin_user = $this
      ->drupalCreateUser(array(
      'administer profiles',
    ));
    $user2 = $this
      ->drupalCreateUser();

    // Create profiles for the admin user.
    $profile = profile2_create(array(
      'type' => 'test',
      'uid' => $admin_user->uid,
    ));
    profile2_save($profile);

    // Make sure access is denied to the profile.
    $this
      ->drupalLogin($user2);
    $this
      ->drupalGet('user/' . $admin_user->uid . '/edit/main');
    $this
      ->assertText(t('Access denied'), 'Access has been denied.');

    // Set the global user to ensure the defaults are respected.
    $user = $user2;

    // Ensure optional parameters check access for the current logged in user.
    $this
      ->assertFalse(profile2_access('edit'), 'No edit access for user 2');

    // Ensure optional parameters check access for the admin user.
    $this
      ->assertTrue(profile2_access('edit', NULL, $admin_user), 'No edit access for user 1');
  }

}

Classes

Namesort descending Description
Profile2CRUDTestCase Test basic CRUD functionality.