View source
<?php
namespace Drupal\Tests\sharerich\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Component\Serialization\Json;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class SharerichTests extends BrowserTestBase {
use StringTranslationTrait;
protected $defaultTheme = 'stark';
protected $profile = 'minimal';
protected static $modules = [
'block',
'token',
'contextual',
'node',
'field',
'text',
'sharerich',
];
protected $adminUser;
protected $services;
protected function setUp() {
parent::setUp();
$this->services = [
'facebook',
'email',
'tumblr',
'twitter',
];
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'administer sharerich',
'administer blocks',
'access contextual links',
], 'Sharerich Admin', TRUE);
}
protected function assertElementByXPath($xpath, array $arguments = array(), $message, $group = 'Other') {
$elements = $this
->xpath($xpath, $arguments);
return $this
->assertTrue(!empty($elements[0]), $message, $group);
}
function testLinkToConfig() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/modules');
$link = $this
->xpath('//a[contains(@href, :href) and contains(@id, :id)]', [
':href' => 'admin/structure/sharerich',
':id' => 'edit-modules-sharerich-links-configure',
]);
$this
->assertTrue(count($link) === 1, 'Link to config is present');
}
function testAdminUI() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/structure/sharerich/default');
$element = $this
->xpath('//input[@type="text" and @id="edit-label" and @value="Default"]');
$this
->assertTrue(count($element) === 1, 'The label is correct.');
foreach ($this->services as $item) {
$element = $this
->xpath('//input[@type="checkbox" and @name="services[' . $item . '][enabled]" and @checked="checked"]');
$this
->assertTrue(count($element) === 1, $this
->t('The :item is checked.', [
':item' => ucfirst($item),
]));
$actual = $this
->xpath('//textarea[@name="services[' . $item . '][markup]"]');
$expected = $this
->xpath('//input[@type="hidden"][@name="services[' . $item . '][default_markup]"]/@value');
$actual = preg_replace('/(\\r\\n|\\r|\\n|\\s|\\t)/s', "", $actual[0]
->getText());
$expected = preg_replace('/(\\r\\n|\\r|\\n|\\s|\\t)/s', "", $expected[0]
->getText());
$this
->assertEquals($actual, $expected, $this
->t('The :item widget is correct.', [
':item' => $item,
]));
}
}
function testBlock() {
$this
->drupalLogin($this->adminUser);
$node_type = NodeType::create([
'type' => 'page',
'name' => 'Basic page',
]);
$node_type
->save();
$page = Node::create([
'type' => 'page',
'title' => 'Sharerich page',
]);
$page
->save();
$url = \Drupal\Core\Url::fromRoute('entity.node.canonical', [
'node' => $page
->id(),
]);
$this
->drupalGet($url
->toString());
$text = $this
->xpath('//div[@id="block-sharerich-block"]//h2');
$this
->assertEqual($text[0]
->getText(), $this
->t('Share this'), t("The title of sharerich block is correct"));
$elements = $this
->xpath('//ul[contains(@class, :class)]/li', [
':class' => 'sharerich-buttons',
]);
$this
->assertTrue(!empty($elements), 'Found a sticky sharerich block');
foreach ($this->services as $item) {
$text = $this
->xpath('//div[@id="block-sharerich-block"]//ul/li[@class="rrssb-' . $item . '"]//span[@class="rrssb-text"]');
$this
->assertEqual($text[0]
->getText(), $item, $this
->t('The text of :item button is correct', [
':item' => $item,
]));
}
$this
->assertElementByXPath('//div[@id="block-sharerich-block"]//ul/li[contains(@class, :li_class)]/a[contains(@href, :href)]', [
':li_class' => 'rrssb-email',
':href' => 'mailto:?subject=Sharerich%20page&body=http',
], "Email Tokens rendered correctly.");
$this
->assertElementByXPath('//div[@id="block-sharerich-block"]//ul/li[contains(@class, :li_class)]/a[contains(@href, :href)]', [
':li_class' => 'rrssb-facebook',
':href' => 'https://www.facebook.com/sharer/sharer.php?u=http',
], "Facebook Tokens rendered correctly.");
$this
->assertElementByXPath('//div[@id="block-sharerich-block"]//ul/li[contains(@class, :li_class)]/a[contains(@href, :href)]', [
':li_class' => 'rrssb-tumblr',
':href' => 'http://www.tumblr.com/share?s=&v=3&t=Sharerich%20page&u=http',
], "Tumblr Tokens rendered correctly.");
$this
->assertElementByXPath('//div[@id="block-sharerich-block"]//ul/li[contains(@class, :li_class)]/a[contains(@href, :href)]', [
':li_class' => 'rrssb-twitter',
':href' => 'https://twitter.com/intent/tweet?url=http',
], "Twitter Tokens rendered correctly.");
$id = 'block:block=sharerich_block:langcode=en|sharerich:sharerich=default:langcode=en';
$this
->assertElementByXPath('//div[@data-contextual-id="' . $id . '"]', [], "Contextual link placeholder is rendered correctly.");
}
}