function watch(Request $request, Application $app)
 {
     if ($request->request->get('action') && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
         $repo = new UserWatchesSiteRepository();
         if ($request->request->get('action') == 'watch') {
             $repo->startUserWatchingSite($app['currentUser'], $app['currentSite']);
         } else {
             if ($request->request->get('action') == 'unwatch') {
                 $repo->stopUserWatchingSite($app['currentUser'], $app['currentSite']);
             }
         }
         // 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.
         return $app->redirect('/watch');
     }
     $repo = new \repositories\UserNotificationPreferenceRepository();
     $preferences = array();
     foreach ($app['extensions']->getExtensionsIncludingCore() as $extension) {
         if (!isset($preferences[$extension->getId()])) {
             $preferences[$extension->getId()] = array();
         }
         foreach ($extension->getUserNotificationPreferenceTypes() as $type) {
             $userPref = $repo->load($app['currentUser'], $extension->getId(), $type);
             $preferences[$extension->getId()][$type] = array('email' => $userPref->getIsEmail());
         }
     }
     return $app['twig']->render('site/index/watch.html.twig', array('preferences' => $preferences));
 }
 function test1()
 {
     $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());
     $userWatchesSiteRepo = new UserWatchesSiteRepository();
     # Part 1: User does not watch site
     $t = $userWatchesSiteRepo->loadByUserAndSite($user, $site);
     $this->assertNull($t);
     $b = new UserWatchesSiteRepositoryBuilder();
     $t = $b->fetchAll();
     $this->assertEquals(1, count($t));
     # Part 2: Watches!
     $userWatchesSiteRepo->startUserWatchingSite($user, $site);
     $t = $userWatchesSiteRepo->loadByUserAndSite($user, $site);
     $this->assertEquals($user->getId(), $t->getUserAccountId());
     $this->assertEquals(true, $t->getIsWatching());
     $this->assertEquals(true, $t->getIsWasOnceWatching());
     $b = new UserWatchesSiteRepositoryBuilder();
     $t = $b->fetchAll();
     $this->assertEquals(2, count($t));
     # Part 3: Stops Watching!
     $userWatchesSiteRepo->stopUserWatchingSite($user, $site);
     $t = $userWatchesSiteRepo->loadByUserAndSite($user, $site);
     $this->assertEquals(false, $t->getIsWatching());
     $this->assertEquals(true, $t->getIsWasOnceWatching());
     $b = new UserWatchesSiteRepositoryBuilder();
     $t = $b->fetchAll();
     $this->assertEquals(1, count($t));
 }
 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));
 }