View source
<?php
namespace Drupal\Tests\diff\Functional;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\link\LinkItemInterface;
class DiffPluginVariousTest extends DiffPluginTestBase {
use CommentTestTrait;
use CoreVersionUiTestTrait;
protected static $modules = [
'comment',
];
protected function addArticleTextField($field_name, $label, $field_type, $widget_type) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => $field_type,
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'article',
'label' => $label,
])
->save();
$this->formDisplay
->load('node.article.default')
->setComponent($field_name, [
'type' => $widget_type,
])
->save();
$this->viewDisplay
->load('node.article.default')
->setComponent($field_name)
->save();
}
public function testCommentPlugin() {
$this
->addDefaultCommentField('node', 'article');
$title = 'Sample article';
$edit = array(
'title[0][value]' => $title,
'body[0][value]' => '<p>Revision 1</p>',
'comment[0][status]' => CommentItemInterface::OPEN,
);
$this
->drupalPostNodeForm('node/add/article', $edit, t('Save and publish'));
$node = $this
->drupalGetNodeByTitle($title);
$edit = array(
'comment[0][status]' => CommentItemInterface::CLOSED,
'revision' => TRUE,
);
$this
->drupalPostNodeForm('node/' . $node
->id() . '/edit', $edit, t('Save and keep published'));
$this
->clickLink(t('Revisions'));
$this
->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
$this
->assertText('Comments');
$this
->assertText('Comments for this entity are open.');
$this
->assertText('Comments for this entity are closed.');
}
public function testCorePlugin() {
$field_name = 'field_email';
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'email',
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'article',
'label' => 'Email',
])
->save();
$this->formDisplay
->load('node.article.default')
->setComponent($field_name, [
'type' => 'email_default',
])
->save();
$this->viewDisplay
->load('node.article.default')
->setComponent($field_name, [
'type' => 'basic_string',
])
->save();
$node = $this
->drupalCreateNode([
'type' => 'article',
'field_email' => 'foo@example.com',
]);
$edit = array(
'field_email[0][value]' => 'bar@example.com',
'revision' => TRUE,
);
$this
->drupalPostNodeForm('node/' . $node
->id() . '/edit', $edit, t('Save and keep published'));
$this
->clickLink(t('Revisions'));
$this
->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
$this
->assertText('Email');
$this
->assertText('foo@example.com');
$this
->assertText('bar@example.com');
}
public function testCorePluginTimestampField() {
$field_name = 'field_timestamp';
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'timestamp',
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'article',
'label' => 'Timestamp test',
])
->save();
$this->formDisplay
->load('node.article.default')
->setComponent($field_name, [
'type' => 'datetime_timestamp',
])
->save();
$this->viewDisplay
->load('node.article.default')
->setComponent($field_name, [
'type' => 'timestamp',
])
->save();
$old_timestamp = '321321321';
$new_timestamp = '123123123';
$this
->drupalCreateNode([
'title' => 'timestamp_test',
'type' => 'article',
'field_timestamp' => $old_timestamp,
]);
$node = $this
->drupalGetNodeByTitle('timestamp_test');
$node->field_timestamp = $new_timestamp;
$node
->setNewRevision(TRUE);
$node
->save();
$this
->drupalGet('node/' . $node
->id() . '/revisions');
$this
->drupalPostForm(NULL, NULL, t('Compare selected revisions'));
$this
->assertText('Timestamp test');
$date_formatter = \Drupal::service('date.formatter');
$this
->assertText($date_formatter
->format($old_timestamp));
$this
->assertText($date_formatter
->format($new_timestamp));
}
public function testLinkPlugin() {
$field_name = 'field_link';
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'link',
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'article',
'label' => 'Link',
'settings' => array(
'title' => DRUPAL_OPTIONAL,
'link_type' => LinkItemInterface::LINK_GENERIC,
),
])
->save();
$this->formDisplay
->load('node.article.default')
->setComponent($field_name, [
'type' => 'link_default',
'settings' => [
'placeholder_url' => 'http://example.com',
],
])
->save();
$this->viewDisplay
->load('node.article.default')
->setComponent($field_name, [
'type' => 'link',
])
->save();
$this
->config('diff.plugins')
->set('fields.node.field_link.type', 'link_field_diff_builder')
->set('fields.node.field_link.settings', [
'compare_title' => TRUE,
])
->save();
$node = $this
->drupalCreateNode([
'type' => 'article',
'title' => 'Test article',
'field_link' => [
'title' => 'Google',
'uri' => 'http://www.google.com',
],
]);
$edit = [
'field_link[0][title]' => 'Guguel',
'field_link[0][uri]' => 'http://www.google.es',
'revision' => TRUE,
];
$this
->drupalPostNodeForm('node/' . $node
->id() . '/edit', $edit, t('Save and keep published'));
$this
->clickLink(t('Revisions'));
$this
->drupalPostForm(NULL, [], t('Compare selected revisions'));
$this
->assertText('Link');
$this
->assertText('Google');
$this
->assertText('http://www.google.com');
$this
->assertText('Guguel');
$this
->assertText('http://www.google.es');
}
public function testListPlugin() {
$field_name = 'field_list';
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'list_string',
'cardinality' => 1,
'settings' => [
'allowed_values' => [
'value_a' => 'Value A',
'value_b' => 'Value B',
],
],
]);
$field_storage
->save();
FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'bundle' => 'article',
'required' => FALSE,
'label' => 'List',
])
->save();
$this->formDisplay
->load('node.article.default')
->setComponent($field_name, [
'type' => 'options_select',
])
->save();
$this->viewDisplay
->load('node.article.default')
->setComponent($field_name, [
'type' => 'list_default',
])
->save();
$node = $this
->drupalCreateNode([
'type' => 'article',
'title' => 'Test article',
'field_list' => 'value_a',
]);
$edit = [
'field_list' => 'value_b',
'revision' => TRUE,
];
$this
->drupalPostNodeForm('node/' . $node
->id() . '/edit', $edit, t('Save and keep published'));
$this
->clickLink(t('Revisions'));
$this
->drupalPostForm(NULL, [], t('Compare selected revisions'));
$this
->assertText('List');
$this
->assertText('value_a');
$this
->assertText('value_b');
}
public function testTextPlugin() {
$this
->addArticleTextField('field_text', 'Text Field', 'string', 'string_textfield');
$this
->addArticleTextField('field_text_long', 'Text Long Field', 'string_long', 'string_textarea');
$node = $this
->drupalCreateNode([
'type' => 'article',
'title' => 'Test article',
'field_text' => 'Foo',
'field_text_long' => 'Fighters',
]);
$edit = [
'field_text[0][value]' => 'Bar',
'field_text_long[0][value]' => 'Fly',
'revision' => TRUE,
];
$this
->drupalPostNodeForm('node/' . $node
->id() . '/edit', $edit, t('Save and keep published'));
$this
->clickLink(t('Revisions'));
$this
->drupalPostForm(NULL, [], t('Compare selected revisions'));
$this
->assertText('Text Field');
$this
->assertText('Text Long Field');
$this
->assertText('Foo');
$this
->assertText('Fighters');
$this
->assertText('Bar');
$this
->assertText('Fly');
}
public function testTextWithSummaryPlugin() {
$config = \Drupal::configFactory()
->getEditable('diff.plugins');
$settings['compare_summary'] = TRUE;
$config
->set('fields.node.body.type', 'text_summary_field_diff_builder');
$config
->set('fields.node.body.settings', $settings);
$config
->save();
$node = $this
->drupalCreateNode([
'type' => 'article',
'title' => 'Test article',
'body' => [
'value' => 'Foo value',
'summary' => 'Foo summary',
],
]);
$edit = [
'body[0][value]' => 'Bar value',
'body[0][summary]' => 'Bar summary',
'revision' => TRUE,
];
$this
->drupalPostNodeForm('node/' . $node
->id() . '/edit', $edit, t('Save and keep published'));
$this
->clickLink(t('Revisions'));
$this
->drupalPostForm(NULL, [], t('Compare selected revisions'));
$this
->assertText('Body');
$this
->assertText('Foo value');
$this
->assertText('Foo summary');
$this
->assertText('Bar value');
$this
->assertText('Bar summary');
}
}