first commit

This commit is contained in:
2024-07-15 12:33:27 +02:00
commit ce50ae282b
22084 changed files with 2623791 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
name: 'Comment base field test'
type: module
description: 'Test comment as a base field'
package: Testing
# version: VERSION
dependencies:
- drupal:comment
- drupal:entity_test
# Information added by Drupal.org packaging script on 2024-07-04
version: '10.3.1'
project: 'drupal'
datestamp: 1720094222

View File

@@ -0,0 +1,39 @@
<?php
namespace Drupal\comment_base_field_test\Entity;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\entity_test\Entity\EntityTest;
/**
* Defines a test entity class for comment as a base field.
*
* @ContentEntityType(
* id = "comment_test_base_field",
* label = @Translation("Test comment - base field"),
* base_table = "comment_test_base_field",
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "bundle" = "type"
* },
* )
*/
class CommentTestBaseField extends EntityTest {
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['test_comment'] = BaseFieldDefinition::create('comment')
->setLabel(t('A comment field'))
->setSetting('comment_type', 'test_comment_type')
->setDefaultValue([
'status' => CommentItemInterface::OPEN,
]);
return $fields;
}
}

View File

@@ -0,0 +1,10 @@
name: 'Comment configurable display module tests'
type: module
description: 'Support module for comment \Drupal\Core\Field\BaseFieldDefinition::setDisplayConfigurable() testing.'
package: Testing
# version: VERSION
# Information added by Drupal.org packaging script on 2024-07-04
version: '10.3.1'
project: 'drupal'
datestamp: 1720094222

View File

@@ -0,0 +1,28 @@
<?php
/**
* @file
* A module for testing making comment base fields' displays configurable.
*/
use Drupal\Core\Entity\EntityTypeInterface;
/**
* Implements hook_entity_base_field_info_alter().
*/
function comment_display_configurable_test_entity_base_field_info_alter(&$base_field_definitions, EntityTypeInterface $entity_type) {
if ($entity_type->id() == 'comment') {
foreach (['created', 'uid', 'pid', 'subject'] as $field) {
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $base_field_definitions */
$base_field_definitions[$field]->setDisplayConfigurable('view', TRUE);
}
}
}
/**
* Implements hook_entity_type_build().
*/
function comment_display_configurable_test_entity_type_build(array &$entity_types) {
// Allow skipping of extra preprocessing for configurable display.
$entity_types['comment']->set('enable_base_field_custom_preprocess_skipping', TRUE);
}

View File

@@ -0,0 +1,12 @@
name: 'Comment empty titles test'
type: module
description: 'Support module for testing empty title accessibility with Comment module.'
package: Testing
# version: VERSION
dependencies:
- drupal:comment
# Information added by Drupal.org packaging script on 2024-07-04
version: '10.3.1'
project: 'drupal'
datestamp: 1720094222

View File

@@ -0,0 +1,13 @@
<?php
/**
* @file
* Empties comment titles to test markup in edge case scenarios.
*/
/**
* Implements hook_preprocess_comment().
*/
function comment_empty_title_test_preprocess_comment(&$variables) {
$variables['title'] = '';
}

View File

@@ -0,0 +1,12 @@
name: 'Comment test'
type: module
description: 'Support module for Comment module testing.'
package: Testing
# version: VERSION
dependencies:
- drupal:comment
# Information added by Drupal.org packaging script on 2024-07-04
version: '10.3.1'
project: 'drupal'
datestamp: 1720094222

View File

@@ -0,0 +1,43 @@
<?php
/**
* @file
* Implements comment-related hooks to test API interactions.
*/
use Drupal\comment\CommentInterface;
use Drupal\Core\Url;
/**
* Implements hook_entity_type_alter().
*/
function comment_test_entity_type_alter(array &$entity_types) {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
if (\Drupal::languageManager()->isMultilingual()) {
// Enable language handling for comment fields.
$translation = $entity_types['comment']->get('translation');
$translation['comment_test'] = TRUE;
$entity_types['comment']->set('translation', $translation);
}
}
/**
* Implements hook_comment_links_alter().
*/
function comment_test_comment_links_alter(array &$links, CommentInterface &$entity, array &$context) {
// Allow tests to enable or disable this alter hook.
if (!\Drupal::state()->get('comment_test_links_alter_enabled', FALSE)) {
return;
}
$links['comment_test'] = [
'#theme' => 'links__comment__comment_test',
'#attributes' => ['class' => ['links', 'inline']],
'#links' => [
'comment-report' => [
'title' => t('Report'),
'url' => Url::fromRoute('comment_test.report', ['comment' => $entity->id()], ['query' => ['token' => \Drupal::getContainer()->get('csrf_token')->get("comment/{$entity->id()}/report")]]),
],
],
];
}

View File

@@ -0,0 +1,8 @@
comment_test.report:
path: '/comment/{comment}/report'
defaults:
_title: 'Report'
_controller: '\Drupal\comment_test\Controller\CommentTestController::commentReport'
requirements:
_access: 'TRUE'
_csrf_token: 'TRUE'

View File

@@ -0,0 +1,20 @@
<?php
namespace Drupal\comment_test\Controller;
use Drupal\comment\CommentInterface;
use Drupal\Core\Controller\ControllerBase;
/**
* Controller for the comment_test.module.
*/
class CommentTestController extends ControllerBase {
/**
* Provides a comment report.
*/
public function commentReport(CommentInterface $comment) {
return ['#markup' => $this->t('Report for a comment')];
}
}

View File

@@ -0,0 +1,13 @@
name: 'Comment test views'
type: module
description: 'Provides default views for views comment tests.'
package: Testing
# version: VERSION
dependencies:
- drupal:comment
- drupal:views
# Information added by Drupal.org packaging script on 2024-07-04
version: '10.3.1'
project: 'drupal'
datestamp: 1720094222

View File

@@ -0,0 +1,42 @@
langcode: en
status: true
dependencies:
module:
- comment
- entity_test
- user
id: test_comment
label: test_comment
module: views
description: ''
tag: ''
base_table: comment_field_data
base_field: cid
display:
default:
display_plugin: default
display_title: Default
id: default
position: 0
display_options:
defaults:
fields: false
pager: false
sorts: false
row:
type: fields
fields:
subject:
id: subject
table: comment_field_data
field: subject
label: ''
type: string
plugin_id: field
entity_type: comment
entity_field: subject
pager:
options:
offset: 0
type: none
sorts: { }

View File

@@ -0,0 +1,244 @@
langcode: en
status: true
dependencies:
module:
- node
- user
id: test_comment_count
label: 'test comment count'
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
display:
default:
display_plugin: default
id: default
display_title: Default
position: 0
display_options:
access:
type: perm
options:
perm: 'access content'
cache:
type: tag
options: { }
query:
type: views_query
options:
disable_sql_rewrite: false
distinct: false
replica: false
query_comment: ''
query_tags: { }
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: mini
options:
items_per_page: 10
offset: 0
id: 0
total_pages: null
expose:
items_per_page: false
items_per_page_label: 'Items per page'
items_per_page_options: '5, 10, 25, 50'
items_per_page_options_all: false
items_per_page_options_all_label: '- All -'
offset: false
offset_label: Offset
tags:
previous:
next:
style:
type: default
options:
grouping: { }
row_class: ''
default_row_class: true
uses_fields: false
row:
type: fields
options:
inline: { }
separator: ''
hide_empty: false
default_field_elements: true
fields:
title:
id: title
table: node_field_data
field: title
entity_type: node
entity_field: title
label: ''
alter:
alter_text: false
make_link: false
absolute: false
trim: false
word_boundary: false
ellipsis: false
strip_tags: false
html: false
hide_empty: false
empty_zero: false
settings:
link_to_entity: true
plugin_id: field
relationship: none
group_type: group
admin_label: ''
exclude: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_alter_empty: true
click_sort_column: value
type: string
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
comment_count:
id: comment_count
table: comment_entity_statistics
field: comment_count
relationship: none
group_type: group
admin_label: ''
label: ''
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
set_precision: false
precision: 0
decimal: .
separator: ''
format_plural: false
format_plural_string: !!binary MQNAY291bnQ=
prefix: ''
suffix: ''
plugin_id: numeric
filters:
status:
value: '1'
table: node_field_data
field: status
plugin_id: boolean
entity_type: node
entity_field: status
id: status
expose:
operator: ''
operator_limit_selection: false
operator_list: { }
group: 1
sorts:
created:
id: created
table: node_field_data
field: created
order: DESC
entity_type: node
entity_field: created
plugin_id: date
relationship: none
group_type: group
admin_label: ''
exposed: false
expose:
label: ''
granularity: second
header: { }
footer: { }
empty: { }
relationships: { }
arguments: { }
display_extenders: { }
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
- 'user.node_grants:view'
- user.permissions
tags: { }
page_1:
display_plugin: page
id: page_1
display_title: Page
position: 1
display_options:
display_extenders: { }
path: test-comment-count
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
- 'user.node_grants:view'
- user.permissions
tags: { }

View File

@@ -0,0 +1,202 @@
langcode: en
status: true
dependencies:
module:
- comment
- user
id: test_comment_field_name
label: 'Comment field name test'
module: views
description: ''
tag: ''
base_table: comment_field_data
base_field: cid
display:
default:
display_plugin: default
id: default
display_title: Default
position: 0
display_options:
access:
type: perm
options:
perm: 'access comments'
cache:
type: tag
options: { }
query:
type: views_query
options:
disable_sql_rewrite: false
distinct: false
replica: false
query_comment: ''
query_tags: { }
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: full
options:
items_per_page: 10
offset: 0
id: 0
total_pages: null
expose:
items_per_page: false
items_per_page_label: 'Items per page'
items_per_page_options: '5, 10, 25, 50'
items_per_page_options_all: false
items_per_page_options_all_label: '- All -'
offset: false
offset_label: Offset
tags:
previous: ' Previous'
next: 'Next '
first: '« First'
last: 'Last »'
quantity: 9
style:
type: default
row:
type: fields
relationships: { }
fields:
field_name:
id: field_name
table: comment_field_data
field: field_name
relationship: none
group_type: group
admin_label: ''
label: ''
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: string
settings:
link_to_entity: false
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
entity_type: comment
entity_field: field_name
plugin_id: field
filters:
field_name:
id: field_name
table: comment_field_data
field: field_name
relationship: none
group_type: group
admin_label: ''
operator: starts
value: comment
group: 1
exposed: false
expose:
operator_id: ''
label: ''
description: ''
use_operator: false
operator: ''
identifier: ''
required: false
remember: false
multiple: false
remember_roles:
authenticated: authenticated
is_grouped: false
group_info:
label: ''
description: ''
identifier: ''
optional: true
widget: select
multiple: false
remember: false
default_group: All
default_group_multiple: { }
group_items: { }
entity_type: comment
entity_field: field_name
plugin_id: string
sorts:
field_name:
id: field_name
table: comment_field_data
field: field_name
relationship: none
group_type: group
admin_label: ''
order: ASC
exposed: false
expose:
label: ''
entity_type: comment
entity_field: field_name
plugin_id: standard
title: 'Comment Field Name test'
header: { }
footer: { }
empty: { }
arguments: { }
display_extenders: { }
cache_metadata:
contexts:
- languages
- user
max-age: 0

View File

@@ -0,0 +1,253 @@
langcode: en
status: true
dependencies:
module:
- comment
- user
id: test_comment_operations
label: test_comment_operations
module: views
description: ''
tag: ''
base_table: comment_field_data
base_field: cid
display:
default:
display_plugin: default
id: default
display_title: Default
position: 0
display_options:
access:
type: perm
options:
perm: 'administer comments'
cache:
type: tag
options: { }
query:
type: views_query
options:
disable_sql_rewrite: false
distinct: false
replica: false
query_comment: ''
query_tags: { }
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: full
options:
items_per_page: 10
offset: 0
id: 0
total_pages: null
expose:
items_per_page: false
items_per_page_label: 'Items per page'
items_per_page_options: '5, 10, 25, 50'
items_per_page_options_all: false
items_per_page_options_all_label: '- All -'
offset: false
offset_label: Offset
tags:
previous: ' Previous'
next: 'Next '
first: '« First'
last: 'Last »'
quantity: 9
style:
type: table
options:
grouping: { }
row_class: ''
default_row_class: true
override: true
sticky: false
caption: ''
summary: ''
description: ''
columns:
subject: subject
operations: operations
info:
subject:
sortable: false
default_sort_order: asc
align: ''
separator: ''
empty_column: false
responsive: ''
operations:
sortable: false
default_sort_order: asc
align: ''
separator: ''
empty_column: false
responsive: ''
default: '-1'
empty_table: false
row:
type: fields
options:
inline: { }
separator: ''
hide_empty: false
default_field_elements: true
relationships: { }
fields:
subject:
id: subject
table: comment_field_data
field: subject
relationship: none
group_type: group
admin_label: ''
label: Subject
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: false
ellipsis: false
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: string
settings:
link_to_entity: true
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
entity_type: comment
entity_field: subject
plugin_id: field
operations:
id: operations
table: comment
field: operations
relationship: none
group_type: group
admin_label: ''
label: Operations
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
destination: true
entity_type: comment
plugin_id: entity_operations
filters: { }
sorts: { }
header: { }
footer: { }
empty: { }
arguments: { }
display_extenders: { }
cache_metadata:
max-age: 0
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
- user.permissions
tags: { }
page_1:
display_plugin: page
id: page_1
display_title: Page
position: 1
display_options:
display_extenders: { }
path: test-comment-operations
cache_metadata:
max-age: 0
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
- user.permissions
tags: { }

View File

@@ -0,0 +1,156 @@
langcode: en
status: true
dependencies:
module:
- comment
- node
- user
id: test_comment_row
label: test_comment_row
module: views
description: ''
tag: ''
base_table: comment_field_data
base_field: cid
display:
default:
display_plugin: default
id: default
display_title: Default
position: 1
display_options:
access:
type: perm
options:
perm: 'access content'
cache:
type: tag
options: { }
query:
type: views_query
options:
disable_sql_rewrite: false
distinct: false
replica: false
query_comment: ''
query_tags: { }
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: full
options:
items_per_page: 10
offset: 0
id: 0
total_pages: null
expose:
items_per_page: false
items_per_page_label: 'Items per page'
items_per_page_options: '5, 10, 25, 50'
items_per_page_options_all: false
items_per_page_options_all_label: '- All -'
offset: false
offset_label: Offset
tags:
previous: ' Previous'
next: 'Next '
first: '« First'
last: 'Last »'
quantity: 9
style:
type: default
row:
type: 'entity:comment'
options:
view_mode: full
relationships:
node:
field: node
id: node
required: true
table: comment_field_data
relationship: none
group_type: group
admin_label: Content
fields:
subject:
id: subject
table: comment_field_data
field: subject
label: ''
alter:
alter_text: false
make_link: false
absolute: false
trim: false
word_boundary: false
ellipsis: false
strip_tags: false
html: false
hide_empty: false
empty_zero: false
relationship: none
group_type: group
admin_label: ''
exclude: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_alter_empty: true
type: string
settings:
link_to_entity: true
plugin_id: field
entity_type: comment
entity_field: subject
filters:
status:
value: '1'
table: comment_field_data
field: status
id: status
expose:
operator: ''
group: 1
plugin_id: boolean
entity_type: comment
entity_field: status
status_node:
value: '1'
table: node_field_data
field: status
relationship: node
id: status_node
expose:
operator: ''
group: 1
plugin_id: boolean
entity_type: node
entity_field: status
sorts: { }
title: test_comment_row
header: { }
footer: { }
empty: { }
arguments: { }
page_1:
display_plugin: page
id: page_1
display_title: Page
position: 1
display_options:
path: test-comment-row

View File

@@ -0,0 +1,78 @@
langcode: en
status: true
dependencies:
module:
- comment
- node
- user
id: test_comment_rss
label: test_comment_rss
module: views
description: ''
tag: ''
base_table: comment_field_data
base_field: cid
display:
default:
display_plugin: default
id: default
display_title: Default
position: null
display_options:
access:
type: perm
cache:
type: tag
query:
type: views_query
exposed_form:
type: basic
pager:
type: full
style:
type: default
row:
type: fields
relationships:
node:
field: node
id: node
required: true
table: comment_field_data
fields:
subject:
id: subject
table: comment_field_data
field: subject
plugin_id: field
label: ''
alter:
alter_text: false
make_link: false
absolute: false
trim: false
word_boundary: false
ellipsis: false
strip_tags: false
html: false
hide_empty: false
empty_zero: false
type: string
settings:
link_to_entity: true
entity_type: comment
entity_field: subject
filters: { }
sorts: { }
feed_1:
display_plugin: feed
id: feed_1
display_title: Feed
position: null
display_options:
path: test-comment-rss
defaults:
row: false
row:
type: comment_rss
options: {}

View File

@@ -0,0 +1,301 @@
langcode: en
status: true
dependencies:
module:
- comment
- user
id: test_comment_schema
label: Comments
module: comment
description: 'Find and manage comments.'
tag: default
base_table: comment_field_data
base_field: cid
display:
default:
id: default
display_title: Default
display_plugin: default
position: 0
display_options:
title: Comments
fields:
subject:
id: subject
table: comment_field_data
field: subject
relationship: none
group_type: group
admin_label: ''
entity_type: comment
entity_field: subject
plugin_id: field
label: Title
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: comment_permalink
settings:
link_to_entity: false
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
approve_comment:
id: approve_comment
table: comment
field: approve_comment
relationship: none
group_type: group
admin_label: ''
entity_type: comment
plugin_id: comment_link_approve
label: 'Link to approve comment'
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
text: Approve
status:
id: status
table: comment_field_data
field: status
relationship: none
group_type: group
admin_label: ''
entity_type: comment
entity_field: status
plugin_id: field
label: 'Approved status'
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: boolean
settings:
format: yes-no
format_custom_false: ''
format_custom_true: Approved
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
pager:
type: none
options:
offset: 0
exposed_form:
type: basic
options:
submit_button: Filter
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
access:
type: perm
options:
perm: 'administer comments'
cache:
type: none
options: { }
empty: { }
sorts: { }
arguments: { }
filters: { }
filter_groups:
operator: AND
groups: { }
style:
type: table
options:
grouping: { }
row_class: ''
default_row_class: false
columns:
approve_comment: approve_comment
default: '-1'
info:
approve_comment:
sortable: false
default_sort_order: asc
align: ''
separator: ''
empty_column: false
responsive: ''
override: false
sticky: false
summary: ''
empty_table: false
caption: ''
description: ''
row:
type: fields
query:
type: views_query
options:
query_comment: ''
disable_sql_rewrite: false
distinct: false
replica: false
query_tags: { }
relationships: { }
css_class: ''
use_ajax: false
group_by: false
show_admin_links: true
use_more: false
use_more_always: true
use_more_text: more
header: { }
footer: { }
hide_attachment_summary: false
display_extenders: { }
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- user.permissions
tags: { }
cacheable: false
page_1:
id: page_1
display_title: Page
display_plugin: page
position: 1
display_options:
display_extenders: { }
path: admin/moderate-comments
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- user.permissions
tags: { }

View File

@@ -0,0 +1,66 @@
langcode: en
status: true
dependencies:
module:
- comment
- comment_test_views
- node
- user
id: test_comment_user_uid
label: test_comment_user_uid
module: comment_test_views
description: ''
tag: default
base_table: node_field_data
base_field: nid
display:
default:
display_options:
access:
type: perm
arguments:
uid_touch:
default_argument_type: fixed
field: uid_touch
id: uid_touch
summary:
format: default_summary
number_of_records: 0
summary_options:
items_per_page: 25
table: node_field_data
plugin_id: argument_comment_user_uid
entity_type: node
cache:
type: tag
exposed_form:
type: basic
fields:
nid:
field: nid
id: nid
table: node_field_data
plugin_id: node
entity_type: node
entity_field: nid
title:
field: title
id: title
table: node_field_data
plugin_id: field
entity_type: node
entity_field: title
pager:
type: full
query:
options:
query_comment: ''
type: views_query
style:
type: default
row:
type: 'entity:node'
display_plugin: default
display_title: Default
id: default
position: 0

View File

@@ -0,0 +1,324 @@
langcode: en
status: true
dependencies:
module:
- comment
- node
- user
id: test_field_filters
label: 'Test field filters'
module: views
description: ''
tag: ''
base_table: comment_field_data
base_field: cid
display:
default:
display_plugin: default
id: default
display_title: Default
position: 0
display_options:
access:
type: perm
options:
perm: 'access content'
cache:
type: tag
options: { }
query:
type: views_query
options:
disable_sql_rewrite: false
distinct: false
replica: false
query_comment: ''
query_tags: { }
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: none
options:
items_per_page: 0
offset: 0
style:
type: default
row:
type: 'entity:comment'
options:
view_mode: default
relationships:
node:
id: node
table: comment_field_data
field: node
required: true
plugin_id: standard
relationship: none
group_type: group
admin_label: Content
fields:
subject:
id: subject
table: comment_field_data
field: subject
label: ''
alter:
alter_text: false
make_link: false
absolute: false
trim: false
word_boundary: false
ellipsis: false
strip_tags: false
html: false
hide_empty: false
empty_zero: false
relationship: none
group_type: group
admin_label: ''
exclude: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_alter_empty: true
type: string
settings:
link_to_entity: false
plugin_id: field
entity_type: comment
entity_field: subject
filters:
subject:
id: subject
table: comment_field_data
field: subject
relationship: none
group_type: group
admin_label: ''
operator: contains
value: Comida
group: 1
exposed: false
expose:
operator_id: ''
label: ''
description: ''
use_operator: false
operator: ''
identifier: ''
required: false
remember: false
multiple: false
remember_roles:
authenticated: authenticated
is_grouped: false
group_info:
label: ''
description: ''
identifier: ''
optional: true
widget: select
multiple: false
remember: false
default_group: All
default_group_multiple: { }
group_items: { }
plugin_id: string
entity_type: comment
entity_field: subject
sorts: { }
title: 'Title filter page'
header: { }
footer: { }
empty: { }
arguments: { }
rendering_language: '***LANGUAGE_entity_translation***'
page_bf:
display_plugin: page
id: page_bf
display_title: 'Body Comida'
position: 1
display_options:
path: test-body-filter
display_description: ''
filters:
comment_body_value:
id: comment_body_value
table: comment__comment_body
field: comment_body_value
relationship: none
group_type: group
admin_label: ''
operator: contains
value: Comida
group: 1
exposed: false
expose:
operator_id: ''
label: ''
description: ''
use_operator: false
operator: ''
identifier: ''
required: false
remember: false
multiple: false
remember_roles:
authenticated: authenticated
is_grouped: false
group_info:
label: ''
description: ''
identifier: ''
optional: true
widget: select
multiple: false
remember: false
default_group: All
default_group_multiple: { }
group_items: { }
plugin_id: string
entity_type: comment
entity_field: comment_body
defaults:
filters: false
filter_groups: false
title: false
filter_groups:
operator: AND
groups:
1: AND
title: 'Body filter page'
page_bp:
display_plugin: page
id: page_bp
display_title: 'Body Paris'
position: 1
display_options:
path: test-body-paris
display_description: ''
filters:
comment_body_value:
id: comment_body_value
table: comment__comment_body
field: comment_body_value
relationship: none
group_type: group
admin_label: ''
operator: contains
value: Paris
group: 1
exposed: false
expose:
operator_id: ''
label: ''
description: ''
use_operator: false
operator: ''
identifier: ''
required: false
remember: false
multiple: false
remember_roles:
authenticated: authenticated
is_grouped: false
group_info:
label: ''
description: ''
identifier: ''
optional: true
widget: select
multiple: false
remember: false
default_group: All
default_group_multiple: { }
group_items: { }
plugin_id: string
entity_type: comment
entity_field: comment_body
defaults:
filters: false
filter_groups: false
title: false
filter_groups:
operator: AND
groups:
1: AND
title: 'Body filter page'
page_tc:
display_plugin: page
id: page_tc
display_title: 'Title Comida'
position: 1
display_options:
path: test-title-filter
display_description: ''
page_tp:
display_plugin: page
id: page_tp
display_title: 'Title Paris'
position: 1
display_options:
path: test-title-paris
display_description: ''
filters:
subject:
id: subject
table: comment_field_data
field: subject
relationship: none
group_type: group
admin_label: ''
operator: contains
value: Paris
group: 1
exposed: false
expose:
operator_id: ''
label: ''
description: ''
use_operator: false
operator: ''
identifier: ''
required: false
remember: false
multiple: false
remember_roles:
authenticated: authenticated
is_grouped: false
group_info:
label: ''
description: ''
identifier: ''
optional: true
widget: select
multiple: false
remember: false
default_group: All
default_group_multiple: { }
group_items: { }
plugin_id: string
entity_type: comment
entity_field: subject
defaults:
filters: false
filter_groups: false
filter_groups:
operator: AND
groups:
1: AND

View File

@@ -0,0 +1,162 @@
langcode: en
status: true
dependencies:
module:
- comment
- node
- user
id: '2505879'
label: '2505879'
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
display:
default:
display_plugin: default
id: default
display_title: Default
position: 0
display_options:
access:
type: perm
options:
perm: 'access content'
cache:
type: tag
options: { }
query:
type: views_query
options:
disable_sql_rewrite: false
distinct: false
replica: false
query_comment: ''
query_tags: { }
pager:
type: full
style:
type: table
row:
type: fields
fields:
title:
id: title
table: node_field_data
field: title
entity_type: node
entity_field: title
alter:
alter_text: false
make_link: false
absolute: false
trim: false
word_boundary: false
ellipsis: false
strip_tags: false
html: false
hide_empty: false
empty_zero: false
settings:
link_to_entity: true
plugin_id: field
relationship: none
group_type: group
admin_label: ''
label: Title
exclude: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_alter_empty: true
click_sort_column: value
type: string
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
new_comments:
id: new_comments
table: node
field: new_comments
relationship: none
group_type: group
admin_label: ''
label: 'New comments'
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
set_precision: false
precision: 0
decimal: .
prefix: ''
suffix: ''
link_to_comment: true
entity_type: node
plugin_id: node_new_comments
filters: { }
sorts: { }
title: ''
header: { }
footer: { }
empty: { }
relationships: { }
arguments: { }
display_extenders: { }
page_1:
display_plugin: page
id: page_1
display_title: Page
position: 1
display_options:
path: 'test-new-comments'

View File

@@ -0,0 +1,127 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
/**
* Tests comment administration and preview access.
*
* @group comment
*/
class CommentAccessTest extends BrowserTestBase {
use CommentTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'node',
'comment',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Node for commenting.
*
* @var \Drupal\node\NodeInterface
*/
protected $unpublishedNode;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$node_type = NodeType::create([
'type' => 'article',
'name' => 'Article',
]);
$node_type->save();
$node_author = $this->drupalCreateUser([
'create article content',
'access comments',
]);
$this->drupalLogin($this->drupalCreateUser([
'edit own comments',
'skip comment approval',
'post comments',
'access comments',
'access content',
]));
$this->addDefaultCommentField('node', 'article');
$this->unpublishedNode = $this->createNode([
'title' => 'This is unpublished',
'uid' => $node_author->id(),
'status' => 0,
'type' => 'article',
]);
$this->unpublishedNode->save();
}
/**
* Tests commenting disabled for access-blocked entities.
*/
public function testCannotCommentOnEntitiesYouCannotView(): void {
$assert = $this->assertSession();
$comment_url = 'comment/reply/node/' . $this->unpublishedNode->id() . '/comment';
// Commenting on an unpublished node results in access denied.
$this->drupalGet($comment_url);
$assert->statusCodeEquals(403);
// Publishing the node grants access.
$this->unpublishedNode->setPublished()->save();
$this->drupalGet($comment_url);
$assert->statusCodeEquals(200);
}
/**
* Tests cannot view comment reply form on entities you cannot view.
*/
public function testCannotViewCommentReplyFormOnEntitiesYouCannotView(): void {
$assert = $this->assertSession();
// Create a comment on an unpublished node.
$comment = Comment::create([
'entity_type' => 'node',
'name' => 'Tony',
'hostname' => 'magic.example.com',
'mail' => 'foo@example.com',
'subject' => 'Comment on unpublished node',
'entity_id' => $this->unpublishedNode->id(),
'comment_type' => 'comment',
'field_name' => 'comment',
'pid' => 0,
'uid' => $this->unpublishedNode->getOwnerId(),
'status' => 1,
]);
$comment->save();
$comment_url = 'comment/reply/node/' . $this->unpublishedNode->id() . '/comment/' . $comment->id();
// Replying to a comment on an unpublished node results in access denied.
$this->drupalGet($comment_url);
$assert->statusCodeEquals(403);
// Publishing the node grants access.
$this->unpublishedNode->setPublished()->save();
$this->drupalGet($comment_url);
$assert->statusCodeEquals(200);
}
}

View File

@@ -0,0 +1,293 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\CommentInterface;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Html;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\user\RoleInterface;
use Drupal\comment\Entity\Comment;
/**
* Tests comment approval functionality.
*
* @group comment
*/
class CommentAdminTest extends CommentTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->drupalPlaceBlock('page_title_block');
}
/**
* Tests comment approval functionality through admin/content/comment.
*/
public function testApprovalAdminInterface(): void {
// Set anonymous comments to require approval.
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => TRUE,
'post comments' => TRUE,
'skip comment approval' => FALSE,
]);
$this->drupalLogin($this->adminUser);
// Ensure that doesn't require contact info.
$this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAYNOT_CONTACT);
// Test that the comments page loads correctly when there are no comments
$this->drupalGet('admin/content/comment');
$this->assertSession()->pageTextContains('No comments available.');
$this->drupalLogout();
// Post anonymous comment without contact info.
$subject = $this->randomMachineName();
$body = $this->randomMachineName();
// Set $contact to true so that it won't check for id and message.
$this->postComment($this->node, $body, $subject, TRUE);
$this->assertSession()->pageTextContains('Your comment has been queued for review by site administrators and will be published after approval.');
// Get unapproved comment id.
$this->drupalLogin($this->adminUser);
$anonymous_comment4 = $this->getUnapprovedComment($subject);
$anonymous_comment4 = Comment::create([
'cid' => $anonymous_comment4,
'subject' => $subject,
'comment_body' => $body,
'entity_id' => $this->node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
]);
$this->drupalLogout();
$this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
// Approve comment.
$this->drupalLogin($this->adminUser);
$this->performCommentOperation($anonymous_comment4, 'publish', TRUE);
$this->drupalLogout();
$this->drupalGet('node/' . $this->node->id());
$this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
// Post 2 anonymous comments without contact info.
$comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// Publish multiple comments in one operation.
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/content/comment/approval');
$this->assertSession()->pageTextContains('Unapproved comments (2)');
$edit = [
"comments[{$comments[0]->id()}]" => 1,
"comments[{$comments[1]->id()}]" => 1,
];
$this->submitForm($edit, 'Update');
$this->assertSession()->pageTextContains('Unapproved comments (0)');
// Delete multiple comments in one operation.
$edit = [
'operation' => 'delete',
"comments[{$comments[0]->id()}]" => 1,
"comments[{$comments[1]->id()}]" => 1,
"comments[{$anonymous_comment4->id()}]" => 1,
];
$this->submitForm($edit, 'Update');
$this->assertSession()->pageTextContains('Are you sure you want to delete these comments and all their children?');
$this->submitForm([], 'Delete');
$this->assertSession()->pageTextContains('No comments available.');
// Test message when no comments selected.
$edit = [
'operation' => 'delete',
];
$this->submitForm($edit, 'Update');
$this->assertSession()->pageTextContains('Select one or more comments to perform the update on.');
// Make sure the label of unpublished node is not visible on listing page.
$this->drupalGet('admin/content/comment');
$this->postComment($this->node, $this->randomMachineName());
$this->drupalGet('admin/content/comment');
$this->assertSession()->pageTextContains(Html::escape($this->node->label()));
$this->node->setUnpublished()->save();
$this->drupalGet('admin/content/comment');
$this->assertSession()->pageTextNotContains(Html::escape($this->node->label()));
}
/**
* Tests comment approval functionality through the node interface.
*/
public function testApprovalNodeInterface(): void {
// Set anonymous comments to require approval.
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => TRUE,
'post comments' => TRUE,
'skip comment approval' => FALSE,
]);
// Ensure that doesn't require contact info.
$this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAYNOT_CONTACT);
// Post anonymous comment without contact info.
$subject = $this->randomMachineName();
$body = $this->randomMachineName();
// Set $contact to true so that it won't check for id and message.
$this->postComment($this->node, $body, $subject, TRUE);
$this->assertSession()->pageTextContains('Your comment has been queued for review by site administrators and will be published after approval.');
// Get unapproved comment id.
$this->drupalLogin($this->adminUser);
$anonymous_comment4 = $this->getUnapprovedComment($subject);
$anonymous_comment4 = Comment::create([
'cid' => $anonymous_comment4,
'subject' => $subject,
'comment_body' => $body,
'entity_id' => $this->node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
]);
$this->drupalLogout();
$this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
// Ensure comments cannot be approved without a valid token.
$this->drupalLogin($this->adminUser);
$this->drupalGet('comment/1/approve');
$this->assertSession()->statusCodeEquals(403);
$this->drupalGet('comment/1/approve', ['query' => ['token' => 'forged']]);
$this->assertSession()->statusCodeEquals(403);
// Approve comment.
$this->drupalGet('comment/1/edit');
$this->assertSession()->checkboxChecked('edit-status-0');
$this->drupalGet('node/' . $this->node->id());
$this->clickLink('Approve');
$this->drupalLogout();
$this->drupalGet('node/' . $this->node->id());
$this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
}
/**
* Tests comment bundle admin.
*/
public function testCommentAdmin(): void {
// Login.
$this->drupalLogin($this->adminUser);
// Browse to comment bundle overview.
$this->drupalGet('admin/structure/comment');
$this->assertSession()->statusCodeEquals(200);
// Make sure titles visible.
$this->assertSession()->pageTextContains('Comment type');
$this->assertSession()->pageTextContains('Description');
// Make sure the description is present.
$this->assertSession()->pageTextContains('Default comment field');
// Manage fields.
$this->clickLink('Manage fields');
$this->assertSession()->statusCodeEquals(200);
// Make sure comment_body field is shown.
$this->assertSession()->pageTextContains('comment_body');
// Rest from here on in is field_ui.
}
/**
* Tests editing a comment as an admin.
*/
public function testEditComment(): void {
// Enable anonymous user comments.
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments',
'post comments',
'skip comment approval',
]);
// Log in as a web user.
$this->drupalLogin($this->webUser);
// Post a comment.
$comment = $this->postComment($this->node, $this->randomMachineName());
$this->drupalLogout();
// Post anonymous comment.
// Ensure that we need email id before posting comment.
$this->setCommentAnonymous(CommentInterface::ANONYMOUS_MUST_CONTACT);
// Post comment with contact info (required).
$author_name = $this->randomMachineName();
$author_mail = $this->randomMachineName() . '@example.com';
$anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), ['name' => $author_name, 'mail' => $author_mail]);
// Log in as an admin user.
$this->drupalLogin($this->adminUser);
// Make sure the comment field is not visible when
// the comment was posted by an authenticated user.
$this->drupalGet('comment/' . $comment->id() . '/edit');
$this->assertSession()->fieldNotExists('edit-mail');
// Make sure the comment field is visible when
// the comment was posted by an anonymous user.
$this->drupalGet('comment/' . $anonymous_comment->id() . '/edit');
$this->assertSession()->fieldValueEquals('edit-mail', $anonymous_comment->getAuthorEmail());
}
/**
* Tests commented translation deletion admin view.
*/
public function testCommentedTranslationDeletion(): void {
\Drupal::service('module_installer')->install([
'language',
'locale',
]);
\Drupal::service('router.builder')->rebuildIfNeeded();
ConfigurableLanguage::createFromLangcode('ur')->save();
// Rebuild the container to update the default language container variable.
$this->rebuildContainer();
// Ensure that doesn't require contact info.
$this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAYNOT_CONTACT);
$this->drupalLogin($this->webUser);
$count_query = \Drupal::entityTypeManager()
->getStorage('comment')
->getQuery()
->accessCheck(FALSE)
->count();
$before_count = $count_query->execute();
// Post 2 anonymous comments without contact info.
$comment1 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$comment2 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$comment1->addTranslation('ur', ['subject' => 'ur ' . $comment1->label()])
->save();
$comment2->addTranslation('ur', ['subject' => 'ur ' . $comment1->label()])
->save();
$this->drupalLogout();
$this->drupalLogin($this->adminUser);
// Delete multiple comments in one operation.
$edit = [
'operation' => 'delete',
"comments[{$comment1->id()}]" => 1,
"comments[{$comment2->id()}]" => 1,
];
$this->drupalGet('admin/content/comment');
$this->submitForm($edit, 'Update');
$this->assertSession()->responseContains(new FormattableMarkup('@label (Original translation) - <em>The following comment translations will be deleted:</em>', ['@label' => $comment1->label()]));
$this->assertSession()->responseContains(new FormattableMarkup('@label (Original translation) - <em>The following comment translations will be deleted:</em>', ['@label' => $comment2->label()]));
$this->assertSession()->pageTextContains('English');
$this->assertSession()->pageTextContains('Urdu');
$this->submitForm([], 'Delete');
$after_count = $count_query->execute();
$this->assertEquals($before_count, $after_count, 'No comment or translation found.');
}
}

View File

@@ -0,0 +1,214 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\CommentInterface;
use Drupal\user\RoleInterface;
/**
* Tests anonymous commenting.
*
* @group comment
*/
class CommentAnonymousTest extends CommentTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Enable anonymous and authenticated user comments.
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments',
'post comments',
'skip comment approval',
]);
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
'access comments',
'post comments',
'skip comment approval',
]);
}
/**
* Tests anonymous comment functionality.
*/
public function testAnonymous(): void {
$this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAYNOT_CONTACT);
// Preview comments (with `skip comment approval` permission).
$edit = [];
$title = 'comment title with skip comment approval';
$body = 'comment body with skip comment approval';
$edit['subject[0][value]'] = $title;
$edit['comment_body[0][value]'] = $body;
$this->drupalGet($this->node->toUrl());
$this->submitForm($edit, 'Preview');
// Cannot use assertRaw here since both title and body are in the form.
$preview = (string) $this->cssSelect('[data-drupal-selector="edit-comment-preview"]')[0]->getHtml();
$this->assertStringContainsString($title, $preview, 'Anonymous user can preview comment title.');
$this->assertStringContainsString($body, $preview, 'Anonymous user can preview comment body.');
// Preview comments (without `skip comment approval` permission).
user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['skip comment approval']);
$edit = [];
$title = 'comment title without skip comment approval';
$body = 'comment body without skip comment approval';
$edit['subject[0][value]'] = $title;
$edit['comment_body[0][value]'] = $body;
$this->drupalGet($this->node->toUrl());
$this->submitForm($edit, 'Preview');
// Cannot use assertRaw here since both title and body are in the form.
$preview = (string) $this->cssSelect('[data-drupal-selector="edit-comment-preview"]')[0]->getHtml();
$this->assertStringContainsString($title, $preview, 'Anonymous user can preview comment title.');
$this->assertStringContainsString($body, $preview, 'Anonymous user can preview comment body.');
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['skip comment approval']);
// Post anonymous comment without contact info.
$anonymous_comment1 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
$this->assertTrue($this->commentExists($anonymous_comment1), 'Anonymous comment without contact info found.');
// Ensure anonymous users cannot post in the name of registered users.
$edit = [
'name' => $this->adminUser->getAccountName(),
'comment_body[0][value]' => $this->randomMachineName(),
];
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('The name you used (' . $this->adminUser->getAccountName() . ') belongs to a registered user.');
// Allow contact info.
$this->drupalLogin($this->adminUser);
$this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAY_CONTACT);
// Attempt to edit anonymous comment.
$this->drupalGet('comment/' . $anonymous_comment1->id() . '/edit');
$edited_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName());
$this->assertTrue($this->commentExists($edited_comment, FALSE), 'Modified reply found.');
$this->drupalLogout();
// Post anonymous comment with contact info (optional).
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
// Check the presence of expected cache tags.
$this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config:field.field.node.article.comment');
$this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config:user.settings');
$anonymous_comment2 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
$this->assertTrue($this->commentExists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.');
// Ensure anonymous users cannot post in the name of registered users.
$edit = [
'name' => $this->adminUser->getAccountName(),
'mail' => $this->randomMachineName() . '@example.com',
'subject[0][value]' => $this->randomMachineName(),
'comment_body[0][value]' => $this->randomMachineName(),
];
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('The name you used (' . $this->adminUser->getAccountName() . ') belongs to a registered user.');
// Require contact info.
$this->setCommentAnonymous(CommentInterface::ANONYMOUS_MUST_CONTACT);
// Try to post comment with contact info (required).
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
$anonymous_comment3 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// Name should have 'Anonymous' for value by default.
$this->assertSession()->pageTextContains('Email field is required.');
$this->assertFalse($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) not found.');
// Post comment with contact info (required).
$author_name = $this->randomMachineName();
$author_mail = $this->randomMachineName() . '@example.com';
$anonymous_comment3 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), ['name' => $author_name, 'mail' => $author_mail]);
$this->assertTrue($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) found.');
// Make sure the user data appears correctly when editing the comment.
$this->drupalLogin($this->adminUser);
$this->drupalGet('comment/' . $anonymous_comment3->id() . '/edit');
$this->assertSession()->responseContains($author_name);
// Check the author field is empty (i.e. anonymous) when editing the comment.
$this->assertSession()->fieldValueEquals('uid', '');
$this->assertSession()->responseContains($author_mail);
// Unpublish comment.
$this->performCommentOperation($anonymous_comment3, 'unpublish');
$this->drupalGet('admin/content/comment/approval');
$this->assertSession()->responseContains('comments[' . $anonymous_comment3->id() . ']');
// Publish comment.
$this->performCommentOperation($anonymous_comment3, 'publish', TRUE);
$this->drupalGet('admin/content/comment');
$this->assertSession()->responseContains('comments[' . $anonymous_comment3->id() . ']');
// Delete comment.
$this->performCommentOperation($anonymous_comment3, 'delete');
$this->drupalGet('admin/content/comment');
$this->assertSession()->responseNotContains('comments[' . $anonymous_comment3->id() . ']');
$this->drupalLogout();
// Comment 3 was deleted.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $anonymous_comment3->id());
$this->assertSession()->statusCodeEquals(403);
// Reset.
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => FALSE,
'post comments' => FALSE,
'skip comment approval' => FALSE,
]);
// Attempt to view comments while disallowed.
// NOTE: if authenticated user has permission to post comments, then a
// "Login or register to post comments" type link may be shown.
$this->drupalGet('node/' . $this->node->id());
// Verify that comments were not displayed.
$this->assertSession()->responseNotMatches('@<h2[^>]*>Comments</h2>@');
$this->assertSession()->linkNotExists('Add new comment', 'Link to add comment was found.');
// Attempt to view node-comment form while disallowed.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$this->assertSession()->statusCodeEquals(403);
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => TRUE,
'post comments' => FALSE,
'skip comment approval' => FALSE,
]);
$this->drupalGet('node/' . $this->node->id());
// Verify that the comment field title is displayed.
$this->assertSession()->responseMatches('@<h2[^>]*>Comments</h2>@');
$this->assertSession()->linkExists('Log in', 1, 'Link to login was found.');
$this->assertSession()->linkExists('register', 1, 'Link to register was found.');
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => FALSE,
'post comments' => TRUE,
'skip comment approval' => TRUE,
]);
$this->drupalGet('node/' . $this->node->id());
// Verify that comments were not displayed.
$this->assertSession()->responseNotMatches('@<h2[^>]*>Comments</h2>@');
$this->assertSession()->fieldValueEquals('subject[0][value]', '');
$this->assertSession()->fieldValueEquals('comment_body[0][value]', '');
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $anonymous_comment2->id());
$this->assertSession()->statusCodeEquals(403);
}
}

View File

@@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\user\RoleInterface;
/**
* Tests comment block functionality.
*
* @group comment
*/
class CommentBlockTest extends CommentTestBase {
/**
* Modules to install.
*
* @var array
*/
protected static $modules = ['block', 'views'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Update admin user to have the 'administer blocks' permission.
$this->adminUser = $this->drupalCreateUser([
'administer content types',
'administer comments',
'skip comment approval',
'post comments',
'access comments',
'access content',
'administer blocks',
]);
}
/**
* Tests the recent comments block.
*/
public function testRecentCommentBlock(): void {
$this->drupalLogin($this->adminUser);
$this->drupalPlaceBlock('views_block:comments_recent-block_1');
// Add some test comments, with and without subjects. Because the 10 newest
// comments should be shown by the block, we create 11 to test that behavior
// below.
$timestamp = \Drupal::time()->getRequestTime();
for ($i = 0; $i < 11; ++$i) {
$subject = ($i % 2) ? $this->randomMachineName() : '';
$comments[$i] = $this->postComment($this->node, $this->randomMachineName(), $subject);
$comments[$i]->created->value = $timestamp--;
$comments[$i]->save();
}
// Test that a user without the 'access comments' permission cannot see the
// block.
$this->drupalLogout();
user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']);
$this->drupalGet('');
$this->assertSession()->pageTextNotContains('Recent comments');
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']);
// Test that a user with the 'access comments' permission can see the
// block.
$this->drupalLogin($this->webUser);
$this->drupalGet('');
$this->assertSession()->pageTextContains('Recent comments');
// Test the only the 10 latest comments are shown and in the proper order.
$this->assertSession()->pageTextNotContains($comments[10]->getSubject());
for ($i = 0; $i < 10; $i++) {
$this->assertSession()->pageTextContains($comments[$i]->getSubject());
if ($i > 1) {
$previous_position = $position;
$position = strpos($this->getSession()->getPage()->getContent(), $comments[$i]->getSubject());
$this->assertGreaterThan($previous_position, $position, sprintf('Comment %d does not appear after comment %d', 10 - $i, 11 - $i));
}
$position = strpos($this->getSession()->getPage()->getContent(), $comments[$i]->getSubject());
}
// Test that links to comments work when comments are across pages.
$this->setCommentsPerPage(1);
for ($i = 0; $i < 10; $i++) {
$this->clickLink($comments[$i]->getSubject());
$this->assertSession()->pageTextContains($comments[$i]->getSubject());
$this->assertSession()->responseContains('<link rel="canonical"');
}
}
}

View File

@@ -0,0 +1,149 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\Core\Language\LanguageInterface;
use Drupal\comment\CommentInterface;
use Drupal\user\RoleInterface;
use Drupal\comment\Entity\Comment;
use Drupal\Tests\Traits\Core\GeneratePermutationsTrait;
/**
* Tests CSS classes on comments.
*
* @group comment
*/
class CommentCSSTest extends CommentTestBase {
use GeneratePermutationsTrait;
/**
* The theme to install as the default for testing.
*
* @var string
*/
protected $defaultTheme = 'starterkit_theme';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Allow anonymous users to see comments.
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments',
'access content',
]);
}
/**
* Tests CSS classes on comments.
*/
public function testCommentClasses(): void {
// Create all permutations for comments, users, and nodes.
$parameters = [
'node_uid' => [0, $this->webUser->id()],
'comment_uid' => [0, $this->webUser->id(), $this->adminUser->id()],
'comment_status' => [CommentInterface::PUBLISHED, CommentInterface::NOT_PUBLISHED],
'user' => ['anonymous', 'authenticated', 'admin'],
];
$permutations = $this->generatePermutations($parameters);
foreach ($permutations as $case) {
// Create a new node.
$node = $this->drupalCreateNode(['type' => 'article', 'uid' => $case['node_uid']]);
// Add a comment.
/** @var \Drupal\comment\CommentInterface $comment */
$comment = Comment::create([
'entity_id' => $node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'uid' => $case['comment_uid'],
'status' => $case['comment_status'],
'subject' => $this->randomMachineName(),
'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'comment_body' => [LanguageInterface::LANGCODE_NOT_SPECIFIED => [$this->randomMachineName()]],
]);
$comment->save();
// Adjust the current/viewing user.
switch ($case['user']) {
case 'anonymous':
if ($this->loggedInUser) {
$this->drupalLogout();
}
$case['user_uid'] = 0;
break;
case 'authenticated':
$this->drupalLogin($this->webUser);
$case['user_uid'] = $this->webUser->id();
break;
case 'admin':
$this->drupalLogin($this->adminUser);
$case['user_uid'] = $this->adminUser->id();
break;
}
// Request the node with the comment.
$this->drupalGet('node/' . $node->id());
$settings = $this->getDrupalSettings();
// Verify the data-history-node-id attribute, which is necessary for the
// by-viewer class and the "new" indicator, see below.
$this->assertSession()->elementsCount('xpath', '//*[@data-history-node-id="' . $node->id() . '"]', 1);
// Verify classes if the comment is visible for the current user.
if ($case['comment_status'] == CommentInterface::PUBLISHED || $case['user'] == 'admin') {
// Verify the by-anonymous class.
$comments = '//*[contains(@class, "comment") and contains(@class, "by-anonymous")]';
if ($case['comment_uid'] == 0) {
$this->assertSession()->elementsCount('xpath', $comments, 1);
}
else {
$this->assertSession()->elementNotExists('xpath', $comments);
}
// Verify the by-node-author class.
$comments = '//*[contains(@class, "comment") and contains(@class, "by-node-author")]';
if ($case['comment_uid'] > 0 && $case['comment_uid'] == $case['node_uid']) {
$this->assertSession()->elementsCount('xpath', $comments, 1);
}
else {
$this->assertSession()->elementNotExists('xpath', $comments);
}
// Verify the data-comment-user-id attribute, which is used by the
// drupal.comment-by-viewer library to add a by-viewer when the current
// user (the viewer) was the author of the comment. We do this in Java-
// Script to prevent breaking the render cache.
$this->assertSession()->elementsCount('xpath', '//*[contains(@class, "comment") and @data-comment-user-id="' . $case['comment_uid'] . '"]', 1);
$this->assertSession()->responseContains($this->getModulePath('comment') . '/js/comment-by-viewer.js');
}
// Verify the unpublished class.
$comments = '//*[contains(@class, "comment") and contains(@class, "unpublished")]';
if ($case['comment_status'] == CommentInterface::NOT_PUBLISHED && $case['user'] == 'admin') {
$this->assertSession()->elementsCount('xpath', $comments, 1);
}
else {
$this->assertSession()->elementNotExists('xpath', $comments);
}
// Verify the data-comment-timestamp attribute, which is used by the
// drupal.comment-new-indicator library to add a "new" indicator to each
// comment that was created or changed after the last time the current
// user read the corresponding node.
if ($case['comment_status'] == CommentInterface::PUBLISHED || $case['user'] == 'admin') {
$this->assertSession()->elementsCount('xpath', '//*[contains(@class, "comment")]/*[@data-comment-timestamp="' . $comment->getChangedTime() . '"]', 1);
$expectedJS = ($case['user'] !== 'anonymous');
$this->assertSame($expectedJS, isset($settings['ajaxPageState']['libraries']) && in_array('comment/drupal.comment-new-indicator', explode(',', $settings['ajaxPageState']['libraries'])), 'drupal.comment-new-indicator library is present.');
}
}
}
}

View File

@@ -0,0 +1,177 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\CommentInterface;
use Drupal\comment\CommentManagerInterface;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\system\Functional\Entity\EntityWithUriCacheTagsTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
// cspell:ignore amphibius Hippopotamidae
/**
* Tests the Comment entity's cache tags.
*
* @group comment
*/
class CommentCacheTagsTest extends EntityWithUriCacheTagsTestBase {
use CommentTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['comment'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* @var \Drupal\entity_test\Entity\EntityTest
*/
protected $entityTestCamelid;
/**
* @var \Drupal\entity_test\Entity\EntityTest
*/
protected $entityTestHippopotamidae;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Give anonymous users permission to view comments, so that we can verify
// the cache tags of cached versions of comment pages.
$user_role = Role::load(RoleInterface::ANONYMOUS_ID);
$user_role->grantPermission('access comments');
$user_role->save();
}
/**
* {@inheritdoc}
*/
protected function createEntity() {
// Create a "bar" bundle for the "entity_test" entity type and create.
$bundle = 'bar';
entity_test_create_bundle($bundle, NULL, 'entity_test');
// Create a comment field on this bundle.
$this->addDefaultCommentField('entity_test', 'bar', 'comment');
// Display comments in a flat list; threaded comments are not render cached.
$field = FieldConfig::loadByName('entity_test', 'bar', 'comment');
$field->setSetting('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT);
$field->save();
// Create a "Camelids" test entity that the comment will be assigned to.
$this->entityTestCamelid = EntityTest::create([
'name' => 'Camelids',
'type' => 'bar',
]);
$this->entityTestCamelid->save();
// Create a "Llama" comment.
$comment = Comment::create([
'subject' => 'Llama',
'comment_body' => [
'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
'format' => 'plain_text',
],
'entity_id' => $this->entityTestCamelid->id(),
'entity_type' => 'entity_test',
'field_name' => 'comment',
'status' => CommentInterface::PUBLISHED,
]);
$comment->save();
return $comment;
}
/**
* Tests that comments invalidate the cache tag of their host entity.
*/
public function testCommentEntity(): void {
$this->verifyPageCache($this->entityTestCamelid->toUrl(), 'MISS');
$this->verifyPageCache($this->entityTestCamelid->toUrl(), 'HIT');
// Create a "Hippopotamus" comment.
$this->entityTestHippopotamidae = EntityTest::create([
'name' => 'Hippopotamus',
'type' => 'bar',
]);
$this->entityTestHippopotamidae->save();
$this->verifyPageCache($this->entityTestHippopotamidae->toUrl(), 'MISS');
$this->verifyPageCache($this->entityTestHippopotamidae->toUrl(), 'HIT');
$hippo_comment = Comment::create([
'subject' => 'Hippopotamus',
'comment_body' => [
'value' => 'The common hippopotamus (Hippopotamus amphibius), or hippo, is a large, mostly herbivorous mammal in sub-Saharan Africa',
'format' => 'plain_text',
],
'entity_id' => $this->entityTestHippopotamidae->id(),
'entity_type' => 'entity_test',
'field_name' => 'comment',
'status' => CommentInterface::PUBLISHED,
]);
$hippo_comment->save();
// Ensure that a new comment only invalidates the commented entity.
$this->verifyPageCache($this->entityTestCamelid->toUrl(), 'HIT');
$this->verifyPageCache($this->entityTestHippopotamidae->toUrl(), 'MISS');
$this->assertSession()->pageTextContains($hippo_comment->getSubject());
// Ensure that updating an existing comment only invalidates the commented
// entity.
$this->entity->save();
$this->verifyPageCache($this->entityTestCamelid->toUrl(), 'MISS');
$this->verifyPageCache($this->entityTestHippopotamidae->toUrl(), 'HIT');
}
/**
* {@inheritdoc}
*/
protected function getAdditionalCacheContextsForEntity(EntityInterface $entity) {
return [];
}
/**
* {@inheritdoc}
*
* Each comment must have a comment body, which always has a text format.
*/
protected function getAdditionalCacheTagsForEntity(EntityInterface $entity) {
/** @var \Drupal\comment\CommentInterface $entity */
return [
'config:filter.format.plain_text',
'user:' . $entity->getOwnerId(),
'user_view',
];
}
/**
* {@inheritdoc}
*/
protected function getDefaultCacheContexts() {
return [
'languages:' . LanguageInterface::TYPE_INTERFACE,
'theme',
'user.permissions',
];
}
}

View File

@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Language\LanguageInterface;
use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment;
use Drupal\user\RoleInterface;
/**
* Tests making comment base fields' displays configurable.
*
* @group comment
*/
class CommentDisplayConfigurableTest extends CommentTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'olivero';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Allow anonymous users to see comments.
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments',
'access content',
]);
}
/**
* Sets base fields to configurable display and check settings are respected.
*/
public function testDisplayConfigurable(): void {
// Add a comment.
$nid = $this->node->id();
/** @var \Drupal\comment\CommentInterface $comment */
$comment = Comment::create([
'entity_id' => $nid,
'entity_type' => 'node',
'field_name' => 'comment',
'uid' => $this->webUser->id(),
'status' => CommentInterface::PUBLISHED,
'subject' => $this->randomMachineName(),
'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'comment_body' => [LanguageInterface::LANGCODE_NOT_SPECIFIED => [$this->randomMachineName()]],
]);
$comment->save();
$assert = $this->assertSession();
// Check the comment author with Drupal default non-configurable display.
$this->drupalGet('node/' . $nid);
$assert->elementExists('css', 'p.comment__author span');
// Enable module to make base fields' displays configurable.
\Drupal::service('module_installer')->install(['comment_display_configurable_test']);
// Configure display.
$display = EntityViewDisplay::load('comment.comment.default');
$display->setComponent('uid',
[
'type' => 'entity_reference_label',
'label' => 'above',
'settings' => ['link' => FALSE],
])
->save();
// Recheck the comment author with configurable display.
$this->drupalGet('node/' . $nid);
$assert->elementExists('css', '.field--name-uid .field__item');
// Remove from display.
$display->removeComponent('uid')
->removeComponent('created')
->save();
$this->drupalGet('node/' . $this->node->id());
$assert->elementNotExists('css', '.field--name-uid .field__item');
}
}

View File

@@ -0,0 +1,126 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\Entity\Comment;
use Drupal\Tests\field\Traits\EntityReferenceFieldCreationTrait;
/**
* Tests that comments behave correctly when added as entity references.
*
* @group comment
*/
class CommentEntityReferenceTest extends CommentTestBase {
use EntityReferenceFieldCreationTrait;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* A second test node containing references to comments.
*
* @var \Drupal\node\NodeInterface
*/
protected $node2;
/**
* A comment linked to a node.
*
* @var \Drupal\comment\CommentInterface
*/
protected $comment;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->createEntityReferenceField(
'node',
'article',
'entity_reference_comment',
'Entity Reference Comment',
'comment',
'default',
['target_bundles' => ['comment']]
);
\Drupal::service('entity_display.repository')
->getFormDisplay('node', 'article')
->setComponent('entity_reference_comment', ['type' => 'options_select'])
->save();
\Drupal::service('entity_display.repository')
->getViewDisplay('node', 'article')
->setComponent('entity_reference_comment', ['type' => 'entity_reference_label'])
->save();
$administratorUser = $this->drupalCreateUser([
'skip comment approval',
'post comments',
'access comments',
'access content',
'administer nodes',
'administer comments',
'bypass node access',
]);
$this->drupalLogin($administratorUser);
$this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
$this->comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
$this->assertInstanceOf(Comment::class, $this->comment);
$this->node2 = $this->drupalCreateNode([
'title' => $this->randomMachineName(),
'type' => 'article',
]);
}
/**
* Tests that comments are correctly saved as entity references.
*/
public function testCommentAsEntityReference(): void {
// Load the node and save it.
$edit = [
'entity_reference_comment' => $this->comment->id(),
];
$this->drupalGet('node/' . $this->node2->id() . '/edit');
$this->submitForm($edit, 'Save');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('has been updated');
// Make sure the comment is linked.
$this->assertSession()->pageTextContains($this->comment->label());
}
/**
* Tests that comments of unpublished are not shown.
*/
public function testCommentOfUnpublishedNodeBypassAccess(): void {
// Unpublish the node that has the comment.
$this->node->setUnpublished()->save();
// When the user has 'bypass node access' permission, they can still set it.
$edit = [
'entity_reference_comment' => $this->comment->id(),
];
$this->drupalGet('node/' . $this->node2->id() . '/edit');
$this->submitForm($edit, 'Save');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('has been updated');
// Comment is seen as administrator user.
$this->assertSession()->pageTextContains($this->comment->label());
// But not as anonymous.
$this->drupalLogout();
$this->drupalGet('node/' . $this->node2->id());
$this->assertSession()->pageTextContains($this->node2->label());
$this->assertSession()->pageTextNotContains($this->comment->label());
}
}

View File

@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\Entity\CommentType;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
/**
* Tests comments with other entities.
*
* @group comment
*/
class CommentEntityTest extends CommentTestBase {
use TaxonomyTestTrait;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = [
'block',
'comment',
'node',
'history',
'field_ui',
'datetime',
'taxonomy',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
protected $vocab;
protected $commentType;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->vocab = $this->createVocabulary();
$this->commentType = CommentType::create([
'id' => 'taxonomy_comment',
'label' => 'Taxonomy comment',
'description' => '',
'target_entity_type_id' => 'taxonomy_term',
]);
$this->commentType->save();
$this->addDefaultCommentField(
'taxonomy_term',
$this->vocab->id(),
'field_comment',
CommentItemInterface::OPEN,
$this->commentType->id()
);
}
/**
* Tests CSS classes on comments.
*/
public function testEntityChanges(): void {
$this->drupalLogin($this->webUser);
// Create a new node.
$term = $this->createTerm($this->vocab, ['uid' => $this->webUser->id()]);
// Add a comment.
/** @var \Drupal\comment\CommentInterface $comment */
$comment = Comment::create([
'entity_id' => $term->id(),
'entity_type' => 'taxonomy_term',
'field_name' => 'field_comment',
'uid' => $this->webUser->id(),
'status' => CommentInterface::PUBLISHED,
'subject' => $this->randomMachineName(),
'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'comment_body' => [LanguageInterface::LANGCODE_NOT_SPECIFIED => [$this->randomMachineName()]],
]);
$comment->save();
// Request the node with the comment.
$this->drupalGet('taxonomy/term/' . $term->id());
$settings = $this->getDrupalSettings();
$this->assertFalse(isset($settings['ajaxPageState']['libraries']) && in_array('comment/drupal.comment-new-indicator', explode(',', $settings['ajaxPageState']['libraries'])), 'drupal.comment-new-indicator library is present.');
$this->assertFalse(isset($settings['history']['lastReadTimestamps']) && in_array($term->id(), array_keys($settings['history']['lastReadTimestamps'])), 'history.lastReadTimestamps is present.');
}
}

View File

@@ -0,0 +1,201 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\comment\Entity\CommentType;
/**
* Tests fields on comments.
*
* @group comment
*/
class CommentFieldsTest extends CommentTestBase {
/**
* Install the field UI.
*
* @var array
*/
protected static $modules = ['field_ui'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests that the default 'comment_body' field is correctly added.
*/
public function testCommentDefaultFields(): void {
// Do not make assumptions on default node types created by the test
// installation profile, and create our own.
$this->drupalCreateContentType(['type' => 'test_node_type']);
$this->addDefaultCommentField('node', 'test_node_type');
// Check that the 'comment_body' field is present on the comment bundle.
$field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
$this->assertNotEmpty($field, 'The comment_body field is added when a comment bundle is created');
$field->delete();
// Check that the 'comment_body' field is not deleted since it is persisted
// even if it has no fields.
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
$this->assertInstanceOf(FieldStorageConfig::class, $field_storage);
// Create a new content type.
$type_name = 'test_node_type_2';
$this->drupalCreateContentType(['type' => $type_name]);
$this->addDefaultCommentField('node', $type_name);
// Check that the 'comment_body' field exists and has an instance on the
// new comment bundle.
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
$this->assertInstanceOf(FieldStorageConfig::class, $field_storage);
$field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
$this->assertTrue(isset($field), "The comment_body field is present for comments on type $type_name");
// Test adding a field that defaults to CommentItemInterface::CLOSED.
$this->addDefaultCommentField('node', 'test_node_type', 'who_likes_ponies', CommentItemInterface::CLOSED, 'who_likes_ponies');
$field = FieldConfig::load('node.test_node_type.who_likes_ponies');
$this->assertEquals(CommentItemInterface::CLOSED, $field->getDefaultValueLiteral()[0]['status']);
}
/**
* Tests that you can remove a comment field.
*/
public function testCommentFieldDelete(): void {
$this->drupalCreateContentType(['type' => 'test_node_type']);
$this->addDefaultCommentField('node', 'test_node_type');
// We want to test the handling of removing the primary comment field, so we
// ensure there is at least one other comment field attached to a node type
// so that comment_entity_load() runs for nodes.
$this->addDefaultCommentField('node', 'test_node_type', 'comment2');
// Create a sample node.
$node = $this->drupalCreateNode([
'title' => 'Baloney',
'type' => 'test_node_type',
]);
$this->drupalLogin($this->webUser);
$this->drupalGet('node/' . $node->nid->value);
$elements = $this->cssSelect('.comment-form');
$this->assertCount(2, $elements, 'There are two comment fields on the node.');
// Delete the first comment field.
FieldStorageConfig::loadByName('node', 'comment')->delete();
$this->drupalGet('node/' . $node->nid->value);
$elements = $this->cssSelect('.comment-form');
$this->assertCount(1, $elements, 'There is one comment field on the node.');
}
/**
* Tests link building with non-default comment field names.
*/
public function testCommentFieldLinksNonDefaultName(): void {
$this->drupalCreateContentType(['type' => 'test_node_type']);
$this->addDefaultCommentField('node', 'test_node_type', 'comment2');
$web_user2 = $this->drupalCreateUser([
'access comments',
'post comments',
'create article content',
'edit own comments',
'skip comment approval',
'access content',
]);
// Create a sample node.
$node = $this->drupalCreateNode([
'title' => 'Baloney',
'type' => 'test_node_type',
]);
// Go to the node first so that web_user2 see new comments.
$this->drupalLogin($web_user2);
$this->drupalGet($node->toUrl());
$this->drupalLogout();
// Test that buildCommentedEntityLinks() does not break when the 'comment'
// field does not exist. Requires at least one comment.
$this->drupalLogin($this->webUser);
$this->postComment($node, 'Here is a comment', '', NULL, 'comment2');
$this->drupalLogout();
$this->drupalLogin($web_user2);
// We want to check the attached drupalSettings of
// \Drupal\comment\CommentLinkBuilder::buildCommentedEntityLinks. Therefore
// we need a node listing, let's use views for that.
$this->container->get('module_installer')->install(['views'], TRUE);
$this->drupalGet('node');
$link_info = $this->getDrupalSettings()['comment']['newCommentsLinks']['node']['comment2']['2'];
$this->assertSame(1, $link_info['new_comment_count']);
$this->assertSame($node->toUrl('canonical', ['fragment' => 'new'])->toString(), $link_info['first_new_comment_link']);
}
/**
* Tests creating a comment field through the interface.
*/
public function testCommentFieldCreate(): void {
// Create user who can administer user fields.
$user = $this->drupalCreateUser([
'administer user fields',
]);
$this->drupalLogin($user);
// Create comment field in account settings.
$edit = [
'new_storage_type' => 'comment',
];
$this->drupalGet('admin/config/people/accounts/fields/add-field');
$this->submitForm($edit, 'Continue');
$edit = [
'label' => 'User comment',
'field_name' => 'user_comment',
];
$this->submitForm($edit, 'Continue');
// Try to save the comment field without selecting a comment type.
$edit = [];
$this->submitForm($edit, 'Update settings');
// We should get an error message.
$this->assertSession()->pageTextContains('The submitted value in the Comment type element is not allowed.');
// Create a comment type for users.
$bundle = CommentType::create([
'id' => 'user_comment_type',
'label' => 'user_comment_type',
'description' => '',
'target_entity_type_id' => 'user',
]);
$bundle->save();
// Select a comment type and try to save again.
$edit = [
'field_storage[subform][settings][comment_type]' => 'user_comment_type',
];
$this->drupalGet('admin/config/people/accounts/add-field/user/field_user_comment');
$this->submitForm($edit, 'Update settings');
// We shouldn't get an error message.
$this->assertSession()->pageTextNotContains('The submitted value in the Comment type element is not allowed.');
// Try to save the comment field with "Comments per page"
// setting value as zero.
$edit = [
'settings[per_page]' => 0,
];
$this->drupalGet('admin/config/people/accounts/add-field/user/field_user_comment');
$this->submitForm($edit, 'Save settings');
$this->assertSession()->statusMessageContains('Saved User comment configuration.', 'status');
}
}

View File

@@ -0,0 +1,359 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\Core\Url;
use Drupal\comment\CommentManagerInterface;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Entity\Comment;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\field\Entity\FieldConfig;
use Drupal\user\RoleInterface;
use Drupal\filter\Entity\FilterFormat;
/**
* Tests comment user interfaces.
*
* @group comment
*/
class CommentInterfaceTest extends CommentTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Set up comments to have subject and preview disabled.
*/
protected function setUp(): void {
parent::setUp();
// Make sure that comment field title is not displayed when there's no
// comments posted.
$this->drupalGet($this->node->toUrl());
$this->assertSession()->responseNotMatches('@<h2[^>]*>Comments</h2>@');
// Set comments to have subject and preview disabled.
$this->setCommentPreview(DRUPAL_DISABLED);
$this->setCommentForm(TRUE);
$this->setCommentSubject(FALSE);
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
}
/**
* Tests the comment interface.
*/
public function testCommentInterface(): void {
// Post comment #1 without subject or preview.
$this->drupalLogin($this->webUser);
$comment_text = $this->randomMachineName();
$comment = $this->postComment($this->node, $comment_text);
$this->assertTrue($this->commentExists($comment), 'Comment found.');
// Test that using an invalid entity-type does not raise an error.
$this->drupalGet('comment/reply/yeah-this-is-not-an-entity-type/' . $this->node->id() . '/comment/' . $comment->id());
$this->assertSession()->statusCodeEquals(404);
// Test the comment field title is displayed when there's comments.
$this->drupalGet($this->node->toUrl());
$this->assertSession()->responseMatches('@<h2[^>]*>Comments</h2>@');
// Set comments to have subject and preview to required.
$this->drupalLogout();
$this->setCommentSubject(TRUE);
$this->setCommentPreview(DRUPAL_REQUIRED);
// Create comment #2 that allows subject and requires preview.
$this->drupalLogin($this->webUser);
$subject_text = $this->randomMachineName();
$comment_text = $this->randomMachineName();
$comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
$this->assertTrue($this->commentExists($comment), 'Comment found.');
// Comment as anonymous with preview required.
$this->drupalLogout();
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access content', 'access comments', 'post comments', 'skip comment approval']);
$anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$this->assertTrue($this->commentExists($anonymous_comment), 'Comment found.');
$anonymous_comment->delete();
// Check comment display.
$this->drupalLogin($this->webUser);
$this->drupalGet('node/' . $this->node->id());
$this->assertSession()->pageTextContains($subject_text);
$this->assertSession()->pageTextContains($comment_text);
$this->assertSession()->elementExists('xpath', '//footer/a[contains(@href,"' . base_path() . 'comment/' . $comment->id() . '#comment-' . $comment->id() . '") and text()="Permalink"]');
// Set comments to have subject and preview to optional.
$this->drupalLogout();
$this->drupalLogin($this->adminUser);
$this->setCommentSubject(TRUE);
$this->setCommentPreview(DRUPAL_OPTIONAL);
$this->drupalGet('comment/' . $comment->id() . '/edit');
$this->assertSession()->titleEquals('Edit comment ' . $comment->getSubject() . ' | Drupal');
// Test changing the comment author to "Anonymous".
$comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['uid' => '']);
$this->assertSame('Anonymous', $comment->getAuthorName());
$this->assertEquals(0, $comment->getOwnerId());
// Test changing the comment author to an unverified user.
$random_name = $this->randomMachineName();
$this->drupalGet('comment/' . $comment->id() . '/edit');
$comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['name' => $random_name]);
$this->drupalGet('node/' . $this->node->id());
$this->assertSession()->pageTextContains($random_name . ' (not verified)');
// Test changing the comment author to a verified user.
$this->drupalGet('comment/' . $comment->id() . '/edit');
$comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['uid' => $this->webUser->getAccountName() . ' (' . $this->webUser->id() . ')']);
$this->assertSame($this->webUser->getAccountName(), $comment->getAuthorName());
$this->assertSame($this->webUser->id(), $comment->getOwnerId());
$this->drupalLogout();
// Reply to comment #2 creating comment #3 with optional preview and no
// subject though field enabled.
$this->drupalLogin($this->webUser);
// Deliberately use the wrong URL to test
// \Drupal\comment\Controller\CommentController::redirectNode().
$this->drupalGet('comment/' . $this->node->id() . '/reply');
// Verify we were correctly redirected.
$this->assertSession()->addressEquals(Url::fromRoute('comment.reply', ['entity_type' => 'node', 'entity' => $this->node->id(), 'field_name' => 'comment']));
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
$this->assertSession()->pageTextContains($subject_text);
$this->assertSession()->pageTextContains($comment_text);
$reply = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
$reply_loaded = Comment::load($reply->id());
$this->assertTrue($this->commentExists($reply, TRUE), 'Reply found.');
$this->assertEquals($comment->id(), $reply_loaded->getParentComment()->id(), 'Pid of a reply to a comment is set correctly.');
// Check the thread of reply grows correctly.
$this->assertEquals(rtrim($comment->getThread(), '/') . '.00/', $reply_loaded->getThread());
// Second reply to comment #2 creating comment #4.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
$this->assertSession()->pageTextContains($comment->getSubject());
$this->assertSession()->pageTextContains($comment->comment_body->value);
$reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$reply_loaded = Comment::load($reply->id());
$this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
// Check the thread of second reply grows correctly.
$this->assertEquals(rtrim($comment->getThread(), '/') . '.01/', $reply_loaded->getThread());
// Reply to comment #4 creating comment #5.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id());
$this->assertSession()->pageTextContains($reply_loaded->getSubject());
$this->assertSession()->pageTextContains($reply_loaded->comment_body->value);
$reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$reply_loaded = Comment::load($reply->id());
$this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
// Check the thread of reply to second reply grows correctly.
$this->assertEquals(rtrim($comment->getThread(), '/') . '.01.00/', $reply_loaded->getThread());
// Edit reply.
$this->drupalGet('comment/' . $reply->id() . '/edit');
$reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$this->assertTrue($this->commentExists($reply, TRUE), 'Modified reply found.');
// Confirm a new comment is posted to the correct page.
$this->setCommentsPerPage(2);
$comment_new_page = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$this->assertTrue($this->commentExists($comment_new_page), 'Page one exists. %s');
$this->drupalGet('node/' . $this->node->id(), ['query' => ['page' => 2]]);
$this->assertTrue($this->commentExists($reply, TRUE), 'Page two exists. %s');
$this->setCommentsPerPage(50);
// Attempt to reply to an unpublished comment.
$reply_loaded->setUnpublished();
$reply_loaded->save();
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id());
$this->assertSession()->statusCodeEquals(403);
// Attempt to post to node with comments disabled.
$this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::HIDDEN]]]);
$this->assertNotNull($this->node, 'Article node created.');
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$this->assertSession()->statusCodeEquals(403);
$this->assertSession()->fieldNotExists('edit-comment');
// Attempt to post to node with read-only comments.
$this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::CLOSED]]]);
$this->assertNotNull($this->node, 'Article node created.');
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$this->assertSession()->statusCodeEquals(403);
$this->assertSession()->fieldNotExists('edit-comment');
// Attempt to post to node with comments enabled (check field names etc).
$this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::OPEN]]]);
$this->assertNotNull($this->node, 'Article node created.');
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$this->assertSession()->pageTextNotContains('This discussion is closed');
// Ensure that the comment body field exists.
$this->assertSession()->fieldExists('edit-comment-body-0-value');
// Delete comment and make sure that reply is also removed.
$this->drupalLogout();
$this->drupalLogin($this->adminUser);
$this->deleteComment($comment);
$this->deleteComment($comment_new_page);
$this->drupalGet('node/' . $this->node->id());
$this->assertFalse($this->commentExists($comment), 'Comment not found.');
$this->assertFalse($this->commentExists($reply, TRUE), 'Reply not found.');
// Enabled comment form on node page.
$this->setCommentForm(TRUE);
// Submit comment through node form.
$this->drupalLogin($this->webUser);
$this->drupalGet('node/' . $this->node->id());
$form_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$this->assertTrue($this->commentExists($form_comment), 'Form comment found.');
// Disable comment form on node page.
$this->drupalLogout();
$this->setCommentForm(FALSE);
}
/**
* Tests that the subject is automatically filled if disabled or left blank.
*
* When the subject field is blank or disabled, the first 29 characters of the
* comment body are used for the subject. If this would break within a word,
* then the break is put at the previous word boundary instead.
*/
public function testAutoFilledSubject(): void {
$this->drupalLogin($this->webUser);
$this->drupalGet('node/' . $this->node->id());
// Break when there is a word boundary before 29 characters.
$body_text = 'A quick brown fox jumped over the lazy dog';
$comment1 = $this->postComment(NULL, $body_text, '', TRUE);
$this->assertTrue($this->commentExists($comment1), 'Form comment found.');
$this->assertEquals('A quick brown fox jumped…', $comment1->getSubject());
// Break at 29 characters where there's no boundary before that.
$body_text2 = 'AQuickBrownFoxJumpedOverTheLazyDog';
$comment2 = $this->postComment(NULL, $body_text2, '', TRUE);
$this->assertEquals('AQuickBrownFoxJumpedOverTheL…', $comment2->getSubject());
// Make the body field non required.
$comment_body_field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
$comment_body_field->setRequired(FALSE)->save();
// Try to post a comment without any value in body and subject fields.
$this->drupalGet('node/' . $this->node->id());
// Ensure that there are no PHP errors or warnings when automatically
// generating the subject. This occurs when the comment body is empty.
$comment2 = $this->postComment(NULL, '', '', TRUE);
$this->assertEquals('(No subject)', $comment2->getSubject());
}
/**
* Tests that automatic subject is correctly created from HTML comment text.
*
* This is the same test as in CommentInterfaceTest::testAutoFilledSubject()
* with the additional check that HTML is stripped appropriately prior to
* character-counting.
*/
public function testAutoFilledHtmlSubject(): void {
// Set up two default (i.e. filtered HTML) input formats, because then we
// can select one of them. Then create a user that can use these formats,
// log the user in, and then GET the node page on which to test the
// comments.
$filtered_html_format = FilterFormat::create([
'format' => 'filtered_html',
'name' => 'Filtered HTML',
]);
$filtered_html_format->save();
$full_html_format = FilterFormat::create([
'format' => 'full_html',
'name' => 'Full HTML',
]);
$full_html_format->save();
$html_user = $this->drupalCreateUser([
'access comments',
'post comments',
'edit own comments',
'skip comment approval',
'access content',
$filtered_html_format->getPermissionName(),
$full_html_format->getPermissionName(),
]);
$this->drupalLogin($html_user);
$this->drupalGet('node/' . $this->node->id());
// HTML should not be included in the character count.
$body_text1 = '<span></span><strong> </strong><span> </span><strong></strong>Hello World<br />';
$edit1 = [
'comment_body[0][value]' => $body_text1,
'comment_body[0][format]' => 'filtered_html',
];
$this->submitForm($edit1, 'Save');
$this->assertEquals('Hello World', Comment::load(1)->getSubject());
// If there's nothing other than HTML, the subject should be '(No subject)'.
$body_text2 = '<span></span><strong> </strong><span> </span><strong></strong> <br />';
$edit2 = [
'comment_body[0][value]' => $body_text2,
'comment_body[0][format]' => 'filtered_html',
];
$this->submitForm($edit2, 'Save');
$this->assertEquals('(No subject)', Comment::load(2)->getSubject());
}
/**
* Tests the comment formatter configured with a custom comment view mode.
*/
public function testViewMode(): void {
$this->drupalLogin($this->webUser);
$this->drupalGet($this->node->toUrl());
$comment_text = $this->randomMachineName();
// Post a comment.
$this->postComment($this->node, $comment_text);
// Comment displayed in 'default' display mode found and has body text.
$comment_element = $this->cssSelect('#comment-1');
$this->assertNotEmpty($comment_element);
$this->assertSession()->responseContains('<p>' . $comment_text . '</p>');
// Create a new comment entity view mode.
$mode = $this->randomMachineName();
EntityViewMode::create([
'targetEntityType' => 'comment',
'id' => "comment.$mode",
'label' => 'Comment test',
])->save();
// Create the corresponding entity view display for article node-type. Note
// that this new view display mode doesn't contain the comment body.
EntityViewDisplay::create([
'targetEntityType' => 'comment',
'bundle' => 'comment',
'mode' => $mode,
])->setStatus(TRUE)->save();
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $node_display */
$node_display = EntityViewDisplay::load('node.article.default');
$formatter = $node_display->getComponent('comment');
// Change the node comment field formatter to use $mode mode instead of
// 'default' mode.
$formatter['settings']['view_mode'] = $mode;
$node_display
->setComponent('comment', $formatter)
->save();
// Reloading the node page to show the same node with its same comment but
// with a different display mode.
$this->drupalGet($this->node->toUrl());
// The comment should exist but without the body text because we used $mode
// mode this time.
$comment_element = $this->cssSelect('#comment-1');
$this->assertNotEmpty($comment_element);
$this->assertSession()->responseNotContains('<p>' . $comment_text . '</p>');
}
}

View File

@@ -0,0 +1,167 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
/**
* Tests for comment language.
*
* @group comment
*/
class CommentLanguageTest extends BrowserTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
* We also use the language_test module here to be able to turn on content
* language negotiation. Drupal core does not provide a way in itself to do
* that.
*
* @var array
*/
protected static $modules = [
'node',
'language',
'language_test',
'comment_test',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
// Create and log in user.
$admin_user = $this->drupalCreateUser([
'administer site configuration',
'administer languages',
'access administration pages',
'administer content types',
'administer comments',
'create article content',
'access comments',
'post comments',
'skip comment approval',
]);
$this->drupalLogin($admin_user);
// Add language.
$edit = ['predefined_langcode' => 'fr'];
$this->drupalGet('admin/config/regional/language/add');
$this->submitForm($edit, 'Add language');
// Set "Article" content type to use multilingual support.
$edit = ['language_configuration[language_alterable]' => TRUE];
$this->drupalGet('admin/structure/types/manage/article');
$this->submitForm($edit, 'Save');
// Enable content language negotiation UI.
\Drupal::state()->set('language_test.content_language_type', TRUE);
// Set interface language detection to user and content language detection
// to URL. Disable inheritance from interface language to ensure content
// language will fall back to the default language if no URL language can be
// detected.
$edit = [
'language_interface[enabled][language-user]' => TRUE,
'language_content[enabled][language-url]' => TRUE,
'language_content[enabled][language-interface]' => FALSE,
];
$this->drupalGet('admin/config/regional/language/detection');
$this->submitForm($edit, 'Save settings');
// Change user language preference, this way interface language is always
// French no matter what path prefix the URLs have.
$edit = ['preferred_langcode' => 'fr'];
$this->drupalGet("user/" . $admin_user->id() . "/edit");
$this->submitForm($edit, 'Save');
// Create comment field on article.
$this->addDefaultCommentField('node', 'article');
// Make comment body translatable.
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
$field_storage->setTranslatable(TRUE);
$field_storage->save();
$this->assertTrue($field_storage->isTranslatable(), 'Comment body is translatable.');
}
/**
* Tests that comment language is properly set.
*/
public function testCommentLanguage(): void {
// Create two nodes, one for english and one for french, and comment each
// node using both english and french as content language by changing URL
// language prefixes. Meanwhile interface language is always French, which
// is the user language preference. This way we can ensure that node
// language and interface language do not influence comment language, as
// only content language has to.
foreach ($this->container->get('language_manager')->getLanguages() as $node_langcode => $node_language) {
// Create "Article" content.
$title = $this->randomMachineName();
$edit = [
'title[0][value]' => $title,
'body[0][value]' => $this->randomMachineName(),
'langcode[0][value]' => $node_langcode,
'comment[0][status]' => CommentItemInterface::OPEN,
];
$this->drupalGet("node/add/article");
$this->submitForm($edit, 'Save');
$node = $this->drupalGetNodeByTitle($title);
$prefixes = $this->config('language.negotiation')->get('url.prefixes');
foreach ($this->container->get('language_manager')->getLanguages() as $langcode => $language) {
// Post a comment with content language $langcode.
$prefix = empty($prefixes[$langcode]) ? '' : $prefixes[$langcode] . '/';
$comment_values[$node_langcode][$langcode] = $this->randomMachineName();
$edit = [
'subject[0][value]' => $this->randomMachineName(),
'comment_body[0][value]' => $comment_values[$node_langcode][$langcode],
];
$this->drupalGet($prefix . 'node/' . $node->id());
$this->submitForm($edit, 'Preview');
$this->submitForm($edit, 'Save');
// Check that comment language matches the current content language.
$cids = \Drupal::entityQuery('comment')
->accessCheck(FALSE)
->condition('entity_id', $node->id())
->condition('entity_type', 'node')
->condition('field_name', 'comment')
->sort('cid', 'DESC')
->range(0, 1)
->execute();
$comment = Comment::load(reset($cids));
$this->assertEquals($langcode, $comment->langcode->value, "The comment posted with content language $langcode and belonging to the node with language $node_langcode has language {$comment->langcode->value}");
$this->assertEquals($comment_values[$node_langcode][$langcode], $comment->comment_body->value, 'Comment body correctly stored.');
}
}
// Check that comment bodies appear in the administration UI.
$this->drupalGet('admin/content/comment');
foreach ($comment_values as $node_values) {
foreach ($node_values as $value) {
$this->assertSession()->responseContains($value);
}
}
}
}

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
/**
* Tests comment links altering.
*
* @group comment
*/
class CommentLinksAlterTest extends CommentTestBase {
protected static $modules = ['comment_test'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Enable comment_test.module's hook_comment_links_alter() implementation.
$this->container->get('state')->set('comment_test_links_alter_enabled', TRUE);
}
/**
* Tests comment links altering.
*/
public function testCommentLinksAlter(): void {
$this->drupalLogin($this->webUser);
$comment_text = $this->randomMachineName();
$subject = $this->randomMachineName();
$this->postComment($this->node, $comment_text, $subject);
$this->drupalGet('node/' . $this->node->id());
$this->assertSession()->linkExists('Report');
}
}

View File

@@ -0,0 +1,159 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\comment\CommentInterface;
use Drupal\comment\CommentManagerInterface;
use Drupal\user\RoleInterface;
use Drupal\comment\Entity\Comment;
/**
* Basic comment links tests to ensure markup present.
*
* @group comment
*/
class CommentLinksTest extends CommentTestBase {
/**
* Comment being tested.
*
* @var \Drupal\comment\CommentInterface
*/
protected $comment;
/**
* Seen comments, array of comment IDs.
*
* @var array
*/
protected $seen = [];
/**
* Use the main node listing to test rendering on teasers.
*
* @var array
*
* @todo Remove this dependency.
*/
protected static $modules = ['views'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests that comment links are output and can be hidden.
*/
public function testCommentLinks(): void {
// Remove additional user permissions from $this->webUser added by setUp(),
// since this test is limited to anonymous and authenticated roles only.
$roles = $this->webUser->getRoles();
\Drupal::entityTypeManager()->getStorage('user_role')->load(reset($roles))->delete();
// Create a comment via CRUD API functionality, since
// $this->postComment() relies on actual user permissions.
$comment = Comment::create([
'cid' => NULL,
'entity_id' => $this->node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'pid' => 0,
'uid' => 0,
'status' => CommentInterface::PUBLISHED,
'subject' => $this->randomMachineName(),
'hostname' => '127.0.0.1',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'comment_body' => [['value' => $this->randomMachineName()]],
]);
$comment->save();
$this->comment = $comment;
// Tests that reply link is not visible when threading is disabled.
$this->drupalLogin($this->webUser);
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
$this->drupalGet('node/' . $this->node->id());
$this->assertSession()->linkNotExists('Reply');
// Tests that reply link is visible when threading is enabled.
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->drupalGet('node/' . $this->node->id());
$this->assertSession()->linkExists('Reply');
// Change comment settings.
$this->setCommentSettings('form_location', CommentItemInterface::FORM_BELOW, 'Set comment form location');
$this->node->comment = CommentItemInterface::OPEN;
$this->node->save();
// Change user permissions.
$perms = [
'access comments' => 1,
'post comments' => 1,
'skip comment approval' => 1,
'edit own comments' => 1,
];
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, $perms);
$nid = $this->node->id();
// Assert basic link is output, actual functionality is unit-tested in
// \Drupal\comment\Tests\CommentLinkBuilderTest.
foreach (['node', "node/$nid"] as $path) {
$this->drupalGet($path);
// In teaser view, a link containing the comment count is always
// expected.
if ($path == 'node') {
$this->assertSession()->linkExists('1 comment');
}
$this->assertSession()->linkExists('Add new comment');
}
$display_repository = $this->container->get('entity_display.repository');
// Change weight to make links go before comment body.
$display_repository->getViewDisplay('comment', 'comment')
->setComponent('links', ['weight' => -100])
->save();
$this->drupalGet($this->node->toUrl());
$element = $this->cssSelect('article.js-comment > div');
// Get last child element.
$element = end($element);
$this->assertSame('div', $element->getTagName(), 'Last element is comment body.');
// Change weight to make links go after comment body.
$display_repository->getViewDisplay('comment', 'comment')
->setComponent('links', ['weight' => 100])
->save();
$this->drupalGet($this->node->toUrl());
$element = $this->cssSelect('article.js-comment > div');
// Get last child element.
$element = end($element);
$this->assertNotEmpty($element->find('css', 'ul.links'), 'Last element is comment links.');
// Make sure we can hide node links.
$display_repository->getViewDisplay('node', $this->node->bundle())
->removeComponent('links')
->save();
$this->drupalGet($this->node->toUrl());
$this->assertSession()->linkNotExists('1 comment');
$this->assertSession()->linkNotExists('Add new comment');
// Visit the full node, make sure there are links for the comment.
$this->drupalGet('node/' . $this->node->id());
$this->assertSession()->pageTextContains($comment->getSubject());
$this->assertSession()->linkExists('Reply');
// Make sure we can hide comment links.
$display_repository->getViewDisplay('comment', 'comment')
->removeComponent('links')
->save();
$this->drupalGet('node/' . $this->node->id());
$this->assertSession()->pageTextContains($comment->getSubject());
$this->assertSession()->linkNotExists('Reply');
}
}

View File

@@ -0,0 +1,144 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Language\LanguageInterface;
use Drupal\comment\CommentInterface;
use Drupal\Core\Url;
use Drupal\comment\Entity\Comment;
/**
* Tests the 'new' indicator posted on comments.
*
* @group comment
*/
class CommentNewIndicatorTest extends CommentTestBase {
/**
* Use the main node listing to test rendering on teasers.
*
* @var array
*
* @todo Remove this dependency.
*/
protected static $modules = ['views'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Get node "x new comments" metadata from the server for the current user.
*
* @param array $node_ids
* An array of node IDs.
*
* @return \Psr\Http\Message\ResponseInterface
* The HTTP response.
*/
protected function renderNewCommentsNodeLinks(array $node_ids) {
$client = $this->getHttpClient();
$url = Url::fromRoute('comment.new_comments_node_links');
return $client->request('POST', $this->buildUrl($url), [
'cookies' => $this->getSessionCookies(),
'http_errors' => FALSE,
'form_params' => [
'node_ids' => $node_ids,
'field_name' => 'comment',
],
]);
}
/**
* Tests new comment marker.
*/
public function testCommentNewCommentsIndicator(): void {
// Test if the right links are displayed when no comment is present for the
// node.
$this->drupalLogin($this->adminUser);
$this->drupalGet('node');
$this->assertSession()->linkNotExists('0 comments');
$this->assertSession()->linkExists('Read more');
// Verify the data-history-node-last-comment-timestamp attribute, which is
// used by the drupal.node-new-comments-link library to determine whether
// a "x new comments" link might be necessary or not. We do this in
// JavaScript to prevent breaking the render cache.
$this->assertSession()->elementNotExists('xpath', '//*[@data-history-node-last-comment-timestamp]');
// Create a new comment. This helper function may be run with different
// comment settings so use $comment->save() to avoid complex setup.
/** @var \Drupal\comment\CommentInterface $comment */
$comment = Comment::create([
'cid' => NULL,
'entity_id' => $this->node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'pid' => 0,
'uid' => $this->loggedInUser->id(),
'status' => CommentInterface::PUBLISHED,
'subject' => $this->randomMachineName(),
'hostname' => '127.0.0.1',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'comment_body' => [LanguageInterface::LANGCODE_NOT_SPECIFIED => [$this->randomMachineName()]],
]);
$comment->save();
$this->drupalLogout();
// Log in with 'web user' and check comment links.
$this->drupalLogin($this->webUser);
$this->drupalGet('node');
// Verify the data-history-node-last-comment-timestamp attribute. Given its
// value, the drupal.node-new-comments-link library would determine that the
// node received a comment after the user last viewed it, and hence it would
// perform an HTTP request to render the "new comments" node link.
$this->assertSession()->elementsCount('xpath', '//*[@data-history-node-last-comment-timestamp="' . $comment->getChangedTime() . '"]', 1);
$this->assertSession()->elementsCount('xpath', '//*[@data-history-node-field-name="comment"]', 1);
// The data will be pre-seeded on this particular page in drupalSettings, to
// avoid the need for the client to make a separate request to the server.
$settings = $this->getDrupalSettings();
$this->assertEquals(['lastReadTimestamps' => [1 => 0]], $settings['history']);
$this->assertEquals([
'newCommentsLinks' => [
'node' => [
'comment' => [
1 => [
'new_comment_count' => 1,
'first_new_comment_link' => Url::fromRoute('entity.node.canonical', ['node' => 1])->setOptions([
'fragment' => 'new',
])->toString(),
],
],
],
],
], $settings['comment']);
// Pretend the data was not present in drupalSettings, i.e. test the
// separate request to the server.
$response = $this->renderNewCommentsNodeLinks([$this->node->id()]);
$this->assertSame(200, $response->getStatusCode());
$json = Json::decode($response->getBody());
$expected = [
$this->node->id() => [
'new_comment_count' => 1,
'first_new_comment_link' => $this->node->toUrl('canonical', ['fragment' => 'new'])->toString(),
],
];
$this->assertSame($expected, $json);
// Failing to specify node IDs for the endpoint should return a 404.
$response = $this->renderNewCommentsNodeLinks([]);
$this->assertSame(404, $response->getStatusCode());
// Accessing the endpoint as the anonymous user should return a 403.
$this->drupalLogout();
$response = $this->renderNewCommentsNodeLinks([$this->node->id()]);
$this->assertSame(403, $response->getStatusCode());
$response = $this->renderNewCommentsNodeLinks([]);
$this->assertSame(403, $response->getStatusCode());
}
}

View File

@@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\CommentManagerInterface;
/**
* Tests comments with node access.
*
* Verifies there is no PostgreSQL error when viewing a node with threaded
* comments (a comment and a reply), if a node access module is in use.
*
* @group comment
*/
class CommentNodeAccessTest extends CommentTestBase {
/**
* Modules to install.
*
* @var array
*/
protected static $modules = ['node_access_test'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
node_access_rebuild();
// Re-create user.
$this->webUser = $this->drupalCreateUser([
'access comments',
'post comments',
'create article content',
'edit own comments',
'node test view',
'skip comment approval',
]);
// Set the author of the created node to the web_user uid.
$this->node->setOwnerId($this->webUser->id())->save();
}
/**
* Tests that threaded comments can be viewed.
*/
public function testThreadedCommentView(): void {
// Set comments to have subject required and preview disabled.
$this->setCommentPreview(DRUPAL_DISABLED);
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
// Post comment.
$this->drupalLogin($this->webUser);
$comment_text = $this->randomMachineName();
$comment_subject = $this->randomMachineName();
$comment = $this->postComment($this->node, $comment_text, $comment_subject);
$this->assertTrue($this->commentExists($comment), 'Comment found.');
// Check comment display.
$this->drupalGet('node/' . $this->node->id());
$this->assertSession()->pageTextContains($comment_subject);
$this->assertSession()->pageTextContains($comment_text);
// Reply to comment, creating second comment.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
$reply_text = $this->randomMachineName();
$reply_subject = $this->randomMachineName();
$reply = $this->postComment(NULL, $reply_text, $reply_subject, TRUE);
$this->assertTrue($this->commentExists($reply, TRUE), 'Reply found.');
// Go to the node page and verify comment and reply are visible.
$this->drupalGet('node/' . $this->node->id());
$this->assertSession()->pageTextContains($comment_text);
$this->assertSession()->pageTextContains($comment_subject);
$this->assertSession()->pageTextContains($reply_text);
$this->assertSession()->pageTextContains($reply_subject);
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\Entity\Comment;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Tests that comments behave correctly when the node is changed.
*
* @group comment
*/
class CommentNodeChangesTest extends CommentTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests that comments are deleted with the node.
*/
public function testNodeDeletion(): void {
$this->drupalLogin($this->webUser);
$comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
$this->assertInstanceOf(Comment::class, $comment);
$this->node->delete();
$this->assertNull(Comment::load($comment->id()), 'The comment could not be loaded after the node was deleted.');
// Make sure the comment field storage and all its fields are deleted when
// the node type is deleted.
$this->assertNotNull(FieldStorageConfig::load('node.comment'), 'Comment field storage exists');
$this->assertNotNull(FieldConfig::load('node.article.comment'), 'Comment field exists');
// Delete the node type.
$this->node->get('type')->entity->delete();
$this->assertNull(FieldStorageConfig::load('node.comment'), 'Comment field storage deleted');
$this->assertNull(FieldConfig::load('node.article.comment'), 'Comment field deleted');
}
}

View File

@@ -0,0 +1,555 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Entity\CommentType;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
use Drupal\user\RoleInterface;
/**
* Tests commenting on a test entity.
*
* @group comment
*/
class CommentNonNodeTest extends BrowserTestBase {
use FieldUiTestTrait;
use CommentTestTrait;
protected static $modules = [
'comment',
'user',
'field_ui',
'entity_test',
'block',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* An administrative user with permission to configure comment settings.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* The entity to use within tests.
*
* @var \Drupal\entity_test\Entity\EntityTest
*/
protected $entity;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->drupalPlaceBlock('system_breadcrumb_block');
$this->drupalPlaceBlock('page_title_block');
// Create a bundle for entity_test.
entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test');
CommentType::create([
'id' => 'comment',
'label' => 'Comment settings',
'description' => 'Comment settings',
'target_entity_type_id' => 'entity_test',
])->save();
// Create comment field on entity_test bundle.
$this->addDefaultCommentField('entity_test', 'entity_test');
// Verify that bundles are defined correctly.
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('comment');
$this->assertEquals('Comment settings', $bundles['comment']['label']);
// Create test user.
$this->adminUser = $this->drupalCreateUser([
'administer comments',
'skip comment approval',
'post comments',
'access comments',
'view test entity',
'administer entity_test content',
]);
// Enable anonymous and authenticated user comments.
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments',
'post comments',
'skip comment approval',
]);
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
'access comments',
'post comments',
'skip comment approval',
]);
// Create a test entity.
$random_label = $this->randomMachineName();
$data = ['type' => 'entity_test', 'name' => $random_label];
$this->entity = EntityTest::create($data);
$this->entity->save();
}
/**
* Posts a comment.
*
* @param \Drupal\Core\Entity\EntityInterface|null $entity
* Entity to post comment on or NULL to post to the previously loaded page.
* @param string $comment
* Comment body.
* @param string $subject
* Comment subject.
* @param mixed $contact
* Set to NULL for no contact info, TRUE to ignore success checking, and
* array of values to set contact info.
*
* @return \Drupal\comment\CommentInterface
* The new comment entity.
*/
public function postComment(?EntityInterface $entity, $comment, $subject = '', $contact = NULL) {
$edit = [];
$edit['comment_body[0][value]'] = $comment;
$field = FieldConfig::loadByName('entity_test', 'entity_test', 'comment');
$preview_mode = $field->getSetting('preview');
// Must get the page before we test for fields.
if ($entity !== NULL) {
$this->drupalGet('comment/reply/entity_test/' . $entity->id() . '/comment');
}
// Determine the visibility of subject form field.
$display_repository = $this->container->get('entity_display.repository');
if ($display_repository->getFormDisplay('comment', 'comment')->getComponent('subject')) {
// Subject input allowed.
$edit['subject[0][value]'] = $subject;
}
else {
$this->assertSession()->fieldValueNotEquals('subject[0][value]', '');
}
if ($contact !== NULL && is_array($contact)) {
$edit += $contact;
}
switch ($preview_mode) {
case DRUPAL_REQUIRED:
// Preview required so no save button should be found.
$this->assertSession()->buttonNotExists('Save');
$this->submitForm($edit, 'Preview');
// Don't break here so that we can test post-preview field presence and
// function below.
case DRUPAL_OPTIONAL:
$this->assertSession()->buttonExists('Preview');
$this->assertSession()->buttonExists('Save');
$this->submitForm($edit, 'Save');
break;
case DRUPAL_DISABLED:
$this->assertSession()->buttonNotExists('Preview');
$this->assertSession()->buttonExists('Save');
$this->submitForm($edit, 'Save');
break;
}
$match = [];
// Get comment ID
preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
// Get comment.
if ($contact !== TRUE) {
// If true then attempting to find error message.
if ($subject) {
$this->assertSession()->pageTextContains($subject);
}
$this->assertSession()->pageTextContains($comment);
// Check the comment ID was extracted.
$this->assertArrayHasKey(1, $match);
}
return Comment::load($match[1]);
}
/**
* Checks current page for specified comment.
*
* @param \Drupal\comment\CommentInterface $comment
* The comment object.
* @param bool $reply
* Boolean indicating whether the comment is a reply to another comment.
*
* @return bool
* Boolean indicating whether the comment was found.
*/
public function commentExists(?CommentInterface $comment = NULL, $reply = FALSE) {
if ($comment) {
$regex = '/' . ($reply ? '<div class="indented">(.*?)' : '');
$regex .= '<article(.*?)id="comment-' . $comment->id() . '"(.*?)';
$regex .= $comment->getSubject() . '(.*?)';
$regex .= $comment->comment_body->value . '(.*?)';
$regex .= '/s';
return (boolean) preg_match($regex, $this->getSession()->getPage()->getContent());
}
else {
return FALSE;
}
}
/**
* Checks whether the commenter's contact information is displayed.
*
* @return bool
* Contact info is available.
*/
public function commentContactInfoAvailable() {
return (bool) preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->getSession()->getPage()->getContent());
}
/**
* Performs the specified operation on the specified comment.
*
* @param object $comment
* Comment to perform operation on.
* @param string $operation
* Operation to perform.
* @param bool $approval
* Operation is found on approval page.
*/
public function performCommentOperation($comment, $operation, $approval = FALSE) {
$edit = [];
$edit['operation'] = $operation;
$edit['comments[' . $comment->id() . ']'] = TRUE;
$this->drupalGet('admin/content/comment' . ($approval ? '/approval' : ''));
$this->submitForm($edit, 'Update');
if ($operation == 'delete') {
$this->submitForm([], 'Delete');
$this->assertSession()->pageTextContains('Deleted 1 comment.');
}
else {
$this->assertSession()->pageTextContains('The update has been performed.');
}
}
/**
* Gets the comment ID for an unapproved comment.
*
* @param string $subject
* Comment subject to find.
*
* @return int
* Comment ID.
*/
public function getUnapprovedComment($subject) {
$this->drupalGet('admin/content/comment/approval');
preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->getSession()->getPage()->getContent(), $match);
return $match[2];
}
/**
* Tests anonymous comment functionality.
*/
public function testCommentFunctionality(): void {
$limited_user = $this->drupalCreateUser([
'administer entity_test fields',
]);
$this->drupalLogin($limited_user);
// Test that default field exists.
$this->drupalGet('entity_test/structure/entity_test/fields');
$this->assertSession()->pageTextContains('Comments');
$this->assertSession()->linkByHrefExists('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
// Test widget hidden option is not visible when there's no comments.
$this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->fieldNotExists('edit-default-value-input-comment-und-0-status-0');
// Test that field to change cardinality is not available.
$this->assertSession()->fieldNotExists('cardinality_number');
$this->assertSession()->fieldNotExists('cardinality');
$this->drupalLogin($this->adminUser);
// Test breadcrumb on comment add page.
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
$this->assertSession()->elementTextEquals('xpath', '//nav[@aria-labelledby="system-breadcrumb"]/ol/li[last()]/a', $this->entity->label());
// Post a comment.
/** @var \Drupal\comment\CommentInterface $comment1 */
$comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName());
$this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.');
// Test breadcrumb on comment reply page.
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id());
$this->assertSession()->elementTextEquals('xpath', '//nav[@aria-labelledby="system-breadcrumb"]/ol/li[last()]/a', $comment1->getSubject());
// Test breadcrumb on comment edit page.
$this->drupalGet('comment/' . $comment1->id() . '/edit');
$this->assertSession()->elementTextEquals('xpath', '//nav[@aria-labelledby="system-breadcrumb"]/ol/li[last()]/a', $comment1->getSubject());
// Test breadcrumb on comment delete page.
$this->drupalGet('comment/' . $comment1->id() . '/delete');
$this->assertSession()->elementTextEquals('xpath', '//nav[@aria-labelledby="system-breadcrumb"]/ol/li[last()]/a', $comment1->getSubject());
// Test threading replying to comment #1 creating comment #1_2.
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id());
$comment1_2 = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName());
$this->assertTrue($this->commentExists($comment1_2, TRUE), 'Comment #1_2. Reply found.');
$this->assertEquals('01.00/', $comment1_2->getThread());
// Test nested threading replying to comment #1_2 creating comment #1_2_3.
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1_2->id());
$comment1_2_3 = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName());
$this->assertTrue($this->commentExists($comment1_2_3, TRUE), 'Comment #1_2_3. Reply found.');
$this->assertEquals('01.00.00/', $comment1_2_3->getThread());
// Unpublish the comment.
$this->performCommentOperation($comment1, 'unpublish');
$this->drupalGet('admin/content/comment/approval');
$this->assertSession()->responseContains('comments[' . $comment1->id() . ']');
// Publish the comment.
$this->performCommentOperation($comment1, 'publish', TRUE);
$this->drupalGet('admin/content/comment');
$this->assertSession()->responseContains('comments[' . $comment1->id() . ']');
// Delete the comment.
$this->performCommentOperation($comment1, 'delete');
$this->drupalGet('admin/content/comment');
$this->assertSession()->responseNotContains('comments[' . $comment1->id() . ']');
// Post another comment.
$comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName());
$this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.');
// Check that the comment was found.
$this->drupalGet('admin/content/comment');
$this->assertSession()->responseContains('comments[' . $comment1->id() . ']');
// Check that entity access applies to administrative page.
$this->assertSession()->pageTextContains($this->entity->label());
$limited_user = $this->drupalCreateUser([
'administer comments',
]);
$this->drupalLogin($limited_user);
$this->drupalGet('admin/content/comment');
$this->assertSession()->pageTextNotContains($this->entity->label());
$this->drupalLogout();
// Deny anonymous users access to comments.
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => FALSE,
'post comments' => FALSE,
'skip comment approval' => FALSE,
'view test entity' => TRUE,
]);
// Attempt to view comments while disallowed.
$this->drupalGet('entity-test/' . $this->entity->id());
// Verify that comments were not displayed.
$this->assertSession()->responseNotMatches('@<h2[^>]*>Comments</h2>@');
$this->assertSession()->linkNotExists('Add new comment', 'Link to add comment was found.');
// Attempt to view test entity comment form while disallowed.
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
$this->assertSession()->statusCodeEquals(403);
$this->assertSession()->fieldNotExists('subject[0][value]');
$this->assertSession()->fieldNotExists('comment_body[0][value]');
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => TRUE,
'post comments' => FALSE,
'view test entity' => TRUE,
'skip comment approval' => FALSE,
]);
$this->drupalGet('entity_test/' . $this->entity->id());
// Verify that the comment field title is displayed.
$this->assertSession()->responseMatches('@<h2[^>]*>Comments</h2>@');
$this->assertSession()->linkExists('Log in', 0, 'Link to login was found.');
$this->assertSession()->linkExists('register', 0, 'Link to register was found.');
$this->assertSession()->fieldNotExists('subject[0][value]');
$this->assertSession()->fieldNotExists('comment_body[0][value]');
// Test the combination of anonymous users being able to post, but not view
// comments, to ensure that access to post comments doesn't grant access to
// view them.
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => FALSE,
'post comments' => TRUE,
'skip comment approval' => TRUE,
'view test entity' => TRUE,
]);
$this->drupalGet('entity_test/' . $this->entity->id());
// Verify that comments were not displayed.
$this->assertSession()->responseNotMatches('@<h2[^>]*>Comments</h2>@');
$this->assertSession()->fieldValueEquals('subject[0][value]', '');
$this->assertSession()->fieldValueEquals('comment_body[0][value]', '');
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id());
$this->assertSession()->statusCodeEquals(403);
$this->assertSession()->pageTextNotContains($comment1->getSubject());
// Test comment field widget changes.
$limited_user = $this->drupalCreateUser([
'administer entity_test fields',
'view test entity',
'administer entity_test content',
'administer comments',
]);
$this->drupalLogin($limited_user);
$this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
$this->assertSession()->checkboxNotChecked('edit-default-value-input-comment-0-status-0');
$this->assertSession()->checkboxNotChecked('edit-default-value-input-comment-0-status-1');
$this->assertSession()->checkboxChecked('edit-default-value-input-comment-0-status-2');
// Test comment option change in field settings.
$edit = [
'default_value_input[comment][0][status]' => CommentItemInterface::CLOSED,
'settings[anonymous]' => CommentInterface::ANONYMOUS_MAY_CONTACT,
];
$this->submitForm($edit, 'Save settings');
$this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
$this->assertSession()->checkboxNotChecked('edit-default-value-input-comment-0-status-0');
$this->assertSession()->checkboxChecked('edit-default-value-input-comment-0-status-1');
$this->assertSession()->checkboxNotChecked('edit-default-value-input-comment-0-status-2');
$this->assertSession()->fieldValueEquals('settings[anonymous]', CommentInterface::ANONYMOUS_MAY_CONTACT);
// Add a new comment-type.
$bundle = CommentType::create([
'id' => 'foobar',
'label' => 'Foobar',
'description' => '',
'target_entity_type_id' => 'entity_test',
]);
$bundle->save();
// Add a new comment field.
$storage_edit = [
'settings[comment_type]' => 'foobar',
];
$this->fieldUIAddNewField('entity_test/structure/entity_test', 'foobar', 'Foobar', 'comment', $storage_edit);
// Add a third comment field.
$this->fieldUIAddNewField('entity_test/structure/entity_test', 'bar_foo', 'Bar_Foo', 'comment', $storage_edit);
// Check the field contains the correct comment type.
$field_storage = FieldStorageConfig::load('entity_test.field_bar_foo');
$this->assertInstanceOf(FieldStorageConfig::class, $field_storage);
$this->assertEquals('foobar', $field_storage->getSetting('comment_type'));
$this->assertEquals(1, $field_storage->getCardinality());
// Test the new entity commenting inherits default.
$random_label = $this->randomMachineName();
$data = ['bundle' => 'entity_test', 'name' => $random_label];
$new_entity = EntityTest::create($data);
$new_entity->save();
$this->drupalGet('entity_test/manage/' . $new_entity->id() . '/edit');
$this->assertSession()->checkboxChecked('edit-field-foobar-0-status-2');
$this->assertSession()->checkboxNotChecked('edit-field-foobar-0-status-0');
$this->assertSession()->fieldNotExists('edit-field-foobar-0-status-1');
// @todo Check proper URL and form https://www.drupal.org/node/2458323
$this->drupalGet('comment/reply/entity_test/comment/' . $new_entity->id());
$this->assertSession()->fieldNotExists('subject[0][value]');
$this->assertSession()->fieldNotExists('comment_body[0][value]');
// Test removal of comment_body field.
$limited_user = $this->drupalCreateUser([
'administer entity_test fields',
'post comments',
'administer comment fields',
'administer comment types',
'view test entity',
]);
$this->drupalLogin($limited_user);
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
$this->assertSession()->fieldValueEquals('comment_body[0][value]', '');
$this->fieldUIDeleteField('admin/structure/comment/manage/comment', 'comment.comment.comment_body', 'Comment', 'Comment settings', 'comment type');
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
$this->assertSession()->fieldNotExists('comment_body[0][value]');
// Set subject field to autogenerate it.
$edit = ['subject[0][value]' => ''];
$this->submitForm($edit, 'Save');
}
/**
* Tests comment fields cannot be added to entity types without integer IDs.
*/
public function testsNonIntegerIdEntities(): void {
// Create a bundle for entity_test_string_id.
entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_string_id');
$limited_user = $this->drupalCreateUser([
'administer entity_test_string_id fields',
'administer comment types',
]);
$this->drupalLogin($limited_user);
// Visit the Field UI field add page.
$this->drupalGet('entity_test_string_id/structure/entity_test/fields/add-field');
// Ensure field isn't shown for string IDs.
$this->assertSession()->elementNotExists('css', "[name='new_storage_type'][value='comment']");
// Ensure a core field type shown.
$this->assertSession()->elementExists('css', "[name='new_storage_type'][value='boolean']");
// Attempt to add a comment-type referencing this entity-type.
$this->drupalGet('admin/structure/comment/types/add');
$this->assertSession()->optionNotExists('edit-target-entity-type-id', 'entity_test_string_id');
$this->assertSession()->responseNotContains('Test entity with string_id');
// Create a bundle for entity_test_no_id.
entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_no_id');
$this->drupalLogin($this->drupalCreateUser([
'administer entity_test_no_id fields',
]));
// Visit the Field UI field add page.
$this->drupalGet('entity_test_no_id/structure/entity_test/fields/add-field');
// Ensure field isn't shown for empty IDs.
$this->assertSession()->elementNotExists('css', "[name='new_storage_type'][value='comment']");
// Ensure a core field type shown.
$this->assertSession()->elementExists('css', "[name='new_storage_type'][value='boolean']");
}
/**
* Ensures that comment settings are not required.
*/
public function testCommentSettingsNotRequired(): void {
$limited_user = $this->drupalCreateUser([
'administer entity_test fields',
]);
$this->drupalLogin($limited_user);
$this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
// Change the comments to be displayed as hidden by default.
$edit = [
'default_value_input[comment][0][status]' => CommentItemInterface::HIDDEN,
'settings[anonymous]' => CommentInterface::ANONYMOUS_MAY_CONTACT,
];
$this->submitForm($edit, 'Save settings');
// Ensure that the comment settings field is not required and can be saved
// with the default value.
$this->drupalLogin($this->adminUser);
$this->drupalGet('/entity_test/add');
$this->assertSession()->checkboxChecked('edit-comment-0-status-0');
$edit = [
"name[0][value]" => 'Comment test',
];
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('entity_test 2 has been created.');
}
}

View File

@@ -0,0 +1,453 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\CommentManagerInterface;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\node\Entity\Node;
/**
* Tests paging of comments and their settings.
*
* @group comment
*/
class CommentPagerTest extends CommentTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Confirms comment paging works correctly with flat and threaded comments.
*/
public function testCommentPaging(): void {
$this->drupalLogin($this->adminUser);
// Set comment variables.
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
$this->setCommentPreview(DRUPAL_DISABLED);
// Create a node and three comments.
$node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
$comments = [];
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
// Set "Comments per page" as zero and verify that all comments are appearing
// on the page.
$this->setCommentsPerPage(0);
$this->drupalGet('node/' . $node->id());
$this->assertTrue($this->commentExists($comments[0]), 'Comment 1 appears on page.');
$this->assertTrue($this->commentExists($comments[1]), 'Comment 2 appears on page.');
$this->assertTrue($this->commentExists($comments[2]), 'Comment 3 appears on page.');
// Set comments to one per page so that we are able to test paging without
// needing to insert large numbers of comments.
$this->setCommentsPerPage(1);
// Check the first page of the node, and confirm the correct comments are
// shown.
$this->drupalGet('node/' . $node->id());
$this->assertSession()->pageTextContains('next');
$this->assertTrue($this->commentExists($comments[0]), 'Comment 1 appears on page 1.');
$this->assertFalse($this->commentExists($comments[1]), 'Comment 2 does not appear on page 1.');
$this->assertFalse($this->commentExists($comments[2]), 'Comment 3 does not appear on page 1.');
// Check the second page.
$this->drupalGet('node/' . $node->id(), ['query' => ['page' => 1]]);
$this->assertTrue($this->commentExists($comments[1]), 'Comment 2 appears on page 2.');
$this->assertFalse($this->commentExists($comments[0]), 'Comment 1 does not appear on page 2.');
$this->assertFalse($this->commentExists($comments[2]), 'Comment 3 does not appear on page 2.');
// Check the third page.
$this->drupalGet('node/' . $node->id(), ['query' => ['page' => 2]]);
$this->assertTrue($this->commentExists($comments[2]), 'Comment 3 appears on page 3.');
$this->assertFalse($this->commentExists($comments[0]), 'Comment 1 does not appear on page 3.');
$this->assertFalse($this->commentExists($comments[1]), 'Comment 2 does not appear on page 3.');
// Post a reply to the oldest comment and test again.
$oldest_comment = reset($comments);
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $oldest_comment->id());
$reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$this->setCommentsPerPage(2);
// We are still in flat view - the replies should not be on the first page,
// even though they are replies to the oldest comment.
$this->drupalGet('node/' . $node->id(), ['query' => ['page' => 0]]);
$this->assertFalse($this->commentExists($reply, TRUE), 'In flat mode, reply does not appear on page 1.');
// If we switch to threaded mode, the replies on the oldest comment
// should be bumped to the first page and comment 6 should be bumped
// to the second page.
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
$this->drupalGet('node/' . $node->id(), ['query' => ['page' => 0]]);
$this->assertTrue($this->commentExists($reply, TRUE), 'In threaded mode, reply appears on page 1.');
$this->assertFalse($this->commentExists($comments[1]), 'In threaded mode, comment 2 has been bumped off of page 1.');
// If (# replies > # comments per page) in threaded expanded view,
// the overage should be bumped.
$reply2 = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$this->drupalGet('node/' . $node->id(), ['query' => ['page' => 0]]);
$this->assertFalse($this->commentExists($reply2, TRUE), 'In threaded mode where # replies > # comments per page, the newest reply does not appear on page 1.');
// Test that the page build process does not somehow generate errors when
// # comments per page is set to 0.
$this->setCommentsPerPage(0);
$this->drupalGet('node/' . $node->id(), ['query' => ['page' => 0]]);
$this->assertFalse($this->commentExists($reply2, TRUE), 'Threaded mode works correctly when comments per page is 0.');
// Test that all main comments are appearing in the threaded mode.
$this->assertTrue($this->commentExists($comments[0]), 'Comment 1 appears on page.');
$this->assertTrue($this->commentExists($comments[1]), 'Comment 2 appears on page.');
$this->assertTrue($this->commentExists($comments[2]), 'Comment 3 appears on page.');
$this->drupalLogout();
}
/**
* Confirms comment paging works correctly with flat and threaded comments.
*/
public function testCommentPermalink(): void {
$this->drupalLogin($this->adminUser);
// Set comment variables.
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
$this->setCommentPreview(DRUPAL_DISABLED);
// Create a node and three comments.
$node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
$comments = [];
$comments[] = $this->postComment($node, 'comment 1: ' . $this->randomMachineName(), $this->randomMachineName(), TRUE);
$comments[] = $this->postComment($node, 'comment 2: ' . $this->randomMachineName(), $this->randomMachineName(), TRUE);
$comments[] = $this->postComment($node, 'comment 3: ' . $this->randomMachineName(), $this->randomMachineName(), TRUE);
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
// Set comments to one per page so that we are able to test paging without
// needing to insert large numbers of comments.
$this->setCommentsPerPage(1);
// Navigate to each comment permalink as anonymous and assert it appears on
// the page.
foreach ($comments as $index => $comment) {
$this->drupalGet($comment->toUrl());
$this->assertTrue($this->commentExists($comment), sprintf('Comment %d appears on page %d.', $index + 1, $index + 1));
}
}
/**
* Tests comment ordering and threading.
*/
public function testCommentOrderingThreading(): void {
$this->drupalLogin($this->adminUser);
// Set comment variables.
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
$this->setCommentPreview(DRUPAL_DISABLED);
// Display all the comments on the same page.
$this->setCommentsPerPage(1000);
// Create a node and three comments.
$node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
$comments = [];
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// Post a reply to the second comment.
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[1]->id());
$comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// Post a reply to the first comment.
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[0]->id());
$comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// Post a reply to the last comment.
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[2]->id());
$comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// Post a reply to the second comment.
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[3]->id());
$comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// At this point, the comment tree is:
// - 0
// - 4
// - 1
// - 3
// - 6
// - 2
// - 5
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
$expected_order = [
0,
1,
2,
3,
4,
5,
6,
];
$this->drupalGet('node/' . $node->id());
$this->assertCommentOrder($comments, $expected_order);
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
$expected_order = [
0,
4,
1,
3,
6,
2,
5,
];
$this->drupalGet('node/' . $node->id());
$this->assertCommentOrder($comments, $expected_order);
}
/**
* Asserts that the comments are displayed in the correct order.
*
* @param \Drupal\comment\CommentInterface[] $comments
* An array of comments, must be of the type CommentInterface.
* @param array $expected_order
* An array of keys from $comments describing the expected order.
*
* @internal
*/
public function assertCommentOrder(array $comments, array $expected_order): void {
$expected_cids = [];
// First, rekey the expected order by cid.
foreach ($expected_order as $key) {
$expected_cids[] = $comments[$key]->id();
}
$comment_anchors = $this->xpath('//article[starts-with(@id,"comment-")]');
$result_order = [];
foreach ($comment_anchors as $anchor) {
$result_order[] = substr($anchor->getAttribute('id'), 8);
}
$this->assertEquals($expected_cids, $result_order, sprintf('Comment order: expected %s, returned %s.', implode(',', $expected_cids), implode(',', $result_order)));
}
/**
* Tests calculation of first page with new comment.
*/
public function testCommentNewPageIndicator(): void {
$this->drupalLogin($this->adminUser);
// Set comment variables.
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
$this->setCommentPreview(DRUPAL_DISABLED);
// Set comments to one per page so that we are able to test paging without
// needing to insert large numbers of comments.
$this->setCommentsPerPage(1);
// Create a node and three comments.
$node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
$comments = [];
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// Post a reply to the second comment.
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[1]->id());
$comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// Post a reply to the first comment.
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[0]->id());
$comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// Post a reply to the last comment.
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[2]->id());
$comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// At this point, the comment tree is:
// - 0
// - 4
// - 1
// - 3
// - 2
// - 5
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
$expected_pages = [
// Page of comment 5
1 => 5,
// Page of comment 4
2 => 4,
// Page of comment 3
3 => 3,
// Page of comment 2
4 => 2,
// Page of comment 1
5 => 1,
// Page of comment 0
6 => 0,
];
$node = Node::load($node->id());
foreach ($expected_pages as $new_replies => $expected_page) {
$returned_page = \Drupal::entityTypeManager()->getStorage('comment')
->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, 'comment');
$this->assertSame($expected_page, $returned_page, "Flat mode, $new_replies replies: expected page $expected_page, returned page $returned_page.");
}
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
$expected_pages = [
// Page of comment 5
1 => 5,
// Page of comment 4
2 => 1,
// Page of comment 4
3 => 1,
// Page of comment 4
4 => 1,
// Page of comment 4
5 => 1,
// Page of comment 0
6 => 0,
];
\Drupal::entityTypeManager()->getStorage('node')->resetCache([$node->id()]);
$node = Node::load($node->id());
foreach ($expected_pages as $new_replies => $expected_page) {
$returned_page = \Drupal::entityTypeManager()->getStorage('comment')
->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, 'comment');
$this->assertEquals($expected_page, $returned_page, "Threaded mode, $new_replies replies: expected page $expected_page, returned page $returned_page.");
}
}
/**
* Confirms comment paging works correctly with two pagers.
*/
public function testTwoPagers(): void {
// Add another field to article content-type.
$this->addDefaultCommentField('node', 'article', 'comment_2');
// Set default to display comment list with unique pager id.
\Drupal::service('entity_display.repository')
->getViewDisplay('node', 'article')
->setComponent('comment_2', [
'label' => 'hidden',
'type' => 'comment_default',
'weight' => 30,
'settings' => [
'pager_id' => 1,
'view_mode' => 'default',
],
])
->save();
// Make sure pager appears in formatter summary and settings form.
$account = $this->drupalCreateUser(['administer node display']);
$this->drupalLogin($account);
$this->drupalGet('admin/structure/types/manage/article/display');
// No summary for standard pager.
$this->assertSession()->pageTextNotContains('Pager ID: 0');
$this->assertSession()->pageTextContains('Pager ID: 1');
$this->submitForm([], 'comment_settings_edit');
// Change default pager to 2.
$this->submitForm(['fields[comment][settings_edit_form][settings][pager_id]' => 2], 'Save');
$this->assertSession()->pageTextContains('Pager ID: 2');
// Revert the changes.
$this->submitForm([], 'comment_settings_edit');
$this->submitForm(['fields[comment][settings_edit_form][settings][pager_id]' => 0], 'Save');
// No summary for standard pager.
$this->assertSession()->pageTextNotContains('Pager ID: 0');
$this->drupalLogin($this->adminUser);
// Add a new node with both comment fields open.
$node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
// Set comment options.
$comments = [];
foreach (['comment', 'comment_2'] as $field_name) {
$this->setCommentForm(TRUE, $field_name);
$this->setCommentPreview(DRUPAL_OPTIONAL, $field_name);
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.', $field_name);
// Set comments to one per page so that we are able to test paging without
// needing to insert large numbers of comments.
$this->setCommentsPerPage(1, $field_name);
for ($i = 1; $i <= 4; $i++) {
$comment = "Comment $i on field $field_name";
$comments[] = $this->postComment($node, $comment, $comment, TRUE, $field_name);
}
}
// Check the first page of the node, and confirm the correct comments are
// shown.
$this->drupalGet('node/' . $node->id());
$this->assertSession()->pageTextContains('next');
$this->assertSession()->pageTextContains('Comment 1 on field comment');
$this->assertSession()->pageTextContains('Comment 1 on field comment_2');
// Navigate to next page of field 1.
$this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', [':label' => 'Comment 1 on field comment']);
// Check only one pager updated.
$this->assertSession()->pageTextContains('Comment 2 on field comment');
$this->assertSession()->pageTextContains('Comment 1 on field comment_2');
// Return to page 1.
$this->drupalGet('node/' . $node->id());
// Navigate to next page of field 2.
$this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', [':label' => 'Comment 1 on field comment_2']);
// Check only one pager updated.
$this->assertSession()->pageTextContains('Comment 1 on field comment');
$this->assertSession()->pageTextContains('Comment 2 on field comment_2');
// Navigate to next page of field 1.
$this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', [':label' => 'Comment 1 on field comment']);
// Check only one pager updated.
$this->assertSession()->pageTextContains('Comment 2 on field comment');
$this->assertSession()->pageTextContains('Comment 2 on field comment_2');
}
/**
* Follows a link found at a give xpath query.
*
* Will click the first link found with the given xpath query by default,
* or a later one if an index is given.
*
* If the link is discovered and clicked, the test passes. Fail otherwise.
*
* @param string $xpath
* Xpath query that targets an anchor tag, or set of anchor tags.
* @param array $arguments
* An array of arguments with keys in the form ':name' matching the
* placeholders in the query. The values may be either strings or numeric
* values.
* @param int $index
* Link position counting from zero.
*
* @return string|false
* Page contents on success, or FALSE on failure.
*
* @see \Drupal\Tests\UiHelperTrait::clickLink()
*/
protected function clickLinkWithXPath($xpath, $arguments = [], $index = 0) {
$url_before = $this->getUrl();
$urls = $this->xpath($xpath, $arguments);
if (isset($urls[$index])) {
$url_target = $this->getAbsoluteUrl($urls[$index]->getAttribute('href'));
return $this->drupalGet($url_target);
}
$this->fail(new FormattableMarkup('Link %label does not exist on @url_before', ['%label' => $xpath, '@url_before' => $url_before]));
return FALSE;
}
}

View File

@@ -0,0 +1,227 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\CommentManagerInterface;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\comment\Entity\Comment;
use Drupal\Tests\TestFileCreationTrait;
/**
* Tests comment preview.
*
* @group comment
*/
class CommentPreviewTest extends CommentTestBase {
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
}
/**
* Modules to install.
*
* @var array
*/
protected static $modules = ['olivero_test', 'test_user_config'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'olivero';
/**
* Tests comment preview.
*/
public function testCommentPreview(): void {
$this->setCommentPreview(DRUPAL_OPTIONAL);
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
// Log in as web user.
$this->drupalLogin($this->webUser);
// Test escaping of the username on the preview form.
\Drupal::service('module_installer')->install(['user_hooks_test']);
\Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE);
$edit = [];
$edit['subject[0][value]'] = $this->randomMachineName(8);
$edit['comment_body[0][value]'] = $this->randomMachineName(16);
$this->drupalGet('node/' . $this->node->id());
$this->submitForm($edit, 'Preview');
$this->assertSession()->assertEscaped('<em>' . $this->webUser->id() . '</em>');
\Drupal::state()->set('user_hooks_test_user_format_name_alter_safe', TRUE);
$this->drupalGet('node/' . $this->node->id());
$this->submitForm($edit, 'Preview');
$this->assertInstanceOf(MarkupInterface::class, $this->webUser->getDisplayName());
$this->assertSession()->assertNoEscaped('<em>' . $this->webUser->id() . '</em>');
$this->assertSession()->responseContains('<em>' . $this->webUser->id() . '</em>');
// Add a user picture.
$image = current($this->drupalGetTestFiles('image'));
$user_edit['files[user_picture_0]'] = \Drupal::service('file_system')->realpath($image->uri);
$this->drupalGet('user/' . $this->webUser->id() . '/edit');
$this->submitForm($user_edit, 'Save');
// As the web user, fill in the comment form and preview the comment.
$this->drupalGet('node/' . $this->node->id());
$this->submitForm($edit, 'Preview');
// Check that the preview is displaying the title and body.
$this->assertSession()->titleEquals('Preview comment | Drupal');
$this->assertSession()->pageTextContains($edit['subject[0][value]']);
$this->assertSession()->pageTextContains($edit['comment_body[0][value]']);
// Check that the title and body fields are displayed with the correct values.
$this->assertSession()->fieldValueEquals('subject[0][value]', $edit['subject[0][value]']);
$this->assertSession()->fieldValueEquals('comment_body[0][value]', $edit['comment_body[0][value]']);
// Check that the user picture is displayed.
$this->assertSession()->elementExists('xpath', "//article[contains(@class, 'preview')]//div[contains(@class, 'user-picture')]//img");
// Ensure that preview node is displayed after the submit buttons of the form.
$xpath = $this->assertSession()->buildXPathQuery('//div[@id=:id]/following-sibling::article', [':id' => 'edit-actions']);
$this->assertSession()->elementExists('xpath', $xpath);
}
/**
* Tests comment preview.
*/
public function testCommentPreviewDuplicateSubmission(): void {
$this->setCommentPreview(DRUPAL_OPTIONAL);
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
// Log in as web user.
$this->drupalLogin($this->webUser);
// As the web user, fill in the comment form and preview the comment.
$edit = [];
$edit['subject[0][value]'] = $this->randomMachineName(8);
$edit['comment_body[0][value]'] = $this->randomMachineName(16);
$this->drupalGet('node/' . $this->node->id());
$this->submitForm($edit, 'Preview');
// Check that the preview is displaying the title and body.
$this->assertSession()->titleEquals('Preview comment | Drupal');
$this->assertSession()->pageTextContains($edit['subject[0][value]']);
$this->assertSession()->pageTextContains($edit['comment_body[0][value]']);
// Check that the title and body fields are displayed with the correct values.
$this->assertSession()->fieldValueEquals('subject[0][value]', $edit['subject[0][value]']);
$this->assertSession()->fieldValueEquals('comment_body[0][value]', $edit['comment_body[0][value]']);
// Store the content of this page.
$this->submitForm([], 'Save');
$this->assertSession()->pageTextContains('Your comment has been posted.');
$this->assertSession()->elementsCount('xpath', '//section[contains(@class, "comments")]/article', 1);
// Go back and re-submit the form.
$this->getSession()->getDriver()->back();
$submit_button = $this->assertSession()->buttonExists('Save');
$submit_button->click();
$this->assertSession()->pageTextContains('Your comment has been posted.');
$this->assertSession()->elementsCount('xpath', '//section[contains(@class, "comments")]/article', 2);
}
/**
* Tests comment edit, preview, and save.
*/
public function testCommentEditPreviewSave(): void {
$web_user = $this->drupalCreateUser([
'access comments',
'post comments',
'skip comment approval',
'edit own comments',
]);
$this->drupalLogin($this->adminUser);
$this->setCommentPreview(DRUPAL_OPTIONAL);
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
$edit = [];
$date = new DrupalDateTime('2008-03-02 17:23');
$edit['subject[0][value]'] = $this->randomMachineName(8);
$edit['comment_body[0][value]'] = $this->randomMachineName(16);
$edit['uid'] = $web_user->getAccountName() . ' (' . $web_user->id() . ')';
$edit['date[date]'] = $date->format('Y-m-d');
$edit['date[time]'] = $date->format('H:i:s');
$raw_date = $date->getTimestamp();
$expected_text_date = $this->container->get('date.formatter')->formatInterval(\Drupal::time()->getRequestTime() - $raw_date);
$expected_form_date = $date->format('Y-m-d');
$expected_form_time = $date->format('H:i:s');
$comment = $this->postComment($this->node, $edit['subject[0][value]'], $edit['comment_body[0][value]'], TRUE);
$this->drupalGet('comment/' . $comment->id() . '/edit');
$this->submitForm($edit, 'Preview');
// Check that the preview is displaying the subject, comment, author and date correctly.
$this->assertSession()->titleEquals('Preview comment | Drupal');
$this->assertSession()->pageTextContains($edit['subject[0][value]']);
$this->assertSession()->pageTextContains($edit['comment_body[0][value]']);
$this->assertSession()->pageTextContains($web_user->getAccountName());
$this->assertSession()->pageTextContains($expected_text_date);
// Check that the subject, comment, author and date fields are displayed with the correct values.
$this->assertSession()->fieldValueEquals('subject[0][value]', $edit['subject[0][value]']);
$this->assertSession()->fieldValueEquals('comment_body[0][value]', $edit['comment_body[0][value]']);
$this->assertSession()->fieldValueEquals('uid', $edit['uid']);
$this->assertSession()->fieldValueEquals('date[date]', $edit['date[date]']);
$this->assertSession()->fieldValueEquals('date[time]', $edit['date[time]']);
// Check that saving a comment produces a success message.
$this->drupalGet('comment/' . $comment->id() . '/edit');
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('Your comment has been updated.');
// Check that the comment fields are correct after loading the saved comment.
$this->drupalGet('comment/' . $comment->id() . '/edit');
$this->assertSession()->fieldValueEquals('subject[0][value]', $edit['subject[0][value]']);
$this->assertSession()->fieldValueEquals('comment_body[0][value]', $edit['comment_body[0][value]']);
$this->assertSession()->fieldValueEquals('uid', $edit['uid']);
$this->assertSession()->fieldValueEquals('date[date]', $expected_form_date);
$this->assertSession()->fieldValueEquals('date[time]', $expected_form_time);
// Submit the form using the displayed values.
$displayed = [];
$displayed['subject[0][value]'] = $this->assertSession()->fieldExists('edit-subject-0-value')->getValue();
$displayed['comment_body[0][value]'] = $this->assertSession()->fieldExists('edit-comment-body-0-value')->getValue();
$displayed['uid'] = $this->assertSession()->fieldExists('edit-uid')->getValue();
$displayed['date[date]'] = $this->assertSession()->fieldExists('edit-date-date')->getValue();
$displayed['date[time]'] = $this->assertSession()->fieldExists('edit-date-time')->getValue();
$this->drupalGet('comment/' . $comment->id() . '/edit');
$this->submitForm($displayed, 'Save');
// Check that the saved comment is still correct.
$comment_storage = \Drupal::entityTypeManager()->getStorage('comment');
$comment_storage->resetCache([$comment->id()]);
/** @var \Drupal\comment\CommentInterface $comment_loaded */
$comment_loaded = Comment::load($comment->id());
$this->assertEquals($edit['subject[0][value]'], $comment_loaded->getSubject(), 'Subject loaded.');
$this->assertEquals($edit['comment_body[0][value]'], $comment_loaded->comment_body->value, 'Comment body loaded.');
$this->assertEquals($web_user->id(), $comment_loaded->getOwner()->id(), 'Name loaded.');
$this->assertEquals($raw_date, $comment_loaded->getCreatedTime(), 'Date loaded.');
$this->drupalLogout();
// Check that the date and time of the comment are correct when edited by
// non-admin users.
$user_edit = [];
$expected_created_time = $comment_loaded->getCreatedTime();
$this->drupalLogin($web_user);
// Web user cannot change the comment author.
unset($edit['uid']);
$this->drupalGet('comment/' . $comment->id() . '/edit');
$this->submitForm($user_edit, 'Save');
$comment_storage->resetCache([$comment->id()]);
$comment_loaded = Comment::load($comment->id());
$this->assertEquals($expected_created_time, $comment_loaded->getCreatedTime(), 'Expected date and time for comment edited.');
$this->drupalLogout();
}
}

View File

@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
/**
* Tests comments as part of an RSS feed.
*
* @group comment
*/
class CommentRssTest extends CommentTestBase {
use AssertPageCacheContextsAndTagsTrait;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = ['views'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Setup the rss view display.
EntityViewDisplay::create([
'status' => TRUE,
'targetEntityType' => 'node',
'bundle' => 'article',
'mode' => 'rss',
'content' => ['links' => ['weight' => 100]],
])->save();
}
/**
* Tests comments as part of an RSS feed.
*/
public function testCommentRss(): void {
// Find comment in RSS feed.
$this->drupalLogin($this->webUser);
$this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
$this->drupalGet('rss.xml');
$cache_contexts = [
'languages:language_interface',
'theme',
'url.site',
'user.node_grants:view',
'user.permissions',
'timezone',
];
$this->assertCacheContexts($cache_contexts);
$cache_context_tags = \Drupal::service('cache_contexts_manager')->convertTokensToKeys($cache_contexts)->getCacheTags();
$this->assertCacheTags(Cache::mergeTags($cache_context_tags, [
'config:views.view.frontpage',
'node:1', 'node_list',
'node_view',
'user:3',
]));
$raw = '<comments>' . $this->node->toUrl('canonical', ['fragment' => 'comments', 'absolute' => TRUE])->toString() . '</comments>';
$this->assertSession()->responseContains($raw);
// Hide comments from RSS feed and check presence.
$this->node->set('comment', CommentItemInterface::HIDDEN);
$this->node->save();
$this->drupalGet('rss.xml');
$this->assertSession()->responseNotContains($raw);
}
}

View File

@@ -0,0 +1,140 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\CommentInterface;
use Drupal\comment\CommentManagerInterface;
use Drupal\comment\Entity\Comment;
/**
* Tests comment statistics on nodes.
*
* @group comment
*/
class CommentStatisticsTest extends CommentTestBase {
/**
* A secondary user for posting comments.
*
* @var \Drupal\user\UserInterface
*/
protected $webUser2;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Add more permissions the admin user.
$this->adminUser->addRole($this->drupalCreateRole([
'administer permissions',
'access administration pages',
'administer site configuration',
]))->save();
// Create a second user to post comments.
$this->webUser2 = $this->drupalCreateUser([
'post comments',
'create article content',
'edit own comments',
'skip comment approval',
'access comments',
'access content',
]);
}
/**
* Tests the node comment statistics.
*/
public function testCommentNodeCommentStatistics(): void {
$node_storage = $this->container->get('entity_type.manager')->getStorage('node');
// Set comments to have subject and preview disabled.
$this->setCommentPreview(DRUPAL_DISABLED);
$this->setCommentForm(TRUE);
$this->setCommentSubject(FALSE);
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
// Checks the initial values of node comment statistics with no comment.
$node = $node_storage->load($this->node->id());
$this->assertEquals($this->node->getCreatedTime(), $node->get('comment')->last_comment_timestamp, 'The initial value of node last_comment_timestamp is the node created date.');
$this->assertNull($node->get('comment')->last_comment_name, 'The initial value of node last_comment_name is NULL.');
$this->assertEquals($this->webUser->id(), $node->get('comment')->last_comment_uid, 'The initial value of node last_comment_uid is the node uid.');
$this->assertEquals(0, $node->get('comment')->comment_count, 'The initial value of node comment_count is zero.');
// Post comment #1 as web_user2.
$this->drupalLogin($this->webUser2);
$comment_text = $this->randomMachineName();
$this->postComment($this->node, $comment_text);
// Checks the new values of node comment statistics with comment #1.
// The node cache needs to be reset before reload.
$node_storage->resetCache([$this->node->id()]);
$node = $node_storage->load($this->node->id());
$this->assertSame('', $node->get('comment')->last_comment_name, 'The value of node last_comment_name should be an empty string.');
$this->assertEquals($this->webUser2->id(), $node->get('comment')->last_comment_uid, 'The value of node last_comment_uid is the comment #1 uid.');
$this->assertEquals(1, $node->get('comment')->comment_count, 'The value of node comment_count is 1.');
$this->drupalLogout();
// Prepare for anonymous comment submission (comment approval enabled).
// Note we don't use user_role_change_permissions(), because that caused
// random test failures.
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/people/permissions');
$edit = [
'anonymous[access comments]' => 1,
'anonymous[post comments]' => 1,
'anonymous[skip comment approval]' => 0,
];
$this->submitForm($edit, 'Save permissions');
$this->drupalLogout();
// Ensure that the poster can leave some contact info.
$this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAY_CONTACT);
// Post comment #2 as anonymous (comment approval enabled).
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), '', TRUE);
// Checks the new values of node comment statistics with comment #2 and
// ensure they haven't changed since the comment has not been moderated.
// The node needs to be reloaded with the cache reset.
$node_storage->resetCache([$this->node->id()]);
$node = $node_storage->load($this->node->id());
$this->assertSame('', $node->get('comment')->last_comment_name, 'The value of node last_comment_name should be an empty string.');
$this->assertEquals($this->webUser2->id(), $node->get('comment')->last_comment_uid, 'The value of node last_comment_uid is still the comment #1 uid.');
$this->assertEquals(1, $node->get('comment')->comment_count, 'The value of node comment_count is still 1.');
// Prepare for anonymous comment submission (no approval required).
// Note we don't use user_role_change_permissions(), because that caused
// random test failures.
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/people/permissions');
$edit = [
'anonymous[skip comment approval]' => 1,
];
$this->submitForm($edit, 'Save permissions');
$this->drupalLogout();
// Post comment #3 as anonymous.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), '', ['name' => $this->randomMachineName()]);
$comment_loaded = Comment::load($anonymous_comment->id());
// Checks the new values of node comment statistics with comment #3.
// The node needs to be reloaded with the cache reset.
$node_storage->resetCache([$this->node->id()]);
$node = $node_storage->load($this->node->id());
$this->assertEquals($comment_loaded->getAuthorName(), $node->get('comment')->last_comment_name, 'The value of node last_comment_name is the name of the anonymous user.');
$this->assertEquals(0, $node->get('comment')->last_comment_uid, 'The value of node last_comment_uid is zero.');
$this->assertEquals(2, $node->get('comment')->comment_count, 'The value of node comment_count is 2.');
}
}

View File

@@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
/**
* Tests comment status field access.
*
* @group comment
*/
class CommentStatusFieldAccessTest extends BrowserTestBase {
use CommentTestTrait;
/**
* {@inheritdoc}
*/
public $profile = 'testing';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Comment admin.
*
* @var \Drupal\user\UserInterface
*/
protected $commentAdmin;
/**
* Node author.
*
* @var \Drupal\user\UserInterface
*/
protected $nodeAuthor;
/**
* {@inheritdoc}
*/
protected static $modules = [
'node',
'comment',
'user',
'system',
'text',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$node_type = NodeType::create([
'type' => 'article',
'name' => 'Article',
]);
$node_type->save();
$this->nodeAuthor = $this->drupalCreateUser([
'create article content',
'skip comment approval',
'post comments',
'edit own comments',
'access comments',
'administer nodes',
]);
$this->commentAdmin = $this->drupalCreateUser([
'administer comments',
'create article content',
'edit own comments',
'skip comment approval',
'post comments',
'access comments',
'administer nodes',
]);
$this->addDefaultCommentField('node', 'article');
}
/**
* Tests comment status field access.
*/
public function testCommentStatusFieldAccessStatus(): void {
$this->drupalLogin($this->nodeAuthor);
$this->drupalGet('node/add/article');
$assert = $this->assertSession();
$assert->fieldNotExists('comment[0][status]');
$this->submitForm(['title[0][value]' => 'Node 1'], 'Save');
$assert->fieldExists('subject[0][value]');
$this->drupalLogin($this->commentAdmin);
$this->drupalGet('node/add/article');
$assert->fieldExists('comment[0][status]');
$this->submitForm(['title[0][value]' => 'Node 2'], 'Save');
$assert->fieldExists('subject[0][value]');
}
}

View File

@@ -0,0 +1,418 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\comment\Entity\CommentType;
use Drupal\comment\Entity\Comment;
use Drupal\comment\CommentInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
/**
* Provides setup and helper methods for comment tests.
*/
abstract class CommentTestBase extends BrowserTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = [
'block',
'comment',
'node',
'history',
'field_ui',
'datetime',
];
/**
* An administrative user with permission to configure comment settings.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* A normal user with permission to post comments.
*
* @var \Drupal\user\UserInterface
*/
protected $webUser;
/**
* A test node to which comments will be posted.
*
* @var \Drupal\node\NodeInterface
*/
protected $node;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create an article content type only if it does not yet exist, so that
// child classes may specify the standard profile.
$types = NodeType::loadMultiple();
if (empty($types['article'])) {
$this->drupalCreateContentType(['type' => 'article', 'name' => t('Article')]);
}
// Create two test users.
$this->adminUser = $this->drupalCreateUser([
'administer content types',
'administer comments',
'administer comment types',
'administer comment fields',
'administer comment display',
'skip comment approval',
'post comments',
'access comments',
// Usernames aren't shown in comment edit form autocomplete unless this
// permission is granted.
'access user profiles',
'access content',
]);
$this->webUser = $this->drupalCreateUser([
'access comments',
'post comments',
'create article content',
'edit own comments',
'skip comment approval',
'access content',
]);
// Create comment field on article.
$this->addDefaultCommentField('node', 'article');
// Create a test node authored by the web user.
$this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
$this->drupalPlaceBlock('local_tasks_block');
}
/**
* Posts a comment.
*
* @param \Drupal\Core\Entity\EntityInterface|null $entity
* Node to post comment on or NULL to post to the previously loaded page.
* @param string $comment
* Comment body.
* @param string $subject
* Comment subject.
* @param string $contact
* Set to NULL for no contact info, TRUE to ignore success checking, and
* array of values to set contact info.
* @param string $field_name
* (optional) Field name through which the comment should be posted.
* Defaults to 'comment'.
*
* @return \Drupal\comment\CommentInterface|null
* The posted comment or NULL when posted comment was not found.
*/
public function postComment($entity, $comment, $subject = '', $contact = NULL, $field_name = 'comment') {
$edit = [];
$edit['comment_body[0][value]'] = $comment;
if ($entity !== NULL) {
$field = FieldConfig::loadByName($entity->getEntityTypeId(), $entity->bundle(), $field_name);
}
else {
$field = FieldConfig::loadByName('node', 'article', $field_name);
}
$preview_mode = $field->getSetting('preview');
// Must get the page before we test for fields.
if ($entity !== NULL) {
$this->drupalGet('comment/reply/' . $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $field_name);
}
// Determine the visibility of subject form field.
$display_repository = $this->container->get('entity_display.repository');
if ($display_repository->getFormDisplay('comment', 'comment')->getComponent('subject')) {
// Subject input allowed.
$edit['subject[0][value]'] = $subject;
}
else {
$this->assertSession()->fieldNotExists('subject[0][value]');
}
if ($contact !== NULL && is_array($contact)) {
$edit += $contact;
}
switch ($preview_mode) {
case DRUPAL_REQUIRED:
// Preview required so no save button should be found.
$this->assertSession()->buttonNotExists('Save');
$this->submitForm($edit, 'Preview');
// Don't break here so that we can test post-preview field presence and
// function below.
case DRUPAL_OPTIONAL:
$this->assertSession()->buttonExists('Preview');
$this->assertSession()->buttonExists('Save');
$this->submitForm($edit, 'Save');
break;
case DRUPAL_DISABLED:
$this->assertSession()->buttonNotExists('Preview');
$this->assertSession()->buttonExists('Save');
$this->submitForm($edit, 'Save');
break;
}
$match = [];
// Get comment ID
preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
// Get comment.
if ($contact !== TRUE) {
// If true then attempting to find error message.
if ($subject) {
$this->assertSession()->pageTextContains($subject);
}
$this->assertSession()->pageTextContains($comment);
// Check the comment ID was extracted.
$this->assertArrayHasKey(1, $match);
}
if (isset($match[1])) {
\Drupal::entityTypeManager()->getStorage('comment')->resetCache([$match[1]]);
return Comment::load($match[1]);
}
}
/**
* Checks current page for specified comment.
*
* @param \Drupal\comment\CommentInterface $comment
* The comment object.
* @param bool $reply
* Boolean indicating whether the comment is a reply to another comment.
*
* @return bool
* Boolean indicating whether the comment was found.
*/
public function commentExists(?CommentInterface $comment = NULL, $reply = FALSE) {
if ($comment) {
$comment_element = $this->cssSelect(($reply ? '.indented ' : '') . 'article#comment-' . $comment->id());
if (empty($comment_element)) {
return FALSE;
}
$comment_title = $comment_element[0]->find('xpath', 'div/h3/a');
if (empty($comment_title) || $comment_title->getText() !== $comment->getSubject()) {
return FALSE;
}
$comment_body = $comment_element[0]->find('xpath', 'div/div/p');
if (empty($comment_body) || $comment_body->getText() !== $comment->comment_body->value) {
return FALSE;
}
return TRUE;
}
else {
return FALSE;
}
}
/**
* Deletes a comment.
*
* @param \Drupal\comment\CommentInterface $comment
* Comment to delete.
*/
public function deleteComment(CommentInterface $comment) {
$this->drupalGet('comment/' . $comment->id() . '/delete');
$this->submitForm([], 'Delete');
$this->assertSession()->pageTextContains('The comment and all its replies have been deleted.');
}
/**
* Sets the value governing whether the subject field should be enabled.
*
* @param bool $enabled
* Boolean specifying whether the subject field should be enabled.
*/
public function setCommentSubject($enabled) {
$form_display = $this->container->get('entity_display.repository')
->getFormDisplay('comment', 'comment');
if ($enabled) {
$form_display->setComponent('subject', [
'type' => 'string_textfield',
]);
}
else {
$form_display->removeComponent('subject');
}
$form_display->save();
}
/**
* Sets the value governing the previewing mode for the comment form.
*
* @param int $mode
* The preview mode: DRUPAL_DISABLED, DRUPAL_OPTIONAL or DRUPAL_REQUIRED.
* @param string $field_name
* (optional) Field name through which the comment should be posted.
* Defaults to 'comment'.
*/
public function setCommentPreview($mode, $field_name = 'comment') {
switch ($mode) {
case DRUPAL_DISABLED:
$mode_text = 'disabled';
break;
case DRUPAL_OPTIONAL:
$mode_text = 'optional';
break;
case DRUPAL_REQUIRED:
$mode_text = 'required';
break;
}
$this->setCommentSettings('preview', $mode, new FormattableMarkup('Comment preview @mode_text.', ['@mode_text' => $mode_text]), $field_name);
}
/**
* Sets the value governing whether the comment form is on its own page.
*
* @param bool $enabled
* TRUE if the comment form should be displayed on the same page as the
* comments; FALSE if it should be displayed on its own page.
* @param string $field_name
* (optional) Field name through which the comment should be posted.
* Defaults to 'comment'.
*/
public function setCommentForm($enabled, $field_name = 'comment') {
$this->setCommentSettings('form_location', ($enabled ? CommentItemInterface::FORM_BELOW : CommentItemInterface::FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.', $field_name);
}
/**
* Sets the value governing restrictions on anonymous comments.
*
* @param int $level
* The level of the contact information allowed for anonymous comments:
* - 0: No contact information allowed.
* - 1: Contact information allowed but not required.
* - 2: Contact information required.
*/
public function setCommentAnonymous($level) {
$this->setCommentSettings('anonymous', $level, new FormattableMarkup('Anonymous commenting set to level @level.', ['@level' => $level]));
}
/**
* Sets the value specifying the default number of comments per page.
*
* @param int $number
* Comments per page value.
* @param string $field_name
* (optional) Field name through which the comment should be posted.
* Defaults to 'comment'.
*/
public function setCommentsPerPage($number, $field_name = 'comment') {
$this->setCommentSettings('per_page', $number, new FormattableMarkup('Number of comments per page set to @number.', ['@number' => $number]), $field_name);
}
/**
* Sets a comment settings variable for the article content type.
*
* @param string $name
* Name of variable.
* @param string $value
* Value of variable.
* @param string $message
* Status message to display.
* @param string $field_name
* (optional) Field name through which the comment should be posted.
* Defaults to 'comment'.
*/
public function setCommentSettings($name, $value, $message, $field_name = 'comment') {
$field = FieldConfig::loadByName('node', 'article', $field_name);
$field->setSetting($name, $value);
$field->save();
}
/**
* Checks whether the commenter's contact information is displayed.
*
* @return bool
* Contact info is available.
*/
public function commentContactInfoAvailable() {
return (bool) preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->getSession()->getPage()->getContent());
}
/**
* Performs the specified operation on the specified comment.
*
* @param \Drupal\comment\CommentInterface $comment
* Comment to perform operation on.
* @param string $operation
* Operation to perform.
* @param bool $approval
* Operation is found on approval page.
*/
public function performCommentOperation(CommentInterface $comment, $operation, $approval = FALSE) {
$edit = [];
$edit['operation'] = $operation;
$edit['comments[' . $comment->id() . ']'] = TRUE;
$this->drupalGet('admin/content/comment' . ($approval ? '/approval' : ''));
$this->submitForm($edit, 'Update');
if ($operation == 'delete') {
$this->submitForm([], 'Delete');
$this->assertSession()->pageTextContains('Deleted 1 comment.');
}
else {
$this->assertSession()->pageTextContains('The update has been performed.');
}
}
/**
* Gets the comment ID for an unapproved comment.
*
* @param string $subject
* Comment subject to find.
*
* @return int
* Comment id.
*/
public function getUnapprovedComment($subject) {
$this->drupalGet('admin/content/comment/approval');
preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->getSession()->getPage()->getContent(), $match);
return $match[2];
}
/**
* Creates a comment type (bundle).
*
* @param string $label
* The comment type label.
*
* @return \Drupal\comment\Entity\CommentType
* Created comment type.
*/
protected function createCommentType($label) {
$bundle = CommentType::create([
'id' => $label,
'label' => $label,
'description' => '',
'target_entity_type_id' => 'node',
]);
$bundle->save();
return $bundle;
}
}

View File

@@ -0,0 +1,172 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\CommentManagerInterface;
/**
* Tests to make sure the comment number increments properly.
*
* @group comment
*/
class CommentThreadingTest extends CommentTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests the comment threading.
*/
public function testCommentThreading(): void {
// Set comments to have a subject with preview disabled.
$this->setCommentPreview(DRUPAL_DISABLED);
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
// Create a node.
$this->drupalLogin($this->webUser);
$this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
// Post comment #1.
$this->drupalLogin($this->webUser);
$subject_text = $this->randomMachineName();
$comment_text = $this->randomMachineName();
$comment1 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
// Confirm that the comment was created and has the correct threading.
$this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
$this->assertEquals('01/', $comment1->getThread());
// Confirm that there is no reference to a parent comment.
$this->assertNoParentLink($comment1->id());
// Post comment #2 following the comment #1 to test if it correctly jumps
// out the indentation in case there is a thread above.
$subject_text = $this->randomMachineName();
$comment_text = $this->randomMachineName();
$this->postComment($this->node, $comment_text, $subject_text, TRUE);
// Reply to comment #1 creating comment #1_3.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1->id());
$comment1_3 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
// Confirm that the comment was created and has the correct threading.
$this->assertTrue($this->commentExists($comment1_3, TRUE), 'Comment #1_3. Reply found.');
$this->assertEquals('01.00/', $comment1_3->getThread());
// Confirm that there is a link to the parent comment.
$this->assertParentLink($comment1_3->id(), $comment1->id());
// Reply to comment #1_3 creating comment #1_3_4.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1_3->id());
$comment1_3_4 = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// Confirm that the comment was created and has the correct threading.
$this->assertTrue($this->commentExists($comment1_3_4, TRUE), 'Comment #1_3_4. Second reply found.');
$this->assertEquals('01.00.00/', $comment1_3_4->getThread());
// Confirm that there is a link to the parent comment.
$this->assertParentLink($comment1_3_4->id(), $comment1_3->id());
// Reply to comment #1 creating comment #1_5.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1->id());
$comment1_5 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
// Confirm that the comment was created and has the correct threading.
$this->assertTrue($this->commentExists($comment1_5), 'Comment #1_5. Third reply found.');
$this->assertEquals('01.01/', $comment1_5->getThread());
// Confirm that there is a link to the parent comment.
$this->assertParentLink($comment1_5->id(), $comment1->id());
// Post comment #3 overall comment #5.
$this->drupalLogin($this->webUser);
$subject_text = $this->randomMachineName();
$comment_text = $this->randomMachineName();
$comment5 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
// Confirm that the comment was created and has the correct threading.
$this->assertTrue($this->commentExists($comment5), 'Comment #5. Second comment found.');
$this->assertEquals('03/', $comment5->getThread());
// Confirm that there is no link to a parent comment.
$this->assertNoParentLink($comment5->id());
// Reply to comment #5 creating comment #5_6.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5->id());
$comment5_6 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
// Confirm that the comment was created and has the correct threading.
$this->assertTrue($this->commentExists($comment5_6, TRUE), 'Comment #6. Reply found.');
$this->assertEquals('03.00/', $comment5_6->getThread());
// Confirm that there is a link to the parent comment.
$this->assertParentLink($comment5_6->id(), $comment5->id());
// Reply to comment #5_6 creating comment #5_6_7.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5_6->id());
$comment5_6_7 = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// Confirm that the comment was created and has the correct threading.
$this->assertTrue($this->commentExists($comment5_6_7, TRUE), 'Comment #5_6_7. Second reply found.');
$this->assertEquals('03.00.00/', $comment5_6_7->getThread());
// Confirm that there is a link to the parent comment.
$this->assertParentLink($comment5_6_7->id(), $comment5_6->id());
// Reply to comment #5 creating comment #5_8.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5->id());
$comment5_8 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
// Confirm that the comment was created and has the correct threading.
$this->assertTrue($this->commentExists($comment5_8), 'Comment #5_8. Third reply found.');
$this->assertEquals('03.01/', $comment5_8->getThread());
// Confirm that there is a link to the parent comment.
$this->assertParentLink($comment5_8->id(), $comment5->id());
}
/**
* Asserts that the link to the specified parent comment is present.
*
* @param string $cid
* The comment ID to check.
* @param string $pid
* The expected parent comment ID.
*
* @internal
*/
protected function assertParentLink(string $cid, string $pid): void {
// This pattern matches a markup structure like:
// @code
// <article id="comment-2">
// <p>
// In reply to
// <a href="...comment-1"></a>
// </p>
// </article>
// @endcode
$pattern = "//article[@id='comment-$cid']//p/a[contains(@href, 'comment-$pid')]";
$this->assertSession()->elementExists('xpath', $pattern);
// A parent link is always accompanied by the text "In reply to".
// If we don't assert this text here, then the assertNoParentLink()
// method is not effective.
$pattern = "//article[@id='comment-$cid']";
$this->assertSession()->elementTextContains('xpath', $pattern, 'In reply to');
}
/**
* Asserts that the specified comment does not have a link to a parent.
*
* @param string $cid
* The comment ID to check.
*
* @internal
*/
protected function assertNoParentLink(string $cid): void {
$pattern = "//article[@id='comment-$cid']";
// A parent link is always accompanied by the text "In reply to".
$this->assertSession()->elementTextNotContains('xpath', $pattern, 'In reply to');
}
}

View File

@@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
/**
* Tests that appropriate and accessible markup is created for comment titles.
*
* @group comment
*/
class CommentTitleTest extends CommentTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests markup for comments with empty titles.
*/
public function testCommentEmptyTitles(): void {
// Create a node.
$this->drupalLogin($this->webUser);
$this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
// Post comment #1 and verify that h3 is rendered.
$subject_text = "Test subject";
$comment_text = "Test comment";
$this->postComment($this->node, $comment_text, $subject_text, TRUE);
// Tests that markup is generated for the comment title.
$regex_h3 = '|<h3[^>]*>.*?</h3>|';
$this->assertSession()->responseMatches($regex_h3);
// Installs module that sets comment title to an empty string.
\Drupal::service('module_installer')->install(['comment_empty_title_test']);
// Set comments to have a subject with preview disabled.
$this->setCommentPreview(DRUPAL_DISABLED);
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
// Create a new node.
$this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
// Post another comment and verify that h3 is not rendered.
$subject_text = $this->randomMachineName();
$comment_text = $this->randomMachineName();
$comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
// The entity fields for name and mail have no meaning if the user is not
// Anonymous.
$this->assertNull($comment->name->value);
$this->assertNull($comment->mail->value);
// Confirm that the comment was created.
$regex = '/<article(.*?)id="comment-' . $comment->id() . '"(.*?)';
$regex .= $comment->comment_body->value . '(.*?)';
$regex .= '/s';
// Verify that the comment is created successfully.
$this->assertSession()->responseMatches($regex);
// Tests that markup is not generated for the comment title.
$this->assertSession()->responseNotMatches($regex_h3);
$this->assertSession()->pageTextNotContains($subject_text);
}
/**
* Tests markup for comments with populated titles.
*/
public function testCommentPopulatedTitles(): void {
// Set comments to have a subject with preview disabled.
$this->setCommentPreview(DRUPAL_DISABLED);
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
// Create a node.
$this->drupalLogin($this->webUser);
$this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
// Post comment #1 and verify that title is rendered in h3.
$subject_text = $this->randomMachineName();
$comment_text = $this->randomMachineName();
$comment1 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
// The entity fields for name and mail have no meaning if the user is not
// Anonymous.
$this->assertNull($comment1->name->value);
$this->assertNull($comment1->mail->value);
// Confirm that the comment was created.
$this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
// Tests that markup is created for comment with heading.
$this->assertSession()->responseMatches('|<h3[^>]*><a[^>]*>' . $subject_text . '</a></h3>|');
// Tests that the comment's title link is the permalink of the comment.
$comment_permalink = $this->cssSelect('.permalink');
$comment_permalink = $comment_permalink[0]->getAttribute('href');
// Tests that the comment's title link contains the URL fragment.
$this->assertStringContainsString('#comment-' . $comment1->id(), $comment_permalink, "The comment's title link contains the url fragment.");
$this->assertEquals($comment1->permalink()->toString(), $comment_permalink, "The comment's title has the correct link.");
}
}

View File

@@ -0,0 +1,193 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\comment\Entity\Comment;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\user\Entity\User;
/**
* Tests comment token replacement.
*
* @group comment
*/
class CommentTokenReplaceTest extends CommentTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['taxonomy'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Creates a comment, then tests the tokens generated from it.
*/
public function testCommentTokenReplacement(): void {
$token_service = \Drupal::token();
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
$url_options = [
'absolute' => TRUE,
'language' => $language_interface,
];
// Setup vocabulary.
Vocabulary::create([
'vid' => 'tags',
'name' => 'Tags',
])->save();
// Change the title of the admin user.
$this->adminUser->name->value = 'This is a title with some special & > " stuff.';
$this->adminUser->save();
$this->drupalLogin($this->adminUser);
// Set comment variables.
$this->setCommentSubject(TRUE);
// To test hostname token field should be populated.
\Drupal::configFactory()
->getEditable('comment.settings')
->set('log_ip_addresses', TRUE)
->save(TRUE);
// Create a node and a comment.
$node = $this->drupalCreateNode(['type' => 'article', 'title' => '<script>alert("123")</script>']);
$parent_comment = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// Post a reply to the comment.
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $parent_comment->id());
$child_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName());
$comment = Comment::load($child_comment->id());
$comment->setHomepage('http://example.org/');
// Add HTML to ensure that sanitation of some fields tested directly.
$comment->setSubject('<blink>Blinking Comment</blink>');
// Generate and test tokens.
$tests = [];
$tests['[comment:cid]'] = $comment->id();
$tests['[comment:hostname]'] = $comment->getHostname();
$tests['[comment:author]'] = Html::escape($comment->getAuthorName());
$tests['[comment:mail]'] = $this->adminUser->getEmail();
$tests['[comment:homepage]'] = UrlHelper::filterBadProtocol($comment->getHomepage());
$tests['[comment:title]'] = Html::escape($comment->getSubject());
$tests['[comment:body]'] = $comment->comment_body->processed;
$tests['[comment:langcode]'] = $comment->language()->getId();
$tests['[comment:url]'] = $comment->toUrl('canonical', $url_options + ['fragment' => 'comment-' . $comment->id()])->toString();
$tests['[comment:edit-url]'] = $comment->toUrl('edit-form', $url_options)->toString();
$tests['[comment:created]'] = \Drupal::service('date.formatter')->format($comment->getCreatedTime(), 'medium', ['langcode' => $language_interface->getId()]);
$tests['[comment:created:since]'] = \Drupal::service('date.formatter')->formatTimeDiffSince($comment->getCreatedTime(), ['langcode' => $language_interface->getId()]);
$tests['[comment:changed:since]'] = \Drupal::service('date.formatter')->formatTimeDiffSince($comment->getChangedTimeAcrossTranslations(), ['langcode' => $language_interface->getId()]);
$tests['[comment:parent:cid]'] = $comment->hasParentComment() ? $comment->getParentComment()->id() : NULL;
$tests['[comment:parent:title]'] = $parent_comment->getSubject();
$tests['[comment:entity]'] = Html::escape($node->getTitle());
// Test node specific tokens.
$tests['[comment:entity:nid]'] = $comment->getCommentedEntityId();
$tests['[comment:entity:title]'] = Html::escape($node->getTitle());
$tests['[comment:author:uid]'] = $comment->getOwnerId();
$tests['[comment:author:name]'] = Html::escape($this->adminUser->getDisplayName());
$base_bubbleable_metadata = BubbleableMetadata::createFromObject($comment);
$metadata_tests = [];
$metadata_tests['[comment:cid]'] = $base_bubbleable_metadata;
$metadata_tests['[comment:hostname]'] = $base_bubbleable_metadata;
$bubbleable_metadata = clone $base_bubbleable_metadata;
$bubbleable_metadata->addCacheableDependency($this->adminUser);
$metadata_tests['[comment:author]'] = $bubbleable_metadata;
$bubbleable_metadata = clone $base_bubbleable_metadata;
$bubbleable_metadata->addCacheableDependency($this->adminUser);
$metadata_tests['[comment:mail]'] = $bubbleable_metadata;
$metadata_tests['[comment:homepage]'] = $base_bubbleable_metadata;
$metadata_tests['[comment:title]'] = $base_bubbleable_metadata;
$metadata_tests['[comment:body]'] = $base_bubbleable_metadata;
$metadata_tests['[comment:langcode]'] = $base_bubbleable_metadata;
$metadata_tests['[comment:url]'] = $base_bubbleable_metadata;
$metadata_tests['[comment:edit-url]'] = $base_bubbleable_metadata;
$bubbleable_metadata = clone $base_bubbleable_metadata;
$metadata_tests['[comment:created]'] = $bubbleable_metadata->addCacheTags(['rendered']);
$bubbleable_metadata = clone $base_bubbleable_metadata;
$metadata_tests['[comment:created:since]'] = $bubbleable_metadata->setCacheMaxAge(0);
$bubbleable_metadata = clone $base_bubbleable_metadata;
$metadata_tests['[comment:changed:since]'] = $bubbleable_metadata->setCacheMaxAge(0);
$bubbleable_metadata = clone $base_bubbleable_metadata;
$metadata_tests['[comment:parent:cid]'] = $bubbleable_metadata->addCacheTags(['comment:1']);
$metadata_tests['[comment:parent:title]'] = $bubbleable_metadata;
$bubbleable_metadata = clone $base_bubbleable_metadata;
$metadata_tests['[comment:entity]'] = $bubbleable_metadata->addCacheTags(['node:2']);
// Test node specific tokens.
$metadata_tests['[comment:entity:nid]'] = $bubbleable_metadata;
$metadata_tests['[comment:entity:title]'] = $bubbleable_metadata;
$bubbleable_metadata = clone $base_bubbleable_metadata;
$metadata_tests['[comment:author:uid]'] = $bubbleable_metadata->addCacheTags(['user:2']);
$metadata_tests['[comment:author:name]'] = $bubbleable_metadata;
// Test to make sure that we generated something for each token.
$this->assertNotContains(0, array_map('strlen', $tests), 'No empty tokens generated.');
foreach ($tests as $input => $expected) {
$bubbleable_metadata = new BubbleableMetadata();
$output = $token_service->replace($input, ['comment' => $comment], ['langcode' => $language_interface->getId()], $bubbleable_metadata);
$this->assertSame((string) $expected, (string) $output, "Failed test case: {$input}");
$this->assertEquals($metadata_tests[$input], $bubbleable_metadata);
}
// Test anonymous comment author.
$author_name = 'This is a random & " > string';
$comment->setOwnerId(0)->setAuthorName($author_name);
$input = '[comment:author]';
$output = $token_service->replace($input, ['comment' => $comment], ['langcode' => $language_interface->getId()]);
$this->assertSame((string) Html::escape($author_name), (string) $output);
// Add comment field to user and term entities.
$this->addDefaultCommentField('user', 'user', 'comment', CommentItemInterface::OPEN, 'comment_user');
$this->addDefaultCommentField('taxonomy_term', 'tags', 'comment', CommentItemInterface::OPEN, 'comment_term');
// Create a user and a comment.
$user = User::create(['name' => 'alice']);
$user->activate();
$user->save();
$this->postComment($user, 'user body', 'user subject', TRUE);
// Create a term and a comment.
$term = Term::create([
'vid' => 'tags',
'name' => 'term',
]);
$term->save();
$this->postComment($term, 'term body', 'term subject', TRUE);
// Load node, user and term again so comment_count gets computed.
$node = Node::load($node->id());
$user = User::load($user->id());
$term = Term::load($term->id());
// Generate comment tokens for node (it has 2 comments, both new),
// user and term.
$tests = [];
$tests['[entity:comment-count]'] = 2;
$tests['[entity:comment-count-new]'] = 2;
$tests['[node:comment-count]'] = 2;
$tests['[node:comment-count-new]'] = 2;
$tests['[user:comment-count]'] = 1;
$tests['[user:comment-count-new]'] = 1;
$tests['[term:comment-count]'] = 1;
$tests['[term:comment-count-new]'] = 1;
foreach ($tests as $input => $expected) {
$output = $token_service->replace($input, ['entity' => $node, 'node' => $node, 'user' => $user, 'term' => $term], ['langcode' => $language_interface->getId()]);
$this->assertSame((string) $expected, (string) $output, "Failed test case: {$input}");
}
}
}

View File

@@ -0,0 +1,242 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase;
/**
* Tests the Comment Translation UI.
*
* @group comment
*/
class CommentTranslationUITest extends ContentTranslationUITestBase {
use CommentTestTrait;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* The subject of the test comment.
*
* @var string
*/
protected $subject;
/**
* An administrative user with permission to administer comments.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
protected $defaultCacheContexts = [
'languages:language_interface',
'session',
'theme',
'timezone',
'url.query_args:_wrapper_format',
'url.query_args.pagers:0',
'url.site',
'user.permissions',
'user.roles',
];
/**
* Modules to install.
*
* @var array
*/
protected static $modules = [
'language',
'content_translation',
'node',
'comment',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
$this->entityTypeId = 'comment';
$this->bundle = 'comment_article';
$this->testLanguageSelector = FALSE;
$this->subject = $this->randomMachineName();
parent::setUp();
$this->doSetup();
}
/**
* {@inheritdoc}
*/
public function setupBundle() {
parent::setupBundle();
$this->drupalCreateContentType(['type' => 'article', 'name' => 'article']);
// Add a comment field to the article content type.
$this->addDefaultCommentField('node', 'article', 'comment_article', CommentItemInterface::OPEN, 'comment_article');
// Create a page content type.
$this->drupalCreateContentType(['type' => 'page', 'name' => 'page']);
// Add a comment field to the page content type - this one won't be
// translatable.
$this->addDefaultCommentField('node', 'page', 'comment');
// Mark this bundle as translatable.
$this->container->get('content_translation.manager')->setEnabled('comment', 'comment_article', TRUE);
}
/**
* {@inheritdoc}
*/
protected function getTranslatorPermissions() {
return array_merge(parent::getTranslatorPermissions(), ['post comments', 'administer comments', 'access comments']);
}
/**
* {@inheritdoc}
*/
protected function createEntity($values, $langcode, $comment_type = 'comment_article') {
if ($comment_type == 'comment_article') {
// This is the article node type, with the 'comment_article' field.
$node_type = 'article';
$field_name = 'comment_article';
}
else {
// This is the page node type with the non-translatable 'comment' field.
$node_type = 'page';
$field_name = 'comment';
}
$node = $this->drupalCreateNode([
'type' => $node_type,
$field_name => [
['status' => CommentItemInterface::OPEN],
],
]);
$values['entity_id'] = $node->id();
$values['entity_type'] = 'node';
$values['field_name'] = $field_name;
$values['uid'] = $node->getOwnerId();
return parent::createEntity($values, $langcode, $comment_type);
}
/**
* {@inheritdoc}
*/
protected function getNewEntityValues($langcode) {
// Comment subject is not translatable hence we use a fixed value.
return [
'subject' => [['value' => $this->subject]],
'comment_body' => [['value' => $this->randomMachineName(16)]],
] + parent::getNewEntityValues($langcode);
}
/**
* {@inheritdoc}
*/
protected function doTestPublishedStatus() {
$entity_type_manager = \Drupal::entityTypeManager();
$storage = $entity_type_manager->getStorage($this->entityTypeId);
$storage->resetCache();
$entity = $storage->load($this->entityId);
// Unpublish translations.
foreach ($this->langcodes as $index => $langcode) {
if ($index > 0) {
$edit = ['status' => 0];
$url = $entity->toUrl('edit-form', ['language' => ConfigurableLanguage::load($langcode)]);
$this->drupalGet($url);
$this->submitForm($edit, $this->getFormSubmitAction($entity, $langcode));
$storage->resetCache();
$entity = $storage->load($this->entityId);
$this->assertFalse($this->manager->getTranslationMetadata($entity->getTranslation($langcode))->isPublished(), 'The translation has been correctly unpublished.');
}
}
}
/**
* {@inheritdoc}
*/
protected function doTestAuthoringInfo() {
$storage = $this->container->get('entity_type.manager')
->getStorage($this->entityTypeId);
$storage->resetCache([$this->entityId]);
$entity = $storage->load($this->entityId);
$languages = $this->container->get('language_manager')->getLanguages();
$values = [];
// Post different authoring information for each translation.
foreach ($this->langcodes as $langcode) {
$url = $entity->toUrl('edit-form', ['language' => $languages[$langcode]]);
$user = $this->drupalCreateUser();
$values[$langcode] = [
'uid' => $user->id(),
'created' => \Drupal::time()->getRequestTime() - mt_rand(0, 1000),
];
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = $this->container->get('date.formatter');
$edit = [
'uid' => $user->getAccountName() . ' (' . $user->id() . ')',
'date[date]' => $date_formatter->format($values[$langcode]['created'], 'custom', 'Y-m-d'),
'date[time]' => $date_formatter->format($values[$langcode]['created'], 'custom', 'H:i:s'),
];
$this->drupalGet($url);
$this->submitForm($edit, $this->getFormSubmitAction($entity, $langcode));
}
$storage->resetCache([$this->entityId]);
$entity = $storage->load($this->entityId);
foreach ($this->langcodes as $langcode) {
$metadata = $this->manager->getTranslationMetadata($entity->getTranslation($langcode));
$this->assertEquals($values[$langcode]['uid'], $metadata->getAuthor()->id(), 'Translation author correctly stored.');
$this->assertEquals($values[$langcode]['created'], $metadata->getCreatedTime(), 'Translation date correctly stored.');
}
}
/**
* Tests translate link on comment content admin page.
*/
public function testTranslateLinkCommentAdminPage(): void {
$this->adminUser = $this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), ['access administration pages', 'administer comments', 'skip comment approval']));
$this->drupalLogin($this->adminUser);
$cid_translatable = $this->createEntity([], $this->langcodes[0]);
$cid_untranslatable = $this->createEntity([], $this->langcodes[0], 'comment');
// Verify translation links.
$this->drupalGet('admin/content/comment');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->linkByHrefExists('comment/' . $cid_translatable . '/translations');
$this->assertSession()->linkByHrefNotExists('comment/' . $cid_untranslatable . '/translations');
}
/**
* {@inheritdoc}
*/
protected function doTestTranslationEdit() {
$storage = $this->container->get('entity_type.manager')
->getStorage($this->entityTypeId);
$storage->resetCache([$this->entityId]);
$entity = $storage->load($this->entityId);
$languages = $this->container->get('language_manager')->getLanguages();
foreach ($this->langcodes as $langcode) {
// We only want to test the title for non-english translations.
if ($langcode != 'en') {
$options = ['language' => $languages[$langcode]];
$url = $entity->toUrl('edit-form', $options);
$this->drupalGet($url);
$this->assertSession()->pageTextContains("Edit {$this->entityTypeId} {$entity->getTranslation($langcode)->label()} [{$languages[$langcode]->getName()} translation]");
}
}
}
}

View File

@@ -0,0 +1,214 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\Core\Url;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Entity\CommentType;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\Node;
/**
* Ensures that comment type functions work correctly.
*
* @group comment
*/
class CommentTypeTest extends CommentTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Admin user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $adminUser;
/**
* Permissions to grant admin user.
*
* @var array
*/
protected $permissions = [
'administer comments',
'administer comment fields',
'administer comment types',
];
/**
* Sets the test up.
*/
protected function setUp(): void {
parent::setUp();
$this->drupalPlaceBlock('page_title_block');
$this->drupalPlaceBlock('system_breadcrumb_block');
$this->adminUser = $this->drupalCreateUser($this->permissions);
}
/**
* Tests creating a comment type programmatically and via a form.
*/
public function testCommentTypeCreation(): void {
// Create a comment type programmatically.
$type = $this->createCommentType('other');
$comment_type = CommentType::load('other');
$this->assertInstanceOf(CommentType::class, $comment_type);
// Log in a test user.
$this->drupalLogin($this->adminUser);
// Ensure that the new comment type admin page can be accessed.
$this->drupalGet('admin/structure/comment/manage/' . $type->id());
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->elementTextEquals('css', 'h1', "Edit {$comment_type->label()} comment type");
// Create a comment type via the user interface.
$edit = [
'id' => 'foo',
'label' => 'title for foo',
'description' => '',
];
$this->drupalGet('admin/structure/comment/types/add');
// Ensure that target entity type is a required field.
$this->submitForm($edit, 'Save and manage fields');
$this->assertSession()->pageTextContains('Target entity type field is required.');
// Ensure that comment type is saved when target entity type is provided.
$edit['target_entity_type_id'] = 'node';
$this->submitForm($edit, 'Save and manage fields');
$this->assertSession()->pageTextContains('Comment type title for foo has been added.');
// Asserts that form submit redirects to the expected manage fields page.
$this->assertSession()->addressEquals('admin/structure/comment/manage/' . $edit['id'] . '/fields');
// Asserts that the comment type is visible in breadcrumb.
$this->assertTrue($this->assertSession()->elementExists('css', 'nav[role="navigation"]')->hasLink('title for foo'));
$comment_type = CommentType::load('foo');
$this->assertInstanceOf(CommentType::class, $comment_type);
// Check that the comment type was created in site default language.
$default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
$this->assertEquals($default_langcode, $comment_type->language()->getId());
// Edit the comment-type and ensure that we cannot change the entity-type.
$this->drupalGet('admin/structure/comment/manage/foo');
$this->assertSession()->fieldNotExists('target_entity_type_id');
$this->assertSession()->pageTextContains('Target entity type');
// Save the form and ensure the entity-type value is preserved even though
// the field isn't present.
$this->submitForm([], 'Save');
\Drupal::entityTypeManager()->getStorage('comment_type')->resetCache(['foo']);
$comment_type = CommentType::load('foo');
$this->assertEquals('node', $comment_type->getTargetEntityTypeId());
// Ensure that target type is displayed in the comment type list.
$this->drupalGet('admin/structure/comment');
$this->assertSession()->elementExists('xpath', '//td[text() = "Content"]');
}
/**
* Tests editing a comment type using the UI.
*/
public function testCommentTypeEditing(): void {
$this->drupalLogin($this->adminUser);
$field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
$this->assertEquals('Comment', $field->getLabel(), 'Comment body field was found.');
// Change the comment type name.
$this->drupalGet('admin/structure/comment');
$edit = [
'label' => 'Bar',
];
$this->drupalGet('admin/structure/comment/manage/comment');
$this->submitForm($edit, 'Save');
$this->drupalGet('admin/structure/comment');
$this->assertSession()->pageTextContains('Bar');
$this->clickLink('Manage fields');
// Verify that the original machine name was used in the URL.
$this->assertSession()->addressEquals(Url::fromRoute('entity.comment.field_ui_fields', ['comment_type' => 'comment']));
$this->assertCount(1, $this->cssSelect('tr#comment-body'), 'Body field exists.');
// Remove the body field.
$this->drupalGet('admin/structure/comment/manage/comment/fields/comment.comment.comment_body/delete');
$this->submitForm([], 'Delete');
// Resave the settings for this type.
$this->drupalGet('admin/structure/comment/manage/comment');
$this->submitForm([], 'Save');
// Check that the body field doesn't exist.
$this->drupalGet('admin/structure/comment/manage/comment/fields');
$this->assertCount(0, $this->cssSelect('tr#comment-body'), 'Body field does not exist.');
}
/**
* Tests deleting a comment type that still has content.
*/
public function testCommentTypeDeletion(): void {
// Create a comment type programmatically.
$type = $this->createCommentType('foo');
$this->drupalCreateContentType(['type' => 'page']);
$this->addDefaultCommentField('node', 'page', 'foo', CommentItemInterface::OPEN, 'foo');
$field_storage = FieldStorageConfig::loadByName('node', 'foo');
$this->drupalLogin($this->adminUser);
// Create a node.
$node = Node::create([
'type' => 'page',
'title' => 'foo',
]);
$node->save();
// Add a new comment of this type.
$comment = Comment::create([
'comment_type' => 'foo',
'entity_type' => 'node',
'field_name' => 'foo',
'entity_id' => $node->id(),
]);
$comment->save();
// Attempt to delete the comment type, which should not be allowed.
$this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete');
$this->assertSession()->pageTextContains($type->label() . ' is used by 1 comment on your site. You can not remove this comment type until you have removed all of the ' . $type->label() . ' comments.');
$this->assertSession()->pageTextContains('foo is used by the node.foo field on your site. You can not remove this comment type until you have removed the field.');
$this->assertSession()->pageTextNotContains('This action cannot be undone.');
// Delete the comment and the field.
$comment->delete();
$field_storage->delete();
// Attempt to delete the comment type, which should now be allowed.
$this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete');
$this->assertSession()->pageTextContains('Are you sure you want to delete the comment type ' . $type->id() . '?');
$this->assertSession()->pageTextContains('This action cannot be undone.');
// Test exception thrown when re-using an existing comment type.
try {
$this->addDefaultCommentField('comment', 'comment', 'bar');
$this->fail('Exception not thrown.');
}
catch (\InvalidArgumentException $e) {
// Expected exception; just continue testing.
}
// Delete the comment type.
$this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete');
$this->submitForm([], 'Delete');
$this->assertNull(CommentType::load($type->id()), 'Comment type deleted.');
$this->assertSession()->pageTextContains('The comment type ' . $type->label() . ' has been deleted.');
}
}

View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\Tests\system\Functional\Module\GenericModuleTestBase;
/**
* Generic module test for comment.
*
* @group comment
*/
class GenericTest extends GenericModuleTestBase {}

View File

@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Rest;
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
/**
* @group rest
* @group #slow
*/
class CommentJsonAnonTest extends CommentResourceTestBase {
use AnonResourceTestTrait;
/**
* {@inheritdoc}
*/
protected static $format = 'json';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*
* Anonymous users cannot edit their own comments.
*
* @see \Drupal\comment\CommentAccessControlHandler::checkAccess
*
* Therefore we grant them the 'administer comments' permission for the
* purpose of this test.
*
* @see ::setUpAuthorization
*/
protected static $patchProtectedFieldNames = [
'pid' => NULL,
'entity_id' => NULL,
'changed' => NULL,
'thread' => NULL,
'entity_type' => NULL,
'field_name' => NULL,
];
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Rest;
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
/**
* @group rest
* @group #slow
*/
class CommentJsonBasicAuthTest extends CommentResourceTestBase {
use BasicAuthResourceTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['basic_auth'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $format = 'json';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $auth = 'basic_auth';
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Rest;
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
/**
* @group rest
* @group #slow
*/
class CommentJsonCookieTest extends CommentResourceTestBase {
use CookieResourceTestTrait;
/**
* {@inheritdoc}
*/
protected static $format = 'json';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $auth = 'cookie';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
}

View File

@@ -0,0 +1,386 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Rest;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Entity\CommentType;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Cache\Cache;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
use Drupal\user\Entity\User;
use GuzzleHttp\RequestOptions;
abstract class CommentResourceTestBase extends EntityResourceTestBase {
use CommentTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['comment', 'entity_test'];
/**
* {@inheritdoc}
*/
protected static $entityTypeId = 'comment';
/**
* {@inheritdoc}
*/
protected static $patchProtectedFieldNames = [
'status' => "The 'administer comments' permission is required.",
'uid' => "The 'administer comments' permission is required.",
'pid' => NULL,
'entity_id' => NULL,
'name' => "The 'administer comments' permission is required.",
'homepage' => "The 'administer comments' permission is required.",
'created' => "The 'administer comments' permission is required.",
'changed' => NULL,
'thread' => NULL,
'entity_type' => NULL,
'field_name' => NULL,
];
/**
* @var \Drupal\comment\CommentInterface
*/
protected $entity;
/**
* {@inheritdoc}
*/
protected function setUpAuthorization($method) {
switch ($method) {
case 'GET':
$this->grantPermissionsToTestedRole(['access comments', 'view test entity']);
break;
case 'POST':
$this->grantPermissionsToTestedRole(['post comments']);
break;
case 'PATCH':
// Anonymous users are not ever allowed to edit their own comments. To
// be able to test PATCHing comments as the anonymous user, the more
// permissive 'administer comments' permission must be granted.
// @see \Drupal\comment\CommentAccessControlHandler::checkAccess
if (static::$auth) {
$this->grantPermissionsToTestedRole(['edit own comments']);
}
else {
$this->grantPermissionsToTestedRole(['administer comments']);
}
break;
case 'DELETE':
$this->grantPermissionsToTestedRole(['administer comments']);
break;
}
}
/**
* {@inheritdoc}
*/
protected function createEntity() {
// Create a "bar" bundle for the "entity_test" entity type and create.
$bundle = 'bar';
entity_test_create_bundle($bundle, NULL, 'entity_test');
// Create a comment field on this bundle.
$this->addDefaultCommentField('entity_test', 'bar', 'comment');
// Create a "Camelids" test entity that the comment will be assigned to.
$commented_entity = EntityTest::create([
'name' => 'Camelids',
'type' => 'bar',
'comment' => CommentItemInterface::OPEN,
]);
$commented_entity->save();
// Create a "Llama" comment.
$comment = Comment::create([
'comment_body' => [
'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
'format' => 'plain_text',
],
'entity_id' => $commented_entity->id(),
'entity_type' => 'entity_test',
'field_name' => 'comment',
]);
$comment->setSubject('Llama')
->setOwnerId(static::$auth ? $this->account->id() : 0)
->setPublished()
->setCreatedTime(123456789)
->setChangedTime(123456789);
$comment->save();
return $comment;
}
/**
* {@inheritdoc}
*/
protected function getExpectedNormalizedEntity() {
$author = User::load($this->entity->getOwnerId());
return [
'cid' => [
['value' => 1],
],
'uuid' => [
['value' => $this->entity->uuid()],
],
'langcode' => [
[
'value' => 'en',
],
],
'comment_type' => [
[
'target_id' => 'comment',
'target_type' => 'comment_type',
'target_uuid' => CommentType::load('comment')->uuid(),
],
],
'subject' => [
[
'value' => 'Llama',
],
],
'status' => [
[
'value' => TRUE,
],
],
'created' => [
[
'value' => (new \DateTime())->setTimestamp(123456789)->setTimezone(new \DateTimeZone('UTC'))->format(\DateTime::RFC3339),
'format' => \DateTime::RFC3339,
],
],
'changed' => [
[
'value' => (new \DateTime())->setTimestamp((int) $this->entity->getChangedTime())->setTimezone(new \DateTimeZone('UTC'))->format(\DateTime::RFC3339),
'format' => \DateTime::RFC3339,
],
],
'default_langcode' => [
[
'value' => TRUE,
],
],
'uid' => [
[
'target_id' => (int) $author->id(),
'target_type' => 'user',
'target_uuid' => $author->uuid(),
'url' => base_path() . 'user/' . $author->id(),
],
],
'pid' => [],
'entity_type' => [
[
'value' => 'entity_test',
],
],
'entity_id' => [
[
'target_id' => 1,
'target_type' => 'entity_test',
'target_uuid' => EntityTest::load(1)->uuid(),
'url' => base_path() . 'entity_test/1',
],
],
'field_name' => [
[
'value' => 'comment',
],
],
'name' => [],
'homepage' => [],
'thread' => [
[
'value' => '01/',
],
],
'comment_body' => [
[
'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
'format' => 'plain_text',
'processed' => '<p>The name &quot;llama&quot; was adopted by European settlers from native Peruvians.</p>' . "\n",
],
],
];
}
/**
* {@inheritdoc}
*/
protected function getNormalizedPostEntity() {
return [
'comment_type' => [
[
'target_id' => 'comment',
],
],
'entity_type' => [
[
'value' => 'entity_test',
],
],
'entity_id' => [
[
'target_id' => (int) EntityTest::load(1)->id(),
],
],
'field_name' => [
[
'value' => 'comment',
],
],
'subject' => [
[
'value' => 'Drama llama',
],
],
'comment_body' => [
[
'value' => 'Llamas are awesome.',
'format' => 'plain_text',
],
],
];
}
/**
* {@inheritdoc}
*/
protected function getNormalizedPatchEntity() {
return array_diff_key($this->getNormalizedPostEntity(), ['entity_type' => TRUE, 'entity_id' => TRUE, 'field_name' => TRUE]);
}
/**
* {@inheritdoc}
*/
protected function getExpectedCacheTags() {
return Cache::mergeTags(parent::getExpectedCacheTags(), ['config:filter.format.plain_text']);
}
/**
* {@inheritdoc}
*/
protected function getExpectedCacheContexts() {
return Cache::mergeContexts(['languages:language_interface', 'theme'], parent::getExpectedCacheContexts());
}
/**
* Tests POSTing a comment without critical base fields.
*
* Tests with the most minimal normalization possible: the one returned by
* ::getNormalizedPostEntity().
*
* But Comment entities have some very special edge cases:
* - base fields that are not marked as required in
* \Drupal\comment\Entity\Comment::baseFieldDefinitions() yet in fact are
* required.
* - base fields that are marked as required, but yet can still result in
* validation errors other than "missing required field".
*/
public function testPostDxWithoutCriticalBaseFields(): void {
$this->initAuthentication();
$this->provisionEntityResource();
$this->setUpAuthorization('POST');
$url = $this->getEntityResourcePostUrl()->setOption('query', ['_format' => static::$format]);
$request_options = [];
$request_options[RequestOptions::HEADERS]['Accept'] = static::$mimeType;
$request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType;
$request_options = array_merge_recursive($request_options, $this->getAuthenticationRequestOptions('POST'));
// DX: 422 when missing 'entity_type' field.
$request_options[RequestOptions::BODY] = $this->serializer->encode(array_diff_key($this->getNormalizedPostEntity(), ['entity_type' => TRUE]), static::$format);
$response = $this->request('POST', $url, $request_options);
$this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nentity_type: This value should not be null.\n", $response);
// DX: 422 when missing 'entity_id' field.
$request_options[RequestOptions::BODY] = $this->serializer->encode(array_diff_key($this->getNormalizedPostEntity(), ['entity_id' => TRUE]), static::$format);
$response = $this->request('POST', $url, $request_options);
$this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nentity_id: This value should not be null.\n", $response);
// DX: 422 when missing 'field_name' field.
$request_options[RequestOptions::BODY] = $this->serializer->encode(array_diff_key($this->getNormalizedPostEntity(), ['field_name' => TRUE]), static::$format);
$response = $this->request('POST', $url, $request_options);
$this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nfield_name: This value should not be null.\n", $response);
}
/**
* {@inheritdoc}
*/
protected function getExpectedUnauthorizedAccessMessage($method) {
switch ($method) {
case 'GET':
return "The 'access comments' permission is required and the comment must be published.";
case 'POST':
return "The 'post comments' permission is required.";
case 'PATCH':
return "The 'edit own comments' permission is required, the user must be the comment author, and the comment must be published.";
case 'DELETE':
default:
// \Drupal\comment\CommentAccessControlHandler::checkAccess() does not
// specify a reason for not allowing a comment to be deleted.
return '';
}
}
/**
* Tests POSTing a comment with and without 'skip comment approval'.
*/
public function testPostSkipCommentApproval(): void {
$this->initAuthentication();
$this->provisionEntityResource();
$this->setUpAuthorization('POST');
// Create request.
$request_options = [];
$request_options[RequestOptions::HEADERS]['Accept'] = static::$mimeType;
$request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType;
$request_options = array_merge_recursive($request_options, $this->getAuthenticationRequestOptions('POST'));
$request_options[RequestOptions::BODY] = $this->serializer->encode($this->getNormalizedPostEntity(), static::$format);
$url = $this->getEntityResourcePostUrl()->setOption('query', ['_format' => static::$format]);
// Status should be FALSE when posting as anonymous.
$response = $this->request('POST', $url, $request_options);
$unserialized = $this->serializer->deserialize((string) $response->getBody(), get_class($this->entity), static::$format);
$this->assertResourceResponse(201, FALSE, $response);
$this->assertFalse($unserialized->isPublished());
// Make sure the role save below properly invalidates cache tags.
$this->refreshVariables();
// Grant anonymous permission to skip comment approval.
$this->grantPermissionsToTestedRole(['skip comment approval']);
// Status should be TRUE when posting as anonymous and skip comment approval.
$response = $this->request('POST', $url, $request_options);
$unserialized = $this->serializer->deserialize((string) $response->getBody(), get_class($this->entity), static::$format);
$this->assertResourceResponse(201, FALSE, $response);
$this->assertTrue($unserialized->isPublished());
}
/**
* {@inheritdoc}
*/
protected function getExpectedUnauthorizedEntityAccessCacheability($is_authenticated) {
// @see \Drupal\comment\CommentAccessControlHandler::checkAccess()
return parent::getExpectedUnauthorizedEntityAccessCacheability($is_authenticated)
->addCacheTags(['comment:1']);
}
}

View File

@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Rest;
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
/**
* @group rest
*/
class CommentTypeJsonAnonTest extends CommentTypeResourceTestBase {
use AnonResourceTestTrait;
/**
* {@inheritdoc}
*/
protected static $format = 'json';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
}

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Rest;
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
/**
* @group rest
*/
class CommentTypeJsonBasicAuthTest extends CommentTypeResourceTestBase {
use BasicAuthResourceTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['basic_auth'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $format = 'json';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $auth = 'basic_auth';
}

View File

@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Rest;
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
/**
* @group rest
*/
class CommentTypeJsonCookieTest extends CommentTypeResourceTestBase {
use CookieResourceTestTrait;
/**
* {@inheritdoc}
*/
protected static $format = 'json';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $auth = 'cookie';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
}

View File

@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Rest;
use Drupal\comment\Entity\CommentType;
use Drupal\Tests\rest\Functional\EntityResource\ConfigEntityResourceTestBase;
/**
* ResourceTestBase for CommentType entity.
*/
abstract class CommentTypeResourceTestBase extends ConfigEntityResourceTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['node', 'comment'];
/**
* {@inheritdoc}
*/
protected static $entityTypeId = 'comment_type';
/**
* The CommentType entity.
*
* @var \Drupal\comment\CommentTypeInterface
*/
protected $entity;
/**
* {@inheritdoc}
*/
protected function setUpAuthorization($method) {
$this->grantPermissionsToTestedRole(['administer comment types']);
}
/**
* {@inheritdoc}
*/
protected function createEntity() {
// Create a "Camelids" comment type.
$camelids = CommentType::create([
'id' => 'camelids',
'label' => 'Camelids',
'description' => 'Camelids are large, strictly herbivorous animals with slender necks and long legs.',
'target_entity_type_id' => 'node',
]);
$camelids->save();
return $camelids;
}
/**
* {@inheritdoc}
*/
protected function getExpectedNormalizedEntity() {
return [
'dependencies' => [],
'description' => 'Camelids are large, strictly herbivorous animals with slender necks and long legs.',
'id' => 'camelids',
'label' => 'Camelids',
'langcode' => 'en',
'status' => TRUE,
'target_entity_type_id' => 'node',
'uuid' => $this->entity->uuid(),
];
}
/**
* {@inheritdoc}
*/
protected function getNormalizedPostEntity() {
// @todo Update in https://www.drupal.org/node/2300677.
return [];
}
}

View File

@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Rest;
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
use Drupal\Tests\rest\Functional\EntityResource\XmlEntityNormalizationQuirksTrait;
/**
* @group rest
*/
class CommentTypeXmlAnonTest extends CommentTypeResourceTestBase {
use AnonResourceTestTrait;
use XmlEntityNormalizationQuirksTrait;
/**
* {@inheritdoc}
*/
protected static $format = 'xml';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'text/xml; charset=UTF-8';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
}

View File

@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Rest;
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
use Drupal\Tests\rest\Functional\EntityResource\XmlEntityNormalizationQuirksTrait;
/**
* @group rest
*/
class CommentTypeXmlBasicAuthTest extends CommentTypeResourceTestBase {
use BasicAuthResourceTestTrait;
use XmlEntityNormalizationQuirksTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['basic_auth'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $format = 'xml';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'text/xml; charset=UTF-8';
/**
* {@inheritdoc}
*/
protected static $auth = 'basic_auth';
}

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Rest;
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
use Drupal\Tests\rest\Functional\EntityResource\XmlEntityNormalizationQuirksTrait;
/**
* @group rest
*/
class CommentTypeXmlCookieTest extends CommentTypeResourceTestBase {
use CookieResourceTestTrait;
use XmlEntityNormalizationQuirksTrait;
/**
* {@inheritdoc}
*/
protected static $format = 'xml';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'text/xml; charset=UTF-8';
/**
* {@inheritdoc}
*/
protected static $auth = 'cookie';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
}

View File

@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Rest;
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
use Drupal\Tests\rest\Functional\EntityResource\XmlEntityNormalizationQuirksTrait;
/**
* @group rest
* @group #slow
*/
class CommentXmlAnonTest extends CommentResourceTestBase {
use AnonResourceTestTrait;
use XmlEntityNormalizationQuirksTrait;
/**
* {@inheritdoc}
*/
protected static $format = 'xml';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'text/xml; charset=UTF-8';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*
* Anonymous users cannot edit their own comments.
*
* @see \Drupal\comment\CommentAccessControlHandler::checkAccess
*
* Therefore we grant them the 'administer comments' permission for the
* purpose of this test.
*
* @see ::setUpAuthorization
*/
protected static $patchProtectedFieldNames = [
'pid',
'entity_id',
'changed',
'thread',
'entity_type',
'field_name',
];
/**
* {@inheritdoc}
*/
public function testPostDxWithoutCriticalBaseFields(): void {
// Deserialization of the XML format is not supported.
$this->markTestSkipped();
}
/**
* {@inheritdoc}
*/
public function testPostSkipCommentApproval(): void {
// Deserialization of the XML format is not supported.
$this->markTestSkipped();
}
}

View File

@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Rest;
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
use Drupal\Tests\rest\Functional\EntityResource\XmlEntityNormalizationQuirksTrait;
/**
* @group rest
* @group #slow
*/
class CommentXmlBasicAuthTest extends CommentResourceTestBase {
use BasicAuthResourceTestTrait;
use XmlEntityNormalizationQuirksTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['basic_auth'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $format = 'xml';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'text/xml; charset=UTF-8';
/**
* {@inheritdoc}
*/
protected static $auth = 'basic_auth';
/**
* {@inheritdoc}
*/
public function testPostDxWithoutCriticalBaseFields(): void {
// Deserialization of the XML format is not supported.
$this->markTestSkipped();
}
/**
* {@inheritdoc}
*/
public function testPostSkipCommentApproval(): void {
// Deserialization of the XML format is not supported.
$this->markTestSkipped();
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Rest;
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
use Drupal\Tests\rest\Functional\EntityResource\XmlEntityNormalizationQuirksTrait;
/**
* @group rest
* @group #slow
*/
class CommentXmlCookieTest extends CommentResourceTestBase {
use CookieResourceTestTrait;
use XmlEntityNormalizationQuirksTrait;
/**
* {@inheritdoc}
*/
protected static $format = 'xml';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'text/xml; charset=UTF-8';
/**
* {@inheritdoc}
*/
protected static $auth = 'cookie';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
public function testPostDxWithoutCriticalBaseFields(): void {
// Deserialization of the XML format is not supported.
$this->markTestSkipped();
}
/**
* {@inheritdoc}
*/
public function testPostSkipCommentApproval(): void {
// Deserialization of the XML format is not supported.
$this->markTestSkipped();
}
}

View File

@@ -0,0 +1,254 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Views;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\comment\Functional\CommentTestBase as CommentBrowserTestBase;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\user\RoleInterface;
use Drupal\views\Views;
/**
* Tests comment approval functionality.
*
* @group comment
*/
class CommentAdminTest extends CommentBrowserTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $modules = [
'language',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Make the site multilingual to have a working language field handler.
ConfigurableLanguage::create([
'id' => 'es',
'title' => 'Spanish title',
'label' => 'Spanish label',
])->save();
\Drupal::service('module_installer')->install(['views']);
$view = Views::getView('comment');
$view->storage->enable()->save();
\Drupal::service('router.builder')->rebuildIfNeeded();
}
/**
* Tests comment approval functionality through admin/content/comment.
*/
public function testApprovalAdminInterface(): void {
// Set anonymous comments to require approval.
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => TRUE,
'post comments' => TRUE,
'skip comment approval' => FALSE,
]);
$this->drupalPlaceBlock('page_title_block');
$this->drupalLogin($this->adminUser);
// Ensure that doesn't require contact info.
$this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAYNOT_CONTACT);
// Test that the comments page loads correctly when there are no comments.
$this->drupalGet('admin/content/comment');
$this->assertSession()->pageTextContains('No comments available.');
// Assert the expose filters on the admin page.
$this->assertSession()->fieldExists('subject');
$this->assertSession()->fieldExists('author_name');
$this->assertSession()->fieldExists('langcode');
$this->drupalLogout();
// Post anonymous comment without contact info.
$body = $this->getRandomGenerator()->sentences(4);
$subject = Unicode::truncate(trim(Html::decodeEntities(strip_tags($body))), 29, TRUE, TRUE);
$author_name = $this->randomMachineName();
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$this->submitForm([
'name' => $author_name,
'comment_body[0][value]' => $body,
], 'Save');
$this->assertSession()->pageTextContains('Your comment has been queued for review by site administrators and will be published after approval.');
// Get unapproved comment id.
$this->drupalLogin($this->adminUser);
$anonymous_comment4 = $this->getUnapprovedComment($subject);
$anonymous_comment4 = Comment::create([
'cid' => $anonymous_comment4,
'subject' => $subject,
'comment_body' => $body,
'entity_id' => $this->node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
]);
$this->drupalLogout();
$this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
// Approve comment.
$this->drupalLogin($this->adminUser);
$edit = [];
$edit['action'] = 'comment_publish_action';
$edit['comment_bulk_form[0]'] = $anonymous_comment4->id();
$this->drupalGet('admin/content/comment/approval');
$this->submitForm($edit, 'Apply to selected items');
$this->assertSession()->pageTextContains('Publish comment was applied to 1 item.');
$this->drupalLogout();
$this->drupalGet('node/' . $this->node->id());
$this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
// Post 2 anonymous comments without contact info.
$comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// Publish multiple comments in one operation.
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/content/comment/approval');
$this->assertSession()->pageTextContains('Unapproved comments (2)');
// Assert the expose filters on the admin page.
$this->assertSession()->fieldExists('subject');
$this->assertSession()->fieldExists('author_name');
$this->assertSession()->fieldExists('langcode');
$edit = [
"action" => 'comment_publish_action',
"comment_bulk_form[1]" => $comments[0]->id(),
"comment_bulk_form[0]" => $comments[1]->id(),
];
$this->submitForm($edit, 'Apply to selected items');
$this->assertSession()->pageTextContains('Unapproved comments (0)');
// Test message when no comments selected.
$this->drupalGet('admin/content/comment');
$this->submitForm([], 'Apply to selected items');
$this->assertSession()->pageTextContains('Select one or more comments to perform the update on.');
// Test that comment listing shows the correct subject link.
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery('//table/tbody/tr/td/a[contains(@href, :href) and contains(@title, :title) and text()=:text]', [
':href' => $comments[0]->permalink()->toString(),
':title' => Unicode::truncate($comments[0]->get('comment_body')->value, 128),
':text' => $comments[0]->getSubject(),
]));
// Verify that anonymous author name is displayed correctly.
$this->assertSession()->pageTextContains($author_name . ' (not verified)');
// Test that comment listing shows the correct subject link.
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery('//table/tbody/tr/td/a[contains(@href, :href) and contains(@title, :title) and text()=:text]', [
':href' => $anonymous_comment4->permalink()->toString(),
':title' => Unicode::truncate($body, 128),
':text' => $subject,
]));
// Verify that anonymous author name is displayed correctly.
$this->assertSession()->pageTextContains($author_name . ' (not verified)');
// Delete multiple comments in one operation.
$edit = [
'action' => 'comment_delete_action',
"comment_bulk_form[1]" => $comments[0]->id(),
"comment_bulk_form[0]" => $comments[1]->id(),
"comment_bulk_form[2]" => $anonymous_comment4->id(),
];
$this->submitForm($edit, 'Apply to selected items');
$this->assertSession()->pageTextContains('Are you sure you want to delete these comments and all their children?');
$this->submitForm([], 'Delete');
$this->assertSession()->pageTextContains('No comments available.');
// Make sure the label of unpublished node is not visible on listing page.
$this->drupalGet('admin/content/comment');
$this->postComment($this->node, $this->randomMachineName());
$this->drupalLogout();
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/content/comment');
// Verify that comment admin can see the title of a published node.
$this->assertSession()->pageTextContains(Html::escape($this->node->label()));
$this->node->setUnpublished()->save();
$this->assertFalse($this->node->isPublished(), 'Node is unpublished now.');
$this->drupalGet('admin/content/comment');
// Verify that comment admin cannot see the title of an unpublished node.
$this->assertSession()->pageTextNotContains(Html::escape($this->node->label()));
$this->drupalLogout();
$node_access_user = $this->drupalCreateUser([
'administer comments',
'bypass node access',
]);
$this->drupalLogin($node_access_user);
$this->drupalGet('admin/content/comment');
// Verify that comment admin with bypass node access permissions can still
// see the title of a published node.
$this->assertSession()->pageTextContains(Html::escape($this->node->label()));
}
/**
* Tests commented entity label of admin view.
*/
public function testCommentedEntityLabel(): void {
\Drupal::service('module_installer')->install(['block_content']);
\Drupal::service('router.builder')->rebuildIfNeeded();
$bundle = BlockContentType::create([
'id' => 'basic',
'label' => 'basic',
'revision' => FALSE,
]);
$bundle->save();
$block_content = BlockContent::create([
'type' => 'basic',
'label' => 'Some block title',
'info' => 'Test block',
]);
$block_content->save();
// Create comment field on block_content.
$this->addDefaultCommentField('block_content', 'basic', 'block_comment', CommentItemInterface::OPEN, 'block_comment');
$this->drupalLogin($this->webUser);
// Post a comment to node.
$node_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// Post a comment to block content.
$block_content_comment = $this->postComment($block_content, $this->randomMachineName(), $this->randomMachineName(), TRUE, 'block_comment');
$this->drupalLogout();
// Login as admin to test the admin comment page.
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/content/comment');
// Test that comment listing links to comment author.
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery('//table/tbody/tr[1]/td/a[contains(@href, :href) and text()=:text]', [
':href' => $this->webUser->toUrl()->toString(),
':text' => $this->webUser->label(),
]));
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery('//table/tbody/tr[2]/td/a[contains(@href, :href) and text()=:text]', [
':href' => $this->webUser->toUrl()->toString(),
':text' => $this->webUser->label(),
]));
// Admin page contains label of both entities.
$this->assertSession()->pageTextContains(Html::escape($this->node->label()));
$this->assertSession()->pageTextContains(Html::escape($block_content->label()));
// Admin page contains subject of both entities.
$this->assertSession()->pageTextContains(Html::escape($node_comment->label()));
$this->assertSession()->pageTextContains(Html::escape($block_content_comment->label()));
}
}

View File

@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Views;
/**
* Test the "approve_comment" views field for approving comments.
*
* @group comment
*/
class CommentApproveLinkTest extends CommentTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'comment',
'comment_test_views',
'system',
'views',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_comment_schema'];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE, $modules = ['comment_test_views']): void {
parent::setUp($import_test_views, $modules);
}
/**
* Tests that "approve comment" link exists and works as expected.
*/
public function testCommentApproveLink(): void {
$this->drupalLogin($this->drupalCreateUser(['administer comments']));
// Set the comment status to unpublished.
$this->comment->setUnpublished();
$this->comment->save();
$this->drupalGet('/admin/moderate-comments');
$this->assertSession()->pageTextContains($this->comment->getSubject());
$this->assertSession()->linkExists('Approve');
$this->clickLink('Approve');
$this->drupalGet('/admin/moderate-comments');
$this->assertSession()->linkNotExists('Approve');
// Ensure that "published" column in table is marked as yes.
$this->assertSession()->elementTextContains('xpath', "//table/tbody/tr/td[3]", 'Yes');
}
}

View File

@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Views;
use Drupal\Tests\comment\Functional\CommentTestBase as CommentBrowserTestBase;
/**
* Tests comment edit functionality.
*
* @group comment
*/
class CommentEditTest extends CommentBrowserTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests comment label in admin view.
*/
public function testCommentEdit(): void {
$this->drupalLogin($this->adminUser);
// Post a comment to node.
$node_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$this->drupalGet('admin/content/comment');
$this->assertSession()->pageTextContains($this->adminUser->label());
$this->drupalGet($node_comment->toUrl('edit-form'));
$edit = [
'comment_body[0][value]' => $this->randomMachineName(),
];
$this->submitForm($edit, 'Save');
$this->drupalGet('admin/content/comment');
$this->assertSession()->pageTextContains($this->adminUser->label());
}
}

View File

@@ -0,0 +1,132 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Views;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\comment\Entity\Comment;
/**
* Tests comment field filters with translations.
*
* @group comment
*/
class CommentFieldFilterTest extends CommentTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['language'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_field_filters'];
/**
* List of comment titles by language.
*
* @var array
*/
public $commentTitles = [];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE, $modules = ['comment_test_views']): void {
parent::setUp($import_test_views, $modules);
$this->drupalLogin($this->drupalCreateUser(['access comments']));
// Add two new languages.
ConfigurableLanguage::createFromLangcode('fr')->save();
ConfigurableLanguage::createFromLangcode('es')->save();
// Set up comment titles.
$this->commentTitles = [
'en' => 'Food in Paris',
'es' => 'Comida en Paris',
'fr' => 'Nourriture en Paris',
];
// Create a new comment. Using the one created earlier will not work,
// as it predates the language set-up.
$comment = [
'uid' => $this->loggedInUser->id(),
'entity_id' => $this->nodeUserCommented->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'cid' => '',
'pid' => '',
'node_type' => '',
];
$this->comment = Comment::create($comment);
// Add field values and translate the comment.
$this->comment->subject->value = $this->commentTitles['en'];
$this->comment->comment_body->value = $this->commentTitles['en'];
$this->comment->langcode = 'en';
$this->comment->save();
foreach (['es', 'fr'] as $langcode) {
$translation = $this->comment->addTranslation($langcode, []);
$translation->comment_body->value = $this->commentTitles[$langcode];
$translation->subject->value = $this->commentTitles[$langcode];
}
$this->comment->save();
}
/**
* Tests body and title filters.
*/
public function testFilters(): void {
// Test the title filter page, which filters for title contains 'Comida'.
// Should show just the Spanish translation, once.
$this->assertPageCounts('test-title-filter', ['es' => 1, 'fr' => 0, 'en' => 0], 'Comida title filter');
// Test the body filter page, which filters for body contains 'Comida'.
// Should show just the Spanish translation, once.
$this->assertPageCounts('test-body-filter', ['es' => 1, 'fr' => 0, 'en' => 0], 'Comida body filter');
// Test the title Paris filter page, which filters for title contains
// 'Paris'. Should show each translation once.
$this->assertPageCounts('test-title-paris', ['es' => 1, 'fr' => 1, 'en' => 1], 'Paris title filter');
// Test the body Paris filter page, which filters for body contains
// 'Paris'. Should show each translation once.
$this->assertPageCounts('test-body-paris', ['es' => 1, 'fr' => 1, 'en' => 1], 'Paris body filter');
}
/**
* Asserts that the given comment translation counts are correct.
*
* @param string $path
* Path of the page to test.
* @param array $counts
* Array whose keys are languages, and values are the number of times
* that translation should be shown on the given page.
* @param string $message
* Message suffix to display.
*
* @internal
*/
protected function assertPageCounts(string $path, array $counts, string $message): void {
// Get the text of the page.
$this->drupalGet($path);
$text = $this->getTextContent();
// Check the counts. Note that the title and body are both shown on the
// page, and they are the same. So the title/body string should appear on
// the page twice as many times as the input count.
foreach ($counts as $langcode => $count) {
$this->assertEquals(2 * $count, substr_count($text, $this->commentTitles[$langcode]), 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);
}
}
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Views;
/**
* Tests comment operations.
*
* @group comment
*/
class CommentOperationsTest extends CommentTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_comment_operations'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests the operations field plugin.
*/
public function testCommentOperations(): void {
$admin_account = $this->drupalCreateUser(['administer comments']);
$this->drupalLogin($admin_account);
$this->drupalGet('test-comment-operations');
$this->assertSession()->statusCodeEquals(200);
// Assert Edit operation is present.
$this->assertSession()->elementsCount('xpath', '//td[contains(@class, "views-field-operations")]//li/a[text() = "Edit"]', 1);
// Assert Delete operation is present.
$this->assertSession()->elementsCount('xpath', '//td[contains(@class, "views-field-operations")]//li/a[text() = "Delete"]', 1);
}
}

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Views;
/**
* Tests the comment row plugin.
*
* @group comment
*/
class CommentRowTest extends CommentTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_comment_row'];
/**
* Tests comment row.
*/
public function testCommentRow(): void {
$this->drupalGet('test-comment-row');
$this->assertSession()->elementsCount('xpath', '//article[contains(@class, "comment")]', 1);
}
}

View File

@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Views;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\comment\Entity\Comment;
/**
* Provides setup and helper methods for comment views tests.
*/
abstract class CommentTestBase extends ViewTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = ['node', 'comment', 'comment_test_views'];
/**
* A normal user with permission to post comments (without approval).
*
* @var \Drupal\user\UserInterface
*/
protected $account;
/**
* A second normal user that will author a node for $account to comment on.
*
* @var \Drupal\user\UserInterface
*/
protected $account2;
/**
* Stores a node posted by the user created as $account.
*
* @var \Drupal\node\NodeInterface
*/
protected $nodeUserPosted;
/**
* Stores a node posted by the user created as $account2.
*
* @var \Drupal\node\NodeInterface
*/
protected $nodeUserCommented;
/**
* Stores a comment used by the tests.
*
* @var \Drupal\comment\Entity\Comment
*/
protected $comment;
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE, $modules = ['comment_test_views']): void {
parent::setUp($import_test_views, $modules);
// Add two users, create a node with the user1 as author and another node
// with user2 as author. For the second node add a comment from user1.
$this->account = $this->drupalCreateUser(['skip comment approval']);
$this->account2 = $this->drupalCreateUser();
$this->drupalLogin($this->account);
$this->drupalCreateContentType(['type' => 'page', 'name' => t('Basic page')]);
$this->addDefaultCommentField('node', 'page');
$this->nodeUserPosted = $this->drupalCreateNode();
$this->nodeUserCommented = $this->drupalCreateNode(['uid' => $this->account2->id()]);
$comment = [
'uid' => $this->loggedInUser->id(),
'entity_id' => $this->nodeUserCommented->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'subject' => 'How much wood would a woodchuck chuck',
'cid' => '',
'pid' => '',
'mail' => 'someone@example.com',
];
$this->comment = Comment::create($comment);
$this->comment->save();
}
}

View File

@@ -0,0 +1,148 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Views;
use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\views\Views;
use Drupal\Tests\views\Functional\ViewTestBase;
/**
* Tests results for the Recent Comments view shipped with the module.
*
* @group comment
*/
class DefaultViewRecentCommentsTest extends ViewTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = ['node', 'comment', 'block'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Number of results for the Default display.
*
* @var int
*/
protected $defaultDisplayResults = 5;
/**
* Number of results for the Block display.
*
* @var int
*/
protected $blockDisplayResults = 5;
/**
* Number of results for the Page display.
*
* @var int
*/
protected $pageDisplayResults = 5;
/**
* Will hold the comments created for testing.
*
* @var array
*/
protected $commentsCreated = [];
/**
* Contains the node object used for comments of this test.
*
* @var \Drupal\node\NodeInterface
*/
public $node;
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE, $modules = []): void {
parent::setUp($import_test_views, $modules);
// Create a new content type
$content_type = $this->drupalCreateContentType();
// Add a node of the new content type.
$node_data = [
'type' => $content_type->id(),
];
$this->addDefaultCommentField('node', $content_type->id());
$this->node = $this->drupalCreateNode($node_data);
// Force a flush of the in-memory storage.
$this->container->get('views.views_data')->clear();
// Create some comments and attach them to the created node.
for ($i = 0; $i < $this->defaultDisplayResults; $i++) {
/** @var \Drupal\comment\CommentInterface $comment */
$comment = Comment::create([
'status' => CommentInterface::PUBLISHED,
'field_name' => 'comment',
'entity_type' => 'node',
'entity_id' => $this->node->id(),
]);
$comment->setOwnerId(0);
$comment->setSubject('Test comment ' . $i);
$comment->comment_body->value = 'Test body ' . $i;
$comment->comment_body->format = 'full_html';
// Ensure comments are sorted in ascending order.
$time = \Drupal::time()->getRequestTime() + ($this->defaultDisplayResults - $i);
$comment->setCreatedTime($time);
$comment->changed->value = $time;
$comment->save();
}
// Store all the nodes just created to access their properties on the tests.
$this->commentsCreated = Comment::loadMultiple();
// Sort created comments in descending order.
ksort($this->commentsCreated, SORT_NUMERIC);
}
/**
* Tests the block defined by the comments_recent view.
*/
public function testBlockDisplay(): void {
$user = $this->drupalCreateUser(['access comments']);
$this->drupalLogin($user);
$view = Views::getView('comments_recent');
$view->setDisplay('block_1');
$this->executeView($view);
$map = [
'subject' => 'subject',
'cid' => 'cid',
'comment_field_data_created' => 'created',
];
$expected_result = [];
foreach (array_values($this->commentsCreated) as $key => $comment) {
$expected_result[$key]['subject'] = $comment->getSubject();
$expected_result[$key]['cid'] = $comment->id();
$expected_result[$key]['created'] = $comment->getCreatedTime();
}
$this->assertIdenticalResultset($view, $expected_result, $map);
// Check the number of results given by the display is the expected.
$this->assertCount($this->blockDisplayResults, $view->result,
'There are exactly ' . count($view->result) . ' comments. Expected ' . $this->blockDisplayResults
);
}
}

View File

@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Views;
/**
* Tests comments on nodes.
*
* @group comment
*/
class NodeCommentsTest extends CommentTestBase {
/**
* Modules to install.
*
* @var array
*/
protected static $modules = ['history'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_new_comments', 'test_comment_count'];
/**
* Tests the new comments field plugin.
*/
public function testNewComments(): void {
$this->drupalGet('test-new-comments');
$this->assertSession()->statusCodeEquals(200);
$new_comments = $this->cssSelect(".views-field-new-comments a:contains('1')");
$this->assertCount(1, $new_comments, 'Found the number of new comments for a certain node.');
}
/**
* Test the comment count field.
*/
public function testCommentCount(): void {
$this->drupalGet('test-comment-count');
$this->assertSession()->statusCodeEquals(200);
$this->assertCount(2, $this->cssSelect('.views-row'));
$comment_count_with_comment = $this->cssSelect(".views-field-comment-count span:contains('1')");
$this->assertCount(1, $comment_count_with_comment);
$comment_count_without_comment = $this->cssSelect(".views-field-comment-count span:contains('0')");
$this->assertCount(1, $comment_count_without_comment);
// Create a content type with no comment field, and add a node.
$this->drupalCreateContentType(['type' => 'no_comment', 'name' => 'No comment page']);
$this->nodeUserPosted = $this->drupalCreateNode(['type' => 'no_comment']);
$this->drupalGet('test-comment-count');
// Test that the node with no comment field is also shown.
$this->assertSession()->statusCodeEquals(200);
$this->assertCount(3, $this->cssSelect('.views-row'));
$comment_count_with_comment = $this->cssSelect(".views-field-comment-count span:contains('1')");
$this->assertCount(1, $comment_count_with_comment);
$comment_count_without_comment = $this->cssSelect(".views-field-comment-count span:contains('0')");
$this->assertCount(2, $comment_count_without_comment);
}
}

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Views;
/**
* Tests the comment rss row plugin.
*
* @group comment
* @see \Drupal\comment\Plugin\views\row\Rss
*/
class RowRssTest extends CommentTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_comment_rss'];
/**
* Tests comment rss output.
*/
public function testRssRow(): void {
$this->drupalGet('test-comment-rss');
// Because the response is XML we can't use the page which depends on an
// HTML tag being present.
$result = $this->getSession()->getDriver()->find('//item');
$this->assertCount(1, $result, 'Just one comment was found in the rss output.');
$this->assertEquals(gmdate('r', $this->comment->getCreatedTime()), $result[0]->find('xpath', '//pubDate')->getHtml(), 'The right pubDate appears in the rss output.');
}
}

View File

@@ -0,0 +1,105 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Functional\Views;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\views\Views;
use Drupal\Tests\views\Functional\Wizard\WizardTestBase;
/**
* Tests the comment module integration into the wizard.
*
* @group comment
* @see \Drupal\comment\Plugin\views\wizard\Comment
*/
class WizardTest extends WizardTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = ['node', 'comment'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE, $modules = []): void {
parent::setUp($import_test_views, $modules);
$this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
// Add comment field to page node type.
$this->addDefaultCommentField('node', 'page');
}
/**
* Tests adding a view of comments.
*/
public function testCommentWizard(): void {
$view = [];
$view['label'] = $this->randomMachineName(16);
$view['id'] = $this->randomMachineName(16);
$view['show[wizard_key]'] = 'comment';
$view['page[create]'] = TRUE;
$view['page[path]'] = $this->randomMachineName(16);
// Just triggering the saving should automatically choose a proper row
// plugin.
$this->drupalGet('admin/structure/views/add');
$this->submitForm($view, 'Save and edit');
// Verify that the view saving was successful and the browser got redirected
// to the edit page.
$this->assertSession()->addressEquals('admin/structure/views/view/' . $view['id']);
// If we update the type first we should get a selection of comment valid
// row plugins as the select field.
$this->drupalGet('admin/structure/views/add');
$this->drupalGet('admin/structure/views/add');
$this->submitForm($view, 'Update "of type" choice');
// Check for available options of the row plugin.
$expected_options = ['entity:comment', 'fields'];
$items = $this->getSession()->getPage()->findField('page[style][row_plugin]')->findAll('xpath', 'option');
$actual_options = [];
foreach ($items as $item) {
$actual_options[] = $item->getValue();
}
$this->assertEquals($expected_options, $actual_options);
$view['id'] = $this->randomMachineName(16);
$this->submitForm($view, 'Save and edit');
// Verify that the view saving was successful and the browser got redirected
// to the edit page.
$this->assertSession()->addressEquals('admin/structure/views/view/' . $view['id']);
$user = $this->drupalCreateUser(['access comments']);
$this->drupalLogin($user);
$view = Views::getView($view['id']);
$view->initHandlers();
$row = $view->display_handler->getOption('row');
$this->assertEquals('entity:comment', $row['type']);
// Check for the default filters.
$this->assertEquals('comment_field_data', $view->filter['status']->table);
$this->assertEquals('status', $view->filter['status']->field);
$this->assertEquals('1', $view->filter['status']->value);
$this->assertEquals('node_field_data', $view->filter['status_node']->table);
$this->assertEquals('status', $view->filter['status_node']->field);
$this->assertEquals('1', $view->filter['status_node']->value);
// Check for the default fields.
$this->assertEquals('comment_field_data', $view->field['subject']->table);
$this->assertEquals('subject', $view->field['subject']->field);
}
}

View File

@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment_base_field_test\Entity\CommentTestBaseField;
use Drupal\Core\Language\LanguageInterface;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests that comment as a base field.
*
* @group comment
*/
class CommentBaseFieldTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'user',
'comment',
'comment_base_field_test',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('comment_test_base_field');
$this->installEntitySchema('comment');
$this->installEntitySchema('user');
}
/**
* Tests comment as a base field.
*/
public function testCommentBaseField(): void {
// Verify entity creation.
$entity = CommentTestBaseField::create([
'name' => $this->randomMachineName(),
'test_comment' => CommentItemInterface::OPEN,
]);
$entity->save();
$comment = Comment::create([
'entity_id' => $entity->id(),
'entity_type' => 'comment_test_base_field',
'field_name' => 'test_comment',
'pid' => 0,
'uid' => 0,
'status' => CommentInterface::PUBLISHED,
'subject' => $this->randomMachineName(),
'hostname' => '127.0.0.1',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'comment_body' => [['value' => $this->randomMachineName()]],
]);
$comment->save();
$this->assertEquals('test_comment_type', $comment->bundle());
}
}

View File

@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\Entity\CommentType;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests that comment bundles behave as expected.
*
* @group comment
*/
class CommentBundlesTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['comment', 'node', 'taxonomy', 'user'];
/**
* Entity type ids to use for target_entity_type_id on comment bundles.
*
* @var array
*/
protected $targetEntityTypes;
/**
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->entityFieldManager = $this->container->get('entity_field.manager');
$this->installEntitySchema('comment');
// Create multiple comment bundles,
// each of which has a different target entity type.
$this->targetEntityTypes = [
'comment' => 'Comment',
'node' => 'Node',
'taxonomy_term' => 'Taxonomy Term',
];
foreach ($this->targetEntityTypes as $id => $label) {
CommentType::create([
'id' => 'comment_on_' . $id,
'label' => 'Comment on ' . $label,
'target_entity_type_id' => $id,
])->save();
}
}
/**
* Tests that the entity_id field is set correctly for each comment bundle.
*/
public function testEntityIdField(): void {
$field_definitions = [];
foreach (array_keys($this->targetEntityTypes) as $id) {
$bundle = 'comment_on_' . $id;
$field_definitions[$bundle] = $this->entityFieldManager
->getFieldDefinitions('comment', $bundle);
}
// Test that the value of the entity_id field for each bundle is correct.
foreach ($field_definitions as $bundle => $definition) {
$entity_type_id = str_replace('comment_on_', '', $bundle);
$target_type = $definition['entity_id']->getSetting('target_type');
$this->assertEquals($entity_type_id, $target_type);
// Verify that the target type remains correct
// in the deeply-nested object properties.
$nested_target_type = $definition['entity_id']->getItemDefinition()->getFieldDefinition()->getSetting('target_type');
$this->assertEquals($entity_type_id, $nested_target_type);
}
}
}

View File

@@ -0,0 +1,166 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Cache\Cache;
use Drupal\comment\CommentInterface;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Drupal\comment\Entity\Comment;
use Drupal\entity_test\Entity\EntityTest;
/**
* Tests comment cache tag bubbling up when using the Comment list formatter.
*
* @group comment
*/
class CommentDefaultFormatterCacheTagsTest extends EntityKernelTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = ['entity_test', 'comment'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create user 1 so that the user created later in the test has a different
// user ID.
// @todo Remove in https://www.drupal.org/node/540008.
$this->createUser([], NULL, FALSE, ['uid' => 1, 'name' => 'user1'])->save();
$this->container->get('module_handler')->loadInclude('comment', 'install');
comment_install();
$session = new Session();
$request = Request::create('/');
$request->setSession($session);
/** @var \Symfony\Component\HttpFoundation\RequestStack $stack */
$stack = $this->container->get('request_stack');
$stack->pop();
$stack->push($request);
// Set the current user to one that can access comments. Specifically, this
// user does not have access to the 'administer comments' permission, to
// ensure only published comments are visible to the end user.
$current_user = $this->container->get('current_user');
$current_user->setAccount($this->createUser(['access comments', 'post comments']));
// Install tables and config needed to render comments.
$this->installSchema('comment', ['comment_entity_statistics']);
$this->installConfig(['system', 'filter', 'comment']);
// Set up a field, so that the entity that'll be referenced bubbles up a
// cache tag when rendering it entirely.
$this->addDefaultCommentField('entity_test', 'entity_test');
}
/**
* Tests the bubbling of cache tags.
*/
public function testCacheTags(): void {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container->get('renderer');
// Create the entity that will be commented upon.
$commented_entity = EntityTest::create(['name' => $this->randomMachineName()]);
$commented_entity->save();
// Verify cache tags on the rendered entity before it has comments.
$build = \Drupal::entityTypeManager()
->getViewBuilder('entity_test')
->view($commented_entity);
$renderer->renderRoot($build);
$expected_cache_tags = [
'entity_test_view',
'CACHE_MISS_IF_UNCACHEABLE_HTTP_METHOD:form',
'entity_test:' . $commented_entity->id(),
'config:core.entity_form_display.comment.comment.default',
'config:field.field.comment.comment.comment_body',
'config:field.field.entity_test.entity_test.comment',
'config:field.storage.comment.comment_body',
'config:user.settings',
];
$this->assertEqualsCanonicalizing($expected_cache_tags, $build['#cache']['tags']);
// Create a comment on that entity. Comment loading requires that the uid
// also exists in the {users} table.
$user = $this->createUser();
$user->save();
$comment = Comment::create([
'subject' => 'Llama',
'comment_body' => [
'value' => 'Llamas are cool!',
'format' => 'plain_text',
],
'entity_id' => $commented_entity->id(),
'entity_type' => 'entity_test',
'field_name' => 'comment',
'comment_type' => 'comment',
'status' => CommentInterface::PUBLISHED,
'uid' => $user->id(),
]);
$comment->save();
// Load commented entity so comment_count gets computed.
// @todo Remove the $reset = TRUE parameter after
// https://www.drupal.org/node/597236 lands. It's a temporary work-around.
$storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
$storage->resetCache([$commented_entity->id()]);
$commented_entity = $storage->load($commented_entity->id());
// Verify cache tags on the rendered entity when it has comments.
$build = \Drupal::entityTypeManager()
->getViewBuilder('entity_test')
->view($commented_entity);
$renderer->renderRoot($build);
$expected_cache_tags = [
'entity_test_view',
'entity_test:' . $commented_entity->id(),
'comment_view',
'comment:' . $comment->id(),
'config:filter.format.plain_text',
'user_view',
'CACHE_MISS_IF_UNCACHEABLE_HTTP_METHOD:form',
'user:' . $user->id(),
'config:core.entity_form_display.comment.comment.default',
'config:field.field.comment.comment.comment_body',
'config:field.field.entity_test.entity_test.comment',
'config:field.storage.comment.comment_body',
'config:user.settings',
];
$this->assertEqualsCanonicalizing($expected_cache_tags, $build['#cache']['tags']);
// Build a render array with the entity in a sub-element so that lazy
// builder elements bubble up outside of the entity and we can check that
// it got the correct cache max age.
$build = ['#type' => 'container'];
$build['entity'] = \Drupal::entityTypeManager()
->getViewBuilder('entity_test')
->view($commented_entity);
$renderer->renderRoot($build);
// The entity itself was cached but the top-level element is max-age 0 due
// to the bubbled up max age due to the lazy-built comment form.
$this->assertSame(Cache::PERMANENT, $build['entity']['#cache']['max-age']);
$this->assertSame(0, $build['#cache']['max-age'], 'Top level render array has max-age 0');
// The children (fields) of the entity render array are only built in case
// of a cache miss.
$this->assertFalse(isset($build['entity']['comment']), 'Cache hit');
}
}

View File

@@ -0,0 +1,357 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Entity\CommentType;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\Tests\Traits\Core\GeneratePermutationsTrait;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
/**
* Tests comment field level access.
*
* @group comment
* @group Access
*/
class CommentFieldAccessTest extends EntityKernelTestBase {
use CommentTestTrait;
use GeneratePermutationsTrait;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = ['comment', 'entity_test', 'user'];
/**
* Fields that only users with administer comments permissions can change.
*
* @var array
*/
protected $administrativeFields = [
'uid',
'status',
'created',
];
/**
* These fields are automatically managed and can not be changed by any user.
*
* @var array
*/
protected $readOnlyFields = [
'changed',
'hostname',
'cid',
'thread',
];
/**
* These fields can be edited on create only.
*
* @var array
*/
protected $createOnlyFields = [
'uuid',
'pid',
'comment_type',
'entity_id',
'entity_type',
'field_name',
];
/**
* These fields can only be edited by the admin or anonymous users if allowed.
*
* @var array
*/
protected $contactFields = [
'name',
'mail',
'homepage',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['user', 'comment']);
$this->installSchema('comment', ['comment_entity_statistics']);
}
/**
* Tests permissions on comment fields.
*/
public function testAccessToAdministrativeFields(): void {
// Create a comment type.
$comment_type = CommentType::create([
'id' => 'comment',
'label' => 'Default comments',
'description' => 'Default comment field',
'target_entity_type_id' => 'entity_test',
]);
$comment_type->save();
// An administrator user. No user exists yet, ensure that the first user
// does not have UID 1.
$comment_admin_user = $this->createUser([
'administer comments',
'access comments',
], 'admin', FALSE, ['uid' => 2]);
// Two comment enabled users, one with edit access.
$comment_enabled_user = $this->createUser([
'post comments',
'skip comment approval',
'edit own comments',
'access comments',
], 'enabled');
$comment_no_edit_user = $this->createUser([
'post comments',
'skip comment approval',
'access comments',
], 'no edit');
// An unprivileged user.
$comment_disabled_user = $this->createUser(['access content'], 'disabled');
$role = Role::load(RoleInterface::ANONYMOUS_ID);
$role->grantPermission('post comments')
->save();
$anonymous_user = new AnonymousUserSession();
// Add two fields.
$this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
$this->addDefaultCommentField('entity_test', 'entity_test', 'comment_other');
// Create a comment against a test entity.
$host = EntityTest::create();
$host->save();
$host2 = EntityTest::create();
$host2->comment->status = CommentItemInterface::CLOSED;
$host2->comment_other->status = CommentItemInterface::CLOSED;
$host2->save();
// Change the second field's anonymous contact setting.
$instance = FieldConfig::loadByName('entity_test', 'entity_test', 'comment_other');
// Default is 'May not contact', for this field - they may contact.
$instance->setSetting('anonymous', CommentInterface::ANONYMOUS_MAY_CONTACT);
$instance->save();
// Create three "Comments". One is owned by our edit-enabled user.
$comment1 = Comment::create([
'entity_type' => 'entity_test',
'name' => 'Tony',
'hostname' => 'magic.example.com',
'mail' => 'tonythemagicalpony@example.com',
'subject' => 'Bruce the Mesopotamian moose',
'entity_id' => $host->id(),
'comment_type' => 'comment',
'field_name' => 'comment',
'pid' => 0,
'uid' => 0,
'status' => 1,
]);
$comment1->save();
$comment2 = Comment::create([
'entity_type' => 'entity_test',
'hostname' => 'magic.example.com',
'subject' => 'Brian the messed up lion',
'entity_id' => $host->id(),
'comment_type' => 'comment',
'field_name' => 'comment',
'status' => 1,
'pid' => 0,
'uid' => $comment_enabled_user->id(),
]);
$comment2->save();
$comment3 = Comment::create([
'entity_type' => 'entity_test',
'hostname' => 'magic.example.com',
// Unpublished.
'status' => 0,
'subject' => 'Gail the minke whale',
'entity_id' => $host->id(),
'comment_type' => 'comment',
'field_name' => 'comment_other',
'pid' => $comment2->id(),
'uid' => $comment_no_edit_user->id(),
]);
$comment3->save();
// Note we intentionally don't save this comment so it remains 'new'.
$comment4 = Comment::create([
'entity_type' => 'entity_test',
'hostname' => 'magic.example.com',
// Unpublished.
'status' => 0,
'subject' => 'Daniel the Cocker-Spaniel',
'entity_id' => $host->id(),
'comment_type' => 'comment',
'field_name' => 'comment_other',
'pid' => 0,
'uid' => $anonymous_user->id(),
]);
// Note we intentionally don't save this comment so it remains 'new'.
$comment5 = Comment::create([
'entity_type' => 'entity_test',
'hostname' => 'magic.example.com',
// Unpublished.
'status' => 0,
'subject' => 'Wally the Border Collie',
// This one is closed for comments.
'entity_id' => $host2->id(),
'comment_type' => 'comment',
'field_name' => 'comment_other',
'pid' => 0,
'uid' => $anonymous_user->id(),
]);
// Generate permutations.
$combinations = [
'comment' => [$comment1, $comment2, $comment3, $comment4, $comment5],
'user' => [$comment_admin_user, $comment_enabled_user, $comment_no_edit_user, $comment_disabled_user, $anonymous_user],
];
$permutations = $this->generatePermutations($combinations);
// Check access to administrative fields.
foreach ($this->administrativeFields as $field) {
foreach ($permutations as $set) {
$may_view = $set['comment']->{$field}->access('view', $set['user']);
$may_update = $set['comment']->{$field}->access('edit', $set['user']);
$account_name = $set['user']->getAccountName();
$comment_subject = $set['comment']->getSubject();
$this->assertTrue($may_view, "User $account_name can view field $field on comment $comment_subject");
$this->assertEquals(
$may_update,
$set['user']->hasPermission('administer comments'),
"User $account_name" . ($may_update ? 'can' : 'cannot') . "update field $field on comment $comment_subject"
);
}
}
// Check access to normal field.
foreach ($permutations as $set) {
$may_update = $set['comment']->access('update', $set['user']) && $set['comment']->subject->access('edit', $set['user']);
$this->assertEquals(
$may_update,
$set['user']->hasPermission('administer comments') || ($set['user']->hasPermission('edit own comments') && $set['user']->id() == $set['comment']->getOwnerId()),
sprintf('User %s %s update field subject on comment %s',
$set['user']->getAccountName(),
$may_update ? 'can' : 'cannot',
$set['comment']->getSubject(),
),
);
}
// Check read-only fields.
foreach ($this->readOnlyFields as $field) {
// Check view operation.
foreach ($permutations as $set) {
$may_view = $set['comment']->{$field}->access('view', $set['user']);
$may_update = $set['comment']->{$field}->access('edit', $set['user']);
// Nobody has access to view the hostname field.
if ($field === 'hostname') {
$view_access = FALSE;
$state = 'cannot';
}
else {
$view_access = TRUE;
$state = 'can';
}
$this->assertEquals(
$may_view,
$view_access,
sprintf('User %s %s view field %s on comment %s',
$set['user']->getAccountName(),
$state,
$field,
$set['comment']->getSubject(),
),
);
$this->assertFalse(
$may_update,
sprintf('User %s %s update field %s on comment %s',
$set['user']->getAccountName(),
$may_update ? 'can' : 'cannot',
$field,
$set['comment']->getSubject(),
),
);
}
}
// Check create-only fields.
foreach ($this->createOnlyFields as $field) {
// Check view operation.
foreach ($permutations as $set) {
$may_view = $set['comment']->{$field}->access('view', $set['user']);
$may_update = $set['comment']->{$field}->access('edit', $set['user']);
$this->assertTrue(
$may_view,
sprintf('User %s can view field %s on comment %s',
$set['user']->getAccountName(),
$field,
$set['comment']->getSubject(),
),
);
$expected = $set['user']->hasPermission('post comments') && $set['comment']->isNew() && (int) $set['comment']->getCommentedEntity()->get($set['comment']->getFieldName())->status !== CommentItemInterface::CLOSED;
$this->assertEquals(
$expected,
$may_update,
sprintf('User %s %s update field %s on comment %s',
$set['user']->getAccountName(),
$expected ? 'can' : 'cannot',
$field,
$set['comment']->getSubject(),
),
);
}
}
// Check contact fields.
foreach ($this->contactFields as $field) {
// Check view operation.
foreach ($permutations as $set) {
$may_update = $set['comment']->{$field}->access('edit', $set['user']);
// To edit the 'mail' or 'name' field, either the user has the
// "administer comments" permissions or the user is anonymous and
// adding a new comment using a field that allows contact details.
$this->assertEquals($may_update, $set['user']->hasPermission('administer comments') || (
$set['user']->isAnonymous() &&
$set['comment']->isNew() &&
$set['user']->hasPermission('post comments') &&
$set['comment']->getFieldName() == 'comment_other'
),
sprintf('User %s %s update field %s on comment %s',
$set['user']->getAccountName(),
$may_update ? 'can' : 'cannot',
$field,
$set['comment']->getSubject(),
),
);
}
}
foreach ($permutations as $set) {
// Check no view-access to mail field for other than admin.
$may_view = $set['comment']->mail->access('view', $set['user']);
$this->assertEquals($may_view, $set['user']->hasPermission('administer comments'));
}
}
}

View File

@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Entity\CommentType;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
/**
* Tests the hostname base field.
*
* @coversDefaultClass \Drupal\comment\Entity\Comment
*
* @group comment
*/
class CommentHostnameTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['comment', 'entity_test', 'user'];
/**
* Tests hostname default value callback.
*
* @covers ::getDefaultHostname
*/
public function testGetDefaultHostname(): void {
// Create a fake request to be used for testing.
$request = Request::create('/', 'GET', [], [], [], ['REMOTE_ADDR' => '203.0.113.1']);
$request->setSession(new Session(new MockArraySessionStorage()));
/** @var \Symfony\Component\HttpFoundation\RequestStack $stack */
$stack = $this->container->get('request_stack');
$stack->push($request);
CommentType::create([
'id' => 'foo',
'label' => 'Foo',
'target_entity_type_id' => 'entity_test',
])->save();
// Check that the hostname is empty by default.
$comment = Comment::create(['comment_type' => 'foo']);
$this->assertEquals('', $comment->getHostname());
\Drupal::configFactory()
->getEditable('comment.settings')
->set('log_ip_addresses', TRUE)
->save(TRUE);
// Check that the hostname was set correctly.
$comment = Comment::create(['comment_type' => 'foo']);
$this->assertEquals('203.0.113.1', $comment->getHostname());
}
}

View File

@@ -0,0 +1,168 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Entity\CommentType;
use Drupal\Core\Database\Database;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
/**
* Tests integration of comment with other components.
*
* @group comment
*/
class CommentIntegrationTest extends KernelTestBase {
use UserCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'comment',
'field',
'entity_test',
'user',
'system',
'dblog',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('entity_test');
$this->installEntitySchema('user');
$this->installEntitySchema('comment');
$this->installSchema('dblog', ['watchdog']);
// Create a new 'comment' comment-type.
CommentType::create([
'id' => 'comment',
'label' => $this->randomString(),
'target_entity_type_id' => 'entity_test',
])->save();
}
/**
* Tests view mode setting integration.
*
* @see comment_entity_view_display_presave()
* @see CommentDefaultFormatter::calculateDependencies()
*/
public function testViewMode(): void {
$mode = $this->randomMachineName();
// Create a new comment view mode and a view display entity.
EntityViewMode::create([
'id' => "comment.$mode",
'targetEntityType' => 'comment',
'settings' => ['comment_type' => 'comment'],
'label' => $mode,
])->save();
EntityViewDisplay::create([
'targetEntityType' => 'comment',
'bundle' => 'comment',
'mode' => $mode,
])->setStatus(TRUE)->save();
// Create a comment field attached to a host 'entity_test' entity.
FieldStorageConfig::create([
'entity_type' => 'entity_test',
'type' => 'comment',
'field_name' => $field_name = $this->randomMachineName(),
'settings' => [
'comment_type' => 'comment',
],
])->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'field_name' => $field_name,
])->save();
$component = [
'type' => 'comment_default',
'settings' => ['view_mode' => $mode, 'pager_id' => 0],
];
// Create a new 'entity_test' view display on host entity that uses the
// custom comment display in field formatter to show the field.
EntityViewDisplay::create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
])->setComponent($field_name, $component)->setStatus(TRUE)->save();
$host_display_id = 'entity_test.entity_test.default';
$comment_display_id = "comment.comment.$mode";
// Disable the "comment.comment.$mode" display.
EntityViewDisplay::load($comment_display_id)->setStatus(FALSE)->save();
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $host_display */
$host_display = EntityViewDisplay::load($host_display_id);
// Check that the field formatter has been disabled on host view display.
$this->assertNull($host_display->getComponent($field_name));
$this->assertTrue($host_display->get('hidden')[$field_name]);
// Check that the proper warning has been logged.
$arguments = [
'@id' => $host_display_id,
'@name' => $field_name,
'@display' => EntityViewMode::load("comment.$mode")->label(),
'@mode' => $mode,
];
$logged = Database::getConnection()->select('watchdog')
->fields('watchdog', ['variables'])
->condition('type', 'system')
->condition('message', "View display '@id': Comment field formatter '@name' was disabled because it is using the comment view display '@display' (@mode) that was just disabled.")
->execute()
->fetchField();
$this->assertEquals(serialize($arguments), $logged);
// Re-enable the comment view display.
EntityViewDisplay::load($comment_display_id)->setStatus(TRUE)->save();
// Re-enable the comment field formatter on host entity view display.
EntityViewDisplay::load($host_display_id)->setComponent($field_name, $component)->save();
// Delete the "comment.$mode" view mode.
EntityViewMode::load("comment.$mode")->delete();
// Check that the comment view display entity has been deleted too.
$this->assertNull(EntityViewDisplay::load($comment_display_id));
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
$host_display = EntityViewDisplay::load($host_display_id);
// Check that the field formatter has been disabled on host view display.
$this->assertNull($host_display->getComponent($field_name));
$this->assertTrue($host_display->get('hidden')[$field_name]);
}
/**
* Tests the default owner of comment entities.
*/
public function testCommentDefaultOwner(): void {
$comment = Comment::create([
'comment_type' => 'comment',
]);
$this->assertEquals(0, $comment->getOwnerId());
$user = $this->createUser();
$this->container->get('current_user')->setAccount($user);
$comment = Comment::create([
'comment_type' => 'comment',
]);
$this->assertEquals($user->id(), $comment->getOwnerId());
}
}

View File

@@ -0,0 +1,130 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
/**
* Tests the new entity API for the comment field type.
*
* @group comment
*/
class CommentItemTest extends FieldKernelTestBase {
use CommentTestTrait;
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['comment', 'entity_test', 'user'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('comment');
$this->installSchema('comment', ['comment_entity_statistics']);
$this->installConfig(['comment']);
}
/**
* Tests using entity fields of the comment field type.
*/
public function testCommentItem(): void {
$this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
// Verify entity creation.
$entity = EntityTest::create();
$entity->name->value = $this->randomMachineName();
$entity->save();
// Verify entity has been created properly.
$id = $entity->id();
$storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
$storage->resetCache([$id]);
$entity = $storage->load($id);
$this->assertInstanceOf(FieldItemListInterface::class, $entity->comment);
$this->assertInstanceOf(CommentItemInterface::class, $entity->comment[0]);
// Test sample item generation.
/** @var \Drupal\entity_test\Entity\EntityTest $entity */
$entity = EntityTest::create();
$entity->comment->generateSampleItems();
$this->entityValidateAndSave($entity);
$this->assertContains($entity->get('comment')->status, [
CommentItemInterface::HIDDEN,
CommentItemInterface::CLOSED,
CommentItemInterface::OPEN,
], 'Comment status value in defined range');
$mainProperty = $entity->comment[0]->mainPropertyName();
$this->assertEquals('status', $mainProperty);
}
/**
* Tests comment author name.
*/
public function testCommentAuthorName(): void {
$this->installEntitySchema('comment');
$this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
$host = EntityTest::create(['name' => $this->randomString()]);
$host->save();
// Create some comments.
$comment = Comment::create([
'subject' => 'My comment title',
'uid' => 1,
'name' => 'entity-test',
'mail' => 'entity@localhost',
'entity_type' => 'entity_test',
'field_name' => 'comment',
'entity_id' => $host->id(),
'comment_type' => 'entity_test',
'status' => 1,
]);
$comment->save();
// The entity fields for name and mail have no meaning if the user is not
// Anonymous.
$this->assertNull($comment->name->value);
$this->assertNull($comment->mail->value);
$comment_anonymous = Comment::create([
'subject' => 'Anonymous comment title',
'uid' => 0,
'name' => 'barry',
'mail' => 'test@example.com',
'homepage' => 'https://example.com',
'entity_type' => 'entity_test',
'field_name' => 'comment',
'entity_id' => $host->id(),
'comment_type' => 'entity_test',
'status' => 1,
]);
$comment_anonymous->save();
// The entity fields for name and mail have retained their values when
// comment belongs to an anonymous user.
$this->assertNotNull($comment_anonymous->name->value);
$this->assertNotNull($comment_anonymous->mail->value);
$comment_anonymous->setOwnerId(1)
->save();
// The entity fields for name and mail have no meaning if the user is not
// Anonymous.
$this->assertNull($comment_anonymous->name->value);
$this->assertNull($comment_anonymous->mail->value);
}
}

View File

@@ -0,0 +1,137 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\Tests\EntityViewTrait;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Tests loading and rendering orphan comments.
*
* @group comment
*/
class CommentOrphanTest extends EntityKernelTestBase {
use EntityViewTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['comment', 'node', 'user'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['user']);
$this->installEntitySchema('date_format');
$this->installEntitySchema('comment');
$this->installSchema('comment', ['comment_entity_statistics']);
}
/**
* Test loading/deleting/rendering orphaned comments.
*
* @dataProvider providerTestOrphan
*/
public function testOrphan($property): void {
DateFormat::create([
'id' => 'fallback',
'label' => 'Fallback',
'pattern' => 'Y-m-d',
])->save();
$comment_storage = $this->entityTypeManager->getStorage('comment');
$node_storage = $this->entityTypeManager->getStorage('node');
// Create a page node type.
$this->entityTypeManager->getStorage('node_type')->create([
'type' => 'page',
'name' => 'page',
])->save();
$node = $node_storage->create([
'type' => 'page',
'title' => 'test',
]);
$node->save();
// Create comment field.
$this->entityTypeManager->getStorage('field_storage_config')->create([
'type' => 'text_long',
'entity_type' => 'node',
'field_name' => 'comment',
])->save();
// Add comment field to page content.
$this->entityTypeManager->getStorage('field_config')->create([
'field_storage' => FieldStorageConfig::loadByName('node', 'comment'),
'entity_type' => 'node',
'bundle' => 'page',
'label' => 'Comment',
])->save();
// Make two comments
$comment1 = $comment_storage->create([
'field_name' => 'comment',
'comment_body' => 'test',
'entity_id' => $node->id(),
'entity_type' => 'node',
'comment_type' => 'default',
])->save();
$comment_storage->create([
'field_name' => 'comment',
'comment_body' => 'test',
'entity_id' => $node->id(),
'entity_type' => 'node',
'comment_type' => 'default',
'pid' => $comment1,
])->save();
// Render the comments.
$renderer = \Drupal::service('renderer');
$comments = $comment_storage->loadMultiple();
foreach ($comments as $comment) {
$built = $this->buildEntityView($comment, 'full', NULL);
$renderer->renderInIsolation($built);
}
// Make comment 2 an orphan by setting the property to an invalid value.
\Drupal::database()->update('comment_field_data')
->fields([$property => 10])
->condition('cid', 2)
->execute();
$comment_storage->resetCache();
$node_storage->resetCache();
// Render the comments with an orphan comment.
$comments = $comment_storage->loadMultiple();
foreach ($comments as $comment) {
$built = $this->buildEntityView($comment, 'full', NULL);
$renderer->renderInIsolation($built);
}
$node = $node_storage->load($node->id());
$built = $this->buildEntityView($node, 'full', NULL);
$renderer->renderInIsolation($built);
}
/**
* Provides test data for testOrphan.
*/
public static function providerTestOrphan() {
return [
['entity_id'],
['uid'],
['pid'],
];
}
}

View File

@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\Entity\CommentType;
use Drupal\KernelTests\KernelTestBase;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Tests that comment fields cannot be added to entities with non-integer IDs.
*
* @group comment
*/
class CommentStringIdEntitiesTest extends KernelTestBase {
/**
* Modules to install.
*
* @var array
*/
protected static $modules = [
'comment',
'user',
'field',
'field_ui',
'entity_test',
'text',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('comment');
$this->installEntitySchema('entity_test_string_id');
$this->installSchema('comment', ['comment_entity_statistics']);
// Create the comment body field storage.
$this->installConfig(['field']);
}
/**
* Tests that comment fields cannot be added entities with non-integer IDs.
*/
public function testCommentFieldNonStringId(): void {
$this->expectException(\UnexpectedValueException::class);
$bundle = CommentType::create([
'id' => 'foo',
'label' => 'foo',
'description' => '',
'target_entity_type_id' => 'entity_test_string_id',
]);
$bundle->save();
$field_storage = FieldStorageConfig::create([
'field_name' => 'foo',
'entity_type' => 'entity_test_string_id',
'settings' => [
'comment_type' => 'entity_test_string_id',
],
'type' => 'comment',
]);
$field_storage->save();
}
}

View File

@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\Entity\CommentType;
use Drupal\KernelTests\Core\Config\ConfigEntityValidationTestBase;
/**
* Tests validation of comment_type entities.
*
* @group comment
*/
class CommentTypeValidationTest extends ConfigEntityValidationTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['comment', 'node'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->entity = CommentType::create([
'id' => 'test',
'label' => 'Test',
'target_entity_type_id' => 'node',
]);
$this->entity->save();
}
}

View File

@@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Extension\ModuleUninstallValidatorException;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
/**
* Tests comment module uninstall.
*
* @group comment
*/
class CommentUninstallTest extends KernelTestBase {
use CommentTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'comment',
'field',
'node',
'system',
'text',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('comment');
$this->installConfig(['comment']);
$this->installSchema('user', ['users_data']);
NodeType::create([
'type' => 'article',
'name' => 'Article',
])->save();
// Create comment field on article so that it adds 'comment_body' field.
FieldStorageConfig::create([
'type' => 'text_long',
'entity_type' => 'comment',
'field_name' => 'comment',
])->save();
$this->addDefaultCommentField('node', 'article');
}
/**
* Tests if comment module uninstall fails if the field exists.
*/
public function testCommentUninstallWithField(): void {
// Ensure that the field exists before uninstalling.
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
$this->assertNotNull($field_storage);
// Uninstall the comment module which should trigger an exception.
$this->expectException(ModuleUninstallValidatorException::class);
$this->expectExceptionMessage('The following reasons prevent the modules from being uninstalled: The <em class="placeholder">Comments</em> field type is used in the following field: node.comment');
$this->container->get('module_installer')->uninstall(['comment']);
}
/**
* Tests if uninstallation succeeds if the field has been deleted beforehand.
*/
public function testCommentUninstallWithoutField(): void {
// Tests if uninstall succeeds if the field has been deleted beforehand.
// Manually delete the comment_body field before module uninstall.
FieldStorageConfig::loadByName('comment', 'comment_body')->delete();
// Check that the field is now deleted.
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
$this->assertNull($field_storage);
// Manually delete the comment field on the node before module uninstall.
$field_storage = FieldStorageConfig::loadByName('node', 'comment');
$this->assertNotNull($field_storage);
$field_storage->delete();
// Check that the field is now deleted.
$field_storage = FieldStorageConfig::loadByName('node', 'comment');
$this->assertNull($field_storage);
field_purge_batch(10);
// Ensure that uninstall succeeds even if the field has already been deleted
// manually beforehand.
$this->container->get('module_installer')->uninstall(['comment']);
}
}

View File

@@ -0,0 +1,305 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Entity\CommentType;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\Tests\field\Traits\EntityReferenceFieldCreationTrait;
use Drupal\user\Entity\User;
/**
* Tests comment validation constraints.
*
* @group comment
*/
class CommentValidationTest extends EntityKernelTestBase {
use CommentTestTrait;
use EntityReferenceFieldCreationTrait;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = ['comment', 'node'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installSchema('comment', ['comment_entity_statistics']);
$this->installConfig(['comment']);
}
/**
* Tests the comment validation constraints.
*/
public function testValidation(): void {
// Add a user.
$user = User::create(['name' => 'test', 'status' => TRUE]);
$user->save();
// Add comment type.
$this->entityTypeManager->getStorage('comment_type')->create([
'id' => 'comment',
'label' => 'comment',
'target_entity_type_id' => 'node',
])->save();
// Add comment field to content.
$this->entityTypeManager->getStorage('field_storage_config')->create([
'entity_type' => 'node',
'field_name' => 'comment',
'type' => 'comment',
'settings' => [
'comment_type' => 'comment',
],
])->save();
// Create a page node type.
$this->entityTypeManager->getStorage('node_type')->create([
'type' => 'page',
'name' => 'page',
])->save();
// Add comment field to page content.
/** @var \Drupal\field\FieldConfigInterface $field */
$field = $this->entityTypeManager->getStorage('field_config')->create([
'field_name' => 'comment',
'entity_type' => 'node',
'bundle' => 'page',
'label' => 'Comment settings',
]);
$field->save();
$node = $this->entityTypeManager->getStorage('node')->create([
'type' => 'page',
'title' => 'test',
]);
$node->save();
$comment = $this->entityTypeManager->getStorage('comment')->create([
'entity_id' => $node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'comment_body' => $this->randomMachineName(),
]);
$violations = $comment->validate();
$this->assertCount(0, $violations, 'No violations when validating a default comment.');
$comment->set('subject', $this->randomString(65));
$this->assertLengthViolation($comment, 'subject', 64);
// Make the subject valid.
$comment->set('subject', $this->randomString());
$comment->set('name', $this->randomString(61));
$this->assertLengthViolation($comment, 'name', 60);
// Validate a name collision between an anonymous comment author name and an
// existing user account name.
$comment->set('name', 'test');
$comment->set('uid', 0);
$violations = $comment->validate();
$this->assertCount(1, $violations, "Violation found on author name collision");
$this->assertEquals("name", $violations[0]->getPropertyPath());
$this->assertEquals('The name you used (test) belongs to a registered user.', $violations[0]->getMessage());
// Make the name valid.
$comment->set('name', 'valid unused name');
$comment->set('mail', 'invalid');
$violations = $comment->validate();
$this->assertCount(1, $violations, 'Violation found when email is invalid');
$this->assertEquals('mail.0.value', $violations[0]->getPropertyPath());
$this->assertEquals('This value is not a valid email address.', $violations[0]->getMessage());
$comment->set('mail', NULL);
$comment->set('homepage', 'http://example.com/' . $this->randomMachineName(237));
$this->assertLengthViolation($comment, 'homepage', 255);
$comment->set('homepage', 'invalid');
$violations = $comment->validate();
$this->assertCount(1, $violations, 'Violation found when homepage is invalid');
$this->assertEquals('homepage.0.value', $violations[0]->getPropertyPath());
// @todo This message should be improved in
// https://www.drupal.org/node/2012690.
$this->assertEquals('This value should be of the correct primitive type.', $violations[0]->getMessage());
$comment->set('homepage', NULL);
$comment->set('hostname', $this->randomString(129));
$this->assertLengthViolation($comment, 'hostname', 128);
$comment->set('hostname', NULL);
$comment->set('thread', $this->randomString(256));
$this->assertLengthViolation($comment, 'thread', 255);
$comment->set('thread', NULL);
// Force anonymous users to enter contact details.
$field->setSetting('anonymous', CommentInterface::ANONYMOUS_MUST_CONTACT);
$field->save();
// Reset the node entity.
\Drupal::entityTypeManager()->getStorage('node')->resetCache([$node->id()]);
$node = Node::load($node->id());
// Create a new comment with the new field.
$comment = $this->entityTypeManager->getStorage('comment')->create([
'entity_id' => $node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'comment_body' => $this->randomMachineName(),
'uid' => 0,
'name' => '',
]);
$violations = $comment->validate();
$this->assertCount(1, $violations, 'Violation found when name is required, but empty and UID is anonymous.');
$this->assertEquals('name', $violations[0]->getPropertyPath());
$this->assertEquals('You have to specify a valid author.', $violations[0]->getMessage());
// Test creating a default comment with a given user id works.
$comment = $this->entityTypeManager->getStorage('comment')->create([
'entity_id' => $node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'comment_body' => $this->randomMachineName(),
'uid' => $user->id(),
]);
$violations = $comment->validate();
$this->assertCount(0, $violations, 'No violations when validating a default comment with an author.');
// Test specifying a wrong author name does not work.
$comment = $this->entityTypeManager->getStorage('comment')->create([
'entity_id' => $node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'comment_body' => $this->randomMachineName(),
'uid' => $user->id(),
'name' => 'not-test',
]);
$violations = $comment->validate();
$this->assertCount(1, $violations, 'Violation found when author name and comment author do not match.');
$this->assertEquals('name', $violations[0]->getPropertyPath());
$this->assertEquals('The specified author name does not match the comment author.', $violations[0]->getMessage());
}
/**
* Tests that comments of unpublished nodes are not valid.
*/
public function testValidationOfCommentOfUnpublishedNode(): void {
// Create a page node type.
$this->entityTypeManager->getStorage('node_type')->create([
'type' => 'page',
'name' => 'page',
])->save();
// Create a comment type.
CommentType::create([
'id' => 'comment',
'label' => 'Default comments',
'description' => 'Default comment field',
'target_entity_type_id' => 'node',
])->save();
// Add comment and entity reference comment fields.
$this->addDefaultCommentField('node', 'page', 'comment');
$this->createEntityReferenceField(
'node',
'page',
'entity_reference_comment',
'Entity Reference Comment',
'comment',
'default',
['target_bundles' => ['comment']]
);
$comment_admin_user = $this->drupalCreateUser([
'skip comment approval',
'post comments',
'access comments',
'access content',
'administer nodes',
'administer comments',
'bypass node access',
]);
$comment_non_admin_user = $this->drupalCreateUser([
'access comments',
'post comments',
'create page content',
'edit own comments',
'skip comment approval',
'access content',
]);
// Create a node with a comment and make it unpublished.
$node1 = $this->entityTypeManager->getStorage('node')->create([
'type' => 'page',
'title' => 'test 1',
'promote' => 1,
'status' => 0,
'uid' => $comment_non_admin_user->id(),
]);
$node1->save();
$comment1 = $this->entityTypeManager->getStorage('comment')->create([
'entity_id' => $node1->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'comment_body' => $this->randomMachineName(),
]);
$comment1->save();
$this->assertInstanceOf(Comment::class, $comment1);
// Create a second published node.
/** @var \Drupal\node\Entity\Node $node2 */
$node2 = $this->entityTypeManager->getStorage('node')->create([
'type' => 'page',
'title' => 'test 2',
'promote' => 1,
'status' => 1,
'uid' => $comment_non_admin_user->id(),
]);
$node2->save();
// Test the validation API directly.
$this->drupalSetCurrentUser($comment_non_admin_user);
$this->assertEquals(\Drupal::currentUser()->id(), $comment_non_admin_user->id());
$node2->set('entity_reference_comment', $comment1->id());
$violations = $node2->validate();
$this->assertCount(1, $violations);
$this->assertEquals('entity_reference_comment.0.target_id', $violations[0]->getPropertyPath());
$this->assertEquals(sprintf('This entity (%s: %s) cannot be referenced.', $comment1->getEntityTypeId(), $comment1->id()), $violations[0]->getMessage());
$this->drupalSetCurrentUser($comment_admin_user);
$this->assertEquals(\Drupal::currentUser()->id(), $comment_admin_user->id());
$node2->set('entity_reference_comment', $comment1->id());
$violations = $node2->validate();
$this->assertCount(0, $violations);
}
/**
* Verifies that a length violation exists for the given field.
*
* @param \Drupal\comment\CommentInterface $comment
* The comment object to validate.
* @param string $field_name
* The field that violates the maximum length.
* @param int $length
* Number of characters that was exceeded.
*
* @internal
*/
protected function assertLengthViolation(CommentInterface $comment, string $field_name, int $length): void {
$violations = $comment->validate();
$this->assertCount(1, $violations, "Violation found when $field_name is too long.");
$this->assertEquals("{$field_name}.0.value", $violations[0]->getPropertyPath());
$field_label = $comment->get($field_name)->getFieldDefinition()->getLabel();
$this->assertEquals("{$field_label}: may not be longer than {$length} characters.", $violations[0]->getMessage());
}
}

View File

@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel\Migrate;
use Drupal\comment\Entity\CommentType;
use Drupal\Tests\migrate_drupal\Kernel\MigrateDrupalTestBase;
use Drupal\migrate_drupal\Tests\StubTestTrait;
use Drupal\node\Entity\NodeType;
/**
* Test stub creation for comment entities.
*
* @group comment
*/
class MigrateCommentStubTest extends MigrateDrupalTestBase {
use StubTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['comment', 'node'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('comment');
$this->installEntitySchema('node');
// Make sure uid 0 is created (default uid for comments is 0).
$storage = \Drupal::entityTypeManager()->getStorage('user');
// Insert a row for the anonymous user.
$storage
->create([
'uid' => 0,
'status' => 0,
'name' => '',
])
->save();
// Need at least one node type and comment type present.
NodeType::create([
'type' => 'testnodetype',
'name' => 'Test node type',
])->save();
CommentType::create([
'id' => 'testcommenttype',
'label' => 'Test comment type',
'target_entity_type_id' => 'node',
])->save();
}
/**
* Tests creation of comment stubs.
*/
public function testStub(): void {
$this->performStubTest('comment');
}
}

View File

@@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel\Migrate\d6;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* Tests the migration of comment entity displays from Drupal 6.
*
* @group comment
* @group migrate_drupal_6
*/
class MigrateCommentEntityDisplayTest extends MigrateDrupal6TestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['comment', 'menu_ui'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['comment']);
$this->migrateContentTypes();
$this->executeMigrations([
'd6_node_type',
'd6_comment_type',
'd6_comment_field',
'd6_comment_field_instance',
'd6_comment_entity_display',
]);
}
/**
* Asserts various aspects of a comment component in an entity view display.
*
* @param string $id
* The entity ID.
* @param string $component_id
* The ID of the display component.
*
* @internal
*/
protected function assertDisplay(string $id, string $component_id): void {
$component = EntityViewDisplay::load($id)->getComponent($component_id);
$this->assertIsArray($component);
$this->assertSame('hidden', $component['label']);
$this->assertSame('comment_default', $component['type']);
$this->assertSame(20, $component['weight']);
}
/**
* Tests the migrated display configuration.
*/
public function testMigration(): void {
$this->assertDisplay('node.article.default', 'comment_node_article');
$this->assertDisplay('node.company.default', 'comment_node_company');
$this->assertDisplay('node.employee.default', 'comment_node_employee');
$this->assertDisplay('node.event.default', 'comment_node_event');
$this->assertDisplay('node.forum.default', 'comment_forum');
$this->assertDisplay('node.page.default', 'comment_node_page');
$this->assertDisplay('node.sponsor.default', 'comment_node_sponsor');
$this->assertDisplay('node.story.default', 'comment_node_story');
$this->assertDisplay('node.test_event.default', 'comment_node_test_event');
$this->assertDisplay('node.test_page.default', 'comment_node_test_page');
$this->assertDisplay('node.test_planet.default', 'comment_node_test_planet');
$this->assertDisplay('node.test_story.default', 'comment_node_test_story');
}
}

View File

@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel\Migrate\d6;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* Tests the migration of comment form's subject display from Drupal 6.
*
* @group comment
* @group migrate_drupal_6
*/
class MigrateCommentEntityFormDisplaySubjectTest extends MigrateDrupal6TestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['comment'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['comment']);
$this->executeMigrations([
'd6_comment_type',
'd6_comment_entity_form_display_subject',
]);
}
/**
* Asserts that the comment subject field is visible for a node type.
*
* @param string $id
* The entity form display ID.
*
* @internal
*/
protected function assertSubjectVisible(string $id): void {
$component = EntityFormDisplay::load($id)->getComponent('subject');
$this->assertIsArray($component);
$this->assertSame('string_textfield', $component['type']);
$this->assertSame(10, $component['weight']);
}
/**
* Asserts that the comment subject field is not visible for a node type.
*
* @param string $id
* The entity form display ID.
*
* @internal
*/
protected function assertSubjectNotVisible(string $id): void {
$component = EntityFormDisplay::load($id)->getComponent('subject');
$this->assertNull($component);
}
/**
* Tests the migrated display configuration.
*/
public function testMigration(): void {
$this->assertSubjectVisible('comment.comment_node_article.default');
$this->assertSubjectVisible('comment.comment_node_company.default');
$this->assertSubjectVisible('comment.comment_node_employee.default');
$this->assertSubjectVisible('comment.comment_node_page.default');
$this->assertSubjectVisible('comment.comment_node_sponsor.default');
$this->assertSubjectNotVisible('comment.comment_node_story.default');
$this->assertSubjectVisible('comment.comment_node_test_event.default');
$this->assertSubjectVisible('comment.comment_node_test_page.default');
$this->assertSubjectVisible('comment.comment_node_test_planet.default');
$this->assertSubjectVisible('comment.comment_node_test_story.default');
}
}

View File

@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel\Migrate\d6;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* Tests the migration of comment form display from Drupal 6.
*
* @group comment
* @group migrate_drupal_6
*/
class MigrateCommentEntityFormDisplayTest extends MigrateDrupal6TestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['comment', 'menu_ui'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['comment']);
$this->migrateContentTypes();
$this->executeMigrations([
'd6_comment_type',
'd6_comment_field',
'd6_comment_field_instance',
'd6_comment_entity_form_display',
]);
}
/**
* Asserts various aspects of a comment component in an entity form display.
*
* @param string $id
* The entity ID.
* @param string $component_id
* The ID of the form component.
*
* @internal
*/
protected function assertDisplay(string $id, string $component_id): void {
$component = EntityFormDisplay::load($id)->getComponent($component_id);
$this->assertIsArray($component);
$this->assertSame('comment_default', $component['type']);
$this->assertSame(20, $component['weight']);
}
/**
* Tests the migrated display configuration.
*/
public function testMigration(): void {
$this->assertDisplay('node.article.default', 'comment_node_article');
$this->assertDisplay('node.company.default', 'comment_node_company');
$this->assertDisplay('node.employee.default', 'comment_node_employee');
$this->assertDisplay('node.event.default', 'comment_node_event');
$this->assertDisplay('node.forum.default', 'comment_forum');
$this->assertDisplay('node.page.default', 'comment_node_page');
$this->assertDisplay('node.sponsor.default', 'comment_node_sponsor');
$this->assertDisplay('node.story.default', 'comment_node_story');
$this->assertDisplay('node.test_event.default', 'comment_node_test_event');
$this->assertDisplay('node.test_page.default', 'comment_node_test_page');
$this->assertDisplay('node.test_planet.default', 'comment_node_test_planet');
$this->assertDisplay('node.test_story.default', 'comment_node_test_story');
}
}

View File

@@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel\Migrate\d6;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* Tests the migration of comment field instances from Drupal 6.
*
* @group comment
* @group migrate_drupal_6
*/
class MigrateCommentFieldInstanceTest extends MigrateDrupal6TestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['comment', 'menu_ui'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['comment']);
$this->migrateContentTypes();
$this->executeMigrations([
'd6_comment_type',
'd6_comment_field',
'd6_comment_field_instance',
]);
}
/**
* Asserts a comment field instance entity.
*
* @param string $bundle
* The bundle ID.
* @param string $field_name
* The field name.
* @param int $default_value
* The field's default_value setting.
* @param int $default_mode
* The field's default_mode setting.
* @param int $per_page
* The field's per_page setting.
* @param int $anonymous
* The field's anonymous setting.
* @param bool $form_location
* The field's form_location setting.
* @param int $preview
* The field's preview setting.
*
* @internal
*/
protected function assertEntity(string $bundle, string $field_name, int $default_value, int $default_mode, int $per_page, int $anonymous, bool $form_location, int $preview): void {
$entity = FieldConfig::load("node.$bundle.$field_name");
$this->assertInstanceOf(FieldConfig::class, $entity);
$this->assertSame('node', $entity->getTargetEntityTypeId());
$this->assertSame('Comments', $entity->label());
$this->assertTrue($entity->isRequired());
$this->assertSame($bundle, $entity->getTargetBundle());
$this->assertSame($field_name, $entity->getFieldStorageDefinition()->getName());
$this->assertSame($default_value, $entity->get('default_value')[0]['status']);
$this->assertSame($default_mode, $entity->getSetting('default_mode'));
$this->assertSame($per_page, $entity->getSetting('per_page'));
$this->assertSame($anonymous, $entity->getSetting('anonymous'));
$this->assertSame($form_location, $entity->getSetting('form_location'));
$this->assertSame($preview, $entity->getSetting('preview'));
}
/**
* Tests the migrated field instance values.
*/
public function testMigration(): void {
$this->assertEntity('article', 'comment_node_article', 2, 1, 50, 0, FALSE, 1);
$this->assertEntity('company', 'comment_node_company', 2, 1, 50, 0, FALSE, 1);
$this->assertEntity('employee', 'comment_node_employee', 2, 1, 50, 0, FALSE, 1);
$this->assertEntity('event', 'comment_node_event', 2, 1, 50, 0, FALSE, 1);
$this->assertEntity('forum', 'comment_forum', 2, 1, 50, 0, FALSE, 1);
$this->assertEntity('page', 'comment_node_page', 0, 1, 50, 0, FALSE, 1);
$this->assertEntity('sponsor', 'comment_node_sponsor', 2, 1, 50, 0, FALSE, 1);
$this->assertEntity('story', 'comment_node_story', 2, 0, 70, 1, FALSE, 0);
$this->assertEntity('test_event', 'comment_node_test_event', 2, 1, 50, 0, FALSE, 1);
$this->assertEntity('test_page', 'comment_node_test_page', 2, 1, 50, 0, FALSE, 1);
$this->assertEntity('test_planet', 'comment_node_test_planet', 2, 1, 50, 0, FALSE, 1);
$this->assertEntity('test_story', 'comment_node_test_story', 2, 1, 50, 0, FALSE, 1);
}
}

View File

@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel\Migrate\d6;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* Tests the migration of comment fields from Drupal 6.
*
* @group comment
* @group migrate_drupal_6
*/
class MigrateCommentFieldTest extends MigrateDrupal6TestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['comment', 'menu_ui'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['comment']);
$this->executeMigrations([
'd6_comment_type',
'd6_comment_field',
]);
}
/**
* Asserts a comment field entity.
*
* @param string $comment_type
* The comment type.
*
* @internal
*/
protected function assertEntity(string $comment_type): void {
$entity = FieldStorageConfig::load('node.' . $comment_type);
$this->assertInstanceOf(FieldStorageConfig::class, $entity);
$this->assertSame('node', $entity->getTargetEntityTypeId());
$this->assertSame('comment', $entity->getType());
$this->assertSame($comment_type, $entity->getSetting('comment_type'));
}
/**
* Tests the migrated comment fields.
*/
public function testMigration(): void {
$this->assertEntity('comment_node_article');
$this->assertEntity('comment_node_company');
$this->assertEntity('comment_node_employee');
$this->assertEntity('comment_node_event');
$this->assertEntity('comment_forum');
$this->assertEntity('comment_node_page');
$this->assertEntity('comment_node_sponsor');
$this->assertEntity('comment_node_story');
$this->assertEntity('comment_node_test_event');
$this->assertEntity('comment_node_test_page');
$this->assertEntity('comment_node_test_planet');
$this->assertEntity('comment_node_test_story');
}
}

View File

@@ -0,0 +1,122 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel\Migrate\d6;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
use Drupal\node\NodeInterface;
/**
* Tests the migration of comments from Drupal 6.
*
* @group comment
* @group migrate_drupal_6
*/
class MigrateCommentTest extends MigrateDrupal6TestBase {
use CommentTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'comment',
'content_translation',
'language',
'menu_ui',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('node');
$this->installEntitySchema('comment');
$this->installSchema('comment', ['comment_entity_statistics']);
$this->installSchema('node', ['node_access']);
$this->installConfig(['comment']);
$this->migrateContent();
$this->executeMigrations([
'language',
'd6_language_content_settings',
'd6_node',
'd6_node_translation',
'd6_comment_type',
'd6_comment_field',
'd6_comment_field_instance',
'd6_comment_entity_display',
'd6_comment_entity_form_display',
'd6_comment',
]);
}
/**
* Tests the migrated comments.
*/
public function testMigration(): void {
$comment = Comment::load(1);
$this->assertSame('The first comment.', $comment->getSubject());
$this->assertSame('The first comment body.', $comment->comment_body->value);
$this->assertSame('filtered_html', $comment->comment_body->format);
$this->assertNull($comment->pid->target_id);
$this->assertSame('1', $comment->getCommentedEntityId());
$this->assertSame('node', $comment->getCommentedEntityTypeId());
$this->assertSame('en', $comment->language()->getId());
$this->assertSame('comment_node_story', $comment->getTypeId());
$this->assertSame('203.0.113.1', $comment->getHostname());
$node = $comment->getCommentedEntity();
$this->assertInstanceOf(NodeInterface::class, $node);
$this->assertSame('1', $node->id());
$comment = Comment::load(2);
$this->assertSame('The response to the second comment.', $comment->subject->value);
$this->assertSame('3', $comment->pid->target_id);
$this->assertSame('203.0.113.2', $comment->getHostname());
$node = $comment->getCommentedEntity();
$this->assertInstanceOf(NodeInterface::class, $node);
$this->assertSame('1', $node->id());
$comment = Comment::load(3);
$this->assertSame('The second comment.', $comment->subject->value);
$this->assertNull($comment->pid->target_id);
$this->assertSame('203.0.113.3', $comment->getHostname());
$node = $comment->getCommentedEntity();
$this->assertInstanceOf(NodeInterface::class, $node);
$this->assertSame('1', $node->id());
// Tests that the language of the comment is migrated from the node.
$comment = Comment::load(7);
$this->assertSame('Comment to John Smith - EN', $comment->subject->value);
$this->assertSame('This is an English comment.', $comment->comment_body->value);
$this->assertSame('21', $comment->getCommentedEntityId());
$this->assertSame('node', $comment->getCommentedEntityTypeId());
$this->assertSame('en', $comment->language()->getId());
$node = $comment->getCommentedEntity();
$this->assertInstanceOf(NodeInterface::class, $node);
$this->assertSame('21', $node->id());
// Tests that the comment language is correct and that the commented entity
// is correctly migrated when the comment was posted to a node translation.
$comment = Comment::load(8);
$this->assertSame('Comment to John Smith - FR', $comment->subject->value);
$this->assertSame('This is a French comment.', $comment->comment_body->value);
$this->assertSame('21', $comment->getCommentedEntityId());
$this->assertSame('node', $comment->getCommentedEntityTypeId());
$this->assertSame('fr', $comment->language()->getId());
$node = $comment->getCommentedEntity();
$this->assertInstanceOf(NodeInterface::class, $node);
$this->assertSame('21', $node->id());
}
}

View File

@@ -0,0 +1,147 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel\Migrate\d6;
use Drupal\comment\Entity\CommentType;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* Tests the migration of comment types from Drupal 6.
*
* @group comment
* @group migrate_drupal_6
*/
class MigrateCommentTypeTest extends MigrateDrupal6TestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['comment'];
/**
* Asserts a comment type entity.
*
* @param string $id
* The entity ID.
* @param string $label
* The entity label.
*
* @internal
*/
protected function assertEntity(string $id, string $label): void {
$entity = CommentType::load($id);
$this->assertInstanceOf(CommentType::class, $entity);
$this->assertSame($label, $entity->label());
$this->assertSame('node', $entity->getTargetEntityTypeId());
}
/**
* Tests the migrated comment types.
*/
public function testMigration(): void {
$this->installConfig(['comment']);
$this->executeMigration('d6_comment_type');
$this->assertEntity('comment_node_article', 'Article comment');
$this->assertEntity('comment_node_company', 'Company comment');
$this->assertEntity('comment_node_employee', 'Employee comment');
$this->assertEntity('comment_node_event', 'Event comment');
$this->assertEntity('comment_forum', 'Forum topic comment');
$this->assertEntity('comment_node_page', 'Page comment');
$this->assertEntity('comment_node_sponsor', 'Sponsor comment');
$this->assertEntity('comment_node_story', 'Story comment');
$this->assertEntity('comment_node_test_event', 'Migrate test event comment');
$this->assertEntity('comment_node_test_page', 'Migrate test page comment');
$this->assertEntity('comment_node_test_planet', 'Migrate test planet comment');
$this->assertEntity('comment_node_test_story', 'Migrate test story comment');
$this->assertEntity('comment_node_a_thirty_two_char', 'Test long name comment');
}
/**
* Tests comment type migration without node or / and comment on source.
*
* Usually, MigrateDumpAlterInterface::migrateDumpAlter() should be used when
* the source fixture needs to be changed in a Migrate kernel test, but that
* would end in three additional tests and an extra overhead in maintenance.
*
* @param string[] $disabled_source_modules
* List of the modules to disable in the source Drupal database.
* @param string[][] $expected_messages
* List of the expected migration messages, keyed by the message type.
* Message type should be "status" "warning" or "error".
*
* @dataProvider providerTestNoCommentTypeMigration
*/
public function testNoCommentTypeMigration(array $disabled_source_modules, array $expected_messages): void {
if (!empty($disabled_source_modules)) {
$this->sourceDatabase->update('system')
->condition('name', $disabled_source_modules, 'IN')
->fields(['status' => 0])
->execute();
}
$this->startCollectingMessages();
$this->executeMigration('d6_comment_type');
$expected_messages += [
'status' => [],
'warning' => [],
'error' => [],
];
$actual_messages = $this->migrateMessages + [
'status' => [],
'warning' => [],
'error' => [],
];
foreach ($expected_messages as $type => $expected_messages_by_type) {
$this->assertSameSize($expected_messages_by_type, $actual_messages[$type]);
// Cast the actual messages to string.
$actual_messages_by_type = array_reduce($actual_messages[$type], function (array $carry, $actual_message) {
$carry[] = (string) $actual_message;
return $carry;
}, []);
$missing_expected_messages_by_type = array_diff($expected_messages_by_type, $actual_messages_by_type);
$unexpected_messages_by_type = array_diff($actual_messages_by_type, $expected_messages_by_type);
$this->assertEmpty($unexpected_messages_by_type, sprintf('No additional messages are present with type "%s". This expectation is wrong, because there are additional messages present: "%s"', $type, implode('", "', $unexpected_messages_by_type)));
$this->assertEmpty($missing_expected_messages_by_type, sprintf('Every expected messages are present with type "%s". This expectation is wrong, because the following messages aren\'t present: "%s"', $type, implode('", "', $missing_expected_messages_by_type)));
}
$this->assertEmpty(CommentType::loadMultiple());
}
/**
* Provides test cases for ::testNoCommentTypeMigration().
*/
public static function providerTestNoCommentTypeMigration() {
return [
'Node module is disabled in source' => [
'disabled_source_modules' => ['node'],
'expected_messages' => [
'error' => [
'Migration d6_comment_type did not meet the requirements. The node module is not enabled in the source site.',
],
],
],
'Comment module is disabled in source' => [
'disabled_source_modules' => ['comment'],
'expected_messages' => [
'error' => [
'Migration d6_comment_type did not meet the requirements. The module comment is not enabled in the source site.',
],
],
],
'Node and comment modules are disabled in source' => [
'disabled_source_modules' => ['comment', 'node'],
'expected_messages' => [
'error' => [
'Migration d6_comment_type did not meet the requirements. The module comment is not enabled in the source site.',
],
],
],
];
}
}

View File

@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel\Migrate\d7;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
use Drupal\migrate\Exception\RequirementsException;
/**
* Tests check requirements for comment entity translation source plugin.
*
* @group comment
*/
class CommentEntityTranslationCheckRequirementsTest extends MigrateDrupal7TestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'content_translation',
'comment',
'language',
];
/**
* Tests exception thrown when the given module is not enabled in the source.
*
* @dataProvider providerTestCheckRequirements
*/
public function testCheckRequirements($module): void {
// Disable the module in the source site.
$this->sourceDatabase->update('system')
->condition('name', $module)
->fields([
'status' => '0',
])
->execute();
$this->expectException(RequirementsException::class);
$this->expectExceptionMessage("The module $module is not enabled in the source site");
$this->getMigration('d7_comment_entity_translation')
->getSourcePlugin()
->checkRequirements();
}
/**
* Provides data for testCheckRequirements.
*
* @return string[][]
*/
public static function providerTestCheckRequirements() {
return [
['comment'],
['node'],
];
}
}

View File

@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel\Migrate\d7;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
/**
* Tests the migration of comment entity displays from Drupal 7.
*
* @group comment
* @group migrate_drupal_7
*/
class MigrateCommentEntityDisplayTest extends MigrateDrupal7TestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['node', 'comment', 'text', 'menu_ui'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->migrateContentTypes();
$this->migrateCommentTypes();
$this->executeMigrations([
'd7_comment_field',
'd7_comment_field_instance',
'd7_comment_entity_display',
]);
}
/**
* Asserts various aspects of a comment component in an entity view display.
*
* @param string $id
* The entity ID.
* @param string $component_id
* The ID of the display component.
*
* @internal
*/
protected function assertDisplay(string $id, string $component_id): void {
$component = EntityViewDisplay::load($id)->getComponent($component_id);
$this->assertIsArray($component);
$this->assertSame('hidden', $component['label']);
$this->assertSame('comment_default', $component['type']);
$this->assertSame(20, $component['weight']);
}
/**
* Tests the migrated display configuration.
*/
public function testMigration(): void {
$this->assertDisplay('node.page.default', 'comment_node_page');
$this->assertDisplay('node.article.default', 'comment_node_article');
$this->assertDisplay('node.book.default', 'comment_node_book');
$this->assertDisplay('node.blog.default', 'comment_node_blog');
$this->assertDisplay('node.forum.default', 'comment_forum');
$this->assertDisplay('node.test_content_type.default', 'comment_node_test_content_type');
}
}

View File

@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel\Migrate\d7;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
/**
* Tests the migration of comment form's subject display from Drupal 7.
*
* @group comment
* @group migrate_drupal_7
*/
class MigrateCommentEntityFormDisplaySubjectTest extends MigrateDrupal7TestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['node', 'comment', 'text', 'menu_ui'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->migrateCommentTypes();
$this->executeMigration('d7_comment_entity_form_display_subject');
}
/**
* Asserts that the comment subject field is visible for a node type.
*
* @param string $id
* The entity form display ID.
*
* @internal
*/
protected function assertSubjectVisible(string $id): void {
$component = EntityFormDisplay::load($id)->getComponent('subject');
$this->assertIsArray($component);
$this->assertSame('string_textfield', $component['type']);
$this->assertSame(10, $component['weight']);
}
/**
* Asserts that the comment subject field is not visible for a node type.
*
* @param string $id
* The entity form display ID.
*
* @internal
*/
protected function assertSubjectNotVisible(string $id): void {
$component = EntityFormDisplay::load($id)->getComponent('subject');
$this->assertNull($component);
}
/**
* Tests the migrated display configuration.
*/
public function testMigration(): void {
$this->assertSubjectVisible('comment.comment_node_page.default');
$this->assertSubjectVisible('comment.comment_node_article.default');
$this->assertSubjectVisible('comment.comment_node_book.default');
$this->assertSubjectVisible('comment.comment_node_blog.default');
$this->assertSubjectVisible('comment.comment_forum.default');
$this->assertSubjectNotVisible('comment.comment_node_test_content_type.default');
}
}

View File

@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\comment\Kernel\Migrate\d7;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
/**
* Tests the migration of comment form display from Drupal 7.
*
* @group comment
* @group migrate_drupal_7
*/
class MigrateCommentEntityFormDisplayTest extends MigrateDrupal7TestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['node', 'comment', 'text', 'menu_ui'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->migrateContentTypes();
$this->migrateCommentTypes();
$this->executeMigrations([
'd7_comment_field',
'd7_comment_field_instance',
'd7_comment_entity_form_display',
]);
}
/**
* Asserts various aspects of a comment component in an entity form display.
*
* @param string $id
* The entity ID.
* @param string $component_id
* The ID of the form component.
*
* @internal
*/
protected function assertDisplay(string $id, string $component_id): void {
$component = EntityFormDisplay::load($id)->getComponent($component_id);
$this->assertIsArray($component);
$this->assertSame('comment_default', $component['type']);
$this->assertSame(20, $component['weight']);
}
/**
* Tests the migrated display configuration.
*/
public function testMigration(): void {
$this->assertDisplay('node.page.default', 'comment_node_page');
$this->assertDisplay('node.article.default', 'comment_node_article');
$this->assertDisplay('node.book.default', 'comment_node_book');
$this->assertDisplay('node.blog.default', 'comment_node_blog');
$this->assertDisplay('node.forum.default', 'comment_forum');
$this->assertDisplay('node.test_content_type.default', 'comment_node_test_content_type');
}
}

Some files were not shown because too many files have changed in this diff Show More