From 275275f246c980afad902b53ea7658f3a060ed65 Mon, 28 Nov 2011 19:17:21 +0100
From: Jorrit Schippers <jorrit@161217.no-reply.drupal.org>
Date: Tue, 15 Nov 2011 07:21:02 +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 06ee8ec..7d600fc 100644
--- a/opengraph_meta.common.inc
+++ b/opengraph_meta.common.inc
@@ -126,7 +126,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
@@ -147,9 +146,8 @@
         $description = !empty($node->body) ? $node->body : $node->title;
 
       $ret[self::DESCRIPTION] = mb_substr(strip_tags($description),0,200);
-
-      
       $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, '');
 
       $image_paths = $this->harvest_images_from_node($node);
       if (!empty($image_paths))
diff --git a/tests/Basic.test b/tests/Basic.test
index a0d0f42..01e1e1d 100644
--- a/tests/Basic.test
+++ b/tests/Basic.test
@@ -228,6 +228,39 @@
 
 
 
+  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;
@@ -281,4 +314,4 @@
   public function update_tags($field_data_including_nid, $primary_key = array()) {
     $this->tags[$field_data_including_nid->nid] = $field_data_including_nid;
   }
-}
\ No newline at end of file
+}