You are here

public function IpAddressFieldTest::testIpAddressField in IP address fields 8

Same name and namespace in other branches
  1. 2.0.x src/Tests/IpAddressFieldTest.php \Drupal\field_ipaddress\Tests\IpAddressFieldTest::testIpAddressField()

Tests date field functionality.

File

src/Tests/IpAddressFieldTest.php, line 77

Class

IpAddressFieldTest
Tests IP address field functionality.

Namespace

Drupal\field_ipaddress\Tests

Code

public function testIpAddressField() {

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName("{$this->field_name}[0][value]", '', "IP address element found ({$this->field_name}[0][value])");

  // First put in buggy data.
  $edit = [
    "{$this->field_name}[0][value]" => 'A255.255.255.255 - 255.255.255.255',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('Please provide a valid', 'Buggy input has been caught.');

  // Second, put in too big span.
  $edit = [
    "{$this->field_name}[0][value]" => '1.1.1.1 - 255.255.255.255',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('Please make sure', 'To big IP range has been caught.');

  // Put in some data.
  $edit = [
    "{$this->field_name}[0][value]" => '255.255.255.255 - 255.255.255.255',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
  $this
    ->assert(isset($match[1]), "URL matched after entity form submission ({$this->url})");
  $id = $match[1];
  $this
    ->assertText(t('entity_test @id has been created.', [
    '@id' => $id,
  ]));
  $this
    ->assertText($value, 'Data is present on page after form submission.');

  // Make sure we can query the data.
  $greater_than_value = '255.255.255.254';
  $query = \Drupal::entityQuery('entity_test')
    ->condition($this->field_name . '.ip_from', $greater_than_value, '>');
  $nids = $query
    ->execute();
  $this
    ->assert(count($nids) == 1, 'Entity query matches number of created entities.');
}