<?php

/**
 * @file
 * Shared functionality to make the rest of the tests simpler.
 */

/**
 * Base class for testing a module's custom tags.
 */
abstract class BmTestBase extends DrupalWebTestCase {

  /**
   * {@inheritdoc}
   */
  public function setUp(array $modules = array()) {
    $modules[] = 'backup_migrate';
    parent::setUp($modules);
  }

  /**
   * Log in as user 1.
   *
   * The benefit of doing this is that it ignores permissions entirely, so the
   * raw functionality can be tested.
   */
  protected function loginUser1() {
    // Load user 1.
    $account = user_load(1, TRUE);

    // Reset the password.
    $password = user_password();
    $edit = array(
      'pass' => $password,
    );
    user_save($account, $edit);
    $account->pass_raw = $password;

    // Login.
    $this->drupalLogin($account);
  }

  /**
   * {@inheritdoc}
   */
  protected function verbose($message, $title = NULL) {
    // Handle arrays, objects, etc.
    if (!is_string($message)) {
      $message = "<pre>\n" . print_r($message, TRUE) . "\n</pre>\n";
    }

    // Optional title to go before the output.
    if (!empty($title)) {
      $title = '<h2>' . check_plain($title) . "</h2>\n";
    }

    parent::verbose($title . $message);
  }

  /**
   * Confirm that a selector has the expected items.
   */
  protected function assertSelectOptions($select_id, array $options, $message = '') {
    $elements = $this
      ->xpath('//select[@id=:id]//option', array(
        ':id' => $select_id,
      ));
    $results = $this->assertEqual(count($elements), count($options), t('The same number of items were found as were requested'));
    $this->verbose($elements);

    foreach ($options as $option) {
      $elements = $this
        ->xpath('//select[@id=:id]//option[@value=:option]', array(
          ':id' => $select_id,
          ':option' => $option,
        ));
      $this->verbose($elements);
      $results *= $this->assertTrue(isset($elements[0]), $message ? $message : t('Option @option for field @id is present.', array(
        '@option' => $option,
        '@id' => $select_id,
      )), t('Browser'));
    }

    return $results;
  }

  /**
   * Confirm that a specific selector does not have items selected.
   */
  protected function assertNoOptionsSelected($id, $message = '') {
    $elements = $this
      ->xpath('//select[@id=:id]//option[@selected="selected"]', array(
        ':id' => $id,
      ));
    return $this
      ->assertTrue(!isset($elements[0]), $message ? $message : t('Field @id does not have any selected items.', array(
        '@id' => $id,
      )), t('Browser'));
  }

  /**
   * Work out which compressor systems are supported by PHP.
   *
   * @return array
   *   The list of supported compressors. Will always include the item 'none'.
   */
  protected function supportedCompressors() {
    $items = array('none');

    // Work out which systems are supported.
    if (@function_exists("gzencode")) {
      $items[] = 'gzip';
    }
    if (@function_exists("bzcompress")) {
      $items[] = 'bzip';
    }
    if (class_exists('ZipArchive')) {
      $items[] = 'zip';
    }

    return $items;
  }

  /**
   * Get a list of the files in a specific destination.
   *
   * @param string $destination_id
   *   The ID of the destination to check. Defaults to the manual file path.
   *
   * @return array
   *   The backup files found in the requested backup destination.
   */
  protected function listBackupFiles($destination_id = 'manual') {
    $items = array();

    backup_migrate_include('destinations');

    // Load the destination object.
    $destination = backup_migrate_get_destination($destination_id);
    if (!empty($destination)) {
      $items = $destination->list_files();
    }

    return $items;
  }

  /**
   * Run a specific backup.
   *
   * @param string $destination_id
   *   The ID of the destination to check. Defaults to the manual file path.
   */
  protected function runBackup($destination_id = 'manual') {
    $this->drupalGet(BACKUP_MIGRATE_MENU_PATH);
    $this->assertResponse(200);
    $edit = array(
      'destination_id' => $destination_id,
    );
    $this->drupalPost(NULL, $edit, 'Backup now');
    $this->assertResponse(200);
    // Confirm the response is as expected. This is split up into separate
    // pieces because it'd be more effort than is necessary right now to confirm
    // what the exact filename is.
    $this->assertText('Default Database backed up successfully');
    $this->assertText('in destination');
    $this->assertLink('download');
    $this->assertLink('restore');
    $this->assertLink('delete');
  }

  /**
   * Delete all of the files in a specific backup destination.
   *
   * @param string $destination_id
   *   The ID of the destination to check. Defaults to the manual file path.
   */
  protected function deleteBackups($destination_id = 'manual') {
    $destination = backup_migrate_get_destination($destination_id);
    $files = $this->listBackupFiles($destination_id);
    if (!empty($files)) {
      foreach ($files as $file_id => $file) {
        $destination->delete_file($file_id);
      }
    }
  }

  /**
   * Work out whether a backup filename includes a timestamp.
   *
   * @param object $file
   *   The backup file to examine.
   *
   * @return mixed
   *   Returns 1 if found, 0 if not found, FALSE if an error occurs.
   */
  protected function fileHasTimestamp($file) {
    // Get the default filename, this is used later.
    backup_migrate_include('files');
    $default_filename = _backup_migrate_default_filename();
    $ext = implode('.', $file->ext);
    $pattern = "/{$default_filename}-(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d)-(\d\d)-(\d\d).{$ext}/";

    return preg_match($pattern, $file->file_info['filename']);
  }

  /**
   * Confirm that a backup filename includes a timestamp.
   *
   * @param object $file
   *   The backup file to examine.
   *
   * @return bool
   *   Indicates whether the file includes a timestamp.
   */
  protected function assertFileTimestamp($file) {
    return $this->assertTrue($this->fileHasTimestamp($file));
  }

  /**
   * Confirm that a backup filename does not include a timestamp.
   *
   * @param object $file
   *   The backup file to examine.
   *
   * @return bool
   *   Indicates whether the file does not include a timestamp.
   */
  protected function assertNoFileTimestamp($file) {
    return !$this->assertFalse($this->fileHasTimestamp($file));
  }

  /**
   * Get a profile.
   *
   * @param string $profile_id
   *   The name of the profile to load. Defaults to 'default'.
   *
   * @return object
   *   The profile object.
   */
  protected function getProfile($profile_id = 'default') {
    backup_migrate_include('profiles');
    return backup_migrate_get_profile($profile_id);
  }

}
