<?php

/**
 * @file
 * Contains StepTestCase.
 */

/**
 * Testing Multi-Step Registration functionality.
 */
class StepTestCase extends DrupalWebTestCase {

  protected $account;

  public static function getInfo() {
    return array(
      'name' => 'Multi-step registration form',
      'description' => 'Tests functionality of Step module.',
      'group' => 'User',
    );
  }

  function setUp() {
    parent::setUp('step_test');

    $this->account = $this->drupalCreateUser(array(
      'administer profile types',
      'administer profiles',
      'administer site configuration'
    ));
  }

  public function testStep() {
    $this->drupalLogin($this->account);

    // Add first wizard step.
    $edit = array(
      'title' => 'Step 1',
      'id' => 'step1',
      'description[value]' => $this->randomString(100),
      'description[format]' => 'filtered_html',
    );
    $this->drupalPost('admin/config/people/step/create', $edit, t('Create'));
    // And the second.
    $edit = array(
      'title' => 'Step 2',
      'id' => 'step2',
      'description[value]' => $this->randomString(100),
      'description[format]' => 'plain_text',
    );
    $this->drupalPost('admin/config/people/step/create', $edit, t('Create'));
    $this->assertText('Step 1');
    $this->assertText('Step 2');
    // Make sure that Step 1 comes first.
    $edit = array(
      'steps[step1][weight]' => 0,
      'steps[step2][weight]' => 10,
      'step_show_back' => TRUE,
    );
    $this->drupalPost(NULL, $edit, t('Save Changes'));
    // Check stored values.
    $step1 = step_load('step1');
    $this->assertEqual($step1['weight'], 0);
    $step1 = step_load('step2');
    $this->assertEqual($step1['weight'], 10);

    // Add a field and wizard setting profile2 type 'main'.
    $field = $this->getFieldPostData();
    $step1_field = 'profile_main[field_' . $field['fields[_add_new_field][field_name]'] . '][und][0][value]';
    $step1_field_name = 'field_' . $field['fields[_add_new_field][field_name]'];
    $this->drupalPost('admin/structure/profiles/manage/main/fields', $field, t('Save'));
    $edit = array('data[step_step]' => 'step1');
    $this->drupalPost('admin/structure/profiles/manage/main', $edit, t('Save profile type'));

    // Create the second profile2 type and add it to the step2.
    $profile2 = drupal_strtolower($this->randomName());
    $edit = array(
      'label' => $this->randomName(),
      'type' => $profile2,
      'data[step_step]' => 'step2',
    );
    $this->drupalPost('admin/structure/profiles/add', $edit, t('Save profile type'));
    $field = $this->getFieldPostData();
    $step2_field = 'profile_' . $profile2 . '[field_' . $field['fields[_add_new_field][field_name]'] . '][und][0][value]';
    $step2_field_name = 'field_' . $field['fields[_add_new_field][field_name]'];
    $this->drupalPost('admin/structure/profiles/manage/' . $profile2 . '/fields', $field, t('Save'));

    $this->drupalLogout();

    // Register as an anonymous.
    $username = drupal_strtolower($this->randomName());
    $edit = array(
      'name' => drupal_strtolower($this->randomName()),
      'mail' => "$username@example.com",
    );
    $this->drupalPost('user/register', $edit, t('Continue'));

    // Second step. I'm expecting to find the $step1_field there.
    $this->assertFieldByName($step1_field);

    // Post the second step form.
    $step1_value = $this->randomString();
    $this->drupalPost(NULL, array($step1_field => $step1_value), t('Continue'));

    // Third step. I'm expecting to find the $step2_field there.
    $this->assertFieldByName($step2_field);

    // We are trying to go back. We must find the value entered in step 1 loaded
    // from the database.
    $step2_value = $this->randomString();
    $this->drupalPost(NULL, array($step2_field => $step2_value), t('Back'));
    $this->assertFieldByName($step1_field, $step1_value);

    // Finalize.
    $this->drupalPost('user/register/step2', array(), t('Finish'));

    // Check if values are saved in the backend.

    // Get the latest registered user ID.
    $uid = db_query_range("SELECT uid FROM {users} ORDER BY uid DESC", 0, 1)
      ->fetchField();
    $account = user_load($uid);

    $profile = profile2_load_by_user($account, 'main');
    $this->assertEqual($profile->{$step1_field_name}[LANGUAGE_NONE][0]['value'], $step1_value);
    $profile = profile2_load_by_user($account, $profile2);
    $this->assertEqual($profile->{$step2_field_name}[LANGUAGE_NONE][0]['value'], $step2_value);
  }

  protected function getFieldPostData() {
    return array(
      'fields[_add_new_field][label]' => $this->randomName(),
      'fields[_add_new_field][field_name]' => drupal_strtolower($this->randomName()),
      'fields[_add_new_field][type]' => 'text',
      'fields[_add_new_field][widget_type]' => 'text_textfield',
    );
  }

}
