You are here

public function BrowserTestBaseTest::testFieldAssertsForCheckbox in Drupal 9

Same name and namespace in other branches
  1. 8 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 656

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
    ->assertSession()
    ->fieldExists('checkbox_enabled');
  $this
    ->assertSession()
    ->fieldExists('checkbox_disabled');
  $this
    ->assertSession()
    ->fieldValueEquals('checkbox_enabled', TRUE);
  $this
    ->assertSession()
    ->fieldValueEquals('checkbox_disabled', FALSE);
  $this
    ->assertSession()
    ->fieldValueNotEquals('checkbox_enabled', FALSE);
  $this
    ->assertSession()
    ->fieldValueNotEquals('checkbox_disabled', TRUE);

  // 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', TRUE);
    $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
  // TRUE or FALSE to match their 'checked' state.
  $this
    ->assertSession()
    ->fieldValueEquals('edit-checkbox-enabled', TRUE);
  $this
    ->assertSession()
    ->fieldValueEquals('edit-checkbox-disabled', FALSE);
  $this
    ->assertSession()
    ->fieldValueNotEquals('edit-checkbox-enabled', FALSE);
  $this
    ->assertSession()
    ->fieldValueNotEquals('edit-checkbox-disabled', TRUE);

  // 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.
  }
}