View source
<?php
namespace Drupal\field_ipaddress\Tests;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\simpletest\WebTestBase;
class IpAddressFieldTest extends WebTestBase {
public static $modules = [
'node',
'entity_test',
'field_ipaddress',
'field',
'field_ui',
];
protected $fieldName = 'field_testip';
protected function setUp() {
parent::setUp();
$web_user = $this
->drupalCreateUser([
'access content',
'view test entity',
'administer entity_test content',
'administer entity_test form display',
'administer content types',
'administer node fields',
]);
$this
->drupalLogin($web_user);
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $this->field_name,
'entity_type' => 'entity_test',
'type' => 'ipaddress',
'settings' => [],
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
'required' => TRUE,
]);
$this->field
->save();
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->field_name, [
'type' => 'ipaddress_default',
])
->save();
}
public function testIpAddressField() {
$this
->drupalGet('entity_test/add');
$this
->assertFieldByName("{$this->field_name}[0][value]", '', "IP address element found ({$this->field_name}[0][value])");
$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.');
$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.');
$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.');
$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.');
}
}