Simpletest Coverage - modules/comment/comment.pages.inc

1 <?php
2 // $Id: comment.pages.inc,v 1.24 2009/08/01 05:58:18 dries Exp $
3
4 /**
5 * @file
6 * User page callbacks for the comment module.
7 */
8
9 /**
10 * This function is responsible for generating a comment reply form.
11 * There are several cases that have to be handled, including:
12 * - replies to comments
13 * - replies to nodes
14 * - attempts to reply to nodes that can no longer accept comments
15 * - respecting access permissions ('access comments', 'post comments', etc.)
16 *
17 * The node or comment that is being replied to must appear above the comment
18 * form to provide the user context while authoring the comment.
19 *
20 * @param $node
21 * Every comment belongs to a node. This is that node.
22 *
23 * @param $pid
24 * Some comments are replies to other comments. In those cases, $pid is the parent
25 * comment's cid.
26 *
27 * @return
28 * The rendered parent node or comment plus the new comment form.
29 */
30 function comment_reply($node, $pid = NULL) {
31 // Set the breadcrumb trail.
32 drupal_set_breadcrumb(array(l(t('Home'), NULL), l($node->title, 'node/' . $node->nid)));
33 $op = isset($_POST['op']) ? $_POST['op'] : '';
34 $build = array();
35
36 if (user_access('access comments')) {
37 // The user is previewing a comment prior to submitting it.
38 if ($op == t('Preview')) {
39 if (user_access('post comments')) {
40 $build['comment_form'] = drupal_get_form('comment_form', (object) array('pid' => $pid, 'nid' => $node->nid));
41 }
42 else {
43 drupal_set_message(t('You are not authorized to post comments.'), 'error');
44 drupal_goto("node/$node->nid");
45 }
46 }
47 else {
48 // $pid indicates that this is a reply to a comment.
49 if ($pid) {
50 // Load the comment whose cid = $pid
51 $comment = db_query('SELECT c.*, u.uid, u.name AS registered_name, u.signature, u.picture, u.data FROM {comment} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = :cid AND c.status = :status', array(
52 ':cid' => $pid,
53 ':status' => COMMENT_PUBLISHED,
54 ))->fetchObject();
55 if ( $comment ) {
56 // If that comment exists, make sure that the current comment and the
57 // parent comment both belong to the same parent node.
58 if ($comment->nid != $node->nid) {
59 // Attempting to reply to a comment not belonging to the current nid.
60 drupal_set_message(t('The comment you are replying to does not exist.'), 'error');
61 drupal_goto("node/$node->nid");
62 }
63 // Display the parent comment
64 $comment = drupal_unpack($comment);
65 $comment->node_type = 'comment_node_' . $node->type;
66 field_attach_load('comment', array($comment->cid => $comment));
67 $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
68 $build['comment_parent'] = comment_build($comment);
69 }
70 else {
71 drupal_set_message(t('The comment you are replying to does not exist.'), 'error');
72 drupal_goto("node/$node->nid");
73 }
74 }
75 // This is the case where the comment is in response to a node. Display the node.
76 elseif (user_access('access content')) {
77 $build['comment_node'] = node_build($node);
78 }
79
80 // Should we show the reply box?
81 if ($node->comment != COMMENT_NODE_OPEN) {
82 drupal_set_message(t("This discussion is closed: you can't post new comments."), 'error');
83 drupal_goto("node/$node->nid");
84 }
85 elseif (user_access('post comments')) {
86 $edit = array('nid' => $node->nid, 'pid' => $pid);
87 $build['comment_form'] = drupal_get_form('comment_form', (object) $edit);
88 }
89 else {
90 drupal_set_message(t('You are not authorized to post comments.'), 'error');
91 drupal_goto("node/$node->nid");
92 }
93 }
94 }
95 else {
96 drupal_set_message(t('You are not authorized to view comments.'), 'error');
97 drupal_goto("node/$node->nid");
98 }
99
100 return $build;
101 }
102
103 /**
104 * Menu callback; publish specified comment.
105 *
106 * @param $cid ID of comment to be published.
107 */
108 function comment_approve($cid) {
109 // Load the comment whose cid = $cid
110 if ($comment = comment_load($cid)) {
111 $comment->status = COMMENT_PUBLISHED;
112 $comment->comment_format = $comment->format;
113 comment_save($comment);
114
115 drupal_set_message(t('Comment approved.'));
116 drupal_goto('node/' . $comment->nid);
117 }
118 else {
119 drupal_set_message(t('The comment you are approving does not exist.'), 'error');
120 drupal_goto();
121 }
122 }
123

Legend

Missed
lines code that were not excersized during program execution.
Covered
lines code were excersized during program execution.
Comment/non executable
Comment or non-executable line of code.
Dead
lines of code that according to xdebug could not be executed. This is counted as coverage code because in almost all cases it is code that runnable.