function BrowserTestBaseTest::testFieldAssertsForCheckbox

Same name and namespace in other branches
  1. 9 core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php \Drupal\FunctionalTests\BrowserTestBaseTest::testFieldAssertsForCheckbox()
  2. 8.9.x 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 374

Class

BrowserTestBaseTest
Tests BrowserTestBase functionality.

Namespace

Drupal\FunctionalTests

Code

public function testFieldAssertsForCheckbox() : void {
  $this->drupalGet('test-field-xpath');
  // Part 1 - Test by name.
  // Test that checkboxes are found/not found correctly by name, when using
  // '1' or '' to match their 'checked' state.
  $this->assertSession()
    ->fieldExists('checkbox_enabled');
  $this->assertSession()
    ->fieldExists('checkbox_disabled');
  $this->assertSession()
    ->fieldValueEquals('checkbox_enabled', '1');
  $this->assertSession()
    ->fieldValueEquals('checkbox_disabled', '');
  $this->assertSession()
    ->fieldValueNotEquals('checkbox_enabled', '');
  $this->assertSession()
    ->fieldValueNotEquals('checkbox_disabled', '1');
  // Test that we have legacy support.
  $this->assertSession()
    ->fieldValueEquals('checkbox_enabled', '1');
  $this->assertSession()
    ->fieldValueEquals('checkbox_disabled', '');
  // Test that the assertion fails correctly if given the right value.
  try {
    $this->assertSession()
      ->fieldValueNotEquals('checkbox_enabled', '1');
    $this->fail('fieldValueNotEquals failed to throw an exception.');
  } 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
  // '1' or '' to match their 'checked' state.
  $this->assertSession()
    ->fieldValueEquals('edit-checkbox-enabled', '1');
  $this->assertSession()
    ->fieldValueEquals('edit-checkbox-disabled', '');
  $this->assertSession()
    ->fieldValueNotEquals('edit-checkbox-enabled', '');
  $this->assertSession()
    ->fieldValueNotEquals('edit-checkbox-disabled', '1');
  // Test that checkboxes are found by ID, when using NULL to ignore the
  // 'checked' state.
  $this->assertSession()
    ->fieldExists('edit-checkbox-enabled');
  $this->assertSession()
    ->fieldExists('edit-checkbox-disabled');
  // Test that checkboxes are found by ID when passing no second parameter.
  $this->assertSession()
    ->fieldExists('edit-checkbox-enabled');
  $this->assertSession()
    ->fieldExists('edit-checkbox-disabled');
  // Test that we have legacy support.
  $this->assertSession()
    ->fieldValueEquals('edit-checkbox-enabled', '1');
  $this->assertSession()
    ->fieldValueEquals('edit-checkbox-disabled', '');
  // Test that the assertion fails correctly when using NULL to ignore state.
  try {
    $this->assertSession()
      ->fieldNotExists('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->assertSession()
    ->checkboxChecked('edit-checkbox-enabled');
  $this->assertSession()
    ->checkboxNotChecked('edit-checkbox-disabled');
  // Test that the assertion fails correctly with non-existent field id.
  try {
    $this->assertSession()
      ->checkboxNotChecked('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->assertSession()
      ->checkboxNotChecked('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->assertSession()
      ->checkboxChecked('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.