/**
  * @dataProvider supportedMethods
  */
 public function testProcessValidDataWithTargetEntityActivity($method)
 {
     $targetEntity = new User();
     ReflectionUtil::setId($targetEntity, 123);
     $organization = new Organization();
     ReflectionUtil::setId($organization, 1);
     $targetEntity->setOrganization($organization);
     $defaultCalendar = $this->getMockBuilder('Oro\\Bundle\\CalendarBundle\\Entity\\Calendar')->disableOriginalConstructor()->getMock();
     $this->entity->setCalendar($defaultCalendar);
     $this->entityRoutingHelper->expects($this->once())->method('getEntityClassName')->will($this->returnValue(get_class($targetEntity)));
     $this->entityRoutingHelper->expects($this->once())->method('getEntityId')->will($this->returnValue($targetEntity->getId()));
     $this->entityRoutingHelper->expects($this->once())->method('getAction')->will($this->returnValue('activity'));
     $this->request->setMethod($method);
     $this->form->expects($this->once())->method('setData')->with($this->identicalTo($this->entity));
     $this->form->expects($this->once())->method('submit')->with($this->identicalTo($this->request));
     $this->form->expects($this->once())->method('isValid')->will($this->returnValue(true));
     $this->entityRoutingHelper->expects($this->once())->method('getEntityReference')->with(get_class($targetEntity), $targetEntity->getId())->will($this->returnValue($targetEntity));
     $this->activityManager->expects($this->once())->method('addActivityTarget')->with($this->identicalTo($this->entity), $this->identicalTo($targetEntity));
     $this->form->expects($this->once())->method('get')->will($this->returnValue($this->form));
     $this->form->expects($this->once())->method('getData');
     $this->om->expects($this->once())->method('persist')->with($this->identicalTo($this->entity));
     $this->om->expects($this->once())->method('flush');
     $this->assertTrue($this->handler->process($this->entity));
     $this->assertSame($defaultCalendar, $this->entity->getCalendar());
 }
Esempio n. 2
0
 /**
  * Process form
  *
  * @param  CalendarEvent $entity
  * @throws \LogicException
  *
  * @return bool  True on successful processing, false otherwise
  */
 public function process(CalendarEvent $entity)
 {
     if (!$entity->getCalendar()) {
         if ($this->securityFacade->getLoggedUser() && $this->securityFacade->getOrganization()) {
             /** @var Calendar $defaultCalendar */
             $defaultCalendar = $this->manager->getRepository('OroCalendarBundle:Calendar')->findDefaultCalendar($this->securityFacade->getLoggedUser()->getId(), $this->securityFacade->getOrganization()->getId());
             $entity->setCalendar($defaultCalendar);
         } else {
             throw new \LogicException('Current user did not define');
         }
     }
     $this->form->setData($entity);
     if (in_array($this->request->getMethod(), array('POST', 'PUT'))) {
         $this->form->submit($this->request);
         if ($this->form->isValid()) {
             $targetEntityClass = $this->entityRoutingHelper->getEntityClassName($this->request);
             if ($targetEntityClass) {
                 $targetEntityId = $this->entityRoutingHelper->getEntityId($this->request);
                 $targetEntity = $this->entityRoutingHelper->getEntityReference($targetEntityClass, $targetEntityId);
                 $action = $this->entityRoutingHelper->getAction($this->request);
                 if ($action === 'activity') {
                     $this->activityManager->addActivityTarget($entity, $targetEntity);
                 }
                 if ($action === 'assign' && $targetEntity instanceof User && $targetEntityId !== $this->securityFacade->getLoggedUserId()) {
                     /** @var Calendar $defaultCalendar */
                     $defaultCalendar = $this->manager->getRepository('OroCalendarBundle:Calendar')->findDefaultCalendar($targetEntity->getId(), $targetEntity->getOrganization()->getId());
                     $entity->setCalendar($defaultCalendar);
                 }
             }
             $this->onSuccess($entity);
             return true;
         }
     }
     return false;
 }
Esempio n. 3
0
 /**
  * Links an event with a calendar by its alias and id
  *
  * @param CalendarEvent $event
  * @param string        $calendarAlias
  * @param int           $calendarId
  *
  * @throws \LogicException
  * @throws ForbiddenException
  */
 public function setCalendar(CalendarEvent $event, $calendarAlias, $calendarId)
 {
     if ($calendarAlias === Calendar::CALENDAR_ALIAS) {
         $calendar = $event->getCalendar();
         if (!$calendar || $calendar->getId() !== $calendarId) {
             $event->setCalendar($this->findCalendar($calendarId));
         }
     } elseif (in_array($calendarAlias, [SystemCalendar::CALENDAR_ALIAS, SystemCalendar::PUBLIC_CALENDAR_ALIAS])) {
         $systemCalendar = $this->findSystemCalendar($calendarId);
         //@TODO: Added permission verification
         if ($systemCalendar->isPublic() && !$this->calendarConfig->isPublicCalendarEnabled()) {
             throw new ForbiddenException('Public calendars are disabled.');
         }
         if (!$systemCalendar->isPublic() && !$this->calendarConfig->isSystemCalendarEnabled()) {
             throw new ForbiddenException('System calendars are disabled.');
         }
         $event->setSystemCalendar($systemCalendar);
     } else {
         throw new \LogicException(sprintf('Unexpected calendar alias: "%s". CalendarId: %d.', $calendarAlias, $calendarId));
     }
 }
Esempio n. 4
0
 public function testSetCalendar()
 {
     $calendar = new Calendar();
     $systemCalendar = new SystemCalendar();
     $obj = new CalendarEvent();
     $this->assertNull($obj->getCalendar());
     $this->assertNull($obj->getSystemCalendar());
     $obj->setCalendar($calendar);
     $this->assertSame($calendar, $obj->getCalendar());
     $this->assertNull($obj->getSystemCalendar());
     $obj->setSystemCalendar($systemCalendar);
     $this->assertNull($obj->getCalendar());
     $this->assertSame($systemCalendar, $obj->getSystemCalendar());
     $obj->setCalendar($calendar);
     $this->assertSame($calendar, $obj->getCalendar());
     $this->assertNull($obj->getSystemCalendar());
     $obj->setCalendar(null);
     $this->assertNull($obj->getCalendar());
     $obj->setSystemCalendar($systemCalendar);
     $this->assertNull($obj->getCalendar());
     $this->assertSame($systemCalendar, $obj->getSystemCalendar());
     $obj->setSystemCalendar(null);
     $this->assertNull($obj->getCalendar());
     $this->assertNull($obj->getSystemCalendar());
 }
 /**
  * Get calendar owner
  *
  * @param CalendarEvent $activityEntity
  * @return null|User
  */
 protected function getOwner($activityEntity)
 {
     /** @var $activityEntity CalendarEvent */
     if ($activityEntity->getCalendar()) {
         return $activityEntity->getCalendar()->getOwner();
     }
     return null;
 }
 /**
  * @param CalendarEvent $entity
  *
  * @throws \LogicException
  */
 protected function ensureCalendarSet(CalendarEvent $entity)
 {
     if ($entity->getCalendar() || $entity->getSystemCalendar()) {
         return;
     }
     if (!$this->securityFacade->getLoggedUser() || !$this->securityFacade->getOrganization()) {
         throw new \LogicException('Both logged in user and organization must be defined.');
     }
     /** @var Calendar $defaultCalendar */
     $defaultCalendar = $this->manager->getRepository('OroCalendarBundle:Calendar')->findDefaultCalendar($this->securityFacade->getLoggedUser()->getId(), $this->securityFacade->getOrganization()->getId());
     $entity->setCalendar($defaultCalendar);
 }
 /**
  * @param CalendarEvent $event
  *
  * @return array
  */
 protected function serializeCalendarEvent(CalendarEvent $event)
 {
     return ['id' => $event->getId(), 'title' => $event->getTitle(), 'description' => $event->getDescription(), 'start' => $event->getStart(), 'end' => $event->getEnd(), 'allDay' => $event->getAllDay(), 'backgroundColor' => $event->getBackgroundColor(), 'createdAt' => $event->getCreatedAt(), 'updatedAt' => $event->getUpdatedAt(), 'invitationStatus' => $event->getInvitationStatus(), 'parentEventId' => $event->getParent() ? $event->getParent()->getId() : null, 'calendar' => $event->getCalendar() ? $event->getCalendar()->getId() : null];
 }