Source of file ForumTest.php
Size: 18,656 Bytes - Last Modified: 2021-12-23T10:30:31+00:00
/var/www/docs.ssmods.com/process/src/tests/ForumTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497 | <?php /** * @todo Write Tests for doPostMessageForm() */ class ForumTest extends FunctionalTest { static $fixture_file = "forum/tests/ForumTest.yml"; static $use_draft_site = true; public function testCanView() { // test viewing not logged in if ($member = Member::currentUser()) { $member->logOut(); } $public = $this->objFromFixture('Forum', 'general'); $private = $this->objFromFixture('Forum', 'loggedInOnly'); $limited = $this->objFromFixture('Forum', 'limitedToGroup'); $noposting = $this->objFromFixture('Forum', 'noPostingForum'); $inherited = $this->objFromFixture('Forum', 'inheritedForum'); $this->assertTrue($public->canView()); $this->assertFalse($private->canView()); $this->assertFalse($limited->canView()); $this->assertTrue($noposting->canView()); $this->assertFalse($inherited->canView()); // try logging in a member $member = $this->objFromFixture('Member', 'test1'); $member->logIn(); $this->assertTrue($public->canView()); $this->assertTrue($private->canView()); $this->assertFalse($limited->canView()); $this->assertTrue($noposting->canView()); $this->assertFalse($inherited->canView()); // login as a person with access to restricted forum $member = $this->objFromFixture('Member', 'test2'); $member->logIn(); $this->assertTrue($public->canView()); $this->assertTrue($private->canView()); $this->assertTrue($limited->canView()); $this->assertTrue($noposting->canView()); $this->assertFalse($inherited->canView()); // Moderator should be able to view his own forums $member = $this->objFromFixture('Member', 'moderator'); $member->logIn(); $this->assertTrue($public->canView()); $this->assertTrue($private->canView()); $this->assertTrue($limited->canView()); $this->assertTrue($noposting->canView()); $this->assertTrue($inherited->canView()); } public function testCanPost() { // test viewing not logged in if ($member = Member::currentUser()) { $member->logOut(); } $public = $this->objFromFixture('Forum', 'general'); $private = $this->objFromFixture('Forum', 'loggedInOnly'); $limited = $this->objFromFixture('Forum', 'limitedToGroup'); $noposting = $this->objFromFixture('Forum', 'noPostingForum'); $inherited = $this->objFromFixture('Forum', 'inheritedForum'); $this->assertTrue($public->canPost()); $this->assertFalse($private->canPost()); $this->assertFalse($limited->canPost()); $this->assertFalse($noposting->canPost()); $this->assertFalse($inherited->canPost()); // try logging in a member $member = $this->objFromFixture('Member', 'test1'); $member->logIn(); $this->assertTrue($public->canPost()); $this->assertTrue($private->canPost()); $this->assertFalse($limited->canPost()); $this->assertFalse($noposting->canPost()); $this->assertFalse($inherited->canPost()); // login as a person with access to restricted forum $member = $this->objFromFixture('Member', 'test2'); $member->logIn(); $this->assertTrue($public->canPost()); $this->assertTrue($private->canPost()); $this->assertTrue($limited->canPost()); $this->assertFalse($noposting->canPost()); $this->assertFalse($inherited->canPost()); // Moderator should be able to view his own forums $member = $this->objFromFixture('Member', 'moderator'); $member->logIn(); $this->assertTrue($public->canPost()); $this->assertTrue($private->canPost()); $this->assertFalse($limited->canPost()); $this->assertFalse($noposting->canPost()); $this->assertFalse($inherited->canPost()); } public function testSuspended() { $private = $this->objFromFixture('Forum', 'loggedInOnly'); $limited = $this->objFromFixture('Forum', 'limitedToGroup'); $inheritedForum_loggedInOnly = $this->objFromFixture('Forum', 'inheritedForum_loggedInOnly'); SS_Datetime::set_mock_now('2011-10-10 12:00:00'); // try logging in a member suspendedexpired $suspendedexpired = $this->objFromFixture('Member', 'suspendedexpired'); $this->assertFalse($suspendedexpired->IsSuspended()); $suspendedexpired->logIn(); $this->assertTrue($private->canPost()); $this->assertTrue($limited->canPost()); $this->assertTrue($inheritedForum_loggedInOnly->canPost()); // try logging in a member suspended $suspended = $this->objFromFixture('Member', 'suspended'); $this->assertTrue($suspended->IsSuspended()); $suspended->logIn(); $this->assertFalse($private->canPost()); $this->assertFalse($limited->canPost()); $this->assertFalse($inheritedForum_loggedInOnly->canPost()); } public function testCanModerate() { // test viewing not logged in if ($member = Member::currentUser()) { $member->logOut(); } $public = $this->objFromFixture('Forum', 'general'); $private = $this->objFromFixture('Forum', 'loggedInOnly'); $limited = $this->objFromFixture('Forum', 'limitedToGroup'); $noposting = $this->objFromFixture('Forum', 'noPostingForum'); $inherited = $this->objFromFixture('Forum', 'inheritedForum'); $this->assertFalse($public->canModerate()); $this->assertFalse($private->canModerate()); $this->assertFalse($limited->canModerate()); $this->assertFalse($noposting->canModerate()); $this->assertFalse($inherited->canModerate()); // try logging in a member $member = $this->objFromFixture('Member', 'test1'); $member->logIn(); $this->assertFalse($public->canModerate()); $this->assertFalse($private->canModerate()); $this->assertFalse($limited->canModerate()); $this->assertFalse($noposting->canModerate()); $this->assertFalse($inherited->canModerate()); // login as a person with access to restricted forum $member = $this->objFromFixture('Member', 'test2'); $member->logIn(); $this->assertFalse($public->canModerate()); $this->assertFalse($private->canModerate()); $this->assertFalse($limited->canModerate()); $this->assertFalse($noposting->canModerate()); $this->assertFalse($inherited->canModerate()); // Moderator should be able to view his own forums $member = $this->objFromFixture('Member', 'moderator'); $member->logIn(); $this->assertTrue($public->canModerate()); $this->assertTrue($private->canModerate()); $this->assertTrue($limited->canModerate()); $this->assertTrue($noposting->canModerate()); $this->assertTrue($inherited->canModerate()); } public function testCanAttach() { $canAttach = $this->objFromFixture('Forum', 'general'); $this->assertTrue($canAttach->canAttach()); $noAttach = $this->objFromFixture('Forum', 'forum1cat2'); $this->assertFalse($noAttach->canAttach()); } public function testgetForbiddenWords() { $forum = $this->objFromFixture("Forum", "general"); $f_controller = new Forum_Controller($forum); $this->assertEquals($f_controller->getForbiddenWords(), "shit,fuck"); } public function testfilterLanguage() { $forum = $this->objFromFixture("Forum", "general"); $f_controller = new Forum_Controller($forum); $this->assertEquals($f_controller->filterLanguage('shit'), "*"); $this->assertEquals($f_controller->filterLanguage('shit and fuck'), "* and *"); $this->assertEquals($f_controller->filterLanguage('hello'), "hello"); } public function testGetStickyTopics() { $forumWithSticky = $this->objFromFixture("Forum", "general"); $stickies = $forumWithSticky->getStickyTopics(); $this->assertEquals($stickies->Count(), '2'); // TODO: Sorts by Created, which is all equal on all Posts in test, and can't be overridden, so can't rely on order //$this->assertEquals($stickies->First()->Title, 'Global Sticky Thread'); $stickies = $forumWithSticky->getStickyTopics($include_global = false); $this->assertEquals($stickies->Count(), '1'); $this->assertEquals($stickies->First()->Title, 'Sticky Thread'); $forumWithGlobalOnly = $this->objFromFixture("Forum", "forum1cat2"); $stickies = $forumWithGlobalOnly->getStickyTopics(); $this->assertEquals($stickies->Count(), '1'); $this->assertEquals($stickies->First()->Title, 'Global Sticky Thread'); $stickies = $forumWithGlobalOnly->getStickyTopics($include_global = false); $this->assertEquals($stickies->Count(), '0'); } public function testTopics() { $forumWithPosts = $this->objFromFixture("Forum", "general"); $this->assertEquals($forumWithPosts->getTopics()->Count(), '4'); $forumWithoutPosts = $this->objFromFixture("Forum", "forum1cat2"); $this->assertNull($forumWithoutPosts->getTopics()); } public function testGetLatestPost() { $forumWithPosts = $this->objFromFixture("Forum", "general"); $this->assertEquals($forumWithPosts->getLatestPost()->Content, 'This is the last post to a long thread'); $forumWithoutPosts = $this->objFromFixture("Forum", "forum1cat2"); $this->assertNull($forumWithoutPosts->getLatestPost()); } public function testGetNumPosts() { $forumWithPosts = $this->objFromFixture("Forum", "general"); $this->assertEquals(24, $forumWithPosts->getNumPosts()); //Mark spammer accounts and retest the posts count $this->markGhosts(); $this->assertEquals(22, $forumWithPosts->getNumPosts()); } public function testGetNumTopics() { $forumWithPosts = $this->objFromFixture("Forum", "general"); $this->assertEquals(6, $forumWithPosts->getNumTopics()); $forumWithoutPosts = $this->objFromFixture("Forum", "forum1cat2"); $this->assertEquals(0, $forumWithoutPosts->getNumTopics()); //Mark spammer accounts and retest the threads count $this->markGhosts(); $this->assertEquals(5, $forumWithPosts->getNumTopics()); } public function testGetTotalAuthors() { $forumWithPosts = $this->objFromFixture("Forum", "general"); $this->assertEquals(4, $forumWithPosts->getNumAuthors()); $forumWithoutPosts = $this->objFromFixture("Forum", "forum1cat2"); $this->assertEquals(0, $forumWithoutPosts->getNumAuthors()); //Mark spammer accounts and retest the authors count $this->markGhosts(); $this->assertEquals(2, $forumWithPosts->getNumAuthors()); } protected function markGhosts() { //Mark a members as a spammers $spammer = $this->objFromFixture("Member", "spammer"); $spammer->ForumStatus = 'Ghost'; $spammer->write(); $spammer2 = $this->objFromFixture("Member", "spammer2"); $spammer2->ForumStatus = 'Ghost'; $spammer2->write(); } /** * Note: See {@link testCanModerate()} for detailed permission tests. */ public function testMarkAsSpamLink() { $spampost = $this->objFromFixture('Post', 'SpamSecondPost'); $forum = $spampost->Forum(); $author = $spampost->Author(); $moderator = $this->objFromFixture('Member', 'moderator'); // moderator for "general" forum // without a logged-in moderator $this->assertFalse($spampost->MarkAsSpamLink(), 'Link not present by default'); $c = new Forum_Controller($forum); $response = $c->handleRequest(new SS_HTTPRequest('GET', 'markasspam/'. $spampost->ID), DataModel::inst()); $this->assertEquals(403, $response->getStatusCode()); // with logged-in moderator $moderator->logIn(); $this->assertNotEquals(false, $spampost->MarkAsSpamLink(), 'Link present for moderators on this forum'); $this->assertNull($author->SuspendedUntil); $c = new Forum_Controller($forum); $response = $c->handleRequest(new SS_HTTPRequest('GET', 'markasspam/'. $spampost->ID), DataModel::inst()); $this->assertFalse($response->isError()); // removes the post $this->assertNull(Post::get()->byID($spampost->ID)); // suspends the member $author = Member::get()->byID($author->ID); $this->assertNotNull($author->SuspendedUntil); // does not effect the thread $thread = ForumThread::get()->byID($spampost->Thread()->ID); $this->assertEquals('1', $thread->getNumPosts()); // mark the first post in that now as spam $spamfirst = $this->objFromFixture('Post', 'SpamFirstPost'); $response = $c->handleRequest(new SS_HTTPRequest('GET', 'markasspam/'. $spamfirst->ID), DataModel::inst()); // removes the thread $this->assertNull(ForumThread::get()->byID($spamfirst->Thread()->ID)); } public function testBanLink() { $spampost = $this->objFromFixture('Post', 'SpamSecondPost'); $forum = $spampost->Forum(); $author = $spampost->Author(); $moderator = $this->objFromFixture('Member', 'moderator'); // moderator for "general" forum // without a logged-in moderator $this->assertFalse($spampost->BanLink(), 'Link not present by default'); $c = new Forum_Controller($forum); $response = $c->handleRequest(new SS_HTTPRequest('GET', 'ban/'. $spampost->AuthorID), DataModel::inst()); $this->assertEquals(403, $response->getStatusCode()); // with logged-in moderator $moderator->logIn(); $this->assertNotEquals(false, $spampost->BanLink(), 'Link present for moderators on this forum'); $c = new Forum_Controller($forum); $response = $c->handleRequest(new SS_HTTPRequest('GET', 'ban/'. $spampost->AuthorID), DataModel::inst()); $this->assertFalse($response->isError()); // user is banned $author = Member::get()->byId($author->ID); $this->assertTrue($author->IsBanned()); } public function testGhostLink() { $spampost = $this->objFromFixture('Post', 'SpamSecondPost'); $forum = $spampost->Forum(); $author = $spampost->Author(); $moderator = $this->objFromFixture('Member', 'moderator'); // moderator for "general" forum // without a logged-in moderator $this->assertFalse($spampost->GhostLink(), 'Link not present by default'); $c = new Forum_Controller($forum); $response = $c->handleRequest(new SS_HTTPRequest('GET', 'ghost/'. $spampost->AuthorID), DataModel::inst()); $this->assertEquals(403, $response->getStatusCode()); // with logged-in moderator $moderator->logIn(); $this->assertNotEquals(false, $spampost->GhostLink(), 'Link present for moderators on this forum'); $c = new Forum_Controller($forum); $response = $c->handleRequest(new SS_HTTPRequest('GET', 'ghost/'. $spampost->AuthorID), DataModel::inst()); $this->assertFalse($response->isError()); // post isn't available anymore in normal queries. {@link ForumSpamPostExtension} $post = Post::get()->byId($spampost->ID); $this->assertNull($post); // user is banned $author = Member::get()->byId($author->ID); $this->assertTrue($author->IsGhost()); } public function testNotifyModerators() { SecurityToken::disable(); $notifyModerators = Forum::$notify_moderators; Forum::$notify_moderators = true; $forum = $this->objFromFixture('Forum', 'general'); $controller = new Forum_Controller($forum); $user = $this->objFromFixture('Member', 'test1'); $this->session()->inst_set('loggedInAs', $user->ID); // New thread $this->post( $forum->RelativeLink('PostMessageForm'), array( 'Title' => 'New thread', 'Content' => 'Meticulously crafted content', 'action_doPostMessageForm' => 1 ) ); $adminEmail = Config::inst()->get('Email', 'admin_email'); $this->assertEmailSent('test3@example.com', $adminEmail, "New thread \"New thread\" in forum [General Discussion]"); $this->clearEmails(); // New response $thread = DataObject::get_one('ForumThread', "\"ForumThread\".\"Title\"='New thread'"); $this->post( $forum->RelativeLink('PostMessageForm'), array( 'Title' => 'Re: New thread', 'Content' => 'Rough response', 'ThreadID' => $thread->ID, 'action_doPostMessageForm' => 1 ) ); $this->assertEmailSent('test3@example.com', $adminEmail, "New post \"Re: New thread\" in forum [General Discussion]"); $this->clearEmails(); // Edit $post = $thread->Posts()->Last(); $this->post( $forum->RelativeLink('PostMessageForm'), array( 'Title' => 'Re: New thread', 'Content' => 'Pleasant response', 'ThreadID' => $thread->ID, 'ID' => $post->ID, 'action_doPostMessageForm' => 1 ) ); $this->assertEmailSent('test3@example.com', $adminEmail, "New post \"Re: New thread\" in forum [General Discussion]"); $this->clearEmails(); Forum::$notify_moderators = $notifyModerators; } /** * Confirm that when a post is deleted, Member with corresponding ID still exists * * @throws ValidationException * @throws null */ public function testPostDeletionMemberIntegrity() { $checkID = 100012; $post = new Post(); $post->ID = $checkID; $post->write(); $user = new Member(); $user->ID = $checkID; $user->FirstName = 'TestUser100012'; $user->write(); $post->delete(); $member = DataObject::get_by_id('Member', $checkID); $this->assertTrue($member->ID == $checkID); } } |