function test1()
 {
     \TimeSource::mock(2014, 1, 1, 0, 0, 0);
     $user1 = new UserAccountModel();
     $user1->setEmail("*****@*****.**");
     $user1->setUsername("test");
     $user1->setPassword("password");
     $user2 = new UserAccountModel();
     $user2->setEmail("*****@*****.**");
     $user2->setUsername("test2");
     $user2->setPassword("password");
     $userRepo = new UserAccountRepository();
     $userRepo->create($user1);
     $userRepo->create($user2);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo = new SiteRepository();
     $siteRepo->create($site, $user1, array(), $this->getSiteQuotaUsedForTesting());
     $group1 = new GroupModel();
     $group1->setTitle("test1");
     $group1->setDescription("test test");
     $group1->setUrl("http://www.group.com");
     $group2 = new GroupModel();
     $group2->setTitle("test this looks similar");
     $group2->setDescription("test test");
     $group2->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     \TimeSource::mock(2014, 1, 1, 1, 0, 0);
     $groupRepo->create($group1, $site, $user1);
     $groupRepo->create($group2, $site, $user2);
     $event = new EventModel();
     $event->setSummary("test");
     $event->setStartAt(getUTCDateTime(2014, 5, 10, 19, 0, 0));
     $event->setEndAt(getUTCDateTime(2014, 5, 10, 21, 0, 0));
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user1, $group2);
     $uwgr = new UserWatchesGroupRepository();
     // Test before
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setGroup($group1);
     $this->assertEquals(0, count($erb->fetchAll()));
     $this->assertNull($uwgr->loadByUserAndGroup($user2, $group1));
     $group2 = $groupRepo->loadById($group2->getId());
     $this->assertFalse($group2->getIsDeleted());
     $this->assertNull($group2->getIsDuplicateOfId());
     // Mark
     \TimeSource::mock(2014, 1, 1, 2, 0, 0);
     $groupRepo->markDuplicate($group2, $group1, $user1);
     // Test Duplicate
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setGroup($group1);
     $this->assertEquals(1, count($erb->fetchAll()));
     $uwg = $uwgr->loadByUserAndGroup($user2, $group1);
     $this->assertNotNull($uwg);
     $group2 = $groupRepo->loadById($group2->getId());
     $this->assertTrue($group2->getIsDeleted());
     $this->assertEquals($group1->getId(), $group2->getIsDuplicateOfId());
 }
 public function markNotificationSent(\DateTime $checkTime)
 {
     $userWatchesGroupRepository = new UserWatchesGroupRepository();
     $userWatchesGroup = $userWatchesGroupRepository->loadByUserAndGroup($this->userAccount, $this->group);
     $userWatchesGroupRepository->markNotifyEmailSent($userWatchesGroup, $checkTime);
 }
 function test3()
 {
     $user = new UserAccountModel();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("password");
     $userOwner = new UserAccountModel();
     $userOwner->setEmail("*****@*****.**");
     $userOwner->setUsername("test2");
     $userOwner->setPassword("password");
     $userRepo = new UserAccountRepository();
     $userRepo->create($user);
     $userRepo->create($userOwner);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo = new SiteRepository();
     $siteRepo->create($site, $userOwner, array(), $this->getSiteQuotaUsedForTesting());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $userOwner);
     $userWatchesgroupRepo = new UserWatchesGroupRepository();
     # Part 1: User does not watch group
     $t = $userWatchesgroupRepo->loadByUserAndGroup($user, $group);
     $this->assertNull($t);
     $b = new UserWatchesGroupRepositoryBuilder();
     $b->setGroup($group);
     $b->setUser($user);
     $t = $b->fetchAll();
     $this->assertEquals(0, count($t));
     # Part 2: Watches if not watched before!
     $userWatchesgroupRepo->startUserWatchingGroupIfNotWatchedBefore($user, $group);
     $t = $userWatchesgroupRepo->loadByUserAndGroup($user, $group);
     $this->assertEquals($user->getId(), $t->getUserAccountId());
     $this->assertEquals(true, $t->getIsWatching());
     $this->assertEquals(true, $t->getIsWasOnceWatching());
     $b = new UserWatchesGroupRepositoryBuilder();
     $b->setGroup($group);
     $b->setUser($user);
     $t = $b->fetchAll();
     $this->assertEquals(1, count($t));
     # Part 3: Stops Watching!
     $userWatchesgroupRepo->stopUserWatchingGroup($user, $group);
     $t = $userWatchesgroupRepo->loadByUserAndGroup($user, $group);
     $this->assertEquals(false, $t->getIsWatching());
     $this->assertEquals(true, $t->getIsWasOnceWatching());
     $b = new UserWatchesGroupRepositoryBuilder();
     $b->setGroup($group);
     $b->setUser($user);
     $t = $b->fetchAll();
     $this->assertEquals(0, count($t));
     # Part 4: Watches if not watched before! As they have watched before, nothing happens
     $userWatchesgroupRepo->startUserWatchingGroupIfNotWatchedBefore($user, $group);
     $t = $userWatchesgroupRepo->loadByUserAndGroup($user, $group);
     $this->assertEquals(false, $t->getIsWatching());
     $this->assertEquals(true, $t->getIsWasOnceWatching());
     $b = new UserWatchesGroupRepositoryBuilder();
     $b->setGroup($group);
     $b->setUser($user);
     $t = $b->fetchAll();
     $this->assertEquals(0, count($t));
 }
 /**
  * One event, 31 days from now, then 30 days, then 29 days, etc, only 1 email sent
  * @global type $CONFIG
  */
 function test6()
 {
     global $CONFIG;
     $CONFIG->userWatchesPromptEmailSafeGapDays = 30;
     \TimeSource::mock(2013, 1, 1, 0, 0, 0);
     $user = new UserAccountModel();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("password");
     $userRepo = new UserAccountRepository();
     $userRepo->create($user);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo = new SiteRepository();
     $siteRepo->create($site, $user, array(), $this->getSiteQuotaUsedForTesting());
     $group = new GroupModel();
     $group->setTitle("Group");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     $event = new EventModel();
     $start = \TimeSource::getDateTime();
     $start->setDate(2013, 30, 9);
     $start->setTime(9, 0, 0);
     $event->setStartAt($start);
     $end = \TimeSource::getDateTime();
     $end->setDate(2013, 30, 9);
     $end->setTime(12, 0, 0);
     $event->setEndAt($end);
     $eventRepo = new EventRepository();
     $eventRepo->create($event, $site, $user, $group);
     // User will watch site automatically in site->create()
     // We don't want that, we want the group instead
     $userWatchesSiteRepo = new UserWatchesSiteRepository();
     $userWatchesSiteRepo->stopUserWatchingSite($user, $site);
     $userWatchesGroupRepo = new UserWatchesGroupRepository();
     $userWatchesGroupRepo->startUserWatchingGroup($user, $group);
     #Before email sent!
     for ($day = 1; $day <= 29; $day++) {
         \TimeSource::mock(2013, $day, 8, 1, 0, 0);
         $userWatchesGroup = $userWatchesGroupRepo->loadByUserAndGroup($user, $group);
         $data = $userWatchesGroup->getPromptEmailData($site, $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId()));
         $this->assertFalse($data['moreEventsNeeded']);
     }
     #Email sent!
     \TimeSource::mock(2013, 30, 8, 1, 0, 0);
     $userWatchesGroup = $userWatchesGroupRepo->loadByUserAndGroup($user, $group);
     $data = $userWatchesGroup->getPromptEmailData($site, $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId()));
     $this->assertTrue($data['moreEventsNeeded']);
     $userWatchesGroupRepo->markPromptEmailSent($userWatchesGroup, \TimeSource::getDateTime());
     #After email sent
     \TimeSource::mock(2013, 31, 8, 1, 0, 0);
     $userWatchesGroup = $userWatchesGroupRepo->loadByUserAndGroup($user, $group);
     $data = $userWatchesGroup->getPromptEmailData($site, $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId()));
     $this->assertFalse($data['moreEventsNeeded']);
     for ($day = 1; $day <= 30; $day++) {
         \TimeSource::mock(2013, $day, 9, 1, 0, 0);
         $userWatchesGroup = $userWatchesGroupRepo->loadByUserAndGroup($user, $group);
         $data = $userWatchesGroup->getPromptEmailData($site, $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId()));
         $this->assertFalse($data['moreEventsNeeded']);
     }
     for ($day = 1; $day <= 31; $day++) {
         \TimeSource::mock(2013, $day, 10, 1, 0, 0);
         $userWatchesGroup = $userWatchesGroupRepo->loadByUserAndGroup($user, $group);
         $data = $userWatchesGroup->getPromptEmailData($site, $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId()));
         $this->assertFalse($data['moreEventsNeeded']);
     }
     for ($day = 1; $day <= 30; $day++) {
         \TimeSource::mock(2013, $day, 11, 1, 0, 0);
         $userWatchesGroup = $userWatchesGroupRepo->loadByUserAndGroup($user, $group);
         $data = $userWatchesGroup->getPromptEmailData($site, $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId()));
         $this->assertFalse($data['moreEventsNeeded']);
     }
     for ($day = 1; $day <= 31; $day++) {
         \TimeSource::mock(2013, $day, 12, 1, 0, 0);
         $userWatchesGroup = $userWatchesGroupRepo->loadByUserAndGroup($user, $group);
         $data = $userWatchesGroup->getPromptEmailData($site, $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId()));
         $this->assertFalse($data['moreEventsNeeded']);
     }
 }
 function stopWatchingFromEmail($slug, $userid, $code, Request $request, Application $app)
 {
     if (!$this->build($slug, $request, $app)) {
         $app->abort(404, "Group does not exist.");
     }
     $userRepo = new UserAccountRepository();
     $user = $userRepo->loadByID($userid);
     if (!$user) {
         $app['monolog']->addError("Failed stop watching group from email - no user ");
         die("NO");
         // TODO
     }
     $userWatchesGroupStopRepo = new UserWatchesGroupStopRepository();
     $userWatchesGroupStop = $userWatchesGroupStopRepo->loadByUserAccountIDAndGroupIDAndAccessKey($user->getId(), $this->parameters['group']->getId(), $code);
     if (!$userWatchesGroupStop) {
         $app['monolog']->addError("Failed stop watching group from email - user " . $user->getId() . " - code wrong");
         die("NO");
         // TODO
     }
     $userWatchesGroupRepo = new UserWatchesGroupRepository();
     $userWatchesGroup = $userWatchesGroupRepo->loadByUserAndGroup($user, $this->parameters['group']);
     if (!$userWatchesGroup || !$userWatchesGroup->getIsWatching()) {
         $app['monolog']->addError("Failed stop watching group from email - user " . $user->getId() . " - not watching");
         die("You don't watch this group");
         // TODO
     }
     if ($request->request->get('action') == 'unwatch' && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
         $userWatchesGroupRepo->stopUserWatchingGroup($user, $this->parameters['group']);
         // redirect here because if we didn't the twig global and $app vars would be wrong (the old state)
         // this is an easy way to get round that.
         $app['flashmessages']->addMessage("You have stopped watching this group.");
         return $app->redirect('/group/' . $this->parameters['group']->getSlugForURL());
     }
     $this->parameters['user'] = $user;
     return $app['twig']->render('site/group/stopWatchingFromEmail.html.twig', $this->parameters);
 }