comparison og_statistics.test @ 2:48f07e7acaca

OG Stats 1.0-rc2
author Franck Deroche <franck@defr.org>
date Tue, 24 Nov 2009 14:27:37 +0100
parents 9ce879ecbce6
children
comparison
equal deleted inserted replaced
0:9ce879ecbce6 2:48f07e7acaca
1 <?php 1 <?php
2 // $Id: og_statistics.test,v 1.2.2.2 2009/06/05 22:38:12 dereine Exp $ 2 // $Id: og_statistics.test,v 1.2.2.6 2009/09/07 19:22:31 dereine Exp $
3 3
4 /** 4 /**
5 * Test file for og_statitics. 5 * Test file for og_statitics.
6 * 6 *
7 * @TODO Use multiple groups for testing. 7 * @TODO Use multiple groups for testing.
9 */ 9 */
10 10
11 require_once drupal_get_path('module', 'og') .'/tests/og_testcase.php'; 11 require_once drupal_get_path('module', 'og') .'/tests/og_testcase.php';
12 12
13 class OgStatisticsTestCase extends OgTestCase { 13 class OgStatisticsTestCase extends OgTestCase {
14 function getInfo() { 14 public static function getInfo() {
15 return array( 15 return array(
16 'name' => t('Organic groups statistics'), 16 'name' => t('Organic groups statistics'),
17 'description' => t('tests statistics of og_statistics module'), 17 'description' => t('tests statistics of og_statistics module'),
18 'group' => t('Organic groups'), 18 'group' => t('Organic groups'),
19 ); 19 );
20 } 20 }
21 21
22 function setUp() { 22 function setUp() {
23 parent::setUp('node', 'user', 'comment', 'views', 'og', 'og_views', 'og_statistics', 'og_access'); 23 parent::setUp('node', 'user', 'comment', 'views', 'og', 'og_views', 'og_statistics', 'og_access', 'devel');
24 24
25 // Create a user with admin permissions. 25 // Create a user with admin permissions.
26 $web_admin = $this->drupalCreateUser(array('administer nodes', 'administer content types', 'access administration pages', 'administer site configuration', 'administer organic groups')); 26 $web_admin = $this->drupalCreateUser(array('administer nodes', 'administer content types', 'access administration pages', 'administer site configuration', 'administer organic groups'));
27 $this->drupalLogin($web_admin); 27 $this->drupalLogin($web_admin);
28 } 28 }
46 $stat = og_statistics_load($gid); 46 $stat = og_statistics_load($gid);
47 unset($stat['nid']); 47 unset($stat['nid']);
48 $members = 48 $members =
49 49
50 $empty = FALSE; 50 $empty = FALSE;
51 $this->assertFalse($empty, print_r($stat, TRUE));
52 foreach ($stat as $key => $item) { 51 foreach ($stat as $key => $item) {
53 $empty = $empty || $item == TRUE; 52 $empty = $empty || $item == TRUE;
54 } 53 }
55 $this->assertFalse($empty, t('All stats should have 0 as item')); 54 $this->assertFalse($empty, t('All stats should have 0 as item'));
56 } 55 }
80 $after = og_statistics_load($gid); 79 $after = og_statistics_load($gid);
81 $count_one_up = ($after['posts_count'] - $before['posts_count']) == 1; 80 $count_one_up = ($after['posts_count'] - $before['posts_count']) == 1;
82 81
83 $this->assertTrue($count_one_up, 'A node post in a group, counts the counter 1 up'); 82 $this->assertTrue($count_one_up, 'A node post in a group, counts the counter 1 up');
84 $this->assertEqual($node->created, $after['last_node_timestamp'], 'A node post in a group, sets the last node timestamp'); 83 $this->assertEqual($node->created, $after['last_node_timestamp'], 'A node post in a group, sets the last node timestamp');
84 $this->assertEqual($node->nid, $after['last_node_nid'], 'A node post in a group sets the last node nid');
85 $this->assertEqual($node->uid, $after['last_node_uid'], 'A node post in a group sets the last node uid');
85 86
86 // remove the post 87 // remove the post
87 node_delete($nid); 88 node_delete($nid);
88 $before = $after; 89 $before = $after;
89 $after = og_statistics_load($gid); 90 $after = og_statistics_load($gid);
90 $count_down = ($after['posts_count'] - $before['posts_count']) == -1; 91 $count_down = ($after['posts_count'] - $before['posts_count']) == -1;
91 92
92 $this->assertTrue($count_down, 'A node poast in a group was deleted, counts the counter 1 down'); 93 $this->assertTrue($count_down, 'A node poast in a group was deleted, counts the counter 1 down');
93 } 94 }
94 95
96 function drupalCreateComment($edit) {
97 if ($cid = comment_save($edit)) {
98 return $cid;
99 }
100 }
101
95 // Tests comments hooks statistic 102 // Tests comments hooks statistic
96 function TestOgStatisticsComment() { 103 function TestOgStatisticsComment() {
104 global $user;
97 // Create a group node content type. 105 // Create a group node content type.
98 $og_group_type = $this->drupalCreateContentType(); 106 $og_group_type = $this->drupalCreateContentType();
99 variable_set('og_content_type_usage_'. $og_group_type->name, 'group'); 107 variable_set('og_content_type_usage_'. $og_group_type->name, 'group');
100 108
101 // Create a group post content type. 109 // Create a group post content type.
109 $gid = $this->addOgGroup($og_group_type->name); 117 $gid = $this->addOgGroup($og_group_type->name);
110 $before = og_statistics_load($gid); 118 $before = og_statistics_load($gid);
111 119
112 // add a test post 120 // add a test post
113 $nids[] = $nid = $this->addOgPost($og_post_type->name, array($gid)); 121 $nids[] = $nid = $this->addOgPost($og_post_type->name, array($gid));
122 $node = node_load($nid);
114 123
115 // adds some comments 124 // adds some comments
116 $edit = array(); 125 $edit = array();
117 $edit['subject'] = $this->randomName(10); 126 $edit['subject'] = $this->randomName(10);
118 $edit['comment'] = $this->randomName(50); 127 $edit['comment'] = $this->randomName(50);
119 $edit['nid'] = $nid; 128 $edit['nid'] = $nid;
120 $cid = comment_save($edit); 129 $edit['format'] = FILTER_FORMAT_DEFAULT;
130 $edit['node'] = $node;
131 $edit['uid'] = $user->uid;
132 $edit['pid'] = NULL;
133 $edit['cid'] = NULL;
134 $cid = $this->drupalCreateComment($edit);
121 $comment = _comment_load($cid); 135 $comment = _comment_load($cid);
122 136
123 $after = og_statistics_load($gid); 137 $after = og_statistics_load($gid);
124 $count_up = ($after['comments_count'] - $before['comments_count']) == 1; 138 $count_up = ($after['comments_count'] - $before['comments_count']) == 1;
125 $this->assertTrue($count_up, 'new comment to one group post, counts up the comment counter'); 139 $this->assertTrue($count_up, 'new comment to one group post, counts up the comment counter');
126 $this->assertEqual($comment->timestamp, $after['last_comment_timestamp'], 'new comment to one group post, counts up the comment counter'); 140 $this->assertEqual($comment->timestamp, $after['last_comment_timestamp'], 'new comment to one group post, counts up the comment counter');
141 $this->assertEqual($comment->cid, $after['last_comment_cid'], 'New comment to one group post sets the last comment cid.');
142 $this->assertEqual($comment->nid, $after['last_comment_nid'], 'New comment to one group post sets the last comment nid.');
143 $this->assertEqual($comment->uid, $after['last_comment_uid'], 'New comment to one group post sets the last comment uid.');
144
145 // add another comment.
146
147 $edit = array();
148 $edit['subject'] = $this->randomName(10);
149 $edit['comment'] = $this->randomName(50);
150 $edit['nid'] = $nid;
151 $edit['format'] = FILTER_FORMAT_DEFAULT;
152 $edit['node'] = $node;
153 $edit['uid'] = $user->uid;
154 $edit['pid'] = NULL;
155 $edit['cid'] = NULL;
156 $cid = $this->drupalCreateComment($edit);
157
158 // Remove the node and check whether comment count is updated
159 // reload the node, to update the stats.
160 $node = node_load($node, NULL, TRUE);
161
162 node_delete($nid);
163 $stat = og_statistics_load($gid);
164 $this->assertEqual($stat['comments_count'], 0, 'all comments were deleted');
127 } 165 }
128 166
129 function TestOgStatisticsOg() { 167 function TestOgStatisticsOg() {
130 // Create a group node content type. 168 // Create a group node content type.
131 $og_group_type = $this->drupalCreateContentType(); 169 $og_group_type = $this->drupalCreateContentType();
151 189
152 $count_up = ($after['members_count'] - $before['members_count']) == 1; 190 $count_up = ($after['members_count'] - $before['members_count']) == 1;
153 191
154 $this->assertTrue($count_up, 'New subscription to a group adds the counter 1 up'); 192 $this->assertTrue($count_up, 'New subscription to a group adds the counter 1 up');
155 $this->assertEqual($time, $after['last_member_timestamp'], 'New subscription to a group adds the counter 1 up'); 193 $this->assertEqual($time, $after['last_member_timestamp'], 'New subscription to a group adds the counter 1 up');
194 $this->assertEqual($account->uid, $after['last_member_uid'], 'New subscription to a group sets the last member uid.');
195 }
196
197 function TestOgStatisticsDelete() {
198 // Create a group node content type.
199 $og_group_type = $this->drupalCreateContentType();
200 variable_set('og_content_type_usage_'. $og_group_type->name, 'group');
201
202 // Create a group post content type.
203 $og_post_type = $this->drupalCreateContentType();
204 variable_set('og_content_type_usage_'. $og_post_type->name, 'group_post_standard');
205
206 // Rebuild the menu so the new content types will appear in the menu.
207 menu_rebuild();
208
209 $gid = $this->addOgGroup($og_group_type->name);
210 // create a test group
211 $gid = $this->addOgGroup($og_group_type->name);
212 $before = og_statistics_load($gid);
213
214 // Delete the node, the stat-record should be deleted too.
215 $node = node_load($gid);
216 node_delete($node->nid);
217 $after = og_statistics_load($gid);
218
219 $this->assertTrue(empty($after), 'Og statistics record get deleted');
156 } 220 }
157 } 221 }
158 222