View source
<?php
namespace Drupal\Tests\xmlsitemap\Functional;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\CronRunTrait;
use Drupal\xmlsitemap\Entity\XmlSitemap;
abstract class XmlSitemapTestBase extends BrowserTestBase {
use CronRunTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'node',
'system',
'user',
'xmlsitemap',
];
protected $admin_user;
protected $normal_user;
protected $state;
protected $config;
protected $moduleHandler;
protected $languageManager;
protected $linkStorage;
protected $time;
protected $entityTypeManager;
protected function setUp() {
parent::setUp();
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->state = $this->container
->get('state');
$this->config = $this->container
->get('config.factory')
->getEditable('xmlsitemap.settings');
$this->moduleHandler = $this->container
->get('module_handler');
$this->languageManager = $this->container
->get('language_manager');
$this->linkStorage = $this->container
->get('xmlsitemap.link_storage');
$this->time = $this->container
->get('datetime.time');
if ($this->profile != 'standard') {
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
'settings' => [
'node' => [
'options' => [
'promote' => FALSE,
],
'submitted' => FALSE,
],
],
]);
}
}
public function tearDown() {
$this
->assertNoWatchdogErrors();
parent::tearDown();
}
protected function assertNoResponse($code, $message = '', $group = 'Browser') {
$curl_code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
$match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code;
return $this
->assertFalse($match, $message ? $message : t('HTTP response not expected @code, actual @curl_code', [
'@code' => $code,
'@curl_code' => $curl_code,
]), t('Browser'));
}
protected function drupalGetSitemap(array $context = [], array $options = [], array $headers = []) {
$sitemap = XmlSitemap::loadByContext($context);
if (!$sitemap) {
return $this
->fail('Could not load sitemap by context.');
}
$uri = xmlsitemap_sitemap_uri($sitemap);
return $this
->drupalGet($uri['path'], $options + $uri['options'], $headers);
}
protected function regenerateSitemap() {
$this->state
->set('xmlsitemap_regenerate_needed', TRUE);
$this->state
->set('xmlsitemap_generated_last', 0);
$this
->cronRun();
$this
->assertTrue($this->state
->get('xmlsitemap_generated_last') && !$this->state
->get('xmlsitemap_regenerate_needed'), t('XML sitemaps regenerated and flag cleared.'));
}
protected function assertSitemapLink($entity_type, $entity_id = NULL) {
if (is_array($entity_type)) {
$links = $this->linkStorage
->loadMultiple($entity_type);
$link = $links ? reset($links) : FALSE;
}
else {
$link = $this->linkStorage
->load($entity_type, $entity_id);
}
$this
->assertIsArray($link, 'Link loaded.');
return $link;
}
protected function assertNoSitemapLink($entity_type, $entity_id = NULL) {
if (is_array($entity_type)) {
$links = $this->linkStorage
->loadMultiple($entity_type);
$link = $links ? reset($links) : FALSE;
}
else {
$link = $this->linkStorage
->load($entity_type, $entity_id);
}
$this
->assertFalse($link, 'Link not loaded.');
return $link;
}
protected function assertSitemapLinkVisible($entity_type, $entity_id) {
$link = $this->linkStorage
->load($entity_type, $entity_id);
$this
->assertTrue($link && $link['access'] && $link['status'], t('Sitemap link @type @id is visible.', [
'@type' => $entity_type,
'@id' => $entity_id,
]));
}
protected function assertSitemapLinkNotVisible($entity_type, $entity_id) {
$link = $this->linkStorage
->load($entity_type, $entity_id);
$this
->assertTrue($link && !($link['access'] && $link['status']), t('Sitemap link @type @id is not visible.', [
'@type' => $entity_type,
'@id' => $entity_id,
]));
}
protected function assertSitemapLinkValues($entity_type, $entity_id, array $conditions) {
$link = $this->linkStorage
->load($entity_type, $entity_id);
if (!$link) {
return $this
->fail(sprintf('Could not load sitemap link for %s %s.', $entity_type, $entity_id));
}
foreach ($conditions as $key => $value) {
if ($value === NULL || $link[$key] === NULL) {
$this
->assertSame($value, $link[$key], t('Identical values for @type @id link field @key.', [
'@type' => $entity_type,
'@id' => $entity_id,
'@key' => $key,
]));
}
else {
$this
->assertEquals($value, $link[$key], t('Equal values for @type @id link field @key - @a - @b.', [
'@type' => $entity_type,
'@id' => $entity_id,
'@key' => $key,
'@a' => $link[$key],
'@b' => $value,
]));
}
}
}
protected function assertNotSitemapLinkValues($entity_type, $entity_id, array $conditions) {
$link = $this->linkStorage
->load($entity_type, $entity_id);
if (!$link) {
return $this
->fail(sprintf('Could not load sitemap link for %s %s.', $entity_type, $entity_id));
}
foreach ($conditions as $key => $value) {
if ($value === NULL || $link[$key] === NULL) {
$this
->assertNotSame($value, $link[$key], t('Not identical values for @type @id link field @key.', [
'@type' => $entity_type,
'@id' => $entity_id,
'@key' => $key,
]));
}
else {
$this
->assertNotEquals($value, $link[$key], t('Not equal values for link @type @id field @key.', [
'@type' => $entity_type,
'@id' => $entity_id,
'@key' => $key,
]));
}
}
}
protected function assertRawSitemapLinks() {
$links = func_get_args();
foreach ($links as $link) {
$path = Url::fromUri('internal:' . $link['loc'], [
'language' => xmlsitemap_language_load($link['language']),
'absolute' => TRUE,
])
->toString();
$this
->assertSession()
->responseContains($link['loc']);
}
}
protected function assertNoRawSitemapLinks() {
$links = func_get_args();
foreach ($links as $link) {
$path = Url::fromUri('internal:' . $link['loc'], [
'language' => xmlsitemap_language_load($link['language']),
'absolute' => TRUE,
])
->toString();
$this
->assertSession()
->responseNotContains($link['loc']);
}
}
protected function addSitemapLink(array $link = []) {
$last_id =& drupal_static(__FUNCTION__, 1);
$link += [
'type' => 'testing',
'subtype' => '',
'id' => $last_id,
'access' => 1,
'status' => 1,
];
$link += [
'loc' => '/' . $link['type'] . '-' . $link['id'],
];
$last_id = max($last_id, $link['id']) + 1;
$this->linkStorage
->save($link);
return $link;
}
protected function assertFlag($variable, $assert_value = TRUE, $reset_if_true = TRUE) {
$value = xmlsitemap_var($variable);
if ($reset_if_true && $value) {
$state_variables = xmlsitemap_state_variables();
if (isset($state_variables[$variable])) {
$this->state
->set($variable, FALSE);
}
else {
$this->config
->set($variable, FALSE)
->save();
}
}
$this
->assertEquals($assert_value, $value, "{$variable} is " . ($assert_value ? 'TRUE' : 'FALSE'));
return $value == $assert_value;
}
protected function assertXMLSitemapProblems($problem_text = FALSE) {
$this
->drupalGet('admin/config/search/xmlsitemap');
$this
->assertSession()
->pageTextContains('One or more problems were detected with your XML sitemap configuration');
if ($problem_text) {
$this
->clickLink('status report');
$this
->assertSession()
->pageTextContains($problem_text);
}
}
protected function assertNoXMLSitemapProblems() {
$this
->drupalGet('admin/config/search/xmlsitemap');
$this
->assertSession()
->pageTextNotContains('One or more problems were detected with your XML sitemap configuration');
}
protected function getWatchdogMessages(array $conditions = [], $reset = FALSE) {
static $seen_ids = [];
if (!$this->moduleHandler
->moduleExists('dblog') || $reset) {
$seen_ids = [];
return [];
}
$query = \Drupal::database()
->select('watchdog');
$query
->fields('watchdog', [
'wid',
'type',
'severity',
'message',
'variables',
'timestamp',
]);
foreach ($conditions as $field => $value) {
if ($field == 'variables' && !is_string($value)) {
$value = serialize($value);
}
$query
->condition($field, $value);
}
if ($seen_ids) {
$query
->condition('wid', $seen_ids, 'NOT IN');
}
$query
->orderBy('timestamp');
$messages = $query
->execute()
->fetchAllAssoc('wid');
$seen_ids = array_merge($seen_ids, array_keys($messages));
return $messages;
}
protected function assertWatchdogMessage(array $conditions, $message = 'Watchdog message found.') {
$this
->assertNotEmpty($this
->getWatchdogMessages($conditions), $message);
}
protected function assertNoWatchdogMessage(array $conditions, $message = 'Watchdog message not found.') {
$this
->assertEmpty($this
->getWatchdogMessages($conditions), $message);
}
protected function assertNoWatchdogErrors() {
$messages = $this
->getWatchdogMessages();
$verbose = [];
foreach ($messages as $message) {
$message->text = $this
->formatWatchdogMessage($message);
if (in_array($message->severity, [
RfcLogLevel::EMERGENCY,
RfcLogLevel::ALERT,
RfcLogLevel::CRITICAL,
RfcLogLevel::ERROR,
RfcLogLevel::WARNING,
])) {
$this
->fail($message->text);
}
$verbose[] = $message->text;
}
if ($verbose) {
array_unshift($verbose, '<h2>Watchdog messages</h2>');
$this
->verbose(implode('<br />', $verbose), 'Watchdog messages from test run');
}
$this
->getWatchdogMessages([], TRUE);
}
private function formatWatchdogMessage($message) {
static $levels;
if (!isset($levels)) {
module_load_include('admin.inc', 'dblog');
$levels = RfcLogLevel::getLevels();
}
return t('@timestamp - @severity - @type - @message', [
'@timestamp' => $message->timestamp,
'@severity' => $levels[$message->severity],
'@type' => $message->type,
]);
}
protected function verbose($verbose_message, $message = 'Verbose message') {
if ($id = parent::verbose($verbose_message)) {
$url = file_create_url($this->originalFileDirectory . '/simpletest/verbose/' . get_class($this) . '-' . $id . '.html');
$message_url = Url::fromUri($url, [
'attributes' => [
'target' => '_blank',
],
]);
$this
->error(\Drupal::linkGenerator()
->generate($message, $message_url), 'User notice');
}
}
}