View source
<?php
namespace Drupal\Tests\comment\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\comment\Entity\CommentType;
class CommentFieldsTest extends CommentTestBase {
protected static $modules = [
'field_ui',
];
protected $defaultTheme = 'stark';
public function testCommentDefaultFields() {
$this
->drupalCreateContentType([
'type' => 'test_node_type',
]);
$this
->addDefaultCommentField('node', 'test_node_type');
$field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
$this
->assertNotEmpty($field, 'The comment_body field is added when a comment bundle is created');
$field
->delete();
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
$this
->assertInstanceOf(FieldStorageConfig::class, $field_storage);
$type_name = 'test_node_type_2';
$this
->drupalCreateContentType([
'type' => $type_name,
]);
$this
->addDefaultCommentField('node', $type_name);
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
$this
->assertInstanceOf(FieldStorageConfig::class, $field_storage);
$field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
$this
->assertTrue(isset($field), new FormattableMarkup('The comment_body field is present for comments on type @type', [
'@type' => $type_name,
]));
$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']);
}
public function testCommentFieldDelete() {
$this
->drupalCreateContentType([
'type' => 'test_node_type',
]);
$this
->addDefaultCommentField('node', 'test_node_type');
$this
->addDefaultCommentField('node', 'test_node_type', 'comment2');
$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.');
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.');
}
public function testCommentFieldLinksNonDefaultName() {
$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',
]);
$node = $this
->drupalCreateNode([
'title' => 'Baloney',
'type' => 'test_node_type',
]);
$this
->drupalLogin($web_user2);
$this
->drupalGet($node
->toUrl());
$this
->drupalLogout();
$this
->drupalLogin($this->webUser);
$this
->postComment($node, 'Here is a comment', '', NULL, 'comment2');
$this
->drupalLogout();
$this
->drupalLogin($web_user2);
$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']);
}
public function testCommentFieldCreate() {
$user = $this
->drupalCreateUser([
'administer user fields',
]);
$this
->drupalLogin($user);
$edit = [
'new_storage_type' => 'comment',
'label' => 'User comment',
'field_name' => 'user_comment',
];
$this
->drupalGet('admin/config/people/accounts/fields/add-field');
$this
->submitForm($edit, 'Save and continue');
$edit = [];
$this
->drupalGet('admin/config/people/accounts/fields/user.user.field_user_comment/storage');
$this
->submitForm($edit, 'Save field settings');
$this
->assertSession()
->pageTextContains('An illegal choice has been detected. Please contact the site administrator.');
$bundle = CommentType::create([
'id' => 'user_comment_type',
'label' => 'user_comment_type',
'description' => '',
'target_entity_type_id' => 'user',
]);
$bundle
->save();
$edit = [
'settings[comment_type]' => 'user_comment_type',
];
$this
->drupalGet('admin/config/people/accounts/fields/user.user.field_user_comment/storage');
$this
->submitForm($edit, 'Save field settings');
$this
->assertSession()
->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
}
public function testCommentInstallAfterContentModule() {
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'administer modules',
]);
$this
->drupalLogin($this->adminUser);
FieldStorageConfig::loadByName('node', 'comment')
->delete();
if ($field_storage = FieldStorageConfig::loadByName('node', 'comment_forum')) {
$field_storage
->delete();
}
field_purge_batch(10);
$edit = [];
$edit['uninstall[comment]'] = TRUE;
$this
->drupalGet('admin/modules/uninstall');
$this
->submitForm($edit, 'Uninstall');
$this
->submitForm([], 'Uninstall');
$this
->rebuildContainer();
$this
->assertFalse($this->container
->get('module_handler')
->moduleExists('comment'), 'Comment module uninstalled.');
$edit = [];
$edit['modules[book][enable]'] = 'book';
$this
->drupalGet('admin/modules');
$this
->submitForm($edit, 'Install');
$edit = [];
$edit['modules[comment][enable]'] = 'comment';
$this
->drupalGet('admin/modules');
$this
->submitForm($edit, 'Install');
$this
->rebuildContainer();
$this
->assertTrue($this->container
->get('module_handler')
->moduleExists('comment'), 'Comment module enabled.');
$this
->addDefaultCommentField('node', 'book');
$book_node = $this
->drupalCreateNode([
'type' => 'book',
]);
$this
->drupalLogout();
$this->webUser = $this
->drupalCreateUser([
'access content',
'access comments',
'post comments',
'skip comment approval',
]);
$this
->drupalLogin($this->webUser);
$this
->postComment($book_node, $this
->randomMachineName(), $this
->randomMachineName());
}
}