View source
<?php
namespace Drupal\Tests\xmlsitemap\Functional;
class XmlSitemapUnitTest extends XmlSitemapTestBase {
protected function setUp() {
parent::setUp();
$this->admin_user = $this
->drupalCreateUser([
'access content',
'administer site configuration',
'administer xmlsitemap',
]);
}
public function testAssertFlag() {
$this->state
->set('xmlsitemap_rebuild_needed', TRUE);
$this
->assertTrue(xmlsitemap_var('xmlsitemap_rebuild_needed'));
$this
->assertTrue($this
->assertFlag('xmlsitemap_rebuild_needed', TRUE, FALSE));
$this
->assertTrue(xmlsitemap_var('xmlsitemap_rebuild_needed'));
$this
->assertTrue($this
->assertFlag('xmlsitemap_rebuild_needed', TRUE, TRUE));
$this
->assertFalse(xmlsitemap_var('xmlsitemap_rebuild_needed'));
$this
->assertTrue($this
->assertFlag('xmlsitemap_rebuild_needed', FALSE, FALSE));
$this
->assertFalse(xmlsitemap_var('xmlsitemap_rebuild_needed'));
}
public function testGetChangefreq() {
$values = [
0,
mt_rand(1, XMLSITEMAP_FREQUENCY_ALWAYS),
mt_rand(XMLSITEMAP_FREQUENCY_ALWAYS + 1, XMLSITEMAP_FREQUENCY_HOURLY),
mt_rand(XMLSITEMAP_FREQUENCY_HOURLY + 1, XMLSITEMAP_FREQUENCY_DAILY),
mt_rand(XMLSITEMAP_FREQUENCY_DAILY + 1, XMLSITEMAP_FREQUENCY_WEEKLY),
mt_rand(XMLSITEMAP_FREQUENCY_WEEKLY + 1, XMLSITEMAP_FREQUENCY_MONTHLY),
mt_rand(XMLSITEMAP_FREQUENCY_MONTHLY + 1, XMLSITEMAP_FREQUENCY_YEARLY),
mt_rand(XMLSITEMAP_FREQUENCY_YEARLY + 1, mt_getrandmax()),
];
$expected = [
FALSE,
'always',
'hourly',
'daily',
'weekly',
'monthly',
'yearly',
'never',
];
foreach ($values as $i => $value) {
$actual = xmlsitemap_get_changefreq($value);
$this
->assertSame($expected[$i], $actual);
}
}
public function testGetChunkCount() {
$this->config
->set('chunk_size', 4)
->save();
$database = \Drupal::database();
$count = $database
->query("SELECT COUNT(id) FROM {xmlsitemap}")
->fetchField();
for ($i = $count; $i < 4; $i++) {
$this
->addSitemapLink();
$this
->assertEquals(1, xmlsitemap_get_chunk_count(TRUE));
}
$this
->assertEquals(4, $database
->query("SELECT COUNT(id) FROM {xmlsitemap}")
->fetchField());
$this
->addSitemapLink([
'status' => FALSE,
]);
$this
->assertEquals(1, xmlsitemap_get_chunk_count(TRUE));
$this
->addSitemapLink();
$this
->assertEquals(2, xmlsitemap_get_chunk_count(TRUE));
$database
->query("UPDATE {xmlsitemap} SET status = 0");
$this
->assertEquals(1, xmlsitemap_get_chunk_count(TRUE));
$this
->assertEquals(0, xmlsitemap_get_link_count());
$database
->query("DELETE FROM {xmlsitemap}");
$this
->assertEquals(0, $database
->query("SELECT COUNT(id) FROM {xmlsitemap}")
->fetchField());
$this
->assertEquals(1, xmlsitemap_get_chunk_count(TRUE));
}
public function testCalculateChangefreq() {
$request_time = $this->time
->getRequestTime();
$values = [
[],
[
$request_time,
],
[
$request_time,
$request_time - 200,
],
[
$request_time - 200,
$request_time,
$request_time - 600,
],
];
$expected = [
0,
0,
200,
300,
];
foreach ($values as $i => $value) {
$actual = xmlsitemap_calculate_changefreq($value);
$this
->assertEquals($expected[$i], $actual);
}
}
public function testRecalculateChangefreq() {
$request_time = $this->time
->getRequestTime();
$value = [
'lastmod' => $request_time - 1000,
'changefreq' => 0,
'changecount' => 0,
];
$expecteds = [
[
'lastmod' => $request_time,
'changefreq' => 1000,
'changecount' => 1,
],
[
'lastmod' => $request_time,
'changefreq' => 500,
'changecount' => 2,
],
[
'lastmod' => $request_time,
'changefreq' => 333,
'changecount' => 3,
],
];
foreach ($expecteds as $expected) {
xmlsitemap_recalculate_changefreq($value);
$this
->assertEquals($expected, $value);
}
}
public function testSaveLink() {
$link = [
'type' => 'testing',
'subtype' => '',
'id' => 1,
'loc' => '/testing',
'status' => 1,
];
$this->linkStorage
->save($link);
$this
->assertFlag('xmlsitemap_regenerate_needed', TRUE);
$link['status'] = 0;
$this->linkStorage
->save($link);
$this
->assertFlag('xmlsitemap_regenerate_needed', TRUE);
$link['priority'] = 0.5;
$link['loc'] = '/new_location';
$link['status'] = 1;
$this->linkStorage
->save($link);
$this
->assertFlag('xmlsitemap_regenerate_needed', TRUE);
$link['priority'] = 0.0;
$this->linkStorage
->save($link);
$this
->assertFlag('xmlsitemap_regenerate_needed', TRUE);
$link['priority'] = 0.1;
$this->linkStorage
->save($link);
$this
->assertFlag('xmlsitemap_regenerate_needed', TRUE);
$link['priority'] = 1.0;
$this->linkStorage
->save($link);
$this
->assertFlag('xmlsitemap_regenerate_needed', TRUE);
$link['priority'] = 1;
$this->linkStorage
->save($link);
$this
->assertFlag('xmlsitemap_regenerate_needed', FALSE);
$link['priority'] = 0;
$this->linkStorage
->save($link);
$this
->assertFlag('xmlsitemap_regenerate_needed', TRUE);
$link['priority'] = 0.5;
$this->linkStorage
->save($link);
$this
->assertFlag('xmlsitemap_regenerate_needed', TRUE);
$link['priority'] = 0.5;
$link['priority_override'] = 0;
$link['status'] = 1;
$this->linkStorage
->save($link);
$this
->assertFlag('xmlsitemap_regenerate_needed', FALSE);
}
public function testLinkDelete() {
$link1 = $this
->addSitemapLink([
'loc' => '/testing1',
'status' => 0,
]);
$link2 = $this
->addSitemapLink([
'loc' => '/testing1',
'status' => 1,
]);
$link3 = $this
->addSitemapLink([
'status' => 0,
]);
$this->state
->set('xmlsitemap_regenerate_needed', FALSE);
$deleted = $this->linkStorage
->deleteMultiple([
'loc' => '/testing1',
]);
$this
->assertEquals(2, $deleted);
$this
->assertEmpty($this->linkStorage
->load($link1['type'], $link1['id']));
$this
->assertEmpty($this->linkStorage
->load($link2['type'], $link2['id']));
$this
->assertNotEmpty($this->linkStorage
->load($link3['type'], $link3['id']));
$this
->assertFlag('xmlsitemap_regenerate_needed', TRUE);
$deleted = $this->linkStorage
->delete($link3['type'], $link3['id']);
$this
->assertEquals(1, $deleted);
$this
->assertEmpty($this->linkStorage
->load($link3['type'], $link3['id']));
$this
->assertFlag('xmlsitemap_regenerate_needed', FALSE);
}
public function testUpdateLinks() {
$links = [];
$links[1] = $this
->addSitemapLink([
'subtype' => 'group1',
]);
$links[2] = $this
->addSitemapLink([
'subtype' => 'group1',
]);
$links[3] = $this
->addSitemapLink([
'subtype' => 'group2',
]);
$this->state
->set('xmlsitemap_regenerate_needed', FALSE);
$updated = $this->linkStorage
->updateMultiple([
'status' => 0,
], [
'type' => 'testing',
'subtype' => 'group1',
'status_override' => 0,
]);
$this
->assertEquals(2, $updated);
$this
->assertFlag('xmlsitemap_regenerate_needed', TRUE);
$updated = $this->linkStorage
->updateMultiple([
'priority' => 0.0,
], [
'type' => 'testing',
'subtype' => 'group1',
'priority_override' => 0,
]);
$this
->assertEquals(2, $updated);
$this
->assertFlag('xmlsitemap_regenerate_needed', FALSE);
$updated = $this->linkStorage
->updateMultiple([
'subtype' => 'group2',
], [
'type' => 'testing',
'subtype' => 'group1',
]);
$this
->assertEquals(2, $updated);
$this
->assertFlag('xmlsitemap_regenerate_needed', FALSE);
$updated = $this->linkStorage
->updateMultiple([
'status' => 1,
], [
'type' => 'testing',
'subtype' => 'group2',
'status_override' => 0,
'status' => 0,
]);
$this
->assertEquals(2, $updated);
$this
->assertFlag('xmlsitemap_regenerate_needed', TRUE);
}
public function testDuplicatePaths() {
$this
->drupalLogin($this->admin_user);
$link1 = $this
->addSitemapLink([
'loc' => '/duplicate',
]);
$link2 = $this
->addSitemapLink([
'loc' => '/duplicate',
]);
$this
->regenerateSitemap();
$this
->drupalGetSitemap();
$page_text = $this
->getSession()
->getPage()
->getContent();
$nr_found = substr_count($page_text, 'duplicate');
$this
->assertSame(1, $nr_found);
}
public function testMinimumLifetime() {
$this
->drupalLogin($this->admin_user);
$this->config
->set('minimum_lifetime', 300)
->save();
$this
->regenerateSitemap();
$link = $this
->addSitemapLink([
'loc' => '/lifetime-test',
]);
$this
->cronRun();
$this
->drupalGetSitemap();
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseNotContains('lifetime-test');
$this->state
->set('xmlsitemap_generated_last', $this->time
->getRequestTime() - 400);
$this
->cronRun();
$this
->drupalGetSitemap();
$this
->assertSession()
->responseContains('lifetime-test');
$this->linkStorage
->delete($link['type'], $link['id']);
$this
->cronRun();
$this
->drupalGetSitemap();
$this
->assertSession()
->responseContains('lifetime-test');
$this
->regenerateSitemap();
$this
->drupalGetSitemap();
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseNotContains('lifetime-test');
}
}