Commit b33347b6 authored by Dave Reid's avatar Dave Reid

by Dave Reid: First round of simplifying and unifying test assertion functions.

parent 4aae1791
......@@ -91,11 +91,6 @@ function xmlsitemap_requirements($phase) {
/**
* Check the status of all hook_requirements() from xmlsitemap modules.
*
* @param $return_only
* If TRUE, will return the result, otherwise it will show a message.
* @return
* TRUE if there is a warning or error requirement, or FALSE otherwise.
*/
function xmlsitemap_check_status() {
$messages = &drupal_static(__FUNCTION__);
......
......@@ -811,6 +811,18 @@ function xmlsitemap_clear_directory($delete = FALSE) {
return _xmlsitemap_delete_recursive($directory, $delete);
}
/**
* Recursively delete all files and folders in the specified filepath.
*
* This is a backport of Drupal 7's file_unmanaged_delete_recursive().
*
* Note that this only deletes visible files with write permission.
*
* @param $path
* A filepath relative to file_directory_path.
* @param $delete_root
* A boolean if TRUE will delete the $path directory afterwards.
*/
function _xmlsitemap_delete_recursive($path, $delete_root = FALSE) {
// Resolve streamwrapper URI to local path.
$path = drupal_realpath($path);
......
......@@ -46,6 +46,78 @@ class XMLSitemapTestHelper extends DrupalWebTestCase {
}
}
protected function drupalGetSitemap($language = LANGUAGE_NONE, $regenerate = FALSE) {
if ($regenerate) {
$this->regenerateSitemap();
}
$this->drupalGet('sitemap.xml', array('language' => xmlsitemap_language_load($language)));
$this->assertResponse(200);
}
/**
* Regenerate the sitemap by setting the regenerate flag and running cron.
*/
protected function regenerateSitemap() {
variable_set('xmlsitemap_regenerate_needed', TRUE);
variable_set('xmlsitemap_generated_last', 0);
module_load_include('inc', 'xmlsitemap');
xmlsitemap_regenerate();
$this->assertTrue(variable_get('xmlsitemap_generated_last', 0) && !variable_get('xmlsitemap_regenerate_needed', FALSE), t('XML sitemaps regenerated and flag cleared.'));
}
protected function assertSitemapLink($conditions) {
$link = xmlsitemap_load_link($conditions);
$this->assertTrue(is_array($link), 'Link loaded.');
return $link;
}
protected function assertNoSitemapLink($conditions) {
$link = xmlsitemap_load_link($conditions);
$this->assertFalse($link, 'Link not loaded.');
return $link;
}
protected function assertSitemapLinkVisible(array $link, $load = FALSE) {
if ($load) {
$link = $this->assertSitemapLink($link);
}
$this->assertTrue($link && $link['access'] && $link['status'], 'Sitemap link is visible.');
}
protected function assertSitemapLinkNotVisible(array $link, $load = FALSE) {
if ($load) {
$link = $this->assertSitemapLink($link);
}
$this->assertTrue($link && !($link['access'] && $link['status']), 'Sitemap link is not visible.');
}
protected function assertSitemapLinkValues(array $link, array $conditions) {
foreach ($conditions as $key => $value) {
if ($value === NULL || $link[$key] === NULL) {
// For nullable fields, always check for identical values (===).
$this->assertIdentical($link[$key], $value, t('Identical values for link field @key.', array('@key' => $key)));
}
else {
// Otherwise check simple equality (==).
$this->assertEqual($link[$key], $value, t('Equal values for link field @key.', array('@key' => $key)));
}
}
}
protected function assertNotSitemapLinkValues(array $link, array $conditions) {
foreach ($conditions as $key => $value) {
if ($value === NULL || $link[$key] === NULL) {
// For nullable fields, always check for identical values (===).
$this->assertNotIdentical($link[$key], $value, t('Not identical values for link field @key.', array('@key' => $key)));
}
else {
// Otherwise check simple equality (==).
$this->assertNotEqual($link[$key], $value, t('Not equal values for link field @key.', array('@key' => $key)));
}
}
}
/**
* Assert that a link is found in the sitemap.
*
......@@ -76,35 +148,13 @@ class XMLSitemapTestHelper extends DrupalWebTestCase {
return $link;
}
protected function drupalGetSitemap($language = LANGUAGE_NONE, $regenerate = FALSE) {
if ($regenerate) {
$this->regenerateSitemap();
}
$this->drupalGet('sitemap.xml', array('language' => xmlsitemap_language_load($language)));
$this->assertResponse(200);
}
protected function assertLinkNotInSitemap(&$link) {
$link = xmlsitemap_load_link(array('id' => $link['id'], 'type' => $link['type']));
$this->assertTrue(!$link || !$link['access'] || !$link['status'], t('Link found in sitemap.'));
return $link;
}
protected function assertLinkValues($link, array $values) {
foreach ($values as $key => $value) {
if ($value === NULL || $link[$key] === NULL) {
// For nullable fields, always check for identical values (===).
$this->assertIdentical($link[$key], $value, t('Identical values for link field @key.', array('@key' => $key)));
}
else {
// Otherwise check simple equality (==).
$this->assertEqual($link[$key], $value, t('Equal values for link field @key.', array('@key' => $key)));
}
}
}
protected function assertLinksInSitemap() {
protected function assertRawSitemapLinks() {
$links = func_get_args();
foreach ($links as $link) {
$path = url($link['loc'], array('language' => xmlsitemap_language_load($link['language']), 'absolute' => TRUE));
......@@ -112,7 +162,7 @@ class XMLSitemapTestHelper extends DrupalWebTestCase {
}
}
protected function assertLinksNotInSitemap() {
protected function assertNoRawSitemapLinks() {
$links = func_get_args();
foreach ($links as $link) {
$path = url($link['loc'], array('language' => xmlsitemap_language_load($link['language']), 'absolute' => TRUE));
......@@ -136,15 +186,14 @@ class XMLSitemapTestHelper extends DrupalWebTestCase {
return $link;
}
/**
* Regenerate the sitemap by setting the regenerate flag and running cron.
*/
protected function regenerateSitemap() {
variable_set('xmlsitemap_regenerate_needed', TRUE);
variable_set('xmlsitemap_generated_last', 0);
module_load_include('inc', 'xmlsitemap');
xmlsitemap_regenerate();
$this->assertTrue(variable_get('xmlsitemap_generated_last', 0), t('XML sitemaps regenerated.'));
protected function assertFlag($variable, $assert_value = TRUE, $reset_if_true = TRUE) {
$value = xmlsitemap_var($variable);
if ($reset_if_true && $value) {
variable_set('xmlsitemap_' . $variable, FALSE);
}
return $this->assertEqual($value, $assert_value, "xmlsitemap_$variable is " . ($assert_value ? 'TRUE' : 'FALSE'));
}
protected function assertXMLSitemapProblems($problem_text = FALSE) {
......@@ -180,28 +229,6 @@ class XMLSitemapTestHelper extends DrupalWebTestCase {
$this->assertFalse($this->getWatchdogMessage($conditions), $message);
}
protected function assertSitemapLink($conditions) {
$link = xmlsitemap_load_link($conditions);
$this->assertTrue($link);
return $link;
}
protected function assertNotSitemapLink($conditions) {
$link = xmlsitemap_load_link($conditions);
$this->assertFalse($link);
return $link;
}
protected function assertFlag($variable, $assert_value = TRUE, $reset_if_true = TRUE) {
$value = xmlsitemap_var($variable);
if ($reset_if_true && $value) {
variable_set('xmlsitemap_' . $variable, FALSE);
}
return $this->assertEqual($value, $assert_value, "xmlsitemap_$variable is " . ($assert_value ? 'TRUE' : 'FALSE'));
}
/**
* Fetch all seen watchdog messages.
*/
......@@ -250,8 +277,6 @@ class XMLSitemapTestHelper extends DrupalWebTestCase {
$this->verbose(implode("<br />", $verbose));
}
}
}
class XMLSitemapUnitTest extends XMLSitemapTestHelper {
......@@ -590,8 +615,7 @@ class XMLSitemapFunctionalTest extends XMLSitemapTestHelper {
* Test the sitemap file caching.
*/
function testSitemapCaching() {
$this->regenerateSitemap();
$this->drupalGet('sitemap.xml');
$this->drupalGetSitemap(LANGUAGE_NONE, TRUE);
$this->assertResponse(200);
$etag = $this->drupalGetHeader('etag');
$last_modified = $this->drupalGetHeader('last-modified');
......@@ -612,7 +636,7 @@ class XMLSitemapFunctionalTest extends XMLSitemapTestHelper {
$this->drupalPost('admin/config/search/xmlsitemap', $edit, t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'));
$this->addSitemapLink(array('loc' => 'lifetime-test'));
$link = $this->addSitemapLink(array('loc' => 'lifetime-test'));
drupal_cron_run();
$this->drupalGetSitemap();
$this->assertNoRaw('lifetime-test');
......@@ -622,7 +646,7 @@ class XMLSitemapFunctionalTest extends XMLSitemapTestHelper {
$this->drupalGetSitemap();
$this->assertRaw('lifetime-test');
xmlsitemap_delete_link(array('type' => 'testing', 'id' => 1));
xmlsitemap_delete_link(array('type' => $link['type'], 'id' => $link['id']));
drupal_cron_run();
$this->drupalGetSitemap();
$this->assertRaw('lifetime-test');
......@@ -651,8 +675,7 @@ class XMLSitemapFunctionalTest extends XMLSitemapTestHelper {
$this->drupalPost('admin/config/search/xmlsitemap', $edit, t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'));
$this->regenerateSitemap();
$this->drupalGet('sitemap.xml');
$this->drupalGetSitemap(LANGUAGE_NONE, TRUE);
$this->assertRaw('<loc>http://example.com/</loc>');
}
......@@ -715,7 +738,7 @@ class XMLSitemapRobotsTxtIntegrationTest extends XMLSitemapTestHelper {
function testRobotsTxt() {
// Request the un-clean robots.txt path so this will work in case there is
// still the robots.txt file in the root directory.
$this->drupalGet('', array('query' => 'q=robots.txt'));
$this->drupalGet('', array('query' => array('q' => 'robots.txt')));
$this->assertRaw('Sitemap: ' . url('sitemap.xml', array('absolute' => TRUE)));
}
}
......@@ -144,11 +144,7 @@ function xmlsitemap_custom_edit_link_form_validate($form, &$form_state) {
// Make sure we trim and normalize the path first.
$link['loc'] = trim($link['loc']);
$normal_path = drupal_get_normal_path($link['loc']);
if ($link['loc'] != $normal_path) {
drupal_set_message(t('The sitemap system stores system paths only, but will use the URL alias in the sitemap output. The custom link %link has been stored as %normal_path.', array('%link' => $link['loc'], '%normal_path' => $normal_path)));
$link['loc'] = $normal_path;
}
$link['loc'] = drupal_get_normal_path($link['loc'], $link['language']);
// Test anonymous user access to the custom link paths.
xmlsitemap_switch_user(0);
......
......@@ -21,13 +21,13 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
parent::setUp('xmlsitemap_custom', 'path');
$this->admin_user = $this->drupalCreateUser(array('access content', 'administer xmlsitemap'));
$this->drupalLogin($this->admin_user);
}
function testCustomLinks() {
// Set a path alias for the node page.
$alias = array('source' => 'node', 'alias' => 'content');
$alias = array('source' => 'system/files', 'alias' => 'public-files');
path_save($alias);
}
function testCustomLinks() {
$this->drupalGet('admin/config/search/xmlsitemap/custom');
$this->clickLink(t('Add custom link'));
......@@ -35,26 +35,42 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
$edit['loc'] = 'invalid-testing-path';
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc'])));
$this->assertNotInSitemap(array('type' => 'custom', 'loc' => $edit['loc']));
$this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));
// Test a path not accessible to anonymous user.
$edit['loc'] = 'admin/config/people/accounts';
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc'])));
$this->assertNotInSitemap(array('type' => 'custom', 'loc' => $edit['loc']));
$this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));
// Test that the current page, which should not give a false positive for
// $menu_item['access'] since the result has been cached already.
$edit['loc'] = 'admin/config/search/xmlsitemap/custom/add';
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc'])));
$this->assertNotInSitemap(array('type' => 'custom', 'loc' => $edit['loc']));
$this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));
// Add an aliased path with padded spaces.
$edit['loc'] = ' content ';
$edit['loc'] = ' public-files ';
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertText(t('The sitemap system stores system paths only, but will use the URL alias in the sitemap output. The custom link @link has been stored as @normal_path.', array('@link' => 'content', '@normal_path' => 'node')));
$this->assertInSitemap(array('type' => 'custom', 'loc' => 'node'));
$this->assertText('The custom link for system/files was saved');
$link = $this->assertSitemapLink(array('type' => 'custom', 'loc' => 'system/files'));
$this->assertSitemapLinkValues($link, array('priority' => 0.5, 'changefreq' => 0, 'access' => 1, 'status' => 1));
$this->clickLink('Edit');
$edit = array(
'priority' => 0.1,
'changefreq' => XMLSITEMAP_FREQUENCY_ALWAYS,
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertText('The custom link for system/files was saved');
$link = $this->assertSitemapLink(array('type' => 'custom', 'id' => $link['id']));
$this->assertSitemapLinkValues($link, array('priority' => 0.1, 'changefreq' => XMLSITEMAP_FREQUENCY_ALWAYS, 'access' => 1, 'status' => 1));
$this->clickLink('Delete');
$this->drupalPost(NULL, array(), t('Delete'));
$this->assertText('The custom link for system/files has been deleted.');
$this->assertNoSitemapLink(array('type' => 'custom', 'loc' => 'system/files'));
}
/**
......@@ -65,24 +81,24 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
$edit['loc'] = $this->randomName();
$this->drupalPost('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
$this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc'])));
$this->assertNotInSitemap(array('type' => 'custom', 'loc' => $edit['loc']));
$this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));
// Test an unaccessible file .
//$edit['loc'] = '.htaccess';
//$this->drupalPost('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
//$this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc'])));
//$this->assertNotInSitemap(array('type' => 'custom', 'loc' => $edit['loc']));
//$this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));
// Test a valid file.
$edit['loc'] = 'misc/drupal.js';
$this->drupalPost('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
$this->assertText('The custom link for ' . $edit['loc'] . ' was saved');
$this->assertInSitemap(array('type' => 'custom', 'loc' => $edit['loc']));
$link = $this->assertSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));
// Test a valid folder.
$edit['loc'] = 'misc';
$this->drupalPost('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
$this->assertText('The custom link for ' . $edit['loc'] . ' was saved');
$this->assertInSitemap(array('type' => 'custom', 'loc' => $edit['loc']));
$link = $this->assertSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));
}
}
......@@ -44,7 +44,7 @@ class XMLSitemapEnginesFunctionalTest extends XMLSitemapTestHelper {
$this->assertWatchdogMessage(array('type' => 'xmlsitemap', 'message' => 'Submitted the sitemap to %url and received response @code.'));
$this->assertWatchdogMessage(array('type' => 'xmlsitemap', 'message' => 'No sitemap parameter provided.'));
$edit = array('xmlsitemap_engines_custom_urls' => url('ping', array('absolute' => TRUE, 'query' => '[sitemap]')));
$edit = array('xmlsitemap_engines_custom_urls' => url('ping', array('absolute' => TRUE, 'query' => array('sitemap' => '[sitemap]'))));
$this->drupalPost('admin/config/search/xmlsitemap/engines', $edit, t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'));
......
......@@ -28,7 +28,7 @@ function xmlsitemap_engines_test_xmlsitemap_engine_info() {
* Implements hook_xmlsitemap_engine_info_alter().
*/
function xmlsitemap_engines_test_xmlsitemap_engine_info_alter(&$engines) {
$engines['simpletest']['url'] = url('ping', array('absolute' => TRUE, 'query' => 'sitemap=[sitemap]'));
$engines['simpletest']['url'] = url('ping', array('absolute' => TRUE, 'query' => array('sitemap' => '[sitemap]')));
}
function xmlsitemap_engines_test_pinged() {
......
......@@ -40,7 +40,7 @@ function xmlsitemap_engines_settings_form() {
$form['xmlsitemap_engines_custom_urls'] = array(
'#type' => 'textarea',
'#title' => t('Custom submission URLs'),
'#description' => t('Enter one custom submission URL per line. The token [sitemap] will be replaced with the URL to your sitemap. For example: %example-before would become %example-after.', array('%example-before' => 'http://example.com/ping?[sitemap]', '%example-after' => xmlsitemap_engines_prepare_url('http://example.com/ping?[sitemap]', url('sitemap.xml', xmlsitemap_get_url_options())))),
'#description' => t('Enter one custom submission URL per line. The token [sitemap] will be replaced with the URL to your sitemap. For example: %example-before would become %example-after.', array('%example-before' => 'http://example.com/ping?[sitemap]', '%example-after' => xmlsitemap_engines_prepare_url('http://example.com/ping?[sitemap]', url('sitemap.xml', array('absolute' => TRUE))))),
'#default_value' => variable_get('xmlsitemap_engines_custom_urls', ''),
'#rows' => 2,
'#wysiwyg' => FALSE,
......
......@@ -54,46 +54,46 @@ class XMLSitemapI18nTest extends XMLSitemapI18nWebTestCase {
variable_set('i18n_selection_mode', 'off');
$this->regenerateSitemap();
$this->drupalGetSitemap('en');
$this->assertLinksInSitemap($node, $node_en, $node_fr, $link, $link_en, $link_fr);
$this->assertRawSitemapLinks($node, $node_en, $node_fr, $link, $link_en, $link_fr);
$this->drupalGetSitemap('fr');
$this->assertLinksInSitemap($node, $node_en, $node_fr, $link, $link_en, $link_fr);
$this->assertRawSitemapLinks($node, $node_en, $node_fr, $link, $link_en, $link_fr);
variable_set('i18n_selection_mode', 'simple');
$this->regenerateSitemap();
$this->drupalGetSitemap('en');
$this->assertLinksInSitemap($node, $node_en, $link, $link_en);
$this->assertLinksNotInSitemap($node_fr, $link_fr);
$this->assertRawSitemapLinks($node, $node_en, $link, $link_en);
$this->assertNoRawSitemapLinks($node_fr, $link_fr);
$this->drupalGetSitemap('fr');
$this->assertLinksInSitemap($node, $node_fr, $link, $link_fr);
$this->assertLinksNotInSitemap($node_en, $link_en);
$this->assertRawSitemapLinks($node, $node_fr, $link, $link_fr);
$this->assertNoRawSitemapLinks($node_en, $link_en);
variable_set('i18n_selection_mode', 'mixed');
$this->regenerateSitemap();
$this->drupalGetSitemap('en');
$this->assertLinksInSitemap($node, $node_en, $link, $link_en);
$this->assertLinksNotInSitemap($node_fr, $link_fr);
$this->assertRawSitemapLinks($node, $node_en, $link, $link_en);
$this->assertNoRawSitemapLinks($node_fr, $link_fr);
$this->drupalGetSitemap('fr');
$this->assertLinksInSitemap($node, $node_en, $node_fr, $link, $link_en, $link_fr);
$this->assertRawSitemapLinks($node, $node_en, $node_fr, $link, $link_en, $link_fr);
variable_set('i18n_selection_mode', 'default');
$this->regenerateSitemap();
$this->drupalGetSitemap('en');
$this->assertLinksInSitemap($node, $node_en, $link, $link_en);
$this->assertLinksNotInSitemap($node_fr, $link_fr);
$this->assertRawSitemapLinks($node, $node_en, $link, $link_en);
$this->assertNoRawSitemapLinks($node_fr, $link_fr);
$this->drupalGetSitemap('fr');
$this->assertLinksInSitemap($node, $node_en, $link, $link_en);
$this->assertLinksNotInSitemap($node_fr, $link_fr);
$this->assertRawSitemapLinks($node, $node_en, $link, $link_en);
$this->assertNoRawSitemapLinks($node_fr, $link_fr);
// With strict mode, the language neutral node should not be found, but the
// language neutral non-node should be.
variable_set('i18n_selection_mode', 'strict');
$this->regenerateSitemap();
$this->drupalGetSitemap('en');
$this->assertLinksInSitemap($node_en, $link, $link_en);
$this->assertLinksNotInSitemap($node, $node_fr, $link_fr);
$this->assertRawSitemapLinks($node_en, $link, $link_en);
$this->assertNoRawSitemapLinks($node, $node_fr, $link_fr);
$this->drupalGetSitemap('fr');
$this->assertLinksInSitemap($node_fr, $link, $link_fr);
$this->assertLinksNotInSitemap($node, $node_en, $link_en);
$this->assertRawSitemapLinks($node_fr, $link, $link_fr);
$this->assertNoRawSitemapLinks($node, $node_en, $link_en);
}
}
......
......@@ -39,7 +39,7 @@ class XMLSitemapNodeTestHelper extends XMLSitemapTestHelper {
$this->fail(t('Could not load sitemap link for node @nid.', array('@nid' => $node->nid)));
}
else {
$this->assertLinkValues($link, $values);
$this->assertSitemapLinkValues($link, $values);
}
}
}
......
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