node_access_test.module

You are here

  1. 7 modules/node/tests/node_access_test.module
  2. 8 core/modules/node/tests/modules/node_access_test/node_access_test.module

A dummy module implementing node access related hooks for testing purposes.

A dummy module implementing node access related hooks to test API interaction with the Node module. This module restricts view permission to those with a special 'node test view' permission.

Functions

File

modules/node/tests/node_access_test.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * A dummy module implementing node access related hooks for testing purposes.
  5. *
  6. * A dummy module implementing node access related hooks to test API interaction
  7. * with the Node module. This module restricts view permission to those with
  8. * a special 'node test view' permission.
  9. */
  10. /**
  11. * Implements hook_node_grants().
  12. */
  13. function node_access_test_node_grants($account, $op) {
  14. $grants = array();
  15. // First grant a grant to the author for own content.
  16. $grants['node_access_test_author'] = array($account->uid);
  17. if ($op == 'view' && user_access('node test view', $account)) {
  18. $grants['node_access_test'] = array(8888, 8889);
  19. }
  20. if ($op == 'view' && $account->uid == variable_get('node_test_node_access_all_uid', 0)) {
  21. $grants['node_access_all'] = array(0);
  22. }
  23. return $grants;
  24. }
  25. /**
  26. * Implements hook_node_access_records().
  27. */
  28. function node_access_test_node_access_records($node) {
  29. $grants = array();
  30. // For NodeAccessBaseTableTestCase, only set records for private nodes.
  31. if (!variable_get('node_access_test_private') || $node->private) {
  32. $grants[] = array(
  33. 'realm' => 'node_access_test',
  34. 'gid' => 8888,
  35. 'grant_view' => 1,
  36. 'grant_update' => 0,
  37. 'grant_delete' => 0,
  38. 'priority' => 0,
  39. );
  40. $grants[] = array(
  41. 'realm' => 'node_access_test',
  42. 'gid' => 8889,
  43. 'grant_view' => 1,
  44. 'grant_update' => 0,
  45. 'grant_delete' => 0,
  46. 'priority' => 0,
  47. );
  48. // For the author realm, the GID is equivalent to a UID, which
  49. // means there are many many groups of just 1 user.
  50. $grants[] = array(
  51. 'realm' => 'node_access_test_author',
  52. 'gid' => $node->uid,
  53. 'grant_view' => 1,
  54. 'grant_update' => 1,
  55. 'grant_delete' => 1,
  56. 'priority' => 0,
  57. );
  58. }
  59. return $grants;
  60. }
  61. /**
  62. * Implements hook_permission().
  63. *
  64. * Sets up permissions for this module.
  65. */
  66. function node_access_test_permission() {
  67. return array('node test view' => array('title' => 'View content'));
  68. }
  69. /**
  70. * Implements hook_menu().
  71. *
  72. * Sets up a page that lists nodes.
  73. */
  74. function node_access_test_menu() {
  75. $items = array();
  76. $items['node_access_test_page'] = array(
  77. 'title' => 'Node access test',
  78. 'page callback' => 'node_access_test_page',
  79. 'access arguments' => array('access content'),
  80. 'type' => MENU_SUGGESTED_ITEM,
  81. );
  82. $items['node_access_entity_test_page'] = array(
  83. 'title' => 'Node access test',
  84. 'page callback' => 'node_access_entity_test_page',
  85. 'access arguments' => array('access content'),
  86. 'type' => MENU_SUGGESTED_ITEM,
  87. );
  88. return $items;
  89. }
  90. /**
  91. * Page callback for node access test page.
  92. *
  93. * Page should say "No nodes" if there are no nodes, and "Yes, # nodes" (with
  94. * the number filled in) if there were nodes the user could access. Also, the
  95. * database query is shown, and a list of the node IDs, for debugging purposes.
  96. * And if there is a query exception, the page says "Exception" and gives the
  97. * error.
  98. */
  99. function node_access_test_page() {
  100. $output = '';
  101. try {
  102. $query = db_select('node', 'mytab')
  103. ->fields('mytab');
  104. $query->addTag('node_access');
  105. $result = $query->execute()->fetchAll();
  106. if (count($result)) {
  107. $output .= '<p>Yes, ' . count($result) . ' nodes</p>';
  108. $output .= '<ul>';
  109. foreach ($result as $item) {
  110. $output .= '<li>' . $item->nid . '</li>';
  111. }
  112. $output .= '</ul>';
  113. }
  114. else {
  115. $output .= '<p>No nodes</p>';
  116. }
  117. $output .= '<p>' . ((string) $query ) . '</p>';
  118. }
  119. catch (Exception $e) {
  120. $output = '<p>Exception</p>';
  121. $output .= '<p>' . $e->getMessage() . '</p>';
  122. }
  123. return $output;
  124. }
  125. /**
  126. * Page callback for node access entity test page.
  127. *
  128. * Page should say "No nodes" if there are no nodes, and "Yes, # nodes" (with
  129. * the number filled in) if there were nodes the user could access. Also, the
  130. * database query is shown, and a list of the node IDs, for debugging purposes.
  131. * And if there is a query exception, the page says "Exception" and gives the
  132. * error.
  133. *
  134. * @see node_access_test_menu()
  135. */
  136. function node_access_entity_test_page() {
  137. $output = '';
  138. try {
  139. $query = new EntityFieldQuery;
  140. $result = $query->fieldCondition('body', 'value', 'A', 'STARTS_WITH')->execute();
  141. if (!empty($result['node'])) {
  142. $output .= '<p>Yes, ' . count($result['node']) . ' nodes</p>';
  143. $output .= '<ul>';
  144. foreach ($result['node'] as $nid => $v) {
  145. $output .= '<li>' . $nid . '</li>';
  146. }
  147. $output .= '</ul>';
  148. }
  149. else {
  150. $output .= '<p>No nodes</p>';
  151. }
  152. }
  153. catch (Exception $e) {
  154. $output = '<p>Exception</p>';
  155. $output .= '<p>' . $e->getMessage() . '</p>';
  156. }
  157. return $output;
  158. }
  159. /**
  160. * Implements hook_form_BASE_FORM_ID_alter().
  161. */
  162. function node_access_test_form_node_form_alter(&$form, $form_state) {
  163. // Only show this checkbox for NodeAccessBaseTableTestCase.
  164. if (variable_get('node_access_test_private')) {
  165. $form['private'] = array(
  166. '#type' => 'checkbox',
  167. '#title' => t('Private'),
  168. '#description' => t('Check here if this content should be set private and only shown to privileged users.'),
  169. '#default_value' => isset($form['#node']->private) ? $form['#node']->private : FALSE,
  170. );
  171. }
  172. }
  173. /**
  174. * Implements hook_node_load().
  175. */
  176. function node_access_test_node_load($nodes, $types) {
  177. $result = db_query('SELECT nid, private FROM {node_access_test} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)));
  178. foreach ($result as $record) {
  179. $nodes[$record->nid]->private = $record->private;
  180. }
  181. }
  182. /**
  183. * Implements hook_node_delete().
  184. */
  185. function node_access_test_node_delete($node) {
  186. db_delete('node_access_test')->condition('nid', $node->nid)->execute();
  187. }
  188. /**
  189. * Implements hook_node_insert().
  190. */
  191. function node_access_test_node_insert($node) {
  192. _node_access_test_node_write($node);
  193. }
  194. /**
  195. * Implements hook_nodeapi_update().
  196. */
  197. function node_access_test_node_update($node) {
  198. _node_access_test_node_write($node);
  199. }
  200. /**
  201. * Helper for node insert/update.
  202. */
  203. function _node_access_test_node_write($node) {
  204. if (isset($node->private)) {
  205. db_merge('node_access_test')
  206. ->key(array('nid' => $node->nid))
  207. ->fields(array('private' => (int) $node->private))
  208. ->execute();
  209. }
  210. }