function BrowserTestBaseTest::testFieldAssertsForCheckbox

Same name in other branches
  1. 9 core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php \Drupal\FunctionalTests\BrowserTestBaseTest::testFieldAssertsForCheckbox()
  2. 10 core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php \Drupal\FunctionalTests\BrowserTestBaseTest::testFieldAssertsForCheckbox()
  3. 11.x core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php \Drupal\FunctionalTests\BrowserTestBaseTest::testFieldAssertsForCheckbox()

Tests legacy field asserts for checkbox field type.

File

core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php, line 546

Class

BrowserTestBaseTest
Tests BrowserTestBase functionality.

Namespace

Drupal\FunctionalTests

Code

public function testFieldAssertsForCheckbox() {
    $this->drupalGet('test-field-xpath');
    // Part 1 - Test by name.
    // Test that checkboxes are found/not found correctly by name, when using
    // TRUE or FALSE to match their 'checked' state.
    $this->assertFieldByName('checkbox_enabled', TRUE);
    $this->assertFieldByName('checkbox_disabled', FALSE);
    $this->assertNoFieldByName('checkbox_enabled', FALSE);
    $this->assertNoFieldByName('checkbox_disabled', TRUE);
    // Test that checkboxes are found by name when using NULL to ignore the
    // 'checked' state.
    $this->assertFieldByName('checkbox_enabled', NULL);
    $this->assertFieldByName('checkbox_disabled', NULL);
    // Test that checkboxes are found by name when passing no second parameter.
    $this->assertFieldByName('checkbox_enabled');
    $this->assertFieldByName('checkbox_disabled');
    // Test that we have legacy support.
    $this->assertFieldByName('checkbox_enabled', '1');
    $this->assertFieldByName('checkbox_disabled', '');
    // Test that the assertion fails correctly when using NULL to ignore state.
    try {
        $this->assertNoFieldByName('checkbox_enabled', NULL);
        $this->fail('The "checkbox_enabled" field was not found by name, using NULL value.');
    } catch (ExpectationException $e) {
        // Expected exception; just continue testing.
    }
    // Part 2 - Test by ID.
    // Test that checkboxes are found/not found correctly by ID, when using
    // TRUE or FALSE to match their 'checked' state.
    $this->assertFieldById('edit-checkbox-enabled', TRUE);
    $this->assertFieldById('edit-checkbox-disabled', FALSE);
    $this->assertNoFieldById('edit-checkbox-enabled', FALSE);
    $this->assertNoFieldById('edit-checkbox-disabled', TRUE);
    // Test that checkboxes are found by ID, when using NULL to ignore the
    // 'checked' state.
    $this->assertFieldById('edit-checkbox-enabled', NULL);
    $this->assertFieldById('edit-checkbox-disabled', NULL);
    // Test that checkboxes are found by ID when passing no second parameter.
    $this->assertFieldById('edit-checkbox-enabled');
    $this->assertFieldById('edit-checkbox-disabled');
    // Test that we have legacy support.
    $this->assertFieldById('edit-checkbox-enabled', '1');
    $this->assertFieldById('edit-checkbox-disabled', '');
    // Test that the assertion fails correctly when using NULL to ignore state.
    try {
        $this->assertNoFieldById('edit-checkbox-disabled', NULL);
        $this->fail('The "edit-checkbox-disabled" field was not found by ID, using NULL value.');
    } catch (ExpectationException $e) {
        // Expected exception; just continue testing.
    }
    // Part 3 - Test the specific 'checked' assertions.
    $this->assertFieldChecked('edit-checkbox-enabled');
    $this->assertNoFieldChecked('edit-checkbox-disabled');
    // Test that the assertion fails correctly with non-existent field id.
    try {
        $this->assertNoFieldChecked('incorrect_checkbox_id');
        $this->fail('The "incorrect_checkbox_id" field was found');
    } catch (ExpectationException $e) {
        // Expected exception; just continue testing.
    }
    // Test that the assertion fails correctly for a checkbox that is checked.
    try {
        $this->assertNoFieldChecked('edit-checkbox-enabled');
        $this->fail('The "edit-checkbox-enabled" field was not found in a checked state.');
    } catch (ExpectationException $e) {
        // Expected exception; just continue testing.
    }
    // Test that the assertion fails correctly for a checkbox that is not
    // checked.
    try {
        $this->assertFieldChecked('edit-checkbox-disabled');
        $this->fail('The "edit-checkbox-disabled" field was found and checked.');
    } catch (ExpectationException $e) {
        // Expected exception; just continue testing.
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.