View source
<?php
namespace Drupal\Tests\xmlsitemap\Functional;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\user\Entity\Role;
class XmlSitemapNodeFunctionalTest extends XmlSitemapTestBase {
public static $modules = [
'taxonomy',
];
protected $nodes = [];
protected $entityTypeManager;
protected function setUp() {
parent::setUp();
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->admin_user = $this
->drupalCreateUser([
'administer nodes',
'bypass node access',
'administer content types',
'administer xmlsitemap',
'administer taxonomy',
]);
$this->normal_user = $this
->drupalCreateUser([
'create page content',
'edit any page content',
'access content',
'view own unpublished content',
]);
$user_role = Role::load(AccountInterface::ANONYMOUS_ROLE);
$user_role
->grantPermission('access content');
$user_role
->save();
xmlsitemap_link_bundle_enable('node', 'article');
xmlsitemap_link_bundle_enable('node', 'page');
xmlsitemap_link_bundle_settings_save('node', 'page', [
'status' => 1,
'priority' => 0.6,
'changefreq' => XMLSITEMAP_FREQUENCY_WEEKLY,
]);
$vocabulary = Vocabulary::create([
'name' => 'Tags',
'description' => $this
->randomMachineName(),
'vid' => 'tags',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'help' => '',
]);
$vocabulary
->save();
xmlsitemap_link_bundle_enable('taxonomy_term', 'tags');
$field_name = 'tags';
FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'entity_reference',
'settings' => [
'target_type' => 'taxonomy_term',
],
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
])
->save();
FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'bundle' => 'page',
])
->save();
EntityFormDisplay::load('node.page.default')
->setComponent($field_name, [
'type' => 'entity_reference_autocomplete_tags',
])
->save();
EntityViewDisplay::load('node.page.default')
->setComponent($field_name, [
'type' => 'entity_reference_label',
])
->save();
EntityViewDisplay::load('node.page.teaser')
->setComponent($field_name, [
'type' => 'entity_reference_label',
])
->save();
}
public function testTagsField() {
$this
->drupalLogin($this->admin_user);
$this
->drupalGet('node/add/page');
$title_key = 'title[0][value]';
$body_key = 'body[0][value]';
$edit = [];
$edit[$title_key] = $this
->randomMachineName(8);
$edit[$body_key] = $this
->randomMachineName(16);
$edit['tags[target_id]'] = 'tag1, tag2, tag3';
$edit['status[value]'] = TRUE;
$this
->drupalPostForm('node/add/page', $edit, t('Save'));
$tags = Term::loadMultiple();
foreach ($tags as $tag) {
$this
->assertSitemapLinkValues('taxonomy_term', $tag
->id(), [
'status' => 0,
'priority' => 0.5,
'changefreq' => 0,
]);
$tag
->delete();
}
xmlsitemap_link_bundle_settings_save('taxonomy_term', 'tags', [
'status' => 1,
'priority' => 0.2,
'changefreq' => XMLSITEMAP_FREQUENCY_HOURLY,
]);
$this
->drupalPostForm('node/add/page', $edit, t('Save'));
$tags = Term::loadMultiple();
foreach ($tags as $tag) {
$this
->assertSitemapLinkValues('taxonomy_term', $tag
->id(), [
'status' => 1,
'priority' => 0.2,
'changefreq' => XMLSITEMAP_FREQUENCY_HOURLY,
]);
$tag
->delete();
}
}
public function testNodeSettings() {
$node = $this
->drupalCreateNode([
'publish' => 0,
'uid' => $this->normal_user
->id(),
]);
$this
->assertSitemapLinkValues('node', $node
->id(), [
'access' => 1,
'status' => 1,
'priority' => 0.6,
'status_override' => 0,
'priority_override' => 0,
'changefreq' => XMLSITEMAP_FREQUENCY_WEEKLY,
]);
$this
->drupalLogin($this->normal_user);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->fieldNotExists('xmlsitemap[status]');
$this
->assertSession()
->fieldNotExists('xmlsitemap[priority]');
$edit = [
'title[0][value]' => 'Test node title',
'body[0][value]' => 'Test node body',
];
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, t('Save'));
$this
->assertSession()
->pageTextContains('Basic page Test node title has been updated.');
$this
->assertSitemapLinkValues('node', $node
->id(), [
'access' => 1,
'status' => 1,
'priority' => 0.6,
'status_override' => 0,
'priority_override' => 0,
'changefreq' => XMLSITEMAP_FREQUENCY_WEEKLY,
]);
$this
->drupalLogin($this->admin_user);
$this
->drupalGet('node/add/page');
$this
->assertSession()
->fieldExists('xmlsitemap[status]');
$this
->assertSession()
->fieldExists('xmlsitemap[priority]');
$this
->assertSession()
->fieldExists('xmlsitemap[changefreq]');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$edit = [
'xmlsitemap[status]' => 1,
'xmlsitemap[priority]' => 0.9,
'xmlsitemap[changefreq]' => XMLSITEMAP_FREQUENCY_ALWAYS,
];
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, t('Save'));
$this
->assertSession()
->pageTextContains('Basic page Test node title has been updated.');
$this
->assertSitemapLinkValues('node', $node
->id(), [
'access' => 1,
'status' => 1,
'priority' => 0.9,
'status_override' => 1,
'priority_override' => 1,
'changefreq' => XMLSITEMAP_FREQUENCY_ALWAYS,
]);
$edit = [
'xmlsitemap[status]' => 'default',
'xmlsitemap[priority]' => 'default',
];
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, t('Save'));
$this
->assertSession()
->pageTextContains('Basic page Test node title has been updated.');
$this
->assertSitemapLinkValues('node', $node
->id(), [
'access' => 1,
'status' => 1,
'priority' => 0.6,
'status_override' => 0,
'priority_override' => 0,
]);
}
public function testTypeSettings() {
$this
->drupalLogin($this->admin_user);
$node_old = $this
->drupalCreateNode();
$this
->assertSitemapLinkValues('node', $node_old
->id(), [
'status' => 1,
'priority' => 0.6,
'changefreq' => XMLSITEMAP_FREQUENCY_WEEKLY,
]);
$edit = [
'xmlsitemap[status]' => 0,
'xmlsitemap[priority]' => '0.0',
];
$this
->drupalPostForm('admin/config/search/xmlsitemap/settings/node/page', $edit, t('Save configuration'));
$this
->assertSession()
->pageTextContains('The configuration options have been saved.');
$node = $this
->drupalCreateNode();
$this
->assertSitemapLinkValues('node', $node
->id(), [
'status' => 0,
'priority' => 0.0,
]);
$this
->assertSitemapLinkValues('node', $node_old
->id(), [
'status' => 0,
'priority' => 0.0,
]);
$node
->delete();
$node_old
->delete();
$this
->drupalPostForm('admin/structure/types/manage/page/delete', [], t('Delete'));
$this
->assertSession()
->pageTextContains('The content type Basic page has been deleted.');
$this
->assertEmpty($this->linkStorage
->loadMultiple([
'type' => 'node',
'subtype' => 'page',
]), 'Nodes with deleted node type removed from {xmlsitemap}.');
}
public function testCron() {
$limit = 5;
$this->config
->set('batch_limit', $limit)
->save();
$nodes = [];
for ($i = 1; $i <= $limit + 1; $i++) {
$node = $this
->drupalCreateNode();
array_push($nodes, $node);
sleep(1);
}
\Drupal::database()
->delete('xmlsitemap')
->condition('type', 'node')
->execute();
xmlsitemap_cron();
for ($i = 1; $i <= $limit + 1; $i++) {
$node = array_pop($nodes);
if ($i != 1) {
$this
->assertSitemapLinkValues('node', $node
->id(), [
'access' => 1,
'status' => 1,
]);
}
else {
$this
->assertNoSitemapLink([
'type' => 'node',
'id' => $node
->id(),
]);
}
}
}
}