You are here

class UserImportNodeprofile in User Import 5

Same name and namespace in other branches
  1. 8 tests/user_import_nodeprofile.test \UserImportNodeprofile

Test import of user data into Profile module

Hierarchy

Expanded class hierarchy of UserImportNodeprofile

File

tests/user_import_nodeprofile.test, line 6

View source
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();

    // 		$html = $this->drupalGet('node/add/identity');
    //    var_dump($html);
    // Prepare a user to do testing
    $this
      ->get('logout');

    // log out first
    $this
      ->drupalLoginUser($this->user_importer);

    //$html = $this->drupalGet('admin/user/access');

    //var_dump($html);

    // 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\n\t\t1|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/access', $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,
        ));

        //if ($row == 1) var_dump($user);
        $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();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalTestCase::$_cleanupModules property
DrupalTestCase::$_cleanupRoles property
DrupalTestCase::$_cleanupUsers property
DrupalTestCase::$_cleanupVariables property
DrupalTestCase::$_content property
DrupalTestCase::assertCopy function Will trigger a pass if both parameters refer to different objects. Fail otherwise.
DrupalTestCase::assertEqual function Will trigger a pass if the two parameters have the same value only. Otherwise a fail.
DrupalTestCase::assertError function Confirms that an error has occurred and optionally that the error text matches exactly.
DrupalTestCase::assertErrorPattern function Confirms that an error has occurred and that the error text matches a Perl regular expression.
DrupalTestCase::assertIdentical function Will trigger a pass if the two parameters have the same value and same type. Otherwise a fail.
DrupalTestCase::assertIsA function Type and class test. Will pass if class matches the type name or is a subclass or if not an object, but the type is correct.
DrupalTestCase::assertNoErrors function Confirms that no errors have occurred so far in the test method.
DrupalTestCase::assertNotA function Type and class mismatch test. Will pass if class name or underling type does not match the one specified.
DrupalTestCase::assertNotEqual function Will trigger a pass if the two parameters have a different value. Otherwise a fail.
DrupalTestCase::assertNotIdentical function Will trigger a pass if the two parameters have the different value or different type.
DrupalTestCase::assertNotNull function Will be true if the value is set.
DrupalTestCase::assertNoUnwantedPattern function Will trigger a pass if the Perl regex pattern is not present in subject. Fail if found.
DrupalTestCase::assertNoUnwantedRaw function Will trigger a pass if the raw text is NOT found on the loaded page Fail otherwise.
DrupalTestCase::assertNull function Will be true if the value is null.
DrupalTestCase::assertReference function Will trigger a pass if both parameters refer to the same object. Fail otherwise.
DrupalTestCase::assertWantedPattern function Will trigger a pass if the Perl regex pattern is found in the subject. Fail otherwise.
DrupalTestCase::assertWantedRaw function Will trigger a pass if the raw text is found on the loaded page Fail otherwise.
DrupalTestCase::clickLink function Follows a link by name. Will click the first link found with this link text by default, or a later one if an index is given. Match is case insensitive with normalised space. Does make assertations if the click was sucessful or not and it does…
DrupalTestCase::drupalCheckAuth function @abstract Checks to see if we need to send a http-auth header to authenticate when browsing a site.
DrupalTestCase::drupalCreateRolePerm function Create a role / perm combination specified by permissions
DrupalTestCase::drupalCreateUserRolePerm function Creates a user / role / permissions combination specified by permissions
DrupalTestCase::drupalGet function @abstract Brokder for the get function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::drupalGetContent function @TODO: needs documentation
DrupalTestCase::drupalLoginUser function Logs in a user with the internal browser
DrupalTestCase::drupalModuleDisable function Disables a drupal module
DrupalTestCase::drupalModuleEnable function Enables a drupal module
DrupalTestCase::drupalPostRequest function Do a post request on a drupal page. It will be done as usual post request with SimpleBrowser
DrupalTestCase::drupalRawPost function @abstract Broker for the post function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::DrupalTestCase function
DrupalTestCase::drupalVariableSet function Set a druapl variable and keep track of the changes for tearDown()
DrupalTestCase::randomName function Generates a random string, to be used as name or whatever
DrupalTestCase::run function Just some info for the reporter
UserImportNodeprofile::$modules property
UserImportNodeprofile::checkNodeprofileExist function * Check data in CSV file matches data in profiles
UserImportNodeprofile::get_info function
UserImportNodeprofile::nodeprofileConfiguration function * Configure modules
UserImportNodeprofile::settingsNodeprofileMatch function * Match CSV columns to Profile fields
UserImportNodeprofile::tearDown function SimpleTest core method: code run after each and every test method. Overrides UserImportTest::tearDown
UserImportNodeprofile::testCreateImport function * User with right permissions creates import (with new settings) * - test import of user data into Nodeprofile module
UserImportTest::$user_importer property
UserImportTest::$user_limited_importer property
UserImportTest::checkAccountsExist function
UserImportTest::enableModules function * enable necessary modules
UserImportTest::importID function * Store import ID * - set on import settings page, retrieve on later tasks
UserImportTest::settingsEmailMatch function
UserImportTest::settingsFileSelect function * Select CSV file (the included example file)
UserImportTest::settingsIgnoreFirstLine function
UserImportTest::setUp function
UserImportTest::usersList function * List of users (email addresses) being imported * To Do - Generate this dynamically, bearing in mind it could be used for stress testing