-
Notifications
You must be signed in to change notification settings - Fork 676
/
TestOfPostDAO.php
executable file
·278 lines (191 loc) · 11.1 KB
/
TestOfPostDAO.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
require_once dirname(__FILE__).'/config.tests.inc.php';
require_once $SOURCE_ROOT_PATH.'extlib/simpletest/autorun.php';
require_once $SOURCE_ROOT_PATH.'extlib/simpletest/web_tester.php';
ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.$INCLUDE_PATH);
require_once $SOURCE_ROOT_PATH.'tests/classes/class.ThinkTankUnitTestCase.php';
require_once $SOURCE_ROOT_PATH.'webapp/model/class.Post.php';
require_once $SOURCE_ROOT_PATH.'webapp/model/class.Link.php';
class TestOfPostDAO extends ThinkTankUnitTestCase {
var $logger;
function TestOfPostDAO() {
$this->UnitTestCase('PostDAO class test');
}
function setUp() {
parent::setUp();
$this->logger = Logger::getInstance();
//TODO: Insert test data into post table
//$this->db->exec($q);
//Add instance_owner
$q = "INSERT INTO tt_owner_instances (owner_id, instance_id) VALUES (1, 1)";
$this->db->exec($q);
//Insert test data into test table
$q = "INSERT INTO tt_users (user_id, user_name, full_name, avatar, last_updated) VALUES (13, 'ev', 'Ev Williams', 'avatar.jpg', '1/1/2005');";
$this->db->exec($q);
$q = "INSERT INTO tt_users (user_id, user_name, full_name, avatar, is_protected, follower_count) VALUES (18, 'shutterbug', 'Shutter Bug', 'avatar.jpg', 0, 10);";
$this->db->exec($q);
$q = "INSERT INTO tt_users (user_id, user_name, full_name, avatar, is_protected, follower_count) VALUES (19, 'linkbaiter', 'Link Baiter', 'avatar.jpg', 0, 10);";
$this->db->exec($q);
//Make public
$q = "INSERT INTO tt_instances (network_user_id, network_username, is_public) VALUES (13, 'ev', 1);";
$this->db->exec($q);
$q = "INSERT INTO tt_instances (network_user_id, network_username, is_public) VALUES (18, 'shutterbug', 1);";
$this->db->exec($q);
$q = "INSERT INTO tt_instances (network_user_id, network_username, is_public) VALUES (19, 'linkbaiter', 1);";
$this->db->exec($q);
//Add a bunch of posts
$counter = 0;
while ($counter < 40) {
$pseudo_minute = str_pad($counter, 2, "0", STR_PAD_LEFT);
$q = "INSERT INTO tt_posts (post_id, author_user_id, author_username, author_fullname, author_avatar, post_text, source, pub_date, mention_count_cache, retweet_count_cache) VALUES ($counter, 13, 'ev', 'Ev Williams', 'avatar.jpg', 'This is post $counter', 'web', '2006-01-01 00:$pseudo_minute:00', ".rand(0, 4).", 5);";
$this->db->exec($q);
$counter++;
}
//Add some photo posts
$counter = 0;
while ($counter < 40) {
$post_id = $counter + 40;
$pseudo_minute = str_pad($counter, 2, "0", STR_PAD_LEFT);
$q = "INSERT INTO tt_posts (post_id, author_user_id, author_username, author_fullname, author_avatar, post_text, source, pub_date, mention_count_cache, retweet_count_cache) VALUES ($post_id, 18, 'shutterbug', 'Shutter Bug', 'avatar.jpg', 'This is image post $counter', 'web', '2006-01-02 00:$pseudo_minute:00', 0, 0);";
$this->db->exec($q);
$q = "INSERT INTO tt_links (url, expanded_url, title, clicks, post_id, is_image) VALUES ('http://example.com/".$counter."', 'http://example.com/".$counter.".jpg', '', 0, $post_id, 1);";
$this->db->exec($q);
$counter++;
}
//Add some link posts
$counter = 0;
while ($counter < 40) {
$post_id = $counter + 80;
$pseudo_minute = str_pad(($counter), 2, "0", STR_PAD_LEFT);
$q = "INSERT INTO tt_posts (post_id, author_user_id, author_username, author_fullname, author_avatar, post_text, source, pub_date, mention_count_cache, retweet_count_cache) VALUES ($post_id, 19, 'linkbaiter', 'Link Baiter', 'avatar.jpg', 'This is link post $counter', 'web', '2006-03-01 00:$pseudo_minute:00', 0, 0);";
$this->db->exec($q);
$q = "INSERT INTO tt_links (url, expanded_url, title, clicks, post_id, is_image) VALUES ('http://example.com/".$counter."', 'http://example.com/".$counter.".html', 'Link $counter', 0, $post_id, 0);";
$this->db->exec($q);
$counter++;
}
}
function tearDown() {
parent::tearDown();
$this->logger->close();
}
function testGetPageOneOfPublicPosts() {
//Instantiate DAO
$pdao = new PostDAO($this->db, $this->logger);
//Get page 1 containing 15 public posts
$page_of_posts = $pdao->getPostsByPublicInstances(1, 15);
//Assert DAO returns 15 posts
$this->assertTrue(sizeof($page_of_posts) == 15);
//Assert first post 1 contains the right text
$this->assertTrue($page_of_posts[0]->post_text == "This is post 39");
//Asert last post 15 contains the right text
$this->assertTrue($page_of_posts[14]->post_text == "This is post 25");
}
function testGetPageTwoOfPublicPosts() {
$pdao = new PostDAO($this->db, $this->logger);
$page_of_posts = $pdao->getPostsByPublicInstances(2, 15);
$this->assertTrue(sizeof($page_of_posts) == 15);
$this->assertTrue($page_of_posts[0]->post_text == "This is post 24");
$this->assertTrue($page_of_posts[14]->post_text == "This is post 10");
}
function testGetPageThreeOfPublicPosts() {
$pdao = new PostDAO($this->db, $this->logger);
$page_of_posts = $pdao->getPostsByPublicInstances(3, 15);
//Assert DAO returns 10 posts
$this->assertTrue(sizeof($page_of_posts) == 10);
$this->assertTrue($page_of_posts[0]->post_text == "This is post 9");
$this->assertTrue($page_of_posts[9]->post_text == "This is post 0");
}
function testGetTotalPagesAndPostsByPublicInstances() {
$pdao = new PostDAO($this->db, $this->logger);
$totals = $pdao->getTotalPagesAndPostsByPublicInstances(15);
$this->assertTrue($totals["total_posts"] == 40);
$this->assertTrue($totals["total_pages"] == 3);
}
//Start Public Photo Tests
function testGetPageOneOfPhotoPublicPosts() {
$pdao = new PostDAO($this->db, $this->logger);
$page_of_posts = $pdao->getPhotoPostsByPublicInstances(1, 15);
$this->assertTrue(sizeof($page_of_posts) == 15);
$this->assertTrue($page_of_posts[0]->post_text == "This is image post 39");
$this->assertTrue($page_of_posts[14]->post_text == "This is image post 25");
}
function testGetPageTwoOfPhotoPublicPosts() {
$pdao = new PostDAO($this->db, $this->logger);
$page_of_posts = $pdao->getPhotoPostsByPublicInstances(2, 15);
$this->assertTrue(sizeof($page_of_posts) == 15);
$this->assertTrue($page_of_posts[0]->post_text == "This is image post 24");
$this->assertTrue($page_of_posts[14]->post_text == "This is image post 10");
}
function testGetPageThreeOfPhotoPublicPosts() {
$pdao = new PostDAO($this->db, $this->logger);
$page_of_posts = $pdao->getPhotoPostsByPublicInstances(3, 15);
$this->assertTrue(sizeof($page_of_posts) == 10);
$this->assertTrue($page_of_posts[0]->post_text == "This is image post 9");
$this->assertTrue($page_of_posts[9]->post_text == "This is image post 0");
}
function testGetTotalPhotoPagesAndPostsByPublicInstances() {
$pdao = new PostDAO($this->db, $this->logger);
$totals = $pdao->getTotalPhotoPagesAndPostsByPublicInstances(15);
$this->assertTrue($totals["total_posts"] == 40);
$this->assertTrue($totals["total_pages"] == 3);
}
//Start Public Link Tests
function testGetPageOneOfLinkPublicPosts() {
$pdao = new PostDAO($this->db, $this->logger);
$page_of_posts = $pdao->getLinkPostsByPublicInstances(1, 15);
$this->assertTrue(sizeof($page_of_posts) == 15);
$this->assertTrue($page_of_posts[0]->post_text == "This is link post 39");
$this->assertTrue($page_of_posts[14]->post_text == "This is link post 25");
}
function testGetPageTwoOfLinkPublicPosts() {
$pdao = new PostDAO($this->db, $this->logger);
$page_of_posts = $pdao->getLinkPostsByPublicInstances(2, 15);
$this->assertTrue(sizeof($page_of_posts) == 15);
$this->assertTrue($page_of_posts[0]->post_text == "This is link post 24");
$this->assertTrue($page_of_posts[14]->post_text == "This is link post 10");
}
function testGetPageThreeOfLinkPublicPosts() {
$pdao = new PostDAO($this->db, $this->logger);
$page_of_posts = $pdao->getLinkPostsByPublicInstances(3, 15);
$this->assertTrue(sizeof($page_of_posts) == 10);
$this->assertTrue($page_of_posts[0]->post_text == "This is link post 9");
$this->assertTrue($page_of_posts[9]->post_text == "This is link post 0");
}
function testGetTotalLinkPagesAndPostsByPublicInstances() {
$pdao = new PostDAO($this->db, $this->logger);
$totals = $pdao->getTotalLinkPagesAndPostsByPublicInstances(15);
$this->assertTrue($totals["total_posts"] == 40);
$this->assertTrue($totals["total_pages"] == 3);
}
function testGetTotalPostsByUser() {
$pdao = new PostDAO($this->db, $this->logger);
$total_posts = $pdao->getTotalPostsByUser(13);
$this->assertTrue($total_posts == 40);
}
function testAssignParent() {
//Add two "parent" posts
$q = "INSERT INTO tt_posts (post_id, author_user_id, author_username, author_fullname, author_avatar, post_text, source, pub_date, mention_count_cache, retweet_count_cache) VALUES (550, 19, 'linkbaiter', 'Link Baiter', 'avatar.jpg', 'This is parent post 1', 'web', '2006-03-01 00:01:00', 1, 0);";
$this->db->exec($q);
$q = "INSERT INTO tt_posts (post_id, author_user_id, author_username, author_fullname, author_avatar, post_text, source, pub_date, mention_count_cache, retweet_count_cache) VALUES (551, 19, 'linkbaiter', 'Link Baiter', 'avatar.jpg', 'This is parent post 2', 'web', '2006-03-01 00:01:00', 0, 0);";
$this->db->exec($q);
//Add a post with the parent post 550
$q = "INSERT INTO tt_posts (post_id, author_user_id, author_username, author_fullname, author_avatar, post_text, source, pub_date, mention_count_cache, retweet_count_cache, in_reply_to_post_id) VALUES (552, 19, 'linkbaiter', 'Link Baiter', 'avatar.jpg', 'This is a reply with the wrong parent', 'web', '2006-03-01 00:01:00', 0, 0, 550);";
$this->db->exec($q);
$pdao = new PostDAO($this->db, $this->logger);
$post = $pdao->getPost(552);
//Assert parent post is 550
$this->assertEqual($post->in_reply_to_post_id, 550);
//Change parent post to 551
$pdao->assignParent(551, 552);
$child_post = $pdao->getPost(552);
//Assert parent post is now 551
$this->assertEqual($child_post->in_reply_to_post_id, 551);
//Assert old parent post has one fewer reply total
$old_parent = $pdao->getPost(550);
$this->assertEqual($old_parent->mention_count_cache, 0);
//Assert new parent post has one more reply total
$new_parent = $pdao->getPost(551);
$this->assertEqual($new_parent->mention_count_cache, 1);
}
}
?>