/** * Tests whether we can grab an Episode that is due to be released within a * specified length of time from now. */ public function testGetOneEpisodeReleasedWithinSeconds() { // Create Test Subreddit $subreddit = new Subreddit(); $subreddit->setName(rand(0, 1000)); $subreddit->setDomain(rand(0, 1000)); $subreddit->save(); // Create Test Episode $episode = new Episode(); $episode->setReleaseDate(date('Y-m-d H:i:s', time() + 100)); $episode->setSubreddit($subreddit); $episode->save(); $another_episode = new Episode(); $another_episode->setReleaseDate(date('Y-m-d H:i:s', time() + 500)); $another_episode->setSubreddit($subreddit); $another_episode->save(); $seconds_within = 200; // Run table query to grab episode without ID $random_episode = EpisodeTable::getInstance()->getOneEpisodeReleasedWithinSeconds($seconds_within); $this->assertTrue($random_episode instanceof Episode); // Run table query to grab episode WITH ID $random_episode = EpisodeTable::getInstance()->getOneEpisodeReleasedWithinSeconds($seconds_within, $episode->getIncremented()); $this->assertTrue($random_episode instanceof Episode); // Run table query to grab anotherepisode WITH ID $random_episode = EpisodeTable::getInstance()->getOneEpisodeReleasedWithinSeconds($seconds_within, $another_episode->getIncremented()); $this->assertFalse($random_episode instanceof Episode); // Delete episodes $another_episode->delete(); $episode->delete(); // Delete subreddit. $subreddit->delete(); }
/** * Tests saving a sfGuardUserSubredditMembership that identifies a User as * "blocked" for a particular Subreddit. When this occurs, all existing * future EpisodeAssignments should be deleted because blocked users cannot * participate with the Subreddit. */ public function testSavingBlockedUser() { // Establish fake Subreddit $subreddit = new Subreddit(); $subreddit->setName(rand(0, 1000)); $subreddit->setDomain(rand(0, 1000)); $subreddit->save(); // Establish User $user = new sfGuardUser(); $user->setEmailAddress(rand(0, 100000)); $user->setUsername(rand(0, 10000)); $user->setIsValidated(1); $user->save(); $user_id = $user->getIncremented(); $this->assertNotEquals(0, $user_id); // Establish Episode for Subreddit $episode = new Episode(); $episode->setSubreddit($subreddit); $episode->setReleaseDate(date('Y-m-d H:i:s', time() + 34000)); $episode->save(); $author_type = AuthorTypeTable::getInstance()->findOneBy('type', 'squid'); $deadline = new Deadline(); $deadline->setSubreddit($subreddit); $deadline->setAuthorType($author_type); $deadline->setSeconds(0); $deadline->save(); $episode_assignment = new EpisodeAssignment(); $episode_assignment->setSfGuardUser($user); $episode_assignment->setEpisode($episode); $episode_assignment->setAuthorType($author_type); $episode_assignment->save(); $number_of_episodes = EpisodeAssignmentTable::getInstance()->createQuery()->select('COUNT(EpisodeAssignment.id)')->leftJoin('Episode')->where('subreddit_id = ?', $subreddit->getIncremented())->andWhere('EpisodeAssignment.sf_guard_user_id = ?', $user_id)->andWhere('Episode.release_date > NOW()')->groupBy('EpisodeAssignment.id')->count(); $this->assertEquals(1, $number_of_episodes); // Establish User Membership as Blocked $blocked = MembershipTable::getInstance()->findOneBy('type', 'blocked'); $user_membership = new sfGuardUserSubredditMembership(); $user_membership->setSubreddit($subreddit); $user_membership->setSfGuardUser($user); $user_membership->setMembership($blocked); // Save Membership $user_membership->save(); // Asert that User has zero Episodes $number_of_episodes = EpisodeAssignmentTable::getInstance()->createQuery()->select('COUNT(EpisodeAssignment.id)')->leftJoin('Episode')->where('subreddit_id = ?', $subreddit->getIncremented())->andWhere('EpisodeAssignment.sf_guard_user_id = ?', $user_id)->andWhere('Episode.release_date > NOW()')->groupBy('EpisodeAssignment.id'); $sql = $number_of_episodes->getSqlQuery(); $number_of_episodes = $number_of_episodes->count(); $this->assertTrue(0 == $number_of_episodes, $sql . "\n" . $subreddit->getIncremented() . "\n" . $user_id); // Remove User Membership $user_membership->delete(); // Delete User $user->delete(); // Delete Episode $episode->delete(); // Delete Deadline $deadline->delete(); // Delete Subreddit $subreddit->delete(); }
function unsubscribe($subreddit) { $user = User::whereUsername(Auth::user()->username)->first(); $sredit = Subreddit::whereTitle($subreddit)->first(); $user->subscriptions()->detach([$sredit->id]); return Redirect::to('/r/' . $subreddit); }
function display($subreddit, $post) { $post = Post::whererand($post)->first(); $subreddit = Subreddit::whereTitle($subreddit)->first(); $comments = $post->comments; return View::make('post.view', ['post' => $post, 'subreddit' => $subreddit, 'comments' => $comments]); }
/** * Similar to the testGetSubredditsNotNeedingEpisodeGeneration() test above, * this tests whether we can grab the inevrse: all of the Subreddits that * *don't* have future Episodes beyond one interval of the creation * interval. */ public function testGetSubredditsNeedingEpisodeGeneration() { // Create Test Subreddit $subreddit = new Subreddit(); $subreddit->setName('test2'); $subreddit->setDomain('test2'); $subreddit->setCreateNewEpisodesCronFormatted('0 0 1 * *'); $subreddit->setEpisodeScheduleCronFormatted('0 0 1 * *'); $subreddit->setCreationInterval('0'); $subreddit->save(); // Test against all Subreddits $subreddits = SubredditTable::getInstance()->getSubredditsNeedingEpisodeGeneration(); $this->assertTrue(!empty($subreddits)); $this->assertTrue($subreddits[0] instanceof Subreddit); // Test against Test Subreddit $subreddits = SubredditTable::getInstance()->getSubredditsNeedingEpisodeGeneration($subreddit->getName()); $this->assertTrue(!empty($subreddits)); $this->assertTrue($subreddits[0] instanceof Subreddit); $this->assertTrue($subreddits[0] == $subreddit); $subreddit->delete(); }
/** * Since a User can only have one membership in a Subreddit, this tests that * the first returned sfGuardUserSubredditMembership is the exact same as * the only one made. The limitation on sfGuardUserSubredditMemberships is * in place using Unique indexes in the database, so we depend upon that to * prevent multiple Subreddit Memberships. */ public function testGetFirstByUserSubredditAndMemberships() { $user = new sfGuardUser(); $user->setEmailAddress(rand(0, 1000)); $user->setUsername(rand(0, 1000)); $user->setIsValidated(1); $user->save(); $subreddit = new Subreddit(); $subreddit->setName(rand(0, 1000)); $subreddit->setDomain(rand(0, 1000)); $subreddit->save(); $membership = MembershipTable::getInstance()->findOneByType('user'); $second_membership = MembershipTable::getInstance()->findOneByType('admin'); $user_subreddit_membership = new sfGuardUserSubredditMembership(); $user_subreddit_membership->setSfGuardUserId($user->getIncremented()); $user_subreddit_membership->setSubredditId($subreddit->getIncremented()); $user_subreddit_membership->setMembership($membership); $user_subreddit_membership->save(); $second_user_subreddit_membership = new sfGuardUserSubredditMembership(); $second_user_subreddit_membership->setSfGuardUserId($user->getIncremented()); $second_user_subreddit_membership->setSubredditId($subreddit->getIncremented()); $second_user_subreddit_membership->setMembership($second_membership); $exception_thrown = false; try { $second_user_subreddit_membership->save(); } catch (Exception $exception) { unset($exception); $exception_thrown = true; } $retrieved_object = sfGuardUserSubredditMembershipTable::getInstance()->getFirstByUserSubredditAndMemberships($user->getIncremented(), $subreddit->getIncremented(), array($membership->getType())); $this->assertEquals($retrieved_object->getIncremented(), $user_subreddit_membership->getIncremented()); $user_subreddit_membership->delete(); $subreddit->delete(); $user->delete(); $this->assertTrue($exception_thrown); }
/** * We've combined the process for preventing saving with a specific call for * deletion to aid in handling errors and dealing with unsaved objects. * This tests whether the exception is thrown */ public function testdeleteWithException() { $subreddit = new Subreddit(); $subreddit->setName(rand(0, 10000)); $subreddit->setDomain(rand(0, 10000)); $subreddit->save(); $user = new sfGuardUser(); $user->setUsername(rand(0, 10000)); $user->setEmailAddress(rand(0, 10000)); $user->setIsValidated(1); $user->save(); $first = AuthorTypeTable::getInstance()->findOneByType('squid'); $episode = new Episode(); $episode->setReleaseDate(date('Y-m-d H:i:s', time() + 100000)); $episode->setSubreddit($subreddit); $episode->save(); $deadline = new Deadline(); $deadline->setSubreddit($subreddit); $deadline->setAuthorType($first); $deadline->setSeconds(0); $deadline->save(); $assignment = new EpisodeAssignment(); $assignment->setSfGuardUser($user); $assignment->setEpisode($episode); $assignment->setAuthorType($first); $assignment->save(); // Delete with exception $exception_thrown = false; $exception_code = 987654321; try { $assignment->deleteWithException("Test exception", $exception_code); } catch (sfException $exception) { $this->assertEquals($exception_code, $exception->getCode()); unset($exception); $exception_thrown = true; } $this->assertTrue($exception_thrown); // Delete the rest of the objects $episode->delete(); $user->delete(); $subreddit->delete(); }
<?php include dirname(__FILE__) . '/../../bootstrap/functional.php'; /** * This is an example class of sfTestFunctional * It may require some attention to work with the default values (line 40). */ $browser = new sfBrowser(); $test_browser = new sfTestFunctional($browser); $test_browser->setTester('json.response', 'sfTesterJsonResponse'); $test_browser->get('/subreddit')->with('request')->begin()->isParameter('module', 'subreddit')->isParameter('action', 'index')->end()->with('response')->begin()->isStatusCode(200)->end()->with('json.response')->begin()->isJson()->end(); /** * Test the creation */ $entity = new Subreddit(); $entity_array = $entity->exportTo('array'); $identifier = $entity->getTable()->getIdentifier(); /** * Please build a valid $entity_array here */ unset($entity_array[$identifier]); //$entity_array['name'] = "pony"; //$entity_array['created_at'] = date('Y-m-d H:i:s'); //$entity_array['updated_at'] = date('Y-m-d H:i:s'); $test_browser->call('/subreddit', 'post', array('content' => json_encode($entity_array)))->with('request')->begin()->isParameter('module', 'subreddit')->isParameter('action', 'create')->end()->with('response')->begin()->isStatusCode(200)->end(); /** * If the new entity has been created */ $location = $browser->getResponse()->getHttpHeader('Location'); if ($location) { // Get ?
/** * This tests whether the * EpisodeAssignmentTable::getFirstByUserAuthorTypeAndSubreddit() function * retrieves the correct EpisodeAssignment for a particular User in a * Subreddit based on the AuthorType used in the EpisodeAssignment. Users * can sign up for one Episode per AuthorType in each Subreddit, which means * future Episodes (sicne we don't want the existence of past episode to * disqualify Users from ever signing up again). */ public function testGetFirstByUserAuthorTypeAndSubreddit() { // Create two episode assignments: one for a past Episode, one for a future $subreddit = new Subreddit(); $subreddit->setName(rand(0, 10000)); $subreddit->setDomain(rand(0, 10000)); $subreddit->save(); $user = new sfGuardUser(); $user->setUsername(rand(0, 10000)); $user->setEmailAddress(rand(0, 10000)); $user->setisValidated(1); $user->save(); $first = AuthorTypeTable::getInstance()->findOneBy('type', 'squid'); $understudy = AuthorTypeTable::getInstance()->findOneBy('type', 'shark'); $this->assertTrue($first instanceof AuthorType); $this->assertNotEquals(null, $first->getIncremented()); $this->assertTrue($understudy instanceof AuthorType); $this->assertNotEquals(null, $understudy->getIncremented()); $deadline_one = new Deadline(); $deadline_one->setSeconds(100); $deadline_one->setAuthorType($first); $deadline_one->setSubreddit($subreddit); $deadline_one->save(); $deadline_two = new Deadline(); $deadline_two->setSeconds(0); $deadline_two->setAuthorType($understudy); $deadline_two->setSubreddit($subreddit); $deadline_two->save(); $episode_one = new Episode(); $episode_one->setReleaseDate(date('Y-m-d H:i:s', time() + 200000)); $episode_one->setSubreddit($subreddit); $episode_one->save(); $episode_two = new Episode(); $episode_two->setReleaseDate(date('Y-m-d H:i:s', time() + 100000)); $episode_two->setSubreddit($subreddit); $episode_two->save(); $episode_three = new Episode(); $episode_three->setReleaseDate(date('Y-m-d H:i:s', time() + 300000)); $episode_three->setSubreddit($subreddit); $episode_three->save(); $assignment_one = new EpisodeAssignment(); $assignment_one->setEpisode($episode_one); $assignment_one->setSfGuardUser($user); $assignment_one->setAuthorType($first); $assignment_one->save(); $assignment_two = new EpisodeAssignment(); $assignment_two->setEpisode($episode_two); $assignment_two->setSfGuardUser($user); $assignment_two->setAuthorType($understudy); $assignment_two->save(); /* There should only be one Episode Assignment for future episodes per * authortype per subreddit, so saving the third assignment should fail * --- this is covered more in EpisodeAssignmentTest.phop */ $exception_thrown = false; $assignment_three = new EpisodeAssignment(); $assignment_three->setEpisode($episode_three); $assignment_three->setSfGuardUser($user); $assignment_three->setAuthorType($first); try { $assignment_three->save(); } catch (Exception $exception) { $exception_thrown = true; $this->assertEquals(102, $exception->getCode()); unset($exception); } $this->assertTrue($exception_thrown); /* Since we can trust that only one assignment per authortype per user * per subreddit exists for future episodes (based on the failure to * save $assignment_three), we check now to ensure that the * getFirstByUserAuthorTypeAndSubreddit() function returns a valid * EpisodeAssignment. */ $test_one = EpisodeAssignmentTable::getInstance()->getFirstByUserAuthorTypeAndSubreddit($assignment_one->getAuthorTypeId(), $assignment_one->getSfGuardUserId(), $assignment_one->getEpisode()->getSubredditId()); $test_two = EpisodeAssignmentTable::getInstance()->getFirstByUserAuthorTypeAndSubreddit($assignment_two->getAuthorTypeId(), $assignment_two->getSfGuardUserId(), $assignment_two->getEpisode()->getSubredditId()); $this->assertEquals($test_one->getIncremented(), $assignment_one->getIncremented()); $this->assertEquals($test_two->getIncremented(), $assignment_two->getIncremented()); $assignment_one->delete(); $assignment_two->delete(); $episode_one->delete(); $episode_two->delete(); $episode_three->delete(); $deadline_one->delete(); $deadline_two->delete(); $user->delete(); $subreddit->delete(); }
/** * Fetches and returns an array of the subreddits to which the logged-in user * is subscribed * * @access public * @return array */ public function getMySubreddits() { if (!$this->isLoggedIn()) { $message = 'No user is logged in to list subreddit subscriptions'; $code = RedditException::LOGIN_REQUIRED; throw new RedditException($message, $code); } $verb = 'GET'; $url = 'http://www.reddit.com/reddits/mine.json'; $response = $this->sendRequest($verb, $url); $subreddits = array(); foreach ($response['data']['children'] as $child) { $subreddit = new Subreddit($this); $subreddit->setData($child['data']); $subreddits[] = $subreddit; } return $subreddits; }
/** * Tests whether we can generate a collection of Episodes to be saved. * These Episodes are produced using the creation and episode schedules of * the Subreddit. */ public function testCollectGeneratedEpisodes() { /* @todo: Test iteration lengths, not just creation of episodes. */ $subreddit = new Subreddit(); $subreddit->setName(rand(0, 1000)); $subreddit->setDomain(rand(0, 1000)); $subreddit->setEpisodeScheduleCronFormatted('0 0 * * *'); $subreddit->setCreateNewEpisodesCronFormatted('0 0 1 * *'); $subreddit->save(); $episodes = $subreddit->collectGeneratedEpisodes(); $this->assertTrue(!empty($episodes)); $this->assertTrue($episodes[0] instanceof Episode); $subreddit->delete(); }