View source
<?php
namespace Drupal\Tests\sharemessage\Functional;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
class ShareMessageExtraFieldTest extends ShareMessageTestBase {
public static $modules = [
'field_ui',
'node',
'views',
'taxonomy',
'token',
];
protected $terms = [];
protected $adminPermissions = [
'access administration pages',
'administer blocks',
'administer sharemessages',
'view sharemessages',
'administer themes',
'access content overview',
'administer content types',
'administer nodes',
'access content',
'bypass node access',
'administer node display',
'administer user display',
'access user profiles',
'administer taxonomy',
'administer taxonomy_term display',
];
public function setUp() {
parent::setUp();
Vocabulary::create([
'vid' => 'tags',
'name' => 'Tags',
])
->save();
$term = Term::create([
'vid' => 'tags',
'name' => 'term0',
]);
$term
->save();
$this->terms[0] = $term;
$term = Term::create([
'vid' => 'tags',
'name' => 'term1',
]);
$term
->save();
$this->terms[1] = $term;
\Drupal::service('entity_display.repository')
->getViewDisplay('user', 'user')
->save();
\Drupal::service('entity_display.repository')
->getViewDisplay('taxonomy_term', 'tags')
->save();
}
public function testShareMessageExtraFieldToken() {
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Page',
]);
$article = $this
->drupalCreateNode([
'type' => 'article',
'title' => 'Article SM',
'body' => [
'value' => 'Article body text',
],
]);
$page = $this
->drupalCreateNode([
'type' => 'page',
'title' => 'Page SM',
'body' => [
'value' => 'Page body text',
],
]);
$this
->drupalGet('admin/config/services/sharemessage/add');
$this
->assertOptionSelected('edit-entity-type', '');
$this
->assertSession()
->pageTextContains('Browse available tokens.');
$this
->assertSession()
->responseNotContains('<fieldset data-drupal-selector="edit-bundles"');
$sharemessage = [
'label' => 'Share Message Test Label',
'id' => 'sharemessage_test_label',
'title' => '[node:title]',
'message_long' => 'Share Message Test Long Description',
'message_short' => 'Share Message Test Short Description',
'image_url' => 'http://www.example.com/drupal.jpg',
'share_url' => 'http://www.example.com',
];
$this
->drupalPostForm(NULL, $sharemessage, t('Save'));
$this
->assertText(t('Share Message @label has been added.', [
'@label' => $sharemessage['label'],
]), 'Share Message is successfully saved.');
$sharemessage_article = [
'title' => $article
->getTitle(),
'message_long' => $sharemessage['message_long'],
'share_url' => $sharemessage['share_url'],
];
$sharemessage_page = [
'title' => $page
->getTitle(),
'message_long' => $sharemessage['message_long'],
'share_url' => $sharemessage['share_url'],
];
$this
->setEntityRawContent('node', $article);
$this
->assertNoShareButtons($sharemessage_article);
$this
->setEntityRawContent('node', $page);
$this
->assertNoShareButtons($sharemessage_page);
$this
->drupalGet('admin/config/services/sharemessage/manage/sharemessage_test_label');
$this
->assertOptionSelected('edit-entity-type', '');
$this
->assertSession()
->responseNotContains('<fieldset data-drupal-selector="edit-bundles"');
$this
->drupalPostForm(NULL, [
'entity_type' => 'node',
], 'Save');
$this
->drupalGet('admin/config/services/sharemessage/manage/sharemessage_test_label');
$this
->assertSession()
->responseContains('<span class="fieldset-legend">Content type</span>');
$this
->assertSession()
->checkboxNotChecked('bundles[article]');
$this
->assertSession()
->checkboxNotChecked('bundles[page]');
$this
->drupalPostForm(NULL, [], t('Save'));
$extra_field = [
'fields[sharemessage__sharemessage_test_label][region]' => 'content',
'fields[sharemessage__sharemessage_test_label][weight]' => 105,
];
$this
->drupalGet('admin/structure/types/manage/article/display/teaser');
$this
->drupalPostForm(NULL, $extra_field, 'Save');
$this
->drupalGet('admin/structure/types/manage/page/display/teaser');
$this
->drupalPostForm(NULL, $extra_field, 'Save');
$this
->setEntityRawContent('node', $article);
$this
->assertShareButtons($sharemessage_article, 'addthis_16x16_style', TRUE);
$this
->setEntityRawContent('node', $page);
$this
->assertShareButtons($sharemessage_page, 'addthis_16x16_style', TRUE);
$this
->drupalGet('admin/config/services/sharemessage/manage/sharemessage_test_label');
$this
->assertOptionSelected('edit-entity-type', 'node');
$this
->assertSession()
->checkboxNotChecked('bundles[article]');
$this
->assertSession()
->checkboxNotChecked('bundles[page]');
$this
->drupalPostForm(NULL, [
'entity_type' => '',
], 'Save');
$this
->drupalGet('admin/config/services/sharemessage/manage/sharemessage_test_label');
$this
->assertSession()
->responseNotContains('<fieldset data-drupal-selector="edit-bundles"');
$this
->drupalPostForm(NULL, [], 'Save');
$this
->drupalGet('admin/structure/types/manage/article/display/teaser');
$this
->assertNoFieldByName('fields[sharemessage__sharemessage_test_label][type]');
$this
->drupalGet('admin/structure/types/manage/page/display/teaser');
$this
->assertNoFieldByName('fields[sharemessage__sharemessage_test_label][type]');
$this
->setEntityRawContent('node', $article);
$this
->assertNoShareButtons($sharemessage_article, 'addthis_16x16_style', TRUE);
$this
->setEntityRawContent('node', $article);
$this
->assertNoShareButtons($sharemessage_page, 'addthis_16x16_style', TRUE);
$this
->drupalGet('admin/config/services/sharemessage/manage/sharemessage_test_label');
$this
->assertOptionSelected('edit-entity-type', '');
$this
->drupalPostForm(NULL, [
'entity_type' => 'node',
], 'Save');
$this
->drupalGet('admin/config/services/sharemessage/manage/sharemessage_test_label');
$this
->assertSession()
->checkboxNotChecked('bundles[article]');
$this
->assertSession()
->checkboxNotChecked('bundles[page]');
$this
->drupalPostForm(NULL, [
'bundles[article]' => 1,
], t('Save'));
$this
->drupalGet('admin/structure/types/manage/article/display/teaser');
$this
->drupalPostForm(NULL, $extra_field, 'Save');
$this
->drupalGet('admin/structure/types/manage/page/display/teaser');
$this
->assertNoFieldByName('fields[sharemessage__sharemessage_test_label][type]');
$this
->setEntityRawContent('node', $article);
$this
->assertShareButtons($sharemessage_article, 'addthis_16x16_style', TRUE);
$this
->setEntityRawContent('node', $page);
$this
->assertNoShareButtons($sharemessage_page, 'addthis_16x16_style', TRUE);
$sharemessage_user = [
'title' => $this->adminUser
->getAccountName() . ', ' . $this->adminUser
->id(),
'message_long' => $sharemessage['message_long'],
'share_url' => $sharemessage['share_url'],
];
$this->renderedEntity = NULL;
$this
->drupalGet('user/' . $this->adminUser
->id());
$this
->assertNoShareButtons($sharemessage_user);
$this
->drupalGet('admin/config/services/sharemessage/manage/sharemessage_test_label');
$this
->assertOptionSelected('edit-entity-type', 'node');
$this
->assertSession()
->checkboxChecked('bundles[article]');
$this
->assertSession()
->checkboxNotChecked('bundles[page]');
$this
->drupalPostForm(NULL, [
'entity_type' => 'user',
], 'Save');
$this
->drupalGet('admin/config/services/sharemessage/manage/sharemessage_test_label');
$this
->assertSession()
->responseNotContains('<fieldset data-drupal-selector="edit-bundles"');
$this
->drupalPostForm(NULL, [
'title' => '[user:name], [user:uid]',
], t('Save'));
$this
->drupalGet('admin/structure/types/manage/article/display/teaser');
$this
->assertNoFieldByName('fields[sharemessage__sharemessage_test_label][type]');
$this
->drupalGet('admin/structure/types/manage/page/display/teaser');
$this
->assertNoFieldByName('fields[sharemessage__sharemessage_test_label][type]');
$this
->drupalGet('admin/config/people/accounts/display');
$this
->drupalPostForm(NULL, [
'display_modes_custom[full]' => TRUE,
], t('Save'));
$this
->drupalGet('admin/config/people/accounts/display/full');
$this
->drupalPostForm(NULL, $extra_field, 'Save');
$this
->setEntityRawContent('node', $article);
$this
->assertNoShareButtons($sharemessage_article, 'addthis_16x16_style', TRUE);
$this
->setEntityRawContent('node', $page);
$this
->assertNoShareButtons($sharemessage_page, 'addthis_16x16_style', TRUE);
$this->renderedEntity = NULL;
$this
->drupalGet('user/' . $this->adminUser
->id());
$this
->assertShareButtons($sharemessage_user, 'addthis_16x16_style', TRUE);
$vocabulary = Vocabulary::load($this->terms[0]
->bundle());
$term = Term::load($this->terms[0]
->id());
$sharemessage_taxonomy = [
'title' => $vocabulary
->label() . ', ' . $term
->label(),
'message_long' => $sharemessage['message_long'],
'share_url' => $sharemessage['share_url'],
];
$this
->drupalGet('taxonomy/term/' . $term
->id());
$this
->assertNoShareButtons($sharemessage_taxonomy);
$this
->drupalGet('admin/config/services/sharemessage/manage/sharemessage_test_label');
$this
->assertOptionSelected('edit-entity-type', 'user');
$this
->drupalPostForm(NULL, [
'entity_type' => 'taxonomy_term',
], 'Save');
$this
->drupalGet('admin/config/services/sharemessage/manage/sharemessage_test_label');
$this
->assertSession()
->checkboxNotChecked('bundles[tags]');
$this
->drupalPostForm(NULL, [
'title' => '[term:vocabulary:name], [term:name]',
], t('Save'));
$this
->drupalGet('admin/structure/types/manage/article/display/teaser');
$this
->assertNoFieldByName('fields[sharemessage__sharemessage_test_label][type]');
$this
->drupalGet('admin/structure/types/manage/page/display/teaser');
$this
->assertNoFieldByName('fields[sharemessage__sharemessage_test_label][type]');
$this
->drupalGet('admin/config/people/accounts/display/full');
$this
->assertNoFieldByName('fields[sharemessage__sharemessage_test_label][type]');
$this
->drupalGet('admin/structure/taxonomy/manage/tags/overview/display');
$this
->drupalPostForm(NULL, [
'display_modes_custom[full]' => TRUE,
], t('Save'));
$this
->drupalGet('admin/structure/taxonomy/manage/tags/overview/display/full');
$this
->drupalPostForm(NULL, $extra_field, 'Save');
$this
->setEntityRawContent('node', $article);
$this
->assertNoShareButtons($sharemessage_article, 'addthis_16x16_style', TRUE);
$this
->setEntityRawContent('node', $page);
$this
->assertNoShareButtons($sharemessage_page, 'addthis_16x16_style', TRUE);
$this->renderedEntity = NULL;
$this
->drupalGet('user/' . $this->adminUser
->id());
$this
->assertNoShareButtons($sharemessage_user, 'addthis_16x16_style', TRUE);
$this
->drupalGet('taxonomy/term/' . $term
->id());
$this
->assertShareButtons($sharemessage_taxonomy, 'addthis_16x16_style', TRUE);
$this->renderedEntity = NULL;
$article_special_char = $this
->drupalCreateNode([
'type' => 'article',
'title' => 'Test with special characters \' " < > & ',
'body' => [
'value' => 'Article body text',
],
]);
$sharemessage = [
'label' => 'Special characters test ',
'id' => 'sharemessage_test_special_characters',
'title' => '[node:title]',
'message_long' => 'Long description',
'message_short' => 'Short description',
];
$this
->drupalPostForm('admin/config/services/sharemessage/add', $sharemessage, t('Save'));
$this
->drupalPostForm('admin/config/services/sharemessage/manage/sharemessage_test_special_characters', [
'entity_type' => 'node',
], 'Save');
$extra_field = [
'fields[sharemessage__sharemessage_test_special_characters][region]' => 'content',
'fields[sharemessage__sharemessage_test_special_characters][weight]' => 105,
];
$this
->drupalPostForm('admin/structure/types/manage/article/display/default', $extra_field, 'Save');
$sharemessage_article = [
'title' => 'Test with special characters ' " < > & ',
'message_long' => $sharemessage['message_long'],
];
$this
->drupalGet('node/' . $article_special_char
->id());
$this
->assertShareButtons($sharemessage_article, 'addthis_16x16_style', TRUE);
}
}