You are here

public function MetatagCoreImageTest::testConfigDrupalRelativeURL in Metatag 7

Confirm images can be added to a global config using its relative URL.

File

tests/MetatagCoreImageTest.test, line 77
Tests for the Metatag module to ensure image handling doesn't break.

Class

MetatagCoreImageTest
Tests for the Metatag module to ensure image handling doesn't break.

Code

public function testConfigDrupalRelativeURL() {

  // Generate a test image.
  $image_uri = $this
    ->generateImage();
  $this
    ->verbose($image_uri);

  // Work out the web-accessible URL for this image.
  $image_url = file_create_url($image_uri);

  // Extract the relative URL version of the absolute URL.
  $url_length = strlen($GLOBALS['base_url']);

  // Need to increase the length by 1 as the base_url does not include a
  // trailing slash.
  $relative_url = substr($image_url, $url_length + 1);

  // Update the global config to add an image meta tag.
  $config = metatag_config_load('global');
  $config->config['image_src']['value'] = $relative_url;
  metatag_config_save($config);

  // Dump out the current config, to aid with debugging.
  $this
    ->verbose($config);

  // Load the front page.
  $this
    ->drupalGet('<front>');
  $this
    ->assertResponse(200);

  // Confirm that the image_src meta tag has the expected values.
  $xpath = $this
    ->xpath("//link[@rel='image_src']");
  $this
    ->assertEqual(count($xpath), 1, 'One image_src meta tag found.');
  $this
    ->assertEqual((string) $xpath[0]['href'], $image_url);

  // Confirm the file could be loaded.
  $this
    ->drupalGet($xpath[0]['href']);
  $this
    ->assertResponse(200, "The image_src meta tag's value could be loaded.");
}