View source
<?php
namespace Drupal\Tests\serial\Functional;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Tests\BrowserTestBase;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
class SerialFieldTest extends BrowserTestBase {
public static $modules = [
'field',
'field_ui',
'node',
'entity_test',
'serial',
];
protected $webUser;
protected $displayOptions;
protected $fieldStorage;
protected $serialId;
protected $field;
protected function setUp() {
parent::setUp();
$this->webUser = $this
->drupalCreateUser([
'access content',
'view test entity',
'administer entity_test content',
'administer entity_test form display',
'administer content types',
'administer node fields',
]);
$this
->drupalLogin($this->webUser);
$field_name = 'field_serial';
$type = 'serial';
$widget_type = 'serial_default_widget';
$formatter_type = 'serial_default_formatter';
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => $type,
'settings' => [
'start_value' => 1,
'init_existing_entities' => FALSE,
],
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'label' => 'Serial',
'bundle' => 'entity_test',
'required' => TRUE,
]);
$this->field
->save();
EntityFormDisplay::load('entity_test.entity_test.default')
->setComponent($field_name, [
'type' => $widget_type,
])
->save();
$this->displayOptions = [
'type' => $formatter_type,
'label' => 'hidden',
];
EntityViewDisplay::create([
'targetEntityType' => $this->field
->getTargetEntityTypeId(),
'bundle' => $this->field
->getTargetBundle(),
'mode' => 'full',
'status' => TRUE,
])
->setComponent($field_name, $this->displayOptions)
->save();
}
public function testSerialField() {
$this
->drupalGet('entity_test/add');
$fields = $this
->xpath('//div[contains(@class, "field--widget-serial-default-widget") and @id="edit-field-serial-wrapper"]');
$this
->assertEquals(1, count($fields));
$this
->assertSession()
->fieldNotExists('field_serial[0][value]');
$edit = [];
$this
->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\\d+)|', $this
->getSession()
->getCurrentUrl(), $match);
$id = $match[1];
$this
->assertSession()
->pageTextContains(sprintf('entity_test %s has been created.', $id));
$this->serialId = 1;
$this
->drupalGet('entity_test/' . $id);
$serial = $this
->xpath('//div[contains(@class, "field__item") and text()="' . $this->serialId . '"]');
$this
->assertEquals(1, count($serial));
}
public function testSerialEntityCreation($entities = 15) {
$i = 0;
while ($i < $entities) {
$this
->drupalGet('entity_test/add');
$edit = [];
$this
->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\\d+)|', $this
->getSession()
->getCurrentUrl(), $match);
$id = $match[1];
$this
->assertSession()
->pageTextContains(sprintf('entity_test %s has been created.', $id));
$this->serialId++;
$this
->drupalGet('entity_test/' . $id);
$serial = $this
->xpath('//div[contains(@class, "field__item") and text()="' . $this->serialId . '"]');
$this
->assertEquals(1, count($serial));
$i++;
}
}
}