PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ForumTest.php

https://github.com/lenix/silverstripe-forum
PHP | 358 lines | 252 code | 76 blank | 30 comment | 3 complexity | cbf103ade55747c5bd693218899671d3 MD5 | raw file
  1. <?php
  2. /**
  3. * @todo Write Tests for doPostMessageForm()
  4. */
  5. class ForumTest extends FunctionalTest {
  6. static $fixture_file = "forum/tests/ForumTest.yml";
  7. static $use_draft_site = true;
  8. function testCanView() {
  9. // test viewing not logged in
  10. if($member = Member::currentUser()) $member->logOut();
  11. $public = $this->objFromFixture('Forum', 'general');
  12. $private = $this->objFromFixture('Forum', 'loggedInOnly');
  13. $limited = $this->objFromFixture('Forum', 'limitedToGroup');
  14. $noposting = $this->objFromFixture('Forum', 'noPostingForum');
  15. $inherited = $this->objFromFixture('Forum', 'inheritedForum');
  16. $this->assertTrue($public->canView());
  17. $this->assertFalse($private->canView());
  18. $this->assertFalse($limited->canView());
  19. $this->assertTrue($noposting->canView());
  20. $this->assertFalse($inherited->canView());
  21. // try logging in a member
  22. $member = $this->objFromFixture('Member', 'test1');
  23. $member->logIn();
  24. $this->assertTrue($public->canView());
  25. $this->assertTrue($private->canView());
  26. $this->assertFalse($limited->canView());
  27. $this->assertTrue($noposting->canView());
  28. $this->assertFalse($inherited->canView());
  29. // login as a person with access to restricted forum
  30. $member = $this->objFromFixture('Member', 'test2');
  31. $member->logIn();
  32. $this->assertTrue($public->canView());
  33. $this->assertTrue($private->canView());
  34. $this->assertTrue($limited->canView());
  35. $this->assertTrue($noposting->canView());
  36. $this->assertFalse($inherited->canView());
  37. // Moderator should be able to view his own forums
  38. $member = $this->objFromFixture('Member', 'moderator');
  39. $member->logIn();
  40. $this->assertTrue($public->canView());
  41. $this->assertTrue($private->canView());
  42. $this->assertTrue($limited->canView());
  43. $this->assertTrue($noposting->canView());
  44. $this->assertTrue($inherited->canView());
  45. }
  46. function testCanPost() {
  47. // test viewing not logged in
  48. if($member = Member::currentUser()) $member->logOut();
  49. $public = $this->objFromFixture('Forum', 'general');
  50. $private = $this->objFromFixture('Forum', 'loggedInOnly');
  51. $limited = $this->objFromFixture('Forum', 'limitedToGroup');
  52. $noposting = $this->objFromFixture('Forum', 'noPostingForum');
  53. $inherited = $this->objFromFixture('Forum', 'inheritedForum');
  54. $this->assertTrue($public->canPost());
  55. $this->assertFalse($private->canPost());
  56. $this->assertFalse($limited->canPost());
  57. $this->assertFalse($noposting->canPost());
  58. $this->assertFalse($inherited->canPost());
  59. // try logging in a member
  60. $member = $this->objFromFixture('Member', 'test1');
  61. $member->logIn();
  62. $this->assertTrue($public->canPost());
  63. $this->assertTrue($private->canPost());
  64. $this->assertFalse($limited->canPost());
  65. $this->assertFalse($noposting->canPost());
  66. $this->assertFalse($inherited->canPost());
  67. // login as a person with access to restricted forum
  68. $member = $this->objFromFixture('Member', 'test2');
  69. $member->logIn();
  70. $this->assertTrue($public->canPost());
  71. $this->assertTrue($private->canPost());
  72. $this->assertTrue($limited->canPost());
  73. $this->assertFalse($noposting->canPost());
  74. $this->assertFalse($inherited->canPost());
  75. // Moderator should be able to view his own forums
  76. $member = $this->objFromFixture('Member', 'moderator');
  77. $member->logIn();
  78. $this->assertTrue($public->canPost());
  79. $this->assertTrue($private->canPost());
  80. $this->assertFalse($limited->canPost());
  81. $this->assertFalse($noposting->canPost());
  82. $this->assertFalse($inherited->canPost());
  83. }
  84. function testSuspended() {
  85. $private = $this->objFromFixture('Forum', 'loggedInOnly');
  86. $limited = $this->objFromFixture('Forum', 'limitedToGroup');
  87. $inheritedForum_loggedInOnly = $this->objFromFixture('Forum', 'inheritedForum_loggedInOnly');
  88. SS_Datetime::set_mock_now('2011-10-10 12:00:00');
  89. // try logging in a member suspendedexpired
  90. $suspendedexpired = $this->objFromFixture('Member', 'suspendedexpired');
  91. $this->assertFalse($suspendedexpired->IsSuspended());
  92. $suspendedexpired->logIn();
  93. $this->assertTrue($private->canPost());
  94. $this->assertTrue($limited->canPost());
  95. $this->assertTrue($inheritedForum_loggedInOnly->canPost());
  96. // try logging in a member suspended
  97. $suspended = $this->objFromFixture('Member', 'suspended');
  98. $this->assertTrue($suspended->IsSuspended());
  99. $suspended->logIn();
  100. $this->assertFalse($private->canPost());
  101. $this->assertFalse($limited->canPost());
  102. $this->assertFalse($inheritedForum_loggedInOnly->canPost());
  103. }
  104. function testCanModerate() {
  105. // test viewing not logged in
  106. if($member = Member::currentUser()) $member->logOut();
  107. $public = $this->objFromFixture('Forum', 'general');
  108. $private = $this->objFromFixture('Forum', 'loggedInOnly');
  109. $limited = $this->objFromFixture('Forum', 'limitedToGroup');
  110. $noposting = $this->objFromFixture('Forum', 'noPostingForum');
  111. $inherited = $this->objFromFixture('Forum', 'inheritedForum');
  112. $this->assertFalse($public->canModerate());
  113. $this->assertFalse($private->canModerate());
  114. $this->assertFalse($limited->canModerate());
  115. $this->assertFalse($noposting->canModerate());
  116. $this->assertFalse($inherited->canModerate());
  117. // try logging in a member
  118. $member = $this->objFromFixture('Member', 'test1');
  119. $member->logIn();
  120. $this->assertFalse($public->canModerate());
  121. $this->assertFalse($private->canModerate());
  122. $this->assertFalse($limited->canModerate());
  123. $this->assertFalse($noposting->canModerate());
  124. $this->assertFalse($inherited->canModerate());
  125. // login as a person with access to restricted forum
  126. $member = $this->objFromFixture('Member', 'test2');
  127. $member->logIn();
  128. $this->assertFalse($public->canModerate());
  129. $this->assertFalse($private->canModerate());
  130. $this->assertFalse($limited->canModerate());
  131. $this->assertFalse($noposting->canModerate());
  132. $this->assertFalse($inherited->canModerate());
  133. // Moderator should be able to view his own forums
  134. $member = $this->objFromFixture('Member', 'moderator');
  135. $member->logIn();
  136. $this->assertTrue($public->canModerate());
  137. $this->assertTrue($private->canModerate());
  138. $this->assertTrue($limited->canModerate());
  139. $this->assertTrue($noposting->canModerate());
  140. $this->assertTrue($inherited->canModerate());
  141. }
  142. function testCanAttach() {
  143. $canAttach = $this->objFromFixture('Forum', 'general');
  144. $this->assertTrue($canAttach->canAttach());
  145. $noAttach = $this->objFromFixture('Forum', 'forum1cat2');
  146. $this->assertFalse($noAttach->canAttach());
  147. }
  148. function testgetForbiddenWords(){
  149. $forum = $this->objFromFixture("Forum", "general");
  150. $f_controller = new Forum_Controller($forum);
  151. $this->assertEquals($f_controller->getForbiddenWords(), "shit,fuck");
  152. }
  153. function testfilterLanguage(){
  154. $forum = $this->objFromFixture("Forum", "general");
  155. $f_controller = new Forum_Controller($forum);
  156. $this->assertEquals($f_controller->filterLanguage('shit'), "*");
  157. $this->assertEquals($f_controller->filterLanguage('shit and fuck'), "* and *");
  158. $this->assertEquals($f_controller->filterLanguage('hello'), "hello");
  159. }
  160. function testGetStickyTopics() {
  161. $forumWithSticky = $this->objFromFixture("Forum", "general");
  162. $stickies = $forumWithSticky->getStickyTopics();
  163. $this->assertEquals($stickies->Count(), '2');
  164. $this->assertEquals($stickies->First()->Title, 'Global Sticky Thread');
  165. $stickies = $forumWithSticky->getStickyTopics($include_global = false);
  166. $this->assertEquals($stickies->Count(), '1');
  167. $this->assertEquals($stickies->First()->Title, 'Sticky Thread');
  168. $forumWithGlobalOnly = $this->objFromFixture("Forum", "forum1cat2");
  169. $stickies = $forumWithGlobalOnly->getStickyTopics();
  170. $this->assertEquals($stickies->Count(), '1');
  171. $this->assertEquals($stickies->First()->Title, 'Global Sticky Thread');
  172. $stickies = $forumWithGlobalOnly->getStickyTopics($include_global = false);
  173. $this->assertEquals($stickies->Count(), '0');
  174. }
  175. function testTopics() {
  176. $forumWithPosts = $this->objFromFixture("Forum", "general");
  177. $this->assertEquals($forumWithPosts->getTopics()->Count(), '4');
  178. $forumWithoutPosts = $this->objFromFixture("Forum", "forum1cat2");
  179. $this->assertNull($forumWithoutPosts->getTopics());
  180. }
  181. function testGetLatestPost() {
  182. $forumWithPosts = $this->objFromFixture("Forum", "general");
  183. $this->assertEquals($forumWithPosts->getLatestPost()->Content, 'This is the last post to a long thread');
  184. $forumWithoutPosts = $this->objFromFixture("Forum", "forum1cat2");
  185. $this->assertFalse($forumWithoutPosts->getLatestPost());
  186. }
  187. function testGetNumTopics() {
  188. $forumWithPosts = $this->objFromFixture("Forum", "general");
  189. $this->assertEquals($forumWithPosts->getNumTopics(), 6);
  190. $forumWithoutPosts = $this->objFromFixture("Forum", "forum1cat2");
  191. $this->assertEquals($forumWithoutPosts->getNumTopics(), 0);
  192. }
  193. function testGetTotalAuthors() {
  194. $forumWithPosts = $this->objFromFixture("Forum", "general");
  195. $this->assertEquals($forumWithPosts->getNumAuthors(), 4);
  196. $forumWithoutPosts = $this->objFromFixture("Forum", "forum1cat2");
  197. $this->assertEquals($forumWithoutPosts->getNumAuthors(), 0);
  198. }
  199. /**
  200. * Note: See {@link testCanModerate()} for detailed permission tests.
  201. */
  202. function testMarkAsSpamLink() {
  203. $spampost = $this->objFromFixture('Post', 'SpamSecondPost');
  204. $forum = $spampost->Forum();
  205. $author = $spampost->Author();
  206. $moderator = $this->objFromFixture('Member', 'moderator'); // moderator for "general" forum
  207. // without a logged-in moderator
  208. $this->assertNull($spampost->MarkAsSpamLink(), 'Link not present by default');
  209. $c = new Forum_Controller($forum);
  210. $response = $c->handleRequest(new SS_HTTPRequest('GET', 'markasspam/'. $spampost->ID));
  211. $this->assertEquals(403, $response->getStatusCode());
  212. // with logged-in moderator
  213. $moderator->logIn();
  214. $this->assertNotNull($spampost->MarkAsSpamLink(), 'Link present for moderators on this forum');
  215. $this->assertNull($author->SuspendedUntil);
  216. $link = $forum->AbsoluteLink("markasspam") .'/'. $spampost->ID;
  217. $c = new Forum_Controller($forum);
  218. $response = $c->handleRequest(new SS_HTTPRequest('GET', 'markasspam/'. $spampost->ID));
  219. $this->assertFalse($response->isError());
  220. // removes the post
  221. $this->assertFalse(DataObject::get_by_id('Post', $spampost->ID));
  222. // suspends the member
  223. $author = DataObject::get_by_id('Member', $author->ID);
  224. $this->assertNotNull($author->SuspendedUntil);
  225. // does not effect the thread
  226. $thread = DataObject::get_by_id('ForumThread', $spampost->Thread()->ID);
  227. $this->assertEquals('1', $thread->getNumPosts());
  228. // mark the first post in that now as spam
  229. $spamfirst = $this->objFromFixture('Post', 'SpamFirstPost');
  230. $response = $c->handleRequest(new SS_HTTPRequest('GET', 'markasspam/'. $spamfirst->ID));
  231. // removes the thread
  232. $this->assertFalse(DataObject::get_by_id('ForumThread', $spamfirst->Thread()->ID));
  233. }
  234. function testNotifyModerators() {
  235. Form::disable_all_security_tokens();
  236. $notifyModerators = Forum::$notify_moderators;
  237. Forum::$notify_moderators = true;
  238. $forum = $this->objFromFixture('Forum', 'general');
  239. $controller = new Forum_Controller($forum);
  240. $user = $this->objFromFixture('Member', 'test1');
  241. $this->session()->inst_set('loggedInAs', $user->ID);
  242. // New thread
  243. $this->post(
  244. $forum->RelativeLink('PostMessageForm'),
  245. array(
  246. 'Title' => 'New thread',
  247. 'Content' => 'Meticulously crafted content',
  248. 'action_doPostMessageForm' => 1
  249. )
  250. );
  251. $this->assertEmailSent('test3@example.com', Email::getAdminEmail(), "New thread \"New thread\" in forum [General Discussion]");
  252. $this->clearEmails();
  253. // New response
  254. $thread = DataObject::get_one('ForumThread', "\"ForumThread\".\"Title\"='New thread'");
  255. $this->post(
  256. $forum->RelativeLink('PostMessageForm'),
  257. array(
  258. 'Title' => 'Re: New thread',
  259. 'Content' => 'Rough response',
  260. 'ThreadID' => $thread->ID,
  261. 'action_doPostMessageForm' => 1
  262. )
  263. );
  264. $this->assertEmailSent('test3@example.com', Email::getAdminEmail(), "New post \"Re: New thread\" in forum [General Discussion]");
  265. $this->clearEmails();
  266. // Edit
  267. $post = $thread->Posts()->Last();
  268. $this->post(
  269. $forum->RelativeLink('PostMessageForm'),
  270. array(
  271. 'Title' => 'Re: New thread',
  272. 'Content' => 'Pleasant response',
  273. 'ThreadID' => $thread->ID,
  274. 'ID' => $post->ID,
  275. 'action_doPostMessageForm' => 1
  276. )
  277. );
  278. $this->assertEmailSent('test3@example.com', Email::getAdminEmail(), "New post \"Re: New thread\" in forum [General Discussion]");
  279. $this->clearEmails();
  280. Forum::$notify_moderators = $notifyModerators;
  281. }
  282. }