Open Journal Systems  3.3.0
PKPNotificationManagerTest.php
1 <?php
2 
18 import('lib.pkp.tests.PKPTestCase');
19 import('lib.pkp.classes.notification.PKPNotificationManager');
20 import('lib.pkp.classes.mail.MailTemplate');
21 
22 define('NOTIFICATION_ID', 1);
23 
25 
26  private $notificationMgr;
27 
31  public function testGetNotificationMessage() {
32  $notification = $this->getTrivialNotification();
33  $notification->setType(NOTIFICATION_TYPE_REVIEW_ASSIGNMENT);
34 
35  $requestDummy = $this->getMockBuilder(PKPRequest::class)->getMock();
36  $result = $this->notificationMgr->getNotificationMessage($requestDummy, $notification);
37 
38  $this->assertEquals('##notification.type.reviewAssignment##', $result);
39  }
40 
45  function testCreateNotification($notification, $notificationParams = array()) {
46  $notificationMgrStub = $this->getMgrStubForCreateNotificationTests();
47  $this->injectNotificationDaoMock($notification);
48 
49  if (!empty($notificationParams)) {
50  $this->injectNotificationSettingsDaoMock($notificationParams);
51  }
52 
53  $result = $this->exerciseCreateNotification($notificationMgrStub, $notification, $notificationParams);
54 
55  $this->assertEquals($notification, $result);
56  }
57 
62  $trivialNotification = $this->getTrivialNotification();
63 
64  $blockedNotificationTypes = array($trivialNotification->getType());
65  $notificationMgrStub = $this->getMgrStubForCreateNotificationTests($blockedNotificationTypes);
66 
67  $result = $this->exerciseCreateNotification($notificationMgrStub, $trivialNotification);
68 
69  $this->assertEquals(null, $result);
70  }
71 
76  function testCreateNotificationEmailed($notification, $notificationParams = array()) {
77  $nonTrivialNotification = $notification;
78 
79  // Make the notification non trivial.
80  $nonTrivialNotification->setLevel(NOTIFICATION_LEVEL_NORMAL);
81 
82  // Setup any assoc type and id that have content definition in notification manager,
83  // so we can check it later when sending the email.
84  $nonTrivialNotification->setType(NOTIFICATION_TYPE_NEW_ANNOUNCEMENT);
85  $nonTrivialNotification->setAssocType(ASSOC_TYPE_ANNOUNCEMENT);
86 
87  $fixtureObjects = $this->getFixtureCreateNotificationSendEmail($nonTrivialNotification);
88  list($notificationMgrStub, $requestStub) = $fixtureObjects;
89  $this->injectNotificationDaoMock($nonTrivialNotification);
90 
91  if (!empty($notificationParams)) {
92  $this->injectNotificationSettingsDaoMock($notificationParams);
93  }
94 
95  $result = $this->exerciseCreateNotification($notificationMgrStub, $nonTrivialNotification, $notificationParams, $requestStub);
96 
97  $this->assertEquals($nonTrivialNotification, $result);
98  }
99 
104  // Trivial notifications should never be emailed.
105  $trivialNotification = $this->getTrivialNotification();
106  $emailedNotificationTypes = array($trivialNotification->getType());
107 
108  $notificationMgrStub = $this->getMgrStubForCreateNotificationTests(array(), $emailedNotificationTypes, array('sendNotificationEmail'));
109  // Make sure the sendNotificationEmail method will never be called.
110  $notificationMgrMock = $notificationMgrStub;
111  $notificationMgrMock->expects($this->never())
112  ->method('sendNotificationEmail');
113 
114  $this->injectNotificationDaoMock($trivialNotification);
115 
116  $result = $this->exerciseCreateNotification($notificationMgrMock, $trivialNotification);
117 
118  $this->assertEquals($trivialNotification, $result);
119  }
120 
125  public function testCreateTrivialNotification($notification, $notificationParams = array()) {
126  $trivialNotification = $notification;
127  // Adapt the notification to the expected result.
128  $trivialNotification->unsetData('assocId');
129  $trivialNotification->unsetData('assocType');
130  $trivialNotification->setType(NOTIFICATION_TYPE_SUCCESS);
131 
132  $this->injectNotificationDaoMock($trivialNotification);
133  if (!empty($notificationParams)) {
134  $this->injectNotificationSettingsDaoMock($notificationParams);
135  }
136 
137  $result = $this->notificationMgr->createTrivialNotification($trivialNotification->getUserId());
138 
139  $this->assertEquals($trivialNotification, $result);
140  }
141 
149  $trivialNotification = $this->getTrivialNotification();
150  $notificationParams = array('param1' => 'param1Value');
151  $data = array();
152 
153  $data[] = array($trivialNotification);
154  $data[] = array($trivialNotification, $notificationParams);
155 
156  return $data;
157  }
158 
159  //
160  // Protected methods.
161  //
165  protected function getMockedDAOs() {
166  return array('NotificationDAO', 'NotificationSettingsDAO', 'UserDAO');
167  }
168 
169  protected function setUp() : void {
170  parent::setUp();
171 
172  $this->notificationMgr = new PKPNotificationManager();
173  }
174 
175  //
176  // Helper methods.
177  //
187  private function exerciseCreateNotification($notificationMgr, $notificationToCreate, $notificationToCreateParams = array(), $request = null) {
188  if (is_null($request)) {
189  $request = $this->getMockBuilder(PKPRequest::class)->getMock();
190  }
191 
192  return $notificationMgr->createNotification(
193  $request,
194  $notificationToCreate->getUserId(),
195  $notificationToCreate->getType(),
196  $notificationToCreate->getContextId(),
197  $notificationToCreate->getAssocType(),
198  $notificationToCreate->getAssocId(),
199  $notificationToCreate->getLevel(),
200  $notificationToCreateParams);
201  }
209  private function getFixtureCreateNotificationSendEmail($expectedNotification) {
210  // Add the notification type to the emailed notifications set.
211  $emailedNotifications = array($expectedNotification->getType());
212  $notificationMgrStub = $this->getMgrStubForCreateNotificationTests(array(), $emailedNotifications, array('getMailTemplate'));
213 
214  // Stub a PKPRequest object.
215  $requestStub = $this->getMockBuilder(PKPRequest::class)
216  ->setMethods(array('getSite', 'getContext', 'getUserVar'))
217  ->getMock();
218 
219  // Some site, user and notification data are required for composing the email.
220  // Retrieve/define them so we can check later.
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';
229 
230  // Build a test user object.
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);
237 
238  // Get the user full name to check.
239  $userFullName = $testUser->getFullName();
240 
241  // Stub context.
243  $contextDao = $application->getContextDAO();
244  $contextStub = $this->getMockBuilder(get_class($contextDao->newDataObject()))
245  ->setMethods(array('getLocalizedName', 'getContactName', 'getContactEmail'))
246  ->getMock();
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));
256 
257  // Inject context stub into our request stub.
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));
264  Registry::set('request', $requestStub);
265 
266  // Stub site.
267  $siteStub = $this->getMockBuilder(Site::class)
268  ->setMethods(array('getLocalizedContactName', 'getLocalizedTitle', 'getLocalizedContactEmail'))
269  ->getMock();
270 
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));
280 
281  // Inject site stub into our request stub.
282  $requestStub->expects($this->any())
283  ->method('getSite')
284  ->will($this->returnValue($siteStub));
285 
286  // Mock MailTemplate class so we can verify
287  // notification manager interaction with it. Avoid
288  // calling the mail template original constructor.
289  $mailTemplateMock = $this->getMockBuilder(MailTemplate::class)
290  ->setMethods(array('setReplyTo', 'addRecipient', 'assignParams', 'send'))
291  ->setConstructorArgs(array(null, 'en_US', $contextStub))
292  ->getMock();
293 
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())
304  ->method('send')
305  ->will($this->returnValue(true));
306 
307  // Inject our MailTemplate mock in notification manager.
308  $notificationMgrStub->expects($this->any())
309  ->method('getMailTemplate')
310  ->will($this->returnValue($mailTemplateMock));
311 
312  // Register a UserDao stub to return the test user.
313  $userDaoStub = $this->getMockBuilder(UserDAO::class)
314  ->setMethods(array('getById'))
315  ->getMock();
316  $userDaoStub->expects($this->any())
317  ->method('getById')
318  ->will($this->returnValue($testUser));
319  DAORegistry::registerDAO('UserDAO', $userDaoStub);
320 
321  return array($notificationMgrStub, $requestStub);
322  }
323 
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')))
341  ->getMock();
342 
343  $notificationMgrStub->expects($this->any())
344  ->method('getUserBlockedNotifications')
345  ->will($this->returnValue($blockedNotifications));
346 
347  $notificationMgrStub->expects($this->any())
348  ->method('getEmailedNotifications')
349  ->will($this->returnValue($emailedNotifications));
350 
351  $notificationMgrStub->expects($this->any())
352  ->method('getNotificationUrl')
353  ->will($this->returnValue('anyNotificationUrl'));
354 
355  return $notificationMgrStub;
356  }
357 
363  private function injectNotificationDaoMock($notification) {
364  $notificationDaoMock = $this->getMockBuilder(NotificationDAO::class)
365  ->setMethods(array('insertObject'))
366  ->getMock();
367  $notificationDaoMock->expects($this->once())
368  ->method('insertObject')
369  ->with($this->equalTo($notification))
370  ->will($this->returnValue(NOTIFICATION_ID));
371 
372  DAORegistry::registerDAO('NotificationDAO', $notificationDaoMock);
373  }
374 
379  private function injectNotificationSettingsDaoMock($notificationParams) {
380  // Mock NotificationSettingsDAO.
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)));
387 
388  // Inject notification settings DAO mock.
389  DAORegistry::registerDAO('NotificationSettingsDAO', $notificationSettingsDaoMock);
390  }
391 
396  private function getTrivialNotification() {
397  $notificationDao = DAORegistry::getDAO('NotificationDAO');
398  $notification = $notificationDao->newDataObject();
399  $anyTestInteger = 1;
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);
406 
407  return $notification;
408  }
409 }
410 
411 
PKPNotificationManagerTest\getMockedDAOs
getMockedDAOs()
Definition: PKPNotificationManagerTest.php:165
$application
$application
Definition: index.php:65
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
Seboettg\Collection\current
current()
Definition: ArrayListTrait.php:53
PKPNotificationManagerTest\testCreateTrivialNotification
testCreateTrivialNotification($notification, $notificationParams=array())
Definition: PKPNotificationManagerTest.php:125
GuzzleHttp\Promise\any
any($promises)
Definition: guzzlehttp/promises/src/functions.php:293
PKPNotificationManagerTest\testCreateNotificationEmailed
testCreateNotificationEmailed($notification, $notificationParams=array())
Definition: PKPNotificationManagerTest.php:76
PKPTestCase
Class that implements functionality common to all PKP unit test cases.
Definition: PKPTestCase.inc.php:27
Registry\set
static set($key, &$value)
Definition: Registry.inc.php:53
PKPNotificationManager
Class for Notification Manager.
Definition: PKPNotificationManager.inc.php:20
DAORegistry\registerDAO
static registerDAO($name, $dao)
Definition: DAORegistry.inc.php:40
PKPNotificationManagerTest
Tests for the PKPNotificationManager class.
Definition: PKPNotificationManagerTest.php:24
PKPNotificationManagerTest\testGetNotificationMessage
testGetNotificationMessage()
Definition: PKPNotificationManagerTest.php:31
PKPNotificationManagerTest\testCreateNotificationTrivialNotEmailed
testCreateNotificationTrivialNotEmailed()
Definition: PKPNotificationManagerTest.php:103
PKPNotificationManagerTest\trivialNotificationDataProvider
trivialNotificationDataProvider()
Definition: PKPNotificationManagerTest.php:148
PKPNotificationManagerTest\testCreateNotificationBlocked
testCreateNotificationBlocked()
Definition: PKPNotificationManagerTest.php:61
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
User
Basic class describing users existing in the system.
Definition: User.inc.php:23
PKPNotificationManagerTest\setUp
setUp()
Definition: PKPNotificationManagerTest.php:169
PKPNotificationManagerTest\testCreateNotification
testCreateNotification($notification, $notificationParams=array())
Definition: PKPNotificationManagerTest.php:45