You are here

party_primary_fields.test in Party 7

Access Tests for the Party module.

File

tests/party_primary_fields.test
View source
<?php

/**
 * @file
 * Access Tests for the Party module.
 */

/**
 * Test Core Party functionality
 */
class PartyPrimaryFieldsTestCase extends PartyBaseTestCase {

  /**
   * The name of the profile2 data set in this test.
   * @var string
   */
  protected $data_set_name;
  public static function getInfo() {
    return array(
      'name' => 'Party Primary Fields',
      'description' => 'Test the setting of primary fields.',
      'group' => 'Party',
      'dependencies' => array(
        'email_confirm',
      ),
    );
  }

  /**
   * Set up the testing environment.
   *
   * Install party and party_profile, create a profile2 data set to test with,
   * and create an empty party.
   */
  public function setUp() {
    parent::setUp('party', 'party_profile', 'party_user');

    // Enable any modules required for the test
    // Set up a data set to test permission on.
    $this->data_set_name = 'profile2_' . $this
      ->createTestProfile2();

    // Don't pull the user name from the party for this test.
    variable_set('party_user_format_username', FALSE);
  }

  /**
   * Test the basics of primary fields.
   */
  public function testPrimaryFieldsBasic() {
    $party_controller = entity_get_controller('party');

    // By default, a party should get 'Party [pid]' as a label.
    $party = party_create();
    $party
      ->save();
    $label = t('Party @pid', array(
      '@pid' => $party->pid,
    ));
    $this
      ->assertEqual($party->label, $label, 'Default party label correctly set.');

    // Set up the new primary field for the  username.
    $primary_fields = variable_get('party_primary_fields');
    $primary_fields['label']['user:uid'] = array(
      'data_set' => 'user',
      'property' => 'uid',
      'callback' => 'uid_to_username',
      'weight' => 10,
    );
    variable_set('party_primary_fields', $primary_fields);
    PartyPrimaryFields::clearCaches();

    // If we attach a user, it should be the username.
    $user = $this
      ->drupalCreateUser();
    $controller = $party
      ->getDataSetController('user');
    $controller
      ->attachEntity($user);
    $controller
      ->save();

    // Saving the controller should have updated the label and email.
    $this
      ->assertEqual($party->label, $user->name, 'Label correctly set from username.');
    $this
      ->assertEqual($party->email, $user->mail, 'Email correctly set from user mail.');

    // Attaching a profile with a value in party_test_field shouldn't cause any
    // change as we haven't configured the primary field.
    $controller = $party
      ->getDataSetController($this->data_set_name);
    $entity = $controller
      ->getEntity(0, TRUE);
    $wrapper = entity_metadata_wrapper('profile2', $entity);
    $wrapper->party_test_field = $this
      ->randomName();
    $controller
      ->save();

    // Check the label has not been updated.
    $this
      ->assertEqual($party->label, $user->name, 'Label not updated by attached entity.');

    // Set up the new primary field with a weight less than the username.
    $primary_fields = variable_get('party_primary_fields');
    $primary_fields['label'][$this->data_set_name . ':party_test_field'] = array(
      'data_set' => $this->data_set_name,
      'property' => 'party_test_field',
      'weight' => 0,
    );
    variable_set('party_primary_fields', $primary_fields);
    PartyPrimaryFields::clearCaches();

    // Check our primary field updates correctly.
    $party_controller
      ->setPrimaryFields($party);
    $this
      ->assertEqual($party->label, $wrapper->party_test_field
      ->value(), 'Label correctly updated by attached entity.');
  }

}

Classes

Namesort descending Description
PartyPrimaryFieldsTestCase Test Core Party functionality