<?php 
/**
 *  Test import of user data into Profile module
 */

class UserImportNodeprofile extends UserImportTest {

  var $modules = array('nodeprofile', 'nodefamily', 'subform_element', 'content', 'number', 'optionwidgets', 'text', 'link', 'date', 'date_api', 'node_import');
  
  function get_info() {
    return array(
      'name' => t('Import Users (Nodeprofile)'),
      'desc' => t('Test import of user data into Nodeprofile module.'),
      'group' => t('User Import'),
    );
  }

  /**
   *  User with right permissions creates import (with new settings)
   *  - test import of user data into Nodeprofile module 
   */
  function testCreateImport() {
    $this->enableModules($this->modules);
    $this->nodeprofileConfiguration();
    
    // Prepare a user to do testing 
    $this->get('logout'); // log out first
    $this->drupalLoginUser($this->user_importer);     
  
    // Select CSV file (the included example file)
    $this->settingsFileSelect();
    
    // import settings 
    $this->importID($this->getUrl()); // store import ID for later
    $settings = array();
    $this->settingsEmailMatch($settings);
    $this->settingsNodeprofileMatch($settings);
    $this->settingsIgnoreFirstLine($settings);
    $this->drupalPostRequest($this->getUrl(), $settings, 'Import');

    // check if users have been imported
    $this->checkNodeprofileExist();
    
    
  }

  /**
   *  Configure modules
   */
  function nodeprofileConfiguration() { 
    $this->admin_user = $this->drupalCreateUserRolePerm(array('administer users', 'access administration pages', 'administer access control', 'administer site configuration', 'administer content types', 'administer taxonomy'));    
    $this->drupalLoginUser($this->admin_user);
    
    // create Identity content type
    $edit = array('name' => 'Identity', 'type' => 'identity', 'title_label' => 'Title', 'body_label' => NULL, 'node_options[promote]' => 0, 'nodefamily_max' => 1, 'nodeprofile' => 1); 
    $this->drupalPostRequest('admin/content/types/add', $edit, t('Save content type'));

    // create First Name field
    $edit = array('label' => 'first_name', 'field_widget_type' => 'text-text'); 
    $this->drupalPostRequest('admin/content/types/identity/add_field', $edit, t('Create field'));

    $edit = array('label' => 'First Name', 'required' => 1); 
    $this->drupalPostRequest('admin/content/types/identity/fields/field_first_name', $edit, t('Save field settings'));

    // create Last Name field
    $edit = array('label' => 'last_name', 'field_widget_type' => 'text-text'); 
    $this->drupalPostRequest('admin/content/types/identity/add_field', $edit, t('Create field'));

    $edit = array('label' => 'Last Name', 'required' => 1); 
    $this->drupalPostRequest('admin/content/types/identity/fields/field_last_name', $edit, t('Save field settings'));

    // create Biography content type
    $edit = array('name' => 'Biography', 'type' => 'biography', 'title_label' => 'Title', 'body_label' => NULL, 'node_options[promote]' => 0, 'nodefamily_max' => 1, 'nodeprofile' => 1); 
    $this->drupalPostRequest('admin/content/types/add', $edit, t('Save content type'));
    
    // create CV field
    $edit = array('label' => 'cv', 'field_widget_type' => 'text-text'); 
    $this->drupalPostRequest('admin/content/types/biography/add_field', $edit, t('Create field'));

    $edit = array('label' => 'CV', 'required' => 1, 'rows' => 5); 
    $this->drupalPostRequest('admin/content/types/biography/fields/field_cv', $edit, t('Save field settings'));
    
    // create Interests Vocabulary  - TO DO
    $edit = array('name' => 'Interests', 'nodes[biography]' => 'biography', 'tags' => 1); 
    $this->drupalPostRequest('admin/content/taxonomy/add/vocabulary', $edit, t('Submit'));

    $vocabularies = taxonomy_get_vocabularies('biography');
    
    foreach ($vocabularies as $vocabulary) {
      $this->vocabulary_id = $vocabulary->vid;
    }

    // create Blog field (URL)
    $edit = array('label' => 'blog', 'field_widget_type' => 'link-link'); 
    $this->drupalPostRequest('admin/content/types/biography/add_field', $edit, t('Create field'));

    $edit = array('label' => 'Blog'); 
    $this->drupalPostRequest('admin/content/types/biography/fields/field_blog', $edit, t('Save field settings'));
      
    
    // create Birthday field (date)
    $edit = array('label' => 'birthday', 'field_widget_type' => 'datestamp-date_text'); 
    $this->drupalPostRequest('admin/content/types/biography/add_field', $edit, t('Create field'));

    $edit = array('label' => 'Birthday'); 
    $this->drupalPostRequest('admin/content/types/biography/fields/field_birthday', $edit, t('Save field settings'));
    
    // create Contact Details contact type
    $edit = array('name' => 'Contact Details', 'type' => 'contact_details', 'title_label' => 'Title', 'body_label' => NULL, 'node_options[promote]' => 0, 'nodefamily_max' => 1, 'nodeprofile' => 1); 
    $this->drupalPostRequest('admin/content/types/add', $edit, t('Save content type'));
    
    // create Can Be Contacted field
    $edit = array('label' => 'can_be_contacted', 'field_widget_type' => 'number_integer-options_onoff'); 
    $this->drupalPostRequest('admin/content/types/contact-details/add_field', $edit, t('Create field'));
 
    $edit = array('label' => 'Can Be Contacted', 'allowed_values' => "0
    1|Can be contacted"); 
    $this->drupalPostRequest('admin/content/types/contact-details/fields/field_can_be_contacted', $edit, t('Save field settings'));
    
    // create Contact Preference field
    $edit = array('label' => 'contact_preference', 'field_widget_type' => 'text-options_select'); 
    $this->drupalPostRequest('admin/content/types/contact-details/add_field', $edit, t('Create field'));

    $edit = array('label' => 'Contact Preference', 'allowed_values' => 'email|email
    telephone|telephone
    post|post'); 
    $this->drupalPostRequest('admin/content/types/contact-details/fields/field_contact_preference', $edit, t('Save field settings')); 
                    
    // set access perm for authenticated users to creade profile nodes
    $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->drupalPostRequest('admin/user/permissions', $edit, t('Save permissions'));
  }


  /**
   *  Match CSV columns to Profile fields
   */
  function settingsNodeprofileMatch(&$edit) {
    $edit['field_match[0][field_match]'] = 'nodeprofile-identity field_first_name_value'; // First Name
    $edit['field_match[1][field_match]'] = 'nodeprofile-identity field_last_name_value'; // Last Name 
    $edit['field_match[10][field_match]'] = 'nodeprofile-biography field_cv_value'; // CV 
    $edit['field_match[7][field_match]'] = 'nodeprofile-contact_details field_can_be_contacted_value'; // Contact Permision   
    $edit['field_match[8][field_match]'] = 'nodeprofile-contact_details field_contact_preference_value'; // Contact Preference
    $edit['field_match[9][field_match]'] = 'taxonomy-' . $this->vocabulary_id; // Interests       
    $edit['field_match[6][field_match]'] = 'nodeprofile-biography field_blog_url'; // Blog
    $edit['field_match[11][field_match]'] = 'nodeprofile-biography field_birthday_value'; // Birthday
  }

  /**
   *  Check data in CSV file matches data in profiles
   */
  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])); 
        // test each data cell against nodeprofile field content
        $identity = node_load(array('type' => 'identity', 'uid' => $user->uid));
        $biography = node_load(array('type' => 'biography', 'uid' => $user->uid));
        $contact_details = node_load(array('type' => 'contact_details', 'uid' => $user->uid));

        $this->assertEqual($identity->field_first_name[0]['value'], $csv[0], "[Compare CSV and Profile data] Row: $row Field: First Name");
        $this->assertEqual($identity->field_last_name[0]['value'], $csv[1], "[Compare CSV and Profile data] Row: $row Field: Last Name");
        $this->assertEqual($biography->field_blog[0]['url'], $csv[6], "[Compare CSV and Profile data] Row: $row Field: Blog");
        $this->assertEqual($contact_details->field_can_be_contacted[0]['value'], $csv[7], "[Compare CSV and Profile data] Row: $row Field: Contact Permission");
        $this->assertEqual($contact_details->field_contact_preference[0]['value'], $csv[8], "[Compare CSV and Profile data] Row: $row Field: Contact Preference");
        $this->assertEqual($user->profile_interests, $csv[9], "[Compare CSV and Profile data] Row: $row Field: Profile Interests");
        $this->assertEqual($biography->field_cv[0]['value'], $csv[10], "[Compare CSV and Profile data] Row: $row Field: CV");
        $this->assertEqual($biography->field_birthday[0]['value'], $csv[11], "[Compare CSV and Profile data] Row: $row Field: Birthday");
        
        //test interests link on profile page
        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++;
    }
    
  }
  
  /**
   * SimpleTest core method: code run after each and every test method.
  */
  function tearDown() {

    // delete accounts of users imported
    $users_email = $this->usersList();

    foreach ($users_email as $mail) {
      $account = user_load(array('mail' => $mail));
      // delete node profile nodes
      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);  
      }  
    }
    
    // delete the import
    $import_id = $this->importID();   
    $this->assertTrue(!empty($import_id), t('Import ID: !id', array('!id' => $import_id))); 
    _user_import_settings_deletion($import_id);
  
    // delete vocabulary
    taxonomy_del_vocabulary($this->vocabulary_id);
 
    // uninstall modules
    // - tear down disable doesn't seem to do this 
    foreach($this->modules as $module) {
      if (function_exists($module . '_uninstall')) { 
        $result = call_user_func_array($module . '_uninstall', array());
      } 
    }
    
    // delete nodeprofile content types 
    node_type_delete('dummy'); // (for some reason) the first node_type_delete is completely ignored
    node_type_delete('identity');
    node_type_delete('biography');
    node_type_delete('contact_details');

    // Always call the tearDown() function from the parent class.
    parent::tearDown();
  }

}
