function RulesUiTestCase::testComponentVariables

Tests setting component variables in the UI.

See also

https://www.drupal.org/project/rules/issues/3005864

File

rules_admin/tests/rules_admin.test, line 101

Class

RulesUiTestCase
Tests for creating rules through the UI.

Code

public function testComponentVariables() {
    // Create a simple user account with permission to create a rule.
    $user = $this->drupalCreateUser(array(
        'access administration pages',
        'administer rules',
        'bypass rules access',
    ));
    $this->drupalLogin($user);
    // Variables cannot be set/changed for code-provided components, so we must
    // build our test component here.
    // Create an 'action set' rule component.
    $action_set = rules_action_set(array(
        'node' => array(
            'type' => 'node',
            'label' => 'Input node',
        ),
    ));
    $action_set->save('rules_test_variables');
    // Verify that our test component appears in the UI.
    $this->drupalGet('admin/config/workflow/rules/components');
    $this->assertText('rules_test_variables', 'Test component is defined.');
    // Visit the component edit page.
    $this->clickLink('rules_test_variables');
    $this->assertText('Variables are normally input parameters for the component', 'Component variables form is present.');
    // Check for presence of pre-existing variable fields AND for presence
    // of exactly one row of fields for new variable input.
    $this->assertFieldByName('settings[vars][items][node][label]', 'Input node', 'Label of pre-existing variable is defined.');
    $this->assertFieldByName('settings[vars][items][0][label]', '', 'First row for new variable is present.');
    // Should only have line [0], not [1] or [2].
    $this->assertNoFieldByName('settings[vars][items][1][label]', NULL, 'Second row for new variable is missing.');
    $this->assertNoFieldByName('settings[vars][items][2][label]', NULL, 'Third row for new variable is missing.');
    // Save new variable.
    $edit = array(
        'settings[vars][items][0][type]' => 'integer',
        'settings[vars][items][0][label]' => 'Integer value',
        'settings[vars][items][0][name]' => 'integer_value',
    );
    $this->drupalPost(NULL, $edit, 'Save changes');
    $this->assertFieldByName('settings[vars][items][node][label]', 'Input node', 'Label of pre-existing variable is defined.');
    $this->assertFieldByName('settings[vars][items][integer_value][label]', 'Integer value', 'Label of variable is defined.');
    $this->assertFieldByName('settings[vars][items][integer_value][name]', 'integer_value', 'Machine name of variable is defined.');
    // Check to see if "Add more" button properly adds one more row for
    // variable input while preserving what we've already entered.
    $edit = array(
        'settings[vars][items][0][type]' => 'decimal',
        'settings[vars][items][0][label]' => 'Decimal value',
        'settings[vars][items][0][name]' => 'decimal_value',
    );
    $this->drupalPostAjax(NULL, $edit, array(
        'op' => 'Add more',
    ));
    $this->assertFieldByName('settings[vars][items][node][label]', 'Input node', 'Label of pre-existing variable is defined.');
    $this->assertFieldByName('settings[vars][items][integer_value][label]', 'Integer value', 'Label of integer variable is defined.');
    $this->assertFieldByName('settings[vars][items][decimal_value][label]', 'Decimal value', 'Label of decimal variable is defined.');
    $this->assertFieldByName('settings[vars][items][0][label]', NULL, 'First row for new variable is present.');
}