You are here

public function PartyPrimaryFieldsTestCase::testPrimaryFieldsBasic in Party 7

Test the basics of primary fields.

File

tests/party_primary_fields.test, line 47
Access Tests for the Party module.

Class

PartyPrimaryFieldsTestCase
Test Core Party functionality

Code

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