PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/TestOfGridController.php

http://github.com/ginatrapani/ThinkUp
PHP | 303 lines | 236 code | 33 blank | 34 comment | 0 complexity | 5c069227caca8afad341ba2e25bfc7c5 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. *
  4. * ThinkUp/tests/TestOfGridController.php
  5. *
  6. * Copyright (c) 2009-2012 Mark Wilkie, Gina Trapani, Guillaume Boudreau
  7. *
  8. * LICENSE:
  9. *
  10. * This file is part of ThinkUp (http://thinkupapp.com).
  11. *
  12. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  14. * later version.
  15. *
  16. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  21. * <http://www.gnu.org/licenses/>.
  22. *
  23. *
  24. * @author Mark Wilkie <mark[at]bitterpill[dot]org>
  25. * @author Gina Trapani <ginatrapani[at]gmail[dot]com>
  26. * @author Guillaume Boudreau <gboudreau[at]pommepause[dot]com>
  27. * @license http://www.gnu.org/licenses/gpl.html
  28. * @copyright 2009-2012 Mark Wilkie, Gina Trapani, Guillaume Boudreau
  29. */
  30. require_once dirname(__FILE__).'/init.tests.php';
  31. require_once THINKUP_WEBAPP_PATH.'_lib/extlib/simpletest/autorun.php';
  32. require_once THINKUP_WEBAPP_PATH.'config.inc.php';
  33. class TestOfGridController extends ThinkUpUnitTestCase {
  34. public function setUp(){
  35. parent::setUp();
  36. $webapp = Webapp::getInstance();
  37. $webapp->registerPlugin('twitter', 'TwitterPlugin');
  38. }
  39. public function tearDown() {
  40. parent::tearDown();
  41. GridController::$MAX_ROWS = 5000;
  42. }
  43. public function testConstructor() {
  44. $controller = new GridController(true);
  45. $this->assertTrue(isset($controller));
  46. }
  47. public function testNotLoggedIn() {
  48. $controller = new GridController(true);
  49. $results = $controller->go();
  50. $v_mgr = $controller->getViewManager();
  51. $config = Config::getInstance();
  52. $this->assertEqual('You must <a href="'.$config->getValue('site_root_path').
  53. 'session/login.php">log in</a> to do this.', $v_mgr->getTemplateDataItem('error_msg'));
  54. }
  55. public function testMissingParams() {
  56. $this->simulateLogin('me@example.com');
  57. $controller = new GridController(true);
  58. ob_start();
  59. $controller->control();
  60. $results = ob_get_contents();
  61. ob_end_clean();
  62. $ob = json_decode($results);
  63. $this->assertEqual($ob->status, 'failed');
  64. $this->assertEqual($ob->message, 'Missing Parameters');
  65. }
  66. public function testNonExistentUser() {
  67. $this->simulateLogin('me@example.com');
  68. $_GET['u'] = 'idontexist';
  69. $_GET['n'] = 'idontexist';
  70. $controller = new GridController(true);
  71. ob_start();
  72. $controller->control();
  73. $results = ob_get_contents();
  74. ob_end_clean();
  75. $ob = json_decode($results);
  76. $this->assertEqual($ob->status, 'failed');
  77. $this->assertEqual($ob->message, 'idontexistis not configured.');
  78. }
  79. public function testOwnerWithoutAccess() {
  80. $builders = $this->buildData();
  81. $this->simulateLogin('me@example.com');
  82. $_GET['u'] = 'someuser2';
  83. $_GET['n'] = 'twitter';
  84. ob_start();
  85. $controller = new GridController(true);
  86. $controller->control();
  87. $results = ob_get_contents();
  88. ob_end_clean();
  89. $ob = json_decode($results);
  90. $this->assertEqual($ob->status, 'failed');
  91. $this->assertEqual($ob->message, 'Insufficient privileges.');
  92. }
  93. public function testOwnerWithAccessTweetsAll() {
  94. $builders = $this->buildData();
  95. $this->simulateLogin('me@example.com');
  96. $_GET['u'] = 'someuser1';
  97. $_GET['n'] = 'twitter';
  98. $_GET['d'] = 'tweets-all';
  99. $controller = new GridController(true);
  100. $this->assertTrue(isset($controller));
  101. ob_start();
  102. $controller->control();
  103. $results = ob_get_contents();
  104. ob_end_clean();
  105. $json = substr($results, 29, strrpos($results, ';') - 30);
  106. $ob = json_decode( $json );
  107. $this->assertEqual($ob->status, 'success');
  108. $this->assertEqual(count($ob->posts), 3);
  109. }
  110. public function testOwnerWithAccessTweetsAllMaxLimit() {
  111. $builders = $this->buildData();
  112. GridController::$MAX_ROWS = 1;
  113. $this->simulateLogin('me@example.com');
  114. $_GET['u'] = 'someuser1';
  115. $_GET['n'] = 'twitter';
  116. $_GET['d'] = 'tweets-all';
  117. $controller = new GridController(true);
  118. $this->assertTrue(isset($controller));
  119. ob_start();
  120. $controller->control();
  121. $results = ob_get_contents();
  122. ob_end_clean();
  123. $json = substr($results, 29, strrpos($results, ';') - 30);
  124. $ob = json_decode( $json );
  125. $this->assertEqual($ob->status, 'success');
  126. $this->assertEqual(count($ob->posts), 2);
  127. }
  128. public function testOwnerWithAccessTweetsAllMaxNoLimit() {
  129. $builders = $this->buildData();
  130. GridController::$MAX_ROWS = 0;
  131. $this->simulateLogin('me@example.com');
  132. $_GET['u'] = 'someuser1';
  133. $_GET['n'] = 'twitter';
  134. $_GET['d'] = 'tweets-all';
  135. $_GET['nolimit'] = '1';
  136. $controller = new GridController(true);
  137. $this->assertTrue(isset($controller));
  138. ob_start();
  139. $controller->control();
  140. $results = ob_get_contents();
  141. ob_end_clean();
  142. $json = substr($results, 29, strrpos($results, ';') - 30);
  143. $ob = json_decode( $json );
  144. $this->assertEqual($ob->status, 'success');
  145. $this->assertEqual(count($ob->posts), 3);
  146. }
  147. public function testReplyToSearch() {
  148. $builders = $this->buildData(0,0);
  149. $this->simulateLogin('me@example.com');
  150. $_GET['u'] = 'someuser1';
  151. $_GET['n'] = 'twitter';
  152. $_GET['t'] = '10765432100123456781';
  153. $controller = new GridController(true);
  154. $this->assertTrue(isset($controller));
  155. ob_start();
  156. $controller->control();
  157. $results = ob_get_contents();
  158. ob_end_clean();
  159. $json = substr($results, 29, strrpos($results, ';') - 30);
  160. $ob = json_decode( $json );
  161. $this->assertEqual($ob->status, 'success');
  162. $this->assertEqual(count($ob->posts), 2);
  163. $this->assertEqual($ob->posts[0]->text, 'Reply to a post');
  164. $this->assertEqual($ob->posts[0]->post_id_str, '10765432100123456783_str');
  165. }
  166. public function testReplyToSearchNotLoggedIn() {
  167. $builders = $this->buildData();
  168. //$this->simulateLogin('me@example.com');
  169. $_GET['u'] = 'someuser1';
  170. $_GET['n'] = 'twitter';
  171. $_GET['t'] = '10765432100123456781';
  172. // a private instance
  173. $controller = new GridController(true);
  174. try {
  175. $controller->control();
  176. $this->fail("should throw auth exception");
  177. } catch(Exception $e) {
  178. $this->assertPattern('/You must.*log in/',$e->getMessage());
  179. }
  180. // public instance, but protected posts
  181. $builders = null;
  182. $public = 1;
  183. $builders = $this->buildData($public);
  184. $this->assertTrue(isset($controller));
  185. ob_start();
  186. $controller->control();
  187. $results = ob_get_contents();
  188. ob_end_clean();
  189. $json = substr($results, 29, strrpos($results, ';') - 30);
  190. $ob = json_decode( $json );
  191. $this->assertEqual($ob->status, 'success');
  192. $this->assertEqual(count($ob->posts), 1);
  193. // public instance, and not protected posts
  194. $builders = null;
  195. $public = 1;
  196. $protected = 0;
  197. $builders = $this->buildData($public, $protected);
  198. $this->assertTrue(isset($controller));
  199. ob_start();
  200. $controller->control();
  201. $results = ob_get_contents();
  202. ob_end_clean();
  203. $json = substr($results, 29, strrpos($results, ';') - 30);
  204. $ob = json_decode( $json );
  205. $this->assertEqual($ob->status, 'success');
  206. $this->assertEqual(count($ob->posts), 2);
  207. $this->assertEqual($ob->posts[0]->text, 'Reply to a post');
  208. $this->assertEqual($ob->posts[0]->post_id_str, '10765432100123456783_str');
  209. }
  210. public function testReplyToSearchFilterOutProtected() {
  211. $builders = $this->buildData();
  212. $this->simulateLogin('me@example.com');
  213. $_GET['u'] = 'someuser1';
  214. $_GET['n'] = 'twitter';
  215. $_GET['t'] = '10765432100123456781';
  216. $controller = new GridController(true);
  217. $this->assertTrue(isset($controller));
  218. ob_start();
  219. $controller->control();
  220. $results = ob_get_contents();
  221. ob_end_clean();
  222. $json = substr($results, 29, strrpos($results, ';') - 30);
  223. $ob = json_decode( $json );
  224. $this->assertEqual($ob->status, 'success');
  225. $this->assertEqual(count($ob->posts), 1);
  226. }
  227. public function testNoProfilerOutput() {
  228. // Enable profiler
  229. $config = Config::getInstance();
  230. $config->setValue('enable_profiler', true);
  231. $_SERVER['HTTP_HOST'] = 'something';
  232. $builders = $this->buildData();
  233. $this->simulateLogin('me@example.com');
  234. $_GET['u'] = 'someuser1';
  235. $_GET['n'] = 'twitter';
  236. $_GET['d'] = 'tweets-all';
  237. $controller = new GridController(true);
  238. $this->assertTrue(isset($controller));
  239. ob_start();
  240. $results = $controller->go();
  241. $results .= ob_get_contents();
  242. ob_end_clean();
  243. $json = substr($results, 29, strrpos($results, ';') - 30);
  244. $ob = json_decode($json);
  245. // If the profiler outputs HTML (it shouldn't), the following will fail
  246. $this->assertIsA($ob, 'stdClass');
  247. unset($_SERVER['HTTP_HOST']);
  248. }
  249. private function buildData($is_public = 0, $is_protected = 1) {
  250. $owner_builder = FixtureBuilder::build('owners', array('id'=>1, 'email'=>'me@example.com'));
  251. $user_builder = FixtureBuilder::build('users', array('user_id'=>123, 'user_name'=>'someuser1',
  252. 'network'=>'twitter'));
  253. $user_builder2 = FixtureBuilder::build('users', array('user_id'=>1234, 'user_name'=>'someuser2',
  254. 'network'=>'twitter'));
  255. $instance_builder = FixtureBuilder::build('instances', array('id'=>1, 'network_username'=>'someuser1',
  256. 'network'=>'twitter', 'network_user_id' => 123, 'is_public' => $is_public));
  257. $instance_builder2 = FixtureBuilder::build('instances', array('id'=>2, 'network_username'=>'someuser2',
  258. 'network'=>'twitter'));
  259. $owner_instance_builder = FixtureBuilder::build('owner_instances', array('instance_id'=>1, 'owner_id'=>1));
  260. $posts1_builder = FixtureBuilder::build('posts', array('author_username'=>'someuser1','author_user_id' => 123,
  261. 'post_text'=>'@someuser1 My first post', 'network'=>'twitter', 'post_id' => '10765432100123456781'));
  262. $posts2_builder = FixtureBuilder::build('posts', array('author_username'=>'someuser1','author_user_id' => 123,
  263. 'post_text'=>'My second @someuser1 post', 'network'=>'twitter', 'post_id' => '10765432100123456782'));
  264. $reply_builder = FixtureBuilder::build('posts', array('post_id' => '10765432100123456783',
  265. 'author_username'=>'reply_user', 'post_text'=>'Reply to a post', 'network'=>'twitter',
  266. 'in_reply_to_post_id' => '10765432100123456781', 'author_user_id'=>'1234','is_protected' => $is_protected));
  267. return array($owner_builder, $instance_builder, $owner_instance_builder, $posts1_builder,
  268. $posts2_builder, $user_builder, $user_builder2, $instance_builder2, $reply_builder);
  269. }
  270. }