public function MetatagHreflangWithEntityTranslationTest::testOutput in Metatag 7
Confirm that the meta tags output are correct.
File
- metatag_hreflang/
tests/ MetatagHreflangWithEntityTranslationTest.test, line 140 - Tests for hreflang handle when Entity Translation is used.
Class
- MetatagHreflangWithEntityTranslationTest
- Tests for hreflang handle when Entity Translation is used.
Code
public function testOutput() {
// All of the locales we're supporting in these tests. The languages have
// been enabled already, so this gets a list of language objects.
$languages = language_list('enabled');
$locales = $languages[1];
// Identify the site's default language.
$default_language = language_default('language');
// Create an English node so it can be translated.
$args = array(
'language' => $default_language,
);
$node = $this
->drupalCreateNode($args);
$this
->verbose($node);
// Load the translation page.
$this
->drupalGet('node/' . $node->nid . '/translate');
$this
->assertResponse(200);
$this
->assertText(t('Not translated'));
// Confirm that there are links to translate the node.
$urls = array();
foreach ($locales as $langcode => $locale) {
// There won't be a link to translate to English, that's the default
// language for thos node.
if ($langcode == $default_language) {
continue;
}
// Confirm that a link to translate the node into each locale exists.
$url = 'node/' . $node->nid . '/edit/add/' . $node->language . '/' . $langcode;
$urls[$langcode] = $url;
// @todo This fails in testbot.
// $this->assertLinkbyHref(url($url));
}
// Check each of the 'translate' pages loads properly.
foreach ($urls as $langcode => $url) {
// Confirm the 'translate' page loads.
$this
->drupalGet($url);
$this
->assertResponse(200);
// Confirm all of the hreflang fields exist.
$this
->assertHreflangFields($langcode);
// Save the translation.
$edit = array(
// Add a custom title.
"metatags[{$langcode}][title][value]" => "Tranlation for {$langcode}",
);
$this
->drupalPost(NULL, $edit, t('Save'));
}
// Load the translation page again to confirm everything was translated.
$this
->drupalGet('node/' . $node->nid . '/translate');
$this
->assertResponse(200);
$this
->assertNoText(t('Not translated'));
// Load the node's devel page to see the translations data.
$this
->drupalGet('node/' . $node->nid . '/devel');
$this
->assertResponse(200);
// Load the node's devel page and confirm each of the tokens is available.
$this
->drupalGet('node/' . $node->nid . '/devel/token');
$this
->assertResponse(200);
foreach ($locales as $langcode => $locale) {
$this
->assertText("[node:url-{$langcode}]");
}
// Load the node page again, confirm each hreflang meta tag.
$this
->drupalGet('node/' . $node->nid);
$this
->assertResponse(200);
$xpath = $this
->xpath("//link[@rel='alternate']");
$this
->verbose($xpath);
$this
->assertEqual(count($xpath), count($locales), 'The correct number of hreflang meta tags was found');
// Try to find the position of the xdefault value in the $xpath structure.
$xdefault_pos = NULL;
// This is slightly messy logic as the sort order of $locales may be
// different to the meta tags.
foreach ($locales as $langcode => $locale) {
$found = FALSE;
foreach ($xpath as $ctr => $item) {
if ($item['hreflang'] == 'x-default') {
$xdefault_pos = $ctr;
}
elseif ($item['hreflang'] == $langcode) {
$found = TRUE;
$this
->assertEqual($xpath[$ctr]['hreflang'], $langcode);
// @todo Fix this. Not sure why, but the url() call returns the URL
// without the language prefix.
// @code
// $url_options = array(
// 'language' => $locale,
// 'absolute' => TRUE,
// );
// $this->assertEqual($xpath[$ctr]['href'], url('node/' . $node->nid, $url_options));
// @endcode
}
}
// The node's default language should not have been found, it should have
// been turned into an xdefault.
if ($langcode == $node->language) {
$this
->assertFalse((bool) $found, format_string("A regular hreflang meta tag for the node's default language (%lang) was not found.", array(
'%lang' => $langcode,
)));
}
else {
$this
->assertTrue((bool) $found, format_string('The hreflang meta tag for %lang was found.', array(
'%lang' => $langcode,
)));
}
}
// Confirm the hreflang=xdefault meta tag was found.
$this
->assertNotNull($xdefault_pos, 'The hreflang=xdefault meta tag was found.');
if (!is_null($xdefault_pos)) {
$this
->assertEqual($xpath[$xdefault_pos]['href'], url('node/' . $node->nid, array(
'absolute' => TRUE,
)), 'Found the x-default value.');
}
// Enable the xdefault-dupe option.
variable_set('metatag_hreflang_allow_dupe', TRUE);
metatag_config_cache_clear();
// Load the node page again.
$this
->drupalGet('node/' . $node->nid);
$this
->assertResponse(200);
// Confirm there are now more meta tags.
$xpath = $this
->xpath("//link[@rel='alternate']");
$this
->verbose($xpath);
$this
->assertEqual(count($xpath), count($locales) + 1, 'The correct number of hreflang meta tags was found.');
$found = FALSE;
foreach ($xpath as $ctr => $item) {
if ($item['hreflang'] == $node->language) {
$found = $ctr;
}
}
$this
->assertTrue((bool) $found, "Found an hreflang meta tag for the node's default locale.");
if ($found) {
$this
->assertEqual($xpath[$found]['hreflang'], $node->language);
}
}