You are here

public function MetatagCoreImageTest::testConfigImageWithSpaceInURL in Metatag 7

Confirm images with a space in its URL will be handled properly.

File

tests/MetatagCoreImageTest.test, line 228
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 testConfigImageWithSpaceInURL() {

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

  // Rename the file so it has a space in the filename.
  $image_uri = file_unmanaged_move($image_uri, str_replace('_', ' ', $image_uri));
  $this
    ->verbose($image_uri);

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

  // Confirm the file could be loaded.
  $this
    ->drupalGet($image_url);
  $this
    ->assertResponse(200, 'The image could be loaded.');

  // After processing the image's URL will have "%20" instead of spaces.
  $image_url_friendly = str_replace(' ', '%20', $image_url);

  // Confirm the file's friendly URL could be loaded.
  $this
    ->drupalGet($image_url_friendly);
  $this
    ->assertResponse(200, 'The friendly image could be loaded.');

  // Update the global config to add an image meta tag.
  $config = metatag_config_load('global');
  $config->config['image_src']['value'] = $image_uri;
  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's friendly URL can be found in the raw HTML.
  $this
    ->assertRaw($image_url_friendly, 'Found the friendly image URL in the raw HTML.');

  // 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($xpath[0]['href'], $image_url_friendly, 'The image had its spaces replaces with "%20".');

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