Commit 6056ab57 authored by gbyte.co's avatar gbyte.co

Refactor functional tests

parent 5501a84d
......@@ -67,12 +67,15 @@ class Simplesitemap {
* Configuration key, like 'entity_types'.
* @param mixed $value
* The configuration to be saved.
*
* @return $this
*/
public function saveConfig($key, $value) {
$this->configFactory->getEditable('simple_sitemap.settings')
->set($key, $value)->save();
// Refresh config object after making changes.
$this->config = $this->configFactory->get('simple_sitemap.settings');
return $this;
}
/**
......@@ -92,9 +95,8 @@ class Simplesitemap {
if (empty($entity_types[$entity_type_id])) {
$entity_types[$entity_type_id] = [];
$this->saveConfig('entity_types', $entity_types);
return TRUE;
}
return FALSE;
return $this;
}
/**
......@@ -113,9 +115,8 @@ class Simplesitemap {
if (isset($entity_types[$entity_type_id])) {
unset($entity_types[$entity_type_id]);
$this->saveConfig('entity_types', $entity_types);
return TRUE;
}
return FALSE;
return $this;
}
/**
......@@ -129,12 +130,15 @@ class Simplesitemap {
* @param array $settings
* An array of sitemap settings for this bundle/entity type.
* Example: ['index' => TRUE, 'priority' => 0.5]
*
* @return $this
*/
public function setBundleSettings($entity_type_id, $bundle_name = NULL, $settings) {
$bundle_name = is_null($bundle_name) ? $entity_type_id : $bundle_name;
$entity_types = $this->getConfig('entity_types');
$this->addLinkSettings('entity', $settings, $entity_types[$entity_type_id][$bundle_name]);
$this->saveConfig('entity_types', $entity_types);
return $this;
}
public function getEntityInstanceBundleName($entity) {
......@@ -168,9 +172,8 @@ class Simplesitemap {
unset($entity_types[$entity_type_id][$bundle_name]['entities'][$id]);
}
$this->saveConfig('entity_types', $entity_types);
return TRUE;
}
return FALSE;
return $this;
}
public function getBundleSettings($entity_type_id, $bundle_name = NULL) {
......@@ -222,7 +225,7 @@ class Simplesitemap {
$custom_links[$link_key]['path'] = $path;
$this->addLinkSettings('entity', $settings, $custom_links[$link_key]);
$this->saveConfig('custom', $custom_links);
return TRUE;
return $this;
}
public function getCustomLink($path) {
......@@ -242,10 +245,9 @@ class Simplesitemap {
unset($custom_links[$key]);
$custom_links = array_values($custom_links);
$this->saveConfig('custom', $custom_links);
return TRUE;
}
}
return FALSE;
return $this;
}
public function removeCustomLinks() {
......@@ -266,7 +268,6 @@ class Simplesitemap {
}
$target[$setting_key] = $setting;
}
}
}
......@@ -309,9 +310,9 @@ class Simplesitemap {
* This decides how the batch process is to be run.
*/
public function generateSitemap($from = 'form') {
$generator = \Drupal::service('simple_sitemap.sitemap_generator');
$generator->setGenerateFrom($from);
$generator->startGeneration();
\Drupal::service('simple_sitemap.sitemap_generator')
->setGenerateFrom($from)
->startGeneration();
}
/**
......@@ -324,8 +325,8 @@ class Simplesitemap {
* The sitemap index.
*/
private function getSitemapIndex($chunks) {
$generator = \Drupal::service('simple_sitemap.sitemap_generator');
return $generator->generateSitemapIndex($chunks);
return \Drupal::service('simple_sitemap.sitemap_generator')
->generateSitemapIndex($chunks);
}
/**
......@@ -349,11 +350,14 @@ class Simplesitemap {
* Setting name, like 'max_links'.
* @param $setting
* The setting to be saved.
*
* @return $this
*/
public function saveSetting($name, $setting) {
$settings = $this->getConfig('settings');
$settings[$name] = $setting;
$this->saveConfig('settings', $settings);
return $this;
}
/**
......
......@@ -29,6 +29,7 @@ class SitemapGenerator {
public function setGenerateFrom($from) {
$this->generateFrom = $from;
return $this;
}
/**
......
......@@ -19,72 +19,113 @@ class SimplesitemapTest extends WebTestBase {
* @var array
*/
public static $modules = ['simple_sitemap', 'node'];
protected $generator;
protected $node;
protected $node2;
/**
* Implements setup().
*/
protected function setUp() {
parent::setUp();
$this->drupalCreateContentType(['type' => 'page']);
$this->node = $this->createNode(['title' => 'Node', 'type' => 'page']);
$this->node2 = $this->createNode(['title' => 'Node2', 'type' => 'page']);
$this->generator = \Drupal::service('simple_sitemap.generator');
}
/**
* Test Simple sitemap integration.
* Verify sitemap.xml has been generated on install (custom path generation).
*/
public function testSimplesitemap() {
// Verify sitemap.xml has been generated on install (custom path generation).
public function testInitialGeneration() {
$this->drupalGet('sitemap.xml');
$this->assertRaw('urlset');
$this->assertRaw('http');
}
/* @var $node \Drupal\Node\NodeInterface */
$this->createNode(['title' => 'Node 1', 'type' => 'page']);
$node = $this->createNode(['title' => 'Node 2', 'type' => 'page']);
public function testGeneration() {
// Set up the module.
$generator = \Drupal::service('simple_sitemap.generator');
$generator->setBundleSettings('node', 'page', ['index' => 1, 'priority' => '0.5']);
$this->generator->setBundleSettings('node', 'page', ['index' => 1, 'priority' => '0.5'])
->generateSitemap('nobatch');
// Verify the cache was flushed and node is in the sitemap.
$generator->generateSitemap('nobatch');
$this->drupalGet('sitemap.xml');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
$this->assertText('node/' . $node->id());
$this->assertText('node/' . $this->node->id());
$this->drupalGet('sitemap.xml');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
$this->assertText('node/' . $node->id());
$this->assertText('node/' . $this->node->id());
}
/**
* Test overriding of bundle entities.
*/
public function testBundleOverride() {
$this->generator->setBundleSettings('node', 'page', ['index' => 1, 'priority' => '0.5'])
->setEntityInstanceSettings('node', $this->node->id(), ['index' => 1, 'priority' => '0.1'])
->generateSitemap('nobatch');
// Test overriding of bundle entities.
$generator->setEntityInstanceSettings('node', $node->id(), ['index' => 1, 'priority' => '0.1']);
$generator->generateSitemap('nobatch');
$this->drupalGet('sitemap.xml');
$this->assertText('0.1');
}
/**
* Test sitemap index.
*/
public function testSitemapIndex() {
$this->generator->setBundleSettings('node', 'page', ['index' => 1, 'priority' => '0.5'])
->saveSetting('max_links', 1)
->generateSitemap('nobatch');
// Test sitemap index.
$generator->saveSetting('max_links', 1);
$generator->generateSitemap('nobatch');
$this->drupalGet('sitemap.xml');
$this->assertText('sitemaps/2/sitemap.xml');
}
$generator->saveSetting('max_links', 2000);
/**
* Test disabling sitemap support for an entity type.
*/
public function testDisableEntityType() {
$this->generator->setBundleSettings('node', 'page', ['index' => 1, 'priority' => '0.5'])
->disableEntityType('node')
->generateSitemap('nobatch');
// Test disabling sitemap support for an entity type.
$generator->disableEntityType('node');
$generator->generateSitemap('nobatch');
$this->drupalGet('sitemap.xml');
$this->assertNoText('node/');
}
/**
* Test enabling sitemap support for an entity type.
*/
public function testEnableEntityType() {
$this->generator->disableEntityType('node')
->enableEntityType('nobatch')
->setBundleSettings('node', 'page', ['index' => 1, 'priority' => '0.5'])
->generateSitemap('nobatch');
$this->drupalGet('sitemap.xml');
$this->assertText('node/');
}
/**
* Test adding a custom link to the sitemap.
*/
public function testAddCustomLink() {
$this->generator->addCustomLink('/node/' . $this->node->id(), ['priority' => '0.2'])
->generateSitemap('nobatch');
// Test adding a custom link to the sitemap.
$generator->addCustomLink('/node/' . $node->id(), ['priority' => '0.2']);
$generator->generateSitemap('nobatch');
$this->drupalGet('sitemap.xml');
$this->assertText('0.2');
}
/**
* Test removing custom links from the sitemap.
*/
public function testRemoveCustomLink() {
$this->generator->addCustomLink('/node/' . $this->node->id(), ['priority' => '0.2'])
->removeCustomLink('/node/' . $this->node->id())
->generateSitemap('nobatch');
// Test removing custom links from the sitemap.
$generator->removeCustomLink('/node/' . $node->id());
$generator->generateSitemap('nobatch');
$this->drupalGet('sitemap.xml');
$this->assertNoText('0.2');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment