Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
project
simple_sitemap
Commits
6056ab57
Commit
6056ab57
authored
Aug 15, 2016
by
Pawel G
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor functional tests
parent
5501a84d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
90 additions
and
44 deletions
+90
-44
src/Simplesitemap.php
src/Simplesitemap.php
+19
-15
src/SitemapGenerator.php
src/SitemapGenerator.php
+1
-0
src/Tests/SimplesitemapTest.php
src/Tests/SimplesitemapTest.php
+70
-29
No files found.
src/Simplesitemap.php
View file @
6056ab57
...
...
@@ -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
;
}
/**
...
...
src/SitemapGenerator.php
View file @
6056ab57
...
...
@@ -29,6 +29,7 @@ class SitemapGenerator {
public
function
setGenerateFrom
(
$from
)
{
$this
->
generateFrom
=
$from
;
return
$this
;
}
/**
...
...
src/Tests/SimplesitemapTest.php
View file @
6056ab57
...
...
@@ -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 integ
ration.
*
Verify sitemap.xml has been generated on install (custom path gene
ration
)
.
*/
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'
);
$
generat
or
->
setBundleSettings
(
'node'
,
'page'
,
[
'index'
=>
1
,
'priority'
=>
'0.5'
]
);
$
this
->
generator
->
setBundleSettings
(
'node'
,
'page'
,
[
'index'
=>
1
,
'priority'
=>
'0.5'
])
->
generat
eSitemap
(
'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'
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment