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());
 }
$siteRepo = new \repositories\SiteRepository();
$site = $siteRepo->loadById($CONFIG->singleSiteID);
$customFields = $site->getCachedEventCustomFieldDefinitionsAsModels();
$customFieldOrganiserEmail = $customFields[0];
$userRepo = new \repositories\UserAccountRepository();
$organiserEmailRepo = new \com\meetyournextmp\repositories\OrganiserEmailRepository();
configureAppForSite($site);
$nowPlusSomeEmailReadingTime = TimeSource::getDateTime();
$nowPlusSomeEmailReadingTime->add(new \DateInterval("P1D"));
// Events
$erb = new \repositories\builders\EventRepositoryBuilder();
$erb->setSite($site);
$erb->setAfterNow();
$erb->setIncludeCancelled(false);
$erb->setIncludeDeleted(false);
foreach ($erb->fetchAll() as $event) {
    print $event->getSlug() . " " . $event->getSummary() . "\n";
    $email = \com\meetyournextmp\models\OrganiserEmailModel::getEmailFrom($event->getCustomField($customFieldOrganiserEmail));
    if ($email) {
        print "  -  email: " . $email . "\n";
        if ($event->getStartAtInUTC()->getTimestamp() < $nowPlusSomeEmailReadingTime->getTimestamp()) {
            print "  - event to soon, not sending\n";
        } else {
            print "  - event not to soon\n";
            $organiserEmailFoundInHistory = false;
            $hrb = new \repositories\builders\HistoryRepositoryBuilder();
            $hrb->setEvent($event);
            foreach ($hrb->fetchAll() as $history) {
                if ($history->getUserAccountId()) {
                    $user = $userRepo->loadByID($history->getUserAccountId());
                    if ($user && $user->getEmail() == $email) {
 function test1()
 {
     TimeSource::mock(2014, 5, 1, 7, 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());
     $curatedList = new CuratedListModel();
     $curatedList->setTitle("test");
     $curatedList->setDescription("test this!");
     $clRepo = new CuratedListRepository();
     $clRepo->create($curatedList, $site, $user);
     $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, $user);
     // Test Before
     $eventRepositoryBuilder = new \repositories\builders\EventRepositoryBuilder();
     $eventRepositoryBuilder->setCuratedList($curatedList);
     $this->assertEquals(0, count($eventRepositoryBuilder->fetchAll()));
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setEventInformation($event);
     $curatedListsWithInfo = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(1, count($curatedListsWithInfo));
     $curatedListWithInfo = $curatedListsWithInfo[0];
     $this->assertEquals(false, $curatedListWithInfo->getIsEventInlist());
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setContainsEvent($event);
     $curatedListsContainsEvent = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(0, count($curatedListsContainsEvent));
     // Add event to list
     TimeSource::mock(2014, 5, 1, 8, 0, 0);
     $clRepo->addEventtoCuratedList($event, $curatedList, $user);
     // Test After
     // ... we don't ask for extra info
     $eventRepositoryBuilder = new \repositories\builders\EventRepositoryBuilder();
     $eventRepositoryBuilder->setCuratedList($curatedList);
     $events = $eventRepositoryBuilder->fetchAll();
     $this->assertEquals(1, count($events));
     $eventWithInfo = $events[0];
     $this->assertNull($eventWithInfo->getInCuratedListGroupId());
     $this->assertNull($eventWithInfo->getInCuratedListGroupSlug());
     $this->assertNull($eventWithInfo->getInCuratedListGroupTitle());
     $this->assertFalse($eventWithInfo->getIsEventInCuratedList());
     // ... we DO ask for extra info
     $eventRepositoryBuilder = new \repositories\builders\EventRepositoryBuilder();
     $eventRepositoryBuilder->setCuratedList($curatedList, true);
     $events = $eventRepositoryBuilder->fetchAll();
     $this->assertEquals(1, count($events));
     $eventWithInfo = $events[0];
     $this->assertNull($eventWithInfo->getInCuratedListGroupId());
     $this->assertNull($eventWithInfo->getInCuratedListGroupSlug());
     $this->assertNull($eventWithInfo->getInCuratedListGroupTitle());
     $this->assertTrue($eventWithInfo->getIsEventInCuratedList());
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setEventInformation($event);
     $curatedListsWithInfo = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(1, count($curatedListsWithInfo));
     $curatedListWithInfo = $curatedListsWithInfo[0];
     $this->assertEquals(true, $curatedListWithInfo->getIsEventInlist());
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setContainsEvent($event);
     $curatedListsContainsEvent = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(1, count($curatedListsContainsEvent));
     // Remove event to list
     TimeSource::mock(2014, 5, 1, 9, 0, 0);
     $clRepo->removeEventFromCuratedList($event, $curatedList, $user);
     // Test After
     $eventRepositoryBuilder = new \repositories\builders\EventRepositoryBuilder();
     $eventRepositoryBuilder->setCuratedList($curatedList);
     $this->assertEquals(0, count($eventRepositoryBuilder->fetchAll()));
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setEventInformation($event);
     $curatedListsWithInfo = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(1, count($curatedListsWithInfo));
     $curatedListWithInfo = $curatedListsWithInfo[0];
     $this->assertEquals(false, $curatedListWithInfo->getIsEventInlist());
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setContainsEvent($event);
     $curatedListsContainsEvent = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(0, count($curatedListsContainsEvent));
 }
 function testEventInTwoGroupsAddedDirectlyThenOneGroupAdded()
 {
     TimeSource::mock(2014, 5, 1, 7, 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());
     $curatedList = new CuratedListModel();
     $curatedList->setTitle("test");
     $curatedList->setDescription("test this!");
     $clRepo = new CuratedListRepository();
     $clRepo->create($curatedList, $site, $user);
     $group1 = new GroupModel();
     $group1->setTitle("test");
     $group1->setDescription("test test");
     $group1->setUrl("http://www.group.com");
     $group2 = new GroupModel();
     $group2->setTitle("I don't need no stinking tests");
     $group2->setDescription("works first time");
     $group2->setUrl("http://www.soveryperfect.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group1, $site, $user);
     $groupRepo->create($group2, $site, $user);
     $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, $user, $group1, array($group2));
     // Test Before
     $eventRepositoryBuilder = new \repositories\builders\EventRepositoryBuilder();
     $eventRepositoryBuilder->setCuratedList($curatedList);
     $this->assertEquals(0, count($eventRepositoryBuilder->fetchAll()));
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setGroupInformation($group1);
     $curatedListsWithInfo = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(1, count($curatedListsWithInfo));
     $curatedListWithInfo = $curatedListsWithInfo[0];
     $this->assertEquals(false, $curatedListWithInfo->getIsGroupInlist());
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setEventInformation($event);
     $curatedListsWithInfo = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(1, count($curatedListsWithInfo));
     $curatedListWithInfo = $curatedListsWithInfo[0];
     $this->assertEquals(false, $curatedListWithInfo->isEventInListViaGroup());
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setContainsEvent($event);
     $curatedListsContainsEvent = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(0, count($curatedListsContainsEvent));
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setContainsGroup($group1);
     $curatedListsContainsGroup = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(0, count($curatedListsContainsGroup));
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setContainsGroup($group2);
     $curatedListsContainsGroup = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(0, count($curatedListsContainsGroup));
     // Add group to list
     TimeSource::mock(2014, 5, 1, 8, 0, 0);
     $clRepo->addEventtoCuratedList($event, $curatedList, $user);
     $clRepo->addGroupToCuratedList($group1, $curatedList, $user);
     // Test After
     // .... we don't ask for extra info
     $eventRepositoryBuilder = new \repositories\builders\EventRepositoryBuilder();
     $eventRepositoryBuilder->setCuratedList($curatedList);
     $events = $eventRepositoryBuilder->fetchAll();
     $this->assertEquals(1, count($events));
     $eventWithInfo = $events[0];
     $this->assertNull($eventWithInfo->getInCuratedListGroupId());
     $this->assertNull($eventWithInfo->getInCuratedListGroupSlug());
     $this->assertNull($eventWithInfo->getInCuratedListGroupTitle());
     $this->assertFalse($eventWithInfo->getIsEventInCuratedList());
     // .... we Do ask for extra info
     $eventRepositoryBuilder = new \repositories\builders\EventRepositoryBuilder();
     $eventRepositoryBuilder->setCuratedList($curatedList, true);
     $events = $eventRepositoryBuilder->fetchAll();
     $this->assertEquals(1, count($events));
     $eventWithInfo = $events[0];
     $this->assertEquals($group1->getId(), $eventWithInfo->getInCuratedListGroupId());
     $this->assertEquals($group1->getSlug(), $eventWithInfo->getInCuratedListGroupSlug());
     $this->assertEquals($group1->getTitle(), $eventWithInfo->getInCuratedListGroupTitle());
     $this->assertTrue($eventWithInfo->getIsEventInCuratedList());
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setGroupInformation($group1);
     $curatedListsWithInfo = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(1, count($curatedListsWithInfo));
     $curatedListWithInfo = $curatedListsWithInfo[0];
     $this->assertEquals(true, $curatedListWithInfo->getIsGroupInlist());
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setEventInformation($event);
     $curatedListsWithInfo = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(1, count($curatedListsWithInfo));
     $curatedListWithInfo = $curatedListsWithInfo[0];
     $this->assertEquals(true, $curatedListWithInfo->isEventInListViaGroup());
     $this->assertEquals($group1->getId(), $curatedListWithInfo->getEventInListViaGroupId());
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setContainsEvent($event);
     $curatedListsContainsEvent = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(1, count($curatedListsContainsEvent));
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setContainsGroup($group1);
     $curatedListsContainsGroup = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(1, count($curatedListsContainsGroup));
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setContainsGroup($group2);
     $curatedListsContainsGroup = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(0, count($curatedListsContainsGroup));
     // Remove group from list
     TimeSource::mock(2014, 5, 1, 9, 0, 0);
     $clRepo->removeGroupFromCuratedList($group1, $curatedList, $user);
     $clRepo->removeEventFromCuratedList($event, $curatedList, $user);
     // Test After
     $eventRepositoryBuilder = new \repositories\builders\EventRepositoryBuilder();
     $eventRepositoryBuilder->setCuratedList($curatedList);
     $this->assertEquals(0, count($eventRepositoryBuilder->fetchAll()));
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setGroupInformation($group1);
     $curatedListsWithInfo = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(1, count($curatedListsWithInfo));
     $curatedListWithInfo = $curatedListsWithInfo[0];
     $this->assertEquals(false, $curatedListWithInfo->getIsGroupInlist());
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setEventInformation($event);
     $curatedListsWithInfo = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(1, count($curatedListsWithInfo));
     $curatedListWithInfo = $curatedListsWithInfo[0];
     $this->assertEquals(false, $curatedListWithInfo->isEventInListViaGroup());
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setContainsEvent($event);
     $curatedListsContainsEvent = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(0, count($curatedListsContainsEvent));
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setContainsGroup($group1);
     $curatedListsContainsGroup = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(0, count($curatedListsContainsGroup));
     $curatedListRepoBuilder = new CuratedListRepositoryBuilder();
     $curatedListRepoBuilder->setContainsGroup($group2);
     $curatedListsContainsGroup = $curatedListRepoBuilder->fetchAll();
     $this->assertEquals(0, count($curatedListsContainsGroup));
 }
 function testEventInNoAreaHumanInCentre()
 {
     $this->addCountriesToTestDB();
     TimeSource::mock(2014, 5, 1, 7, 0, 0);
     $user = new UserAccountModel();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("password");
     $userWatching = new UserAccountModel();
     $userWatching->setEmail("*****@*****.**");
     $userWatching->setUsername("test1");
     $userWatching->setPassword("password");
     $userRepo = new UserAccountRepository();
     $userRepo->create($user);
     $userRepo->create($userWatching);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo = new SiteRepository();
     $siteRepo->create($site, $user, array(), $this->getSiteQuotaUsedForTesting());
     $countryRepo = new \repositories\CountryRepository();
     $gb = $countryRepo->loadByTwoCharCode("GB");
     $areaParent = new \models\AreaModel();
     $areaParent->setTitle("Parent");
     $areaChild = new \models\AreaModel();
     $areaChild->setTitle("Child");
     $areaOtherChild = new \models\AreaModel();
     $areaOtherChild->setTitle("Other Child");
     $areaRepo = new \repositories\AreaRepository();
     $areaRepo->create($areaParent, null, $site, $gb);
     $areaRepo->create($areaChild, $areaParent, $site, $gb);
     $areaRepo->create($areaOtherChild, $areaParent, $site, $gb);
     $areaRepo->buildCacheAreaHasParent($areaParent);
     $areaRepo->buildCacheAreaHasParent($areaChild);
     $areaRepo->buildCacheAreaHasParent($areaOtherChild);
     $event = new EventModel();
     $event->setSummary("test");
     $event->setDescription("test test");
     $event->setStartAt($this->mktime(2014, 5, 10, 19, 0, 0, 'Europe/London'));
     $event->setEndAt($this->mktime(2014, 5, 10, 21, 0, 0, 'Europe/London'));
     $event->setUrl("http://www.info.com");
     $event->setTicketUrl("http://www.tickets.com");
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user);
     $human = new \com\meetyournextmp\models\HumanModel();
     $human->setTitle("Bob");
     $humanRepo = new \com\meetyournextmp\repositories\HumanRepository();
     $humanRepo->create($human, $site, null);
     $humanRepo->addHumanToArea($human, $areaChild, null);
     $humanRepo->addHumanToEvent($human, $event, null);
     $userWatchesAreaRepo = new \repositories\UserWatchesAreaRepository();
     //////////////////////////////// TEST ALL EVENTS
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $hrb = new \repositories\builders\HistoryRepositoryBuilder();
     $hrb->setIncludeAreaHistory(false);
     $hrb->setIncludeEventHistory(true);
     $histories = $hrb->fetchAll();
     $this->assertEquals(1, count($histories));
     //////////////////////////////// TEST OTHER CHILD
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setArea($areaOtherChild);
     $events = $erb->fetchAll();
     $this->assertEquals(0, count($events));
     $hrb = new \repositories\builders\HistoryRepositoryBuilder();
     $hrb->getHistoryRepositoryBuilderConfig()->setArea($areaOtherChild);
     $hrb->setIncludeAreaHistory(false);
     $hrb->setIncludeEventHistory(true);
     $histories = $hrb->fetchAll();
     $this->assertEquals(0, count($histories));
     //////////////////////////////// TEST CHILD
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setArea($areaChild);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $hrb = new \repositories\builders\HistoryRepositoryBuilder();
     $hrb->getHistoryRepositoryBuilderConfig()->setArea($areaChild);
     $hrb->setIncludeAreaHistory(false);
     $hrb->setIncludeEventHistory(true);
     $histories = $hrb->fetchAll();
     $this->assertEquals(1, count($histories));
     //////////////////////////////// TEST PARENT
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setArea($areaChild);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $hrb = new \repositories\builders\HistoryRepositoryBuilder();
     $hrb->getHistoryRepositoryBuilderConfig()->setArea($areaChild);
     $hrb->setIncludeAreaHistory(false);
     $hrb->setIncludeEventHistory(true);
     $histories = $hrb->fetchAll();
     $this->assertEquals(1, count($histories));
     //////////////////////////////// USER WATCHES NOTHING
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setUserAccount($userWatching, false, true, true, true);
     $events = $erb->fetchAll();
     $this->assertEquals(0, count($events));
     //////////////////////////////// USER WATCHES PARENT
     $userWatchesAreaRepo->startUserWatchingArea($userWatching, $areaParent);
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setUserAccount($userWatching, false, true, true, true);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     //////////////////////////////// USER WATCHES CHILD
     $userWatchesAreaRepo->stopUserWatchingArea($userWatching, $areaParent);
     $userWatchesAreaRepo->startUserWatchingArea($userWatching, $areaChild);
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setUserAccount($userWatching, false, true, true, true);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
 }