From 0d938201a5bb518f934ecb2d3dc99c572334fbe7 Tue, 15 Nov 2011 07:22:45 +0100
From: Jorrit Schippers <jorrit@161217.no-reply.drupal.org>
Date: Tue, 15 Nov 2011 07:21:10 +0100
Subject: [PATCH] Issue #1341900 by Jorrit: Fixed image fallback being used instead of image from node when there is no node row in the opengraph_meta table.

diff --git a/opengraph_meta.common.inc b/opengraph_meta.common.inc
index 6d990a4..e2f7b1b 100644
--- a/opengraph_meta.common.inc
+++ b/opengraph_meta.common.inc
@@ -119,7 +119,6 @@
     $ret = array_merge($defaults, $this->get_og_optional_tag_defaults($node));
 
     $ret[self::SITE_NAME] = $this->settings_obj->get(OPENGRAPH_META_VAR_SITE_NAME, $this->settings_obj->get('site_name','Drupal'));
-    $ret[self::IMAGE] = $this->settings_obj->get(OPENGRAPH_META_VAR_FALLBACK_IMG, '');
 
     if (!empty($node)) {
       // if node given then override defaults
@@ -127,6 +126,7 @@
       $body = $this->get_node_body($node);
       $ret[self::DESCRIPTION] = !empty($body) ? mb_substr(strip_tags($body),0,200) : $node->title;
       $ret[self::TYPE] = $this->settings_obj->get(OPENGRAPH_META_VAR_CONTENT_TYPE_.$node->type,'');
+      $ret[self::IMAGE] = $this->settings_obj->get(OPENGRAPH_META_VAR_FALLBACK_IMG, '');
 
       $images = $this->harvest_images_from_node($node);
       if (!empty($images)) {
diff --git a/tests/Basic.test b/tests/Basic.test
index f720807..4781f2c 100644
--- a/tests/Basic.test
+++ b/tests/Basic.test
@@ -151,6 +151,40 @@
   }
 
 
+  function testNodeOverrideDefaultImage() {
+    // empty node values
+    // Important: there is no row in the data store!
+    $node = $this->_build_test_node(1);
+    drupal_set_title($node->title);
+    $this->ogm_settings->set(OPENGRAPH_META_VAR_CONTENT_TYPE_.'type1','');
+    $this->ogm_settings->set(OPENGRAPH_META_VAR_FALLBACK_IMG, 'default.jpg');
+
+    // Check if the default image is returned when the node does not contain an image
+    $this->ogm->render_data($node, $this->ogm->load_node_data($node));
+    $this->_check_rendered_meta_tags(array(
+      'og:'.OpenGraphMeta::TITLE => $node->title,
+      'og:'.OpenGraphMeta::DESCRIPTION => mb_substr(strip_tags($node->body),0,200),
+      'og:'.OpenGraphMeta::SITE_NAME => 'Drupal',
+      'og:'.OpenGraphMeta::IMAGE => url('default.jpg', array('absolute' => TRUE)),
+      'og:'.OpenGraphMeta::URL => url('node/1', array('absolute' => TRUE)),
+    ));
+
+    // Add an image to the node
+    $node->field_image = array(
+      'filemime' => 'image/jpeg',
+      'filepath' => 'node.jpg',
+    );
+    $this->ogm->render_data($node, $this->ogm->load_node_data($node));
+    $this->_check_rendered_meta_tags(array(
+      'og:'.OpenGraphMeta::TITLE => $node->title,
+      'og:'.OpenGraphMeta::DESCRIPTION => mb_substr(strip_tags($node->body),0,200),
+      'og:'.OpenGraphMeta::SITE_NAME => 'Drupal',
+      'og:'.OpenGraphMeta::IMAGE => url('node.jpg', array('absolute' => TRUE)),
+      'og:'.OpenGraphMeta::URL => url('node/1', array('absolute' => TRUE)),
+    ));
+  }
+
+
   private function _build_test_node($nid) {
     $node = new stdClass();
     $node->nid = $nid;