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));
 }
 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);
 }