You are here

public function BmTestBasics::testFieldValidators in Backup and Migrate 7.3

Test the custom validators.

File

tests/BmTestBasics.test, line 112
Tests for different parts of the Backup Migrate system.

Class

BmTestBasics
Test that the front page still loads.

Code

public function testFieldValidators() {

  // Need this file loaded for the custom validators.
  module_load_include('advanced_settings.inc', 'backup_migrate');
  $field = 'mock_field';
  $element = array(
    '#value' => 'a',
    '#title' => '',
    '#parents' => array(
      $field,
    ),
  );

  // Test the memory limit validator.
  $element['#title'] = 'Mock Field (Memory Limit)';
  $test_values = array(
    // Value to be tested => validity.
    // Special meaning: no limit.
    '-1' => TRUE,
    '50' => TRUE,
    // 5 megabytes.
    '5M' => TRUE,
    // 500 kilobytes.
    '.5M' => TRUE,
    // 5 gigabytes.
    '5G' => TRUE,
    '.5G' => TRUE,
    '1.5G' => TRUE,
    '0' => TRUE,
    'a' => FALSE,
    '-2' => FALSE,
    // You cannot have half a byte.
    '1.5' => FALSE,
    '5T' => FALSE,
    '5 G' => FALSE,
    '.5.5G' => FALSE,
  );
  foreach ($test_values as $value => $valid) {
    $this
      ->assertValidFieldValue('backup_migrate_memory_limit_validate', $element, $value, $valid, 'memory limit value');
  }

  // Test the positive non-zero integer validator.
  $element['#title'] = 'Mock Field (Unsigned Integer)';
  $test_values = array(
    '0' => TRUE,
    '1' => TRUE,
    '10000' => TRUE,
    // NaN.
    'a' => FALSE,
    // Not an integer.
    '.5' => FALSE,
    // Not positive.
    '-1' => FALSE,
  );
  foreach ($test_values as $value => $valid) {
    $this
      ->assertValidFieldValue('backup_migrate_unsigned_integer_validate', $element, $value, $valid, 'zero or a positive integer');
  }

  // Test the validator for decimals ranging from 0 to 1.
  $element['#title'] = 'Mock Field (Decimal between 0 and 1)';
  $test_values = array(
    '0' => TRUE,
    '1' => TRUE,
    '0.1' => TRUE,
    '.1' => TRUE,
    '1.1' => FALSE,
    '-1' => FALSE,
    '-1.1' => FALSE,
    '2' => FALSE,
    'a' => FALSE,
  );
  foreach ($test_values as $value => $valid) {
    $this
      ->assertValidFieldValue('backup_migrate_fraction_validate', $element, $value, $valid, 'decimal between 0 and 1');
  }
}