You are here

public function FeedsCSVtoUsersTest::testUidTarget in Feeds 7.2

Tests mapping to user ID.

File

tests/feeds_processor_user.test, line 151
Tests for plugins/FeedsUserProcessor.inc.

Class

FeedsCSVtoUsersTest
Test aggregating a feed as data records.

Code

public function testUidTarget() {

  // Set to update existing users.
  $this
    ->setSettings('user_import', 'FeedsUserProcessor', array(
    'update_existing' => FEEDS_UPDATE_EXISTING,
  ));

  // Add mapping to user ID.
  $this
    ->addMappings('user_import', array(
    4 => array(
      'source' => 'uid',
      'target' => 'uid',
      'unique' => TRUE,
    ),
  ));

  // Create account with uid 202. The username and mail address of this account
  // should be updated.
  user_save(drupal_anonymous_user(), array(
    'uid' => 202,
    'name' => 'Joe',
    'mail' => 'joe@example.com',
    'pass' => 'joe',
    'status' => 1,
  ));

  // Import CSV file.
  $this
    ->importFile('user_import', $this
    ->absolutePath() . '/tests/feeds/users.csv');
  $this
    ->assertText('Created 2 users');
  $this
    ->assertText('Updated 1 user');

  // Assert user ID's.
  $account = user_load_by_name('Morticia');
  $this
    ->assertEqual(201, $account->uid, 'Morticia got user ID 201.');
  $account = user_load_by_name('Gomez');
  $this
    ->assertEqual(203, $account->uid, 'Gomez got user ID 203.');

  // Assert that the username and mail address of account 202 were changed.
  $account = user_load(202);
  $values = array(
    'name' => array(
      'expected' => 'Fester',
      'actual' => $account->name,
    ),
    'mail' => array(
      'expected' => 'fester@example.com',
      'actual' => $account->mail,
    ),
  );
  $this
    ->assertEqual($values['name']['expected'], $values['name']['actual'], format_string('Username of account 202 changed in @expected (actual: @actual).', array(
    '@expected' => $values['name']['expected'],
    '@actual' => $values['name']['actual'],
  )));
  $this
    ->assertEqual($values['mail']['expected'], $values['mail']['actual'], format_string('Mail address of account 202 changed in @expected (actual: @actual).', array(
    '@expected' => $values['mail']['expected'],
    '@actual' => $values['mail']['actual'],
  )));

  // Assert that user Joe no longer exists in the system.
  $this
    ->assertFalse(user_load_by_name('Joe'), 'No user with username Joe exists.');
  $this
    ->assertFalse(user_load_by_mail('joe@example.com'), 'No user with mail address joe@example.com exists.');
}