activitymanager.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Thomas Müller <deepdiver@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. *
  8. */
  9. class Test_ActivityManager extends \Test\TestCase {
  10. /** @var \OC\ActivityManager */
  11. private $activityManager;
  12. protected function setUp() {
  13. parent::setUp();
  14. $this->activityManager = new \OC\ActivityManager();
  15. $this->activityManager->registerExtension(function() {
  16. return new NoOpExtension();
  17. });
  18. $this->activityManager->registerExtension(function() {
  19. return new SimpleExtension();
  20. });
  21. }
  22. public function testNotificationTypes() {
  23. $result = $this->activityManager->getNotificationTypes('en');
  24. $this->assertTrue(is_array($result));
  25. $this->assertEquals(2, sizeof($result));
  26. }
  27. public function testDefaultTypes() {
  28. $result = $this->activityManager->getDefaultTypes('stream');
  29. $this->assertTrue(is_array($result));
  30. $this->assertEquals(1, sizeof($result));
  31. $result = $this->activityManager->getDefaultTypes('email');
  32. $this->assertTrue(is_array($result));
  33. $this->assertEquals(0, sizeof($result));
  34. }
  35. public function testTypeIcon() {
  36. $result = $this->activityManager->getTypeIcon('NT1');
  37. $this->assertEquals('icon-nt-one', $result);
  38. $result = $this->activityManager->getTypeIcon('NT2');
  39. $this->assertEquals('', $result);
  40. }
  41. public function testTranslate() {
  42. $result = $this->activityManager->translate('APP0', '', '', array(), false, false, 'en');
  43. $this->assertEquals('Stupid translation', $result);
  44. $result = $this->activityManager->translate('APP1', '', '', array(), false, false, 'en');
  45. $this->assertFalse($result);
  46. }
  47. public function testGetSpecialParameterList() {
  48. $result = $this->activityManager->getSpecialParameterList('APP0', '');
  49. $this->assertEquals(array(0 => 'file', 1 => 'username'), $result);
  50. $result = $this->activityManager->getSpecialParameterList('APP1', '');
  51. $this->assertFalse($result);
  52. }
  53. public function testGroupParameter() {
  54. $result = $this->activityManager->getGroupParameter(array());
  55. $this->assertEquals(5, $result);
  56. }
  57. public function testNavigation() {
  58. $result = $this->activityManager->getNavigation();
  59. $this->assertEquals(4, sizeof($result['apps']));
  60. $this->assertEquals(2, sizeof($result['top']));
  61. }
  62. public function testIsFilterValid() {
  63. $result = $this->activityManager->isFilterValid('fv01');
  64. $this->assertTrue($result);
  65. $result = $this->activityManager->isFilterValid('InvalidFilter');
  66. $this->assertFalse($result);
  67. }
  68. public function testFilterNotificationTypes() {
  69. $result = $this->activityManager->filterNotificationTypes(array('NT0', 'NT1', 'NT2', 'NT3'), 'fv01');
  70. $this->assertTrue(is_array($result));
  71. $this->assertEquals(3, sizeof($result));
  72. $result = $this->activityManager->filterNotificationTypes(array('NT0', 'NT1', 'NT2', 'NT3'), 'InvalidFilter');
  73. $this->assertTrue(is_array($result));
  74. $this->assertEquals(4, sizeof($result));
  75. }
  76. public function testQueryForFilter() {
  77. // Register twice, to test the created sql part
  78. $this->activityManager->registerExtension(function() {
  79. return new SimpleExtension();
  80. });
  81. $result = $this->activityManager->getQueryForFilter('fv01');
  82. $this->assertEquals(
  83. array(
  84. ' and ((`app` = ? and `message` like ?) or (`app` = ? and `message` like ?))',
  85. array('mail', 'ownCloud%', 'mail', 'ownCloud%')
  86. ), $result
  87. );
  88. $result = $this->activityManager->getQueryForFilter('InvalidFilter');
  89. $this->assertEquals(array(null, null), $result);
  90. }
  91. }
  92. class SimpleExtension implements \OCP\Activity\IExtension {
  93. public function getNotificationTypes($languageCode) {
  94. return array('NT1', 'NT2');
  95. }
  96. public function getDefaultTypes($method) {
  97. if ($method === 'stream') {
  98. return array('DT0');
  99. }
  100. return array();
  101. }
  102. public function getTypeIcon($type) {
  103. if ($type === 'NT1') {
  104. return 'icon-nt-one';
  105. }
  106. return '';
  107. }
  108. public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
  109. if ($app === 'APP0') {
  110. return "Stupid translation";
  111. }
  112. return false;
  113. }
  114. public function getSpecialParameterList($app, $text) {
  115. if ($app === 'APP0') {
  116. return array(0 => 'file', 1 => 'username');
  117. }
  118. return false;
  119. }
  120. public function getGroupParameter($activity) {
  121. return 5;
  122. }
  123. public function getNavigation() {
  124. return array(
  125. 'apps' => array('nav1', 'nav2', 'nav3', 'nav4'),
  126. 'top' => array('top1', 'top2')
  127. );
  128. }
  129. public function isFilterValid($filterValue) {
  130. if ($filterValue === 'fv01') {
  131. return true;
  132. }
  133. return false;
  134. }
  135. public function filterNotificationTypes($types, $filter) {
  136. if ($filter === 'fv01') {
  137. unset($types[0]);
  138. }
  139. return $types;
  140. }
  141. public function getQueryForFilter($filter) {
  142. if ($filter === 'fv01') {
  143. return array('`app` = ? and `message` like ?', array('mail', 'ownCloud%'));
  144. }
  145. return false;
  146. }
  147. }
  148. class NoOpExtension implements \OCP\Activity\IExtension {
  149. public function getNotificationTypes($languageCode) {
  150. return false;
  151. }
  152. public function getDefaultTypes($method) {
  153. return false;
  154. }
  155. public function getTypeIcon($type) {
  156. return false;
  157. }
  158. public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
  159. return false;
  160. }
  161. public function getSpecialParameterList($app, $text) {
  162. return false;
  163. }
  164. public function getGroupParameter($activity) {
  165. return false;
  166. }
  167. public function getNavigation() {
  168. return false;
  169. }
  170. public function isFilterValid($filterValue) {
  171. return false;
  172. }
  173. public function filterNotificationTypes($types, $filter) {
  174. return false;
  175. }
  176. public function getQueryForFilter($filter) {
  177. return false;
  178. }
  179. }