PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/postdao_test.php

https://github.com/gedex/thinktank
PHP | 269 lines | 169 code | 84 blank | 16 comment | 40 complexity | d28160d9f4f8111de9015719e1cbe568 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. require_once (dirname(__FILE__).'/simpletest/autorun.php');
  3. require_once (dirname(__FILE__).'/simpletest/web_tester.php');
  4. require_once (dirname(__FILE__).'/config.tests.inc.php');
  5. ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.$INCLUDE_PATH);
  6. require_once ("classes/class.ThinkTankTestCase.php");
  7. require_once ("class.Post.php");
  8. require_once ("class.Link.php");
  9. class TestOfPostDAO extends ThinkTankUnitTestCase {
  10. function TestOfPostDAO() {
  11. $this->UnitTestCase('PostDAO class test');
  12. }
  13. function setUp() {
  14. parent::setUp();
  15. //TODO: Insert test data into post table
  16. //$this->db->exec($q);
  17. //Add instance_owner
  18. $q = "INSERT INTO tt_owner_instances (owner_id, instance_id) VALUES (1, 1)";
  19. $this->db->exec($q);
  20. //Insert test data into test table
  21. $q = "INSERT INTO tt_users (user_id, user_name, full_name, avatar, last_updated) VALUES (13, 'ev', 'Ev Williams', 'avatar.jpg', '1/1/2005');";
  22. $this->db->exec($q);
  23. $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);";
  24. $this->db->exec($q);
  25. $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);";
  26. $this->db->exec($q);
  27. //Make public
  28. $q = "INSERT INTO tt_instances (network_user_id, network_username, is_public) VALUES (13, 'ev', 1);";
  29. $this->db->exec($q);
  30. $q = "INSERT INTO tt_instances (network_user_id, network_username, is_public) VALUES (18, 'shutterbug', 1);";
  31. $this->db->exec($q);
  32. $q = "INSERT INTO tt_instances (network_user_id, network_username, is_public) VALUES (19, 'linkbaiter', 1);";
  33. $this->db->exec($q);
  34. //Add a bunch of posts
  35. $counter = 0;
  36. while ($counter < 40) {
  37. $pseudo_minute = str_pad($counter, 2, "0", STR_PAD_LEFT);
  38. $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);";
  39. $this->db->exec($q);
  40. $counter++;
  41. }
  42. //Add some photo posts
  43. $counter = 0;
  44. while ($counter < 40) {
  45. $post_id = $counter + 40;
  46. $pseudo_minute = str_pad($counter, 2, "0", STR_PAD_LEFT);
  47. $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);";
  48. $this->db->exec($q);
  49. $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);";
  50. $this->db->exec($q);
  51. $counter++;
  52. }
  53. //Add some link posts
  54. $counter = 0;
  55. while ($counter < 40) {
  56. $post_id = $counter + 80;
  57. $pseudo_minute = str_pad(($counter), 2, "0", STR_PAD_LEFT);
  58. $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);";
  59. $this->db->exec($q);
  60. $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);";
  61. $this->db->exec($q);
  62. $counter++;
  63. }
  64. }
  65. function tearDown() {
  66. parent::tearDown();
  67. }
  68. function testIsRetweet() {
  69. $startwithcolon = "RT @ginatrapani: how to do (almost) everything in Google Buzz, including turn it off http://bit.ly/bfQTQH";
  70. $nostartnocolon = "Agreed: RT @ginatrapani guilty pleasure: dropping the &quot;my wife&quot; bomb on unsuspecting straight people, mid-conversation";
  71. $startwithcolonspaces = "RT @ginatrapani how to do (almost) everything in Google Buzz, including turn it off http://bit.ly/bfQTQH";
  72. $startwithcoloncutoff = "RT @ginatrapani: one of the most fun photo shoots &amp; interviews I've ever done http://bit.ly/9ldYNw thx.";
  73. $lowwercase = "rt @ginatrapani: one of the most fun photo shoots &amp; interviews I've ever done http://bit.ly/9ldYNw thx.";
  74. $o = 'ginatrapani';
  75. $this->assertTrue(RetweetDetector::isRetweet($startwithcolon, 'ginatrapani'));
  76. $this->assertTrue(RetweetDetector::isRetweet($nostartnocolon, 'ginatrapani'));
  77. $this->assertTrue(RetweetDetector::isRetweet($startwithcolonspaces, 'ginatrapani'));
  78. $this->assertTrue(RetweetDetector::isRetweet($startwithcoloncutoff, 'ginatrapani'));
  79. $this->assertTrue(RetweetDetector::isRetweet($lowwercase, 'ginatrapani'));
  80. }
  81. function testDetectRetweets() {
  82. $recent_tweets = array( new Post(array('id'=>1, 'author_user_id'=>10, 'author_username'=>'no one', 'author_avatar'=>'yo.jpg', 'source'=>'TweetDeck', 'pub_date'=>'', 'adj_pub_date'=>'', 'in_reply_to_user_id'=>'', 'in_reply_to_post_id'=>'', 'mention_count_cache'=>'', 'in_retweet_of_post_id'=>'', 'retweet_count_cache'=>'', 'post_id'=>9021481076, 'post_text'=>'guilty pleasure: dropping the "my wife" bomb on unsuspecting straight people, mid-conversation', 'network'=>'twitter')), new Post(array('id'=>1, 'author_user_id'=>10, 'author_username'=>'no one', 'author_avatar'=>'yo.jpg', 'source'=>'TweetDeck', 'pub_date'=>'', 'adj_pub_date'=>'', 'in_reply_to_user_id'=>'', 'in_reply_to_post_id'=>'', 'mention_count_cache'=>'', 'in_retweet_of_post_id'=>'', 'retweet_count_cache'=>'', 'post_id'=>9020176425, 'post_text'=>"a Google fangirl's take: no doubt Buzz's privacy issues are seriously problematic, but at least they're iterating quickly and openly.", 'network'=>'twitter')), new Post(array('id'=>1, 'author_user_id'=>10, 'author_username'=>'no one', 'author_avatar'=>'yo.jpg', 'source'=>'TweetDeck', 'pub_date'=>'', 'adj_pub_date'=>'', 'in_reply_to_user_id'=>'', 'in_reply_to_post_id'=>'', 'mention_count_cache'=>'', 'in_retweet_of_post_id'=>'', 'retweet_count_cache'=>'', 'post_id'=>9031523906, 'post_text'=>"one of the most fun photo shoots &amp; interviews I've ever done http://bit.ly/9ldYNw thx @voiceofsandiego, @dagnysalas, & @samuelhodgson", 'network'=>'twitter')), new Post(array('id'=>1, 'author_user_id'=>10, 'author_username'=>'no one', 'author_avatar'=>'yo.jpg', 'source'=>'TweetDeck', 'pub_date'=>'', 'adj_pub_date'=>'', 'in_reply_to_user_id'=>'', 'in_reply_to_post_id'=>'', 'mention_count_cache'=>'', 'in_retweet_of_post_id'=>'', 'retweet_count_cache'=>'', 'post_id'=>8925077246, 'post_text'=>"how to do (almost) everything in Google Buzz, including turn it off http://bit.ly/bfQTQH", 'network'=>'twitter')));
  83. $startwithcolon = "RT @ginatrapani: how to do (almost) everything in Google Buzz, including turn it off http://bit.ly/bfQTQH";
  84. $nostartnocolon = "Agreed: RT @ginatrapani guilty pleasure: dropping the &quot;my wife&quot; bomb on unsuspecting straight people, mid-conversation";
  85. $startwithcolonspaces = "RT @ginatrapani how to do (almost) everything in Google Buzz, including turn it off http://bit.ly/bfQTQH";
  86. $startwithcoloncutoff = "RT @ginatrapani: one of the most fun photo shoots &amp; interviews I've ever done http://bit.ly/9ldYNw thx.";
  87. $lowwercase = "rt @ginatrapani: one of the most fun photo shoots &amp; interviews I've ever done http://bit.ly/9ldYNw thx.";
  88. $nonexistent = "rt @ginatrapani this is a non-existent tweet";
  89. $this->assertTrue(RetweetDetector::detectOriginalTweet($nostartnocolon, $recent_tweets) == 9021481076);
  90. $this->assertTrue(RetweetDetector::detectOriginalTweet($startwithcolonspaces, $recent_tweets) == 8925077246);
  91. $this->assertTrue(RetweetDetector::detectOriginalTweet($startwithcoloncutoff, $recent_tweets) == 9031523906);
  92. $this->assertTrue(RetweetDetector::detectOriginalTweet($startwithcolon, $recent_tweets) == 8925077246);
  93. $this->assertTrue(RetweetDetector::detectOriginalTweet($nonexistent, $recent_tweets) === false);
  94. }
  95. function testGetPageOneOfPublicPosts() {
  96. //Instantiate DAO
  97. $pdao = new PostDAO($this->db, $this->logger);
  98. //Get page 1 containing 15 public posts
  99. $page_of_posts = $pdao->getPostsByPublicInstances(1, 15);
  100. //Assert DAO returns 15 posts
  101. $this->assertTrue(sizeof($page_of_posts) == 15);
  102. //Assert first post 1 contains the right text
  103. $this->assertTrue($page_of_posts[0]->post_text == "This is post 39");
  104. //Asert last post 15 contains the right text
  105. $this->assertTrue($page_of_posts[14]->post_text == "This is post 25");
  106. }
  107. function testGetPageTwoOfPublicPosts() {
  108. $pdao = new PostDAO($this->db, $this->logger);
  109. $page_of_posts = $pdao->getPostsByPublicInstances(2, 15);
  110. $this->assertTrue(sizeof($page_of_posts) == 15);
  111. $this->assertTrue($page_of_posts[0]->post_text == "This is post 24");
  112. $this->assertTrue($page_of_posts[14]->post_text == "This is post 10");
  113. }
  114. function testGetPageThreeOfPublicPosts() {
  115. $pdao = new PostDAO($this->db, $this->logger);
  116. $page_of_posts = $pdao->getPostsByPublicInstances(3, 15);
  117. //Assert DAO returns 10 posts
  118. $this->assertTrue(sizeof($page_of_posts) == 10);
  119. $this->assertTrue($page_of_posts[0]->post_text == "This is post 9");
  120. $this->assertTrue($page_of_posts[9]->post_text == "This is post 0");
  121. }
  122. function testGetTotalPagesAndPostsByPublicInstances() {
  123. $pdao = new PostDAO($this->db, $this->logger);
  124. $totals = $pdao->getTotalPagesAndPostsByPublicInstances(15);
  125. $this->assertTrue($totals["total_posts"] == 40);
  126. $this->assertTrue($totals["total_pages"] == 3);
  127. }
  128. //Start Public Photo Tests
  129. function testGetPageOneOfPhotoPublicPosts() {
  130. $pdao = new PostDAO($this->db, $this->logger);
  131. $page_of_posts = $pdao->getPhotoPostsByPublicInstances(1, 15);
  132. $this->assertTrue(sizeof($page_of_posts) == 15);
  133. $this->assertTrue($page_of_posts[0]->post_text == "This is image post 39");
  134. $this->assertTrue($page_of_posts[14]->post_text == "This is image post 25");
  135. }
  136. function testGetPageTwoOfPhotoPublicPosts() {
  137. $pdao = new PostDAO($this->db, $this->logger);
  138. $page_of_posts = $pdao->getPhotoPostsByPublicInstances(2, 15);
  139. $this->assertTrue(sizeof($page_of_posts) == 15);
  140. $this->assertTrue($page_of_posts[0]->post_text == "This is image post 24");
  141. $this->assertTrue($page_of_posts[14]->post_text == "This is image post 10");
  142. }
  143. function testGetPageThreeOfPhotoPublicPosts() {
  144. $pdao = new PostDAO($this->db, $this->logger);
  145. $page_of_posts = $pdao->getPhotoPostsByPublicInstances(3, 15);
  146. $this->assertTrue(sizeof($page_of_posts) == 10);
  147. $this->assertTrue($page_of_posts[0]->post_text == "This is image post 9");
  148. $this->assertTrue($page_of_posts[9]->post_text == "This is image post 0");
  149. }
  150. function testGetTotalPhotoPagesAndPostsByPublicInstances() {
  151. $pdao = new PostDAO($this->db, $this->logger);
  152. $totals = $pdao->getTotalPhotoPagesAndPostsByPublicInstances(15);
  153. $this->assertTrue($totals["total_posts"] == 40);
  154. $this->assertTrue($totals["total_pages"] == 3);
  155. }
  156. //Start Public Link Tests
  157. function testGetPageOneOfLinkPublicPosts() {
  158. $pdao = new PostDAO($this->db, $this->logger);
  159. $page_of_posts = $pdao->getLinkPostsByPublicInstances(1, 15);
  160. $this->assertTrue(sizeof($page_of_posts) == 15);
  161. $this->assertTrue($page_of_posts[0]->post_text == "This is link post 39");
  162. $this->assertTrue($page_of_posts[14]->post_text == "This is link post 25");
  163. }
  164. function testGetPageTwoOfLinkPublicPosts() {
  165. $pdao = new PostDAO($this->db, $this->logger);
  166. $page_of_posts = $pdao->getLinkPostsByPublicInstances(2, 15);
  167. $this->assertTrue(sizeof($page_of_posts) == 15);
  168. $this->assertTrue($page_of_posts[0]->post_text == "This is link post 24");
  169. $this->assertTrue($page_of_posts[14]->post_text == "This is link post 10");
  170. }
  171. function testGetPageThreeOfLinkPublicPosts() {
  172. $pdao = new PostDAO($this->db, $this->logger);
  173. $page_of_posts = $pdao->getLinkPostsByPublicInstances(3, 15);
  174. $this->assertTrue(sizeof($page_of_posts) == 10);
  175. $this->assertTrue($page_of_posts[0]->post_text == "This is link post 9");
  176. $this->assertTrue($page_of_posts[9]->post_text == "This is link post 0");
  177. }
  178. function testGetTotalLinkPagesAndPostsByPublicInstances() {
  179. $pdao = new PostDAO($this->db, $this->logger);
  180. $totals = $pdao->getTotalLinkPagesAndPostsByPublicInstances(15);
  181. $this->assertTrue($totals["total_posts"] == 40);
  182. $this->assertTrue($totals["total_pages"] == 3);
  183. }
  184. }
  185. ?>