NotificationsServiceTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace Elgg\Notifications;
  3. class NotificationsServiceTest extends \PHPUnit_Framework_TestCase {
  4. protected $session;
  5. public function setUp() {
  6. $this->hooks = new \Elgg\PluginHooksService();
  7. $this->queue = new \Elgg\Queue\MemoryQueue();
  8. $dbMock = $this->getMockBuilder('\Elgg\Database')
  9. ->disableOriginalConstructor()
  10. ->getMock();
  11. $this->sub = new \Elgg\Notifications\SubscriptionsService($dbMock);
  12. $this->session = \ElggSession::getMock();
  13. // Event class has dependency on elgg_get_logged_in_user_guid()
  14. _elgg_services()->setValue('session', $this->session);
  15. }
  16. public function testRegisterEvent() {
  17. $service = new \Elgg\Notifications\NotificationsService($this->sub, $this->queue, $this->hooks, $this->session);
  18. $service->registerEvent('foo', 'bar');
  19. $events = array(
  20. 'foo' => array(
  21. 'bar' => array('create')
  22. )
  23. );
  24. $this->assertEquals($events, $service->getEvents());
  25. $service->registerEvent('foo', 'bar', array('test'));
  26. $events['foo']['bar'] = array('create', 'test');
  27. $this->assertEquals($events, $service->getEvents());
  28. $service->registerEvent('foo', 'bar');
  29. $this->assertEquals($events, $service->getEvents());
  30. }
  31. public function testUnregisterEvent() {
  32. $service = new \Elgg\Notifications\NotificationsService($this->sub, $this->queue, $this->hooks, $this->session);
  33. $service->registerEvent('foo', 'bar');
  34. $this->assertTrue($service->unregisterEvent('foo', 'bar'));
  35. $events = array(
  36. 'foo' => array()
  37. );
  38. $this->assertEquals($events, $service->getEvents());
  39. $this->assertFalse($service->unregisterEvent('foo', 'bar'));
  40. }
  41. public function testRegisterMethod() {
  42. $service = new \Elgg\Notifications\NotificationsService($this->sub, $this->queue, $this->hooks, $this->session);
  43. $service->registerMethod('foo');
  44. $methods = array('foo' => 'foo');
  45. $this->assertEquals($methods, $service->getMethods());
  46. }
  47. public function testUnregisterMethod() {
  48. $service = new \Elgg\Notifications\NotificationsService($this->sub, $this->queue, $this->hooks, $this->session);
  49. $service->registerMethod('foo');
  50. $this->assertTrue($service->unregisterMethod('foo'));
  51. $this->assertEquals(array(), $service->getMethods());
  52. $this->assertFalse($service->unregisterMethod('foo'));
  53. }
  54. public function testEnqueueEvent() {
  55. $service = new \Elgg\Notifications\NotificationsService($this->sub, $this->queue, $this->hooks, $this->session);
  56. $service->registerEvent('object', 'bar');
  57. $object = new \ElggObject();
  58. $object->subtype = 'bar';
  59. $service->enqueueEvent('create', 'object', $object);
  60. $event = new \Elgg\Notifications\Event($object, 'create');
  61. $this->assertEquals($event, $this->queue->dequeue());
  62. $this->assertNull($this->queue->dequeue());
  63. // unregistered action type
  64. $service->enqueueEvent('null', 'object', $object);
  65. $this->assertNull($this->queue->dequeue());
  66. // unregistered object type
  67. $service->enqueueEvent('create', 'object', new \ElggObject());
  68. $this->assertNull($this->queue->dequeue());
  69. }
  70. public function testEnqueueEventHook() {
  71. $object = new \ElggObject();
  72. $object->subtype = 'bar';
  73. $params = array('action' => 'create', 'object' => $object);
  74. $mock = $this->getMock('\Elgg\PluginHooksService', array('trigger'));
  75. $mock->expects($this->once())
  76. ->method('trigger')
  77. ->with('enqueue', 'notification', $params, true);
  78. $service = new \Elgg\Notifications\NotificationsService($this->sub, $this->queue, $mock, $this->session);
  79. $service->registerEvent('object', 'bar');
  80. $service->enqueueEvent('create', 'object', $object);
  81. }
  82. public function testStoppingEnqueueEvent() {
  83. $mock = $this->getMock('\Elgg\PluginHooksService', array('trigger'));
  84. $mock->expects($this->once())
  85. ->method('trigger')
  86. ->will($this->returnValue(false));
  87. $service = new \Elgg\Notifications\NotificationsService($this->sub, $this->queue, $mock, $this->session);
  88. $service->registerEvent('object', 'bar');
  89. $object = new \ElggObject();
  90. $object->subtype = 'bar';
  91. $service->enqueueEvent('create', 'object', $object);
  92. $this->assertNull($this->queue->dequeue());
  93. }
  94. public function testProcessQueueNoEvents() {
  95. $service = new \Elgg\Notifications\NotificationsService($this->sub, $this->queue, $this->hooks, $this->session);
  96. $this->assertEquals(0, $service->processQueue(time() + 10));
  97. }
  98. public function testProcessQueueThreeEvents() {
  99. $mock = $this->getMock(
  100. '\Elgg\Notifications\SubscriptionsService',
  101. array('getSubscriptions'),
  102. array(),
  103. '',
  104. false);
  105. $mock->expects($this->exactly(3))
  106. ->method('getSubscriptions')
  107. ->will($this->returnValue(array()));
  108. $service = new \Elgg\Notifications\NotificationsService($mock, $this->queue, $this->hooks, $this->session);
  109. $service->registerEvent('object', 'bar');
  110. $object = new \ElggObject();
  111. $object->subtype = 'bar';
  112. $service->enqueueEvent('create', 'object', $object);
  113. $service->enqueueEvent('create', 'object', $object);
  114. $service->enqueueEvent('create', 'object', $object);
  115. $this->assertEquals(3, $service->processQueue(time() + 10));
  116. }
  117. public function testProcessQueueTimesout() {
  118. $mock = $this->getMock(
  119. '\Elgg\Notifications\SubscriptionsService',
  120. array('getSubscriptions'),
  121. array(),
  122. '',
  123. false);
  124. $mock->expects($this->exactly(0))
  125. ->method('getSubscriptions');
  126. $service = new \Elgg\Notifications\NotificationsService($mock, $this->queue, $this->hooks, $this->session);
  127. $service->registerEvent('object', 'bar');
  128. $object = new \ElggObject();
  129. $object->subtype = 'bar';
  130. $service->enqueueEvent('create', 'object', $object);
  131. $service->enqueueEvent('create', 'object', $object);
  132. $service->enqueueEvent('create', 'object', $object);
  133. $this->assertEquals(0, $service->processQueue(time()));
  134. }
  135. }