SubscriptionsServiceTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Elgg\Notifications;
  3. class SubscriptionsServiceTest extends \PHPUnit_Framework_TestCase {
  4. public function setUp() {
  5. $this->containerGuid = 42;
  6. // mock \ElggObject that has a container guid
  7. $object = $this->getMock(
  8. '\ElggObject',
  9. array('getContainerGUID'),
  10. array(),
  11. '',
  12. false);
  13. $object->expects($this->any())
  14. ->method('getContainerGUID')
  15. ->will($this->returnValue($this->containerGuid));
  16. // mock event that holds the mock object
  17. $this->event = $this->getMock(
  18. '\Elgg\Notifications\Event',
  19. array('getObject'),
  20. array(),
  21. '',
  22. false);
  23. $this->event->expects($this->any())
  24. ->method('getObject')
  25. ->will($this->returnValue($object));
  26. $this->db = $this->getMock('\Elgg\Database',
  27. array('getData', 'getTablePrefix', 'sanitizeString'),
  28. array(),
  29. '',
  30. false
  31. );
  32. $this->db->expects($this->any())
  33. ->method('getTablePrefix')
  34. ->will($this->returnValue('elgg_'));
  35. $this->db->expects($this->any())
  36. ->method('sanitizeString')
  37. ->will($this->returnArgument(0));
  38. // Event class has dependency on elgg_get_logged_in_user_guid()
  39. _elgg_services()->setValue('session', \ElggSession::getMock());
  40. }
  41. public function testGetSubscriptionsWithNoMethodsRegistered() {
  42. $service = new \Elgg\Notifications\SubscriptionsService($this->db);
  43. $this->assertEquals(array(), $service->getSubscriptions($this->event));
  44. }
  45. public function testGetSubscriptionsWithBadObject() {
  46. $this->event = $this->getMock(
  47. '\Elgg\Notifications\Event',
  48. array('getObject'),
  49. array(),
  50. '',
  51. false);
  52. $this->event->expects($this->any())
  53. ->method('getObject')
  54. ->will($this->returnValue(null));
  55. $service = new \Elgg\Notifications\SubscriptionsService($this->db);
  56. $service->methods = array('one', 'two');
  57. $this->assertEquals(array(), $service->getSubscriptions($this->event));
  58. }
  59. public function testQueryGenerationForRetrievingSubscriptionRelationships() {
  60. $methods = array('apples', 'bananas');
  61. $query = "SELECT guid_one AS guid, GROUP_CONCAT(relationship SEPARATOR ',') AS methods
  62. FROM elgg_entity_relationships
  63. WHERE guid_two = $this->containerGuid AND
  64. relationship IN ('notifyapples','notifybananas') GROUP BY guid_one";
  65. $this->db->expects($this->once())
  66. ->method('getData')
  67. ->with($this->equalTo($query))
  68. ->will($this->returnValue(array()));
  69. $service = new \Elgg\Notifications\SubscriptionsService($this->db);
  70. $service->methods = $methods;
  71. $this->assertEquals(array(), $service->getSubscriptions($this->event));
  72. }
  73. public function testGetSubscriptionsWithProperInput() {
  74. $methods = array('apples', 'bananas');
  75. $queryResult = array(
  76. $this->createObjectFromArray(array('guid' => '22', 'methods' => 'notifyapples')),
  77. $this->createObjectFromArray(array('guid' => '567', 'methods' => 'notifybananas,notifyapples')),
  78. );
  79. $subscriptions = array(
  80. 22 => array('apples'),
  81. 567 => array('bananas', 'apples'),
  82. );
  83. $this->db->expects($this->once())
  84. ->method('getData')
  85. ->will($this->returnValue($queryResult));
  86. $service = new \Elgg\Notifications\SubscriptionsService($this->db);
  87. $service->methods = $methods;
  88. $this->assertEquals($subscriptions, $service->getSubscriptions($this->event));
  89. }
  90. public function testGetSubscriptionsForContainerWithNoMethodsRegistered() {
  91. $container_guid = 132;
  92. $service = new \Elgg\Notifications\SubscriptionsService($this->db);
  93. $this->assertEquals(array(), $service->getSubscriptionsForContainer($container_guid));
  94. }
  95. public function testGetSubscriptionsForContainerWithProperInput() {
  96. $container_guid = 132;
  97. $methods = array('apples', 'bananas');
  98. $queryResult = array(
  99. $this->createObjectFromArray(array('guid' => '22', 'methods' => 'notifyapples')),
  100. $this->createObjectFromArray(array('guid' => '567', 'methods' => 'notifybananas,notifyapples')),
  101. );
  102. $subscriptions = array(
  103. 22 => array('apples'),
  104. 567 => array('bananas', 'apples'),
  105. );
  106. $this->db->expects($this->once())
  107. ->method('getData')
  108. ->will($this->returnValue($queryResult));
  109. $service = new \Elgg\Notifications\SubscriptionsService($this->db);
  110. $service->methods = $methods;
  111. $this->assertEquals($subscriptions, $service->getSubscriptionsForContainer($container_guid));
  112. }
  113. protected function createObjectFromArray(array $data) {
  114. $obj = new \stdClass();
  115. foreach ($data as $key => $value) {
  116. $obj->$key = $value;
  117. }
  118. return $obj;
  119. }
  120. }