public function testCanGetSpecificInstanceByDate() { $startDate = Date::Parse('2010-02-02 12:15', 'UTC'); $endDate = $startDate->AddDays(1); $dateRange = new DateRange($startDate, $endDate); $repeatOptions = $this->getMock('IRepeatOptions'); $series = ReservationSeries::Create(1, new FakeBookableResource(1), null, null, $dateRange, $repeatOptions, new FakeUserSession()); $instance = $series->CurrentInstance(); $this->assertEquals($startDate, $instance->StartDate()); $this->assertEquals($endDate, $instance->EndDate()); }
public function BuildReservation() { $userId = $this->_page->GetUserId(); $primaryResourceId = $this->_page->GetResourceId(); $resource = $this->_resourceRepository->LoadById($primaryResourceId); $title = $this->_page->GetTitle(); $description = $this->_page->GetDescription(); $roFactory = new RepeatOptionsFactory(); $repeatOptions = $roFactory->CreateFromComposite($this->_page, $this->userSession->Timezone); $duration = $this->GetReservationDuration(); /*EXPERIMENT*/ $experiment = $this->_page->getExperiment(); $reservationSeries = ReservationSeries::Create($userId, $resource, $title, $description, $duration, $repeatOptions, $this->userSession, $experiment); $resourceIds = $this->_page->GetResources(); foreach ($resourceIds as $resourceId) { if ($primaryResourceId != $resourceId) { $reservationSeries->AddResource($this->_resourceRepository->LoadById($resourceId)); } } $accessories = $this->_page->GetAccessories(); foreach ($accessories as $accessory) { $reservationSeries->AddAccessory(new ReservationAccessory($accessory->Id, $accessory->Quantity, $accessory->Name)); } $attributes = $this->_page->GetAttributes(); foreach ($attributes as $attribute) { $reservationSeries->AddAttributeValue(new AttributeValue($attribute->Id, $attribute->Value)); } $participantIds = $this->_page->GetParticipants(); $reservationSeries->ChangeParticipants($participantIds); $inviteeIds = $this->_page->GetInvitees(); $reservationSeries->ChangeInvitees($inviteeIds); $reservationSeries->AllowParticipation($this->_page->GetAllowParticipation()); $attachments = $this->_page->GetAttachments(); foreach ($attachments as $attachment) { if ($attachment != null) { if ($attachment->IsError()) { Log::Error('Error attaching file %s. %s', $attachment->OriginalName(), $attachment->Error()); } else { $att = ReservationAttachment::Create($attachment->OriginalName(), $attachment->MimeType(), $attachment->Size(), $attachment->Contents(), $attachment->Extension(), 0); $reservationSeries->AddAttachment($att); } } } if ($this->_page->HasStartReminder()) { $reservationSeries->AddStartReminder(new ReservationReminder($this->_page->GetStartReminderValue(), $this->_page->GetStartReminderInterval())); } if ($this->_page->HasEndReminder()) { $reservationSeries->AddEndReminder(new ReservationReminder($this->_page->GetEndReminderValue(), $this->_page->GetEndReminderInterval())); } return $reservationSeries; }
public function testSucceedsWhenStartAndEndTimeMatchPeriods() { $date = Date::Now(); $dates = new DateRange($date, $date); $scheduleId = 1232; $resource = new FakeBookableResource(1); $resource->SetScheduleId($scheduleId); $series = ReservationSeries::Create(1, $resource, null, null, $dates, new RepeatNone(), $this->fakeUser); $this->scheduleRepository->expects($this->once())->method('GetLayout')->with($this->equalTo($scheduleId), $this->equalTo(new ScheduleLayoutFactory($this->fakeUser->Timezone)))->will($this->returnValue($this->layout)); $period = new SchedulePeriod($date, $date); $this->layout->expects($this->at(0))->method('GetPeriod')->with($this->equalTo($series->CurrentInstance()->StartDate()))->will($this->returnValue($period)); $this->layout->expects($this->at(1))->method('GetPeriod')->with($this->equalTo($series->CurrentInstance()->EndDate()))->will($this->returnValue($period)); $result = $this->rule->Validate($series); $this->assertTrue($result->IsValid()); }
private function GetHourLongReservation($startDate, $endDate, $resourceId1 = null, $resourceId2 = null, $scheduleId = null) { $userId = 12; $resource1 = empty($resourceId1) ? 13 : $resourceId1; $resource2 = empty($resourceId2) ? 14 : $resourceId2; $schedule = empty($scheduleId) ? 1 : $scheduleId; $hourLongReservation = new DateRange($startDate, $endDate, $this->tz); $resource = new FakeBookableResource($resource1); $resource->SetScheduleId($schedule); $series = ReservationSeries::Create($userId, $resource, null, null, $hourLongReservation, new RepeatNone(), new FakeUserSession()); $series->AddResource(new FakeBookableResource($resource2)); return $series; }
private function MigrateReservations(Database $legacyDatabase, Database $currentDatabase) { $reservationsMigrated = MigrationSession::GetLastReservationRow(); $getMigratedCount = new AdHocCommand('SELECT (select count(*) from reservation_series where legacyid is not null) + (select count(*) from blackout_series where legacyid is not null ) as count'); $reader = ServiceLocator::GetDatabase()->Query($getMigratedCount); if ($row = $reader->GetRow()) { $reservationsMigrated = $row['count']; } Log::Debug('Start migrating reservations. Starting at row %s', $reservationsMigrated); $reservationRepository = new ReservationRepository(); $blackoutRepository = new BlackoutRepository(); $getLegacyReservations = new AdHocCommand("select r.resid, machid, scheduleid, start_date, end_date,\n starttime, endtime, created, modified, parentid, is_blackout, is_pending, summary, allow_participation, allow_anon_participation,\n ru.memberid\n FROM reservations r INNER JOIN reservation_users ru ON r.resid = ru.resid AND owner = 1\n ORDER BY r.resid LIMIT {$reservationsMigrated}, 100"); $getExisting = new AdHocCommand('select legacyid from reservation_series'); $reader = $currentDatabase->Query($getExisting); $knownIds = array(); while ($row = $reader->GetRow()) { $knownIds[] = $row['legacyid']; } $getLegacyReservationAccessories = new AdHocCommand('SELECT resid, resourceid from reservation_resources'); $getLegacyReservationParticipants = new AdHocCommand('SELECT resid, memberid, owner, invited FROM reservation_users WHERE (owner is null or owner = 0)'); $getAccessoryMapping = new AdHocCommand('select accessory_id, legacyid from accessories'); $getUserMapping = new AdHocCommand('select user_id, legacyid from users'); $getResourceMapping = new AdHocCommand('select resource_id, legacyid from resources'); $accessoryMapping = array(); $accessoryMappingReader = $currentDatabase->Query($getAccessoryMapping); while ($row = $accessoryMappingReader->GetRow()) { $legacyId = $row['legacyid']; $accessoryMapping[$legacyId] = $row['accessory_id']; } $userMapping = array(); $userMappingReader = $currentDatabase->Query($getUserMapping); while ($row = $userMappingReader->GetRow()) { $legacyId = $row['legacyid']; $userMapping[$legacyId] = $row['user_id']; } $resourceMapping = array(); $resourceMappingReader = $currentDatabase->Query($getResourceMapping); while ($row = $resourceMappingReader->GetRow()) { $legacyId = $row['legacyid']; $resourceMapping[$legacyId] = $row['resource_id']; } $reservationAccessories = array(); $legacyAccessoryReader = $legacyDatabase->Query($getLegacyReservationAccessories); while ($row = $legacyAccessoryReader->GetRow()) { $resId = $row['resid']; if (!array_key_exists($resId, $reservationAccessories)) { $reservationAccessories[$resId] = array(); } $reservationAccessories[$resId][] = $row['resourceid']; } $reservationParticipants = array(); $legacyParticipantReader = $legacyDatabase->Query($getLegacyReservationParticipants); while ($row = $legacyParticipantReader->GetRow()) { $resId = $row['resid']; if (!array_key_exists($resId, $reservationParticipants)) { $reservationParticipants[$resId] = array(); } $reservationParticipants[$resId][] = array('id' => $row['memberid'], 'invited' => $row['invited']); } $legacyReservationReader = $legacyDatabase->Query($getLegacyReservations); while ($row = $legacyReservationReader->GetRow()) { $legacyId = $row['resid']; if (in_array($legacyId, $knownIds)) { continue; } $date = $this->BuildDateRange($row['start_date'], $row['starttime'], $row['end_date'], $row['endtime']); $mappedUserId = $userMapping[$row['memberid']]; $mappedResourceId = $resourceMapping[$row['machid']]; if ($row['is_blackout'] == 1) { // handle blackout $blackout = BlackoutSeries::Create($mappedUserId, '', $date); $blackout->AddResourceId($mappedResourceId); $newId = $blackoutRepository->Add($blackout); $currentDatabase->Execute(new AdHocCommand("update blackout_series set legacyid = \"{$legacyId}\" where blackout_series_id = {$newId}")); } else { // handle reservation $mappedParticipantIds = array(); $mappedInviteeIds = array(); if (array_key_exists($legacyId, $reservationParticipants)) { $legacyParticipants = $reservationParticipants[$legacyId]; foreach ($legacyParticipants as $legacyParticipantId) { $userId = $userMapping[$legacyParticipantId['id']]; if (empty($legacyParticipantId['invited'])) { $mappedParticipantIds[] = $userId; } else { $mappedInviteeIds[] = $userId; } } } $mappedAccessoryList = array(); if (array_key_exists($legacyId, $reservationAccessories)) { $legacyAccessories = $reservationAccessories[$legacyId]; foreach ($legacyAccessories as $legacyAccessoryId) { if (array_key_exists($legacyAccessoryId, $accessoryMapping)) { $mappedAccessoryId = $accessoryMapping[$legacyAccessoryId]; $mappedAccessoryList[] = new ReservationAccessory($mappedAccessoryId, 1); } } } $currentUser = new UserSession($row['memberid']); $currentUser->Timezone = Configuration::Instance()->GetDefaultTimezone(); $mappedResource = new MigrateBookableResource($mappedResourceId); $reservation = ReservationSeries::Create($mappedUserId, $mappedResource, '', $row['summary'], $date, new RepeatNone(), $currentUser); foreach ($mappedAccessoryList as $accessory) { $reservation->AddAccessory($accessory); } $reservation->ChangeParticipants($mappedParticipantIds); $reservation->ChangeInvitees($mappedInviteeIds); try { $reservationRepository->Add($reservation); $newId = $reservation->SeriesId(); $currentDatabase->Execute(new AdHocCommand("update reservation_series set legacyid = \"{$legacyId}\" where series_id = {$newId}")); } catch (Exception $ex) { Log::Error('Error migrating reservation %s. Exception: %s', $legacyId, $ex); } } $reservationsMigrated++; MigrationSession::SetLastReservationRow($reservationsMigrated); } Log::Debug('Done migrating reservations (%s reservations)', $reservationsMigrated); $getLegacyCount = new AdHocCommand('select count(*) as count from reservations'); // $getMigratedCount = new AdHocCommand('SELECT // (select count(*) from reservation_series where legacyid is not null) + // (select count(*) from blackout_series where legacyid is not null ) // as count'); $progressCounts = $this->GetProgressCounts($getLegacyCount, $getMigratedCount); $this->page->SetProgress($progressCounts); Log::Debug('There are %s total legacy reservations and %s already migrated. Progress is %s', $progressCounts->LegacyCount, $progressCounts->MigratedCount, $progressCounts->PercentComplete); $this->page->SetReservationsMigrated($progressCounts->MigratedCount); MigrationSession::SetLastReservationRow($progressCounts->MigratedCount); }
$resource = $resourceRepository->LoadByContactInfo($contact_info); } elseif ($rid) { $resource = $resourceRepository->LoadByPublicId($rid); } else { header('HTTP/1.1 406 Not Acceptable', true, 406); print json_encode(array('message' => "contact_info or rid has to be set")); return; } /************************************************* date *************************************************/ $timing = DateRange::Create($starts_at, $ends_at, $tz); /************************************************* Action *************************************************/ $series = ReservationSeries::Create($user->Id(), $resource, $title, $description, $timing, new RepeatNone(), $user_session); $reservationAction = ReservationAction::Create; $pfactory = new ReservationPersistenceFactory(); $persistenceService = $pfactory->Create($reservationAction); $vfactory = new ReservationValidationFactory(); $validationService = $vfactory->Create($reservationAction, $user_session); $nfactory = new ReservationNotificationFactory(); $notificationService = $nfactory->Create($reservationAction, $user_session); # $handler = new ReservationHandler($persistenceService, $validationService, $notificationService); $validationResult = $validationService->Validate($series); $result = $validationResult->CanBeSaved(); if ($result) { try { $persistenceResult = $persistenceService->Persist($series); header('HTTP/1.1 200 Ok', true, 200); $response = array('series_id' => $series->SeriesId(), 'reference_number' => $series->CurrentInstance()->ReferenceNumber(), 'requires_approval' => $resource->GetRequiresApproval());
$reservationRepo = new ReservationRepository(); $bookedBy = new UserSession(1); $bookedBy->Timezone = 'America/Chicago'; $currentDate = Date::Now(); $i = 0; while ($i < $numberOfReservations) { $periods = $layout->GetLayout($currentDate); foreach ($periods as $period) { $howManyResources = rand(1, count($resources)); $startResource = rand(0, $howManyResources); if ($period->IsReservable()) { for ($resourceNum = $startResource; $resourceNum < $howManyResources; $resourceNum++) { $userId = getRandomUserId($users)->Id(); $resource = $resources[$resourceNum]; $date = new DateRange($period->BeginDate(), $period->EndDate(), 'America/Chicago'); $reservation = ReservationSeries::Create($userId, $resource, "load{$i}", null, $date, new RepeatNone(), $bookedBy); $reservationRepo->Add($reservation); $i++; } } } $currentDate = $currentDate->AddDays(1); } echo "Loaded {$numberOfReservations} reservations<br/>"; $stopWatch->Stop(); echo "<h5>Took " . $stopWatch->GetTotalSeconds() . " seconds</h5>"; /** * @param array|User[] $users * @return User */ function getRandomUserId($users)
public function testRepeatedDatesAreSaved() { $reservationSeriesId = 109; $reservationId = 918; $repeatId1 = 919; $repeatId3 = 921; $timezone = 'UTC'; $startUtc1 = Date::Parse('2010-02-03', $timezone); $startUtc2 = Date::Parse('2010-02-04', $timezone); $startUtc3 = Date::Parse('2010-02-05', $timezone); $endUtc1 = Date::Parse('2010-02-06', $timezone); $endUtc2 = Date::Parse('2010-02-07', $timezone); $endUtc3 = Date::Parse('2010-02-08', $timezone); $dates[] = new DateRange($startUtc1, $endUtc1, $timezone); $dates[] = new DateRange($startUtc2, $endUtc2, $timezone); $dates[] = new DateRange($startUtc3, $endUtc3, $timezone); $duration = new TestDateRange(); $repeats = $this->getMock('IRepeatOptions'); $repeats->expects($this->once())->method('GetDates')->with($this->anything())->will($this->returnValue($dates)); $userSession = new FakeUserSession(); $reservation = ReservationSeries::Create(1, new FakeBookableResource(1), null, null, $duration, $repeats, $userSession); $this->db->_ExpectedInsertIds[0] = $reservationSeriesId; $this->db->_ExpectedInsertIds[1] = $reservationId; $this->db->_ExpectedInsertIds[2] = $repeatId1; $this->db->_ExpectedInsertIds[3] = $repeatId3; $this->repository->Add($reservation); $instances = $reservation->Instances(); foreach ($instances as $instance) { $insertRepeatCommand = new AddReservationCommand($instance->StartDate()->ToUtc(), $instance->EndDate()->ToUtc(), $instance->ReferenceNumber(), $reservationSeriesId); $this->assertTrue(in_array($insertRepeatCommand, $this->db->_Commands), "command {$insertRepeatCommand} not found"); } }
public function testFirstQuotaExceeded() { $scheduleId = 971243; $timezone = 'America/New_York'; $userId = 10; $groupId1 = 8287; $groupId2 = 102; $user = new FakeUser(); $user->SetGroups(array($groupId1, $groupId2)); $schedule = new Schedule(1, null, null, null, null, $timezone); $resource = new FakeBookableResource(20); $resource->SetScheduleId($scheduleId); $series = ReservationSeries::Create($userId, $resource, null, null, new TestDateRange(), new RepeatNone(), new FakeUserSession()); $series->AddResource(new FakeBookableResource(22)); $quota1 = $this->mockQuota('IQuota'); $quota2 = $this->mockQuota('IQuota'); $quotas = array($quota1, $quota2); $this->quotaRepository->expects($this->once())->method('LoadAll')->will($this->returnValue($quotas)); $this->userRepository->expects($this->once())->method('LoadById')->with($this->equalTo($userId))->will($this->returnValue($user)); $this->scheduleRepository->expects($this->once())->method('LoadById')->with($this->equalTo($scheduleId))->will($this->returnValue($schedule)); $this->ChecksAgainstQuota($quota1, $series, $this->reservationViewRepository, $schedule, $user, true); $quota2->expects($this->never())->method('ExceedsQuota'); $rule = new QuotaRule($this->quotaRepository, $this->reservationViewRepository, $this->userRepository, $this->scheduleRepository); $result = $rule->Validate($series); $this->assertFalse($result->IsValid(), 'first quotas was exceeded'); }