public function testBranchedWithMovedAndAddedAndRemovedInstances()
 {
     $newSeriesId = 10910;
     $newInstanceId1 = 2827;
     $newInstanceId2 = 2828;
     $userId = 10;
     $resourceId = 11;
     $title = "new title";
     $description = "new description";
     $allowParticipation = true;
     $dateRange = DateRange::Create('2010-01-10 05:30:00', '2010-01-10 08:30:00', 'UTC');
     $existingInstance1 = new TestReservation('123', $dateRange->AddDays(1));
     $existingInstance2 = new TestReservation('223', $dateRange->AddDays(2));
     $newInstance1 = new TestReservation('323', $dateRange->AddDays(3));
     $newInstance2 = new TestReservation('423', $dateRange->AddDays(4));
     $removedInstance1 = new TestReservation('523', $dateRange->AddDays(5));
     $removedInstance2 = new TestReservation('623', $dateRange->AddDays(6));
     $currentInstance = new TestReservation('999', $dateRange);
     $builder = new ExistingReservationSeriesBuilder();
     $builder->WithEvent(new InstanceAddedEvent($newInstance1, $builder->series));
     $builder->WithEvent(new InstanceAddedEvent($newInstance2, $builder->series));
     $builder->WithEvent(new InstanceRemovedEvent($removedInstance1, $builder->series));
     $builder->WithEvent(new InstanceRemovedEvent($removedInstance2, $builder->series));
     $builder->WithEvent(new SeriesBranchedEvent($builder->series));
     $builder->WithRequiresNewSeries(true);
     $builder->WithInstance($existingInstance1);
     $builder->WithInstance($existingInstance2);
     $builder->WithCurrentInstance($currentInstance);
     $existingReservation = $builder->BuildTestVersion();
     $existingReservation->Update($userId, new FakeBookableResource($resourceId), $title, $description, new FakeUserSession());
     $existingReservation->AllowParticipation($allowParticipation);
     $expectedRepeat = $existingReservation->RepeatOptions();
     $this->db->_ExpectedInsertIds[] = $newSeriesId;
     $this->db->_ExpectedInsertIds[] = $newInstanceId1;
     $this->db->_ExpectedInsertIds[] = $newInstanceId2;
     $this->repository->Update($existingReservation);
     $addNewSeriesCommand = new AddReservationSeriesCommand(Date::Now(), $title, $description, $expectedRepeat->RepeatType(), $expectedRepeat->ConfigurationString(), ReservationTypes::Reservation, ReservationStatus::Created, $userId, $allowParticipation);
     $updateReservationCommand1 = $this->GetUpdateReservationCommand($newSeriesId, $existingInstance1);
     $updateReservationCommand2 = $this->GetUpdateReservationCommand($newSeriesId, $existingInstance2);
     $updateReservationCommand3 = $this->GetUpdateReservationCommand($newSeriesId, $currentInstance);
     $addReservationCommand1 = $this->GetAddReservationCommand($newSeriesId, $newInstance1);
     $addReservationCommand2 = $this->GetAddReservationCommand($newSeriesId, $newInstance2);
     $insertReservationUser1 = $this->GetAddUserCommand($newInstanceId1, $userId, ReservationUserLevel::OWNER);
     $insertReservationUser2 = $this->GetAddUserCommand($newInstanceId2, $userId, ReservationUserLevel::OWNER);
     $removeReservationCommand1 = new RemoveReservationCommand($removedInstance1->ReferenceNumber());
     $removeReservationCommand2 = new RemoveReservationCommand($removedInstance2->ReferenceNumber());
     $this->assertEquals($addNewSeriesCommand, $this->db->_Commands[0]);
     $commands = $this->db->GetCommandsOfType('UpdateReservationCommand');
     $this->assertEquals(3, count($commands));
     $addUserCommands = $this->db->GetCommandsOfType('AddReservationUserCommand');
     $this->assertEquals(2, count($addUserCommands));
     $this->assertTrue(in_array($updateReservationCommand1, $this->db->_Commands));
     $this->assertTrue(in_array($updateReservationCommand2, $this->db->_Commands));
     $this->assertTrue(in_array($updateReservationCommand3, $this->db->_Commands));
     $this->assertTrue(in_array($addReservationCommand1, $this->db->_Commands));
     $this->assertTrue(in_array($addReservationCommand2, $this->db->_Commands));
     $this->assertTrue(in_array($insertReservationUser1, $this->db->_Commands));
     $this->assertTrue(in_array($insertReservationUser2, $this->db->_Commands));
     $this->assertTrue(in_array($removeReservationCommand1, $this->db->_Commands));
     $this->assertTrue(in_array($removeReservationCommand2, $this->db->_Commands));
 }
 public function testWhenReservationStartConstraintIsViolated()
 {
     $this->fakeConfig->SetSectionKey(ConfigSection::RESERVATION, ConfigKeys::RESERVATION_START_TIME_CONSTRAINT, ReservationStartTimeConstraint::FUTURE);
     $referenceNumber = 'abc';
     $currentUserId = 1029;
     $builder = new ExistingReservationSeriesBuilder();
     $instance = new TestReservation($referenceNumber, new DateRange(Date::Now()->AddMinutes(-1), Date::Now()->AddMinutes(30)), null);
     $builder->WithCurrentInstance($instance);
     $series = $builder->Build();
     $this->page->expects($this->once())->method('GetResponseType')->will($this->returnValue('json'));
     $this->page->expects($this->once())->method('GetInvitationAction')->will($this->returnValue(InvitationAction::Join));
     $this->page->expects($this->once())->method('GetInvitationReferenceNumber')->will($this->returnValue($referenceNumber));
     $this->page->expects($this->once())->method('GetUserId')->will($this->returnValue($currentUserId));
     $this->reservationRepo->expects($this->once())->method('LoadByReferenceNumber')->with($this->equalTo($referenceNumber))->will($this->returnValue($series));
     $this->page->expects($this->once())->method('DisplayResult')->with($this->stringContains('ParticipationNotAllowed'));
     $this->presenter->PageLoad();
 }
 public function testDeletesWholeSeriesWhenAdminRegardlessOfDate()
 {
     $r1 = new TestReservation();
     $r1->SetReservationDate(TestDateRange::CreateWithDays(-1));
     $r2 = new TestReservation();
     $r2->SetReservationDate(TestDateRange::CreateWithDays(-2));
     $builder = new ExistingReservationSeriesBuilder();
     $builder->WithCurrentInstance($r1);
     $builder->WithInstance($r2);
     $series = $builder->Build();
     $series->ApplyChangesTo(SeriesUpdateScope::FullSeries);
     $series->Delete($this->admin);
     $events = $series->GetEvents();
     $this->assertEquals(1, count($events));
     $this->assertTrue(in_array(new SeriesDeletedEvent($series), $events));
 }
 public function testWhenDeletingAnInstanceItDoesNotCount()
 {
     $ref1 = 'ref1';
     $ref2 = 'ref2';
     $ref3 = 'ref3';
     $duration = new QuotaDurationDay();
     $limit = new QuotaLimitCount(1);
     $quota = new Quota(1, $duration, $limit);
     $r1start = Date::Parse('2011-04-03 1:30', $this->tz);
     $r1End = Date::Parse('2011-04-03 2:30', $this->tz);
     $r2start = Date::Parse('2011-04-04 1:30', $this->tz);
     $r2End = Date::Parse('2011-04-04 2:30', $this->tz);
     $existing1 = new TestReservation($ref1, new DateRange($r1start, $r1End), 1);
     $deleted = new TestReservation($ref2, new DateRange($r2start, $r2End), 2);
     $new = new TestReservation($ref3, new DateRange($r2start, $r2End), 3);
     $builder = new ExistingReservationSeriesBuilder();
     $builder->WithCurrentInstance($existing1)->WithInstance($deleted)->WithInstance($new);
     $series = $builder->BuildTestVersion();
     $series->RemoveInstance($deleted);
     $res1 = new ReservationItemView($ref1, $r1start, $r1End, '', $series->ResourceId(), $existing1->ReservationId());
     $res2 = new ReservationItemView($ref2, $r1start, $r1End, '', $series->ResourceId(), $deleted->ReservationId());
     $res3 = new ReservationItemView($ref3, $r2start, $r2End, '', $series->ResourceId(), $new->ReservationId());
     $reservations = array($res1, $res2, $res3);
     $this->SearchReturns($reservations);
     $exceeds = $quota->ExceedsQuota($series, $this->user, $this->schedule, $this->reservationViewRepository);
     $this->assertFalse($exceeds);
 }
 public function testRemovesParticipationFromCurrentInstance()
 {
     $userId = 1;
     $r1 = new TestReservation();
     $r1->WithParticipant($userId);
     $r1->SetReservationId(100);
     $r2 = new TestReservation();
     $r2->WithParticipant($userId);
     $r2->SetReservationId(100);
     $builder = new ExistingReservationSeriesBuilder();
     $builder->WithCurrentInstance($r1);
     $builder->WithInstance($r2);
     $series = $builder->Build();
     $series->CancelInstanceParticipation($userId);
     $events = $series->GetEvents();
     $this->assertContains($userId, $r1->RemovedParticipants());
     $this->assertEmpty($r2->RemovedInvitees());
     $this->assertTrue(in_array(new InstanceUpdatedEvent($r1, $series), $events));
     $this->assertFalse(in_array(new InstanceUpdatedEvent($r2, $series), $events));
 }