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