View source
<?php
namespace Drupal\Tests\comment_alter\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\entity_test\Entity\EntityTestRev;
use Drupal\comment\Entity\CommentType;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Component\Utility\Unicode;
class CommentAlterTestBase extends BrowserTestBase {
use CommentTestTrait;
public static $modules = [
'comment_alter',
'entity_test',
'comment',
'field',
];
protected $adminUser;
protected $entity;
protected $entityType;
protected $bundle;
protected function setUp() {
parent::setUp();
$this->entityType = 'entity_test_rev';
$this->bundle = 'entity_test_rev';
CommentType::create([
'id' => 'comment',
'label' => 'Comment',
'description' => 'Comment type for Comment Alter',
'target_entity_type_id' => $this->entityType,
])
->save();
$this
->addDefaultCommentField($this->entityType, $this->bundle);
$this->adminUser = $this
->drupalCreateUser([
'administer comments',
'post comments',
'access comments',
'skip comment approval',
'view test entity',
'view test entity field',
'administer entity_test content',
'access administration pages',
]);
$this
->drupalLogin($this->adminUser);
}
protected function addField($field_type, $widget_type, $storage_settings = [], $field_settings = [], $comment_alter = TRUE) {
$field_name = Unicode::strtolower($this
->randomMachineName() . '_field_name');
FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => $this->entityType,
'type' => $field_type,
] + $storage_settings)
->save();
FieldConfig::create([
'field_name' => $field_name,
'entity_type' => $this->entityType,
'bundle' => $this->bundle,
'settings' => $field_settings,
'widget' => [
'type' => $widget_type,
],
'third_party_settings' => [
'comment_alter' => [
'comment_alter_enabled' => $comment_alter,
],
],
])
->save();
entity_get_form_display($this->entityType, $this->bundle, 'default')
->setComponent($field_name, [
'type' => $widget_type,
])
->save();
\Drupal::cache()
->delete('comment_alter_fields:' . $this->entityType . ':' . $this->bundle);
return $field_name;
}
protected function createEntityObject($values = []) {
$random_label = $this
->randomMachineName();
$data = [
'type' => $this->bundle,
'name' => $random_label,
] + $values;
$this->entity = EntityTestRev::create($data);
$this->entity
->save();
}
protected function assertAlterableField($field_name) {
$this
->drupalGet('comment/reply/' . $this->entityType . '/' . $this->entity
->id() . '/comment');
$comment_display_form = entity_get_form_display('comment', 'comment', 'default');
$comment_field = $this->entityType . '_' . $this->bundle . '_comment_alter_' . $field_name;
$this
->assertSession()
->fieldExists($field_name);
$this
->assertTrue($comment_display_form
->getComponent($comment_field), 'Alterable fields is present in the comment form display.');
}
protected function postComment($comment_edit = []) {
$edit['comment_body[0][value]'] = $this
->randomString();
$edit['subject[0][value]'] = $this
->randomString();
$edit = array_merge($edit, $comment_edit);
$this
->drupalGet('comment/reply/' . $this->entityType . '/' . $this->entity
->id() . '/comment');
$this
->drupalPostForm(NULL, $edit, t('Save'));
}
protected function getCommentAlterations() {
$this
->drupalGet('entity_test_rev/manage/' . $this->entity
->id());
$td_s = $this
->xpath('//table[@class=:class]/tbody/tr/td', [
':class' => 'comment-alter-diff',
]);
$fields = [];
$i = 0;
foreach ($td_s as $td) {
switch ($i % 4) {
case 0:
$field_name = $td
->getText();
$field_name = empty($field_name) ? $last_field_name : $field_name;
break;
case 1:
$old_value = $td
->getText();
break;
case 3:
$new_value = $td
->getText();
break;
}
$i++;
if ($i % 4 == 0) {
$last_field_name = $field_name;
$fields[$field_name][] = [
$old_value,
$new_value,
];
}
}
return $fields;
}
protected function assertCommentDiff($test) {
$fields = $this
->getCommentAlterations();
foreach ($test as $field_name => $values) {
$this
->assertTrue(isset($fields[$field_name]), 'Comment alterable field is present in the comment alter diff.');
foreach ($values as $index => $value) {
$this
->assertEquals($fields[$field_name][$index][0], $value[0], 'Comment alter diff original value matches.');
$this
->assertEquals($fields[$field_name][$index][1], $value[1], 'Comment alter diff changed value matches.');
}
}
}
protected function assertRevisionDelete() {
$old_revision_id = $this->entity
->getRevisionId();
$this->entity
->setNewRevision(TRUE);
$this->entity
->save();
$this
->assertNotEquals($old_revision_id, $this->entity
->getRevisionId(), 'Make sure that two revisions are different.');
\Drupal::entityTypeManager()
->getStorage($this->entityType)
->deleteRevision($old_revision_id);
$content = $this
->drupalGet('entity_test_rev/manage/' . $this->entity
->id());
$this
->assertSession()
->statusCodeEquals(200);
}
protected function assertCommentSettings($field_name) {
$comment_config = $this->entity
->getFieldDefinition('comment');
$comment_config
->setThirdPartySetting('comment_alter', 'comment_alter_reply', TRUE)
->save();
$this
->drupalGet('comment/reply/' . $this->entityType . '/' . $this->entity
->id() . '/comment/1');
$this
->assertSession()
->fieldExists($field_name);
$alterable_field_config = $this->entity
->getFieldDefinition($field_name);
$alterable_field_config
->setThirdPartySetting('comment_alter', 'comment_alter_hide', TRUE)
->save();
$this
->drupalGet('entity_test_rev/manage/' . $this->entity
->id());
$this
->assertSession()
->pageTextContains(t('Changes are hidden'));
}
}