View source
<?php
class UserImportWebTestCase extends DrupalWebTestCase {
protected $admin_user;
protected $user_importer;
function settingsFileSelect() {
$edit = array(
'file_ftp' => 1,
);
$this
->drupalPost('admin/user/user_import/add', $edit, 'Next');
$this
->assertText(t('Use Different CSV File'), '[assert] File was selected');
}
function settingsEmailMatch(&$edit) {
$edit['field_match[5][field_match]'] = 'user-email';
}
function settingsIgnoreFirstLine(&$edit) {
$edit['first_line_skip'] = 1;
}
function checkAccountsExist($list_failures = FALSE) {
$failures_list = '';
$users_email = $this
->usersList();
$failed = array();
foreach ($users_email as $mail) {
$user = user_load(array(
'mail' => $mail,
));
if (empty($user)) {
$failed[] = $mail;
}
}
if (!empty($failed) && $list_failures) {
$failures_list = t('. Failed accounts: %failures', array(
'%failures' => implode(', ', $failed),
));
}
$this
->assertTrue(empty($failed), t('Accounts created for users imported') . $failures_list);
}
function usersList() {
return array(
'john@example.com',
'mandy@example.com',
'charles@example.com',
'sarah@example.com',
'sarah_smith@example.com',
'helen@example.com',
'claire@example.com',
'victoria@example.com',
'james@example.com',
'anna@example.com',
'tino@example.com',
'sofia@example.com',
'steve@example.com',
'lucy@example.com',
'angie@example.com',
'carmen@example.com',
'paul@example.com',
'jason@example.com',
'mike@example.com',
'mary@example.com',
'simon@example.com',
'kieran@example.com',
'arthur@example.com',
'gwen@example.com',
'chester@example.com',
'dorothy@example.com',
'cameron@example.com',
'trisha@example.com',
'david@example.com',
'peter@example.com',
'saul@example.com',
'noel@example.com',
'matt@example.com',
'aston@example.com',
'mille@example.com',
'ernest@example.com',
);
}
function importID($url = NULL) {
static $import_id = 0;
if (empty($import_id) && !empty($url)) {
$args = explode('/', $url);
$import_id = $args[7];
}
return $import_id;
}
function tearDown() {
$users_email = $this
->usersList();
foreach ($users_email as $mail) {
$account = user_load(array(
'mail' => $mail,
));
if (!empty($account)) {
user_delete(array(), $account->uid);
}
}
$import_id = $this
->importID();
$this
->assertTrue(!empty($import_id), t('Import ID: !id', array(
'!id' => $import_id,
)));
_user_import_settings_deletion($import_id);
parent::tearDown();
}
}
class UserImportBasicsTestCase extends UserImportWebTestCase {
public static function getInfo() {
return array(
'name' => 'Import Users (Basics)',
'description' => 'Import users from a CSV file, test basic functions.',
'group' => 'User Import',
);
}
function setUp() {
parent::setUp('user_import');
$this->admin_user = $this
->drupalCreateUser(array(
'administer users',
'access administration pages',
'administer site configuration',
));
$this->user_importer = $this
->drupalCreateUser(array(
'import users',
));
}
function testCreateImport() {
$this
->drupalLogin($this->user_importer);
$this
->settingsFileSelect();
$this
->importID($this
->getUrl());
$setting_edit = array();
$this
->settingsEmailMatch($settings);
$this
->settingsIgnoreFirstLine($settings);
$this
->drupalPost($this
->getUrl(), $settings, 'Import');
$this
->checkAccountsExist(TRUE);
}
}
class UserImportNodeprofileTestCase extends UserImportWebTestCase {
public static function getInfo() {
return array(
'name' => 'Import Users (Nodeprofile)',
'description' => 'Test import of user data into Nodeprofile module.',
'group' => 'User Import',
);
}
function setUp() {
parent::setUp('content', 'number', 'optionwidgets', 'text', 'link', 'date_api', 'date', 'node_import', 'content_profile', 'user_import');
$this->admin_user = $this
->drupalCreateUser(array(
'administer users',
'administer permissions',
'access administration pages',
'administer site configuration',
'administer content types',
'administer taxonomy',
));
$this->user_importer = $this
->drupalCreateUser(array(
'import users',
));
}
function testCreateImport() {
$this
->drupalLogin($this->admin_user);
$this
->nodeprofileConfiguration();
$this
->drupalGet('logout');
$this
->drupalLogin($this->user_importer);
$this
->settingsFileSelect();
$this
->importID($this
->getUrl());
$settings = array();
$this
->settingsEmailMatch($settings);
$this
->settingsNodeprofileMatch($settings);
$this
->settingsIgnoreFirstLine($settings);
$this
->drupalPost($this
->getUrl(), $settings, 'Import');
$this
->checkNodeprofileExist();
}
function nodeprofileConfiguration() {
$edit = array(
'name' => 'Identity',
'type' => 'identity',
'title_label' => 'Title',
'body_label' => '',
'node_options[promote]' => 0,
'content_profile_use' => 1,
);
$this
->drupalPost('admin/content/types/add', $edit, t('Save content type'));
$edit = array(
'_add_new_field[label]' => 'First Name',
'_add_new_field[field_name]' => 'first_name',
'_add_new_field[type]' => 'text',
'_add_new_field[widget_type]' => 'text_textfield',
);
$this
->drupalPost('admin/content/node-type/identity/fields', $edit, t('Save'));
$edit = array(
'required' => 1,
);
$this
->drupalPost('admin/content/node-type/identity/fields/field_first_name', $edit, t('Save field settings'));
$edit = array(
'_add_new_field[label]' => 'Last Name',
'_add_new_field[field_name]' => 'last_name',
'_add_new_field[type]' => 'text',
'_add_new_field[widget_type]' => 'text_textfield',
);
$this
->drupalPost('admin/content/node-type/identity/fields', $edit, t('Save'));
$edit = array(
'required' => 1,
);
$this
->drupalPost('admin/content/node-type/identity/fields/field_last_name', $edit, t('Save field settings'));
$edit = array(
'name' => 'Biography',
'type' => 'biography',
'title_label' => 'Title',
'body_label' => '',
'node_options[promote]' => 0,
'content_profile_use' => 1,
);
$this
->drupalPost('admin/content/types/add', $edit, t('Save content type'));
$edit = array(
'_add_new_field[label]' => 'CV',
'_add_new_field[field_name]' => 'cv',
'_add_new_field[type]' => 'text',
'_add_new_field[widget_type]' => 'text_textarea',
);
$this
->drupalPost('admin/content/node-type/biography/fields', $edit, t('Save'));
$edit = array(
'required' => 1,
'rows' => 5,
);
$this
->drupalPost('admin/content/node-type/biography/fields/field_cv', $edit, t('Save field settings'));
$edit = array(
'_add_new_field[label]' => 'Blog',
'_add_new_field[field_name]' => 'blog',
'_add_new_field[type]' => 'link',
'_add_new_field[widget_type]' => 'link',
);
$this
->drupalPost('admin/content/node-type/biography/fields', $edit, t('Save'));
$edit = array(
'_add_new_field[label]' => 'Birthday',
'_add_new_field[field_name]' => 'birthday',
'_add_new_field[type]' => 'datestamp',
'_add_new_field[widget_type]' => 'date_text',
);
$this
->drupalPost('admin/content/node-type/biography/fields', $edit, t('Save'));
$edit = array(
'name' => 'Interests',
'nodes[biography]' => 'biography',
'tags' => 1,
);
$this
->drupalPost('admin/content/taxonomy/add/vocabulary', $edit, t('Save'));
$vocabularies = taxonomy_get_vocabularies('biography');
foreach ($vocabularies as $vocabulary) {
$this->vocabulary_id = $vocabulary->vid;
}
$edit = array(
'name' => 'Contact Details',
'type' => 'contact_details',
'title_label' => 'Title',
'body_label' => '',
'node_options[promote]' => 0,
'content_profile_use' => 1,
);
$this
->drupalPost('admin/content/types/add', $edit, t('Save content type'));
$edit = array(
'_add_new_field[label]' => 'Contact',
'_add_new_field[field_name]' => 'can_be_contacted',
'_add_new_field[type]' => 'number_integer',
'_add_new_field[widget_type]' => 'optionwidgets_onoff',
);
$this
->drupalPost('admin/content/node-type/contact-details/fields', $edit, t('Save'));
$edit = array(
'allowed_values' => "0\n 1|Can be contacted",
);
$this
->drupalPost('admin/content/node-type/contact-details/fields/field_can_be_contacted', $edit, t('Save field settings'));
$edit = array(
'_add_new_field[label]' => 'Contact Preference',
'_add_new_field[field_name]' => 'contact_preference',
'_add_new_field[type]' => 'text',
'_add_new_field[widget_type]' => 'optionwidgets_select',
);
$this
->drupalPost('admin/content/node-type/contact-details/fields', $edit, t('Save'));
$edit = array(
'allowed_values' => 'email|email
telephone|telephone
post|post',
);
$this
->drupalPost('admin/content/node-type/contact-details/fields/field_contact_preference', $edit, t('Save field settings'));
$edit = array(
'2[create identity content]' => 'create identity content',
'2[create biography content]' => 'create biography content',
'2[create contact_details content]' => 'create contact_details content',
);
$this
->drupalPost('admin/user/permissions', $edit, t('Save permissions'));
}
function settingsNodeprofileMatch(&$edit) {
$edit['field_match[0][field_match]'] = 'content_profile-identity cck:field_first_name:value';
$edit['field_match[1][field_match]'] = 'content_profile-identity cck:field_last_name:value';
$edit['field_match[10][field_match]'] = 'content_profile-biography cck:field_cv:value';
$edit['field_match[7][field_match]'] = 'content_profile-contact_details cck:field_can_be_contacted:value';
$edit['field_match[8][field_match]'] = 'content_profile-contact_details cck:field_contact_preference:value';
$edit['field_match[6][field_match]'] = 'content_profile-biography cck:field_blog:url';
$edit['field_match[11][field_match]'] = 'content_profile-biography cck:field_birthday:value';
}
function checkNodeprofileExist() {
$file_path = drupal_get_path('module', 'user_import') . '/sample.txt';
$handle = @fopen($file_path, "r");
$row = 0;
while ($csv = fgetcsv($handle, 1000, ',')) {
if ($row > 0) {
$user = user_load(array(
'mail' => $csv[5],
));
$identity = node_load(array(
'type' => 'identity',
'uid' => $user->uid,
), NULL, TRUE);
$this
->drupalGet("node/{$identity->nid}");
$this
->assertText(check_plain($csv[0]), "[Compare CSV and Profile data] Row: {$row} Field: First Name");
$this
->assertText(check_plain($csv[1]), "[Compare CSV and Profile data] Row: {$row} Field: Last Name " . $csv[1]);
$biography = node_load(array(
'type' => 'biography',
'uid' => $user->uid,
), NULL, TRUE);
$this
->drupalGet("node/{$biography->nid}");
$this
->assertText($csv[6], "[Compare CSV and Profile data] Row: {$row} Field: Blog");
$this
->assertText($csv[10], "[Compare CSV and Profile data] Row: {$row} Field: CV");
$birthday = format_date(strtotime($csv[11]), 'custom', 'D, j/m/Y');
$this
->assertText($birthday, "[Compare CSV and Profile data] Row: {$row} Field: Birthday " . $birthday);
$contact_details = node_load(array(
'type' => 'contact_details',
'uid' => $user->uid,
), NULL, TRUE);
$this
->drupalGet("node/{$contact_details->nid}");
if (isset($csv[7]) && !empty($csv[7])) {
$this
->assertText('Can be contacted', "[Compare CSV and Profile data] Row: {$row} Field: Contact Permission set");
}
else {
$this
->assertNoText('Can be contacted', "[Compare CSV and Profile data] Row: {$row} Field: Contact Permission not set");
}
$this
->assertText($csv[8], "[Compare CSV and Profile data] Row: {$row} Field: Contact Preference");
if (!empty($user->profile_interests)) {
$interests = explode(',', $user->profile_interests);
$this
->drupalGet('profile/profile_interests/' . $interests[0]);
$this
->assertWantedRaw('<a title="View user profile." href="/' . url('user/' . $user->uid) . '">' . $user->name . '</a>', '[Freeform List] User is listed on page about item in list');
}
}
$row++;
}
}
function tearDown() {
$users_email = $this
->usersList();
foreach ($users_email as $mail) {
$account = user_load(array(
'mail' => $mail,
));
if (!empty($account)) {
$identity = node_load(array(
'type' => 'identity',
'uid' => $account->uid,
));
$biography = node_load(array(
'type' => 'biography',
'uid' => $account->uid,
));
$contact_details = node_load(array(
'type' => 'contact_details',
'uid' => $account->uid,
));
node_delete($identity->nid);
node_delete($biography->nid);
node_delete($contact_details->nid);
user_delete(array(), $account->uid);
}
}
$import_id = $this
->importID();
$this
->assertTrue(!empty($import_id), t('Import ID: !id', array(
'!id' => $import_id,
)));
_user_import_settings_deletion($import_id);
taxonomy_del_vocabulary($this->vocabulary_id);
node_type_delete('dummy');
node_type_delete('identity');
node_type_delete('biography');
node_type_delete('contact_details');
parent::tearDown();
}
}
class UserImportProfileTestCase extends UserImportWebTestCase {
public static function getInfo() {
return array(
'name' => 'Import Users (Profile)',
'description' => 'Test import of user data into Profile module.',
'group' => 'User Import',
);
}
function setUp() {
parent::setUp('user_import', 'profile');
$this->admin_user = $this
->drupalCreateUser(array(
'administer users',
'access administration pages',
'administer site configuration',
));
$this->user_importer = $this
->drupalCreateUser(array(
'import users',
));
}
function testCreateImport() {
$this
->drupalLogin($this->admin_user);
$this
->profileFieldsCreate();
$this
->drupalGet('logout');
$this
->drupalLogin($this->user_importer);
$this
->settingsFileSelect();
$this
->importID($this
->getUrl());
$settings = array();
$this
->settingsEmailMatch($settings);
$this
->settingsProfileMatch($settings);
$this
->settingsIgnoreFirstLine($settings);
$this
->drupalPost($this
->getUrl(), $settings, 'Import');
$this
->checkProfileExist();
}
function profileFieldsCreate() {
$edit = array(
'category' => 'Name',
'title' => 'First Name',
'name' => 'profile_first_name',
);
$this
->drupalPost('admin/user/profile/add/textfield', $edit, t('Save field'));
$edit = array(
'category' => 'Name',
'title' => 'Last Name',
'name' => 'profile_last_name',
);
$this
->drupalPost('admin/user/profile/add/textfield', $edit, t('Save field'));
$edit = array(
'category' => 'Biography',
'title' => 'CV',
'name' => 'profile_cv',
);
$this
->drupalPost('admin/user/profile/add/textarea', $edit, t('Save field'));
$edit = array(
'category' => 'Contact Details',
'title' => 'Can Be Contacted',
'name' => 'profile_contact_permission',
);
$this
->drupalPost('admin/user/profile/add/checkbox', $edit, t('Save field'));
$edit = array(
'category' => 'Contact Details',
'title' => 'Contact Preference',
'name' => 'profile_contact_preference',
'options' => 'email,telephone,post',
);
$this
->drupalPost('admin/user/profile/add/selection', $edit, t('Save field'));
$edit = array(
'category' => 'Biography',
'title' => 'Interests',
'name' => 'profile_interests',
);
$this
->drupalPost('admin/user/profile/add/list', $edit, t('Save field'));
$edit = array(
'category' => 'Biography',
'title' => 'Blog',
'name' => 'profile_blog',
);
$this
->drupalPost('admin/user/profile/add/url', $edit, t('Save field'));
$edit = array(
'category' => 'Biography',
'title' => 'Birthday',
'name' => 'profile_birthday',
);
$this
->drupalPost('admin/user/profile/add/date', $edit, t('Save field'));
}
function settingsProfileMatch(&$edit) {
$edit['field_match[0][field_match]'] = 'profile-1';
$edit['field_match[1][field_match]'] = 'profile-2';
$edit['field_match[10][field_match]'] = 'profile-3';
$edit['field_match[7][field_match]'] = 'profile-4';
$edit['field_match[8][field_match]'] = 'profile-5';
$edit['field_match[9][field_match]'] = 'profile-6';
$edit['field_match[6][field_match]'] = 'profile-7';
$edit['field_match[11][field_match]'] = 'profile-8';
}
function checkProfileExist() {
$file_path = drupal_get_path('module', 'user_import') . '/sample.txt';
$handle = @fopen($file_path, "r");
$row = 0;
while ($csv = fgetcsv($handle, 1000, ',')) {
if ($row > 0) {
$user = user_load(array(
'mail' => $csv[5],
));
$profile_first_name = isset($user->profile_first_name) ? $user->profile_first_name : '';
$this
->assertEqual($profile_first_name, $csv[0], "[Compare CSV data to Profile data] Row: {$row} Field: First Name");
$profile_last_name = isset($user->profile_last_name) ? $user->profile_last_name : '';
$this
->assertEqual($profile_last_name, $csv[1], "[Compare CSV data to Profile data] Row: {$row} Field: Last Name");
$profile_blog = isset($user->profile_blog) ? $user->profile_blog : '';
$this
->assertEqual($profile_blog, $csv[6], "[Compare CSV data to Profile data] Row: {$row} Field: Blog");
$profile_contact_permission = isset($user->profile_contact_permission) ? $user->profile_contact_permission : '';
$file_field_value = !isset($csv[7]) || empty($csv[7]) ? 0 : 1;
$this
->assertEqual($profile_contact_permission, $file_field_value, "[Compare CSV data to Profile data] Row: {$row} Field: Contact Permission");
$profile_contact_preference = isset($user->profile_contact_preference) ? $user->profile_contact_preference : '';
$this
->assertEqual($profile_contact_preference, $csv[8], "[Compare CSV data to Profile data] Row: {$row} Field: Contact Preference");
$profile_interests = isset($user->profile_interests) ? $user->profile_interests : '';
$this
->assertEqual($profile_interests, $csv[9], "[Compare CSV data to Profile data] Row: {$row} Field: Profile Interests");
$profile_cv = isset($user->profile_cv) ? $user->profile_cv : '';
$this
->assertEqual($profile_cv, $csv[10], "[Compare CSV data to Profile data] Row: {$row} Field: CV");
$profile_birthday = isset($user->profile_birthday) ? implode('/', $user->profile_birthday) : '';
if (isset($user->profile_birthday)) {
$profile_birthday = $user->profile_birthday['month'] . '/' . $user->profile_birthday['day'] . '/' . $user->profile_birthday['year'];
}
else {
$profile_birthday = '';
}
$this
->assertEqual($profile_birthday, $csv[11], "[Compare CSV data to Profile data] Row: {$row} Field: Birthday");
}
$row++;
}
}
}