function test2()
 {
     $userOwner = new UserAccountModel();
     $userOwner->setEmail("*****@*****.**");
     $userOwner->setUsername("test2");
     $userOwner->setPassword("password");
     $user = new UserAccountModel();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->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!
     $userWatchesgroupRepo->startUserWatchingGroup($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: Starts watching site, automatically stops Watching group as reported by UserWatchesGroupRepositoryBuilder!
     $userWatchesSiteRepo = new UserWatchesSiteRepository();
     $userWatchesSiteRepo->startUserWatchingSite($user, $site);
     $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 watch($slug, Request $request, Application $app)
 {
     if (!$this->build($slug, $request, $app)) {
         $app->abort(404, "Group does not exist.");
     }
     if ($request->request->get('action') && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
         $repo = new UserWatchesGroupRepository();
         if ($request->request->get('action') == 'watch') {
             $repo->startUserWatchingGroup($app['currentUser'], $this->parameters['group']);
             $app['flashmessages']->addMessage("Watching!");
         } else {
             if ($request->request->get('action') == 'unwatch') {
                 $repo->stopUserWatchingGroup($app['currentUser'], $this->parameters['group']);
                 $app['flashmessages']->addMessage("No longer watching");
             }
         }
         // redirect here because if we didn't the  $this->parameters vars would be wrong (the old state)
         // this is an easy way to get round that. Also it's nice UI to go back to the group page.
         return $app->redirect('/group/' . $this->parameters['group']->getSlugForURL());
     }
     $repo = new \repositories\UserNotificationPreferenceRepository();
     $this->parameters['preferences'] = array();
     foreach ($app['extensions']->getExtensionsIncludingCore() as $extension) {
         if (!isset($this->parameters['preferences'][$extension->getId()])) {
             $this->parameters['preferences'][$extension->getId()] = array();
         }
         foreach ($extension->getUserNotificationPreferenceTypes() as $type) {
             $userPref = $repo->load($app['currentUser'], $extension->getId(), $type);
             $this->parameters['preferences'][$extension->getId()][$type] = array('email' => $userPref->getIsEmail());
         }
     }
     return $app['twig']->render('site/group/watch.html.twig', $this->parameters);
 }