NodeLoadHooksTest.php 2.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

/**
 * @file
 * Definition of Drupal\node\Tests\NodeLoadHooksTest.
 */

namespace Drupal\node\Tests;

/**
 * Tests for the hooks invoked during node_load().
 */
class NodeLoadHooksTest extends NodeTestBase {
14 15 16 17 18 19 20 21

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('node_test');

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  public static function getInfo() {
    return array(
      'name' => 'Node load hooks',
      'description' => 'Test the hooks invoked when a node is being loaded.',
      'group' => 'Node',
    );
  }

  /**
   * Tests 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.
43
    $nodes = entity_load_multiple_by_properties('node', array('status' => NODE_PUBLISHED));
44
    $loaded_node = end($nodes);
45 46
    $this->assertEqual($loaded_node->node_test_loaded_nids, array($node1->nid, $node2->nid), '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'), 'hook_node_load() received the correct list of node types the first time it was called.');
47 48 49 50

    // 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.
51
    $nodes = entity_load_multiple_by_properties('node', array('status' => NODE_NOT_PUBLISHED));
52
    $loaded_node = end($nodes);
53 54
    $this->assertEqual($loaded_node->node_test_loaded_nids, array($node3->nid, $node4->nid), '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'), 'hook_node_load() received the correct list of node types the second time it was called.');
55 56
  }
}