diff --git a/core/modules/user/tests/src/FunctionalJavascript/RegistrationWithUserFieldsTest.php b/core/modules/user/tests/src/FunctionalJavascript/RegistrationWithUserFieldsTest.php new file mode 100644 index 0000000000..64416659d8 --- /dev/null +++ b/core/modules/user/tests/src/FunctionalJavascript/RegistrationWithUserFieldsTest.php @@ -0,0 +1,148 @@ +page = $this->getSession()->getPage(); + $this->webAssert = $this->assertSession(); + } + + /** + * Tests Field API fields on user registration forms. + */ + public function testRegistrationWithUserFields() { + // Create a field on 'user' entity type. + $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(); + + entity_get_form_display('user', 'user', 'default') + ->setComponent('test_user_field', ['type' => 'test_field_widget']) + ->save(); + entity_get_form_display('user', 'user', 'register') + ->save(); + + // Check that the field does not appear on the registration form. + $this->drupalGet('user/register'); + $this->webAssert->pageTextNotContains($field->label()); + $this->webAssert->responseHeaderContains('X-Drupal-Cache-Tags', 'config:core.entity_form_display.user.user.register'); + $this->webAssert->responseHeaderContains('X-Drupal-Cache-Tags', 'config:user.settings'); + + // Have the field appear on the registration form. + entity_get_form_display('user', 'user', 'register') + ->setComponent('test_user_field', ['type' => 'test_field_widget']) + ->save(); + + $this->drupalGet('user/register'); + $this->webAssert->pageTextContains($field->label()); + $this->assertRegistrationFormCacheTagsWithUserFields(); + + // Check that validation errors are correctly reported. + $name = $this->randomMachineName(); + $this->page->fillField('edit-name', $name); + $this->page->fillField('edit-mail', $name . '@example.com'); + $this->page->pressButton('edit-submit'); + $this->assertRegistrationFormCacheTagsWithUserFields(); + $this->webAssert->pageTextContains(t('@name field is required.', ['@name' => $field->label()])); + + // Invalid input. + $this->page->fillField('edit-test-user-field-0-value', '-1'); + $this->page->pressButton('edit-submit'); + $this->assertRegistrationFormCacheTagsWithUserFields(); + $this->webAssert->pageTextContains($field->label() . ' does not accept the value -1.'); + + // Submit with valid data. + $value = (string) mt_rand(1, 255); + $this->page->fillField('edit-test-user-field-0-value', $value); + $this->page->pressButton('edit-submit'); + // Check user fields. + $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.'); + + // Check that the 'add more' button works. + $field_storage->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); + $field_storage->save(); + $name = $this->randomMachineName(); + $this->drupalGet('user/register'); + $this->assertRegistrationFormCacheTagsWithUserFields(); + $this->page->fillField('edit-name', $name); + $this->page->fillField('edit-mail', $name . '@example.com'); + $this->page->fillField('test_user_field[0][value]', $value); + // Add two inputs. + $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'); + + // Submit with three values. + $this->page->pressButton('edit-submit'); + + // Check user fields. + $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, t('The field value was correctly saved.')); + $this->assertEquals($value . '1', $new_user->test_user_field[1]->value, t('The field value was correctly saved.')); + $this->assertEquals($value . '2', $new_user->test_user_field[2]->value, t('The field value was correctly saved.')); + } + + /** + * Asserts the presence of cache tags on registration form with user fields. + */ + protected function assertRegistrationFormCacheTagsWithUserFields() { + $this->webAssert->responseHeaderContains('X-Drupal-Cache-Tags', 'config:core.entity_form_display.user.user.register'); + $this->webAssert->responseHeaderContains('X-Drupal-Cache-Tags', 'config:field.field.user.user.test_user_field'); + $this->webAssert->responseHeaderContains('X-Drupal-Cache-Tags', 'config:field.storage.user.test_user_field'); + $this->webAssert->responseHeaderContains('X-Drupal-Cache-Tags', 'config:user.settings'); + } + +} diff --git a/core/modules/user/tests/src/Kernel/WhosOnlineBlockTest.php b/core/modules/user/tests/src/Kernel/WhosOnlineBlockTest.php new file mode 100644 index 0000000000..248e670d17 --- /dev/null +++ b/core/modules/user/tests/src/Kernel/WhosOnlineBlockTest.php @@ -0,0 +1,89 @@ +installConfig(['system', 'block', 'views', 'user']); + $this->installSchema('system', ['sequences']); + $this->installEntitySchema('user'); + + } + + /** + * Test the Who's Online block. + */ + public function testWhosOnlineBlock() { + + // Generate users. + $user1 = User::create([ + 'name' => 'user1', + 'mail' => 'user1@example.com', + ]); + $user1->addRole('administrator'); + $user1->activate(); + $user1->setLastAccessTime(\Drupal::time()->getRequestTime()); + $user1->save(); + + $user2 = User::create([ + 'name' => 'user2', + 'mail' => 'user2@example.com', + ]); + $user2->activate(); + $user2->setLastAccessTime(\Drupal::time()->getRequestTime() + 1); + $user2->save(); + + $user3 = User::create([ + 'name' => 'user3', + 'mail' => 'user2@example.com', + ]); + $user3->activate(); + // Insert an inactive user who should not be seen in the block. + $inactive_time = \Drupal::time()->getRequestTime() - (15 * 60) - 1; + $user3->setLastAccessTime($inactive_time); + $user3->save(); + + // Test block output. + \Drupal::currentUser()->setAccount($user1); + // Create a block with only required values. + $block = Block::create([ + 'plugin' => 'views_block:who_s_online-who_s_online_block', + 'region' => 'sidebar_first', + 'id' => 'views_block__who_s_online_who_s_online_block', + 'theme' => \Drupal::configFactory()->get('system.theme')->get('default'), + 'label' => "Who's online", + 'visibility' => [], + 'weight' => 0, + ]); + $block->save(); + $this->container->get('cache.render')->deleteAll(); + $render_controller = \Drupal::entityTypeManager()->getViewBuilder($block->getEntityTypeId()); + $content = $render_controller->view($block, 'block'); + $this->setRawContent($this->render($content)); + $this->assertRaw('2 users', 'Correct number of online users (2 users).'); + $this->assertText($user1->getUsername(), 'Active user 1 found in online list.'); + $this->assertText($user2->getUsername(), 'Active user 2 found in online list.'); + $this->assertNoText($user3->getUsername(), 'Inactive user not found in online list.'); + $this->assertTrue(strpos($this->getRawContent(), $user1->getUsername()) > strpos($this->getRawContent(), $user2->getUsername()), 'Online users are ordered correctly.'); + } + +}