View source
<?php
namespace Drupal\Tests\thunder\FunctionalJavascript;
class MetaInformationTest extends ThunderJavascriptTestBase {
use ThunderArticleTestTrait;
use ThunderMetaTagTrait;
protected static $defaultUserRole = 'administrator';
protected static $globalMetaTags = [
'basic title' => 'Global Title',
'basic keywords' => 'Thunder,CMS,Burda',
'basic abstract' => '[random]',
'basic description' => '[random]',
];
protected static $contentMetaTags = [
'basic title' => '[node:title]',
'basic abstract' => '[random]',
];
protected static $articleMetaTags = [
'basic title' => 'Test [node:field_teaser_text]',
'basic description' => '[random]',
'advanced robots' => 'index, follow, noydir',
'advanced referrer' => 'no-referrer-when-downgrade',
'open_graph og:image' => '[node:field_teaser_media:entity:field_image:facebook]',
'open_graph og:image:type' => '[node:field_teaser_media:entity:field_image:facebook:mimetype]',
'open_graph og:image:height' => '[node:field_teaser_media:entity:field_image:facebook:height]',
'open_graph og:image:width' => '[node:field_teaser_media:entity:field_image:facebook:width]',
'open_graph og:description' => '[node:field_teaser_text]',
'open_graph og:title' => '[node:field_seo_title]',
'open_graph og:site_name' => '[node:title]',
'open_graph og:type' => 'article',
];
protected static $customMetaTags = [
'basic title' => 'Custom [node:field_teaser_text]',
'basic description' => '[random]',
'advanced robots' => 'follow',
'advanced referrer' => 'no-referrer',
];
protected static $tokens = [
'[node:field_seo_title]' => 'Test SEO Title',
'[node:field_teaser_text]' => 'Test Teaser Text',
'[node:title]' => 'Test Note Title',
'[node:field_teaser_media:entity:field_image:facebook]' => 'LIKE:/files/styles/facebook/public/2016-05/thunder.jpg?',
'[node:field_teaser_media:entity:field_image:facebook:mimetype]' => 'image/jpeg',
'[node:field_teaser_media:entity:field_image:facebook:height]' => '630',
'[node:field_teaser_media:entity:field_image:facebook:width]' => '1200',
];
protected function setMetaTagConfigurationForUrl($pageUrl, array $configuration) {
$this
->drupalGet($pageUrl);
$page = $this
->getSession()
->getPage();
$this
->expandAllTabs();
$this
->setFieldValues($page, $this
->generateMetaTagFieldValues($configuration));
$this
->scrollElementInView('[name="op"]');
$page
->find('xpath', '//input[@name="op"]')
->click();
}
protected function createArticleWithFields(array $fieldValues = []) {
$fieldValues += [
'field_channel' => 1,
'title[0][value]' => static::$tokens['[node:title]'],
'field_seo_title[0][value]' => static::$tokens['[node:field_seo_title]'],
'field_teaser_text[0][value]' => static::$tokens['[node:field_teaser_text]'],
];
$this
->articleFillNew($fieldValues);
$this
->selectMedia('field_teaser_media', 'image_browser', [
'media:1',
]);
$this
->clickSave();
}
protected function checkSavedConfiguration($configurationUrl, array $configuration) {
$this
->drupalGet('admin/config/search/metatag');
$page = $this
->getSession()
->getPage();
$this
->expandAllTabs();
foreach ($configuration as $metaTagName => $metaTagValue) {
$metaTag = explode(' ', $metaTagName);
$fieldName = $this
->getMetaTagFieldName($metaTag[1]);
$this
->assertNotEquals(NULL, $page
->find('xpath', '//tr[.//a[contains(@href, "/' . $configurationUrl . '")]]/td[1]//table//tr[./td[text()="' . $fieldName . ':"] and ./td[text()="' . $metaTagValue . '"]]'));
}
}
public function testArticleMetaTags() {
$globalConfigs = $this
->generateMetaTagConfiguration([
static::$globalMetaTags,
]);
$contentConfigs = $this
->generateMetaTagConfiguration([
static::$contentMetaTags,
]);
$articleConfigs = $this
->generateMetaTagConfiguration([
static::$articleMetaTags,
]);
$customConfigs = $this
->generateMetaTagConfiguration([
static::$customMetaTags,
]);
$checkArticleConfigs = $this
->generateMetaTagConfiguration([
$globalConfigs,
$contentConfigs,
$articleConfigs,
]);
$checkArticleMetaTags = $this
->replaceTokens($checkArticleConfigs, static::$tokens);
$checkCustomConfigs = $this
->generateMetaTagConfiguration([
$checkArticleConfigs,
$customConfigs,
]);
$checkCustomMetaTags = $this
->replaceTokens($checkCustomConfigs, static::$tokens);
$configurationUrl = 'admin/config/search/metatag/global';
$this
->setMetaTagConfigurationForUrl($configurationUrl, $globalConfigs);
$this
->checkSavedConfiguration($configurationUrl, $globalConfigs);
$configurationUrl = 'admin/config/search/metatag/node';
$this
->setMetaTagConfigurationForUrl($configurationUrl, $contentConfigs);
$this
->checkSavedConfiguration($configurationUrl, $contentConfigs);
$configurationUrl = 'admin/config/search/metatag/node__article';
$this
->setMetaTagConfigurationForUrl($configurationUrl, $articleConfigs);
$this
->checkSavedConfiguration($configurationUrl, $articleConfigs);
$this
->createArticleWithFields();
$this
->checkMetaTags($checkArticleMetaTags);
$this
->createArticleWithFields($this
->generateMetaTagFieldValues($checkCustomConfigs, 'field_meta_tags[0]'));
$this
->checkMetaTags($checkCustomMetaTags);
}
public function testArticleScheduling() {
$articleId = 10;
$startTimestamp = strtotime('-2 days');
$endTimestamp = strtotime('+1 day');
$fieldValues = [
'publish_on[0][value][date]' => date('Y-m-d', $startTimestamp),
'publish_on[0][value][time]' => date('H:i:s', $startTimestamp),
'unpublish_on[0][value][date]' => date('Y-m-d', $endTimestamp),
'unpublish_on[0][value][time]' => date('H:i:s', $endTimestamp),
'publish_state[0]' => 'published',
'unpublish_state[0]' => 'unpublished',
];
$this
->createArticleWithFields($fieldValues);
$this
->drupalGet('node/' . $articleId);
$this
->assertSession()
->elementExists('xpath', '//div[@class="content"]/article[contains(@class, "node--unpublished")]');
$this
->runCron();
$this
->drupalGet('node/' . $articleId);
$this
->assertSession()
->elementNotExists('xpath', '//div[@class="content"]/article[contains(@class, "node--unpublished")]');
$this
->drupalGet('node/' . $articleId . '/edit');
$page = $this
->getSession()
->getPage();
$unPublishDiffSeconds = 5;
$unPublishTimestamp = strtotime("+{$unPublishDiffSeconds} seconds");
$unPublishFieldValues = [
'unpublish_on[0][value][date]' => date('Y-m-d', $unPublishTimestamp),
'unpublish_on[0][value][time]' => date('H:i:s', $unPublishTimestamp),
'unpublish_state[0]' => 'unpublished',
];
$this
->expandAllTabs();
$this
->setFieldValues($page, $unPublishFieldValues);
$this
->clickSave();
$this
->drupalGet('node/' . $articleId);
$this
->assertSession()
->elementNotExists('xpath', '//div[@class="content"]/article[contains(@class, "node--unpublished")]');
sleep($unPublishDiffSeconds + 2);
$this
->runCron();
$this
->drupalGet('node/' . $articleId);
$this
->assertSession()
->elementExists('xpath', '//div[@class="content"]/article[contains(@class, "node--unpublished")]');
}
public function getSiteMapDomElements($content, $xpathQuery) {
$domDoc = new \DOMDocument();
$domDoc
->loadXML($content);
$xpath = new \DOMXpath($domDoc);
$xpath
->registerNamespace('sm', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$elements = $xpath
->query($xpathQuery);
return $elements;
}
public function testSiteMap() {
$articleId = 10;
$articleUrl = 'test-sitemap-seo-title';
$customFields = [
'field_seo_title[0][value]' => $articleUrl,
];
$this
->createArticleWithFields($customFields);
$this
->drupalGet('node/' . $articleId . '/edit');
$this
->setModerationState('published');
$this
->clickSave();
$this
->runCron();
$this
->drupalGet('sitemap.xml');
$content = $this
->getSession()
->getPage()
->getContent();
$domElements = $this
->getSiteMapDomElements($content, '//sm:loc[contains(text(),"/' . $articleUrl . '")]/parent::sm:url/sm:priority');
$this
->assertEquals(1, $domElements->length);
$this
->assertEquals('0.5', $domElements
->item(0)->nodeValue);
$this
->getSession()
->visit($this
->buildUrl('node/' . $articleId . '/edit'));
$page = $this
->getSession()
->getPage();
$this
->expandAllTabs();
$this
->setFieldValues($page, [
'simple_sitemap_priority' => '0.9',
]);
$this
->clickSave();
$this
->runCron();
$this
->drupalGet('sitemap.xml');
$content = $this
->getSession()
->getPage()
->getContent();
$domElements = $this
->getSiteMapDomElements($content, '//sm:loc[contains(text(),"/' . $articleUrl . '")]/parent::sm:url/sm:priority');
$this
->assertEquals(1, $domElements->length);
$this
->assertEquals('0.9', $domElements
->item(0)->nodeValue);
$this
->getSession()
->visit($this
->buildUrl('admin/config/search/simplesitemap'));
$page = $this
->getSession()
->getPage();
$this
->setFieldValues($page, [
'max_links' => '2',
]);
$page
->find('xpath', '//input[@id="edit-submit"]')
->click();
$this
->runCron();
$this
->drupalGet('sitemap.xml');
$content = $this
->getSession()
->getPage()
->getContent();
$domElements = $this
->getSiteMapDomElements($content, '(//sm:loc)[last()]');
$lastSiteMapUrl = $domElements
->item(0)->nodeValue;
$this
->assertStringEndsWith('/sitemap.xml', $lastSiteMapUrl);
$this
->getSession()
->visit($this
->buildUrl('sitemaps/3/sitemap.xml'));
$content = $this
->getSession()
->getPage()
->getContent();
$domElements = $this
->getSiteMapDomElements($content, '//sm:loc[contains(text(),"/' . $articleUrl . '")]/parent::sm:url/sm:priority');
$this
->assertEquals(1, $domElements->length);
$this
->assertEquals('0.9', $domElements
->item(0)->nodeValue);
$this
->getSession()
->visit($this
->buildUrl('node/' . $articleId . '/edit'));
$page = $this
->getSession()
->getPage();
$this
->expandAllTabs();
$this
->scrollElementInView('[name="simple_sitemap_index_content"]');
$page
->find('css', '[name="simple_sitemap_index_content"]')
->click();
$this
->clickSave();
$this
->runCron();
$this
->drupalGet('sitemaps/3/sitemap.xml');
$content = $this
->getSession()
->getPage()
->getContent();
$domElements = $this
->getSiteMapDomElements($content, '//sm:loc[contains(text(),"/' . $articleUrl . '")]');
$this
->assertEquals(0, $domElements->length);
$this
->getSession()
->visit($this
->buildUrl('node/' . $articleId . '/edit'));
}
}