metatag.image.test 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the Metatag module to ensure image handling doesn't break.
  5. */
  6. /**
  7. * Tests for the Metatag module to ensure image handling doesn't break.
  8. */
  9. class MetatagCoreImageTest extends MetatagTestHelper {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Metatag core tests for images',
  16. 'description' => 'Test Metatag integration with image files.',
  17. 'group' => 'Metatag',
  18. 'dependencies' => array('ctools', 'token', 'devel_generate'),
  19. );
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. function setUp(array $modules = array()) {
  25. // Needs the OpenGraph submodule because of testNodeFieldValueMultiple().
  26. $modules[] = 'metatag_opengraph';
  27. // Need image handling.
  28. $modules[] = 'image';
  29. // Need the Devel Generate image generator.
  30. $modules[] = 'devel_generate';
  31. parent::setUp($modules);
  32. }
  33. /**
  34. * Confirm that an image can be added to a global configuration using the
  35. * image's absolute URL.
  36. */
  37. function testConfigAbsoluteURL() {
  38. // Generate a test image.
  39. $image_uri = $this->generateImage();
  40. $this->verbose($image_uri);
  41. // Work out the web-accessible URL for this image.
  42. $image_url = file_create_url($image_uri);
  43. // Update the global config to add an image meta tag.
  44. $config = metatag_config_load('global');
  45. $config->config['image_src']['value'] = $image_url;
  46. metatag_config_save($config);
  47. // Dump out the current config, to aid with debugging.
  48. $this->verbose($config);
  49. // Load the front page.
  50. $this->drupalGet('<front>');
  51. $this->assertResponse(200);
  52. // Confirm that the image_src meta tag has the expected values.
  53. $xpath = $this->xpath("//link[@rel='image_src']");
  54. $this->assertEqual(count($xpath), 1, 'One image_src meta tag found.');
  55. $this->assertEqual($xpath[0]['href'], $image_url, 'The image_src meta tag with an absolute URL is being output correctly.');
  56. // Confirm the file could be loaded.
  57. $this->drupalGet($xpath[0]['href']);
  58. $this->assertResponse(200, "The image_src meta tag's value could be loaded.");
  59. }
  60. /**
  61. * Confirm that an image can be added to a global configuration using the
  62. * image's relative URL.
  63. */
  64. function testConfigDrupalRelativeURL() {
  65. // Generate a test image.
  66. $image_uri = $this->generateImage();
  67. $this->verbose($image_uri);
  68. // Work out the web-accessible URL for this image.
  69. $image_url = file_create_url($image_uri);
  70. // Extract the relative URL version of the absolute URL.
  71. $url_length = strlen($GLOBALS['base_url']);
  72. // Need to increase the length by 1 as the base_url does not include a
  73. // trailing slash.
  74. $relative_url = substr($image_url, $url_length + 1);
  75. // Update the global config to add an image meta tag.
  76. $config = metatag_config_load('global');
  77. $config->config['image_src']['value'] = $relative_url;
  78. metatag_config_save($config);
  79. // Dump out the current config, to aid with debugging.
  80. $this->verbose($config);
  81. // Load the front page.
  82. $this->drupalGet('<front>');
  83. $this->assertResponse(200);
  84. // Confirm that the image_src meta tag has the expected values.
  85. $xpath = $this->xpath("//link[@rel='image_src']");
  86. $this->assertEqual(count($xpath), 1, 'One image_src meta tag found.');
  87. $this->assertEqual((string) $xpath[0]['href'], $image_url);
  88. // , 'The image_src meta tag with a URL relative to the Drupal root is being output as an absolute URL.');
  89. // Confirm the file could be loaded.
  90. $this->drupalGet($xpath[0]['href']);
  91. $this->assertResponse(200, "The image_src meta tag's value could be loaded.");
  92. }
  93. /**
  94. * Confirm that an image can be added to a global configuration using the
  95. * image's relative URL.
  96. */
  97. function testConfigRelativeURL() {
  98. // Generate a test image.
  99. $image_uri = $this->generateImage();
  100. $this->verbose($image_uri);
  101. // Work out the web-accessible URL for this image.
  102. $image_url = file_create_url($image_uri);
  103. // Extract the relative URL version of the absolute URL.
  104. $url_length = drupal_strlen($GLOBALS['base_url']);
  105. // Need to increase the length by 1 as the base_url does not include a
  106. // trailing slash.
  107. $relative_url = drupal_substr($image_url, $url_length + 1);
  108. // Update the global config to add an image meta tag.
  109. $config = metatag_config_load('global');
  110. $config->config['image_src']['value'] = $relative_url;
  111. metatag_config_save($config);
  112. // Dump out the current config, to aid with debugging.
  113. $this->verbose($config);
  114. // Load the front page.
  115. $this->drupalGet('<front>');
  116. $this->assertResponse(200);
  117. // Confirm that the image_src meta tag has the expected values.
  118. $xpath = $this->xpath("//link[@rel='image_src']");
  119. $this->assertEqual(count($xpath), 1, 'One image_src meta tag found.');
  120. $this->assertEqual((string) $xpath[0]['href'], $image_url);
  121. // , 'The image_src meta tag with an internal URI is being output as an absolute URL.');
  122. // Confirm the file could be loaded.
  123. $this->drupalGet($xpath[0]['href']);
  124. $this->assertResponse(200, "The image_src meta tag's value could be loaded.");
  125. }
  126. /**
  127. * Confirm that an image can be added to a global configuration using the
  128. * image's protocol-relative URL.
  129. */
  130. function testConfigProtocolRelativeURL() {
  131. // Generate a test image.
  132. $image_uri = $this->generateImage();
  133. $this->verbose($image_uri);
  134. // Work out the web-accessible URL for this image.
  135. $image_url = file_create_url($image_uri);
  136. // Make the URL protocol-relative.
  137. $relative_url = str_replace('http://', '//', $image_url);
  138. // Update the global config to add an image meta tag.
  139. $config = metatag_config_load('global');
  140. $config->config['image_src']['value'] = $relative_url;
  141. metatag_config_save($config);
  142. // Dump out the current config, to aid with debugging.
  143. $this->verbose($config);
  144. // Load the front page.
  145. $this->drupalGet('<front>');
  146. $this->assertResponse(200);
  147. // Confirm that the image_src meta tag has the expected values.
  148. $xpath = $this->xpath("//link[@rel='image_src']");
  149. $this->assertEqual(count($xpath), 1, 'One image_src meta tag found.');
  150. $this->assertEqual($xpath[0]['href'], $relative_url, 'The image_src meta tag with a protocol-relative URL is being output correctly.');
  151. $this->assertNotEqual($xpath[0]['href'], $image_url, 'The image_src meta tag does not contain the absolute URL.');
  152. }
  153. /**
  154. * Confirm that an image can be added to a global configuration using the
  155. * image's internal URI.
  156. */
  157. function testConfigInternalURL() {
  158. // Generate a test image.
  159. $image_uri = $this->generateImage();
  160. $this->verbose($image_uri);
  161. // Work out the web-accessible URL for this image.
  162. $image_url = file_create_url($image_uri);
  163. // Confirm the file could be loaded.
  164. $this->drupalGet($image_url);
  165. $this->assertResponse(200, 'The image could be loaded.');
  166. // Update the global config to add an image meta tag.
  167. $config = metatag_config_load('global');
  168. $config->config['image_src']['value'] = $image_uri;
  169. metatag_config_save($config);
  170. // Dump out the current config, to aid with debugging.
  171. $this->verbose($config);
  172. // Load the front page.
  173. $this->drupalGet('<front>');
  174. $this->assertResponse(200);
  175. // Confirm that the image URL can be found in the raw HTML.
  176. $this->assertRaw($image_url, 'Found the image URL in the raw HTML.');
  177. // Confirm that the image_src meta tag has the expected values.
  178. $xpath = $this->xpath("//link[@rel='image_src']");
  179. $this->assertEqual(count($xpath), 1, 'One image_src meta tag found.');
  180. $this->assertEqual($xpath[0]['href'], $image_url, 'The image_src meta tag with an internal URI is being output correctly.');
  181. // Confirm the file could be loaded.
  182. $this->drupalGet($xpath[0]['href']);
  183. $this->assertResponse(200, "The image_src meta tag's value could be loaded.");
  184. }
  185. /**
  186. * Confirm that an image with a space in its URL will be handled properly.
  187. */
  188. function testConfigImageWithSpaceInURL() {
  189. // Generate a test image.
  190. $image_uri = $this->generateImage();
  191. $this->verbose($image_uri);
  192. // Rename the file so it has a space in the filename.
  193. $image_uri = file_unmanaged_move($image_uri, str_replace('_', ' ', $image_uri));
  194. $this->verbose($image_uri);
  195. // Work out the web-accessible URL for this image.
  196. $image_url = file_create_url($image_uri);
  197. // Confirm the file could be loaded.
  198. $this->drupalGet($image_url);
  199. $this->assertResponse(200, 'The image could be loaded.');
  200. // After processing the image's URL will have "%20" instead of spaces.
  201. $image_url_friendly = str_replace(' ', '%20', $image_url);
  202. // Confirm the file's friendly URL could be loaded.
  203. $this->drupalGet($image_url_friendly);
  204. $this->assertResponse(200, 'The friendly image could be loaded.');
  205. // Update the global config to add an image meta tag.
  206. $config = metatag_config_load('global');
  207. $config->config['image_src']['value'] = $image_uri;
  208. metatag_config_save($config);
  209. // Dump out the current config, to aid with debugging.
  210. $this->verbose($config);
  211. // Load the front page.
  212. $this->drupalGet('<front>');
  213. $this->assertResponse(200);
  214. // Confirm that the image's friendly URL can be found in the raw HTML.
  215. $this->assertRaw($image_url_friendly, 'Found the friendly image URL in the raw HTML.');
  216. // Confirm that the image_src meta tag has the expected values.
  217. $xpath = $this->xpath("//link[@rel='image_src']");
  218. $this->assertEqual(count($xpath), 1, 'One image_src meta tag found.');
  219. $this->assertEqual($xpath[0]['href'], $image_url_friendly, 'The image had its spaces replaces with "%20".');
  220. // Confirm the file could be loaded.
  221. $this->drupalGet($xpath[0]['href']);
  222. $this->assertResponse(200, "The image_src meta tag's value could be loaded.");
  223. }
  224. /**
  225. * Confirm that a default value on an image field will be output correctly.
  226. */
  227. function testNodeFieldDefault() {
  228. // Generate a test image file object.
  229. $image = $this->generateImageFile();
  230. $image_url = file_create_url($image->uri);
  231. // Dump out the file object, to aid with debugging.
  232. $this->verbose($image);
  233. // Update the article-image default settings to use the new image field.
  234. $entity_type = 'node';
  235. $bundle = 'article';
  236. $field_name = 'field_image';
  237. $instance = field_info_instance($entity_type, $field_name, $bundle);
  238. $instance['settings']['default_image'] = $image->fid;
  239. field_update_instance($instance);
  240. // Create an example node.
  241. $args = array(
  242. 'type' => 'article',
  243. );
  244. $node = $this->drupalCreateNode($args);
  245. // Update the config.
  246. $config = metatag_config_load('node');
  247. $config->config['image_src']['value'] = '[node:field_image]';
  248. metatag_config_save($config);
  249. // Load the node page.
  250. $this->drupalGet('node/' . $node->nid);
  251. $this->assertResponse(200);
  252. // Confirm that the image_src meta tag has the expected values.
  253. $xpath = $this->xpath("//link[@rel='image_src']");
  254. $this->assertEqual(count($xpath), 1, 'One image_src meta tag found.');
  255. $this->assertEqual($xpath[0]['href'], $image_url, "The image_src meta tag was found with the field's default image.");
  256. // Confirm the file could be loaded.
  257. $this->drupalGet($xpath[0]['href']);
  258. $this->assertResponse(200, "The image_src meta tag's value could be loaded.");
  259. }
  260. /**
  261. * Confirm that a file on an image field will be output correctly.
  262. */
  263. function testNodeFieldValue() {
  264. // Update the 'content' config to use the field_image field as the
  265. // image_src meta tag.
  266. $config = metatag_config_load('node');
  267. $config->config['image_src']['value'] = '[node:field_image]';
  268. metatag_config_save($config);
  269. // Generate a test image file object.
  270. $image = $this->generateImageFile();
  271. $image_url = file_create_url($image->uri);
  272. // Dump out the file object, to aid with debugging.
  273. $this->verbose($image);
  274. // Create an example node.
  275. $args = array(
  276. 'type' => 'article',
  277. 'field_image' => array(
  278. LANGUAGE_NONE => array(
  279. array('fid' => $image->fid),
  280. ),
  281. ),
  282. );
  283. $node = $this->drupalCreateNode($args);
  284. // Forcibly reload the node, to avoid working with a cached version.
  285. $node = node_load($node->nid, NULL, TRUE);
  286. $this->verbose($node, 'Node');
  287. // Load the node page.
  288. $this->drupalGet('node/' . $node->nid);
  289. $this->assertResponse(200);
  290. // Confirm that the image_src meta tag has the expected values.
  291. $xpath = $this->xpath("//link[@rel='image_src']");
  292. $this->assertEqual(count($xpath), 1, 'One image_src meta tag found.');
  293. $this->assertEqual($xpath[0]['href'], $image_url, "The image_src meta tag was found with the node's image field's value.");
  294. // Confirm the file could be loaded.
  295. $this->drupalGet($xpath[0]['href']);
  296. $this->assertResponse(200, "The image_src meta tag's value could be loaded.");
  297. }
  298. /**
  299. * Confirm that when using a file field that allows multiple values, only the
  300. * first item will be used when outputting a single meta tag.
  301. */
  302. function testNodeFieldValueNotMultiple() {
  303. // Update the 'content' config to use the field_image field as the
  304. // image_src meta tag.
  305. $config = metatag_config_load('node');
  306. $config->config['image_src']['value'] = '[node:field_image]';
  307. metatag_config_save($config);
  308. // Update the image field to allow unlimited items.
  309. $field_name = 'field_image';
  310. $field = field_info_field($field_name);
  311. $field['cardinality'] = FIELD_CARDINALITY_UNLIMITED;
  312. field_update_field($field);
  313. // Generate a test image file object.
  314. $image1 = $this->generateImageFile();
  315. $image1_url = file_create_url($image1->uri);
  316. $image2 = $this->generateImageFile();
  317. $image2_url = file_create_url($image2->uri);
  318. // Dump out the file objects, to aid with debugging.
  319. $this->verbose($image1, 'Image #1');
  320. $this->verbose($image2, 'Image #2');
  321. // Create an example node.
  322. $args = array(
  323. 'type' => 'article',
  324. 'field_image' => array(
  325. LANGUAGE_NONE => array(
  326. array('fid' => $image1->fid),
  327. array('fid' => $image2->fid),
  328. ),
  329. ),
  330. );
  331. $node = $this->drupalCreateNode($args);
  332. // Forcibly reload the node, to avoid working with a cached version.
  333. $node = node_load($node->nid, NULL, TRUE);
  334. $this->verbose($node, 'Node');
  335. // Load the node page.
  336. $this->drupalGet('node/' . $node->nid);
  337. $this->assertResponse(200);
  338. // Confirm that the image_src meta tag has the expected values.
  339. $xpath = $this->xpath("//link[@rel='image_src']");
  340. $this->assertEqual(count($xpath), 1, 'Only one image_src meta tag found.');
  341. $this->assertEqual($xpath[0]['href'], $image1_url, "The image_src meta tag was found with the node's image field's first value.");
  342. $this->assertNotEqual($xpath[0]['href'], $image2_url, "The image_src meta tag does not contain the node's image field's second value.");
  343. // Confirm the file could be loaded.
  344. $this->drupalGet($xpath[0]['href']);
  345. $this->assertResponse(200, "The image_src meta tag's value could be loaded.");
  346. }
  347. /**
  348. * Confirm that when using a file field that allows multiple values, that
  349. * multiple images can be used and then it will result in multiple meta tags.
  350. */
  351. function testNodeFieldValueMultiple() {
  352. // Update the 'content' config to use the field_image field as the
  353. // image_src meta tag.
  354. $config = metatag_config_load('node');
  355. $config->config['og:image']['value'] = '[node:field_image]';
  356. metatag_config_save($config);
  357. // Update the image field to allow unlimited items.
  358. $field_name = 'field_image';
  359. $field = field_info_field($field_name);
  360. $field['cardinality'] = FIELD_CARDINALITY_UNLIMITED;
  361. field_update_field($field);
  362. // Generate a test image file object.
  363. $image1 = $this->generateImageFile();
  364. $image1_url = file_create_url($image1->uri);
  365. $image2 = $this->generateImageFile();
  366. $image2_url = file_create_url($image2->uri);
  367. // Dump out the file objects, to aid with debugging.
  368. $this->verbose($image1, 'Image #1');
  369. $this->verbose($image2, 'Image #2');
  370. // Create an example node.
  371. $args = array(
  372. 'type' => 'article',
  373. 'field_image' => array(
  374. LANGUAGE_NONE => array(
  375. array('fid' => $image1->fid),
  376. array('fid' => $image2->fid),
  377. ),
  378. ),
  379. );
  380. $node = $this->drupalCreateNode($args);
  381. // Forcibly reload the node, to avoid working with a cached version.
  382. $node = node_load($node->nid, NULL, TRUE);
  383. $this->verbose($node, 'Node');
  384. // Load the node page.
  385. $this->drupalGet('node/' . $node->nid);
  386. $this->assertResponse(200);
  387. // Confirm that the og:image meta tags have the expected values.
  388. $xpath = $this->xpath("//meta[@property='og:image']");
  389. $this->assertEqual(count($xpath), 2, 'Two og:image meta tags were found.');
  390. $this->assertEqual($xpath[0]['content'], $image1_url, "The first og:image meta tag has the correct image.");
  391. $this->assertEqual($xpath[1]['content'], $image2_url, "The second og:image meta tag has the correct image.");
  392. // Confirm the file could be loaded.
  393. $this->drupalGet($xpath[0]['content']);
  394. $this->assertResponse(200, "The first og:image meta tag's value could be loaded.");
  395. $this->drupalGet($xpath[1]['content']);
  396. $this->assertResponse(200, "The second og:image meta tag's value could be loaded.");
  397. }
  398. }