<?php

/**
 * @file
 * Test the basic functions of the user delete module.
 */

/**
 * Inactive user module testcase.
 */
class UserDeleteTest extends DrupalWebTestCase {

  /**
   * User with administration privileges for inactive user module.
   *
   * @var stdlcass
   */
  protected $admin;

  /**
   * User account to test the module.
   *
   * @var stdlcass
   */
  protected $account;

  public static function getInfo() {
    return array(
      'name' => t('User delete'),
      'description' => t('Test user delete module.'),
      'group' => t('User delete')
    );
  }

  function setUp() {
    parent::setUp('user_delete');
    // Create a regular user with permissions to delete its own account.
    $this->account = $this->drupalCreateUser(array('delete own account'));
    // Create an admin user to configure user delete module.
    $this->admin = $this->drupalCreateUser(array('administer users'));
  }

  /**
   * Check admin inteface has not changed and settings ar stored.
   */
  function testUserDeleteAdminInterface() {
    // Login as administrator
    $this->drupalLogin($this->admin);

    // Verify that the following fields exist. Warning, select list may fail.
    $this->drupalGet('admin/user/user_delete');
    $defaults = array(
      'user_delete_default_action' => variable_get('user_delete_default_action', 0),
      'user_delete_redirect' => variable_get('user_delete_redirect', ''),
      'user_delete_backup' => variable_get('user_delete_backup', 0),
      'user_delete_backup_period' => variable_get('user_delete_backup_period', 60*60*24*7*12),
    );
    foreach ($defaults as $name => $value) {
      $this->assertFieldByName(
        $name,
        $value,
        t('Default @field field found successfully.', array('@field' => $name))
      );
    }

    // Save new settings and verify they are saved.
    $options = array(
      'user_delete_default_action' => 'user_delete_block',
      'user_delete_redirect' => '<front>',
      'user_delete_backup' => true,
      'user_delete_backup_period' => '4838400',
    );
    $this->drupalPost('admin/user/user_delete', $options, t('Save configuration'));
    foreach ($defaults as $name => $value) {
      $this->assertEqual(
        $defaults[$name],
        $value,
        t('Option %field saved successfully.', array('%field' => $name))
      );
    }
  }

  /**
   * Check admin inteface has not changed and settings ar stored.
   */
  function testUserDeleteInterface() {

    // Try an unpriviledge user,
    $account = $this->drupalCreateUser();
    $this->drupalLogin($this->account);
    // By default the user may select the delete option.
    $this->drupalGet('user/'. $this->account->uid .'/edit');
    $this->assertNoFieldById(
      'edit-delete',
      t('Delete Account button not found.')
    );

    // try with a priviledge account.
    $this->drupalLogin($this->account);

    // By default the user may select the delete option.
    $this->drupalGet('user/'. $this->account->uid .'/edit');
    $this->assertFieldById(
      'edit-delete',
      t('Delete Account'),
      t('Delete Account button found.')
    );

    // Try to delete this account
    $this->drupalPost('user/'. $this->account->uid .'/edit', array(), t('Delete Account'));
    // User should be able to select the delete action. First option is to 
    // disable and keep content.
    $this->assertField(
      'user_delete_action',
      'user_delete_block',
      t('Delete options found in Confirmation found.')
    );

    /**
     * Now change behavior and select a default option for the users.
     * Users will not be able to select how to delete their account.
     */
    variable_set('user_delete_default_action', 'user_delete_block');
    $this->drupalPost('user/'. $this->account->uid .'/edit', array(), t('Delete Account'));
    // User should be able to select the delete action. First option is to
    // disable and keep content.
    $this->assertNoField(
      'user_delete_action',
      t('Delete options not found in confirmation form.')
    );
    $this->assertText(t('The account will be disabled, all submitted content will be kept.'), t('Text for user delete with selected option found.'));
  }



}
