You are here

public function BrowserTestBaseTest::testFieldAssertsForTextfields in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php \Drupal\FunctionalTests\BrowserTestBaseTest::testFieldAssertsForTextfields()

Tests legacy field asserts using textfields.

File

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

Class

BrowserTestBaseTest
Tests BrowserTestBase functionality.

Namespace

Drupal\FunctionalTests

Code

public function testFieldAssertsForTextfields() {
  $this
    ->drupalGet('test-field-xpath');

  // *** 1. assertNoField().
  $this
    ->assertNoField('invalid_name_and_id');

  // Test that the assertion fails correctly when searching by name.
  try {
    $this
      ->assertNoField('name');
    $this
      ->fail('The "name" field was not found based on name.');
  } catch (ExpectationException $e) {

    // Expected exception; just continue testing.
  }

  // Test that the assertion fails correctly when searching by id.
  try {
    $this
      ->assertNoField('edit-name');
    $this
      ->fail('The "name" field was not found based on id.');
  } catch (ExpectationException $e) {

    // Expected exception; just continue testing.
  }

  // *** 2. assertField().
  $this
    ->assertField('name');
  $this
    ->assertField('edit-name');

  // Test that the assertion fails correctly if the field does not exist.
  try {
    $this
      ->assertField('invalid_name_and_id');
    $this
      ->fail('The "invalid_name_and_id" field was found.');
  } catch (ExpectationFailedException $e) {

    // Expected exception; just continue testing.
  }

  // *** 3. assertNoFieldById().
  $this
    ->assertNoFieldById('name');
  $this
    ->assertNoFieldById('name', 'not the value');
  $this
    ->assertNoFieldById('notexisting');
  $this
    ->assertNoFieldById('notexisting', NULL);

  // Test that the assertion fails correctly if no value is passed in.
  try {
    $this
      ->assertNoFieldById('edit-description');
    $this
      ->fail('The "description" field, with no value was not found.');
  } catch (ExpectationException $e) {

    // Expected exception; just continue testing.
  }

  // Test that the assertion fails correctly if a NULL value is passed in.
  try {
    $this
      ->assertNoFieldById('edit-name', NULL);
    $this
      ->fail('The "name" field was not found.');
  } catch (ExpectationException $e) {

    // Expected exception; just continue testing.
  }

  // *** 4. assertFieldById().
  $this
    ->assertFieldById('edit-name', NULL);
  $this
    ->assertFieldById('edit-name', 'Test name');
  $this
    ->assertFieldById('edit-description', NULL);
  $this
    ->assertFieldById('edit-description');

  // Test that the assertion fails correctly if no value is passed in.
  try {
    $this
      ->assertFieldById('edit-name');
    $this
      ->fail('The "edit-name" field with no value was found.');
  } catch (ExpectationFailedException $e) {

    // Expected exception; just continue testing.
  }

  // Test that the assertion fails correctly if the wrong value is passed in.
  try {
    $this
      ->assertFieldById('edit-name', 'not the value');
    $this
      ->fail('The "name" field was found, using the wrong value.');
  } catch (ExpectationFailedException $e) {

    // Expected exception; just continue testing
  }

  // *** 5. assertNoFieldByName().
  $this
    ->assertNoFieldByName('name');
  $this
    ->assertNoFieldByName('name', 'not the value');
  $this
    ->assertNoFieldByName('notexisting');
  $this
    ->assertNoFieldByName('notexisting', NULL);

  // Test that the assertion fails correctly if no value is passed in.
  try {
    $this
      ->assertNoFieldByName('description');
    $this
      ->fail('The "description" field, with no value was not found.');
  } catch (ExpectationException $e) {

    // Expected exception; just continue testing.
  }

  // Test that the assertion fails correctly if a NULL value is passed in.
  try {
    $this
      ->assertNoFieldByName('name', NULL);
    $this
      ->fail('The "name" field was not found.');
  } catch (ExpectationException $e) {

    // Expected exception; just continue testing.
  }

  // *** 6. assertFieldByName().
  $this
    ->assertFieldByName('name');
  $this
    ->assertFieldByName('name', NULL);
  $this
    ->assertFieldByName('name', 'Test name');
  $this
    ->assertFieldByName('description');
  $this
    ->assertFieldByName('description', '');
  $this
    ->assertFieldByName('description', NULL);

  // Test that the assertion fails correctly if given the wrong name.
  try {
    $this
      ->assertFieldByName('non-existing-name');
    $this
      ->fail('The "non-existing-name" field was found.');
  } catch (ExpectationFailedException $e) {

    // Expected exception; just continue testing.
  }

  // Test that the assertion fails correctly if given the wrong value.
  try {
    $this
      ->assertFieldByName('name', 'not the value');
    $this
      ->fail('The "name" field with incorrect value was found.');
  } catch (ExpectationFailedException $e) {

    // Expected exception; just continue testing.
  }

  // Test that text areas can contain new lines.
  $this
    ->assertFieldsByValue($this
    ->xpath("//textarea[@id = 'edit-test-textarea-with-newline']"), "Test text with\nnewline");
}