function test1() { $this->addCountriesToTestDB(); \TimeSource::mock(2014, 10, 1, 1, 1, 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()); $countryRepo = new CountryRepository(); $gb = $countryRepo->loadByTwoCharCode('GB'); $area = new AreaModel(); $area->setTitle("test"); $area->setDescription("test test"); $areaRepo = new \repositories\AreaRepository(); $areaRepo->create($area, null, $site, $gb, $user); $venue = new VenueModel(); $venue->setTitle("test"); $venue->setDescription("test test"); $venue->setCountryId($gb->getId()); $venue->setAreaId($area->getId()); $venueRepo = new VenueRepository(); $venueRepo->create($venue, $site, $user); $venueDuplicate = new VenueModel(); $venueDuplicate->setTitle("test Duplicate"); $venueRepo->create($venueDuplicate, $site, $user); \TimeSource::mock(2014, 10, 1, 1, 2, 0); $venueRepo->markDuplicate($venueDuplicate, $venue, $user); $event = new EventModel(); $event->setSummary("test"); $event->setStartAt(getUTCDateTime(2014, 5, 10, 19, 0, 0, 'Europe/London')); $event->setEndAt(getUTCDateTime(2014, 5, 10, 21, 0, 0, 'Europe/London')); $event->setVenueId($venue->getId()); $eventRepository = new EventRepository(); $eventRepository->create($event, $site, $user); ## Test $this->assertNotNull($venueRepo->loadBySlug($site, $venue->getSlug())); $event = $eventRepository->loadBySlug($site, $event->getSlug()); $this->assertEquals($venue->getId(), $event->getVenueId()); ## Now Purge! $venueRepo->purge($venue); ## Test $this->assertNull($venueRepo->loadBySlug($site, $venue->getSlug())); $event = $eventRepository->loadBySlug($site, $event->getSlug()); $this->assertNull($event->getVenueId()); }
function testUserWatchesArea() { $this->addCountriesToTestDB(); $countryRepo = new CountryRepository(); $user = new UserAccountModel(); $user->setEmail("*****@*****.**"); $user->setUsername("test"); $user->setPassword("password"); $userTest = new UserAccountModel(); $userTest->setEmail("*****@*****.**"); $userTest->setUsername("testtest"); $userTest->setPassword("password"); $userRepo = new UserAccountRepository(); $userRepo->create($user); $userRepo->create($userTest); $site = new SiteModel(); $site->setTitle("Test"); $site->setSlug("test"); $siteRepo = new SiteRepository(); $siteRepo->create($site, $user, array(), $this->getSiteQuotaUsedForTesting()); $area = new \models\AreaModel(); $area->setTitle("Test"); $areaRepo = new \repositories\AreaRepository(); $areaRepo->create($area, null, $site, $countryRepo->loadByTwoCharCode('GB'), $user); // Test user doesn't have it $srb = new \repositories\builders\SiteRepositoryBuilder(); $srb->setUserInterestedIn($userTest); $sites = $srb->fetchAll(); $this->assertEquals(0, count($sites)); // watch area $userWatchesAreaRepo = new \repositories\UserWatchesAreaRepository(); $userWatchesAreaRepo->startUserWatchingArea($userTest, $area); // has it! $srb = new \repositories\builders\SiteRepositoryBuilder(); $srb->setUserInterestedIn($userTest); $sites = $srb->fetchAll(); $this->assertEquals(1, count($sites)); }
} } foreach ($parentAreas as $key => $value) { if (!$value) { die("No area for " . $key); } } // JSON $file = $argv[1]; $data = json_decode(file_get_contents($file)); if (!$data) { die; } $areaMapItInfoRepo = new \com\meetyournextmp\repositories\AreaMapItInfoRepository(); $countryRepo = new \repositories\CountryRepository(); $areaRepo = new \repositories\AreaRepository(); $gb = $countryRepo->loadByTwoCharCode("GB"); $siteRepo = new \repositories\SiteRepository(); $site = $siteRepo->loadById($CONFIG->singleSiteID); foreach ($data as $json_blob) { print $json_blob->name . "\n"; $areaMapItInfo = $areaMapItInfoRepo->getByCodeGSS($json_blob->codes->gss); if ($areaMapItInfo) { print " ... found area ID " . $areaMapItInfo->getAreaId() . "\n"; $areaMapItInfo->setName($json_blob->name); $areaMapItInfo->setCodeGss($json_blob->codes->gss); if (isset($json_blob->codes->unit_id)) { $areaMapItInfo->setCodeUnitId($json_blob->codes->unit_id); } $areaMapItInfo->setMapitId($json_blob->id); $areaMapItInfoRepo->edit($areaMapItInfo);
if (isset($workToDo[$parentArea->getTitle()])) { foreach ($workToDo[$parentArea->getTitle()] as $newParentAreaTitle => $newParentAreaAreas) { if (count($newParentAreaAreas) > 1) { print "Make: " . $newParentAreaTitle . "\n"; foreach ($newParentAreaAreas as $area) { print " - Area: " . $area->getTitle() . "\n"; } print "\n"; } } } } sleep(10); die; $countryRepo = new \repositories\CountryRepository(); $areaRepo = new \repositories\AreaRepository(); $gb = $countryRepo->loadByTwoCharCode("GB"); $siteRepo = new \repositories\SiteRepository(); $site = $siteRepo->loadById($CONFIG->singleSiteID); foreach ($parentAreas as $parentArea) { print "In: " . $parentArea->getTitle() . "\n\n"; if (isset($workToDo[$parentArea->getTitle()])) { foreach ($workToDo[$parentArea->getTitle()] as $newParentAreaTitle => $newParentAreaAreas) { if (count($newParentAreaAreas) > 1) { print "Make: " . $newParentAreaTitle . "\n"; $newParentAreaModel = new \models\AreaModel(); $newParentAreaModel->setTitle($newParentAreaTitle); $areaRepo->create($newParentAreaModel, $parentArea, $site, $gb); foreach ($newParentAreaAreas as $area) { print " - Area: " . $area->getTitle() . "\n"; $area->setParentAreaId($newParentAreaModel->getId());
/** @return Array Roady to be made into JSON */ protected function run() { // This would be nice, but then we wouldn't delete candidates mistakenly marked as standing // $url = 'http://yournextmp.popit.mysociety.org/api/v0.1/search/persons?q=_exists_:standing_in.2015.post_id&page='; $url = 'https://yournextmp.popit.mysociety.org/api/v0.1/persons?page='; $page = 0; $morePages = true; $countryRepo = new \repositories\CountryRepository(); $areaRepo = new \repositories\AreaRepository(); $gb = $countryRepo->loadByTwoCharCode("GB"); $siteRepo = new \repositories\SiteRepository(); $site = $siteRepo->loadById($this->app['config']->singleSiteID); // TODO assumes single site! $areaMapItInfoRepo = new \com\meetyournextmp\repositories\AreaMapItInfoRepository(); $humanPopItInfoRepo = new \com\meetyournextmp\repositories\HumanPopItInfoRepository(); $humanRepo = new HumanRepository(); $areaRepo = new \repositories\AreaRepository(); while ($morePages) { ++$page; $this->logVerbose("Getting page " . $page); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url . $page); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, 'Meet Your Next MP'); $data = curl_exec($ch); $response = curl_getinfo($ch); curl_close($ch); if ($response['http_code'] != 200) { return array('result' => 'error', 'errorHTTPCode' => 200); } $dataObj = json_decode($data); foreach ($dataObj->result as $humanObj) { $this->logVerbose("Human " . $humanObj->name . " POPITID " . $humanObj->id); /** @var $humanPopItInfo HumanPopItInfoModel */ $humanPopItInfo = $humanPopItInfoRepo->getByPopItID($humanObj->id); $human = null; $standing = false; if (isset($humanObj->standing_in)) { $standingIn = get_object_vars($humanObj->standing_in); $standingIn2015 = null; if (isset($standingIn['2015'])) { $standingIn2015 = get_object_vars($standingIn['2015']); $standing = true; } $partyMemberships = isset($humanObj->party_memberships) ? get_object_vars($humanObj->party_memberships) : array(); $partyMemberships2015 = null; if (isset($partyMemberships['2015'])) { $partyMemberships2015 = get_object_vars($partyMemberships['2015']); } } if ($standing) { $facebook = null; $twitter = null; $imageURL = null; $imageProxyURL = null; $imageHumanURL = null; if (isset($humanObj->links)) { foreach ($humanObj->links as $linkData) { $linkDataArray = get_object_vars($linkData); if ($linkDataArray['note'] == 'facebook page') { $facebook = $linkDataArray['url']; } } } if (isset($humanObj->contact_details)) { foreach ($humanObj->contact_details as $linkData) { $linkDataArray = get_object_vars($linkData); if ($linkDataArray['type'] == 'twitter') { $twitter = $linkDataArray['value']; } } } if (isset($humanObj->images)) { foreach ($humanObj->images as $imageData) { $imageDataArray = get_object_vars($imageData); $imageProxyURL = $imageDataArray['proxy_url'] . '/300/0'; $imageURL = $imageDataArray['url']; $imageHumanURL = $imageProxyURL ? $imageProxyURL : $imageURL; } } $this->logVerbose(" - Standing in 2015!"); $description = "Standing in " . $standingIn2015['name'] . " for " . $partyMemberships2015['name'] . "."; $humanUrl = "https://yournextmp.com/person/" . $humanObj->id; $bits = explode('/', $standingIn2015['mapit_url']); $mapItID = $bits[4]; if ($humanPopItInfo) { $this->logVerbose(" - Found existing record"); $human = $humanRepo->loadById($humanPopItInfo->getHumanId()); if ($human->getIsDeleted()) { $this->logVerbose(" - Undeleting existing Human record"); $humanRepo->undelete($human); } if ($human->getTitle() != $humanObj->name || $description != $human->getDescription() || $human->getEmail() != $humanObj->email || $human->getUrl() != $humanUrl || $human->getTwitter() != $twitter || $human->getParty() != $partyMemberships2015['name'] || $human->getImageUrl() != $imageHumanURL) { $this->logVerbose(" - Updating existing Human record"); $human->setTitle($humanObj->name); $human->setDescription($description); $human->setEmail($humanObj->email); $human->setTwitter($twitter); $human->setParty($partyMemberships2015['name']); $human->setImageUrl($imageHumanURL); $human->setUrl($humanUrl); $humanRepo->edit($human); } $humanPopItInfo->setMapitId($mapItID); $humanPopItInfo->setName($humanObj->name); if (isset($humanObj->gender)) { $humanPopItInfo->setGenderFromString($humanObj->gender); } if (isset($humanObj->email)) { $humanPopItInfo->setEmail($humanObj->email); } $humanPopItInfo->setParty($partyMemberships2015['name']); // TODO if we reenable this we need to make sure it works for "YYYY" format to. //if (isset($humanObj->birth_date)) { // $humanPopItInfo->setBirthDate($humanObj->birth_date); //} $humanPopItInfo->setFacebook($facebook); $humanPopItInfo->setTwitter($twitter); $humanPopItInfo->setImageUrl($imageURL); $humanPopItInfo->setImageProxyUrl($imageProxyURL); $humanPopItInfoRepo->edit($humanPopItInfo); } else { $this->logVerbose(" - Adding new record"); $human = new \com\meetyournextmp\models\HumanModel(); $human->setTitle($humanObj->name); $human->setDescription($description); $human->setEmail($humanObj->email); $human->setTwitter($twitter); $human->setImageUrl($imageHumanURL); $human->setUrl($humanUrl); $human->setParty($partyMemberships2015['name']); $humanRepo->create($human, $site); $humanPopItInfo = new \com\meetyournextmp\models\HumanPopItInfoModel(); $humanPopItInfo->setHumanId($human->getId()); $humanPopItInfo->setPopitId($humanObj->id); $humanPopItInfo->setMapitId($mapItID); $humanPopItInfo->setName($humanObj->name); if (isset($humanObj->gender)) { $humanPopItInfo->setGenderFromString($humanObj->gender); } if (isset($humanObj->email)) { $humanPopItInfo->setEmail($humanObj->email); } $humanPopItInfo->setParty($partyMemberships2015['name']); // TODO if we reenable this we need to make sure it works for "YYYY" format to. //if (isset($humanObj->birth_date)) { // $humanPopItInfo->setBirthDate($humanObj->birth_date); //} $humanPopItInfo->setFacebook($facebook); $humanPopItInfo->setTwitter($twitter); $humanPopItInfo->setImageUrl($imageURL); $humanPopItInfo->setImageProxyUrl($imageProxyURL); $humanPopItInfoRepo->create($humanPopItInfo); } $areaMapItInfo = $areaMapItInfoRepo->getByMapItID($humanPopItInfo->getMapitId()); if ($areaMapItInfo) { $area = $areaRepo->loadById($areaMapItInfo->getAreaId()); if ($area) { $humanRepo->addHumanToArea($human, $area); $humanRepo->removeHumanFromAllButOneArea($human, $area); } } } else { $this->logVerbose(" - NOT Standing in 2015!"); if ($humanPopItInfo) { $human = $humanRepo->loadById($humanPopItInfo->getHumanId()); if ($human && !$human->getIsDeleted()) { $this->logVerbose(" - Deleteing "); $humanRepo->delete($human); } } } // Next Task - have any humans been merged into this one? foreach ($humanObj->versions as $humanVersion) { $idOfOldRecord = $humanVersion->data->id; if ($idOfOldRecord != $humanObj->id) { $this->logVerbose(" - found a record that was merged into this one: " . $idOfOldRecord); $humanPopItInfoOfOldRecord = $humanPopItInfoRepo->getByPopItID($idOfOldRecord); if ($humanPopItInfoOfOldRecord) { $this->logVerbose(" - found HumanPopIt for old record "); $humanOfOldRecord = $humanRepo->loadById($humanPopItInfoOfOldRecord->getHumanId()); if ($humanOfOldRecord) { $this->logVerbose(" - found Human for old record"); if (!$humanOfOldRecord->getIsDeleted()) { if ($human) { $this->logVerbose(" - marking duplicate human of old record "); $humanRepo->markDuplicate($humanOfOldRecord, $human); } else { $this->logVerbose(" - deleting human of old record"); $humanRepo->delete($humanOfOldRecord); } } else { $this->logVerbose(" - human for old record already deleted"); } } } } } } $morePages = $dataObj->has_more; } return array('result' => 'ok'); }
function testParentChildSearches() { $this->addCountriesToTestDB(); 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()); $countryRepo = new \repositories\CountryRepository(); $gb = $countryRepo->loadByTwoCharCode("GB"); $areaParent = new \models\AreaModel(); $areaParent->setTitle("Parent"); $areaCentre = new \models\AreaModel(); $areaCentre->setTitle("Centre"); $areaChild = new \models\AreaModel(); $areaChild->setTitle("Child"); $areaRepo = new \repositories\AreaRepository(); $areaRepo->create($areaParent, null, $site, $gb); $areaRepo->create($areaCentre, $areaParent, $site, $gb); $areaRepo->create($areaChild, $areaCentre, $site, $gb); $areaRepo->buildCacheAreaHasParent($areaParent); $areaRepo->buildCacheAreaHasParent($areaCentre); $areaRepo->buildCacheAreaHasParent($areaChild); $commentParent = new \org\openacalendar\comments\models\AreaCommentModel(); $commentParent->setTitle("Parent"); $commentCentre = new \org\openacalendar\comments\models\AreaCommentModel(); $commentCentre->setTitle("Centre"); $commentChild = new \org\openacalendar\comments\models\AreaCommentModel(); $commentChild->setTitle("Child"); $areaCommentRepo = new \org\openacalendar\comments\repositories\AreaCommentRepository(); TimeSource::mock(2014, 5, 1, 7, 0, 0); $areaCommentRepo->create($commentParent, $areaParent, $user); TimeSource::mock(2014, 5, 1, 7, 0, 1); $areaCommentRepo->create($commentCentre, $areaCentre, $user); TimeSource::mock(2014, 5, 1, 7, 0, 2); $areaCommentRepo->create($commentChild, $areaChild, $user); ##################################### Test Centre $areaCommentRepoBuilder = new \org\openacalendar\comments\repositories\builders\AreaCommentRepositoryBuilder(); $areaCommentRepoBuilder->setArea($areaCentre, false, false); $areaComments = $areaCommentRepoBuilder->fetchAll(); $this->assertEquals(1, count($areaComments)); $this->assertEquals($commentCentre->getId(), $areaComments[0]->getId()); ##################################### Test Centre + Parents $areaCommentRepoBuilder = new \org\openacalendar\comments\repositories\builders\AreaCommentRepositoryBuilder(); $areaCommentRepoBuilder->setArea($areaCentre, true, false); $areaComments = $areaCommentRepoBuilder->fetchAll(); $this->assertEquals(2, count($areaComments)); $this->assertEquals($commentParent->getId(), $areaComments[0]->getId()); $this->assertEquals($commentCentre->getId(), $areaComments[1]->getId()); ##################################### Test Centre + Children $areaCommentRepoBuilder = new \org\openacalendar\comments\repositories\builders\AreaCommentRepositoryBuilder(); $areaCommentRepoBuilder->setArea($areaCentre, false, true); $areaComments = $areaCommentRepoBuilder->fetchAll(); $this->assertEquals(2, count($areaComments)); $this->assertEquals($commentCentre->getId(), $areaComments[0]->getId()); $this->assertEquals($commentChild->getId(), $areaComments[1]->getId()); ##################################### Test Centre + Parents + Children $areaCommentRepoBuilder = new \org\openacalendar\comments\repositories\builders\AreaCommentRepositoryBuilder(); $areaCommentRepoBuilder->setArea($areaCentre, true, true); $areaComments = $areaCommentRepoBuilder->fetchAll(); $this->assertEquals(3, count($areaComments)); $this->assertEquals($commentParent->getId(), $areaComments[0]->getId()); $this->assertEquals($commentCentre->getId(), $areaComments[1]->getId()); $this->assertEquals($commentChild->getId(), $areaComments[2]->getId()); ##################################### Test Child + Children $areaCommentRepoBuilder = new \org\openacalendar\comments\repositories\builders\AreaCommentRepositoryBuilder(); $areaCommentRepoBuilder->setArea($areaChild, false, true); $areaComments = $areaCommentRepoBuilder->fetchAll(); $this->assertEquals(1, count($areaComments)); $this->assertEquals($commentChild->getId(), $areaComments[0]->getId()); }
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)); }