Chris@17
|
1 <?php
|
Chris@17
|
2
|
Chris@17
|
3 namespace Drupal\Tests\user\FunctionalJavascript;
|
Chris@17
|
4
|
Chris@17
|
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
Chris@17
|
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
Chris@17
|
7 use Drupal\field\Entity\FieldConfig;
|
Chris@17
|
8 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@17
|
9
|
Chris@17
|
10 /**
|
Chris@17
|
11 * Tests user registration forms with additional fields.
|
Chris@17
|
12 *
|
Chris@17
|
13 * @group user
|
Chris@17
|
14 */
|
Chris@17
|
15 class RegistrationWithUserFieldsTest extends WebDriverTestBase {
|
Chris@17
|
16
|
Chris@17
|
17 /**
|
Chris@17
|
18 * WebAssert object.
|
Chris@17
|
19 *
|
Chris@17
|
20 * @var \Drupal\Tests\WebAssert
|
Chris@17
|
21 */
|
Chris@17
|
22 protected $webAssert;
|
Chris@17
|
23
|
Chris@17
|
24 /**
|
Chris@17
|
25 * DocumentElement object.
|
Chris@17
|
26 *
|
Chris@17
|
27 * @var \Behat\Mink\Element\DocumentElement
|
Chris@17
|
28 */
|
Chris@17
|
29 protected $page;
|
Chris@17
|
30
|
Chris@17
|
31 /**
|
Chris@17
|
32 * {@inheritdoc}
|
Chris@17
|
33 */
|
Chris@17
|
34 public static $modules = ['field_test'];
|
Chris@17
|
35
|
Chris@17
|
36 /**
|
Chris@17
|
37 * {@inheritdoc}
|
Chris@17
|
38 */
|
Chris@17
|
39 protected function setUp() {
|
Chris@17
|
40 parent::setUp();
|
Chris@17
|
41 $this->page = $this->getSession()->getPage();
|
Chris@17
|
42 $this->webAssert = $this->assertSession();
|
Chris@17
|
43 }
|
Chris@17
|
44
|
Chris@17
|
45 /**
|
Chris@17
|
46 * Tests Field API fields on user registration forms.
|
Chris@17
|
47 */
|
Chris@17
|
48 public function testRegistrationWithUserFields() {
|
Chris@17
|
49 // Create a field on 'user' entity type.
|
Chris@17
|
50 $field_storage = FieldStorageConfig::create([
|
Chris@17
|
51 'field_name' => 'test_user_field',
|
Chris@17
|
52 'entity_type' => 'user',
|
Chris@17
|
53 'type' => 'test_field',
|
Chris@17
|
54 'cardinality' => 1,
|
Chris@17
|
55 ]);
|
Chris@17
|
56 $field_storage->save();
|
Chris@17
|
57 $field = FieldConfig::create([
|
Chris@17
|
58 'field_storage' => $field_storage,
|
Chris@17
|
59 'label' => 'Some user field',
|
Chris@17
|
60 'bundle' => 'user',
|
Chris@17
|
61 'required' => TRUE,
|
Chris@17
|
62 ]);
|
Chris@17
|
63 $field->save();
|
Chris@17
|
64
|
Chris@17
|
65 entity_get_form_display('user', 'user', 'default')
|
Chris@17
|
66 ->setComponent('test_user_field', ['type' => 'test_field_widget'])
|
Chris@17
|
67 ->save();
|
Chris@17
|
68 $user_registration_form = entity_get_form_display('user', 'user', 'register');
|
Chris@17
|
69 $user_registration_form->save();
|
Chris@17
|
70
|
Chris@17
|
71 // Check that the field does not appear on the registration form.
|
Chris@17
|
72 $this->drupalGet('user/register');
|
Chris@17
|
73 $this->webAssert->pageTextNotContains($field->label());
|
Chris@17
|
74
|
Chris@17
|
75 // Have the field appear on the registration form.
|
Chris@17
|
76 $user_registration_form->setComponent('test_user_field', ['type' => 'test_field_widget'])->save();
|
Chris@17
|
77
|
Chris@17
|
78 $this->drupalGet('user/register');
|
Chris@17
|
79 $this->webAssert->pageTextContains($field->label());
|
Chris@17
|
80
|
Chris@17
|
81 // In order to check the server side validation the native browser
|
Chris@17
|
82 // validation for required fields needs to be circumvented.
|
Chris@17
|
83 $session = $this->getSession();
|
Chris@17
|
84 $session->executeScript("jQuery('#edit-test-user-field-0-value').prop('required', false);");
|
Chris@17
|
85
|
Chris@17
|
86 // Check that validation errors are correctly reported.
|
Chris@17
|
87 $name = $this->randomMachineName();
|
Chris@17
|
88 $this->page->fillField('edit-name', $name);
|
Chris@17
|
89 $this->page->fillField('edit-mail', $name . '@example.com');
|
Chris@17
|
90
|
Chris@17
|
91 $this->page->pressButton('edit-submit');
|
Chris@17
|
92 $this->webAssert->pageTextContains(t('@name field is required.', ['@name' => $field->label()]));
|
Chris@17
|
93
|
Chris@17
|
94 // Invalid input.
|
Chris@17
|
95 $this->page->fillField('edit-test-user-field-0-value', '-1');
|
Chris@17
|
96 $this->page->pressButton('edit-submit');
|
Chris@17
|
97 $this->webAssert->pageTextContains($field->label() . ' does not accept the value -1.');
|
Chris@17
|
98
|
Chris@17
|
99 // Submit with valid data.
|
Chris@17
|
100 $value = (string) mt_rand(1, 255);
|
Chris@17
|
101 $this->page->fillField('edit-test-user-field-0-value', $value);
|
Chris@17
|
102 $this->page->pressButton('edit-submit');
|
Chris@17
|
103 // Check user fields.
|
Chris@17
|
104 $accounts = $this->container->get('entity_type.manager')->getStorage('user')
|
Chris@17
|
105 ->loadByProperties(['name' => $name, 'mail' => $name . '@example.com']);
|
Chris@17
|
106 $new_user = reset($accounts);
|
Chris@17
|
107 $this->assertEquals($value, $new_user->test_user_field->value, 'The field value was correctly saved.');
|
Chris@17
|
108
|
Chris@17
|
109 // Check that the 'add more' button works.
|
Chris@17
|
110 $field_storage->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
|
Chris@17
|
111 $field_storage->save();
|
Chris@17
|
112 $name = $this->randomMachineName();
|
Chris@17
|
113 $this->drupalGet('user/register');
|
Chris@17
|
114 $this->page->fillField('edit-name', $name);
|
Chris@17
|
115 $this->page->fillField('edit-mail', $name . '@example.com');
|
Chris@17
|
116 $this->page->fillField('test_user_field[0][value]', $value);
|
Chris@17
|
117 // Add two inputs.
|
Chris@17
|
118 $this->page->pressButton('test_user_field_add_more');
|
Chris@17
|
119 $this->webAssert->waitForElement('css', 'input[name="test_user_field[1][value]"]');
|
Chris@17
|
120 $this->page->fillField('test_user_field[1][value]', $value . '1');
|
Chris@17
|
121 $this->page->pressButton('test_user_field_add_more');
|
Chris@17
|
122 $this->webAssert->waitForElement('css', 'input[name="test_user_field[2][value]"]');
|
Chris@17
|
123 $this->page->fillField('test_user_field[2][value]', $value . '2');
|
Chris@17
|
124
|
Chris@17
|
125 // Submit with three values.
|
Chris@17
|
126 $this->page->pressButton('edit-submit');
|
Chris@17
|
127
|
Chris@17
|
128 // Check user fields.
|
Chris@17
|
129 $accounts = $this->container->get('entity_type.manager')
|
Chris@17
|
130 ->getStorage('user')
|
Chris@17
|
131 ->loadByProperties(['name' => $name, 'mail' => $name . '@example.com']);
|
Chris@17
|
132 $new_user = reset($accounts);
|
Chris@17
|
133 $this->assertEquals($value, $new_user->test_user_field[0]->value, t('The field value was correctly saved.'));
|
Chris@17
|
134 $this->assertEquals($value . '1', $new_user->test_user_field[1]->value, t('The field value was correctly saved.'));
|
Chris@17
|
135 $this->assertEquals($value . '2', $new_user->test_user_field[2]->value, t('The field value was correctly saved.'));
|
Chris@17
|
136 }
|
Chris@17
|
137
|
Chris@17
|
138 }
|