Commit 544c120d authored by Dave Reid's avatar Dave Reid

#787582 by Dave Reid: Cleaned up drupalGetSitemap() in tests.

parent 1664e98c
......@@ -36,6 +36,23 @@ class XMLSitemapTestHelper extends DrupalWebTestCase {
parent::tearDown();
}
/**
* Assert the page does not respond with the specified response code.
*
* @param $code
* Response code. For example 200 is a successful page request. For a list
* of all codes see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.
* @param $message
* Message to display.
* @return
* Assertion result.
*/
protected function assertNoResponse($code, $message = '') {
$curl_code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
$match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code;
return $this->assertFalse($match, $message ? $message : t('HTTP response not expected !code, actual !curl_code', array('!code' => $code, '!curl_code' => $curl_code)), t('Browser'));
}
/**
* Check the files directory is created (massive fails if not done).
*
......@@ -47,13 +64,26 @@ class XMLSitemapTestHelper extends DrupalWebTestCase {
}
}
protected function drupalGetSitemap($language = LANGUAGE_NONE, $regenerate = FALSE) {
if ($regenerate) {
$this->regenerateSitemap();
/**
* Retrieves an XML sitemap.
*
* @param $context
* An optional array of the XML sitemap's context.
* @param $options
* Options to be forwarded to url(). These values will be merged with, but
* always override $sitemap['uri']['options'].
* @param $headers
* An array containing additional HTTP request headers, each formatted as
* "name: value".
* @return
* The retrieved HTML string, also available as $this->drupalGetContent()
*/
protected function drupalGetSitemap(array $context = array(), array $options = array(), array $headers = array()) {
$sitemap = xmlsitemap_sitemap_load_by_context($context);
if (!$sitemap) {
return $this->fail('Could not load sitemap by context.');
}
$this->drupalGet('sitemap.xml', array('language' => xmlsitemap_language_load($language)));
$this->assertResponse(200);
return $this->drupalGet($sitemap['uri']['path'], $options + $sitemap['uri']['options'], $headers);
}
/**
......@@ -579,6 +609,17 @@ class XMLSitemapUnitTest extends XMLSitemapTestHelper {
// 2 | testing | group2 | '' | 1 | 0.0
// 3 | testing | group2 | '' | 1 | 0.5
}
/**
* Test that duplicate paths are skipped during generation.
*/
function testDuplicatePaths() {
$link1 = $this->addSitemapLink(array('loc' => 'duplicate'));
$link2 = $this->addSitemapLink(array('loc' => 'duplicate'));
$this->regenerateSitemap();
$this->drupalGetSitemap();
$this->assertUniqueText('duplicate');
}
}
class XMLSitemapFunctionalTest extends XMLSitemapTestHelper {
......@@ -600,14 +641,15 @@ class XMLSitemapFunctionalTest extends XMLSitemapTestHelper {
* Test the sitemap file caching.
*/
function testSitemapCaching() {
$this->drupalGetSitemap(LANGUAGE_NONE, TRUE);
$this->regenerateSitemap();
$this->drupalGetSitemap();
$this->assertResponse(200);
$etag = $this->drupalGetHeader('etag');
$last_modified = $this->drupalGetHeader('last-modified');
$this->assertTrue($etag, t('Etag header found.'));
$this->assertTrue($last_modified, t('Last-modified header found.'));
$this->drupalGet('sitemap.xml', array(), array('If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag));
$this->drupalGetSitemap(array(), array(), array('If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag));
$this->assertResponse(304);
}
......@@ -622,11 +664,12 @@ class XMLSitemapFunctionalTest extends XMLSitemapTestHelper {
$this->assertText(t('The configuration options have been saved.'));
$link = $this->addSitemapLink(array('loc' => 'lifetime-test'));
drupal_cron_run();
$this->cronRun();
$this->drupalGetSitemap();
$this->assertResponse(200);
$this->assertNoRaw('lifetime-test');
variable_set('xmlsitemap_generated_last', REQUEST_TIME - 300);
variable_set('xmlsitemap_generated_last', REQUEST_TIME - 150);
$this->cronRun();
$this->drupalGetSitemap();
$this->assertRaw('lifetime-test');
......@@ -636,7 +679,9 @@ class XMLSitemapFunctionalTest extends XMLSitemapTestHelper {
$this->drupalGetSitemap();
$this->assertRaw('lifetime-test');
$this->drupalGetSitemap(LANGUAGE_NONE, TRUE);
$this->regenerateSitemap();
$this->drupalGetSitemap();
$this->assertResponse(200);
$this->assertNoRaw('lifetime-test');
}
......@@ -660,7 +705,8 @@ class XMLSitemapFunctionalTest extends XMLSitemapTestHelper {
$this->drupalPost('admin/config/search/xmlsitemap/settings', $edit, t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'));
$this->drupalGetSitemap(LANGUAGE_NONE, TRUE);
$this->regenerateSitemap();
$this->drupalGetSitemap(array(), array('base_url' => NULL));
$this->assertRaw('<loc>http://example.com/</loc>');
}
......@@ -694,16 +740,6 @@ class XMLSitemapFunctionalTest extends XMLSitemapTestHelper {
// Test chunk count > 1000.
// Test directory not writable.
}
/**
* Test that duplicate paths are skipped during generation.
*/
function testDuplicatePaths() {
$link1 = $this->addSitemapLink(array('loc' => 'duplicate'));
$link2 = $this->addSitemapLink(array('loc' => 'duplicate'));
$this->drupalGetSitemap(LANGUAGE_NONE, TRUE);
$this->assertUniqueText('duplicate');
}
}
class XMLSitemapRobotsTxtIntegrationTest extends XMLSitemapTestHelper {
......
......@@ -28,6 +28,11 @@ class XMLSitemapI18nWebTestCase extends XMLSitemapTestHelper {
variable_set('xmlsitemap_languages', array('en', 'fr'));
variable_set('language_negotiation', LANGUAGE_NEGOTIATION_PATH);
// Create the two different language-context sitemaps.
db_query("DELETE FROM {xmlsitemap_sitemap}");
xmlsitemap_sitemap_save(array('context' => array('language' => 'en')));
xmlsitemap_sitemap_save(array('context' => array('language' => 'fr')));
}
}
......@@ -54,34 +59,34 @@ class XMLSitemapI18nTest extends XMLSitemapI18nWebTestCase {
variable_set('i18n_selection_mode', 'off');
$this->regenerateSitemap();
$this->drupalGetSitemap('en');
$this->drupalGetSitemap(array('language' => 'en'));
$this->assertRawSitemapLinks($node, $node_en, $node_fr, $link, $link_en, $link_fr);
$this->drupalGetSitemap('fr');
$this->drupalGetSitemap(array('language' => '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->drupalGetSitemap(array('language' => 'en'));
$this->assertRawSitemapLinks($node, $node_en, $link, $link_en);
$this->assertNoRawSitemapLinks($node_fr, $link_fr);
$this->drupalGetSitemap('fr');
$this->drupalGetSitemap(array('language' => 'fr'));
$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->drupalGetSitemap(array('language' => 'en'));
$this->assertRawSitemapLinks($node, $node_en, $link, $link_en);
$this->assertNoRawSitemapLinks($node_fr, $link_fr);
$this->drupalGetSitemap('fr');
$this->drupalGetSitemap(array('language' => '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->drupalGetSitemap(array('language' => 'en'));
$this->assertRawSitemapLinks($node, $node_en, $link, $link_en);
$this->assertNoRawSitemapLinks($node_fr, $link_fr);
$this->drupalGetSitemap('fr');
$this->drupalGetSitemap(array('language' => 'fr'));
$this->assertRawSitemapLinks($node, $node_en, $link, $link_en);
$this->assertNoRawSitemapLinks($node_fr, $link_fr);
......@@ -89,10 +94,10 @@ class XMLSitemapI18nTest extends XMLSitemapI18nWebTestCase {
// language neutral non-node should be.
variable_set('i18n_selection_mode', 'strict');
$this->regenerateSitemap();
$this->drupalGetSitemap('en');
$this->drupalGetSitemap(array('language' => 'en'));
$this->assertRawSitemapLinks($node_en, $link, $link_en);
$this->assertNoRawSitemapLinks($node, $node_fr, $link_fr);
$this->drupalGetSitemap('fr');
$this->drupalGetSitemap(array('language' => 'fr'));
$this->assertRawSitemapLinks($node_fr, $link, $link_fr);
$this->assertNoRawSitemapLinks($node, $node_en, $link_en);
}
......
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