From b7459d8eb3def52ecdd35c65a55fac7e999f6cde Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Wed, 24 Nov 2010 16:25:39 +0000
Subject: [PATCH] - Patch #884948 by David_Rothstein: the wrong  parameter is
 sometimes passed in to hook_node_load().

---
 modules/node/node.module            |  5 +++-
 modules/node/node.test              | 44 +++++++++++++++++++++++++++++
 modules/node/tests/node_test.module | 16 +++++++++++
 3 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/modules/node/node.module b/modules/node/node.module
index 831dc734e0a1..062078908451 100644
--- a/modules/node/node.module
+++ b/modules/node/node.module
@@ -3854,7 +3854,10 @@ protected function attachLoad(&$nodes, $revision_id = FALSE) {
         $function($nodes_of_type);
       }
     }
-    $this->hookLoadArguments[] = array_keys($typed_nodes);
+    // Besides the list of nodes, pass one additional argument to
+    // hook_node_load(), containing a list of node types that were loaded.
+    $argument = array_keys($typed_nodes);
+    $this->hookLoadArguments = array($argument);
     parent::attachLoad($nodes, $revision_id);
   }
 
diff --git a/modules/node/node.test b/modules/node/node.test
index df189bc74adf..27abb4f4abf3 100644
--- a/modules/node/node.test
+++ b/modules/node/node.test
@@ -81,6 +81,50 @@ class NodeLoadMultipleUnitTest extends DrupalWebTestCase {
   }
 }
 
+/**
+ * Tests for the hooks invoked during node_load().
+ */
+class NodeLoadHooksTestCase extends DrupalWebTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => 'Node load hooks',
+      'description' => 'Test the hooks invoked when a node is being loaded.',
+      'group' => 'Node',
+    );
+  }
+
+  function setUp() {
+    parent::setUp('node_test');
+  }
+
+  /**
+   * Test that hook_node_load() is invoked correctly.
+   */
+  function testHookNodeLoad() {
+    // Create some sample articles and pages.
+    $node1 = $this->drupalCreateNode(array('type' => 'article', 'status' => NODE_PUBLISHED));
+    $node2 = $this->drupalCreateNode(array('type' => 'article', 'status' => NODE_PUBLISHED));
+    $node3 = $this->drupalCreateNode(array('type' => 'article', 'status' => NODE_NOT_PUBLISHED));
+    $node4 = $this->drupalCreateNode(array('type' => 'page', 'status' => NODE_NOT_PUBLISHED));
+
+    // Check that when a set of nodes that only contains articles is loaded,
+    // the properties added to the node by node_test_load_node() correctly
+    // reflect the expected values.
+    $nodes = node_load_multiple(array(), array('status' => NODE_PUBLISHED));
+    $loaded_node = end($nodes);
+    $this->assertEqual($loaded_node->node_test_loaded_nids, array($node1->nid, $node2->nid), t('hook_node_load() received the correct list of node IDs the first time it was called.'));
+    $this->assertEqual($loaded_node->node_test_loaded_types, array('article'), t('hook_node_load() received the correct list of node types the first time it was called.'));
+
+    // Now, as part of the same page request, load a set of nodes that contain
+    // both articles and pages, and make sure the parameters passed to
+    // node_test_node_load() are correctly updated.
+    $nodes = node_load_multiple(array(), array('status' => NODE_NOT_PUBLISHED));
+    $loaded_node = end($nodes);
+    $this->assertEqual($loaded_node->node_test_loaded_nids, array($node3->nid, $node4->nid), t('hook_node_load() received the correct list of node IDs the second time it was called.'));
+    $this->assertEqual($loaded_node->node_test_loaded_types, array('article', 'page'), t('hook_node_load() received the correct list of node types the second time it was called.'));
+  }
+}
+
 class NodeRevisionsTestCase extends DrupalWebTestCase {
   protected $nodes;
   protected $logs;
diff --git a/modules/node/tests/node_test.module b/modules/node/tests/node_test.module
index cfc503da0d75..c32bc1ec0c55 100644
--- a/modules/node/tests/node_test.module
+++ b/modules/node/tests/node_test.module
@@ -7,6 +7,22 @@
  * the Node module.
  */
 
+/**
+ * Implements hook_node_load().
+ */
+function node_test_node_load($nodes, $types) {
+  // Add properties to each loaded node which record the parameters that were
+  // passed in to this function, so the tests can check that (a) this hook was
+  // called, and (b) the parameters were what we expected them to be.
+  $nids = array_keys($nodes);
+  ksort($nids);
+  sort($types);
+  foreach ($nodes as $node) {
+    $node->node_test_loaded_nids = $nids;
+    $node->node_test_loaded_types = $types;
+  }
+}
+
 /**
  * Implements hook_node_view().
  */
-- 
GitLab