Use \OCP\IUserManager instead of static getDisplayName()

This commit is contained in:
Joas Schilling 2015-03-23 10:58:58 +01:00
Родитель 3c4d4ac615
Коммит e1d8fde672
3 изменённых файлов: 27 добавлений и 26 удалений

Просмотреть файл

@ -118,7 +118,8 @@ class Application extends App {
$server->getDateTimeFormatter(), $server->getDateTimeFormatter(),
$server->getDatabaseConnection(), $server->getDatabaseConnection(),
$c->query('DataHelper'), $c->query('DataHelper'),
$server->getMailer() $server->getMailer(),
$server->getUserManager()
); );
}); });

Просмотреть файл

@ -25,6 +25,8 @@ namespace OCA\Activity;
use OCP\IDateTimeFormatter; use OCP\IDateTimeFormatter;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Mail\IMailer; use OCP\Mail\IMailer;
use OCP\Util; use OCP\Util;
@ -63,15 +65,18 @@ class MailQueueHandler {
* @param IDBConnection $connection * @param IDBConnection $connection
* @param DataHelper $dataHelper * @param DataHelper $dataHelper
* @param IMailer $mailer * @param IMailer $mailer
* @param IUserManager $userManager
*/ */
public function __construct(IDateTimeFormatter $dateFormatter, public function __construct(IDateTimeFormatter $dateFormatter,
IDBConnection $connection, IDBConnection $connection,
DataHelper $dataHelper, DataHelper $dataHelper,
IMailer $mailer) { IMailer $mailer,
IUserManager $userManager) {
$this->dateFormatter = $dateFormatter; $this->dateFormatter = $dateFormatter;
$this->connection = $connection; $this->connection = $connection;
$this->dataHelper = $dataHelper; $this->dataHelper = $dataHelper;
$this->mailer = $mailer; $this->mailer = $mailer;
$this->userManager = $userManager;
} }
/** /**
@ -185,15 +190,20 @@ class MailQueueHandler {
/** /**
* Send a notification to one user * Send a notification to one user
* *
* @param string $user Username of the recipient * @param string $userName Username of the recipient
* @param string $email Email address of the recipient * @param string $email Email address of the recipient
* @param string $lang Selected language of the recipient * @param string $lang Selected language of the recipient
* @param string $timezone Selected timezone of the recipient * @param string $timezone Selected timezone of the recipient
* @param array $mailData Notification data we send to the user * @param array $mailData Notification data we send to the user
*/ */
public function sendEmailToUser($user, $email, $lang, $timezone, $mailData) { public function sendEmailToUser($userName, $email, $lang, $timezone, $mailData) {
$user = $this->userManager->get($userName);
if (!$user instanceof IUser) {
return;
}
$l = $this->getLanguage($lang); $l = $this->getLanguage($lang);
$this->dataHelper->setUser($user); $this->dataHelper->setUser($userName);
$this->dataHelper->setL10n($l); $this->dataHelper->setL10n($l);
$activityList = array(); $activityList = array();
@ -213,7 +223,7 @@ class MailQueueHandler {
} }
$alttext = new \OCP\Template('activity', 'email.notification', ''); $alttext = new \OCP\Template('activity', 'email.notification', '');
$alttext->assign('username', \OCP\User::getDisplayName($user)); $alttext->assign('username', $user->getDisplayName());
$alttext->assign('timeframe', $this->getLangForApproximatedTimeFrame($mailData[0]['amq_timestamp'])); $alttext->assign('timeframe', $this->getLangForApproximatedTimeFrame($mailData[0]['amq_timestamp']));
$alttext->assign('activities', $activityList); $alttext->assign('activities', $activityList);
$alttext->assign('owncloud_installation', \OC_Helper::makeURLAbsolute('/')); $alttext->assign('owncloud_installation', \OC_Helper::makeURLAbsolute('/'));
@ -221,7 +231,7 @@ class MailQueueHandler {
$emailText = $alttext->fetchPage(); $emailText = $alttext->fetchPage();
$message = $this->mailer->createMessage(); $message = $this->mailer->createMessage();
$message->setTo([$email => \OCP\User::getDisplayName($user)]); $message->setTo([$email => $user->getDisplayName()]);
$message->setSubject((string) $l->t('Activity notification')); $message->setSubject((string) $l->t('Activity notification'));
$message->setPlainBody($emailText); $message->setPlainBody($emailText);
$message->setFrom([$this->getSenderData('email') => $this->getSenderData('name')]); $message->setFrom([$this->getSenderData('email') => $this->getSenderData('name')]);

Просмотреть файл

@ -46,7 +46,6 @@ class MailQueueHandlerTest extends TestCase {
$app = $this->getUniqueID('MailQueueHandlerTest'); $app = $this->getUniqueID('MailQueueHandlerTest');
$this->userManager = $this->getMock('OCP\IUserManager'); $this->userManager = $this->getMock('OCP\IUserManager');
$this->registerUserManager($this->userManager);
$query = \OCP\DB::prepare('INSERT INTO `*PREFIX*activity_mq` ' $query = \OCP\DB::prepare('INSERT INTO `*PREFIX*activity_mq` '
. ' (`amq_appid`, `amq_subject`, `amq_subjectparams`, `amq_affecteduser`, `amq_timestamp`, `amq_type`, `amq_latest_send`) ' . ' (`amq_appid`, `amq_subject`, `amq_subjectparams`, `amq_affecteduser`, `amq_timestamp`, `amq_type`, `amq_latest_send`) '
@ -72,7 +71,8 @@ class MailQueueHandlerTest extends TestCase {
$this->getMockBuilder('\OCA\Activity\DataHelper') $this->getMockBuilder('\OCA\Activity\DataHelper')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(), ->getMock(),
$this->mailer $this->mailer,
$this->userManager
); );
} }
@ -80,24 +80,9 @@ class MailQueueHandlerTest extends TestCase {
$query = \OCP\DB::prepare('DELETE FROM `*PREFIX*activity_mq` WHERE `amq_timestamp` < 200'); $query = \OCP\DB::prepare('DELETE FROM `*PREFIX*activity_mq` WHERE `amq_timestamp` < 200');
$query->execute(); $query->execute();
$this->restoreUserManager();
parent::tearDown(); parent::tearDown();
} }
protected function registerUserManager($userManager) {
$this->oldUserManager = \OC::$server->getUserManager();
\OC::$server->registerService('UserManager', function () use ($userManager) {
return $userManager;
});
}
protected function restoreUserManager() {
$oldUserManager = $this->oldUserManager;
\OC::$server->registerService('UserManager', function () use ($oldUserManager) {
return $oldUserManager;
});
}
public function getAffectedUsersData() public function getAffectedUsersData()
{ {
return [ return [
@ -171,14 +156,19 @@ class MailQueueHandlerTest extends TestCase {
->willReturn($userDisplayName); ->willReturn($userDisplayName);
$this->userManager->expects($this->any()) $this->userManager->expects($this->any())
->method('get') ->method('get')
->with($user) ->willReturnMap([
->willReturn($userObject); [$user, $userObject],
[$user . $user, null],
]);
$users = $this->mailQueueHandler->getAffectedUsers(1, $maxTime); $users = $this->mailQueueHandler->getAffectedUsers(1, $maxTime);
$this->assertEquals([$user], $users); $this->assertEquals([$user], $users);
$items = $this->mailQueueHandler->getItemsForUsers($users, $maxTime); $items = $this->mailQueueHandler->getItemsForUsers($users, $maxTime);
$this->assertArrayHasKey($user, $items); $this->assertArrayHasKey($user, $items);
$this->mailQueueHandler->sendEmailToUser($user, $email, 'en', 'UTC', $items[$user]); $this->mailQueueHandler->sendEmailToUser($user, $email, 'en', 'UTC', $items[$user]);
// Invalid user, no object no email
$this->mailQueueHandler->sendEmailToUser($user . $user, $email, 'en', 'UTC', $items[$user]);
} }
/** /**