18 import(
'lib.pkp.tests.PKPTestCase');
19 import(
'lib.pkp.classes.notification.PKPNotificationManager');
20 import(
'lib.pkp.classes.mail.MailTemplate');
22 define(
'NOTIFICATION_ID', 1);
26 private $notificationMgr;
32 $notification = $this->getTrivialNotification();
33 $notification->setType(NOTIFICATION_TYPE_REVIEW_ASSIGNMENT);
35 $requestDummy = $this->getMockBuilder(PKPRequest::class)->getMock();
36 $result = $this->notificationMgr->getNotificationMessage($requestDummy, $notification);
38 $this->assertEquals(
'##notification.type.reviewAssignment##', $result);
46 $notificationMgrStub = $this->getMgrStubForCreateNotificationTests();
47 $this->injectNotificationDaoMock($notification);
49 if (!empty($notificationParams)) {
50 $this->injectNotificationSettingsDaoMock($notificationParams);
53 $result = $this->exerciseCreateNotification($notificationMgrStub, $notification, $notificationParams);
55 $this->assertEquals($notification, $result);
62 $trivialNotification = $this->getTrivialNotification();
64 $blockedNotificationTypes = array($trivialNotification->getType());
65 $notificationMgrStub = $this->getMgrStubForCreateNotificationTests($blockedNotificationTypes);
67 $result = $this->exerciseCreateNotification($notificationMgrStub, $trivialNotification);
69 $this->assertEquals(
null, $result);
77 $nonTrivialNotification = $notification;
80 $nonTrivialNotification->setLevel(NOTIFICATION_LEVEL_NORMAL);
84 $nonTrivialNotification->setType(NOTIFICATION_TYPE_NEW_ANNOUNCEMENT);
85 $nonTrivialNotification->setAssocType(ASSOC_TYPE_ANNOUNCEMENT);
87 $fixtureObjects = $this->getFixtureCreateNotificationSendEmail($nonTrivialNotification);
88 list($notificationMgrStub, $requestStub) = $fixtureObjects;
89 $this->injectNotificationDaoMock($nonTrivialNotification);
91 if (!empty($notificationParams)) {
92 $this->injectNotificationSettingsDaoMock($notificationParams);
95 $result = $this->exerciseCreateNotification($notificationMgrStub, $nonTrivialNotification, $notificationParams, $requestStub);
97 $this->assertEquals($nonTrivialNotification, $result);
105 $trivialNotification = $this->getTrivialNotification();
106 $emailedNotificationTypes = array($trivialNotification->getType());
108 $notificationMgrStub = $this->getMgrStubForCreateNotificationTests(array(), $emailedNotificationTypes, array(
'sendNotificationEmail'));
110 $notificationMgrMock = $notificationMgrStub;
111 $notificationMgrMock->expects($this->never())
112 ->method(
'sendNotificationEmail');
114 $this->injectNotificationDaoMock($trivialNotification);
116 $result = $this->exerciseCreateNotification($notificationMgrMock, $trivialNotification);
118 $this->assertEquals($trivialNotification, $result);
126 $trivialNotification = $notification;
128 $trivialNotification->unsetData(
'assocId');
129 $trivialNotification->unsetData(
'assocType');
130 $trivialNotification->setType(NOTIFICATION_TYPE_SUCCESS);
132 $this->injectNotificationDaoMock($trivialNotification);
133 if (!empty($notificationParams)) {
134 $this->injectNotificationSettingsDaoMock($notificationParams);
137 $result = $this->notificationMgr->createTrivialNotification($trivialNotification->getUserId());
139 $this->assertEquals($trivialNotification, $result);
149 $trivialNotification = $this->getTrivialNotification();
150 $notificationParams = array(
'param1' =>
'param1Value');
153 $data[] = array($trivialNotification);
154 $data[] = array($trivialNotification, $notificationParams);
166 return array(
'NotificationDAO',
'NotificationSettingsDAO',
'UserDAO');
187 private function exerciseCreateNotification($notificationMgr, $notificationToCreate, $notificationToCreateParams = array(), $request =
null) {
188 if (is_null($request)) {
189 $request = $this->getMockBuilder(PKPRequest::class)->getMock();
192 return $notificationMgr->createNotification(
194 $notificationToCreate->getUserId(),
195 $notificationToCreate->getType(),
196 $notificationToCreate->getContextId(),
197 $notificationToCreate->getAssocType(),
198 $notificationToCreate->getAssocId(),
199 $notificationToCreate->getLevel(),
200 $notificationToCreateParams);
209 private function getFixtureCreateNotificationSendEmail($expectedNotification) {
211 $emailedNotifications = array($expectedNotification->getType());
212 $notificationMgrStub = $this->getMgrStubForCreateNotificationTests(array(), $emailedNotifications, array(
'getMailTemplate'));
215 $requestStub = $this->getMockBuilder(PKPRequest::class)
216 ->setMethods(array(
'getSite',
'getContext',
'getUserVar'))
221 $siteTitle =
'Site title';
222 $siteContactName =
'Site contact name';
223 $siteEmail =
'site@email.com';
224 $userFirstName =
'FirstName';
225 $userLastName =
'UserLastName';
226 $userEmail =
'user@email.com';
227 $notificationContents = $notificationMgrStub->getNotificationContents($requestStub, $expectedNotification);
228 $contextTitle =
'Context title';
231 import(
'lib.pkp.classes.user.User');
232 $testUser =
new User();
233 $testUser->setId($expectedNotification->getUserId());
234 $testUser->setGivenName($userFirstName,
'en_US');
235 $testUser->setFamilyName($userLastName,
'en_US');
236 $testUser->setEmail($userEmail);
239 $userFullName = $testUser->getFullName();
244 $contextStub = $this->getMockBuilder(get_class($contextDao->newDataObject()))
245 ->setMethods(array(
'getLocalizedName',
'getContactName',
'getContactEmail'))
247 $contextStub->expects($this->
any())
248 ->method(
'getLocalizedName')
249 ->will($this->returnValue($contextTitle));
250 $contextStub->expects($this->
any())
251 ->method(
'getContactName')
252 ->will($this->returnValue($siteContactName));
253 $contextStub->expects($this->
any())
254 ->method(
'getContactEmail')
255 ->will($this->returnValue($siteEmail));
258 $requestStub->expects($this->
any())
259 ->method(
'getContext')
260 ->will($this->returnValue($contextStub));
261 $requestStub->expects($this->
any())
262 ->method(
'getUserVar')
263 ->will($this->returnValue(
null));
267 $siteStub = $this->getMockBuilder(Site::class)
268 ->setMethods(array(
'getLocalizedContactName',
'getLocalizedTitle',
'getLocalizedContactEmail'))
271 $siteStub->expects($this->
any())
272 ->method(
'getLocalizedContactName')
273 ->will($this->returnValue($siteContactName));
274 $siteStub->expects($this->
any())
275 ->method(
'getLocalizedTitle')
276 ->will($this->returnValue($siteTitle));
277 $siteStub->expects($this->
any())
278 ->method(
'getLocalizedContactEmail')
279 ->will($this->returnValue($siteEmail));
282 $requestStub->expects($this->
any())
284 ->will($this->returnValue($siteStub));
289 $mailTemplateMock = $this->getMockBuilder(MailTemplate::class)
290 ->setMethods(array(
'setReplyTo',
'addRecipient',
'assignParams',
'send'))
291 ->setConstructorArgs(array(
null,
'en_US', $contextStub))
294 $mailTemplateMock->expects($this->
any())
295 ->method(
'setReplyTo')
296 ->with($this->equalTo($siteEmail), $this->equalTo($siteContactName));
297 $mailTemplateMock->expects($this->
any())
298 ->method(
'addRecipient')
299 ->with($this->equalTo($userEmail), $this->equalTo($userFullName));
300 $mailTemplateMock->expects($this->
any())
301 ->method(
'assignParams')
302 ->with($this->logicalAnd($this->contains($notificationContents), $this->contains($contextTitle)));
303 $mailTemplateMock->expects($this->once())
305 ->will($this->returnValue(
true));
308 $notificationMgrStub->expects($this->
any())
309 ->method(
'getMailTemplate')
310 ->will($this->returnValue($mailTemplateMock));
313 $userDaoStub = $this->getMockBuilder(UserDAO::class)
314 ->setMethods(array(
'getById'))
316 $userDaoStub->expects($this->
any())
318 ->will($this->returnValue($testUser));
321 return array($notificationMgrStub, $requestStub);
338 private function getMgrStubForCreateNotificationTests($blockedNotifications = array(), $emailedNotifications = array(), $extraOpToStub = array()) {
339 $notificationMgrStub = $this->getMockBuilder(PKPNotificationManager::class)
340 ->setMethods(array_merge($extraOpToStub, array(
'getUserBlockedNotifications',
'getEmailedNotifications',
'getNotificationUrl')))
343 $notificationMgrStub->expects($this->
any())
344 ->method(
'getUserBlockedNotifications')
345 ->will($this->returnValue($blockedNotifications));
347 $notificationMgrStub->expects($this->
any())
348 ->method(
'getEmailedNotifications')
349 ->will($this->returnValue($emailedNotifications));
351 $notificationMgrStub->expects($this->
any())
352 ->method(
'getNotificationUrl')
353 ->will($this->returnValue(
'anyNotificationUrl'));
355 return $notificationMgrStub;
363 private function injectNotificationDaoMock($notification) {
364 $notificationDaoMock = $this->getMockBuilder(NotificationDAO::class)
365 ->setMethods(array(
'insertObject'))
367 $notificationDaoMock->expects($this->once())
368 ->method(
'insertObject')
369 ->with($this->equalTo($notification))
370 ->will($this->returnValue(NOTIFICATION_ID));
379 private function injectNotificationSettingsDaoMock($notificationParams) {
381 $notificationSettingsDaoMock = $this->getMockBuilder(NotificationSettingsDAO::class)->getMock();
382 $notificationSettingsDaoMock->expects($this->
any())
383 ->method(
'updateNotificationSetting')
384 ->with($this->equalTo(NOTIFICATION_ID),
385 $this->equalTo(key($notificationParams)),
386 $this->equalTo(
current($notificationParams)));
396 private function getTrivialNotification() {
398 $notification = $notificationDao->newDataObject();
400 $notification->setUserId($anyTestInteger);
401 $notification->setType($anyTestInteger);
402 $notification->setContextId(CONTEXT_ID_NONE);
403 $notification->setAssocType($anyTestInteger);
404 $notification->setAssocId($anyTestInteger);
405 $notification->setLevel(NOTIFICATION_LEVEL_TRIVIAL);
407 return $notification;