/** * @dataProvider translationNotActivityAppData */ public function testTranslationNotActivityApp($text, $params, $stripPath, $highlightParams, $expected) { $activityLanguage = \OCP\Util::getL10N('activity', 'en'); $activityManager = new ActivityManager($this->getMock('OCP\\IRequest'), $this->getMock('OCP\\IUserSession'), $this->getMock('OCP\\IConfig')); $dataHelper = new DataHelper($activityManager, new ParameterHelper($activityManager, $this->getMockBuilder('OCP\\IUserManager')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('\\OCP\\IURLGenerator')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('OCP\\Contacts\\IManager')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('OC\\Files\\View')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('OCP\\IConfig')->disableOriginalConstructor()->getMock(), $activityLanguage, 'test'), $activityLanguage); $this->assertEquals($expected, (string) $dataHelper->translation('activity', $text, $params, $stripPath, $highlightParams)); }
/** * @dataProvider getTypeIconData */ public function testGetTypeIcon($type, $expected) { $manager = $this->getMock('\\OCP\\Activity\\IManager'); $manager->expects($this->any())->method('getTypeIcon')->willReturn(''); $dataHelper = new DataHelper($manager, new ParameterHelper($manager, new View(''), $this->getMockBuilder('OCP\\IConfig')->disableOriginalConstructor()->getMock(), Util::getL10N('activity')), Util::getL10N('activity')); $this->assertEquals($expected, $dataHelper->getTypeIcon($type)); }
protected function setUp() { parent::setUp(); $app = $this->getUniqueID('MailQueueHandlerTest'); $this->userManager = $this->getMock('OCP\\IUserManager'); $connection = \OC::$server->getDatabaseConnection(); $query = $connection->prepare('INSERT INTO `*PREFIX*activity_mq` ' . ' (`amq_appid`, `amq_subject`, `amq_subjectparams`, `amq_affecteduser`, `amq_timestamp`, `amq_type`, `amq_latest_send`) ' . ' VALUES(?, ?, ?, ?, ?, ?, ?)'); $query->execute(array($app, 'Test data', 'Param1', 'user1', 150, 'phpunit', 152)); $query->execute(array($app, 'Test data', 'Param1', 'user1', 150, 'phpunit', 153)); $query->execute(array($app, 'Test data', 'Param1', 'user2', 150, 'phpunit', 150)); $query->execute(array($app, 'Test data', 'Param1', 'user2', 150, 'phpunit', 151)); $query->execute(array($app, 'Test data', 'Param1', 'user3', 150, 'phpunit', 154)); $query->execute(array($app, 'Test data', 'Param1', 'user3', 150, 'phpunit', 155)); $event = $this->getMockBuilder('OCP\\Activity\\IEvent')->disableOriginalConstructor()->getMock(); $event->expects($this->any())->method('setApp')->willReturnSelf(); $event->expects($this->any())->method('setType')->willReturnSelf(); $event->expects($this->any())->method('setAffectedUser')->willReturnSelf(); $event->expects($this->any())->method('setTimestamp')->willReturnSelf(); $event->expects($this->any())->method('setSubject')->willReturnSelf(); $this->activityManager = $this->getMockBuilder('OCP\\Activity\\IManager')->disableOriginalConstructor()->getMock(); $this->activityManager->expects($this->any())->method('generateEvent')->willReturn($event); $this->dataHelper = $this->getMockBuilder('OCA\\Activity\\DataHelper')->disableOriginalConstructor()->getMock(); $this->dataHelper->expects($this->any())->method('getParameters')->willReturn([]); $this->message = $this->getMockBuilder('OC\\Mail\\Message')->disableOriginalConstructor()->getMock(); $this->mailer = $this->getMock('OCP\\Mail\\IMailer'); $this->mailer->expects($this->any())->method('createMessage')->willReturn($this->message); $this->mailQueueHandler = new MailQueueHandler($this->getMock('\\OCP\\IDateTimeFormatter'), $connection, $this->dataHelper, $this->mailer, $this->getMockBuilder('\\OCP\\IURLGenerator')->disableOriginalConstructor()->getMock(), $this->userManager, $this->activityManager); }
/** * Send a notification to one user * * @param string $userName Username of the recipient * @param string $email Email address of the recipient * @param string $lang Selected language of the recipient * @param string $timezone Selected timezone of the recipient * @param int $maxTime */ public function sendEmailToUser($userName, $email, $lang, $timezone, $maxTime) { $user = $this->userManager->get($userName); if (!$user instanceof IUser) { return; } list($mailData, $skippedCount) = $this->getItemsForUser($userName, $maxTime); $l = $this->getLanguage($lang); $this->dataHelper->setUser($userName); $this->dataHelper->setL10n($l); $activityList = array(); foreach ($mailData as $activity) { $relativeDateTime = $this->dateFormatter->formatDateTimeRelativeDay($activity['amq_timestamp'], 'long', 'medium', new \DateTimeZone($timezone), $l); $activityList[] = array($this->dataHelper->translation($activity['amq_appid'], $activity['amq_subject'], $this->dataHelper->getParameters($activity['amq_subjectparams'])), $relativeDateTime); } $alttext = new Template('activity', 'email.notification', ''); $alttext->assign('username', $user->getDisplayName()); $alttext->assign('activities', $activityList); $alttext->assign('skippedCount', $skippedCount); $alttext->assign('owncloud_installation', $this->urlGenerator->getAbsoluteURL('/')); $alttext->assign('overwriteL10N', $l); $emailText = $alttext->fetchPage(); $message = $this->mailer->createMessage(); $message->setTo([$email => $user->getDisplayName()]); $message->setSubject((string) $l->t('Activity notification')); $message->setPlainBody($emailText); $message->setFrom([$this->getSenderData('email') => $this->getSenderData('name')]); $this->mailer->send($message); }
/** * Get the prepared activities * * @return array translated activities ready for use */ public function getActivities() { $this->closeOpenGroup(); $return = array(); foreach ($this->activities as $activity) { $activity = $this->dataHelper->formatStrings($activity, 'subject'); $activity = $this->dataHelper->formatStrings($activity, 'message'); $activity['typeicon'] = $this->activityManager->getTypeIcon($activity['type']); $return[] = $activity; } return $return; }
/** * @dataProvider dataGetActivities * * @param array $activities * @param array $expected */ public function testGetActivities(array $activities, array $expected) { $numActivities = sizeof($activities); $helper = $this->getHelper(['closeOpenGroup']); $helper->expects($this->once())->method('closeOpenGroup'); $this->invokePrivate($helper, 'activities', [$activities]); $this->dataHelper->expects($this->exactly($numActivities * 2))->method('formatStrings')->willReturnArgument(0); $this->activityManager->expects($this->exactly($numActivities))->method('getTypeIcon')->willReturnCallback(function ($type) { return $type . '-icon'; }); $this->activityManager->expects($this->exactly($numActivities + 1))->method('setFormattingObject'); $this->assertSame($expected, $helper->getActivities()); }
/** * Get the prepared activities * * @return array translated activities ready for use */ public function getActivities() { $this->closeOpenGroup(); $return = array(); foreach ($this->activities as $activity) { $this->activityManager->setFormattingObject($activity['object_type'], $activity['object_id']); $activity = $this->dataHelper->formatStrings($activity, 'subject'); $activity = $this->dataHelper->formatStrings($activity, 'message'); foreach ($activity['subjectparams'] as $i => $param) { /** @var IParameter $param */ $activity['subjectparams'][$i] = $param->getParameterInfo(); } foreach ($activity['messageparams'] as $i => $param) { /** @var IParameter $param */ $activity['messageparams'][$i] = $param->getParameterInfo(); } $activity['typeicon'] = $this->activityManager->getTypeIcon($activity['type']); $return[] = $activity; } $this->activityManager->setFormattingObject('', 0); $this->activities = []; return $return; }
/** * Send a notification to one user * * @param string $user Username of the recipient * @param string $email Email address of the recipient * @param string $lang Selected language of the recipient * @param string $timezone Selected timezone of the recipient * @param array $mailData Notification data we send to the user */ public function sendEmailToUser($user, $email, $lang, $timezone, $mailData) { $l = $this->getLanguage($lang); $dataHelper = new DataHelper(\OC::$server->getActivityManager(), new ParameterHelper(\OC::$server->getActivityManager(), new \OC\Files\View(''), \OC::$server->getConfig(), $l), $l); $activityList = array(); foreach ($mailData as $activity) { $relativeDateTime = $this->dateFormatter->formatDateTimeRelativeDay($activity['amq_timestamp'], 'long', 'medium', new \DateTimeZone($timezone), $l); $activityList[] = array($dataHelper->translation($activity['amq_appid'], $activity['amq_subject'], unserialize($activity['amq_subjectparams'])), $relativeDateTime); } $alttext = new \OCP\Template('activity', 'email.notification', ''); $alttext->assign('username', \OCP\User::getDisplayName($user)); $alttext->assign('timeframe', $this->getLangForApproximatedTimeFrame($mailData[0]['amq_timestamp'])); $alttext->assign('activities', $activityList); $alttext->assign('owncloud_installation', \OC_Helper::makeURLAbsolute('/')); $alttext->assign('overwriteL10N', $l); $emailText = $alttext->fetchPage(); try { \OCP\Util::sendMail($email, \OCP\User::getDisplayName($user), $l->t('Activity notification'), $emailText, $this->getSenderData('email'), $this->getSenderData('name')); } catch (\Exception $e) { \OCP\Util::writeLog('Activity', 'A problem occurred while sending the e-mail. Please revisit your settings.', \OCP\Util::ERROR); } }