You are here

public function RulesFormsConditionsTestCase::testElementValueCondition in Rules Forms Support 7.2

Same name and namespace in other branches
  1. 7 rules_forms.test \RulesFormsConditionsTestCase::testElementValueCondition()

Tests callback for Condition: Element has value.

File

./rules_forms.test, line 361
Rules Forms Support tests.

Class

RulesFormsConditionsTestCase
Tests Rules Forms conditions.

Code

public function testElementValueCondition() {

  // Test the condition with a regular value in the field.
  $form = array();
  $test_form['test'] = array(
    '#type' => 'textfield',
    '#value' => 'test',
    '#default_value' => 'test',
  );
  $form = new ArrayObject($test_form);
  $formState = new ArrayObject(array());

  // Test with a correctly matching field value.
  $condition1 = rules_condition('rules_forms_element_value', array(
    'element' => 'textfield:test',
    'value' => 'test',
    'regex' => 0,
  ));
  $result1 = $condition1
    ->executeByArgs(array(
    'form' => $form,
    'form_state' => $formState,
  ));
  $this
    ->assertTrue($result1, t('Element values are equal.'));

  // Test with an incorrectly matching field value.
  $condition2 = rules_condition('rules_forms_element_value', array(
    'element' => 'textfield:test',
    'value' => 'test',
    'regex' => 0,
  ));
  $result2 = $condition2
    ->executeByArgs(array(
    'form' => $form,
    'form_state' => $formState,
  ));
  $this
    ->assertFalse($result2, t('Element values are not equal.'));

  // Test with a correctly matching regular expression.
  $condition3 = rules_condition('rules_forms_element_value', array(
    'element' => 'textfield:test',
    'value' => '/se/',
    'regex' => 1,
  ));
  $result3 = $condition3
    ->executeByArgs(array(
    'form' => $form,
    'form_state' => $formState,
  ));
  $this
    ->assertTrue($result3, t('Regular expression evaluation: value is equal.'));

  // Test with a correctly matching regular expression.
  $condition4 = rules_condition('rules_forms_element_value', array(
    'element' => 'textfield:test',
    'value' => '/es/',
    'regex' => 1,
  ));
  $result4 = $condition4
    ->executeByArgs(array(
    'form' => $form,
    'form_state' => $formState,
  ));
  $this
    ->assertFalse($result4, t('Regular expression evaluation: value is not equal.'));

  // Test the condition with only a default value in the field.
  unset($test_form['test']['#value']);
  $form = new ArrayObject($test_form);

  // Test with a correctly matching default field value.
  $condition5 = rules_condition('rules_forms_element_value', array(
    'element' => 'textfield:test',
    'value' => 'test',
    'regex' => 0,
  ));
  $result5 = $condition5
    ->executeByArgs(array(
    'form' => $form,
    'form_state' => $formState,
  ));
  $this
    ->assertTrue($result5, t('Element values are equal.'));

  // Test with an incorrectly matching default field value.
  $condition6 = rules_condition('rules_forms_element_value', array(
    'element' => 'textfield:test',
    'value' => 'test',
    'regex' => 0,
  ));
  $result6 = $condition6
    ->executeByArgs(array(
    'form' => $form,
    'form_state' => $formState,
  ));
  $this
    ->assertFalse($result6, t('Element values are not equal.'));

  // Test with a correctly matching regular expression default value.
  $condition7 = rules_condition('rules_forms_element_value', array(
    'element' => 'textfield:test',
    'value' => '/se/',
    'regex' => 1,
  ));
  $result7 = $condition7
    ->executeByArgs(array(
    'form' => $form,
    'form_state' => $formState,
  ));
  $this
    ->assertTrue($result7, t('Regular expression evaluation: value is equal.'));

  // Test with a correctly matching regular expression.
  $condition8 = rules_condition('rules_forms_element_value', array(
    'element' => 'textfield:test',
    'value' => '/es/',
    'regex' => 1,
  ));
  $result8 = $condition8
    ->executeByArgs(array(
    'form' => $form,
    'form_state' => $formState,
  ));
  $this
    ->assertFalse($result8, t('Regular expression evaluation: value is not equal.'));
}