diff --git a/core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php b/core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php index 6c9c178..cae4398 100644 --- a/core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php +++ b/core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php @@ -3,7 +3,7 @@ namespace Drupal\Core\Form\EventSubscriber; use Drupal\Core\Ajax\AjaxResponse; -use Drupal\Core\Ajax\PrependCommand; +use Drupal\Core\Ajax\ReplaceCommand; use Drupal\Core\EventSubscriber\MainContentViewSubscriber; use Drupal\Core\Form\Exception\BrokenPostRequestException; use Drupal\Core\Form\FormAjaxException; @@ -78,7 +78,7 @@ public function onException(GetResponseForExceptionEvent $event) { $this->drupalSetMessage($this->t('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (@size) that this server supports.', ['@size' => $this->formatSize($exception->getSize())]), 'error'); $response = new AjaxResponse(); $status_messages = ['#type' => 'status_messages']; - $response->addCommand(new PrependCommand(NULL, $status_messages)); + $response->addCommand(new ReplaceCommand(NULL, $status_messages)); $response->headers->set('X-Status-Code', 200); $event->setResponse($response); return; diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index bb203bf..b9d84e4 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -661,11 +661,17 @@ function template_preprocess_comment(&$variables) { else { $uri = $comment->permalink(); $attributes = $uri->getOption('attributes') ?: []; - $attributes += ['class' => ['permalink'], 'rel' => 'bookmark']; + $attributes += ['class' => ['permalink']]; + if (CommentType::load($comment->bundle())->getNofollow()) { + $attributes += ['rel' => 'bookmark nofollow']; + } + else { + $attributes += ['rel' => 'bookmark']; + } $uri->setOption('attributes', $attributes); $variables['title'] = \Drupal::l($comment->getSubject(), $uri); - $variables['permalink'] = \Drupal::l(t('Permalink'), $comment->permalink()); + $variables['permalink'] = \Drupal::l(t('Permalink'), $uri); } $variables['submitted'] = t('Submitted by @username on @datetime', ['@username' => $variables['author'], '@datetime' => $variables['created']]); @@ -690,7 +696,13 @@ function template_preprocess_comment(&$variables) { } $permalink_uri_parent = $comment_parent->permalink(); $attributes = $permalink_uri_parent->getOption('attributes') ?: []; - $attributes += ['class' => ['permalink'], 'rel' => 'bookmark']; + $attributes += ['class' => ['permalink']]; + if (CommentType::load($comment_parent->bundle())->getNofollow()) { + $attributes += ['rel' => 'bookmark nofollow']; + } + else { + $attributes += ['rel' => 'bookmark']; + } $permalink_uri_parent->setOption('attributes', $attributes); $variables['parent_title'] = \Drupal::l($comment_parent->getSubject(), $permalink_uri_parent); $variables['parent_permalink'] = \Drupal::l(t('Parent permalink'), $permalink_uri_parent); diff --git a/core/modules/comment/config/schema/comment.schema.yml b/core/modules/comment/config/schema/comment.schema.yml index e4a5b75..2ea37da 100644 --- a/core/modules/comment/config/schema/comment.schema.yml +++ b/core/modules/comment/config/schema/comment.schema.yml @@ -58,6 +58,9 @@ comment.type.*: description: type: text label: 'Description' + nofollow: + type: boolean + label: 'Rel nofollow' field.storage_settings.comment: type: mapping diff --git a/core/modules/comment/src/CommentTypeForm.php b/core/modules/comment/src/CommentTypeForm.php index 0ac5b44..64033b7 100644 --- a/core/modules/comment/src/CommentTypeForm.php +++ b/core/modules/comment/src/CommentTypeForm.php @@ -122,6 +122,13 @@ public function form(array $form, FormStateInterface $form_state) { ]; } + $form['nofollow'] = [ + '#type' => 'checkbox', + '#title' => t('Nofollow'), + '#default_value' => $comment_type->getNofollow(), + '#description' => t('Add nofollow to rel attribute of comment type.'), + ]; + if ($this->moduleHandler->moduleExists('content_translation')) { $form['language'] = [ '#type' => 'details', diff --git a/core/modules/comment/src/CommentTypeInterface.php b/core/modules/comment/src/CommentTypeInterface.php index acde96c..a82bab3 100644 --- a/core/modules/comment/src/CommentTypeInterface.php +++ b/core/modules/comment/src/CommentTypeInterface.php @@ -35,4 +35,12 @@ public function setDescription($description); */ public function getTargetEntityTypeId(); + /** + * Gets the setting of rel's nofollow for this comment type. + * + * @return bool + * TRUE if the comment type has the rel set to nofollow, FALSE otherwise. + */ + public function getNofollow(); + } diff --git a/core/modules/comment/src/Entity/CommentType.php b/core/modules/comment/src/Entity/CommentType.php index a773c58..62009db 100644 --- a/core/modules/comment/src/Entity/CommentType.php +++ b/core/modules/comment/src/Entity/CommentType.php @@ -44,6 +44,7 @@ * "label", * "target_entity_type_id", * "description", + * "nofollow", * } * ) */ @@ -78,6 +79,13 @@ class CommentType extends ConfigEntityBundleBase implements CommentTypeInterface protected $target_entity_type_id; /** + * Whether the comment type has rel set to nofollow. + * + * @var bool + */ + protected $nofollow = TRUE; + + /** * {@inheritdoc} */ public function getDescription() { @@ -99,4 +107,11 @@ public function getTargetEntityTypeId() { return $this->target_entity_type_id; } + /** + * {@inheritdoc} + */ + public function getNofollow() { + return $this->nofollow; + } + } diff --git a/core/modules/comment/src/Tests/CommentTestBase.php b/core/modules/comment/src/Tests/CommentTestBase.php index 3b923ff..7210065 100644 --- a/core/modules/comment/src/Tests/CommentTestBase.php +++ b/core/modules/comment/src/Tests/CommentTestBase.php @@ -401,6 +401,7 @@ protected function createCommentType($label) { 'label' => $label, 'description' => '', 'target_entity_type_id' => 'node', + 'nofollow' => TRUE, ]); $bundle->save(); return $bundle; diff --git a/core/modules/comment/src/Tests/CommentTestTrait.php b/core/modules/comment/src/Tests/CommentTestTrait.php index 038a3df..739aace 100644 --- a/core/modules/comment/src/Tests/CommentTestTrait.php +++ b/core/modules/comment/src/Tests/CommentTestTrait.php @@ -47,6 +47,7 @@ public function addDefaultCommentField($entity_type, $bundle, $field_name = 'com 'id' => $comment_type_id, 'label' => Unicode::ucfirst($comment_type_id), 'target_entity_type_id' => $entity_type, + 'nofollow' => TRUE, 'description' => 'Default comment field', ])->save(); } diff --git a/core/modules/comment/tests/src/Functional/CommentFieldsTest.php b/core/modules/comment/tests/src/Functional/CommentFieldsTest.php index 2df9973..dc5aa2f 100644 --- a/core/modules/comment/tests/src/Functional/CommentFieldsTest.php +++ b/core/modules/comment/tests/src/Functional/CommentFieldsTest.php @@ -168,6 +168,7 @@ public function testCommentFieldCreate() { 'label' => 'user_comment_type', 'description' => '', 'target_entity_type_id' => 'user', + 'nofollow' => TRUE, ]); $bundle->save(); diff --git a/core/modules/comment/tests/src/Functional/CommentInterfaceTest.php b/core/modules/comment/tests/src/Functional/CommentInterfaceTest.php index 7342633..e3293dd 100644 --- a/core/modules/comment/tests/src/Functional/CommentInterfaceTest.php +++ b/core/modules/comment/tests/src/Functional/CommentInterfaceTest.php @@ -80,10 +80,11 @@ public function testCommentInterface() { $this->assertText($comment_text, 'Individual comment body found.'); $arguments = [ ':link' => base_path() . 'comment/' . $comment->id() . '#comment-' . $comment->id(), + ':rel' => 'nofollow', ]; - $pattern_permalink = '//footer[contains(@class,"comment__meta")]/a[contains(@href,:link) and text()="Permalink"]'; + $pattern_permalink = '//footer[contains(@class,"comment__meta")]/a[contains(@href,:link) and contains(@rel,:rel) and text()="Permalink"]'; $permalink = $this->xpath($pattern_permalink, $arguments); - $this->assertTrue(!empty($permalink), 'Permalink link found.'); + $this->assertTrue(!empty($permalink), 'Permalink link found and rel is set to nofollow.'); // Set comments to have subject and preview to optional. $this->drupalLogout(); diff --git a/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php b/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php index 5889d69..19dc593 100644 --- a/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php +++ b/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php @@ -56,6 +56,7 @@ protected function setUp() { 'label' => 'Comment settings', 'description' => 'Comment settings', 'target_entity_type_id' => 'entity_test', + 'nofollow' => TRUE, ])->save(); // Create comment field on entity_test bundle. $this->addDefaultCommentField('entity_test', 'entity_test'); @@ -411,6 +412,7 @@ public function testCommentFunctionality() { 'label' => 'Foobar', 'description' => '', 'target_entity_type_id' => 'entity_test', + 'nofollow' => TRUE, ]); $bundle->save(); diff --git a/core/modules/comment/tests/src/Functional/CommentTestBase.php b/core/modules/comment/tests/src/Functional/CommentTestBase.php index 7cadcbf..e00126c 100644 --- a/core/modules/comment/tests/src/Functional/CommentTestBase.php +++ b/core/modules/comment/tests/src/Functional/CommentTestBase.php @@ -395,6 +395,7 @@ protected function createCommentType($label) { 'label' => $label, 'description' => '', 'target_entity_type_id' => 'node', + 'nofollow' => TRUE, ]); $bundle->save(); return $bundle; diff --git a/core/modules/comment/tests/src/Functional/CommentTitleTest.php b/core/modules/comment/tests/src/Functional/CommentTitleTest.php index 65c4937..b6b5967 100644 --- a/core/modules/comment/tests/src/Functional/CommentTitleTest.php +++ b/core/modules/comment/tests/src/Functional/CommentTitleTest.php @@ -77,6 +77,11 @@ public function testCommentPopulatedTitles() { // Tests that the comment's title link contains the url fragment. $this->assertTrue(strpos($comment_permalink, '#comment-' . $comment1->id()), "The comment's title link contains the url fragment."); $this->assertEqual($comment1->permalink()->toString(), $comment_permalink, "The comment's title has the correct link."); + + $comment_permalink_rel = $this->cssSelect('.permalink'); + $comment_permalink_rel = $comment_permalink_rel[0]->getAttribute('rel'); + // Tests that the comment's title link contains the nofollow rel. + $this->assertTrue(strpos($comment_permalink_rel, 'nofollow'), "The comment's title link contains the nofollow rel."); } } diff --git a/core/modules/comment/tests/src/Functional/CommentTypeNofollowTest.php b/core/modules/comment/tests/src/Functional/CommentTypeNofollowTest.php new file mode 100644 index 0000000..0f79376 --- /dev/null +++ b/core/modules/comment/tests/src/Functional/CommentTypeNofollowTest.php @@ -0,0 +1,151 @@ +adminUser = $this->drupalCreateUser($this->permissions); + + $this->commentType = $this->createCommentType('other'); + + } + + /** + * Tests editing a comment type defined nofollow. + */ + public function testCommentTypeEditNofollow() { + // Log in a test user. + $this->drupalLogin($this->adminUser); + + $this->drupalGet('admin/structure/comment/manage/' . $this->commentType->id()); + $this->assertResponse(200, 'The comment type can be accessed at the edit form.'); + + // Edit the nofollow setting via the user interface. + $edit = [ + 'nofollow' => FALSE, + ]; + $this->drupalPostForm('admin/structure/comment/manage/' . $this->commentType->id(), $edit, t('Save')); + \Drupal::entityManager()->getStorage('comment_type')->resetCache(['other']); + $comment_type = CommentType::load('other'); + $this->assertEqual($comment_type->getNofollow(), FALSE); + } + + public function testCommentViewCommentTypeNofollow() { + // Log in a test user. + $this->drupalLogin($this->adminUser); + + $this->drupalCreateContentType(['type' => 'test_node']); + $this->addDefaultCommentField('node', 'test_node', 'comment_other', CommentItemInterface::OPEN, 'other'); + + $this->drupalGet('admin/structure/comment/manage/' . $this->commentType->id()); + $this->assertResponse(200, 'The comment type can be accessed at the edit form.'); + + // Change the nofollow to FALSE. + $edit = [ + 'nofollow' => FALSE, + ]; + $this->drupalPostForm('admin/structure/comment/manage/' . $this->commentType->id(), $edit, t('Save')); + \Drupal::entityManager()->getStorage('comment_type')->resetCache(['other']); + $this->commentType = CommentType::load('other'); + + // Create a comment. + $comment = Comment::create([ + 'subject' => $this->randomMachineName(8), + 'comment_body' => $this->randomMachineName(8), + 'entity_id' => 'test_node', + 'entity_type' => 'node', + 'field_name' => 'comment_other', + 'status' => CommentInterface::PUBLISHED, + ]); + $comment->save(); + + // Check the nofollow on the comment page. + $this->drupalGet('comment/' . $comment->id()); + $arguments = [ + ':rel' => 'nofollow', + ]; + $pattern_permalink = '//footer[contains(@class,"comment__meta")]/a[contains(@rel,:rel) and text()="Permalink"]'; + $permalink = $this->xpath($pattern_permalink, $arguments); + $this->assertFalse(!empty($permalink), 'Permalink link found and rel is not set to nofollow.'); + + // Check the nofollow on the reply page. + $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment_other/' . $comment->id()); + $arguments = [ + ':rel' => 'nofollow', + ]; + $pattern_permalink = '//footer[contains(@class,"comment__meta")]/a[contains(@rel,:rel) and text()="Permalink"]'; + $permalink = $this->xpath($pattern_permalink, $arguments); + $this->assertFalse(!empty($permalink), 'Permalink link found and rel is not set to nofollow.'); + + $this->drupalGet('admin/structure/comment/manage/' . $this->commentType->id()); + $this->assertResponse(200, 'The comment type can be accessed at the edit form.'); + + // Change the nofollow back to its default value: TRUE. + $edit = [ + 'nofollow' => TRUE, + ]; + $this->drupalPostForm('admin/structure/comment/manage/' . $this->commentType->id(), $edit, t('Save')); + \Drupal::entityManager()->getStorage('comment_type')->resetCache(['other']); + $this->commentType = CommentType::load('other'); + + // Check the nofollow on the comment page. + $this->drupalGet('comment/' . $comment->id()); + $arguments = [ + ':rel' => 'nofollow', + ]; + $pattern_permalink = '//footer[contains(@class,"comment__meta")]/a[contains(@rel,:rel) and text()="Permalink"]'; + $permalink = $this->xpath($pattern_permalink, $arguments); + $this->assertTrue(!empty($permalink), 'Permalink link found and rel is set to nofollow.'); + + // Check the nofollow on the reply page. + $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment_other/' . $comment->id()); + $arguments = [ + ':rel' => 'nofollow', + ]; + $pattern_permalink = '//footer[contains(@class,"comment__meta")]/a[contains(@rel,:rel) and text()="Permalink"]'; + $permalink = $this->xpath($pattern_permalink, $arguments); + $this->assertTrue(!empty($permalink), 'Permalink link found and rel is set to nofollow.'); + } + +} diff --git a/core/modules/comment/tests/src/Functional/CommentTypeTest.php b/core/modules/comment/tests/src/Functional/CommentTypeTest.php index 291f9dc..b84f071 100644 --- a/core/modules/comment/tests/src/Functional/CommentTypeTest.php +++ b/core/modules/comment/tests/src/Functional/CommentTypeTest.php @@ -67,6 +67,7 @@ public function testCommentTypeCreation() { 'label' => 'title for foo', 'description' => '', 'target_entity_type_id' => 'node', + 'nofollow' => TRUE, ]; $this->drupalPostForm('admin/structure/comment/types/add', $edit, t('Save')); $comment_type = CommentType::load('foo'); diff --git a/core/modules/comment/tests/src/Kernel/CommentBundlesTest.php b/core/modules/comment/tests/src/Kernel/CommentBundlesTest.php index d907233..de1c84a 100644 --- a/core/modules/comment/tests/src/Kernel/CommentBundlesTest.php +++ b/core/modules/comment/tests/src/Kernel/CommentBundlesTest.php @@ -51,6 +51,7 @@ protected function setUp() { 'id' => 'comment_on_' . $id, 'label' => 'Comment on ' . $label, 'target_entity_type_id' => $id, + 'nofollow' => TRUE, ])->save(); } } diff --git a/core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php b/core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php index 1e0fd72..0d7c377 100644 --- a/core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php +++ b/core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php @@ -99,6 +99,7 @@ public function testAccessToAdministrativeFields() { 'label' => 'Default comments', 'description' => 'Default comment field', 'target_entity_type_id' => 'entity_test', + 'nofollow' => TRUE, ]); $comment_type->save(); diff --git a/core/modules/comment/tests/src/Kernel/CommentIntegrationTest.php b/core/modules/comment/tests/src/Kernel/CommentIntegrationTest.php index af73034..8db24e4 100644 --- a/core/modules/comment/tests/src/Kernel/CommentIntegrationTest.php +++ b/core/modules/comment/tests/src/Kernel/CommentIntegrationTest.php @@ -37,6 +37,7 @@ protected function setUp() { CommentType::create([ 'id' => 'comment', 'label' => $this->randomString(), + 'nofollow' => TRUE, ])->save(); } diff --git a/core/modules/comment/tests/src/Kernel/CommentStringIdEntitiesTest.php b/core/modules/comment/tests/src/Kernel/CommentStringIdEntitiesTest.php index adbe1b0..2f6519c 100644 --- a/core/modules/comment/tests/src/Kernel/CommentStringIdEntitiesTest.php +++ b/core/modules/comment/tests/src/Kernel/CommentStringIdEntitiesTest.php @@ -45,6 +45,7 @@ public function testCommentFieldNonStringId() { 'label' => 'foo', 'description' => '', 'target_entity_type_id' => 'entity_test_string_id', + 'nofollow' => TRUE, ]); $bundle->save(); $field_storage = FieldStorageConfig::create([ diff --git a/core/modules/comment/tests/src/Kernel/CommentValidationTest.php b/core/modules/comment/tests/src/Kernel/CommentValidationTest.php index 98b7715..fe6eae6 100644 --- a/core/modules/comment/tests/src/Kernel/CommentValidationTest.php +++ b/core/modules/comment/tests/src/Kernel/CommentValidationTest.php @@ -42,6 +42,7 @@ public function testValidation() { 'id' => 'comment', 'label' => 'comment', 'target_entity_type_id' => 'node', + 'nofollow' => TRUE, ])->save(); // Add comment field to content. diff --git a/core/modules/comment/tests/src/Kernel/Migrate/MigrateCommentStubTest.php b/core/modules/comment/tests/src/Kernel/Migrate/MigrateCommentStubTest.php index 0b256ea..cf3744f 100644 --- a/core/modules/comment/tests/src/Kernel/Migrate/MigrateCommentStubTest.php +++ b/core/modules/comment/tests/src/Kernel/Migrate/MigrateCommentStubTest.php @@ -48,6 +48,7 @@ protected function setUp() { 'id' => 'testcommenttype', 'label' => 'Test comment type', 'target_entity_type_id' => 'node', + 'nofollow' => TRUE, ])->save(); } diff --git a/core/modules/comment/tests/src/Kernel/Views/CommentAdminViewTest.php b/core/modules/comment/tests/src/Kernel/Views/CommentAdminViewTest.php index 86e8949..f9c553d 100644 --- a/core/modules/comment/tests/src/Kernel/Views/CommentAdminViewTest.php +++ b/core/modules/comment/tests/src/Kernel/Views/CommentAdminViewTest.php @@ -87,6 +87,7 @@ protected function setUp($import_test_views = TRUE) { 'label' => 'Default comments', 'target_entity_type_id' => 'entity_test', 'description' => 'Default comment field', + 'nofollow' => TRUE, ])->save(); // Create a commented entity. $entity = EntityTest::create(); diff --git a/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldFormatterSettingsTest.php b/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldFormatterSettingsTest.php index 576e86a..e4ea59a 100644 --- a/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldFormatterSettingsTest.php +++ b/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldFormatterSettingsTest.php @@ -42,26 +42,32 @@ protected function setUp() { CommentType::create([ 'id' => 'comment_node_page', 'label' => $this->randomMachineName(), + 'nofollow' => TRUE, ])->save(); CommentType::create([ 'id' => 'comment_node_article', 'label' => $this->randomMachineName(), + 'nofollow' => TRUE, ])->save(); CommentType::create([ 'id' => 'comment_node_blog', 'label' => $this->randomMachineName(), + 'nofollow' => TRUE, ])->save(); CommentType::create([ 'id' => 'comment_node_book', 'label' => $this->randomMachineName(), + 'nofollow' => TRUE, ])->save(); CommentType::create([ 'id' => 'comment_forum', 'label' => $this->randomMachineName(), + 'nofollow' => TRUE, ])->save(); CommentType::create([ 'id' => 'comment_node_test_content_type', 'label' => $this->randomMachineName(), + 'nofollow' => TRUE, ])->save(); NodeType::create([ diff --git a/core/modules/file/tests/src/Functional/FileFieldCreationTrait.php b/core/modules/file/tests/src/Functional/FileFieldCreationTrait.php deleted file mode 100644 index 89c713e..0000000 --- a/core/modules/file/tests/src/Functional/FileFieldCreationTrait.php +++ /dev/null @@ -1,86 +0,0 @@ - $entity_type, - 'field_name' => $name, - 'type' => 'file', - 'settings' => $storage_settings, - 'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1, - ]); - $field_storage->save(); - - $this->attachFileField($name, $entity_type, $bundle, $field_settings, $widget_settings); - return $field_storage; - } - - /** - * Attaches a file field to an entity. - * - * @param string $name - * The name of the new field (all lowercase), exclude the "field_" prefix. - * @param string $entity_type - * The entity type this field will be added to. - * @param string $bundle - * The bundle this field will be added to. - * @param array $field_settings - * A list of field settings that will be added to the defaults. - * @param array $widget_settings - * A list of widget settings that will be added to the widget defaults. - */ - public function attachFileField($name, $entity_type, $bundle, $field_settings = [], $widget_settings = []) { - $field = [ - 'field_name' => $name, - 'label' => $name, - 'entity_type' => $entity_type, - 'bundle' => $bundle, - 'required' => !empty($field_settings['required']), - 'settings' => $field_settings, - ]; - FieldConfig::create($field)->save(); - - entity_get_form_display($entity_type, $bundle, 'default') - ->setComponent($name, [ - 'type' => 'file_generic', - 'settings' => $widget_settings, - ]) - ->save(); - // Assign display settings. - entity_get_display($entity_type, $bundle, 'default') - ->setComponent($name, [ - 'label' => 'hidden', - 'type' => 'file_default', - ]) - ->save(); - } - -} diff --git a/core/modules/file/tests/src/Functional/FileFieldTestBase.php b/core/modules/file/tests/src/Functional/FileFieldTestBase.php index f3d1c22..937bf21 100644 --- a/core/modules/file/tests/src/Functional/FileFieldTestBase.php +++ b/core/modules/file/tests/src/Functional/FileFieldTestBase.php @@ -13,8 +13,6 @@ */ abstract class FileFieldTestBase extends BrowserTestBase { - use FileFieldCreationTrait; - /** * Modules to enable. * @@ -60,6 +58,76 @@ public function getLastFileId() { } /** + * Creates a new file field. + * + * @param string $name + * The name of the new field (all lowercase), exclude the "field_" prefix. + * @param string $entity_type + * The entity type. + * @param string $bundle + * The bundle that this field will be added to. + * @param array $storage_settings + * A list of field storage settings that will be added to the defaults. + * @param array $field_settings + * A list of instance settings that will be added to the instance defaults. + * @param array $widget_settings + * A list of widget settings that will be added to the widget defaults. + */ + public function createFileField($name, $entity_type, $bundle, $storage_settings = [], $field_settings = [], $widget_settings = []) { + $field_storage = FieldStorageConfig::create([ + 'entity_type' => $entity_type, + 'field_name' => $name, + 'type' => 'file', + 'settings' => $storage_settings, + 'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1, + ]); + $field_storage->save(); + + $this->attachFileField($name, $entity_type, $bundle, $field_settings, $widget_settings); + return $field_storage; + } + + /** + * Attaches a file field to an entity. + * + * @param string $name + * The name of the new field (all lowercase), exclude the "field_" prefix. + * @param string $entity_type + * The entity type this field will be added to. + * @param string $bundle + * The bundle this field will be added to. + * @param array $field_settings + * A list of field settings that will be added to the defaults. + * @param array $widget_settings + * A list of widget settings that will be added to the widget defaults. + */ + public function attachFileField($name, $entity_type, $bundle, $field_settings = [], $widget_settings = []) { + $field = [ + 'field_name' => $name, + 'label' => $name, + 'entity_type' => $entity_type, + 'bundle' => $bundle, + 'required' => !empty($field_settings['required']), + 'settings' => $field_settings, + ]; + FieldConfig::create($field)->save(); + + entity_get_form_display($entity_type, $bundle, 'default') + ->setComponent($name, [ + 'type' => 'file_generic', + 'settings' => $widget_settings, + ]) + ->save(); + // Assign display settings. + entity_get_display($entity_type, $bundle, 'default') + ->setComponent($name, [ + 'label' => 'hidden', + 'type' => 'file_default', + ]) + ->save(); + } + + /** * Updates an existing file field with new settings. */ public function updateFileField($name, $type_name, $field_settings = [], $widget_settings = []) { diff --git a/core/modules/file/tests/src/FunctionalJavascript/MaximumFileSizeExceededUploadTest.php b/core/modules/file/tests/src/FunctionalJavascript/MaximumFileSizeExceededUploadTest.php deleted file mode 100644 index 7ccd2b2..0000000 --- a/core/modules/file/tests/src/FunctionalJavascript/MaximumFileSizeExceededUploadTest.php +++ /dev/null @@ -1,119 +0,0 @@ -fileSystem = $this->container->get('file_system'); - - // Create the Article node type. - $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']); - - // Attach a file field to the node type. - $field_settings = ['file_extensions' => 'txt']; - $this->createFileField('field_file', 'node', 'article', [], $field_settings); - - // Log in as a content author who can create Articles. - $this->user = $this->drupalCreateUser([ - 'access content', - 'create article content', - ]); - $this->drupalLogin($this->user); - - // Disable the displaying of errors, so that the AJAX responses are not - // contaminated with error messages about exceeding the maximum POST size. - // @todo Remove this when issue #2905597 is fixed. - // @see https://www.drupal.org/node/2905597 - $this->originalDisplayErrorsValue = ini_set('display_errors', '0'); - } - - /** - * {@inheritdoc} - */ - protected function tearDown() { - // Restore the displaying of errors to the original value. - // @todo Remove this when issue #2905597 is fixed. - // @see https://www.drupal.org/node/2905597 - ini_set('display_errors', $this->originalDisplayErrorsValue); - - parent::tearDown(); - } - - /** - * Tests that uploading files exceeding maximum size are handled correctly. - */ - public function testUploadFileExceedingMaximumFileSize() { - $session = $this->getSession(); - - // Create a test file that exceeds the maximum POST size with 1 kilobyte. - $post_max_size = Bytes::toInt(ini_get('post_max_size')); - $invalid_file = $this->generateFile('exceeding_post_max_size', ceil(($post_max_size + 1024) / 1024), 1024); - - // Go to the node creation form and try to upload the test file. - $this->drupalGet('node/add/article'); - $page = $session->getPage(); - $page->attachFileToField("files[field_file_0]", $this->fileSystem->realpath($invalid_file)); - - // An error message should appear informing the user that the file exceeded - // the maximum file size. - $this->assertSession()->waitForElement('css', '.messages--error'); - // The error message includes the actual file size limit which depends on - // the current environment, so we check for a part of the message. - $this->assertSession()->pageTextContains('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size'); - - // Now upload a valid file and check that the error message disappears. - $valid_file = $this->generateFile('not_exceeding_post_max_size', 8, 8); - $page->attachFileToField("files[field_file_0]", $this->fileSystem->realpath($valid_file)); - $this->assertSession()->waitForElement('named', ['id_or_name', 'field_file_0_remove_button']); - $this->assertSession()->elementNotExists('css', '.messages--error'); - } - -} diff --git a/core/modules/forum/config/optional/comment.type.comment_forum.yml b/core/modules/forum/config/optional/comment.type.comment_forum.yml index 44a2d65..825c92a 100644 --- a/core/modules/forum/config/optional/comment.type.comment_forum.yml +++ b/core/modules/forum/config/optional/comment.type.comment_forum.yml @@ -8,3 +8,4 @@ id: comment_forum label: Comment_forum target_entity_type_id: node description: 'Default comment field' +nofollow: TRUE diff --git a/core/modules/migrate/tests/src/Kernel/NodeCommentCombinationTrait.php b/core/modules/migrate/tests/src/Kernel/NodeCommentCombinationTrait.php index 6f04b3a..f4b6939 100644 --- a/core/modules/migrate/tests/src/Kernel/NodeCommentCombinationTrait.php +++ b/core/modules/migrate/tests/src/Kernel/NodeCommentCombinationTrait.php @@ -32,6 +32,7 @@ protected function createNodeCommentCombination($node_type, $comment_type = NULL 'id' => $comment_type, 'label' => $this->randomString(), 'target_entity_type_id' => 'node', + 'nofollow' => TRUE, ])->save(); } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/CommentType/CommentTypeResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/CommentType/CommentTypeResourceTestBase.php index 9d88211..968998a 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/CommentType/CommentTypeResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/CommentType/CommentTypeResourceTestBase.php @@ -44,6 +44,7 @@ protected function createEntity() { 'label' => 'Camelids', 'description' => 'Camelids are large, strictly herbivorous animals with slender necks and long legs.', 'target_entity_type_id' => 'node', + 'nofollow' => TRUE, ]); $camelids->save(); @@ -63,6 +64,7 @@ protected function getExpectedNormalizedEntity() { 'langcode' => 'en', 'status' => TRUE, 'target_entity_type_id' => 'node', + 'nofollow' => TRUE, 'uuid' => $this->entity->uuid(), ]; } diff --git a/core/modules/views/tests/src/Kernel/Entity/ViewEntityDependenciesTest.php b/core/modules/views/tests/src/Kernel/Entity/ViewEntityDependenciesTest.php index 4929c7e..e826bca 100644 --- a/core/modules/views/tests/src/Kernel/Entity/ViewEntityDependenciesTest.php +++ b/core/modules/views/tests/src/Kernel/Entity/ViewEntityDependenciesTest.php @@ -47,6 +47,7 @@ protected function setUp($import_test_views = TRUE) { 'label' => 'Comment settings', 'description' => 'Comment settings', 'target_entity_type_id' => 'node', + 'nofollow' => TRUE, ]); $comment_type->save(); diff --git a/core/profiles/standard/config/install/comment.type.comment.yml b/core/profiles/standard/config/install/comment.type.comment.yml index ddcbbc9..e64186f 100644 --- a/core/profiles/standard/config/install/comment.type.comment.yml +++ b/core/profiles/standard/config/install/comment.type.comment.yml @@ -5,3 +5,4 @@ id: comment label: 'Default comments' target_entity_type_id: node description: 'Allows commenting on content' +nofollow: TRUE diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityDisplayBaseTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityDisplayBaseTest.php index ec1a55c..1911dc1 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityDisplayBaseTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityDisplayBaseTest.php @@ -67,6 +67,7 @@ public function testOnDependencyRemoval() { 'label' => 'entity_test', 'description' => '', 'target_entity_type_id' => 'entity_test', + 'nofollow' => TRUE, ]); $comment_bundle->save(); $comment_display = EntityViewDisplay::create([ diff --git a/core/tests/Drupal/Tests/Core/Form/EventSubscriber/FormAjaxSubscriberTest.php b/core/tests/Drupal/Tests/Core/Form/EventSubscriber/FormAjaxSubscriberTest.php index 278ec06..050d745 100644 --- a/core/tests/Drupal/Tests/Core/Form/EventSubscriber/FormAjaxSubscriberTest.php +++ b/core/tests/Drupal/Tests/Core/Form/EventSubscriber/FormAjaxSubscriberTest.php @@ -181,7 +181,7 @@ public function testOnExceptionBrokenPostRequest() { $this->assertSame(200, $actual_response->headers->get('X-Status-Code')); $expected_commands[] = [ 'command' => 'insert', - 'method' => 'prepend', + 'method' => 'replaceWith', 'selector' => NULL, 'data' => $rendered_output, 'settings' => NULL, diff --git a/core/tests/Drupal/Tests/TestFileCreationTrait.php b/core/tests/Drupal/Tests/TestFileCreationTrait.php index a2f119b..5bb5394 100644 --- a/core/tests/Drupal/Tests/TestFileCreationTrait.php +++ b/core/tests/Drupal/Tests/TestFileCreationTrait.php @@ -164,8 +164,7 @@ public static function generateFile($filename, $width, $lines, $type = 'binary-t } // Create filename. - $filename = 'public://' . $filename . '.txt'; - file_put_contents($filename, $text); + file_put_contents('public://' . $filename . '.txt', $text); return $filename; }