metatag.image.test 17 KB

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