Projet

Général

Profil

Paste
Télécharger (4,97 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / node / tests / node_test.module @ 76597ebf

1
<?php
2

    
3
/**
4
 * @file
5
 * A dummy module for testing node related hooks.
6
 *
7
 * This is a dummy module that implements node related hooks to test API
8
 * interaction with the Node module.
9
 */
10

    
11
/**
12
 * Implements hook_node_load().
13
 */
14
function node_test_node_load($nodes, $types) {
15
  // Add properties to each loaded node which record the parameters that were
16
  // passed in to this function, so the tests can check that (a) this hook was
17
  // called, and (b) the parameters were what we expected them to be.
18
  $nids = array_keys($nodes);
19
  ksort($nids);
20
  sort($types);
21
  foreach ($nodes as $node) {
22
    $node->node_test_loaded_nids = $nids;
23
    $node->node_test_loaded_types = $types;
24
  }
25
}
26

    
27
/**
28
 * Implements hook_node_view().
29
 */
30
function node_test_node_view($node, $view_mode) {
31
  if ($view_mode == 'rss') {
32
    // Add RSS elements and namespaces when building the RSS feed.
33
    $node->rss_elements[] = array(
34
      'key' => 'testElement',
35
      'value' => t('Value of testElement RSS element for node !nid.', array('!nid' => $node->nid)),
36
    );
37
    $node->rss_namespaces['xmlns:drupaltest'] = 'http://example.com/test-namespace';
38

    
39
    // Add content that should be displayed only in the RSS feed.
40
    $node->content['extra_feed_content'] = array(
41
      '#markup' => '<p>' . t('Extra data that should appear only in the RSS feed for node !nid.', array('!nid' => $node->nid)) . '</p>',
42
      '#weight' => 10,
43
    );
44
  }
45

    
46
  if ($view_mode != 'rss') {
47
    // Add content that should NOT be displayed in the RSS feed.
48
    $node->content['extra_non_feed_content'] = array(
49
      '#markup' => '<p>' . t('Extra data that should appear everywhere except the RSS feed for node !nid.', array('!nid' => $node->nid)) . '</p>',
50
    );
51
  }
52
}
53

    
54
/**
55
 * Implements hook_node_grants().
56
 */
57
function node_test_node_grants($account, $op) {
58
  // Give everyone full grants so we don't break other node tests.
59
  // Our node access tests asserts three realms of access.
60
  // See testGrantAlter().
61
  return array(
62
    'test_article_realm' => array(1),
63
    'test_page_realm' => array(1),
64
    'test_alter_realm' => array(2),
65
  );
66
}
67

    
68
/**
69
 * Implements hook_node_access_records().
70
 */
71
function node_test_node_access_records($node) {
72
  // Return nothing when testing for empty responses.
73
  if (!empty($node->disable_node_access)) {
74
    return;
75
  }
76
  $grants = array();
77
  if ($node->type == 'article') {
78
    // Create grant in arbitrary article_realm for article nodes.
79
    $grants[] = array(
80
      'realm' => 'test_article_realm',
81
      'gid' => 1,
82
      'grant_view' => 1,
83
      'grant_update' => 0,
84
      'grant_delete' => 0,
85
      'priority' => 0,
86
    );
87
  }
88
  elseif ($node->type == 'page') {
89
    // Create grant in arbitrary page_realm for page nodes.
90
    $grants[] = array(
91
      'realm' => 'test_page_realm',
92
      'gid' => 1,
93
      'grant_view' => 1,
94
      'grant_update' => 0,
95
      'grant_delete' => 0,
96
      'priority' => 0,
97
    );
98
  }
99
  return $grants;
100
}
101

    
102
/**
103
 * Implements hook_node_access_records_alter().
104
 */
105
function node_test_node_access_records_alter(&$grants, $node) {
106
  if (!empty($grants)) {
107
    foreach ($grants as $key => $grant) {
108
      // Alter grant from test_page_realm to test_alter_realm and modify the gid.
109
      if ($grant['realm'] == 'test_page_realm' && $node->promote) {
110
        $grants[$key]['realm'] = 'test_alter_realm';
111
        $grants[$key]['gid'] = 2;
112
      }
113
    }
114
  }
115
}
116

    
117
/**
118
 * Implements hook_node_grants_alter().
119
 */
120
function node_test_node_grants_alter(&$grants, $account, $op) {
121
  // Return an empty array of grants to prove that we can alter by reference.
122
  $grants = array();
123
}
124

    
125
/**
126
 * Implements hook_node_presave().
127
 */
128
function node_test_node_presave($node) {
129
  if ($node->title == 'testing_node_presave') {
130
    // Sun, 19 Nov 1978 05:00:00 GMT
131
    $node->created = 280299600;
132
    // Drupal 1.0 release.
133
    $node->changed = 979534800;
134
  }
135
  // Determine changes.
136
  if (!empty($node->original) && $node->original->title == 'test_changes') {
137
    if ($node->original->title != $node->title) {
138
      $node->title .= '_presave';
139
    }
140
  }
141
}
142

    
143
/**
144
 * Implements hook_node_update().
145
 */
146
function node_test_node_update($node) {
147
  // Determine changes on update.
148
  if (!empty($node->original) && $node->original->title == 'test_changes') {
149
    if ($node->original->title != $node->title) {
150
      $node->title .= '_update';
151
    }
152
  }
153
}
154

    
155
/**
156
 * Implements hook_entity_view_mode_alter().
157
 */
158
function node_test_entity_view_mode_alter(&$view_mode, $context) {
159
  // Only alter the view mode if we are on the test callback.
160
  if ($change_view_mode = variable_get('node_test_change_view_mode', '')) {
161
    $view_mode = $change_view_mode;
162
  }
163
}
164

    
165
/**
166
 * Implements hook_node_insert().
167
 *
168
 * This tests saving a node on node insert.
169
 *
170
 * @see NodeSaveTest::testNodeSaveOnInsert()
171
 */
172
function node_test_node_insert($node) {
173
  // Set the node title to the node ID and save.
174
  if ($node->title == 'new') {
175
    $node->title = 'Node '. $node->nid;
176
    // Remove the is_new flag, so that the node is updated and not inserted
177
    // again.
178
    unset($node->is_new);
179
    node_save($node);
180
  }
181
}