You are here

janrain_capture_mapping.test in Janrain Registration 7.2

File

janrain_capture_mapping/tests/janrain_capture_mapping.test
View source
<?php

class JanrainCaptureMappingTestCase extends DrupalWebTestCase {

  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Janrain Capture Mapping tests'),
      'description' => t('Test the field mapping for Janrain Capture.'),
      'group' => t('Janrain Capture'),
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {
    parent::setUp('entity', 'janrain_capture', 'janrain_capture_mapping');

    // Create and login user
    $admin_user = $this
      ->drupalCreateUser(array(
      'access administration pages',
      'administer site configuration',
    ));
    $this
      ->drupalLogin($admin_user);
  }

  /**
   * Create some field mappings through the UI and ensure that they work.
   */
  function testPropertyMapping() {

    // Test that we can map a capture field to the username property
    $display_name_fid = db_query("SELECT fid FROM {janrain_capture_mapping_field} where path = 'displayName'")
      ->fetchField();
    $edit = array(
      '0[fid]' => $display_name_fid,
      '0[field]' => 'name',
    );
    $this
      ->drupalPost('admin/config/people/janrain_capture/mapping', $edit, t('Save configuration'));
    $mapping = variable_get('janrain_capture_mapping_map', array());
    $this
      ->assertEqual($display_name_fid, $mapping[0]['fid'], t('Property mapping was saved'));

    // Create a user whose profile we will manipulate
    $drupal_user = $this
      ->drupalCreateUser();

    // Mock a capture profile that will get mapped.
    $capture_profile = array(
      'uuid' => $this
        ->randomString(),
      'email' => 'user@someaddress.com',
      'givenName' => $this
        ->randomName(),
      'familyName' => $this
        ->randomName(),
      'displayName' => $this
        ->randomName(),
      'gender' => 'male',
    );
    janrain_capture_sync_account($drupal_user, $capture_profile);
    user_save($drupal_user);

    // Test that the email gets mapped even though we have not set up a mapping for it.
    $this
      ->assertTrue($drupal_user->mail == $capture_profile['email'], t('User email was updated from capture'));

    // Test that our username mapping trumps the default mapping of email address to
    // username that is done by the core janrain_capture module.
    $this
      ->assertTrue($drupal_user->name == $capture_profile['displayName'], t('User name was updated from capture'));
  }
  function testFieldApiMapping() {

    // Set up a Field API field on the user so that we can test mapping a Capture
    // property to that field.
    $field = array(
      'type' => 'text',
      'field_name' => 'field_test_field',
      'cardinality' => 1,
    );
    field_create_field($field);
    $instance = array(
      'field_name' => 'field_test_field',
      'entity_type' => 'user',
      'label' => 'User text field',
      'bundle' => 'user',
      'required' => FALSE,
      'settings' => array(
        'user_register_form' => FALSE,
      ),
      'widget' => array(
        'type' => 'text_textfield',
        'weight' => -1,
      ),
    );
    field_create_instance($instance);
    $given_name_fid = db_query("SELECT fid FROM {janrain_capture_mapping_field} where path = 'givenName'")
      ->fetchField();

    // Our mappings start at index 2 because 0 and 1 have been taken by the two default fields that
    // get mapped on install.
    $edit = array(
      '0[fid]' => $given_name_fid,
      '0[field]' => 'field_test_field',
      '0[column]' => 'value',
    );
    $this
      ->drupalPost('admin/config/people/janrain_capture/mapping', $edit, t('Save configuration'));
    $mapping = variable_get('janrain_capture_mapping_map', array());
    $this
      ->assertEqual($given_name_fid, $mapping[0]['fid'], t('Field mapping was saved'));

    // Create a user whose profile we will manipulate
    $drupal_user = $this
      ->drupalCreateUser();

    // Load up our user fully.
    $drupal_user = user_load($drupal_user->uid);

    // Mock a capture profile that will get mapped.
    $capture_profile = array(
      'uuid' => $this
        ->randomString(),
      'email' => 'user2@someaddress.com',
      'givenName' => $this
        ->randomName(),
      'familyName' => $this
        ->randomName(),
      'displayName' => $this
        ->randomName(),
      'gender' => 'female',
    );
    janrain_capture_sync_account($drupal_user, $capture_profile);
    user_save($drupal_user);

    // Test that our field_test_field value was mapped correctly.
    $this
      ->assertTrue($drupal_user->field_test_field[LANGUAGE_NONE][0]['value'] == $capture_profile['givenName'], t('Given name field from capture was mapped to the test field'));
  }

}

Classes