View source
<?php
namespace Drupal\Tests\user\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
class RegistrationWithUserFieldsTest extends WebDriverTestBase {
protected $webAssert;
protected $page;
protected static $modules = [
'field_test',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this->page = $this
->getSession()
->getPage();
$this->webAssert = $this
->assertSession();
}
public function testRegistrationWithUserFields() {
$field_storage = FieldStorageConfig::create([
'field_name' => 'test_user_field',
'entity_type' => 'user',
'type' => 'test_field',
'cardinality' => 1,
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'label' => 'Some user field',
'bundle' => 'user',
'required' => TRUE,
]);
$field
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('user', 'user', 'default')
->setComponent('test_user_field', [
'type' => 'test_field_widget',
])
->save();
$user_registration_form = \Drupal::service('entity_display.repository')
->getFormDisplay('user', 'user', 'register');
$user_registration_form
->save();
$this
->drupalGet('user/register');
$this->webAssert
->pageTextNotContains($field
->label());
$user_registration_form
->setComponent('test_user_field', [
'type' => 'test_field_widget',
])
->save();
$this
->drupalGet('user/register');
$this->webAssert
->pageTextContains($field
->label());
$session = $this
->getSession();
$session
->executeScript("jQuery('#edit-test-user-field-0-value').prop('required', false);");
$name = $this
->randomMachineName();
$this->page
->fillField('edit-name', $name);
$this->page
->fillField('edit-mail', $name . '@example.com');
$this->page
->pressButton('edit-submit');
$this->webAssert
->pageTextContains($field
->label() . ' field is required.');
$this->page
->fillField('edit-test-user-field-0-value', '-1');
$this->page
->pressButton('edit-submit');
$this->webAssert
->pageTextContains($field
->label() . ' does not accept the value -1.');
$value = (string) mt_rand(1, 255);
$this->page
->fillField('edit-test-user-field-0-value', $value);
$this->page
->pressButton('edit-submit');
$accounts = $this->container
->get('entity_type.manager')
->getStorage('user')
->loadByProperties([
'name' => $name,
'mail' => $name . '@example.com',
]);
$new_user = reset($accounts);
$this
->assertEquals($value, $new_user->test_user_field->value, 'The field value was correctly saved.');
$field_storage
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$field_storage
->save();
$name = $this
->randomMachineName();
$this
->drupalGet('user/register');
$this->page
->fillField('edit-name', $name);
$this->page
->fillField('edit-mail', $name . '@example.com');
$this->page
->fillField('test_user_field[0][value]', $value);
$this->page
->pressButton('test_user_field_add_more');
$this->webAssert
->waitForElement('css', 'input[name="test_user_field[1][value]"]');
$this->page
->fillField('test_user_field[1][value]', $value . '1');
$this->page
->pressButton('test_user_field_add_more');
$this->webAssert
->waitForElement('css', 'input[name="test_user_field[2][value]"]');
$this->page
->fillField('test_user_field[2][value]', $value . '2');
$this->page
->pressButton('edit-submit');
$accounts = $this->container
->get('entity_type.manager')
->getStorage('user')
->loadByProperties([
'name' => $name,
'mail' => $name . '@example.com',
]);
$new_user = reset($accounts);
$this
->assertEquals($value, $new_user->test_user_field[0]->value);
$this
->assertEquals($value . '1', $new_user->test_user_field[1]->value);
$this
->assertEquals($value . '2', $new_user->test_user_field[2]->value);
}
}