Chris@17: adminUser = $this->drupalCreateUser(['administer filters']); Chris@17: } Chris@17: Chris@17: // Test fields. Chris@17: Chris@17: /** Chris@17: * Test text field validation. Chris@17: */ Chris@17: public function testTextFieldValidation() { Chris@17: // Create a field with settings to validate. Chris@17: $max_length = 3; Chris@17: $field_name = mb_strtolower($this->randomMachineName()); Chris@17: $field_storage = FieldStorageConfig::create([ Chris@17: 'field_name' => $field_name, Chris@17: 'entity_type' => 'entity_test', Chris@17: 'type' => 'text', Chris@17: 'settings' => [ Chris@17: 'max_length' => $max_length, Chris@17: ], Chris@17: ]); Chris@17: $field_storage->save(); Chris@17: FieldConfig::create([ Chris@17: 'field_storage' => $field_storage, Chris@17: 'bundle' => 'entity_test', Chris@17: ])->save(); Chris@17: Chris@17: // Test validation with valid and invalid values. Chris@17: $entity = EntityTest::create(); Chris@17: for ($i = 0; $i <= $max_length + 2; $i++) { Chris@17: $entity->{$field_name}->value = str_repeat('x', $i); Chris@17: $violations = $entity->{$field_name}->validate(); Chris@17: if ($i <= $max_length) { Chris@17: $this->assertEqual(count($violations), 0, "Length $i does not cause validation error when max_length is $max_length"); Chris@17: } Chris@17: else { Chris@17: $this->assertEqual(count($violations), 1, "Length $i causes validation error when max_length is $max_length"); Chris@17: } Chris@17: } Chris@17: } Chris@17: Chris@17: /** Chris@17: * Test required long text with file upload. Chris@17: */ Chris@17: public function testRequiredLongTextWithFileUpload() { Chris@17: // Create a text field. Chris@17: $text_field_name = 'text_long'; Chris@17: $field_storage = FieldStorageConfig::create([ Chris@17: 'field_name' => $text_field_name, Chris@17: 'entity_type' => 'entity_test', Chris@17: 'type' => 'text_with_summary', Chris@17: ]); Chris@17: $field_storage->save(); Chris@17: FieldConfig::create([ Chris@17: 'field_storage' => $field_storage, Chris@17: 'bundle' => 'entity_test', Chris@17: 'label' => $this->randomMachineName() . '_label', Chris@17: 'required' => TRUE, Chris@17: ])->save(); Chris@17: Chris@17: // Create a file field. Chris@17: $file_field_name = 'file_field'; Chris@17: $field_storage = FieldStorageConfig::create([ Chris@17: 'field_name' => $file_field_name, Chris@17: 'entity_type' => 'entity_test', Chris@17: 'type' => 'file', Chris@17: ]); Chris@17: $field_storage->save(); Chris@17: FieldConfig::create([ Chris@17: 'field_storage' => $field_storage, Chris@17: 'bundle' => 'entity_test', Chris@17: 'label' => $this->randomMachineName() . '_label', Chris@17: ])->save(); Chris@17: Chris@17: entity_get_form_display('entity_test', 'entity_test', 'default') Chris@17: ->setComponent($text_field_name, [ Chris@17: 'type' => 'text_textarea_with_summary', Chris@17: ]) Chris@17: ->setComponent($file_field_name, [ Chris@17: 'type' => 'file_generic', Chris@17: ]) Chris@17: ->save(); Chris@17: entity_get_display('entity_test', 'entity_test', 'full') Chris@17: ->setComponent($text_field_name) Chris@17: ->setComponent($file_field_name) Chris@17: ->save(); Chris@17: Chris@17: $test_file = current($this->drupalGetTestFiles('text')); Chris@17: $edit['files[file_field_0]'] = \Drupal::service('file_system')->realpath($test_file->uri); Chris@17: $this->drupalPostForm('entity_test/add', $edit, 'Upload'); Chris@17: $this->assertResponse(200); Chris@17: $edit = [ Chris@17: 'text_long[0][value]' => 'Long text', Chris@17: ]; Chris@17: $this->drupalPostForm(NULL, $edit, 'Save'); Chris@17: $this->assertResponse(200); Chris@17: $this->drupalGet('entity_test/1'); Chris@17: $this->assertText('Long text'); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Test widgets. Chris@17: */ Chris@17: public function testTextfieldWidgets() { Chris@17: $this->_testTextfieldWidgets('text', 'text_textfield'); Chris@17: $this->_testTextfieldWidgets('text_long', 'text_textarea'); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Test widgets + 'formatted_text' setting. Chris@17: */ Chris@17: public function testTextfieldWidgetsFormatted() { Chris@17: $this->_testTextfieldWidgetsFormatted('text', 'text_textfield'); Chris@17: $this->_testTextfieldWidgetsFormatted('text_long', 'text_textarea'); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Helper function for testTextfieldWidgetsFormatted(). Chris@17: */ Chris@17: public function _testTextfieldWidgetsFormatted($field_type, $widget_type) { Chris@17: /** @var \Drupal\Core\Render\RendererInterface $renderer */ Chris@17: $renderer = $this->container->get('renderer'); Chris@17: Chris@17: // Create a field. Chris@17: $field_name = mb_strtolower($this->randomMachineName()); Chris@17: $field_storage = FieldStorageConfig::create([ Chris@17: 'field_name' => $field_name, Chris@17: 'entity_type' => 'entity_test', Chris@17: 'type' => $field_type, Chris@17: ]); Chris@17: $field_storage->save(); Chris@17: FieldConfig::create([ Chris@17: 'field_storage' => $field_storage, Chris@17: 'bundle' => 'entity_test', Chris@17: 'label' => $this->randomMachineName() . '_label', Chris@17: ])->save(); Chris@17: entity_get_form_display('entity_test', 'entity_test', 'default') Chris@17: ->setComponent($field_name, [ Chris@17: 'type' => $widget_type, Chris@17: ]) Chris@17: ->save(); Chris@17: entity_get_display('entity_test', 'entity_test', 'full') Chris@17: ->setComponent($field_name) Chris@17: ->save(); Chris@17: Chris@17: // Disable all text formats besides the plain text fallback format. Chris@17: $this->drupalLogin($this->adminUser); Chris@17: foreach (filter_formats() as $format) { Chris@17: if (!$format->isFallbackFormat()) { Chris@17: $this->drupalPostForm('admin/config/content/formats/manage/' . $format->id() . '/disable', [], t('Disable')); Chris@17: } Chris@17: } Chris@17: $this->drupalLogin($this->webUser); Chris@17: Chris@17: // Display the creation form. Since the user only has access to one format, Chris@17: // no format selector will be displayed. Chris@17: $this->drupalGet('entity_test/add'); Chris@17: $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed'); Chris@17: $this->assertNoFieldByName("{$field_name}[0][format]", '', 'Format selector is not displayed'); Chris@17: Chris@17: // Submit with data that should be filtered. Chris@17: $value = '' . $this->randomMachineName() . ''; Chris@17: $edit = [ Chris@17: "{$field_name}[0][value]" => $value, Chris@17: ]; Chris@17: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@17: preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match); Chris@17: $id = $match[1]; Chris@17: $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created'); Chris@17: Chris@17: // Display the entity. Chris@17: $entity = EntityTest::load($id); Chris@17: $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full'); Chris@17: $content = $display->build($entity); Chris@17: $rendered_entity = \Drupal::service('renderer')->renderRoot($content); Chris@17: $this->assertNotContains($value, (string) $rendered_entity); Chris@17: $this->assertContains(Html::escape($value), (string) $rendered_entity); Chris@17: Chris@17: // Create a new text format that does not escape HTML, and grant the user Chris@17: // access to it. Chris@17: $this->drupalLogin($this->adminUser); Chris@17: $edit = [ Chris@17: 'format' => mb_strtolower($this->randomMachineName()), Chris@17: 'name' => $this->randomMachineName(), Chris@17: ]; Chris@17: $this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration')); Chris@17: filter_formats_reset(); Chris@17: $format = FilterFormat::load($edit['format']); Chris@17: $format_id = $format->id(); Chris@17: $permission = $format->getPermissionName(); Chris@17: $roles = $this->webUser->getRoles(); Chris@17: $rid = $roles[0]; Chris@17: user_role_grant_permissions($rid, [$permission]); Chris@17: $this->drupalLogin($this->webUser); Chris@17: Chris@17: // Display edition form. Chris@17: // We should now have a 'text format' selector. Chris@17: $this->drupalGet('entity_test/manage/' . $id . '/edit'); Chris@17: $this->assertFieldByName("{$field_name}[0][value]", NULL, 'Widget is displayed'); Chris@17: $this->assertFieldByName("{$field_name}[0][format]", NULL, 'Format selector is displayed'); Chris@17: Chris@17: // Edit and change the text format to the new one that was created. Chris@17: $edit = [ Chris@17: "{$field_name}[0][format]" => $format_id, Chris@17: ]; Chris@17: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@17: $this->assertText(t('entity_test @id has been updated.', ['@id' => $id]), 'Entity was updated'); Chris@17: Chris@17: // Display the entity. Chris@17: $this->container->get('entity.manager')->getStorage('entity_test')->resetCache([$id]); Chris@17: $entity = EntityTest::load($id); Chris@17: $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full'); Chris@17: $content = $display->build($entity); Chris@17: $rendered_entity = \Drupal::service('renderer')->renderRoot($content); Chris@17: $this->assertContains($value, (string) $rendered_entity); Chris@17: } Chris@17: Chris@17: }